helix-im 0.1.7

基于 Helix Core 的确定性 MessageV3 IM 业务模块
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
use crate::error::ImError;
use crate::module::ImModule;
use crate::state::CorrelationContext;
use helix_core::EffectSink;

impl ImModule {
    /// 恢复完成后重新读取当前 viewer 的频道列表。
    pub(crate) fn refresh_channel_list(
        &mut self,
        causation_id: Option<String>,
        out: &mut EffectSink,
    ) -> Result<bool, ImError> {
        let payload = serde_json::to_vec(&serde_json::json!({
            "req_id": causation_id,
        }))
        .map_err(|error| ImError::Serialize(format!("attached dialog refresh: {error}")))?;
        self.handle_query_command("im_query_dialog_list", &payload, out)?;
        Ok(true)
    }

    /// P6 投影/状态命令分发(薄壳,契约在 `crate::query`)。
    ///
    /// - `im_query_messages_by_channel`:进入 local-first 三段状态机;driver 不解释 fallback。
    /// - `im_delete_all_dialogs`:清内存 channel gate + emit 刷新(物理 truncate 缺原语,见 query.rs)。
    pub(crate) fn handle_query_command(
        &mut self,
        name: &str,
        payload: &[u8],
        out: &mut EffectSink,
    ) -> Result<(), ImError> {
        match name {
            "im_query_messages_by_channel" => {
                self.dispatch_message_query(payload, out)?;
            }
            "im_query_channel_sync_page" => {
                self.start_channel_sync_page(payload, out)?;
            }
            "im_complete_channel_sync" => {
                self.complete_channel_sync(payload, out)?;
            }
            "im_query_dialog_list" => {
                // UC-3:低频按需拉全量会话列表(off hot-path)→ Scan channel + 注册 DialogListQuery;
                // PortReply 回报后只 emit 一次 `im:read:result` typed Result(前端零计算)。
                let corr = self.alloc_corr_internal();
                let effect = self.build_dialog_list_query_for_runtime(payload, corr)?;
                self.state.corr_map.insert(
                    corr,
                    CorrelationContext::DialogListQuery {
                        causation_id: crate::query::read_relay::read_req_id(payload),
                    },
                );
                out.push(effect);
            }
            crate::query::channel_view_snapshot::QUERY_CHANNEL_VIEW_SNAPSHOT => {
                let channel_id = crate::query::channel_view_snapshot::parse_channel_id(payload)?;
                if self.config.auth_user_id.is_empty() || self.config.company_id.is_empty() {
                    return Err(crate::error::ImError::Parse(
                        "im_query_channel_view_snapshot requires RuntimeAuth user and company"
                            .into(),
                    ));
                }
                let corr = self.alloc_corr_internal();
                self.state.corr_map.insert(
                    corr,
                    CorrelationContext::ChannelViewSnapshotQuery {
                        channel_id,
                        causation_id: crate::query::read_relay::read_req_id(payload),
                        auth_user_id: self.config.auth_user_id.clone(),
                        company_id: self.config.company_id.clone(),
                    },
                );
                out.push(crate::query::channel_view_snapshot::query_effect(
                    channel_id, corr,
                ));
            }
            "im_query_subtopics" => {
                // G13d:只按 RuntimeAuth 查询本地 parent/topic 关系;空 parent 直接 typed-empty。
                let req_id = crate::query::read_relay::read_req_id(payload);
                let request = match crate::query::parse_subtopics_query(payload) {
                    Ok(request) => request,
                    Err(error) => {
                        tracing::warn!(error = ?error, "subtopic query rejected");
                        out.push(self.emit_subtopics_result_for_runtime(
                            req_id.as_deref(),
                            None,
                            b"[]",
                        ));
                        return Ok(());
                    }
                };
                let Some(parent_channel_id) = request.parent_channel_id.clone() else {
                    out.push(self.emit_subtopics_result_for_runtime(
                        req_id.as_deref(),
                        None,
                        b"[]",
                    ));
                    return Ok(());
                };
                let corr = self.alloc_corr_internal();
                let effect = match self.build_subtopics_query_for_runtime(&request, corr) {
                    Ok(effect) => effect,
                    Err(error) => {
                        tracing::warn!(error = ?error, "subtopic query scope rejected");
                        out.push(self.emit_subtopics_result_for_runtime(
                            req_id.as_deref(),
                            Some(parent_channel_id.as_str()),
                            b"[]",
                        ));
                        return Ok(());
                    }
                };
                self.state.corr_map.insert(
                    corr,
                    CorrelationContext::SubtopicsQuery {
                        parent_channel_id,
                        causation_id: req_id,
                    },
                );
                out.push(effect);
            }
            crate::query::pinned_projection::QUERY_PINNED_PROJECTION => {
                let req_id = crate::query::read_relay::read_req_id(payload).ok_or_else(|| {
                    ImError::Parse("im_query_pinned_projection requires req_id".to_string())
                })?;
                let channel_id = crate::query::pinned_projection::parse_channel_id(payload)?;
                let key = crate::query::pinned_projection::projection_key(
                    self.config.auth_user_id.as_str(),
                    channel_id,
                )?;
                let corr = self.alloc_corr_internal();
                self.state.corr_map.insert(
                    corr,
                    CorrelationContext::PinnedProjectionQuery { req_id, channel_id },
                );
                out.push(crate::query::pinned_projection::query_effect(key, corr));
            }
            "im_delete_all_dialogs" => {
                // 内存 channel gate 清空(core 拥有的状态;物理 DELETE 留待 truncate 原语,query.rs)。
                self.state.channels.clear();
                self.state.reset_recent_query_coverage();
                out.push(crate::query::emit_dialogs_cleared());
            }
            // G11b 上拉页:只用 reqId/scope/anchor correlation,不读取 attached visual window。
            n if n == crate::older_context::LOAD_OLDER_CONTEXT => {
                let (channel_id, anchor_post_id, anchor_create_at, page_size, request_id) =
                    crate::timeline_navigation::parse_older_request_with_anchor(payload)?;
                let state =
                    crate::timeline_navigation::TimelineNavigationState::older_without_window(
                        channel_id,
                        page_size,
                        request_id,
                        anchor_post_id,
                        anchor_create_at,
                    );
                self.start_timeline_navigation_or_local(state, out);
            }
            // G11c 下拉页:Go 解析 anchor,Helix 不保存 window/cursor。
            n if n == crate::timeline_navigation::LOAD_NEWER_CONTEXT => {
                let (channel_id, anchor_post_id, anchor_create_at, page_size, request_id) =
                    crate::timeline_navigation::parse_newer_request_with_anchor(payload)?;
                let state =
                    crate::timeline_navigation::TimelineNavigationState::newer_without_window(
                        channel_id,
                        page_size,
                        request_id,
                        anchor_post_id,
                        anchor_create_at,
                    );
                self.start_timeline_navigation_or_local(state, out);
            }
            // G11d 定位直接进入 Go context authority;两条命令名共享严格的持久读回链。
            n if n == crate::timeline_navigation::LOCATE_MESSAGE
                || n == crate::timeline_navigation::LOCATE_CONTEXT =>
            {
                let (channel_id, target_message_id, page_size, request_id, navigation_token) =
                    crate::timeline_navigation::parse_locate_request_for_command(payload, n)?;
                let navigation_token = navigation_token
                    .or_else(|| request_id.clone())
                    .unwrap_or_else(|| format!("locate:{target_message_id}"));
                let window_token = format!("timeline:{}", channel_id.as_str());
                self.state.timeline_state.begin_locate_navigation(
                    channel_id.as_str(),
                    window_token.as_str(),
                    navigation_token.as_str(),
                );
                let state = crate::timeline_navigation::TimelineNavigationState::locate(
                    channel_id,
                    window_token,
                    page_size,
                    request_id,
                    target_message_id,
                    navigation_token,
                );
                self.start_timeline_navigation_or_local(state, out);
            }
            _ => tracing::debug!("im: query dispatch miss '{}'", name),
        }
        Ok(())
    }

