A MoonBit algorithm trace protocol and offline HTML/SVG visualization kit.
moon add shop1111/frontierlab@0.7.1let trace = @frontierlab.insertion_sort_trace([3, 1, 2])
let result = @frontierlab.diagnose_trace(
trace,
contract=@frontierlab.sorted_int_sequence_contract(object_id="values"),
expected=trace,
)
assert_true(result.passed())moon run cmd/main -- diagnose \
fixtures/agent-traces/selection-sort-expected.json \
fixtures/agent-traces/selection-sort-actual.json \
--contract sorted-int-sequence \
--object values \
--format text \
--counterexample _build/counterexample.json \
--report _build/diagnosis.htmlpython scripts\build_cli.py
.\_dist\frontierlab.exe --versionmoon run cmd/main -- playground --output _build/playground.html| 用途 | 主要 API | 说明 |
|---|---|---|
| 记录轨迹 | TraceBuilder::new、record、finish | 把算法执行过程记录为语义事件和完整场景快照 |
| 统一诊断 | diagnose_trace、TraceDiagnosis::passed | 同时执行规则检查、参考轨迹比较和聚焦切片 |
| 调试与比较 | diff、breakpoint_hits、slice、first_divergence | 查看步骤变化、命中语义断点并定位第一次差异 |
| JSON 协议 | encode_json、AlgorithmTrace::decode_json、validate | 读写并校验稳定的 Trace Schema v1 |
| 输出 | render_trace_html、render_trace_svg、render_trace_playground | 生成离线 HTML、SVG 或诊断页面 |
cd consumer/frontierlab_consumer_demo
moon tree
moon check --target all --deny-warn
moon test --target all --deny-warnmoon check --target all --deny-warn
moon build --target all --deny-warn
moon fmt --check
moon info
moon test --target all --deny-warn
python scripts/check_coverage.py
node scripts/check_playground.mjs
python scripts/validate_cli.py
moon package --list
moon packagepub struct AlgorithmTrace {
schema_version : String
title : String
algorithm : String
description : String
initial_scene : Scene
steps : Array[AlgorithmTraceStep]
summary : Array[TraceAttribute]
} derive(Eq, ToJson, Debug)fn AlgorithmTrace::breakpoint_hits(self : AlgorithmTrace, breakpoint : TraceBreakpoint) -> Array[TraceBreakpointHit]fn AlgorithmTrace::decode_json(input : StringView, options? : TraceOptions) -> AlgorithmTrace raise TraceErrorfn AlgorithmTrace::diff(self : AlgorithmTrace, from_step~ : Int, to_step~ : Int) -> TraceFrameDiff raise TraceErrorfn AlgorithmTrace::slice(self : AlgorithmTrace, center~ : Int, before? : Int, after? : Int) -> TraceCounterexample raise TraceErrorfn AlgorithmTrace::validate(self : AlgorithmTrace, options? : TraceOptions) -> Unit raise TraceErrorpub struct AlgorithmTraceStep {
index : Int
event : TraceEvent
scene : Scene
annotation : Annotation?
} derive(Eq, ToJson, Debug)pub struct ContractReport {
report_version : String
contract_name : String
passed : Bool
violations : Array[TraceViolation]
first_failure : TraceViolation?
counterexample : TraceCounterexample?
} derive(Eq, ToJson, Debug)fn GraphState::new(id~ : String, label~ : String, nodes~ : Array[GraphNode], edges~ : Array[GraphEdge]) -> GraphState raise TraceErrorfn GridCellState::new(id~ : String, x~ : Int, y~ : Int, label? : String, blocked? : Bool) -> GridCellStatepub struct GridState {
id : String
label : String
width : Int
height : Int
cells : Array[GridCellState]
} derive(Eq, ToJson, Debug)fn GridState::new(id~ : String, label~ : String, width~ : Int, height~ : Int, cells~ : Array[GridCellState]) -> GridState raise TraceErrorfn ObjectUsage::new(object_id~ : String, object_kind~ : String, appearances? : Int, max_entities? : Int, first_step? : Int, last_step? : Int) -> ObjectUsagefn Scene::new(objects~ : Array[SceneObject], highlights? : Array[Highlight]) -> Scene raise TraceErrorpub(all) enum SceneObject {
Sequence(SequenceState)
Sets(SetState)
Graph(GraphState)
Grid(GridState)
} derive(Eq, ToJson, Debug)pub struct SequenceState {
id : String
label : String
items : Array[SequenceItem]
} derive(Eq, ToJson, Debug)fn TargetUsage::new(object_id~ : String, entity_id? : String, references? : Int, event_references? : Int, highlight_references? : Int, first_step? : Int, last_step? : Int, event_names? : Array[String], highlight_roles? : Array[String]) -> TargetUsagefn TraceBreakpoint::new(event_kind? : String, target? : TargetRef, role? : String, changed_only? : Bool) -> TraceBreakpointpub struct TraceBuilder {
title : String
algorithm : String
description : String
initial_scene : Scene
steps : Array[AlgorithmTraceStep]
options : TraceOptions
completed : Bool
} derive(Debug)fn TraceBuilder::finish(self : TraceBuilder, summary? : Array[TraceAttribute]) -> AlgorithmTrace raise TraceErrorfn TraceBuilder::new(title~ : String, algorithm~ : String, description? : String, initial_scene~ : Scene, options? : TraceOptions) -> TraceBuilder raise TraceErrorfn TraceBuilder::record(self : TraceBuilder, event~ : TraceEvent, scene~ : Scene, annotation? : Annotation) -> Unit raise TraceErrorpub struct TraceChange {
kind : TraceChangeKind
target : TargetRef
before_value : String
after_value : String
before_index : Int
after_index : Int
} derive(Eq, ToJson, Debug)fn TraceChange::new(kind~ : TraceChangeKind, target~ : TargetRef, before_value? : String, after_value? : String, before_index? : Int, after_index? : Int) -> TraceChangepub struct TraceContract {
name : String
description : String
checker : (AlgorithmTrace) -> Array[TraceViolation]
}fn TraceContract::new(name~ : String, description~ : String, checker~ : (AlgorithmTrace) -> Array[TraceViolation]) -> TraceContractpub struct TraceCounterexample {
original_title : String
original_start : Int
original_focus : Int
original_end : Int
focus_step : Int
trace : AlgorithmTrace
} derive(Eq, ToJson, Debug)pub struct TraceDiagnosis {
contract_report : ContractReport
divergence : TraceDivergence?
focus_step : Int
transition_diff : TraceFrameDiff?
reference_diff : TraceFrameDiff?
focused_slice : TraceCounterexample?
} derive(Eq, ToJson, Debug)pub struct TraceDivergence {
step : Int
kind : TraceDivergenceKind
message : String
expected_event : String
actual_event : String
scene_diff : TraceFrameDiff?
} derive(Eq, ToJson, Debug)pub struct TraceFrameDiff {
from_step : Int
to_step : Int
changes : Array[TraceChange]
} derive(Eq, ToJson, Debug)pub struct TraceLintIssue {
severity : TraceLintSeverity
code : String
message : String
step : Int
} derive(Eq, ToJson, Debug)fn TraceLintIssue::new(severity~ : TraceLintSeverity, code~ : String, message~ : String, step? : Int) -> TraceLintIssuefn TraceOptions::new(max_steps? : Int, max_entities_per_scene? : Int) -> TraceOptions raise TraceErrorpub struct TraceStats {
title : String
algorithm : String
step_count : Int
completed : Bool
event_counts : Array[EventCount]
object_usage : Array[ObjectUsage]
target_usage : Array[TargetUsage]
initial_object_count : Int
max_objects_per_scene : Int
max_entities_per_scene : Int
summary_count : Int
annotation_count : Int
custom_event_count : Int
highlight_count : Int
} derive(Eq, ToJson, Debug)fn TraceTimelineEntry::new(step~ : Int, event_name~ : String, target_count~ : Int, object_count~ : Int, entity_count~ : Int, highlight_count~ : Int, annotation_title? : String, completed? : Bool) -> TraceTimelineEntrypub struct TraceViolation {
contract : String
code : String
severity : TraceViolationSeverity
step : Int
targets : Array[TargetRef]
message : String
evidence : Array[TraceAttribute]
} derive(Eq, ToJson, Debug)fn TraceViolation::new(contract~ : String, code~ : String, severity? : TraceViolationSeverity, step? : Int, targets? : Array[TargetRef], message~ : String, evidence? : Array[TraceAttribute]) -> TraceViolationfn bfs_trace(grid : GridMap, start~ : Position, goal~ : Position, rule? : MoveRule) -> SearchTrace raiselet debug_report_version : Stringfn diagnose_trace(actual : AlgorithmTrace, contract~ : TraceContract, expected? : AlgorithmTrace) -> TraceDiagnosisfn dijkstra(grid : GridMap, start~ : Position, goal~ : Position, rule? : MoveRule) -> PathResult raisefn dijkstra_trace(grid : GridMap, start~ : Position, goal~ : Position, rule? : MoveRule) -> SearchTrace raisefn export_ascii(grid : GridMap, start~ : Position, goal~ : Position, trace? : SearchTrace) -> Stringfn export_svg(grid : GridMap, start~ : Position, goal~ : Position, trace? : SearchTrace, cell_size? : Int) -> Stringfn insertion_sort_items_trace(items : Array[SortTraceItem], title? : String, object_id? : String, label? : String) -> AlgorithmTrace raise TraceErrorfn insertion_sort_trace(values : Array[Int], title? : String, object_id? : String, label? : String) -> AlgorithmTrace raise TraceErrorfn search_trace_to_algorithm_trace(grid : GridMap, start~ : Position, goal~ : Position, algorithm~ : String, trace~ : SearchTrace) -> AlgorithmTrace raisefn union_find_trace(size : Int, operations : Array[UnionOperation], title? : String, object_id? : String, label? : String, compress_paths? : Bool) -> AlgorithmTrace raise TraceErrorInstall
Download zipA MoonBit algorithm trace protocol and offline HTML/SVG visualization kit.