1- // Imessage plugin module implements catchup behavior.
2- import { createHash } from "node:crypto" ;
31import { KeyedAsyncQueue } from "openclaw/plugin-sdk/keyed-async-queue" ;
42import { resolveIntegerOption } from "openclaw/plugin-sdk/number-runtime" ;
53import type { PluginStateSyncKeyedStore } from "openclaw/plugin-sdk/plugin-state-runtime" ;
64import { getIMessageRuntime } from "../runtime.js" ;
5+ import {
6+ IMESSAGE_CATCHUP_CURSOR_NAMESPACE ,
7+ IMESSAGE_CATCHUP_CURSOR_MAX_ENTRIES ,
8+ resolveIMessageCatchupCursorKey ,
9+ capFailureRetriesMap ,
10+ type IMessageCatchupCursor ,
11+ } from "../state-contract.js" ;
712
813// iMessage inbound catchup. When the gateway is offline (crash, restart, mac
914// sleep, machine off), `imsg watch` resumes from current state and ignores
@@ -24,13 +29,6 @@ const MAX_PER_RUN_LIMIT = 500;
2429const DEFAULT_FIRST_RUN_LOOKBACK_MINUTES = 30 ;
2530const DEFAULT_MAX_FAILURE_RETRIES = 10 ;
2631const MAX_MAX_FAILURE_RETRIES = 1_000 ;
27- // Defense-in-depth bound on the retry map. The cursor is one plugin-state
28- // value, so keep the retry payload well below the 64KB store limit.
29- const MAX_FAILURE_RETRY_MAP_SIZE = 512 ;
30- const MAX_FAILURE_RETRY_MAP_JSON_BYTES = 48_000 ;
31- const textEncoder = new TextEncoder ( ) ;
32- export const IMESSAGE_CATCHUP_CURSOR_NAMESPACE = "imessage.catchup-cursors" ;
33- export const IMESSAGE_CATCHUP_CURSOR_MAX_ENTRIES = 256 ;
3432const cursorWriteQueue = new KeyedAsyncQueue ( ) ;
3533
3634type IMessageCatchupConfig = {
@@ -41,28 +39,6 @@ type IMessageCatchupConfig = {
4139 maxFailureRetries ?: number ;
4240} ;
4341
44- export type IMessageCatchupCursor = {
45- /** Timestamp (ms since epoch) of the highest-watermark message we processed. */
46- lastSeenMs : number ;
47- /** ROWID of the highest-watermark processed message. Monotonic in chat.db. */
48- lastSeenRowid : number ;
49- /** UTC ms timestamp of the most recent cursor write. */
50- updatedAt : number ;
51- /**
52- * Per-GUID failure counter, preserved across runs. Two states:
53- * - `1 <= count < maxFailureRetries`: the GUID is still retrying and
54- * continues to hold the cursor back.
55- * - `count >= maxFailureRetries`: catchup has given up on the GUID. The
56- * message is skipped on sight (no dispatch attempt) and the cursor no
57- * longer waits on it. Entry stays in the map until the cursor naturally
58- * advances past the message's timestamp.
59- *
60- * A successful dispatch removes the entry. Optional on the persisted shape
61- * so older cursor files without this field load cleanly.
62- */
63- failureRetries ?: Record < string , number > ;
64- } ;
65-
6642export type IMessageCatchupRow = {
6743 guid : string ;
6844 rowid : number ;
@@ -97,10 +73,6 @@ export type IMessageCatchupSummary = {
9773 windowEndMs : number ;
9874} ;
9975
100- export function resolveIMessageCatchupCursorKey ( accountId : string ) : string {
101- return createHash ( "sha256" ) . update ( accountId , "utf8" ) . digest ( "hex" ) . slice ( 0 , 32 ) ;
102- }
103-
10476function openCatchupCursorStore ( ) : PluginStateSyncKeyedStore < IMessageCatchupCursor > {
10577 return getIMessageRuntime ( ) . state . openSyncKeyedStore < IMessageCatchupCursor > ( {
10678 namespace : IMESSAGE_CATCHUP_CURSOR_NAMESPACE ,
@@ -209,35 +181,6 @@ async function saveIMessageCatchupCursor(
209181 } ) ;
210182}
211183
212- /**
213- * Bound the retry map so a pathological storm of unique failing GUIDs
214- * cannot grow the cursor file without limit. Keeps the `maxSize` entries
215- * with the highest counts (closest to give-up) when over the bound.
216- */
217- export function capFailureRetriesMap (
218- map : Record < string , number > ,
219- maxSize : number = MAX_FAILURE_RETRY_MAP_SIZE ,
220- maxBytes : number = MAX_FAILURE_RETRY_MAP_JSON_BYTES ,
221- ) : Record < string , number > {
222- const entries = Object . entries ( map ) ;
223- if ( entries . length <= maxSize && textEncoder . encode ( JSON . stringify ( map ) ) . byteLength <= maxBytes ) {
224- return map ;
225- }
226- // Sort by count desc; stable tiebreak on guid string so the retained set
227- // is deterministic across runs (important for cursor-file diffing during
228- // debugging).
229- entries . sort ( ( a , b ) => b [ 1 ] - a [ 1 ] || a [ 0 ] . localeCompare ( b [ 0 ] ) ) ;
230- const capped : Record < string , number > = { } ;
231- for ( const [ guid , count ] of entries . slice ( 0 , maxSize ) ) {
232- capped [ guid ] = count ;
233- if ( textEncoder . encode ( JSON . stringify ( capped ) ) . byteLength > maxBytes ) {
234- delete capped [ guid ] ;
235- break ;
236- }
237- }
238- return capped ;
239- }
240-
241184export type ResolvedCatchupConfig = {
242185 enabled : boolean ;
243186 maxAgeMinutes : number ;
0 commit comments