    /// 校验当前 RuntimeAuth 下的 channel-sync session,并发起有界本地 Scan。
    fn start_channel_sync_page(
        &mut self,
        payload: &[u8],
        out: &mut EffectSink,
    ) -> Result<(), ImError> {
        let request = match crate::channel_sync::parse_page_request(payload) {
            Ok(request) => request,
            Err(error) => {
                tracing::warn!(error = ?error, "channel sync page request rejected");
                if let Some(req_id) = crate::query::read_relay::read_req_id(payload) {
                    out.push(crate::read_relay::emit_read_error(
                        req_id.as_str(),
                        error.to_string().as_str(),
                    ));
                }
                return Ok(());
            }
        };
        let Some(session) = self.state.channel_sync_session.clone() else {
            tracing::debug!("channel sync page ignored before ready");
            if let Some(req_id) = request.req_id.as_deref() {
                out.push(crate::read_relay::emit_read_error(
                    req_id,
                    "channel sync session is not ready",
                ));
            }
            return Ok(());
        };
        if !session.matches_scope(
            request.channel_sync_session_id.as_str(),
            self.config.auth_user_id.as_str(),
            self.config.company_id.as_str(),
        ) {
            tracing::warn!("channel sync page scope mismatch");
            if let Some(req_id) = request.req_id.as_deref() {
                out.push(crate::read_relay::emit_read_error(
                    req_id,
                    "channel sync page scope mismatch",
                ));
            }
            return Ok(());
        }
        let offset = match request.next_cursor.as_deref() {
            None => 0,
            Some(cursor) => match crate::channel_sync::decode_cursor(cursor, &session) {
                Ok(offset) => offset,
                Err(error) => {
                    tracing::warn!(error = ?error, "channel sync cursor rejected");
                    if let Some(req_id) = request.req_id.as_deref() {
                        out.push(crate::read_relay::emit_read_error(
                            req_id,
                            error.to_string().as_str(),
                        ));
                    }
                    return Ok(());
                }
            },
        };
        let corr = self.alloc_corr_internal();
        let effect = match crate::channel_sync::page_scan_effect(
            corr,
            session.company_id.as_str(),
            offset,
        ) {
            Ok(effect) => effect,
            Err(error) => {
                tracing::warn!(error = ?error, "channel sync page scan rejected");
                if let Some(req_id) = request.req_id.as_deref() {
                    out.push(crate::read_relay::emit_read_error(
                        req_id,
                        error.to_string().as_str(),
                    ));
                }
                return Ok(());
            }
        };
        self.state.corr_map.insert(
            corr,
            CorrelationContext::ChannelSyncPage {
                channel_sync_session_id: session.channel_sync_session_id,
                generation: session.generation,
                offset,
                req_id: request.req_id,
            },
        );
        tracing::info!(
            corr = corr.raw(),
            offset,
            "channel sync page local scan scheduled"
        );
        out.push(effect);
        Ok(())
    }

