helix-im 0.1.21

基于 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
//! 文字接龙 PortReply:HTTP/WS authority 先过同一 projection barrier,再发布事件。

use crate::chain::{self, ChainAuthority, ChainMutation, ChainMutationState, ChainRequest};
use crate::error::ImError;
use crate::state::{ChannelId, CorrelationContext};
use helix_core::effect::Effect;
use helix_core::tick::{PortError, PortOutcome};
use helix_core::EffectSink;

impl super::ImModule {
    /// 处理接龙 HTTP 回包:transport/business 不确定时先持久化 RECONCILING,再走 reconcile。
    pub(crate) fn handle_chain_http_reply(
        &mut self,
        request: ChainRequest,
        outcome: &PortOutcome,
        out: &mut EffectSink,
    ) -> Result<(), ImError> {
        match outcome {
            PortOutcome::Ok(reply) => {
                match chain::decode_http_authority(reply.0.as_ref(), &request) {
                    Ok(authority) => self.queue_chain_authority(request, authority, out),
                    Err(chain::ChainReplyError::Rejected { code }) => {
                        if request.is_mutation() {
                            self.queue_chain_mutation_status(
                                request,
                                ChainMutationState::Rejected,
                                None,
                                Some(code.as_str()),
                                false,
                                out,
                            )?;
                        } else if let Some(req_id) = Self::chain_req_id(&request) {
                            out.push(crate::query::read_relay::emit_read_error(&req_id, &code));
                        }
                        Ok(())
                    }
                    Err(chain::ChainReplyError::Invalid { message }) => {
                        let retry_reconcile = request.command != "post_chain_reconcile";
                        if request.is_mutation() {
                            self.queue_chain_mutation_status(
                                request,
                                ChainMutationState::Reconciling,
                                None,
                                Some("AUTHORITY_UNKNOWN"),
                                retry_reconcile,
                                out,
                            )?;
                        } else if let Some(req_id) = Self::chain_req_id(&request) {
                            out.push(crate::query::read_relay::emit_read_error(
                                &req_id,
                                "AUTHORITY_INVALID",
                            ));
                        } else {
                            tracing::warn!(command = %request.command, error = %message, "chain read authority rejected");
                        }
                        Ok(())
                    }
                }
            }
            PortOutcome::Err(error) => {
                if !request.is_mutation() {
                    if let Some(req_id) = Self::chain_req_id(&request) {
                        out.push(crate::query::read_relay::emit_read_error(
                            &req_id,
                            "CHAIN_READ_FAILED",
                        ));
                    } else {
                        tracing::warn!(command = %request.command, error = ?error, "chain read HTTP failed");
                    }
                    return Ok(());
                }
                let (state, code, retry_reconcile) = match error {
                    PortError::Timeout | PortError::Network | PortError::Http(500..=599) => (
                        ChainMutationState::Reconciling,
                        "TRANSPORT_UNKNOWN",
                        request.command != "post_chain_reconcile",
                    ),
                    PortError::Http(_) => (ChainMutationState::Rejected, "HTTP_REJECTED", false),
                    PortError::Storage(_) | PortError::Other(_) => (
                        ChainMutationState::Reconciling,
                        "TRANSPORT_UNKNOWN",
                        request.command != "post_chain_reconcile",
                    ),
                };
                self.queue_chain_mutation_status(
                    request,
                    state,
                    None,
                    Some(code),
                    retry_reconcile,
                    out,
                )
            }
        }
    }

    /// 从接龙 command 的 Host-owned payload 提取结构化读回 req_id。
    fn chain_req_id(request: &ChainRequest) -> Option<String> {
        request
            .payload
            .get("req_id")
            .and_then(serde_json::Value::as_str)
            .filter(|value| !value.is_empty())
            .map(str::to_string)
    }

