#constraints

    职责:将明确的事件关联与用户先验转换为差分约束,并保存证据来源。

    #入口与约定

    build(correlation, relation_tolerance_us?=0L, priors?=[], max_constraints?=20000) 返回只读 ConstraintSet,失败抛出 ConstraintBuildError,不会返回部分构建结果。约束数量上限可配置为 1 至 2,000,000。

    固定模型为 模型时间 = 观测时间 + 时钟偏移。一条发送时间为 s、接收时间为 r 的唯一消息对形成:

    o_send - o_receive <= r - s + tolerance

    容差必须非负,单位为微秒;它放宽消息因果不等式,是显式模型假设。边方向为 receive_clock → send_clock。BuiltConstraint 同时保存基础约束、完整消息对及使用的容差,来源行号随原始事件保留。

    OffsetPrior::new(clock_id~, reference_clock~, label~, lower_us?, upper_us?) 表示:

    lower <= o_clock - o_reference <= upper

    至少给出一个边界,双边界须满足 lower ≤ upper。下界转换为 o_reference - o_clock <= -lower,上界保持正向;每条边保存原先验及边界种类。同一次构建中 label 必须唯一。先验可以引入新的时钟域,不会自动锚定到 UTC。

    #边界与证据

    • 唯一消息对产生约束;缺失或歧义组不产生约束,完整关联报告仍在结果中,涉及的时钟也不会丢失。
    • 时钟按名称排序,消息按关联报告的消息键顺序排列,先验按标签排序;同一先验先下界后上界。
    • 所有约束运算检查 Int64 溢出;消息表达式会重排运算,避免最终值可表示时因中间溢出误拒绝。无法表示的最终值使整次构建失败,错误携带消息 ID 或先验标签。
    • 下界为 Int64 最小值时,其相反数无法表示,构建明确报错;不会静默删除边界。
    • 允许负权自环和互相冲突的先验;本包仅构建图,不判断可行性或检测负环。

    fixtures/four-events.jsonl 在零容差、无先验时生成 o_A-o_B <= -100000 和 o_B-o_A <= 160000。2026-10-18 的核心流水线测试已核对这两条边及后续偏移区间。

    新增 14 项约束构建测试,覆盖消息方向、原始来源、容差、偏移先验、缺失与歧义、资源限制及 405 组任意精度算术对照。全项目四后端各通过 111/111,见 核心验证记录。

    ConstraintBuildError

    pub(all) suberror ConstraintBuildError {
    InvalidTolerance
    InvalidPriorField(String)
    EmptyPriorBounds(String)
    InvertedPriorRange(String)
    DuplicatePriorLabel(String)
    InvalidConstraintLimit
    ConstraintLimitExceeded(Int)
    ArithmeticOverflow(
    ConstraintOrigin
    )
    } derive(Eq,
    Debug
    )

    ConstraintBuildError::equal

    #deprecated("`ConstraintBuildError::equal` is deprecated, use `Eq::equal` instead.")
    fn ConstraintBuildError::equal(ConstraintBuildError, ConstraintBuildError) -> Bool
    automatically derived

    ConstraintBuildError::not_equal

    #deprecated("`ConstraintBuildError::not_equal` is deprecated, use `Eq::not_equal` instead.")
    fn ConstraintBuildError::not_equal(x : ConstraintBuildError, y : ConstraintBuildError) -> Bool
    automatically derived

    ConstraintBuildError::to_repr

    #deprecated("`ConstraintBuildError::to_repr` is deprecated, use `@moonbitlang/core/debug.Debug::to_repr` instead.")
    fn ConstraintBuildError::to_repr(ConstraintBuildError) ->
    Repr
    automatically derived

    BuiltConstraint

    BuiltConstraint::equal

    #deprecated("`BuiltConstraint::equal` is deprecated, use `Eq::equal` instead.")
    fn BuiltConstraint::equal(BuiltConstraint, BuiltConstraint) -> Bool
    automatically derived

    BuiltConstraint::not_equal

    #deprecated("`BuiltConstraint::not_equal` is deprecated, use `Eq::not_equal` instead.")
    fn BuiltConstraint::not_equal(x : BuiltConstraint, y : BuiltConstraint) -> Bool
    automatically derived

    BuiltConstraint::to_repr

    #deprecated("`BuiltConstraint::to_repr` is deprecated, use `@moonbitlang/core/debug.Debug::to_repr` instead.")
    fn BuiltConstraint::to_repr(BuiltConstraint) ->
    Repr
    automatically derived

    ConstraintEvidence

    pub(all) enum ConstraintEvidence {
    Message(pair~ :
    MessagePair
    , tolerance_us~ : Int64)
    Prior(prior~ : OffsetPrior, lower_bound~ : Bool)
    } derive(Eq,
    Debug
    )

    Each edge carries either the original paired events or an explicit prior.

    ConstraintEvidence::equal

    #deprecated("`ConstraintEvidence::equal` is deprecated, use `Eq::equal` instead.")
    fn ConstraintEvidence::equal(ConstraintEvidence, ConstraintEvidence) -> Bool
    automatically derived

    ConstraintEvidence::not_equal

    #deprecated("`ConstraintEvidence::not_equal` is deprecated, use `Eq::not_equal` instead.")
    fn ConstraintEvidence::not_equal(x : ConstraintEvidence, y : ConstraintEvidence) -> Bool
    automatically derived

    ConstraintEvidence::to_repr

    #deprecated("`ConstraintEvidence::to_repr` is deprecated, use `@moonbitlang/core/debug.Debug::to_repr` instead.")
    fn ConstraintEvidence::to_repr(ConstraintEvidence) ->
    Repr
    automatically derived

    ConstraintSet

    pub struct ConstraintSet {
    clocks : ReadOnlyArray[String]
    constraints : ReadOnlyArray[BuiltConstraint]
    correlation :
    CorrelationReport

    relation_tolerance_us : Int64
    } derive(Eq,
    Debug
    )

    No feasibility verdict is made here. Correlation evidence is retained even when missing or ambiguous groups contribute no edges.

    ConstraintSet::equal

    #deprecated("`ConstraintSet::equal` is deprecated, use `Eq::equal` instead.")
    fn ConstraintSet::equal(ConstraintSet, ConstraintSet) -> Bool
    automatically derived

    ConstraintSet::not_equal

    #deprecated("`ConstraintSet::not_equal` is deprecated, use `Eq::not_equal` instead.")
    fn ConstraintSet::not_equal(x : ConstraintSet, y : ConstraintSet) -> Bool
    automatically derived

    ConstraintSet::to_repr

    #deprecated("`ConstraintSet::to_repr` is deprecated, use `@moonbitlang/core/debug.Debug::to_repr` instead.")
    fn ConstraintSet::to_repr(ConstraintSet) ->
    Repr
    automatically derived

    OffsetPrior

    pub struct OffsetPrior {
    clock_id : String
    reference_clock : String
    lower_us : Int64?
    upper_us : Int64?
    label : String
    } derive(Eq,
    Debug
    )

    An explicit assumption: lower <= offset(clock) - offset(reference) <= upper. At least one bound is required. The label must be unique within a build.

    OffsetPrior::equal

    #deprecated("`OffsetPrior::equal` is deprecated, use `Eq::equal` instead.")
    fn OffsetPrior::equal(OffsetPrior, OffsetPrior) -> Bool
    automatically derived

    OffsetPrior::new

    fn OffsetPrior::new(clock_id~ : String, reference_clock~ : String, label~ : String, lower_us? : Int64, upper_us? : Int64) -> OffsetPrior raise ConstraintBuildError

    OffsetPrior::not_equal

    #deprecated("`OffsetPrior::not_equal` is deprecated, use `Eq::not_equal` instead.")
    fn OffsetPrior::not_equal(x : OffsetPrior, y : OffsetPrior) -> Bool
    automatically derived

    OffsetPrior::to_repr

    #deprecated("`OffsetPrior::to_repr` is deprecated, use `@moonbitlang/core/debug.Debug::to_repr` instead.")
    fn OffsetPrior::to_repr(OffsetPrior) ->
    Repr
    automatically derived

    build

    fn build(correlation :
    CorrelationReport
    , relation_tolerance_us? : Int64, priors? : ArrayView[OffsetPrior], max_constraints? : Int) -> ConstraintSet raise ConstraintBuildError

    Convert unique message pairs using s + o_send <= r + o_receive + tolerance. Missing and ambiguous groups remain in correlation; their clocks remain in clocks. Priors may introduce clocks. Ordering follows sorted message keys, then prior labels; lower-bound edges precede upper-bound edges.