    /// 关闭当前身份绑定的频道分页 session;重复 complete 是幂等 no-op。
    fn complete_channel_sync(
        &mut self,
        payload: &[u8],
        out: &mut EffectSink,
    ) -> Result<(), ImError> {
        let request = match crate::channel_sync::parse_complete_request(payload) {
            Ok(request) => request,
            Err(error) => {
                tracing::warn!(error = ?error, "channel sync complete request rejected");
                if let Some(req_id) = crate::query::read_relay::read_req_id(payload) {
                    out.push(crate::read_relay::emit_read_error(
                        req_id.as_str(),
                        error.to_string().as_str(),
                    ));
                }
                return Ok(());
            }
        };
        let Some(session) = self.state.channel_sync_session.as_mut() else {
            tracing::debug!("channel sync complete ignored before ready");
            if let Some(req_id) = request.req_id.as_deref() {
                out.push(crate::read_relay::emit_read_error(
                    req_id,
                    "channel sync session is not ready",
                ));
            }
            return Ok(());
        };
        if session.channel_sync_session_id != request.channel_sync_session_id
            || session.account_id != self.config.auth_user_id
            || session.company_id != self.config.company_id
        {
            tracing::warn!("channel sync complete scope mismatch or already completed");
            if let Some(req_id) = request.req_id.as_deref() {
                out.push(crate::read_relay::emit_read_error(
                    req_id,
                    "channel sync complete scope mismatch",
                ));
            }
            return Ok(());
        }
        let first_complete = !session.completed;
        session.completed = true;
        let session_id = session.channel_sync_session_id.clone();
        let generation = session.generation;
        if first_complete {
            // 关闭仅撤销当前 session 的在途 page;其他业务 correlation 不受影响。
            self.state.corr_map.retain(|_, context| {
                !matches!(
                    context,
                    CorrelationContext::ChannelSyncPage {
                        channel_sync_session_id: pending_session_id,
                        generation: pending_generation,
                        ..
                    }
                    | CorrelationContext::ChannelSyncPageMemberSnapshot {
                        channel_sync_session_id: pending_session_id,
                        generation: pending_generation,
                        ..
                    } if pending_session_id == &session_id && *pending_generation == generation
                )
            });
            // 保留一次 typed complete,供非 query waiter 的客户端消费。
            out.push(
                crate::event::channel_sync::complete(session_id.as_str(), generation)?
                    .into_effect(),
            );
        }
        if let Some(req_id) = request.req_id.as_deref() {
            out.push(crate::read_relay::emit_read_body(
                req_id,
                serde_json::json!({
                    "completed": true,
                    "channelSyncSessionId": session_id,
                    "generation": generation,
                }),
            ));
        }
        if first_complete && self.state.channel_sync_refresh_pending {
            self.state.channel_sync_refresh_pending = false;
            tracing::info!(
                "channel-sync-ready 延迟批次已合并:当前分页 session complete 后重新开放"
            );
            self.open_channel_sync_session(out)?;
        }
        Ok(())
    }
}