    /// 把 authority 编译为 ChainSummary/Entry/window/viewer/mutation 单事务写集。
    fn queue_chain_authority(
        &mut self,
        request: ChainRequest,
        authority: ChainAuthority,
        out: &mut EffectSink,
    ) -> Result<(), ImError> {
        if request.command != "post_chain_reconcile"
            && authority.event_id.as_ref().is_some_and(|event_id| {
                self.state.seen_chain_event_ids.contains(event_id)
                    || self.state.pending_chain_event_ids.contains(event_id)
            })
        {
            return Ok(());
        }
        if request.command != "post_chain_get"
            && request.command != "post_chain_reconcile"
            && request.command != "post_chain_update_draft"
            && request.command != "post_chain_mark_read"
            && authority.event_seq.is_none()
            && authority.revision > 0
            && self
                .state
                .chain_revisions
                .get(&authority.chain_id)
                .is_some_and(|revision| authority.revision <= *revision)
        {
            return Ok(());
        }
        let mut cursor_seq = None;
        if let Some(event_seq) = authority.event_seq {
            let channel_id = ChannelId::from_str(&authority.channel_id).ok_or_else(|| {
                ImError::Parse("chain authority has invalid channel_id".to_string())
            })?;
            let channel = self
                .state
                .channels
                .entry(channel_id)
                .or_insert_with(|| crate::channel::Channel::new(channel_id, 0));
            // Canonical channel_stream_event may already have advanced this shared cursor; in that
            // case retain the chain authority for its own projection instead of dropping it as dup.
            if event_seq > channel.cursor.value() && !channel.admit_chain_event_seq(event_seq, out)
            {
                return Ok(());
            }
            cursor_seq = Some(event_seq);
        }
        let mutation_state = request
            .client_mutation_id
            .as_ref()
            .map(|_| ChainMutationState::Confirmed);
        let mut ops = chain::persist_ops(&authority, &request, mutation_state, None);
        if let Some(event_seq) = cursor_seq {
            let channel_id = ChannelId::from_str(&authority.channel_id).ok_or_else(|| {
                ImError::Parse("chain authority has invalid channel_id".to_string())
            })?;
            // Chain 与普通 post 共用此 monotonic cursor;不创建 chain 专用 eventSeq。
            ops.push(crate::acl::to_effect::advance_cursor_op(
                channel_id, event_seq,
            ));
        }
        if ops.is_empty() {
            return Err(ImError::Parse(
                "chain authority produced empty persist set".to_string(),
            ));
        }
        if let Some(event_id) = authority.event_id.as_ref() {
            self.state.pending_chain_event_ids.insert(event_id.clone());
        }
        let corr = self.alloc_corr_internal();
        let event_name = if request.command == "post_chain_reconcile" {
            chain::success_event_name(&request.command).to_string()
        } else {
            authority
                .event_type
                .clone()
                .unwrap_or_else(|| request.command.clone())
        };
        self.state.corr_map.insert(
            corr,
            CorrelationContext::ChainPersist {
                request: Box::new(request),
                authority: Box::new(authority),
                event_name,
                mutation_state,
                error_code: None,
            },
        );
        out.push(Effect::PersistAtomic { corr, ops });
        Ok(())
    }

    /// 为 RECONCILING/REJECTED mutation 写入 mutation 表,成功后才允许发状态事件。
    fn queue_chain_mutation_status(
        &mut self,
        request: ChainRequest,
        state: ChainMutationState,
        entry_id: Option<String>,
        error_code: Option<&str>,
        retry_reconcile: bool,
        out: &mut EffectSink,
    ) -> Result<(), ImError> {
        let Some(client_mutation_id) = request.client_mutation_id.as_deref() else {
            return Ok(());
        };
        let corr = self.alloc_corr_internal();
        let op = chain::mutation_op(
            &request,
            client_mutation_id,
            state,
            entry_id.as_deref(),
            error_code,
        );
        self.state.corr_map.insert(
            corr,
            CorrelationContext::ChainMutationPersist {
                request: Box::new(request),
                state,
                entry_id,
                error_code: error_code.map(str::to_string),
                retry_reconcile,
            },
        );
        out.push(Effect::PersistAtomic {
            corr,
            ops: vec![op],
        });
        Ok(())
    }

