@lobu/runtime-device 24.1.1 → 24.2.0

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 (158) hide show
  1. package/dist/component.json +1 -1
  2. package/dist/dependencies.bun.lock +26 -28
  3. package/npm-shrinkwrap.json +155 -172
  4. package/package.json +1 -1
  5. package/vendor/cli/dist/bundled-skills/lobu/SKILL.md +14 -5
  6. package/vendor/cli/dist/chrome-extension/accessibility-tree.js +834 -0
  7. package/vendor/cli/dist/chrome-extension/ambient-context.js +53 -0
  8. package/vendor/cli/dist/chrome-extension/auth-refresh.js +167 -0
  9. package/vendor/cli/dist/chrome-extension/automation-run.js +13 -0
  10. package/vendor/cli/dist/chrome-extension/background.js +1155 -0
  11. package/vendor/cli/dist/chrome-extension/bridge.js +140 -0
  12. package/vendor/cli/dist/chrome-extension/browser-origin-policy.js +62 -0
  13. package/vendor/cli/dist/chrome-extension/browser-recovery.js +260 -0
  14. package/vendor/cli/dist/chrome-extension/browser-url-sanitizer.js +284 -0
  15. package/vendor/cli/dist/chrome-extension/completion-response.js +12 -0
  16. package/vendor/cli/dist/chrome-extension/config.js +144 -0
  17. package/vendor/cli/dist/chrome-extension/connector-manifests/chrome.json +1516 -0
  18. package/vendor/cli/dist/chrome-extension/connector-manifests/chrome_bookmarks.json +84 -0
  19. package/vendor/cli/dist/chrome-extension/connector-manifests/chrome_downloads.json +86 -0
  20. package/vendor/cli/dist/chrome-extension/connector-manifests/chrome_history.json +78 -0
  21. package/vendor/cli/dist/chrome-extension/connector-manifests.js +1767 -0
  22. package/vendor/cli/dist/chrome-extension/console-capture.js +627 -0
  23. package/vendor/cli/dist/chrome-extension/control-activity.js +70 -0
  24. package/vendor/cli/dist/chrome-extension/debugger-lease.js +439 -0
  25. package/vendor/cli/dist/chrome-extension/feed-listener.js +488 -0
  26. package/vendor/cli/dist/chrome-extension/feed-transaction.js +48 -0
  27. package/vendor/cli/dist/chrome-extension/feeds.js +632 -0
  28. package/vendor/cli/dist/chrome-extension/focus-tab.js +25 -0
  29. package/vendor/cli/dist/chrome-extension/fonts/dm-mono-latin-500-normal.woff2 +0 -0
  30. package/vendor/cli/dist/chrome-extension/fonts/dm-sans-latin-400-normal.woff2 +0 -0
  31. package/vendor/cli/dist/chrome-extension/fonts/dm-sans-latin-600-normal.woff2 +0 -0
  32. package/vendor/cli/dist/chrome-extension/heartbeat.js +140 -0
  33. package/vendor/cli/dist/chrome-extension/icons/icon-128-rec.png +0 -0
  34. package/vendor/cli/dist/chrome-extension/icons/icon-128.png +0 -0
  35. package/vendor/cli/dist/chrome-extension/icons/icon-16-rec.png +0 -0
  36. package/vendor/cli/dist/chrome-extension/icons/icon-16.png +0 -0
  37. package/vendor/cli/dist/chrome-extension/icons/icon-32-rec.png +0 -0
  38. package/vendor/cli/dist/chrome-extension/icons/icon-32.png +0 -0
  39. package/vendor/cli/dist/chrome-extension/icons/icon-48-rec.png +0 -0
  40. package/vendor/cli/dist/chrome-extension/icons/icon-48.png +0 -0
  41. package/vendor/cli/dist/chrome-extension/lens-page.js +135 -0
  42. package/vendor/cli/dist/chrome-extension/lens-target.js +20 -0
  43. package/vendor/cli/dist/chrome-extension/manifest.json +76 -0
  44. package/vendor/cli/dist/chrome-extension/network-intercept.js +657 -0
  45. package/vendor/cli/dist/chrome-extension/page-activation.js +120 -0
  46. package/vendor/cli/dist/chrome-extension/page-dialogs.js +125 -0
  47. package/vendor/cli/dist/chrome-extension/page-url.js +11 -0
  48. package/vendor/cli/dist/chrome-extension/panel-session.js +184 -0
  49. package/vendor/cli/dist/chrome-extension/poll-auth.js +85 -0
  50. package/vendor/cli/dist/chrome-extension/poll-backoff.js +22 -0
  51. package/vendor/cli/dist/chrome-extension/run-fence.js +117 -0
  52. package/vendor/cli/dist/chrome-extension/setup-status.js +71 -0
  53. package/vendor/cli/dist/chrome-extension/side-panel-action-click.js +65 -0
  54. package/vendor/cli/dist/chrome-extension/sidepanel-context.js +50 -0
  55. package/vendor/cli/dist/chrome-extension/sidepanel.html +680 -0
  56. package/vendor/cli/dist/chrome-extension/sidepanel.js +1643 -0
  57. package/vendor/cli/dist/chrome-extension/tab-labels.js +233 -0
  58. package/vendor/cli/dist/chrome-extension/tools.js +3354 -0
  59. package/vendor/cli/dist/chrome-extension/watch-observer.js +354 -0
  60. package/vendor/cli/dist/chrome-extension/watch-redact.js +96 -0
  61. package/vendor/cli/dist/chrome-extension/watch.js +348 -0
  62. package/vendor/cli/dist/commands/_lib/apply/desired-state.d.ts +6 -4
  63. package/vendor/cli/dist/commands/_lib/apply/desired-state.js +5 -3
  64. package/vendor/cli/dist/commands/_lib/apply/diff.js +4 -3
  65. package/vendor/cli/dist/commands/_lib/chrome-for-testing.d.ts +13 -0
  66. package/vendor/cli/dist/commands/_lib/chrome-for-testing.js +226 -0
  67. package/vendor/cli/dist/commands/_lib/device-credential.d.ts +34 -0
  68. package/vendor/cli/dist/commands/_lib/device-credential.js +157 -0
  69. package/vendor/cli/dist/commands/_lib/device-wizard.d.ts +17 -0
  70. package/vendor/cli/dist/commands/_lib/device-wizard.js +11 -2
  71. package/vendor/cli/dist/commands/_lib/native-messaging.d.ts +7 -0
  72. package/vendor/cli/dist/commands/_lib/native-messaging.js +40 -0
  73. package/vendor/cli/dist/commands/browser.d.ts +23 -0
  74. package/vendor/cli/dist/commands/browser.js +492 -0
  75. package/vendor/cli/dist/commands/call.d.ts +12 -0
  76. package/vendor/cli/dist/commands/call.js +48 -26
  77. package/vendor/cli/dist/commands/context.d.ts +3 -1
  78. package/vendor/cli/dist/commands/context.js +13 -2
  79. package/vendor/cli/dist/commands/daemon.js +2 -152
  80. package/vendor/cli/dist/commands/memory/_lib/errors.d.ts +4 -1
  81. package/vendor/cli/dist/commands/memory/_lib/errors.js +9 -1
  82. package/vendor/cli/dist/commands/memory/_lib/mcp.d.ts +0 -9
  83. package/vendor/cli/dist/commands/memory/_lib/mcp.js +1 -23
  84. package/vendor/cli/dist/commands/memory/_lib/memory-auth.d.ts +20 -4
  85. package/vendor/cli/dist/commands/memory/_lib/memory-auth.js +21 -17
  86. package/vendor/cli/dist/commands/memory/_lib/seed-cmd.js +6 -17
  87. package/vendor/cli/dist/commands/memory/run.js +4 -25
  88. package/vendor/cli/dist/connectors/google_gmail.ts +70 -41
  89. package/vendor/cli/dist/connectors/google_gmail_references.ts +406 -0
  90. package/vendor/cli/dist/connectors/linkedin-inbox-adapter.js +107 -36
  91. package/vendor/cli/dist/connectors/linkedin-inbox.ts +13 -0
  92. package/vendor/cli/dist/connectors/linkedin.ts +1 -1
  93. package/vendor/cli/dist/connectors/postgres.ts +5 -3
  94. package/vendor/cli/dist/connectors/reddit.ts +171 -5
  95. package/vendor/cli/dist/connectors/whatsapp-web-adapter.js +147 -61
  96. package/vendor/cli/dist/connectors/whatsapp-web-helpers.ts +6 -2
  97. package/vendor/cli/dist/connectors/whatsapp_web.ts +48 -17
  98. package/vendor/cli/dist/connectors/x-dm-adapter.js +252 -35
  99. package/vendor/cli/dist/connectors/x.ts +189 -13
  100. package/vendor/cli/dist/index.js +46 -6
  101. package/vendor/cli/dist/internal/device-state.d.ts +2 -0
  102. package/vendor/cli/dist/internal/device-state.js +4 -0
  103. package/vendor/cli/dist/runtime-components.json +4 -4
  104. package/vendor/cli/dist/templates/AGENTS.md.tmpl +3 -1
  105. package/vendor/cli/dist/templates/TESTING.md.tmpl +8 -8
  106. package/vendor/cli/package.json +1 -1
  107. package/vendor/connector-sdk/dist/browser-requirement.js +1 -1
  108. package/vendor/connector-sdk/dist/connector-types.d.ts +5 -1
  109. package/vendor/connector-sdk/package.json +1 -1
  110. package/vendor/connector-worker/dist/agent-turn/gateway-tools.d.ts +9 -0
  111. package/vendor/connector-worker/dist/agent-turn/gateway-tools.js +23 -2
  112. package/vendor/connector-worker/dist/agent-turn/guest-entry.js +14 -6
  113. package/vendor/connector-worker/dist/agent-turn/guest.bundle.js +62 -25
  114. package/vendor/connector-worker/dist/agent-turn/media-tools.d.ts +1 -0
  115. package/vendor/connector-worker/dist/agent-turn/media-tools.js +5 -2
  116. package/vendor/connector-worker/dist/bin.js +2 -0
  117. package/vendor/connector-worker/dist/daemon/agent-turn.js +52 -17
  118. package/vendor/connector-worker/dist/daemon/automation.js +2 -12
  119. package/vendor/connector-worker/dist/daemon/client.d.ts +8 -2
  120. package/vendor/connector-worker/dist/daemon/client.js +8 -1
  121. package/vendor/connector-worker/dist/daemon/interactive-session.js +29 -6
  122. package/vendor/connector-worker/dist/daemon/poll-loop.js +23 -1
  123. package/vendor/connector-worker/dist/daemon/terminal-delivery.d.ts +5 -10
  124. package/vendor/connector-worker/dist/daemon/terminal-delivery.js +55 -23
  125. package/vendor/connector-worker/dist/daemon/terminal-failure.js +2 -2
  126. package/vendor/connector-worker/dist/executor/isolate.js +3 -0
  127. package/vendor/connector-worker/package.json +1 -1
  128. package/vendor/core/dist/agent-tooling.d.ts +8 -0
  129. package/vendor/core/dist/agent-tooling.js +9 -1
  130. package/vendor/core/dist/capabilities.d.ts +1 -1
  131. package/vendor/core/dist/capabilities.js +5 -0
  132. package/vendor/core/dist/contracts/entity-reference.d.ts +3 -0
  133. package/vendor/core/dist/contracts/entity-reference.js +31 -0
  134. package/vendor/core/dist/contracts/tools/manage-automations.d.ts +3 -0
  135. package/vendor/core/dist/contracts/tools/manage-automations.js +3 -0
  136. package/vendor/core/dist/contracts/tools/manage-connections.js +1 -1
  137. package/vendor/core/dist/contracts/tools/manage-entity-schema.js +1 -1
  138. package/vendor/core/dist/contracts/tools/manage-entity.d.ts +113 -34
  139. package/vendor/core/dist/contracts/tools/manage-entity.js +41 -14
  140. package/vendor/core/dist/contracts/tools/manage-operations.d.ts +13 -4
  141. package/vendor/core/dist/contracts/tools/manage-operations.js +19 -7
  142. package/vendor/core/dist/contracts/tools/read-knowledge.d.ts +6 -1
  143. package/vendor/core/dist/contracts/tools/read-knowledge.js +20 -1
  144. package/vendor/core/dist/contracts/tools/view-attach.d.ts +1 -1
  145. package/vendor/core/dist/contracts/tools/view-runtime.d.ts +2 -0
  146. package/vendor/core/dist/contracts/tools/view-runtime.js +3 -1
  147. package/vendor/core/dist/contracts/worker/device-automation.d.ts +4 -9
  148. package/vendor/core/dist/contracts/worker/device-automation.js +10 -15
  149. package/vendor/core/dist/contracts/worker/protocol.d.ts +20 -0
  150. package/vendor/core/dist/contracts/worker/protocol.js +20 -0
  151. package/vendor/core/dist/reserved.d.ts +6 -3
  152. package/vendor/core/dist/reserved.js +6 -3
  153. package/vendor/core/dist/worker/auth.d.ts +3 -2
  154. package/vendor/core/package.json +6 -1
  155. package/vendor/postgres/cf/src/query.js +3 -1
  156. package/vendor/postgres/cjs/src/query.js +3 -1
  157. package/vendor/postgres/src/query.js +3 -1
  158. /package/vendor/postgres/{.bun-tag-2f292c6554a8475e → .bun-tag-22d644c2873790db} +0 -0
