Skip to content

Commit b4091c6

Browse files
committed
fix(search): stop only the activity source that cannot advance and pick each item once per window
- A page with no progress stops the accounts pinning the horizon; the others page on - A failed account the listing ends before retrying is reported - Confluence pages and Jira assignments are placed at their latest moment in the whole window, so a later page never repeats them; an unread version history reports partial coverage - Confluence comments before the window are left out - Search MCP charges a continuation by the accounts its cursor still names
1 parent cb48100 commit b4091c6

7 files changed

Lines changed: 225 additions & 47 deletions

File tree

‎apps/sim/lib/knowledge/mcp/my-activity.ts‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
projectResult,
1010
} from '@/lib/knowledge/mcp/tool-runner'
1111
import { listMyActivity, MAX_ACTIVITY_ACCOUNTS } from '@/lib/sim-search/live/activity'
12+
import { activityCursorAccountCount } from '@/lib/sim-search/live/activity-page'
1213
import { listLiveSearchAccounts } from '@/lib/sim-search/live/application'
1314
import { supportsActivity } from '@/lib/sim-search/live/providers'
1415

@@ -26,8 +27,12 @@ interface ListMyActivityToolContext {
2627
async function activityCost(
2728
principal: Principal,
2829
organizationId: string,
29-
source: string | undefined
30+
source: string | undefined,
31+
cursor: string | undefined
3032
): Promise<number> {
33+
/** A continuation reads only the accounts its cursor still names. */
34+
const continued = cursor ? activityCursorAccountCount(cursor) : undefined
35+
if (continued !== undefined) return Math.min(MAX_ACTIVITY_ACCOUNTS, Math.max(1, continued))
3136
try {
3237
const { accounts } = await listLiveSearchAccounts.execute({
3338
principal,
@@ -61,7 +66,12 @@ export function registerListMyActivityTool(
6166
},
6267
async (input: unknown, extra: { signal: AbortSignal }) => {
6368
const requested = listMyActivityMcpSchema.safeParse(input)
64-
const cost = await activityCost(principal, organizationId, requested.data?.source)
69+
const cost = await activityCost(
70+
principal,
71+
organizationId,
72+
requested.data?.source,
73+
requested.data?.cursor
74+
)
6575
return execute(
6676
'list_my_activity',
6777
knowledgeOperations.listActivity,

‎apps/sim/lib/sim-search/live/activity-page.test.ts‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,39 @@ describe('mergeActivityPage', () => {
260260
expect(stopped).toEqual(['mail:failed_again'])
261261
})
262262

263+
it('stops only the account that cannot move past its bound, letting the others page on', () => {
264+
const accounts: FakeAccount[] = [
265+
{
266+
id: 'stuck',
267+
limit: 2,
268+
items: [1, 2, 3].map((index) => ({ at: at(100), key: `s${index}` })),
269+
},
270+
{
271+
id: 'mail',
272+
limit: 50,
273+
items: [
274+
{ at: at(90), key: 'm1' },
275+
{ at: at(80), key: 'm2' },
276+
],
277+
},
278+
]
279+
const { pages, stopped } = pageThrough(accounts, 10)
280+
expect(pages.flat().filter((key) => key.startsWith('mail:'))).toEqual(['mail:m1', 'mail:m2'])
281+
expect(stopped).toEqual(['stuck:no_progress'])
282+
})
283+
284+
it('reports a failed account the listing ended before retrying', () => {
285+
const { stopped } = pageThrough(
286+
[
287+
{ id: 'chat', limit: 50, items: [] },
288+
{ id: 'mail', limit: 50, items: [{ at: at(5), key: 'm1' }] },
289+
],
290+
10,
291+
(accountId) => accountId === 'mail'
292+
)
293+
expect(stopped).toEqual(['mail:not_retried'])
294+
})
295+
263296
it('ends without a continuation when only failed accounts would continue', () => {
264297
const result = mergeActivityPage(
265298
[{ accountId: 'mail', bound: { end: END, seen: [] }, read: false }],

‎apps/sim/lib/sim-search/live/activity-page.ts‎

Lines changed: 48 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,16 @@ export type AccountActivity<E extends ActivityEntry> = {
6565

6666
/**
6767
* Why an account stopped before reading all its activity: a listing could not page past items
68-
* newer than its bound, more entries than a cursor can name sat at one instant, a page made no
69-
* progress at all, or the account failed again on its one retry.
68+
* newer than its bound, more entries than a cursor can name sat at one instant, it held the
69+
* others at a bound it could not move past, it failed again on its one retry, or it failed and
70+
* the listing ended before it could be retried.
7071
*/
71-
export type ActivityStopReason = 'unreachable' | 'crowded' | 'no_progress' | 'failed_again'
72+
export type ActivityStopReason =
73+
| 'unreachable'
74+
| 'crowded'
75+
| 'no_progress'
76+
| 'failed_again'
77+
| 'not_retried'
7278

7379
interface ActivityPageResult<E extends ActivityEntry> {
7480
/** Newest first. */
@@ -200,28 +206,36 @@ export function mergeActivityPage<E extends ActivityEntry>(
200206
}
201207
const entries = returned.map(({ entry }) => entry)
202208
/**
203-
* A page that returned nothing and moved no bound would be read again unchanged, so it ends
204-
* here. Returning entries is progress even when the remaining bounds stay put, as when a
205-
* recovered account catches up above the others.
209+
* A page that returned nothing and moved no bound would be read again unchanged. The accounts
210+
* whose listings pin the horizon are the ones that cannot advance, so only they stop; the
211+
* others continue below it on the next page. Returning entries is progress even when the
212+
* remaining bounds stay put, as when a recovered account catches up above the others.
206213
*/
207-
if (continuing.length && !progressed && !entries.length)
208-
return {
209-
entries,
210-
stopped: [
211-
...stopped,
212-
...continuing.map((accountId) => ({ accountId, reason: 'no_progress' as const })),
213-
],
214+
if (continuing.length && !progressed && !entries.length) {
215+
const pinning = continuing.filter((accountId) => {
216+
const account = accounts.find((candidate) => candidate.accountId === accountId)
217+
return (
218+
account?.read &&
219+
account.streams.some(
220+
(stream) => stream.more && Date.parse(stream.scannedTo ?? '') === horizon
221+
)
222+
)
223+
})
224+
for (const accountId of pinning.length ? pinning : [...continuing]) {
225+
stopped.push({ accountId, reason: 'no_progress' })
226+
delete next[accountId]
227+
continuing.splice(continuing.indexOf(accountId), 1)
214228
}
229+
}
215230
/**
216231
* An account that failed is retried on the next page while this one made progress; a page
217232
* where only failed accounts were left, and failed again, ends the listing.
218233
*/
219-
const retrying = Object.keys(next).some((accountId) => !continuing.includes(accountId))
220-
return {
221-
entries,
222-
...(continuing.length || (retrying && entries.length) ? { next } : {}),
223-
stopped,
224-
}
234+
const retrying = Object.keys(next).filter((accountId) => !continuing.includes(accountId))
235+
if (continuing.length || (retrying.length && entries.length)) return { entries, next, stopped }
236+
/** A listing that ends here leaves the failed accounts it would have retried unread. */
237+
for (const accountId of retrying) stopped.push({ accountId, reason: 'not_retried' })
238+
return { entries, stopped }
225239
}
226240

227241
const boundSchema = z.object({
@@ -273,3 +287,18 @@ export function readActivityCursor(
273287
return undefined
274288
}
275289
}
290+
291+
/**
292+
* How many accounts a cursor continues, for charging a page before it is read. The cursor is not
293+
* verified here; a forged one is refused when the page reads it.
294+
*/
295+
export function activityCursorAccountCount(value: string): number | undefined {
296+
try {
297+
const parsed = cursorSchema
298+
.pick({ accounts: true })
299+
.safeParse(JSON.parse(Buffer.from(value, 'base64url').toString('utf8')))
300+
return parsed.success ? Object.keys(parsed.data.accounts).length : undefined
301+
} catch {
302+
return undefined
303+
}
304+
}

‎apps/sim/lib/sim-search/live/activity-providers.test.ts‎

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import type { NativeActivityInput, NativeClient } from '@/lib/sim-search/live/ty
1010
const WINDOW: NativeActivityInput = {
1111
start: '2026-11-01T00:00:00.000Z',
1212
end: '2026-11-08T00:00:00.000Z',
13+
windowEnd: '2026-11-08T00:00:00.000Z',
1314
limit: 20,
1415
scopes: ['search:read.public'],
1516
}
@@ -278,6 +279,92 @@ describe('Atlassian activity', () => {
278279
])
279280
})
280281

282+
it('places a page at its latest edit in the whole window, so a later page never repeats it', async () => {
283+
const api = client((path, options) => {
284+
if (path === '/oauth/token/accessible-resources') return [site]
285+
if (path === '/ex/confluence/cloud/wiki/rest/api/user/current') return { accountId: 'me' }
286+
if (path === '/ex/confluence/cloud/wiki/api/v2/pages/twice/versions')
287+
return {
288+
results: [
289+
{ number: 3, authorId: 'ann', createdAt: '2026-11-07T10:00:00.000Z' },
290+
{ number: 2, authorId: 'me', createdAt: '2026-11-06T10:00:00.000Z' },
291+
{ number: 1, authorId: 'me', createdAt: '2026-11-04T10:00:00.000Z' },
292+
],
293+
}
294+
if (path !== '/ex/confluence/cloud/wiki/rest/api/search') return undefined
295+
return String(options?.query?.cql).startsWith('type = comment')
296+
? {
297+
results: [
298+
{
299+
content: {
300+
id: 'early',
301+
type: 'comment',
302+
container: { id: '42', type: 'page', title: 'Runbook' },
303+
history: { createdDate: '2026-10-31T20:00:00.000Z' },
304+
},
305+
},
306+
],
307+
_links: {},
308+
}
309+
: {
310+
results: [
311+
{
312+
content: {
313+
id: 'twice',
314+
type: 'page',
315+
title: 'twice',
316+
version: {
317+
when: '2026-11-07T10:00:00.000Z',
318+
number: 3,
319+
by: { accountId: 'ann' },
320+
},
321+
},
322+
},
323+
],
324+
_links: {},
325+
}
326+
})
327+
const [pages, comments] = (
328+
await atlassianActivity(api, 'confluence', { ...WINDOW, end: '2026-11-05T00:00:00.000Z' })
329+
).streams
330+
expect(pages?.events).toEqual([])
331+
expect(comments?.events).toEqual([])
332+
})
333+
334+
it('reports partial coverage when a page history is longer than one read', async () => {
335+
const api = client((path, options) => {
336+
if (path === '/oauth/token/accessible-resources') return [site]
337+
if (path === '/ex/confluence/cloud/wiki/rest/api/user/current') return { accountId: 'me' }
338+
if (path === '/ex/confluence/cloud/wiki/api/v2/pages/long/versions')
339+
return {
340+
results: [{ number: 90, authorId: 'ann', createdAt: '2026-11-07T10:00:00.000Z' }],
341+
_links: { next: '/wiki/api/v2/pages/long/versions?cursor=x' },
342+
}
343+
if (path !== '/ex/confluence/cloud/wiki/rest/api/search') return undefined
344+
return String(options?.query?.cql).startsWith('type = comment')
345+
? { results: [], _links: {} }
346+
: {
347+
results: [
348+
{
349+
content: {
350+
id: 'long',
351+
type: 'page',
352+
title: 'long',
353+
version: {
354+
when: '2026-11-07T10:00:00.000Z',
355+
number: 90,
356+
by: { accountId: 'ann' },
357+
},
358+
},
359+
},
360+
],
361+
_links: {},
362+
}
363+
})
364+
const page = await atlassianActivity(api, 'confluence', WINDOW)
365+
expect(page).toMatchObject({ partial: true })
366+
})
367+
281368
it('reports a Jira issue at the moment it was assigned to the member, from its history', async () => {
282369
const api = client((path, options) => {
283370
if (path === '/oauth/token/accessible-resources') return [site]

‎apps/sim/lib/sim-search/live/activity.ts‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,10 @@ const STOP_MESSAGES: Record<ActivityStopReason, string> = {
9999
crowded:
100100
'This source has more activity at one instant than a page can return, so its older activity in the window was not read.',
101101
no_progress:
102-
'Paging made no progress, so older activity in the window was not read. Narrow the dates to read it.',
102+
'This source could not move past items it already returned, so its older activity in the window was not read. Narrow the dates to read it.',
103103
failed_again: 'It failed again on retry, so its remaining activity was not read.',
104+
not_retried:
105+
'The listing ended before it could be retried, so its remaining activity was not read.',
104106
}
105107

106108
const INVALID_CURSOR_MESSAGE =
@@ -369,6 +371,7 @@ export const listMyActivity = defineAuthorizedKnowledgeUseCase({
369371
const page = await session.activity({
370372
start: window.start,
371373
end: bound.end,
374+
windowEnd: window.end,
372375
limit: streamItems,
373376
scopes: account.scopes,
374377
signal,
@@ -468,9 +471,10 @@ export const listMyActivity = defineAuthorizedKnowledgeUseCase({
468471
if (!account) continue
469472
if (!read.read) {
470473
const failure = failureStatus(account, read.error, organizationId)
474+
const reason = stopped.get(account.id)
471475
statuses.push(
472-
stopped.has(account.id)
473-
? { ...failure, message: `${read.error.message} ${STOP_MESSAGES.failed_again}` }
476+
reason
477+
? { ...failure, message: `${read.error.message} ${STOP_MESSAGES[reason]}` }
474478
: failure
475479
)
476480
continue

0 commit comments

Comments
 (0)