    /// PersistAtomic 成功后推进既有 Channel cursor,并发布唯一 chain event。
    pub(crate) fn handle_chain_persist_reply(
        &mut self,
        request: ChainRequest,
        authority: ChainAuthority,
        event_name: String,
        mutation_state: Option<ChainMutationState>,
        error_code: Option<String>,
        outcome: &PortOutcome,
        out: &mut EffectSink,
    ) -> Result<(), ImError> {
        // WS canonical stream may have committed the shared cursor before this chain authority
        // reply arrives; the chain projection still needs its own durable tables and terminal event.
        if authority
            .event_id
            .as_ref()
            .is_some_and(|event_id| self.state.seen_chain_event_ids.contains(event_id))
        {
            return Ok(());
        }
        if !matches!(outcome, PortOutcome::Ok(_)) {
            if let Some(event_id) = authority.event_id.as_ref() {
                self.state.pending_chain_event_ids.remove(event_id);
                self.state.seen_chain_event_ids.remove(event_id);
            }
            if let Some(client_mutation_id) = request.client_mutation_id.as_deref() {
                if let Some(mutation) = self.state.chain_mutations.get_mut(client_mutation_id) {
                    mutation.state = ChainMutationState::Reconciling;
                    mutation.error_code = Some("PERSIST_FAILED".to_string());
                }
            }
            tracing::warn!(chain_id = %authority.chain_id, "chain projection persist failed; suppress event");
            return Ok(());
        }
        if let Some(event_seq) = authority.event_seq {
            let channel_id = ChannelId::from_str(&authority.channel_id).ok_or_else(|| {
                ImError::Parse("chain authority has invalid channel_id".to_string())
            })?;
            let cursor_already_committed = self
                .state
                .channels
                .get(&channel_id)
                .is_some_and(|channel| event_seq <= channel.cursor.value());
            let committed = if cursor_already_committed {
                true
            } else {
                self.state
                    .channels
                    .get_mut(&channel_id)
                    .map(|channel| channel.commit_contiguous_after_atomic(event_seq, out))
                    .transpose()?
                    .unwrap_or(false)
            };
            if !committed {
                // Persist 已落库但 cursor 不再连续时,不能把 eventId 留在 pending 集合;否则
                // 后续 WS/sync 重放会被永久去重,既没有 Emit 也没有补偿入口。
                if let Some(event_id) = authority.event_id.as_ref() {
                    self.state.pending_chain_event_ids.remove(event_id);
                    self.state.seen_chain_event_ids.remove(event_id);
                }
                tracing::warn!(chain_id = %authority.chain_id, event_seq = event_seq.0, "chain cursor commit no longer contiguous");
                return Ok(());
            }
        }
        if let Some(event_id) = authority.event_id.as_ref() {
            self.state.pending_chain_event_ids.remove(event_id);
            self.state.seen_chain_event_ids.insert(event_id.clone());
        }
        if authority.revision > 0 {
            self.state
                .chain_revisions
                .insert(authority.chain_id.clone(), authority.revision);
        }
        self.update_chain_mutation(
            &request,
            mutation_state,
            authority.entry.as_ref().map(|entry| entry.entry_id.clone()),
            error_code.clone(),
        );
        if request.command == "post_chain_get" {
            if let Some(req_id) = request
                .payload
                .get("req_id")
                .and_then(serde_json::Value::as_str)
                .filter(|value| !value.is_empty())
            {
                out.push(crate::query::read_relay::emit_read_body(
                    req_id,
                    authority.raw.clone(),
                ));
            }
            return Ok(());
        }
        let event_name = if event_name.starts_with("im:post_chain:") {
            event_name.as_str()
        } else {
            chain::success_event_name(&event_name)
        };
        let bytes = chain::event_bytes(
            event_name,
            &request,
            Some(&authority),
            mutation_state,
            error_code.as_deref(),
        )?;
        out.push(Effect::Emit {
            event: helix_core::effect::DomainEventBytes(bytes::Bytes::from(bytes)),
        });
        Ok(())
    }