#[cfg(test)]
mod phase2_contract_tests {
    use super::*;
    use helix_core::effect::Effect;
    use helix_core::EffectSink;

    const CHANNEL_ID: &str = "chfixx0000000000000000002a";

    /// 冻结查询入口的单 corr 注册、20 页长和当前窗口代际。
    #[test]
    fn message_query_registers_one_correlated_generation() {
        let mut module = ImModule::new(crate::module::ImConfig::default());
        let payload = serde_json::to_vec(&serde_json::json!({
            "channel_id": CHANNEL_ID,
            "pageSize": 20,
            "req_id": "phase2-query-1",
        }))
        .expect("query payload serializes");
        let mut out = EffectSink::new();

        module
            .handle_query_command("im_query_messages_by_channel", &payload, &mut out)
            .expect("query command is accepted");

        let Some(Effect::Persist { corr, .. }) = out.as_slice().first() else {
            panic!("query command must start one local Scan persist");
        };
        assert_eq!(module.state.corr_map.len(), 1);
        assert!(matches!(
            module.state.corr_map.get(corr),
            Some(CorrelationContext::MessageQueryLocal {
                request,
                query_generation: 1,
                ..
            }) if request.limit == 20 && request.channel_id.as_str() == CHANNEL_ID
        ));
    }

    /// 置顶查询只发账号级本地 Get,不允许在 query 热路径产生 HTTP。
    #[test]
    fn pinned_projection_query_registers_local_get() {
        let mut config = crate::module::ImConfig::default();
        config.auth_user_id = "user-a".to_string();
        let mut module = ImModule::new(config);
        let mut out = EffectSink::new();

        module
            .handle_query_command(
                crate::query::pinned_projection::QUERY_PINNED_PROJECTION,
                br#"{"channel_id":"chfixx0000000000000000002a","req_id":"pin-local-1"}"#,
                &mut out,
            )
            .expect("pinned local query accepted");

        let Some(Effect::Persist { corr, ops }) = out.as_slice().first() else {
            panic!("pinned local query must emit Persist Get");
        };
        assert!(matches!(
            ops.first(),
            Some(helix_core::effect::StorageOp::Get(spec))
                if spec.table == "channel_pinned_projection"
                    && spec.key_col == "projection_key"
        ));
        assert!(matches!(
            module.state.corr_map.get(corr),
            Some(CorrelationContext::PinnedProjectionQuery { req_id, channel_id })
                if req_id == "pin-local-1" && channel_id.as_str() == CHANNEL_ID
        ));
        assert!(!out
            .as_slice()
            .iter()
            .any(|effect| matches!(effect, Effect::Http { .. } | Effect::HttpFire { .. })));
    }

    /// ChannelViewSnapshot 只读 canonical channel 行,账号与租户来自 RuntimeAuth。
    #[test]
    fn channel_view_snapshot_registers_scoped_local_get() {
        let mut config = crate::module::ImConfig::default();
        config.auth_user_id = "user-a".to_string();
        config.company_id = "team-a".to_string();
        let mut module = ImModule::new(config);
        let mut out = EffectSink::new();

        module
            .handle_query_command(
                crate::query::channel_view_snapshot::QUERY_CHANNEL_VIEW_SNAPSHOT,
                br#"{"channelId":"chfixx0000000000000000002a","req_id":"view-local-1"}"#,
                &mut out,
            )
            .expect("channel view local query accepted");

        let Some(Effect::Persist { corr, ops }) = out.as_slice().first() else {
            panic!("channel view query must emit Persist Get");
        };
        assert!(matches!(
            ops.first(),
            Some(helix_core::effect::StorageOp::Get(spec))
                if spec.table == "channel" && spec.key_col == "id"
        ));
        assert!(matches!(
            module.state.corr_map.get(corr),
            Some(CorrelationContext::ChannelViewSnapshotQuery {
                channel_id,
                causation_id,
                auth_user_id,
                company_id,
            }) if channel_id.as_str() == CHANNEL_ID
                && causation_id.as_deref() == Some("view-local-1")
                && auth_user_id == "user-a"
                && company_id == "team-a"
        ));
        assert!(!out
            .as_slice()
            .iter()
            .any(|effect| matches!(effect, Effect::Http { .. } | Effect::HttpFire { .. })));
    }

