A MoonBit algorithm trace protocol and polished offline HTML visualization kit.
Dependencies
moon add shop1111/frontierlablet 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())| 类别 | Adapter | 场景 | 主要语义事件 |
|---|---|---|---|
| 排序 | insertion_sort_trace、insertion_sort_items_trace | Sequence | Compare、Swap |
| 集合 | union_find_trace | Sets | Compare、Union、Update |
| 一维 DP | fibonacci_dp_trace、coin_change_dp_trace | Sequence | Update、依赖高亮 |
| 二维 DP | zero_one_knapsack_dp_trace、lcs_dp_trace | Grid | Update、Visit |
| 平衡树 | red_black_tree_trace | Graph | 插入、重着色、旋转、查找 |
| 路径搜索桥接 | search_trace_to_algorithm_trace | Grid | Visit、Relax |
let lcs = @frontierlab.lcs_dp_trace("ABCBDAB", "BDCABA")
let tree = @frontierlab.red_black_tree_trace([
@frontierlab.Insert(30),
@frontierlab.Insert(10),
@frontierlab.Insert(20),
@frontierlab.Find(20),
])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 --versionNew-Item -ItemType Directory -Force _build\visual | Out-Null
moon run cmd/main -- demo insertion-sort --format html --output _build/visual/insertion-sort.html
moon run cmd/main -- demo fibonacci --format html --output _build/visual/fibonacci.html
moon run cmd/main -- demo coin-change --format html --output _build/visual/coin-change.html
moon run cmd/main -- demo knapsack --format html --output _build/visual/knapsack.html
moon run cmd/main -- demo lcs --format html --output _build/visual/lcs.html
moon run cmd/main -- demo union-find --format html --output _build/visual/union-find.html
moon run cmd/main -- demo pathfinding --format html --output _build/visual/pathfinding.html
moon run cmd/main -- demo red-black-tree --format html --output _build/visual/red-black-tree.htmlmoon run cmd/main -- render trace.json --format html --output trace.htmlmoon 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_playground | 生成交互式离线 HTML 或诊断页面;SVG API 仅作弃用兼容 |
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
node scripts/check_renderer.mjs _build/renderer/insertion-sort.html
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 raisefn coin_change_dp_trace(coins : Array[Int], amount : Int, title? : String, object_id? : String, label? : String, options? : TraceOptions) -> AlgorithmTrace raise TraceErrorlet 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 fibonacci_dp_trace(n : Int, title? : String, object_id? : String, label? : String, options? : TraceOptions) -> AlgorithmTrace raise TraceErrorfn 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 lcs_dp_trace(left : String, right : String, title? : String, object_id? : String, label? : String, options? : TraceOptions) -> AlgorithmTrace raise TraceErrorfn red_black_tree_trace(operations : Array[RedBlackTreeOperation], title? : String, object_id? : String, label? : String, options? : TraceOptions) -> AlgorithmTrace raise TraceError#deprecated("Legacy static renderer; use render_trace_html or Schema v1 JSON. This API will be removed in v1.0.")
fn render_trace_svg(trace : AlgorithmTrace, step? : Int, width? : Int) -> String raise#deprecated("Legacy static renderer; use render_trace_html or Schema v1 JSON. This API will be removed in v1.0.")
fn render_trace_svg_frames(trace : AlgorithmTrace, width? : Int) -> Array[String] raisefn 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 TraceErrorfn zero_one_knapsack_dp_trace(weights : Array[Int], values : Array[Int], capacity : Int, title? : String, object_id? : String, label? : String, options? : TraceOptions) -> AlgorithmTrace raise TraceErrorA MoonBit algorithm trace protocol and polished offline HTML visualization kit.
Dependencies