@@ -0,0 +1,406 @@
1
+ /** Gmail mailbox additions, retained as references rather than email content. */
2
+ import type {
3
+ EventEnvelope,
4
+ HttpClient,
5
+ SyncContext,
6
+ SyncResult,
7
+ } from '@lobu/connector-sdk';
8
+
9
+ const BASE = 'https://gmail.googleapis.com/gmail/v1/users/me';
10
+ const REQUEST_BUDGET = 20;
11
+
12
+ export interface GmailReferenceState {
13
+ version: 1;
14
+ account: string;
15
+ history_id: string;
16
+ inventory?: { page_token?: string; recent_tokens?: string[] };
17
+ partial?: { record_id: string; after_message_id: string; digest: string };
18
+ history_page?: { token: string; recent_tokens?: string[] };
19
+ }
20
+
21
+ interface ReferenceCheckpoint {
22
+ references?: GmailReferenceState;
23
+ }
24
+ interface ReferenceConfig {
25
+ max_results?: number;
26
+ }
27
+ interface MessageIdentity {
28
+ id: string;
29
+ threadId: string;
30
+ }
31
+ interface HistoryRecord {
32
+ id: string;
33
+ messagesAdded?: Array<{ message: MessageIdentity }>;
34
+ }
35
+ interface HistoryPage {
36
+ historyId: string;
37
+ history?: HistoryRecord[];
38
+ nextPageToken?: string;
39
+ }
40
+
41
+ function historyId(value: unknown): string {
42
+ if (typeof value !== 'string' || !/^\d{1,128}$/.test(value))
43
+ throw new Error('Gmail returned an invalid history identity');
44
+ return value;
45
+ }
46
+
47
+ function nativeId(value: unknown): string {
48
+ if (typeof value !== 'string' || !/^[a-zA-Z0-9_-]{1,256}$/.test(value))
49
+ throw new Error('Gmail returned an invalid message identity');
50
+ return value;
51
+ }
52
+
53
+ async function profile(
54
+ http: HttpClient,
55
+ ): Promise<{ account: string; history: string }> {
56
+ const result = await http.raw(
57
+ `${BASE}/profile?fields=emailAddress,historyId`,
58
+ );
59
+ if (!result.ok) throw new Error(`Gmail profile error (${result.status})`);
60
+ const data = (await result.json()) as {
61
+ emailAddress?: string;
62
+ historyId?: string;
63
+ };
64
+ const account = data.emailAddress?.trim().toLowerCase();
65
+ if (!account || !/^[^\s@]+@[^\s@]+$/.test(account))
66
+ throw new Error('Gmail returned an invalid account identity');
67
+ return { account, history: historyId(data.historyId) };
68
+ }
69
+
70
+ function gap(
71
+ account: string,
72
+ key: string,
73
+ metadata: Record<string, unknown>,
74
+ ): EventEnvelope {
75
+ return {
76
+ origin_id: `gmail_gap:${encodeURIComponent(account)}:${key}`,
77
+ origin_type: 'source_gap',
78
+ payload_text: '',
79
+ // A gap has no provider occurrence timestamp. The epoch keeps retries of
80
+ // this same source discontinuity identical; arrival reads use created_at.
81
+ occurred_at: new Date(0),
82
+ metadata: {
83
+ ...metadata,
84
+ source_occurred_at: null,
85
+ timestamp_basis: 'unknown',
86
+ },
87
+ };
88
+ }
89
+
90
+ function messageReference(
91
+ state: GmailReferenceState,
92
+ stub: MessageIdentity,
93
+ ): EventEnvelope {
94
+ const id = nativeId(stub.id);
95
+ const thread = nativeId(stub.threadId);
96
+ return {
97
+ // Thread content uses bare thread IDs, which may equal the first message ID.
98
+ // Scope native identity to the authenticated account, independently of feeds.
99
+ origin_id: `gmail_message:${encodeURIComponent(state.account)}:${id}`,
100
+ origin_type: 'message_reference',
101
+ payload_text: '',
102
+ source_url: `https://mail.google.com/mail/u/${encodeURIComponent(state.account)}/#all/${thread}`,
103
+ // List/history stubs do not include provider occurrence time. This stable
104
+ // transport sentinel is NOT the email date: use created_at for arrival
105
+ // previews and source_occurred_at for provider-time rendering. Never infer
106
+ // a first contact from it. Omitting title/text also avoids empty embeddings.
107
+ occurred_at: new Date(0),
108
+ metadata: {
109
+ source_id: id,
110
+ thread_id: thread,
111
+ source_occurred_at: null,
112
+ timestamp_basis: 'unknown',
113
+ },
114
+ };
115
+ }
116
+
117
+ function identities(stubs: MessageIdentity[]): MessageIdentity[] {
118
+ const messages = new Map<string, MessageIdentity>();
119
+ for (const stub of stubs) {
120
+ const id = nativeId(stub?.id);
121
+ const threadId = nativeId(stub?.threadId);
122
+ const previous = messages.get(id);
123
+ if (previous && previous.threadId !== threadId)
124
+ throw new Error('Gmail message identity conflicts');
125
+ messages.set(id, { id, threadId });
126
+ }
127
+ return [...messages.values()];
128
+ }
129
+
130
+ function additions(record: HistoryRecord): MessageIdentity[] {
131
+ const entries =
132
+ record.messagesAdded === undefined ? [] : record.messagesAdded;
133
+ if (!Array.isArray(entries))
134
+ throw new Error('Gmail returned invalid history additions');
135
+ return identities(entries.map((entry) => entry?.message)).sort((a, b) =>
136
+ a.id < b.id ? -1 : a.id > b.id ? 1 : 0,
137
+ );
138
+ }
139
+
140
+ function pageToken(value: unknown): string | undefined {
141
+ if (value === undefined) return undefined;
142
+ if (typeof value !== 'string' || !value || value.length > 8192)
143
+ throw new Error('Gmail returned an invalid page token');
144
+ return value;
145
+ }
146
+
147
+ // Remember only a bounded set of exact recent cursors. Empty advancing pages
148
+ // are legitimate source progress; the per-pass request budget bounds work.
149
+ // Reject actual short cursor cycles without imposing an item-arrival deadline.
150
+ function nextTokens(
151
+ next: string,
152
+ current: string | undefined,
153
+ recent: string[] = [],
154
+ ): string[] {
155
+ if (next === current || recent.includes(next))
156
+ throw new Error('Gmail cursor did not advance');
157
+ return [
158
+ ...recent,
159
+ ...(current && !recent.includes(current) ? [current] : []),
160
+ next,
161
+ ].slice(-8);
162
+ }
163
+
164
+ /**
165
+ * Initial available-message inventory, then Gmail messageAdded history,
166
+ * including spam/trash. This is not a current-state mailbox mirror, an
167
+ * edit/deletion archive or an observer capture subscription.
168
+ * Every durable position travels with exactly the references it covers.
169
+ */
170
+ export async function syncGmailReferences(
171
+ ctx: SyncContext<ReferenceCheckpoint, ReferenceConfig>,
172
+ createClient: (token: string) => HttpClient,
173
+ ): Promise<SyncResult> {
174
+ const token = ctx.credentials?.accessToken;
175
+ if (!token) throw new Error('Gmail requires Google OAuth credentials.');
176
+ const maximum = ctx.config.max_results ?? 500;
177
+ if (!Number.isInteger(maximum) || maximum < 1 || maximum > 500)
178
+ throw new Error('Gmail reference max_results must be between 1 and 500');
179
+ const http = createClient(token);
180
+ const identity = await profile(http);
181
+ const saved = ctx.checkpoint?.references;
182
+ if (saved && saved.account !== identity.account)
183
+ throw new Error(
184
+ 'Gmail reference account does not match its saved checkpoint',
185
+ );
186
+ if (saved && saved.version !== 1)
187
+ throw new Error('Gmail reference checkpoint version is unsupported');
188
+ if (saved) historyId(saved.history_id);
189
+ let state: GmailReferenceState = saved
190
+ ? { ...saved }
191
+ : {
192
+ version: 1,
193
+ account: identity.account,
194
+ history_id: identity.history,
195
+ inventory: {},
196
+ };
197
+ const commit = async (events: EventEnvelope[], next: GmailReferenceState) => {
198
+ if ((await profile(http)).account !== state.account)
199
+ throw new Error('Gmail reference account changed during capture');
200
+ await ctx.commit(events, { references: next });
201
+ state = next;
202
+ };
203
+ // Commit the history boundary before the first inventory page so arrivals
204
+ // during a long scan are reconciled after available history has been listed.
205
+ if (!saved) {
206
+ await commit([], {
207
+ version: 1,
208
+ account: identity.account,
209
+ history_id: identity.history,
210
+ inventory: {},
211
+ });
212
+ }
213
+ let inspected = 0;
214
+ let found = 0;
215
+ const recover = async (reason: string) => {
216
+ const baseline = await profile(http);
217
+ if (baseline.account !== state.account)
218
+ throw new Error('Gmail reference account changed during recovery');
219
+ const event = gap(
220
+ state.account,
221
+ `${reason}:${state.history_id}:${baseline.history}`,
222
+ {
223
+ reason,
224
+ from_history_id: state.history_id,
225
+ to_history_id: baseline.history,
226
+ recovery: 'available_message_inventory',
227
+ },
228
+ );
229
+ await commit([event], {
230
+ version: 1,
231
+ account: state.account,
232
+ history_id: baseline.history,
233
+ inventory: {},
234
+ });
235
+ return {
236
+ status: 'more',
237
+ metadata: { items_found: found + 1, continuity_gap: true },
238
+ } satisfies SyncResult;
239
+ };
240
+
241
+ for (let requests = 0; requests < REQUEST_BUDGET; requests++) {
242
+ if (state.inventory) {
243
+ const params = new URLSearchParams({
244
+ includeSpamTrash: 'true',
245
+ maxResults: String(maximum - inspected),
246
+ fields: 'messages(id,threadId),nextPageToken',
247
+ });
248
+ if (state.inventory.page_token)
249
+ params.set('pageToken', state.inventory.page_token);
250
+ const result = await http.raw(`${BASE}/messages?${params}`);
251
+ if (!result.ok) {
252
+ if (
253
+ state.inventory.page_token &&
254
+ (result.status === 400 || result.status === 404)
255
+ ) {
256
+ // Retain this generation's original pre-inventory history baseline.
257
+ await commit([], { ...state, inventory: {} });
258
+ return {
259
+ status: 'more',
260
+ metadata: { items_found: found, inventory_restarted: true },
261
+ };
262
+ }
263
+ throw new Error(`Gmail reference inventory error (${result.status})`);
264
+ }
265
+ const data = (await result.json()) as {
266
+ messages?: MessageIdentity[];
267
+ nextPageToken?: string;
268
+ };
269
+ const messages = data.messages === undefined ? [] : data.messages;
270
+ if (!Array.isArray(messages))
271
+ throw new Error('Gmail returned invalid inventory messages');
272
+ const token = pageToken(data.nextPageToken);
273
+ if (messages.length > maximum - inspected)
274
+ throw new Error(
275
+ 'Gmail inventory exceeded its requested message budget',
276
+ );
277
+ const recent = token
278
+ ? nextTokens(
279
+ token,
280
+ state.inventory.page_token,
281
+ state.inventory.recent_tokens,
282
+ )
283
+ : undefined;
284
+ const events: EventEnvelope[] = [];
285
+ for (const message of identities(messages))
286
+ events.push(messageReference(state, message));
287
+ inspected += messages.length;
288
+ found += events.length;
289
+ const next = { ...state };
290
+ if (token) next.inventory = { page_token: token, recent_tokens: recent };
291
+ else delete next.inventory;
292
+ await commit(events, next);
293
+ } else {
294
+ const params = new URLSearchParams({
295
+ startHistoryId: state.history_id,
296
+ historyTypes: 'messageAdded',
297
+ maxResults: '1',
298
+ fields:
299
+ 'history(id,messagesAdded(message(id,threadId))),historyId,nextPageToken',
300
+ });
301
+ if (state.history_page) params.set('pageToken', state.history_page.token);
302
+ const result = await http.raw(`${BASE}/history?${params}`);
303
+ if (!result.ok) {
304
+ if (
305
+ state.history_page &&
306
+ (result.status === 400 || result.status === 404)
307
+ ) {
308
+ // A retired page token does not prove the history boundary expired.
309
+ // Replay from that boundary, retaining any partially consumed record.
310
+ const next = { ...state };
311
+ delete next.history_page;
312
+ await commit([], next);
313
+ return { status: 'more', metadata: { items_found: found } };
314
+ }
315
+ if (result.status === 404) return recover('history_expired');
316
+ throw new Error(`Gmail reference history error (${result.status})`);
317
+ }
318
+ const page = (await result.json()) as HistoryPage;
319
+ const head = historyId(page.historyId);
320
+ if (BigInt(head) < BigInt(state.history_id))
321
+ throw new Error('Gmail history moved backwards');
322
+ const records = page.history === undefined ? [] : page.history;
323
+ if (!Array.isArray(records))
324
+ throw new Error('Gmail returned invalid history records');
325
+ const token = pageToken(page.nextPageToken);
326
+ if (records.length > 1)
327
+ throw new Error('Gmail history exceeded its requested record budget');
328
+ const record = records[0];
329
+ if (records.length === 0) {
330
+ if (token) {
331
+ const recent = nextTokens(
332
+ token,
333
+ state.history_page?.token,
334
+ state.history_page?.recent_tokens,
335
+ );
336
+ await commit([], {
337
+ ...state,
338
+ history_page: { token, recent_tokens: recent },
339
+ });
340
+ continue;
341
+ }
342
+ if (state.partial) return recover('history_anchor_missing');
343
+ const next = { ...state, history_id: head };
344
+ delete next.history_page;
345
+ await commit([], next);
346
+ return { status: 'complete', metadata: { items_found: found } };
347
+ }
348
+ const recordId = historyId(record?.id);
349
+ if (
350
+ BigInt(recordId) <= BigInt(state.history_id) ||
351
+ BigInt(recordId) > BigInt(head)
352
+ )
353
+ throw new Error('Gmail returned an invalid history boundary');
354
+ if (state.partial && recordId !== state.partial.record_id)
355
+ return recover('history_anchor_missing');
356
+ const messages = additions(record);
357
+ const digest = Array.from(
358
+ new Uint8Array(
359
+ await crypto.subtle.digest(
360
+ 'SHA-256',
361
+ new TextEncoder().encode(JSON.stringify(messages)),
362
+ ),
363
+ ),
364
+ (byte) => byte.toString(16).padStart(2, '0'),
365
+ ).join('');
366
+ if (
367
+ state.partial &&
368
+ (state.partial.digest !== digest ||
369
+ !messages.some(
370
+ (message) => message.id === state.partial?.after_message_id,
371
+ ))
372
+ )
373
+ return recover('history_anchor_changed');
374
+ const unread = state.partial
375
+ ? messages.filter(
376
+ (message) => message.id > state.partial!.after_message_id,
377
+ )
378
+ : messages;
379
+ const batch = unread.slice(0, maximum - inspected);
380
+ const events: EventEnvelope[] = [];
381
+ for (const message of batch) {
382
+ events.push(messageReference(state, message));
383
+ inspected++;
384
+ }
385
+ found += events.length;
386
+ const next = { ...state };
387
+ if (batch.length < unread.length) {
388
+ next.partial = {
389
+ record_id: recordId,
390
+ after_message_id: batch[batch.length - 1].id,
391
+ digest,
392
+ };
393
+ } else {
394
+ next.history_id = token ? recordId : head;
395
+ delete next.partial;
396
+ delete next.history_page;
397
+ }
398
+ await commit(events, next);
399
+ if (!next.partial && !token)
400
+ return { status: 'complete', metadata: { items_found: found } };
401
+ }
402
+ if (inspected >= maximum)
403
+ return { status: 'more', metadata: { items_found: found } };
404
+ }
405
+ return { status: 'more', metadata: { items_found: found } };
406
+ }
@@ -57,6 +57,14 @@ export function linkedInInboxProgram() {
57
57
  fail("Account identity is unavailable");
58
58
  return urn.replace("fs_miniProfile", "fsd_profile");
59
59
  }