    /// PersistAtomic 成功后结算 RECONCILING/REJECTED,并按需发起同 operationId reconcile。
    pub(crate) fn handle_chain_mutation_persist_reply(
        &mut self,
        request: ChainRequest,
        state: ChainMutationState,
        entry_id: Option<String>,
        error_code: Option<String>,
        retry_reconcile: bool,
        outcome: &PortOutcome,
        out: &mut EffectSink,
    ) -> Result<(), ImError> {
        if !matches!(outcome, PortOutcome::Ok(_)) {
            tracing::warn!(command = %request.command, "chain mutation state persist failed; suppress event");
            return Ok(());
        }
        self.update_chain_mutation(&request, Some(state), entry_id, error_code.clone());
        let event_name = if state == ChainMutationState::Rejected {
            chain::rejected_event_name(&request.command)
        } else {
            "im:post_chain:reconcile"
        };
        let bytes = chain::event_bytes(
            event_name,
            &request,
            None,
            Some(state),
            error_code.as_deref(),
        )?;
        out.push(Effect::Emit {
            event: helix_core::effect::DomainEventBytes(bytes::Bytes::from(bytes)),
        });
        if retry_reconcile && state == ChainMutationState::Reconciling {
            let reconcile = reconcile_request(&request)?;
            self.start_chain_http(reconcile, out)?;
        }
        Ok(())
    }

    /// 只有 PortReply 成功后才改变内存 mutation 镜像,保持 durable/renderer 一致。
    fn update_chain_mutation(
        &mut self,
        request: &ChainRequest,
        state: Option<ChainMutationState>,
        entry_id: Option<String>,
        error_code: Option<String>,
    ) {
        let (Some(client_mutation_id), Some(operation_id), Some(state)) = (
            request.client_mutation_id.as_ref(),
            request.operation_id.as_ref(),
            state,
        ) else {
            return;
        };
        self.state.chain_mutations.insert(
            client_mutation_id.clone(),
            ChainMutation {
                client_mutation_id: client_mutation_id.clone(),
                operation_id: operation_id.clone(),
                state,
                entry_id,
                error_code,
            },
        );
    }
}

/// 从原始 mutation request 派生同 operationId 的 reconcile command。
fn reconcile_request(request: &ChainRequest) -> Result<ChainRequest, ImError> {
    let mut payload = MapExt::from_request(request);
    payload.insert(
        "operation_id".to_string(),
        serde_json::json!(request.operation_id.clone().unwrap_or_default()),
    );
    let bytes =
        serde_json::to_vec(&payload).map_err(|error| ImError::Serialize(error.to_string()))?;
    chain::request_from_command("post_chain_reconcile", &bytes)
}

/// 构造 reconcile 所需的最小 canonical payload。
struct MapExt;

impl MapExt {
    /// 过滤原始 append/create 字段,只保留 reconcile 合同。
    fn from_request(request: &ChainRequest) -> serde_json::Map<String, serde_json::Value> {
        let mut map = serde_json::Map::new();
        map.insert(
            "channel_id".to_string(),
            serde_json::json!(request.channel_id),
        );
        map.insert("chain_id".to_string(), serde_json::json!(request.chain_id));
        if let Some(value) = &request.client_mutation_id {
            map.insert("client_mutation_id".to_string(), serde_json::json!(value));
        }
        if let Some(value) = &request.device_id {
            map.insert("device_id".to_string(), serde_json::json!(value));
        }
        map
    }
}