    /// 账号/会话边界必须使旧窗口代际失效,避免迟到回包污染新身份。
    #[test]
    fn identity_reset_restarts_message_query_generation() {
        let mut module = ImModule::new(crate::module::ImConfig::default());
        let channel_id =
            crate::state::ChannelId::from_str(CHANNEL_ID).expect("test channel id is valid");
        assert_eq!(
            module
                .state
                .begin_message_query_generation(channel_id, "latest"),
            1
        );
        assert_eq!(
            module
                .state
                .begin_message_query_generation(channel_id, "latest"),
            2
        );
        let old_epoch = module.state.query_session_epoch;
        module.state.reset_message_v3_identity();
        assert!(module.state.query_session_epoch > old_epoch);
        assert_eq!(
            module
                .state
                .begin_message_query_generation(channel_id, "latest"),
            1
        );
        assert!(!module
            .state
            .is_current_message_query_generation(channel_id, "latest", 2));

        module.state.channel_sync_session = Some(crate::channel_sync::ChannelSyncSession::new(
            "session-a".to_string(),
            1,
            "user-a",
            "company-a",
        ));
        module.state.reset_message_v3_identity();
        assert!(module.state.channel_sync_session.is_none());
    }

    /// 未附着窗口的 older 请求直接进入 Timeline Reactor authority HTTP,不读取视觉窗口。
    #[test]
    fn older_without_attached_window_uses_direct_authority_http() {
        let mut module = ImModule::new(crate::module::ImConfig::default());
        let mut out = EffectSink::new();
        module
            .handle_query_command(
                crate::older_context::LOAD_OLDER_CONTEXT,
                &serde_json::to_vec(&serde_json::json!({
                    "channel_id": CHANNEL_ID,
                    "anchor_post_id": "anchor-not-attached",
                    "pageSize": 40,
                    "req_id": "older-failure-1",
                }))
                .expect("older payload serializes"),
                &mut out,
            )
            .expect("missing attachment must use direct authority HTTP");

        let Effect::Http { req, .. } = out.as_slice().first().expect("authority HTTP emitted")
        else {
            panic!("missing attachment must emit posts/getPostsAfterIndex HTTP");
        };
        assert_eq!(req.method, "POST");
        assert!(req.url.ends_with("/posts/getPostsAfterIndex"));
        let body: serde_json::Value =
            serde_json::from_slice(req.body.as_ref().expect("authority request body")).unwrap();
        assert_eq!(body["postIds"], "anchor-not-attached");
        assert_eq!(body["direction"], "older");
        assert_eq!(body["pageSize"], 40);
        assert_eq!(body["anchor"]["postId"], "anchor-not-attached");
        assert!(body["anchor"]["createAt"].is_null());
        assert!(req
            .headers
            .iter()
            .any(|(key, value)| key == "Cses-Track-Id" && value == "older-failure-1"));
    }

