#solver

    职责:差分约束可行性、偏移上下界、连通分量与可复核负环见证。

    #当前能力

    已实现全图可行性判定、一组可行偏移、可复核负环证据,以及按弱连通分量独立进行的参考时钟选择和偏移区间分析。原有 solve 入口保留整图分析行为;新增 analyze 入口用于分量和区间。

    #入口

    • solve(input : ConstraintSet, limits?) -> SolveReport raise SolveError:保留完整约束构建报告,包含原始消息、来源、容差、先验及未解决关联组。
    • solve_constraints(constraints, clock_ids?=[], limits?) -> ConstraintSolution raise SolveError:直接分析已验证的基础约束。clock_ids 用于保留孤立时钟,约束两端的时钟会自动加入;显式列表长度也受时钟数量上限限制,重复名称不增加图中的时钟数。

    结果中的原始约束顺序保持不变,时钟按名称排序。直接入口的遍历与见证选择受输入边顺序影响;固定输入产生稳定结果,不承诺任意重排后的见证完全相同。

    #可行结果

    Feasible(offsets~) 为每个时钟返回一个 Int64 微秒偏移。Bellman–Ford 的距离初始为零,相当于连接所有时钟的零权超级源;因此所有返回偏移不大于零,没有选定任何真实机器作为参考时钟。

    返回前逐条检查 offset(left) <= offset(right) + upper_bound。这是同一组可行偏移,不是偏移区间、唯一校准结果或 UTC 时间。没有约束的时钟得到零偏移,表示一种自由坐标选择。空图可行但没有证据覆盖;未解决消息仍需查看原始关联报告。

    #冲突结果

    Infeasible(cycle~) 包含一个沿图边正向排列的闭环及其严格为负的边权总和。CycleEdge.constraint_index 是原始约束数组的零基索引:

    report.input.constraints[index].evidence

    可用于回查完整消息对或先验。直接入口则用 solution.constraints[index] 回查基础约束及来源 ID。负环起点规范化为环中原始约束索引最小的边;这只是稳定的展示规则,不保证最小冲突集合。

    负权自环可直接作为冲突见证。其他负环在第 V 轮仍有松弛时沿前驱链提取;返回前检查边连接、闭合及权重和。权重求和优先交替正负项,避免最终值可表示时因求和顺序引入溢出。

    #资源与算术边界

    SolveLimits::new 默认限制为 256 个时钟、20,000 条约束和 5,000,000 次边访问;可配置硬上限分别为 4,096、2,000,000 和 1,000,000,000。参数必须为正,零边图仍可分析。

    边访问预算覆盖松弛、前驱遍历、可行解核对、负环核对与权重求和,不包含图索引和排序。analyze 还将分量划分中的两次边扫描、参考坐标核对和上下界路径扫描计入同一全局预算。无边图不消耗边访问预算。可行性单轮复杂度 O(E),最多 V 轮;每个方向的区间最短路最多 V-1 轮。

    所有路径加法和环权求和检查 Int64 溢出。松弛路径可能超出 Int64,即使数学上存在可行偏移,也可能返回 ArithmeticOverflow;本阶段不使用任意精度运算或重定位坐标来继续分析。环权总和超出 Int64 时同样报错,不能据此推断图可行或冲突。

    SolveError 区分配置错误、时钟/约束超限、边访问预算耗尽、算术溢出和内部见证核对失败。solve 失败不返回部分解或可行性结论;analyze 的分量计算失败明确记录为 ComponentFailed,语义见下文。

    #分量与偏移区间

    #新入口

    • analyze(input, reference_clock?, limits?) -> AnalysisReport raise SolveError 保留完整构建报告,结果位于 analysis 字段。
    • analyze_constraints(constraints, clock_ids?=[], reference_clock?, limits?) -> ConstraintAnalysis raise SolveError 直接分析基础约束。

    若指定参考时钟,必须已经存在于输入图中;只在其所属分量中使用它。其他分量默认使用名称字典序最小的时钟。默认情况下所有分量均采用各自最小名称。分量按最小名称排序,component_id 从零递增,分量内时钟也按名称排序。这里明确使用 UTF-16 码元字典序,不使用 MoonBit String.compare 的长度优先顺序;输入关联和原始约束仍保持各自既定顺序。空图返回空分量集合;向空图指定不存在的参考时钟会报错。

    #三种分量结果

    结果内容与含义
    ComponentFeasible同一组参考归零的可行偏移,以及每个时钟的边际上下界
    ComponentInfeasible这个分量内的一个负环,仍使用原始全局约束索引
    ComponentFailed这个分量未完成分析,携带预算、溢出或内部核对错误;不作可行性结论

    输入/配置错误、未知参考时钟,以及划分分量之前或期间耗尽预算,会使整个入口报错。进入逐分量求解后,算术失败只影响当前分量,其他分量继续使用剩余预算;预算耗尽后剩余分量均记为失败。返回 ConstraintAnalysis 不意味着所有分量都分析成功,调用者必须检查每个 outcome。

    每个分量的统计只包含自身求解、区间计算及核对;总统计还包含分量划分的边访问。因此各分量 edge_visits 之和可能小于总数。passes 记录完成的可行性和最短路扫描轮数。

    #数学约定

    在一个可行分量内,参考时钟 r 的偏移设为零:

    upper(o_i) = d(r, i) lower(o_i) = -d(i, r)

    原图从 r 出发计算上界;反向图保留原权重,从 r 出发计算下界所需的路径。没有对应方向的路径时返回 Unbounded;存在有限路径时返回 Finite(Int64)。Unbounded 表示数学上的无界,不用来代替溢出或未完成分析。

    参考时钟自身区间为 [0,0]。孤立时钟也是自己的参考,[0,0] 只表示自身坐标,不能说明它相对于其他分量的真实偏移。

    代表偏移取自同一组可行解,统一减去参考偏移,并再次核对全部分量原始约束及其所属区间。各时钟的区间端点分别可达,不保证不同端点能够同时取得;不能拼接端点来绘制时间线。不同分量没有共同时间基准,不推断其间的确定先后。

    路径运算、参考坐标平移或有限下界取反超出 Int64 时,分量返回显式失败;不会改成 Unbounded。即使存在其他可表示的代表解,当前实现也不会重新搜索来规避坐标平移溢出。约束核对使用不溢出的数学比较,避免右侧加法越界造成误判。

    #样例与验证状态

    • fixtures/four-events.jsonl 的约束可由 o_A=-100000、o_B=0 满足;analyze 以 A 为参考时,数学预期为 B 的区间 [100000,160000] 微秒,代表偏移为 A=0、B=100000。
    • fixtures/negative-cycle.jsonl 在零容差下产生三条权重均为 -10 微秒的边,合计 -30 微秒,数学预期为冲突;图方向为 B→A、C→B、A→C。
    • fixtures/component-analysis.jsonl 同时包含有限区间、单侧无界、孤立时钟和冲突分量,数学预期见 fixtures/README.md。

    2026-10-18:以上样例已通过 JSONL 解析→配对→约束构建→求解/分量区间的核心流水线测试。求解包新增 37 项测试,其中 384 个小图与独立 Floyd–Warshall 参考算法逐图对照,并用任意精度整数独立核对可行解和负环。全项目四后端各通过 111/111,详见 核心验证记录。测试没有执行 CLI 文件读取或真实网络采集。

    SolveError

    pub(all) suberror SolveError {
    InvalidLimit(String)
    ClockLimitExceeded(Int)
    ConstraintLimitExceeded(Int)
    EmptyClockId
    UnknownReferenceClock(String)
    OffsetOverflow(clock_id~ : String, reference_clock~ : String)
    BoundOverflow(clock_id~ : String, lower_bound~ : Bool)
    EdgeVisitLimitExceeded(Int64)
    ArithmeticOverflow(phase~ : SolvePhase, constraint_index~ : Int)
    InvalidWitness
    } derive(Eq,
    Debug
    )

    Errors carry no feasibility verdict or partial solution.

    SolveError::equal

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

    SolveError::not_equal

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

    SolveError::to_repr

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

    AnalysisReport

    AnalysisReport::equal

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

    AnalysisReport::not_equal

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

    AnalysisReport::to_repr

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

    ClockOffset

    pub struct ClockOffset {
    clock_id : String
    offset_us : Int64
    } derive(Eq,
    Debug
    )

    Offsets share a relative coordinate system, with no UTC anchor. solve returns super-source coordinates; analyze uses a component reference.

    ClockOffset::equal

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

    ClockOffset::not_equal

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

    ClockOffset::to_repr

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

    ComponentAnalysis

    pub struct ComponentAnalysis {
    component_id : Int
    clocks : ReadOnlyArray[String]
    reference_clock : String
    constraint_indices : ReadOnlyArray[Int]
    outcome : ComponentOutcome
    stats : SolveStats
    } derive(Eq,
    Debug
    )

    Component IDs are zero-based in order of the smallest clock name. Constraint indices and cycle indices refer to the original global array.

    ComponentAnalysis::equal

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

    ComponentAnalysis::not_equal

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

    ComponentAnalysis::to_repr

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

    ComponentOutcome

    pub(all) enum ComponentOutcome {
    ComponentFeasible(offsets~ : ReadOnlyArray[ClockOffset], intervals~ : ReadOnlyArray[OffsetInterval])
    ComponentInfeasible(cycle~ : NegativeCycle)
    ComponentFailed(error~ : SolveError)
    } derive(Eq,
    Debug
    )

    ComponentOutcome::equal

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

    ComponentOutcome::not_equal

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

    ComponentOutcome::to_repr

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

    ConstraintAnalysis

    pub struct ConstraintAnalysis {
    clocks : ReadOnlyArray[String]
    constraints : ReadOnlyArray[
    DifferenceConstraint
    ]
    components : ReadOnlyArray[ComponentAnalysis]
    stats : SolveStats
    } derive(Eq,
    Debug
    )

    A completed component traversal may contain ComponentFailed outcomes. Always inspect each outcome; this report is not a global feasibility claim.

    ConstraintAnalysis::equal

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

    ConstraintAnalysis::not_equal

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

    ConstraintAnalysis::to_repr

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

    ConstraintSolution

    pub struct ConstraintSolution {
    clocks : ReadOnlyArray[String]
    constraints : ReadOnlyArray[
    DifferenceConstraint
    ]
    outcome : Feasibility
    stats : SolveStats
    } derive(Eq,
    Debug
    )

    Original edge order is preserved. Independent clocks are sorted by name. A feasible result covers only these supplied constraints, not log coverage.

    ConstraintSolution::equal

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

    ConstraintSolution::not_equal

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

    ConstraintSolution::to_repr

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

    CycleEdge

    pub struct CycleEdge {
    constraint_index : Int
    constraint :
    DifferenceConstraint

    } derive(Eq,
    Debug
    )

    Index into ConstraintSolution.constraints, and (for solve) into SolveReport.input.constraints. The original constraint is retained.

    CycleEdge::equal

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

    CycleEdge::not_equal

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

    CycleEdge::to_repr

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

    Feasibility

    pub(all) enum Feasibility {
    Feasible(offsets~ : ReadOnlyArray[ClockOffset])
    Infeasible(cycle~ : NegativeCycle)
    } derive(Eq,
    Debug
    )

    Feasibility::equal

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

    Feasibility::not_equal

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

    Feasibility::to_repr

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

    NegativeCycle

    pub struct NegativeCycle {
    edges : ReadOnlyArray[CycleEdge]
    total_weight_us : Int64
    } derive(Eq,
    Debug
    )

    Edges are in forward traversal order. The last edge returns to the first. This is one conflict witness, not necessarily a minimum conflict.

    NegativeCycle::equal

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

    NegativeCycle::not_equal

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

    NegativeCycle::to_repr

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

    OffsetBound

    pub(all) enum OffsetBound {
    Finite(Int64)
    Unbounded
    } derive(Eq,
    Debug
    )

    Unbounded is mathematical unboundedness, not a computation failure.

    OffsetBound::equal

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

    OffsetBound::not_equal

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

    OffsetBound::to_repr

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

    OffsetInterval

    pub struct OffsetInterval {
    clock_id : String
    lower : OffsetBound
    upper : OffsetBound
    } derive(Eq,
    Debug
    )

    Marginal bounds relative to the component reference. Endpoints for different clocks need not be simultaneously attainable.

    OffsetInterval::equal

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

    OffsetInterval::not_equal

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

    OffsetInterval::to_repr

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

    SolveLimits

    pub struct SolveLimits {
    max_clocks : Int
    max_constraints : Int
    max_edge_visits : Int64
    }

    Caps apply before returning any verdict. Edge visits include relaxation, predecessor traversal, and result verification, but not graph indexing.

    SolveLimits::default

    fn SolveLimits::default() -> SolveLimits

    SolveLimits::new

    fn SolveLimits::new(max_clocks? : Int, max_constraints? : Int, max_edge_visits? : Int64) -> SolveLimits raise SolveError

    SolvePhase

    pub(all) enum SolvePhase {
    Relaxation
    FeasibleVerification
    CycleWeightSum
    UpperBoundPaths
    LowerBoundPaths
    } derive(Eq,
    Debug
    )

    SolvePhase::equal

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

    SolvePhase::not_equal

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

    SolvePhase::to_repr

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

    SolveReport

    Includes missing/ambiguous correlation groups and full edge evidence.

    SolveReport::equal

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

    SolveReport::not_equal

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

    SolveReport::to_repr

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

    SolveStats

    pub struct SolveStats {
    clock_count : Int
    constraint_count : Int
    passes : Int
    edge_visits : Int64
    } derive(Eq,
    Debug
    )

    SolveStats::equal

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

    SolveStats::not_equal

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

    SolveStats::to_repr

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

    analyze

    fn analyze(input :
    ConstraintSet
    , reference_clock? : String, limits? : SolveLimits) -> AnalysisReport raise SolveError

    Keep correlation coverage, provenance, tolerance, and assumptions available.

    analyze_constraints

    fn analyze_constraints(constraints : ArrayView[
    DifferenceConstraint
    ], clock_ids? : ArrayView[String], reference_clock? : String, limits? : SolveLimits) -> ConstraintAnalysis raise SolveError

    Analyze weak components independently. The requested reference is used in its component; all others use their lexicographically smallest clock. Configuration, graph indexing, partition-budget, and unknown-reference errors fail the call. Per-component computational failures remain explicit.

    solve

    Preserve the entire build report so witness indices resolve to original message pairs, source locations, tolerance, and prior bounds.

    solve_constraints

    Direct graph entry point. Additional clock_ids preserve isolated clocks. Empty constraints are feasible with zero offsets; this is zero evidence. Input edge order determines deterministic relaxation and witness selection.