60
+ async function boundAccount(expected) {
61
+ if (typeof expected !== "string" || !/^urn:li:fs_miniProfile:.+$/.test(expected))
62
+ fail("Source requires the connection's saved account; reconnect this source");
63
+ const member = await account();
64
+ if (expected.replace(/^urn:li:fs_miniProfile:/, "urn:li:fsd_profile:") !== member)
65
+ fail("Source does not match the connection's saved account; reconnect this source");
66
+ return member;
67
+ }
60
68
  const PAGE_MAILBOX = /^urn:li:fsd_pageMailbox:[0-9]+$/;
61
69
  const NOT_ADMIN =
62
70
  "The signed-in LinkedIn account does not administer this page inbox";
@@ -83,8 +91,8 @@ export function linkedInInboxProgram() {
83
91
  // The inbox one feed reads: the signed-in member's own ("me", the default),
84
92
  // or a company page's mailbox the member administers. `member` is the
85
93
  // signed-in account, which must not change while the inbox is in use.
86
- async function inboxOf(selected) {
87
- const member = await account();
94
+ async function inboxOf(selected, expected) {
95
+ const member = await boundAccount(expected);
88
96
  if (selected === undefined || selected === null || selected === "me")
89
97
  return { mailbox: member, member, page: false };
90
98
  if (typeof selected !== "string" || !PAGE_MAILBOX.test(selected))
@@ -656,7 +664,7 @@ export function linkedInInboxProgram() {
656
664
  }
657
665
  async function read(input) {
658
666
  const inbox = await inboxOf(
659
- input.feed === "live_messages" ? input.inbox : undefined
667
+ input.feed === "live_messages" ? input.inbox : undefined, input.expected_account
660
668
  );
661
669
  const mailbox = inbox.mailbox;
662
670
  const cursor = cursorOf(input.cursor, input.feed, inbox);
@@ -713,6 +721,17 @@ export function linkedInInboxProgram() {
713
721
  .map((byte) => byte.toString(16).padStart(2, "0"))
714
722
  .join("");
715
723
  }
724
+ // Keep SHA-256 fingerprints losslessly in base64url inside checkpoints.
725
+ // Batch checkpoints carry both predecessor and successor, so hex would
726
+ // exhaust the transport budget before a large interrupted pass can resume.
727
+ function compactHash(value) {
728
+ if (typeof value !== "string" || !/^[a-f0-9]{64}$/.test(value)) return value;
729
+ return btoa(String.fromCharCode(...value.match(/../g).map(byte => parseInt(byte, 16))))
730
+ .replaceAll("+", "-").replaceAll("/", "_").replace(/=+$/, "");
731
+ }
732
+ function compactSeen(seen) {
733
+ return Object.fromEntries(Object.entries(seen ?? {}).map(([key, value]) => [compactHash(key), Array.isArray(value) ? [compactHash(value[0]), value[1]] : compactHash(value)]));
734
+ }
716
735
  // One observation pass is bounded source work. Message catch-up resumes from
717
736
  // its checkpoint; notifications reorder at the top and report a gap instead.
718
737
  const NOTIFICATION_PAGES_PER_PASS = 10;
@@ -753,7 +772,7 @@ export function linkedInInboxProgram() {
753
772
  }
754
773
  payload = next;
755
774
  }
756
- // `seen` is the whole position: an unchanged inbox repeats its checkpoint.
775
+ // `seen` records source position; the delivery layer also carries a monotonic generation.
757
776
  return {};
758
777
  }
759
778
  async function catchUpConversation(
@@ -844,14 +863,19 @@ export function linkedInInboxProgram() {
844
863
  async function snapshot(feed, previous, inbox) {
845
864
  const observedAt = Date.now();
846
865
  const observed = new Map();
866
+ const previousSeen = compactSeen(previous?.seen);
847
867
  const events = [];
848
868
  // Returns whether the previous checkpoint already held this identity.
849
869
  async function observe(row, eventType, resourceType, revisionText) {
850
870
  const identity = await digest(row.id);
851
871
  const revision =
852
872
  revisionText === undefined ? identity : await digest(revisionText);
853
- const prior = previous?.seen[identity];
854
- if (previous && prior !== revision && !observed.has(identity)) {
873
+ const key = compactHash(identity);
874
+ const fingerprint = revisionText === undefined ? "1" : compactHash(revision);
875
+ const stored = previousSeen[key];
876
+ const prior = Array.isArray(stored) ? stored[0] : stored;
877
+ const unchanged = prior === fingerprint || (revisionText === undefined && prior === key);
878
+ if (previous && !unchanged && !observed.has(key)) {
855
879
  events.push({
856
880
  id: `${identity}:${revision}`,
857
881
  event_type: eventType,
@@ -860,8 +884,12 @@ export function linkedInInboxProgram() {
860
884
  occurred_at: row.occurred_at ?? new Date(observedAt).toISOString(),
861
885
  });
862
886
  }
863
- observed.set(identity, revision);
864
- return prior !== undefined;
887
+ // Overlapping pages must checkpoint the same revision we emitted first.
888
+ if (!observed.has(key)) observed.set(key, fingerprint);
889
+ // Accepted chunks suppress repeats, but newly accepted items are not
890
+ // the original catch-up boundary. Otherwise a rebind stops on page one
891
+ // before reaching unfinished provider pages.
892
+ return Array.isArray(stored) ? stored[1] === true : prior !== undefined;
865
893
  }
866
894
  // The provider cannot show what lies beyond this position. Report it once
867
895
  // per position and advance, rather than retrying or claiming completeness.
@@ -890,41 +918,84 @@ export function linkedInInboxProgram() {
890
918
  seen: Object.fromEntries(
891
919
  [
892
920
  ...observed,
893
- ...Object.entries(previous?.seen ?? {}).filter(
921
+ ...Object.entries(previousSeen).filter(
894
922
  ([identity]) => !observed.has(identity)
895
923
  ),
896
- ].slice(0, SEEN_LIMIT)
924
+ ].slice(0, feed === "notifications" ? Math.max(SEEN_LIMIT, observed.size) : SEEN_LIMIT).map(([key, value]) => [key, Array.isArray(value) ? value[0] : value])
897
925
  ),
898
926
  };
899
- return { checkpoint, events, more };
927
+ return { checkpoint, events, more, observed };
900
928
  }
901
- // Ordered batches of at most 500 references and 128 KiB. Only the final
902
- // batch carries the checkpoint, so an interruption cannot skip references.
929
+ // Each accepted chunk commits only its own seen identities. Source position
930
+ // advances on the final chunk; a rebind can safely rescan the unfinished page.
903
931
  async function batchesOf(feed, previous, result) {
904
- const next = { linkedin_inbox: result.checkpoint };
905
932
  const { events } = result;
906
- const base = `${feed}:${await digest(JSON.stringify([previous, next, events]))}`;
907
- const size = (batch) =>
908
- new TextEncoder().encode(JSON.stringify(batch)).length;
933
+ const prior = previous?.linkedin_inbox;
934
+ const epoch = prior?.observation_epoch ?? (prior
935
+ ? await digest(JSON.stringify(["legacy-observation", prior]))
936
+ : crypto.randomUUID());
937
+ let generation = prior?.observation_generation ?? 0;
938
+ if (!Number.isSafeInteger(generation) || generation < 0)
939
+ fail("Invalid observation generation");
940
+ const size = (batch) => new TextEncoder().encode(JSON.stringify(batch)).length;
909
941
  const batches = [];
942
+ let cursor = previous;
910
943
  for (let offset = 0; ; ) {
911
- const through = (end) => ({
912
- id: `${base}:${offset}`,
913
- events: events.slice(offset, end),
914
- ...(end === events.length
915
- ? { checkpoint: { previous, next }, more: result.more }
916
- : {}),
917
- });
944
+ const nextGeneration = events.length || !prior?.observation_epoch ? generation + 1 : generation;
945
+ if (!Number.isSafeInteger(nextGeneration)) fail("Observation generation exhausted");
946
+ const observedEvents = await Promise.all(events.slice(offset, offset + BATCH_EVENTS).map(async (event) => ({
947
+ ...event,
948
+ id: event.event_type === "notification.changed"
949
+ ? await digest(JSON.stringify([epoch, nextGeneration, event.id])) : event.id,
950
+ })));
951
+ const through = async (end) => {
952
+ const final = end === events.length;
953
+ let seen = compactSeen(cursor?.linkedin_inbox?.seen);
954
+ for (const event of events.slice(offset, end)) {
955
+ const identity = compactHash(await digest(event.resource_ref));
956
+ if (result.observed.has(identity)) {
957
+ const prior = seen[identity];
958
+ const anchor = Array.isArray(prior) ? prior[1] : prior !== undefined;
959
+ seen[identity] = [result.observed.get(identity), anchor];
960
+ }
961
+ }
962
+ // Keep the prior completed pass's bounded anchors until this pass ends.
963
+ // Only provisional message IDs can be evicted safely: native-ID replay
964
+ // dedupes, while evicting an original anchor invents a new source gap.
965
+ // Mutable notification fingerprints survive the entire unfinished pass.
966
+ if (feed === "live_messages") {
967
+ const entries = Object.entries(seen);
968
+ const original = ([, value]) => !Array.isArray(value) || value[1] === true;
969
+ seen = Object.fromEntries([
970
+ ...entries.filter(original),
971
+ ...entries.filter((entry) => !original(entry)).slice(-SEEN_LIMIT),
972
+ ]);
973
+ }
974
+ const next = { linkedin_inbox: {
975
+ ...(final ? result.checkpoint : { ...prior, seen }),
976
+ observation_epoch: epoch,
977
+ observation_generation: nextGeneration,
978
+ } };
979
+ const slice = observedEvents.slice(0, end - offset);
980
+ return {
981
+ id: `${feed}:${epoch}:${nextGeneration}:${await digest(JSON.stringify([cursor, next, slice]))}`,
982
+ events: slice,
983
+ checkpoint: { previous: cursor, next },
984
+ ...(final ? { more: result.more } : {}),
985
+ };
986
+ };
918
987
  let end = Math.min(offset + BATCH_EVENTS, events.length);
919
- let batch = through(end);
988
+ let batch = await through(end);
920
989
  while (size(batch) > BATCH_BYTES && end - offset > 1) {
921
990
  end = offset + Math.floor((end - offset) / 2);
922
- batch = through(end);
991
+ batch = await through(end);
923
992
  }
924
993
  if (size(batch) > BATCH_BYTES)
925
994
  fail("Source reference batch exceeds the browser record limit");
926
995
  batches.push(batch);
927
996
  if (end === events.length) return batches;
997
+ cursor = batch.checkpoint.next;
998
+ generation++;
928
999
  offset = end;
929
1000
  }
930
1001
  }
@@ -1010,11 +1081,11 @@ export function linkedInInboxProgram() {
1010
1081
  "LinkedIn source delivery was rejected; reattach from the durable checkpoint"
1011
1082
  );
1012
1083
  if (event.data.ok && listener.batches.length) {
1013
- // Receipt means the browser durably buffered the batch. Only the server
1014
- // commits the final batch's checkpoint; a reattach starts from that value.
1084
+ // The page receipt means buffered, not committed. A reattach uses the
1085
+ // server's durable per-chunk checkpoint; observation identities survive replay.
1015
1086
  const done = listener.batches.shift();
1016
1087
  if (done.checkpoint) listener.checkpoint = done.checkpoint.next;
1017
- else post(listener, listener.batches[0]);
1088
+ if (listener.batches.length) post(listener, listener.batches[0]);
1018
1089
  }
1019
1090
  }
1020
1091
  // Direct send, approved ONLY for replying to direct messages on accounts the
@@ -1046,9 +1117,9 @@ export function linkedInInboxProgram() {
1046
1117
  // `conversation_id` is a thread id of the member's own inbox, or a full
1047
1118
  // conversation URN whose mailbox names the inbox the reply leaves from: the
1048
1119
  // member's own or a page the member administers.
1049
- async function sendTarget(given) {
1120
+ async function sendTarget(given, expected) {
1050
1121
  if (typeof given === "string" && /^[A-Za-z0-9_-]+=*$/.test(given)) {
1051
- const member = await account();
1122
+ const member = await boundAccount(expected);
1052
1123
  return { inbox: { mailbox: member, member, page: false }, thread: given };
1053
1124
  }
1054
1125
  const match =
@@ -1061,14 +1132,14 @@ export function linkedInInboxProgram() {
1061
1132
  "conversation_id must be a LinkedIn conversation thread id or conversation URN"
1062
1133
  );
1063
1134
  if (PAGE_MAILBOX.test(match[1]))
1064
- return { inbox: await inboxOf(match[1]), thread: match[2] };
1065
- const member = await account();
1135
+ return { inbox: await inboxOf(match[1], expected), thread: match[2] };
1136
+ const member = await boundAccount(expected);
1066
1137
  if (match[1] !== member)
1067
1138
  fail("The conversation belongs to another LinkedIn account; not sending");
1068
1139
  return { inbox: { mailbox: member, member, page: false }, thread: match[2] };
1069
1140
  }
1070
1141
  async function send(input) {
1071
- const { inbox, thread } = await sendTarget(input.conversation_id);
1142
+ const { inbox, thread } = await sendTarget(input.conversation_id, input.expected_account);
1072
1143
  const mailbox = inbox.mailbox;
1073
1144
  const text = typeof input.text === "string" ? input.text.trim() : "";
1074
1145
  if (!text || text.length > 3000)
@@ -1145,7 +1216,7 @@ export function linkedInInboxProgram() {
1145
1216
  if (!input.bridge_id || !input.token)
1146
1217
  fail("Source subscription binding is required");
1147
1218
  const inbox = await inboxOf(
1148
- input.feed === "live_messages" ? input.inbox : undefined
1219
+ input.feed === "live_messages" ? input.inbox : undefined, input.expected_account
1149
1220
  );
1150
1221
  const previous = input.checkpoint?.linkedin_inbox;
1151
1222
  // A mailbox change restarts the baseline, but never permits a changed sign-in.
@@ -1164,7 +1235,7 @@ export function linkedInInboxProgram() {
1164
1235
  inbox,
1165
1236
  // The last posted record; only its receipt may advance delivery.
1166
1237
  sequence: 0,
1167
- // The checkpoint the next snapshot starts from, set by the final batch's receipt.
1238
+ // The checkpoint the next snapshot starts from, set by each batch receipt.
1168
1239
  checkpoint: input.checkpoint ?? null,
1169
1240
  // Undelivered batches of the current snapshot, in order.
1170
1241
  batches: [],