    /// ready 后 page 只注册当前 session 的 account-local Scan,未知 session fail-closed。
    #[test]
    fn channel_sync_page_registers_scoped_scan() {
        let mut config = crate::module::ImConfig::default();
        config.auth_user_id = "user-a".to_string();
        config.company_id = "company-a".to_string();
        let mut module = ImModule::new(config);
        module.state.channel_sync_session = Some(crate::channel_sync::ChannelSyncSession::new(
            "session-a".to_string(),
            4,
            "user-a",
            "company-a",
        ));
        let mut out = EffectSink::new();
        module
            .handle_query_command(
                "im_query_channel_sync_page",
                br#"{"channel_sync_session_id":"session-a","next_cursor":null,"page_size":20,"req_id":"page-1"}"#,
                &mut out,
            )
            .expect("page query accepted");
        let Some(Effect::Persist { corr, ops }) = out.as_slice().first() else {
            panic!("page query must emit local scan");
        };
        assert!(matches!(
            ops.first(),
            Some(helix_core::effect::StorageOp::Scan(scan))
                if scan.table == "channel"
                    && scan.limit == Some(21)
                    && matches!(
                        scan.filter,
                        Some(("team_id", helix_core::effect::SqlValue::Text(ref value)))
                            if value == "company-a"
                    )
        ));
        assert!(matches!(
            module.state.corr_map.get(corr),
            Some(CorrelationContext::ChannelSyncPage {
                channel_sync_session_id,
                generation: 4,
                offset: 0,
                req_id: Some(req_id),
            }) if channel_sync_session_id == "session-a" && req_id == "page-1"
        ));

        let session = module.state.channel_sync_session.as_ref().unwrap();
        let cursor = crate::channel_sync::encode_cursor(session, 20);
        module
            .handle_query_command(
                "im_query_channel_sync_page",
                &serde_json::to_vec(&serde_json::json!({
                    "channel_sync_session_id": "session-a",
                    "next_cursor": cursor,
                    "page_size": 20,
                    "req_id": "page-2",
                }))
                .unwrap(),
                &mut out,
            )
            .expect("second page query accepted");
        let Some(Effect::Persist { ops, .. }) = out.as_slice().last() else {
            panic!("second page query must emit local scan");
        };
        assert!(matches!(
            ops.first(),
            Some(helix_core::effect::StorageOp::Scan(scan)) if scan.limit == Some(41)
        ));

        let before = out.as_slice().len();
        module
            .handle_query_command(
                "im_query_channel_sync_page",
                br#"{"channel_sync_session_id":"other-session","page_size":20}"#,
                &mut out,
            )
            .expect("unknown session is handled");
        assert_eq!(out.as_slice().len(), before);
    }

    /// complete 只关闭同一身份的会话,并为 Host waiter 提供成功/失败回灌。
    #[test]
    fn channel_sync_complete_is_scoped_and_idempotent() {
        let mut config = crate::module::ImConfig::default();
        config.auth_user_id = "user-a".to_string();
        config.company_id = "company-a".to_string();
        let mut module = ImModule::new(config);
        module.state.channel_sync_session = Some(crate::channel_sync::ChannelSyncSession::new(
            "session-a".to_string(),
            4,
            "user-a",
            "company-a",
        ));
        let mut out = EffectSink::new();
        module
            .handle_query_command(
                "im_complete_channel_sync",
                br#"{"channel_sync_session_id":"other-session","req_id":"bad-complete"}"#,
                &mut out,
            )
            .expect("wrong scope is handled");
        assert_eq!(out.as_slice().len(), 1);
        let error: serde_json::Value = match &out.as_slice()[0] {
            Effect::Emit { event } => serde_json::from_slice(event.0.as_ref()).unwrap(),
            _ => panic!("scope failure must resolve query waiter"),
        };
        assert_eq!(error["event"], "im:read:result");
        assert_eq!(error["data"]["req_id"], "bad-complete");
        assert!(error["data"].get("error").is_some());
        out.clear();
        module
            .handle_query_command(
                "im_complete_channel_sync",
                br#"{"channel_sync_session_id":"session-a","req_id":"done-complete"}"#,
                &mut out,
            )
            .expect("complete emits event");
        assert_eq!(out.as_slice().len(), 2);
        let read_result = out
            .as_slice()
            .iter()
            .find_map(|effect| match effect {
                Effect::Emit { event } => {
                    let value: serde_json::Value = serde_json::from_slice(event.0.as_ref()).ok()?;
                    (value["event"] == "im:read:result").then_some(value)
                }
                _ => None,
            })
            .expect("complete waiter result");
        assert_eq!(read_result["data"]["req_id"], "done-complete");
        assert_eq!(read_result["data"]["body"]["completed"], true);
        assert!(
            module
                .state
                .channel_sync_session
                .as_ref()
                .unwrap()
                .completed
        );
        out.clear();
        module
            .handle_query_command(
                "im_complete_channel_sync",
                br#"{"channel_sync_session_id":"session-a","req_id":"done-again"}"#,
                &mut out,
            )
            .expect("duplicate complete is handled");
        assert_eq!(out.as_slice().len(), 1);
        let duplicate: serde_json::Value = match &out.as_slice()[0] {
            Effect::Emit { event } => serde_json::from_slice(event.0.as_ref()).unwrap(),
            _ => panic!("duplicate complete must resolve waiter"),
        };
        assert_eq!(duplicate["event"], "im:read:result");
    }
}