Skip to content

Commit 0b23ed4

Browse files
committed
fix(messages): preserve removal deltas for legacy RPC readers
Keep the first full snapshot cursor for each legacy connection. A new connection starts from a snapshot, while overlapping reads on the same connection can still observe an ID removed and added again.
1 parent 06f80c0 commit 0b23ed4

2 files changed

Lines changed: 52 additions & 14 deletions

File tree

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { DevframeMessagesListDelta } from '@devframes/hub/types'
2+
import type { DevframeNodeRpcSessionMeta } from 'devframe/types'
23
import { defineRpcFunction } from 'devframe'
34
import { getMessagesHost } from './_define'
45

@@ -19,18 +20,34 @@ export const messagesList = defineRpcFunction({
1920
title: 'List messages',
2021
description: 'List the hub message feed: diagnostics, notifications, and tool output entries with level, category, labels, and positions.',
2122
},
22-
setup: ctx => ({
23-
handler: async (since?: number | null, instanceId?: string | null): Promise<DevframeMessagesListDelta> => {
24-
const host = getMessagesHost(ctx)
25-
if (!host) {
26-
return {
27-
entries: [],
28-
removedIds: [],
29-
version: 0,
30-
full: true,
23+
setup: (ctx) => {
24+
const cursors = new WeakMap<DevframeNodeRpcSessionMeta, Pick<DevframeMessagesListDelta, 'version' | 'instanceId'>>()
25+
return {
26+
async handler(this: { $meta?: DevframeNodeRpcSessionMeta } | void, since?: number | null, instanceId?: string | null): Promise<DevframeMessagesListDelta> {
27+
const host = getMessagesHost(ctx)
28+
if (!host) {
29+
return {
30+
entries: [],
31+
removedIds: [],
32+
version: 0,
33+
full: true,
34+
}
3135
}
32-
}
33-
return host.listSince(since, instanceId)
34-
},
35-
}),
36+
37+
// Live RPC binds the peer to the handler. Local reads and static builds
38+
// have no peer and need no connection-specific cursor.
39+
const peer = this?.$meta
40+
const initial = peer && cursors.get(peer)
41+
const knownInstance = instanceId === undefined && initial && since != null && since >= initial.version
42+
? initial.instanceId
43+
: instanceId
44+
const result = host.listSince(since, knownInstance)
45+
// A legacy reader starts with null, or presents an unknown number
46+
// on a fresh connection. Keep its snapshot cursor for overlapping reads.
47+
if (peer && instanceId === undefined && since !== undefined && result.full && (!initial || initial.instanceId !== result.instanceId))
48+
cursors.set(peer, { version: result.version, instanceId: result.instanceId })
49+
return result
50+
},
51+
}
52+
},
3653
})

‎plugins/messages/test/dev-server.test.ts‎

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ describe('messages dev-server (hub context)', () => {
7474
expect(delta.removedIds).toContain('from-server')
7575
})
7676

77-
it('returns full snapshots for foreign and legacy cursors over RPC', async () => {
77+
it('returns full snapshots for foreign and unrecognized numeric cursors over RPC', async () => {
7878
const rpc = connect(server)
7979
const full = await rpc.$call('devframes:plugin:messages:list') as DevframeMessagesListDelta
8080
for (const args of [
@@ -88,6 +88,27 @@ describe('messages dev-server (hub context)', () => {
8888
}
8989
})
9090

91+
it('keeps the removal when a legacy reader sees an ID reused on its connection', async () => {
92+
const rpc = connect(server)
93+
await server.ctx.messages.info('old toast', { id: 'reused', notify: true })
94+
const snapshot = await rpc.$call('devframes:plugin:messages:list', null) as DevframeMessagesListDelta
95+
const fresh = connect(server)
96+
const unknown = await fresh.$call('devframes:plugin:messages:list', snapshot.version) as DevframeMessagesListDelta
97+
expect(unknown.full).toBe(true)
98+
await server.ctx.messages.info('another entry', { id: 'other' })
99+
await rpc.$call('devframes:plugin:messages:list', snapshot.version)
100+
await server.ctx.messages.remove('reused')
101+
await server.ctx.messages.info('replacement', { id: 'reused', notify: false })
102+
103+
const delta = await rpc.$call('devframes:plugin:messages:list', snapshot.version) as DevframeMessagesListDelta
104+
expect(delta.full).toBe(false)
105+
expect(delta.removedIds).toEqual(['reused'])
106+
expect(delta.entries.map(entry => entry.id)).toEqual(['other', 'reused'])
107+
expect(delta.entries[1]).toMatchObject({ message: 'replacement', notify: false })
108+
const resumed = await fresh.$call('devframes:plugin:messages:list', unknown.version) as DevframeMessagesListDelta
109+
expect(resumed).toEqual(delta)
110+
})
111+
91112
it('add stamps browser origin; update patches; clear empties', async () => {
92113
const rpc = connect(server)
93114

0 commit comments

Comments
 (0)