Lab 1: Reactive Property-Binding DAG, Deadband Coalescer & Cycle Guard Engine
Eliminate V4 JIT cold-start latency on Cortex-A7 with qt_add_qml_module and qmlsc ahead-of-time C++ compilation, maintain cycle-free reactive property-binding DAGs, and expose FINAL C++20 domain models via QML_ELEMENT with deadband signal coalescing.
Prerequisites in this module
What ./check.sh verifies
- Builds libcore.a and lab_cli cleanly with -std=c++20 -Wall -Wextra -Werror -pedantic.
- Fix compiler warnings or errors in files/core.cpp.
- Archives core.o into libcore.a and links independent TAP verifier.
- Preserve public declarations in files/core.hpp so tests.cpp links.
step 1/15 · inspect starter files and specification
Frozen gauges, QML Binding loop detected aborts, and 500 Hz NOTIFY storms trace back to three core engine invariants inside a Qt Quick HMI: detaching upstream edges on imperative = writes, rolling back re-entrant cycles in bind(), and filtering NaN plus sub-epsilon jitter before propagating downstream changes.
In this studio lab you will audit and repair an ISO C++20 reactive property-binding DAG engine (files/core.cpp, hmi::BindingDag) supporting multi-hop propagation, imperative literal unbind and bind() restoration, NaN/Inf rejection with epsilon deadband coalescing, and 2-node/3-node cycle rollback benchmarked with CLOCK_MONOTONIC.
learner@ksa:~/gl-q03/lab-m01ls -lastarter/Makefilestarter/cli.cppstarter/core.cppstarter/core.hpplearner@ksa:~/gl-q03/lab-m01starter: starter/Makefile
learner@ksa:~/gl-q03/lab-m01$ bat starter/Makefile File: starter/Makefile CC_BIN = g++ AR = ar CFLAGS_ALL = -std=c++20 -Wall -Wextra -Werror -pedantic -O2 LDLIBS = all: libcore.a lab_cli libcore.a: core.o $(AR) rcs $@ $^ lab_cli: cli.o libcore.a $(CC_BIN) $(CFLAGS_ALL) cli.o libcore.a $(LDLIBS) -o $@ %.o: %.cpp core.hpp $(CC_BIN) $(CFLAGS_ALL) -c $< -o $@ .PHONY: all clean clean: rm -f core.o cli.o libcore.a lab_clistarter: starter/cli.cpp
learner@ksa:~/gl-q03/lab-m01$ bat starter/cli.cpp File: starter/cli.cpp #include "core.hpp" #include <algorithm> #include <cinttypes> #include <cstdio> #include <cstring> #include <ctime> #include <vector> static uint64_t mono_ns() { struct timespec ts {}; clock_gettime(CLOCK_MONOTONIC, &ts); return static_cast<uint64_t>(ts.tv_sec) * 1000000000ULL + static_cast<uint64_t>(ts.tv_nsec); } static int run_bench() { std::vector<uint64_t> samples(40, 0); double acc = 0.0; hmi::BindingDag dag(1e-4); int rpm = dag.create_property(1000.0); int max_rpm = dag.create_property(6000.0); int width = dag.create_property(344.0); int usable = dag.create_property(0.0); int ratio = dag.create_property(0.0); int bar_px = dag.create_property(0.0); dag.bind(usable, {width}, [&] { return dag.value(width) - 24.0; }); dag.bind(ratio, {rpm, max_rpm}, [&] { return std::clamp(dag.value(rpm) / dag.value(max_rpm), 0.0, 1.0); }); dag.bind(bar_px, {usable, ratio}, [&] { return dag.value(usable) * dag.value(ratio); }); for (int iter = 0; iter < 45; ++iter) { uint64_t t0 = mono_ns(); for (int k = 0; k < 500; ++k) { dag.assign_literal(rpm, 1200.0 + static_cast<double>((iter + k) % 4500)); acc += dag.value(bar_px); } uint64_t dt = mono_ns() - t0; if (iter >= 5) samples[static_cast<size_t>(iter - 5)] = dt; } volatile double sink = acc; std::sort(samples.begin(), samples.end()); std::printf( "BENCH lab_m01_qml_binding_dag (500 propagations/batch, 40 batches): " "p50=%" PRIu64 " ns p95=%" PRIu64 " ns p99=%" PRIu64 " ns (sink=%.1f)\n", samples[20], samples[38], samples[39], sink > 0.0 ? 1.0 : 0.0); return 0; } int main(int argc, char** argv) { if (argc > 1 && std::strcmp(argv[1], "--bench") == 0) { return run_bench(); } hmi::BindingDag dag(1e-4); int rpm = dag.create_property(1500.0); int max_rpm = dag.create_property(6000.0); int ratio = dag.create_property(0.0); int deg = dag.create_property(0.0); if (!dag.bind(ratio, {rpm, max_rpm}, [&] { return dag.value(rpm) / dag.value(max_rpm); })) { return 1; } if (!dag.bind(deg, {ratio}, [&] { return dag.value(ratio) * 270.0; })) { return 1; } dag.assign_literal(rpm, 4500.0); double live_deg = dag.value(deg); dag.assign_literal(deg, 90.0); dag.assign_literal(rpm, 6000.0); double manual_deg = dag.value(deg); dag.bind(deg, {ratio}, [&] { return dag.value(ratio) * 270.0; }); double rebound_deg = dag.value(deg); bool self_loop_ok = !dag.bind(rpm, {deg}, [&] { return dag.value(deg); }); std::printf( "DAG rpm=4500 -> deg=%.1f | manual_unbind=%.1f | rebound=%.1f | " "cycle_rejected=%u\n", live_deg, manual_deg, rebound_deg, self_loop_ok ? 1U : 0U); return self_loop_ok ? 0 : 1; }starter: starter/core.cpp
learner@ksa:~/gl-q03/lab-m01$ bat starter/core.cpp File: starter/core.cpp #include "core.hpp" #include <algorithm> #include <cmath> #include <utility> namespace hmi { BindingDag::BindingDag(double epsilon) : epsilon_(epsilon) {} bool BindingDag::valid_id(int id) const noexcept { return id >= 0 && static_cast<size_t>(id) < nodes_.size(); } int BindingDag::create_property(double initial_value) { double safe = std::isfinite(initial_value) ? initial_value : 0.0; nodes_.push_back(PropNode{safe, false, false, {}, nullptr, {}, 0, 0}); return static_cast<int>(nodes_.size() - 1); } void BindingDag::detach_upstream(int id) { for (int dep : nodes_[id].deps) { auto& subs = nodes_[dep].subscribers; subs.erase(std::remove(subs.begin(), subs.end(), id), subs.end()); } nodes_[id].deps.clear(); } bool BindingDag::bind(int target_id, std::vector<int> dependencies, std::function<double()> expr) { if (!valid_id(target_id) || !expr) return false; for (int dep : dependencies) { if (!valid_id(dep) || dep == target_id) return false; } double saved_val = nodes_[target_id].value; detach_upstream(target_id); nodes_[target_id].deps = std::move(dependencies); nodes_[target_id].expr = std::move(expr); nodes_[target_id].bound = true; for (int dep : nodes_[target_id].deps) { if (std::find(nodes_[dep].subscribers.begin(), nodes_[dep].subscribers.end(), target_id) == nodes_[dep].subscribers.end()) { nodes_[dep].subscribers.push_back(target_id); } } if (!recompute_node(target_id)) { detach_upstream(target_id); nodes_[target_id].expr = nullptr; nodes_[target_id].bound = false; nodes_[target_id].evaluating = false; nodes_[target_id].value = saved_val; return false; } return true; } bool BindingDag::assign_literal(int target_id, double literal_value) { if (!valid_id(target_id) || !std::isfinite(literal_value)) return false; // BUG 2: forgot to detach upstream binding edges on imperative assign_literal return write_and_propagate(target_id, literal_value); } bool BindingDag::recompute_node(int id) { PropNode& node = nodes_[id]; if (!node.bound || !node.expr) return true; // BUG 3: omitted active re-entrancy guard (infinite recursion on cycle!) if (node.evals > 64U) return true; node.evaluating = true; ++node.evals; double computed = node.expr(); if (!std::isfinite(computed)) { node.evaluating = false; return false; } bool ok = write_and_propagate(id, computed); node.evaluating = false; return ok; } bool BindingDag::write_and_propagate(int id, double next_val) { // BUG 1: omitted epsilon deadband coalescing check before notifying subscribers nodes_[id].value = next_val; ++nodes_[id].notifies; std::vector<int> snapshot = nodes_[id].subscribers; for (int sub : snapshot) { if (!recompute_node(sub)) return false; } return true; } double BindingDag::value(int id) const noexcept { return valid_id(id) ? nodes_[id].value : 0.0; } bool BindingDag::is_bound(int id) const noexcept { return valid_id(id) ? nodes_[id].bound : false; } uint32_t BindingDag::notify_count(int id) const noexcept { return valid_id(id) ? nodes_[id].notifies : 0; } uint32_t BindingDag::eval_count(int id) const noexcept { return valid_id(id) ? nodes_[id].evals : 0; } } // namespace hmistarter: starter/core.hpp
learner@ksa:~/gl-q03/lab-m01$ bat starter/core.hpp File: starter/core.hpp #pragma once #include <cstdint> #include <functional> #include <vector> namespace hmi { class BindingDag { public: explicit BindingDag(double epsilon = 1e-6); int create_property(double initial_value); bool bind(int target_id, std::vector<int> dependencies, std::function<double()> expr); bool assign_literal(int target_id, double literal_value); double value(int id) const noexcept; bool is_bound(int id) const noexcept; uint32_t notify_count(int id) const noexcept; uint32_t eval_count(int id) const noexcept; private: struct PropNode { double value = 0.0; bool bound = false; bool evaluating = false; std::vector<int> deps; std::function<double()> expr; std::vector<int> subscribers; uint32_t notifies = 0; uint32_t evals = 0; }; double epsilon_; std::vector<PropNode> nodes_; bool valid_id(int id) const noexcept; void detach_upstream(int id); bool recompute_node(int id); bool write_and_propagate(int id, double next_val); }; } // namespace hmistep 2/15 · 2. learning objectives
- Implement multi-hop reactive dirty propagation with IEEE-754
std::isfinitevalidation and epsilon deadband coalescing. - Enforce QML imperative
=semantics inassign_literal()by detaching upstream subscriber edges until explicitly rebound. - Detect direct (
A -> A,A <-> B) and transitive (A -> B -> C -> A)BindingLoopcycles via activeevaluatingguards with clean state rollback.
step 3/15 · 3. environment requirements
All automated verification steps run in unprivileged user space on a POSIX Linux host using g++ -std=c++20 -Wall -Wextra -Werror -pedantic and clock_gettime(CLOCK_MONOTONIC). Recorded transcripts in captures.json were generated on x86_64 running kernel 7.0.0-42-generic and prove protocol, state-machine, and memory invariants only. Physical bus timing, DMA cache coherency, and board peripheral behavior require target validation on Linux kernel 6.12 LTS (ARMv7-A Cortex-A7).
step 4/15 · 4. required software and hardware
- Host workstation:
g++(-std=c++20 -Wall -Wextra -Werror -pedantic), GNUmake,ar(binutils), and POSIX/bin/sh. - Target environment (optional hardware step, Section 13):
i.MX6ULL(ARMv7-ACortex-A7) or multi-core embedded target runningLinux kernel6.12 LTS andQt6.8 LTS (QT_LOGGING_RULES="qt.qml.binding.removal.info=true",QML_IMPORT_TRACE=1).
step 5/15 · 5. complete executable commands
learner@ksa:~/gl-q03/lab-m01$ bat script.sh File: script.sh make -B -C files ./files/lab_cli ./files/lab_cli --bench sh check.shstep 6/15 · 6. annotated source code
files/core.cpp enforces the production invariants that starter/core.cpp breaks:
learner@ksa:~/gl-q03/lab-m01$ bat example.cpp File: example.cpp bool BindingDag::assign_literal(int target_id, double literal_value) { if (!valid_id(target_id) || !std::isfinite(literal_value)) return false; detach_upstream(target_id); // Imperative '=' severs DAG edges! nodes_[target_id].expr = nullptr; nodes_[target_id].bound = false; return write_and_propagate(target_id, literal_value); } bool BindingDag::recompute_node(int id) { PropNode& node = nodes_[id]; if (!node.bound || !node.expr) return true; if (node.evaluating) return false; // Active re-entry -> BindingLoop! node.evaluating = true; ++node.evals; double computed = node.expr(); bool ok = std::isfinite(computed) && write_and_propagate(id, computed); node.evaluating = false; return ok; }starter/core.cpp injects three authentic QML-engine defects: (1) omitting the <= epsilon_ deadband check in write_and_propagate() so sub-epsilon jitter wakes downstream bindings, (2) leaving upstream subscriber edges attached inside assign_literal(), and (3) omitting the if (node.evaluating) return false; re-entrancy guard in recompute_node().
step 7/15 · 7. expected output
Passing files/ run (sh check.sh, exit code 0):
learner@ksa:~/gl-q03/lab-m01$ bat listing.txt File: listing.txt TAP version 13 1..5 ok 1 - builds libcore.a and lab_cli cleanly with -std=c++20 -Wall -Wextra -Werror -pedantic ok 2 - archives core.o into libcore.a and links independent TAP verifier ok 3 - propagates multi-hop bindings, coalesces sub-epsilon writes, and rejects NaN/Inf ok 4 - detaches active declarative binding on imperative assign_literal until rebound ok 5 - rejects direct and 3-node transitive BindingLoop cycles with clean value rollback # pass 5/5CLI verification output (./files/lab_cli):
learner@ksa:~/gl-q03/lab-m01$ bat listing.txt File: listing.txt DAG rpm=4500 -> deg=202.5 | manual_unbind=90.0 | rebound=270.0 | cycle_rejected=1Defective starter/ run (exit code 1): emits FAIL: diagnostics on the broken contract assertions.
step 8/15 · 8. explanation of observed behavior
Passing 5/5 TAP version 13 checks proves that BindingDag propagates rpm -> ratio -> deg in single topological passes, coalesces 0.0002 sub-epsilon noise with zero downstream evals, rejects IEEE-754 NaN/Inf faults, detaches bindings on assign_literal(), and rolls back a -> b -> c -> a cycles cleanly while restoring a == 2.0.
step 9/15 · 9. experiment variations
- Near transfer: Add a
set_readonly(int id, bool ro)flag toBindingDagsoassign_literal()returnsfalseonreadonlyderived nodes while still allowing upstreamrecompute_node()updates. - Far transfer: Implement
begin_deferred_batch()andflush_deferred_batch()onBindingDagto model QMLBinding { delayed: true }, deduplicating diamond-graph (A -> B,A -> C,B+C -> D) evaluations so nodeDevaluates once instead of twice whenAchanges.
step 10/15 · 10. performance measurements
Recorded host benchmark (./files/lab_cli --bench, CLOCK_MONOTONIC, warmup=5, N=40 batches):
learner@ksa:~/gl-q03/lab-m01$ bat listing.txt File: listing.txt BENCH lab_m01_qml_binding_dag (500 propagations/batch, 40 batches): p50=52541 ns p95=56690 ns p99=60019 ns (sink=1.0)The CLOCK_MONOTONIC percentiles measure 500 multi-hop in-memory std::function DAG propagations per batch on the Linux host CPU. Never extrapolate these user-space nanoseconds directly to ARMv7-A Cortex-A7 QML startup or I2C/CAN peripheral bus latency.
step 11/15 · 11. troubleshooting
| Symptom | Root cause | Fix | ||
|---|---|---|---|---|
not ok 3 (sub-epsilon write woke downstream binding) | write_and_propagate mutated value and iterated subscribers even when ` | value - next_val | <= epsilon_` | Return true early at the top of write_and_propagate whenever std::abs(nodes_[id].value - next_val) <= epsilon_. |
not ok 4 (assign_literal failed to clear is_bound()) | assign_literal wrote literal_value without calling detach_upstream(target_id) and clearing bound | Call detach_upstream(target_id), reset expr = nullptr, and set bound = false before write_and_propagate. | ||
not ok 5 (accepted 3-node cycle a->b->c->a) | recompute_node did not check if (node.evaluating) return false; on entry | Check node.evaluating before incrementing evals, and restore saved_val inside bind() when recompute_node fails. |
step 12/15 · 12. guided questions
- Why does
write_and_propagate()iterate overstd::vector<int> snapshot = nodes_[id].subscribers;instead of iteratingnodes_[id].subscribersby reference?
- *Hint 1:* Consider what happens if a subscriber callback invokes
bind()orassign_literal()during propagation. - *Hint 2:* Mutating
subscribers(eraseorpush_back) while iterating it by reference invalidatesstd::vectoriterators and triggers undefined behavior!
- In a diamond dependency graph (
rpm -> leftGauge,rpm -> rightGauge, andtotalPowerdepending on both), doesnode.evaluatingfalsely report aBindingLoop?
- *Hint 1:* Trace the lifetime of
totalPower.evaluatingacross the two sequential branches. - *Hint 2:* No!
totalPower.evaluatingresets tofalseas soon as the first branch (leftGauge -> totalPower) returns, so the second branch (rightGauge -> totalPower) seesevaluating == false— only true ancestor cycles seetrue!
step 13/15 · 13. independent challenges
Instrument a small Qt 6.8 QQmlApplicationEngine prototype on target or host with QT_LOGGING_RULES="qt.qml.binding.removal.info=true" and verify that replacing an imperative onClicked: bar.width = 0 handler with Binding { when: pressed; value: 0 } eliminates every binding-removal warning log.
step 14/15 · 14. solutions and explanations
Restoring the <= epsilon_ short-circuit in write_and_propagate(), detaching upstream edges and clearing bound in assign_literal(), and enforcing if (node.evaluating) return false; in recompute_node() brings all 5/5 TAP checks green.
step 15/15 · run verification and inspect output
Run the verification command and confirm the output matches the expected contract.
learner@ksa:~/gl-q03/lab-m01./files/lab_clilearner@ksa:~/gl-q03/lab-m01DAG rpm=4500 -> deg=202.5 | manual_unbind=90.0 | rebound=270.0 | cycle_rejected=1Progressive hints & solution
hint 1
Compare starter/Makefile against the module lessons. Focus first on: Builds libcore.a and lab_cli cleanly with -std=c++20 -Wall -Wextra -Werror -pedantic.
hint 2
Compile with -std=c11 -Wall -Wextra -Werror, check every system-call return value before touching output buffers, and verify cleanup on error paths.
solution
warning: Reveal the reference solution files? Running ./check.sh against your own attempt first builds stronger retention.
solution: files/Makefile
learner@ksa:~/gl-q03/lab-m01$ bat files/Makefile File: files/Makefile CC_BIN = g++ AR = ar CFLAGS_ALL = -std=c++20 -Wall -Wextra -Werror -pedantic -O2 LDLIBS = all: libcore.a lab_cli libcore.a: core.o $(AR) rcs $@ $^ lab_cli: cli.o libcore.a $(CC_BIN) $(CFLAGS_ALL) cli.o libcore.a $(LDLIBS) -o $@ %.o: %.cpp core.hpp $(CC_BIN) $(CFLAGS_ALL) -c $< -o $@ .PHONY: all clean clean: rm -f core.o cli.o libcore.a lab_clisolution: files/cli.cpp
learner@ksa:~/gl-q03/lab-m01$ bat files/cli.cpp File: files/cli.cpp #include "core.hpp" #include <algorithm> #include <cinttypes> #include <cstdio> #include <cstring> #include <ctime> #include <vector> static uint64_t mono_ns() { struct timespec ts {}; clock_gettime(CLOCK_MONOTONIC, &ts); return static_cast<uint64_t>(ts.tv_sec) * 1000000000ULL + static_cast<uint64_t>(ts.tv_nsec); } static int run_bench() { std::vector<uint64_t> samples(40, 0); double acc = 0.0; hmi::BindingDag dag(1e-4); int rpm = dag.create_property(1000.0); int max_rpm = dag.create_property(6000.0); int width = dag.create_property(344.0); int usable = dag.create_property(0.0); int ratio = dag.create_property(0.0); int bar_px = dag.create_property(0.0); dag.bind(usable, {width}, [&] { return dag.value(width) - 24.0; }); dag.bind(ratio, {rpm, max_rpm}, [&] { return std::clamp(dag.value(rpm) / dag.value(max_rpm), 0.0, 1.0); }); dag.bind(bar_px, {usable, ratio}, [&] { return dag.value(usable) * dag.value(ratio); }); for (int iter = 0; iter < 45; ++iter) { uint64_t t0 = mono_ns(); for (int k = 0; k < 500; ++k) { dag.assign_literal(rpm, 1200.0 + static_cast<double>((iter + k) % 4500)); acc += dag.value(bar_px); } uint64_t dt = mono_ns() - t0; if (iter >= 5) samples[static_cast<size_t>(iter - 5)] = dt; } volatile double sink = acc; std::sort(samples.begin(), samples.end()); std::printf( "BENCH lab_m01_qml_binding_dag (500 propagations/batch, 40 batches): " "p50=%" PRIu64 " ns p95=%" PRIu64 " ns p99=%" PRIu64 " ns (sink=%.1f)\n", samples[20], samples[38], samples[39], sink > 0.0 ? 1.0 : 0.0); return 0; } int main(int argc, char** argv) { if (argc > 1 && std::strcmp(argv[1], "--bench") == 0) { return run_bench(); } hmi::BindingDag dag(1e-4); int rpm = dag.create_property(1500.0); int max_rpm = dag.create_property(6000.0); int ratio = dag.create_property(0.0); int deg = dag.create_property(0.0); if (!dag.bind(ratio, {rpm, max_rpm}, [&] { return dag.value(rpm) / dag.value(max_rpm); })) { return 1; } if (!dag.bind(deg, {ratio}, [&] { return dag.value(ratio) * 270.0; })) { return 1; } dag.assign_literal(rpm, 4500.0); double live_deg = dag.value(deg); dag.assign_literal(deg, 90.0); dag.assign_literal(rpm, 6000.0); double manual_deg = dag.value(deg); dag.bind(deg, {ratio}, [&] { return dag.value(ratio) * 270.0; }); double rebound_deg = dag.value(deg); bool self_loop_ok = !dag.bind(rpm, {deg}, [&] { return dag.value(deg); }); std::printf( "DAG rpm=4500 -> deg=%.1f | manual_unbind=%.1f | rebound=%.1f | " "cycle_rejected=%u\n", live_deg, manual_deg, rebound_deg, self_loop_ok ? 1U : 0U); return self_loop_ok ? 0 : 1; }solution: files/core.cpp
learner@ksa:~/gl-q03/lab-m01$ bat files/core.cpp File: files/core.cpp #include "core.hpp" #include <algorithm> #include <cmath> #include <utility> namespace hmi { BindingDag::BindingDag(double epsilon) : epsilon_(epsilon) {} bool BindingDag::valid_id(int id) const noexcept { return id >= 0 && static_cast<size_t>(id) < nodes_.size(); } int BindingDag::create_property(double initial_value) { double safe = std::isfinite(initial_value) ? initial_value : 0.0; nodes_.push_back(PropNode{safe, false, false, {}, nullptr, {}, 0, 0}); return static_cast<int>(nodes_.size() - 1); } void BindingDag::detach_upstream(int id) { for (int dep : nodes_[id].deps) { auto& subs = nodes_[dep].subscribers; subs.erase(std::remove(subs.begin(), subs.end(), id), subs.end()); } nodes_[id].deps.clear(); } bool BindingDag::bind(int target_id, std::vector<int> dependencies, std::function<double()> expr) { if (!valid_id(target_id) || !expr) return false; for (int dep : dependencies) { if (!valid_id(dep) || dep == target_id) return false; } double saved_val = nodes_[target_id].value; detach_upstream(target_id); nodes_[target_id].deps = std::move(dependencies); nodes_[target_id].expr = std::move(expr); nodes_[target_id].bound = true; for (int dep : nodes_[target_id].deps) { if (std::find(nodes_[dep].subscribers.begin(), nodes_[dep].subscribers.end(), target_id) == nodes_[dep].subscribers.end()) { nodes_[dep].subscribers.push_back(target_id); } } if (!recompute_node(target_id)) { detach_upstream(target_id); nodes_[target_id].expr = nullptr; nodes_[target_id].bound = false; nodes_[target_id].evaluating = false; nodes_[target_id].value = saved_val; return false; } return true; } bool BindingDag::assign_literal(int target_id, double literal_value) { if (!valid_id(target_id) || !std::isfinite(literal_value)) return false; detach_upstream(target_id); nodes_[target_id].expr = nullptr; nodes_[target_id].bound = false; return write_and_propagate(target_id, literal_value); } bool BindingDag::recompute_node(int id) { PropNode& node = nodes_[id]; if (!node.bound || !node.expr) return true; if (node.evaluating) return false; node.evaluating = true; ++node.evals; double computed = node.expr(); if (!std::isfinite(computed)) { node.evaluating = false; return false; } bool ok = write_and_propagate(id, computed); node.evaluating = false; return ok; } bool BindingDag::write_and_propagate(int id, double next_val) { if (std::abs(nodes_[id].value - next_val) <= epsilon_) { return true; } nodes_[id].value = next_val; ++nodes_[id].notifies; std::vector<int> snapshot = nodes_[id].subscribers; for (int sub : snapshot) { if (!recompute_node(sub)) return false; } return true; } double BindingDag::value(int id) const noexcept { return valid_id(id) ? nodes_[id].value : 0.0; } bool BindingDag::is_bound(int id) const noexcept { return valid_id(id) ? nodes_[id].bound : false; } uint32_t BindingDag::notify_count(int id) const noexcept { return valid_id(id) ? nodes_[id].notifies : 0; } uint32_t BindingDag::eval_count(int id) const noexcept { return valid_id(id) ? nodes_[id].evals : 0; } } // namespace hmisolution: files/core.hpp
learner@ksa:~/gl-q03/lab-m01$ bat files/core.hpp File: files/core.hpp #pragma once #include <cstdint> #include <functional> #include <vector> namespace hmi { class BindingDag { public: explicit BindingDag(double epsilon = 1e-6); int create_property(double initial_value); bool bind(int target_id, std::vector<int> dependencies, std::function<double()> expr); bool assign_literal(int target_id, double literal_value); double value(int id) const noexcept; bool is_bound(int id) const noexcept; uint32_t notify_count(int id) const noexcept; uint32_t eval_count(int id) const noexcept; private: struct PropNode { double value = 0.0; bool bound = false; bool evaluating = false; std::vector<int> deps; std::function<double()> expr; std::vector<int> subscribers; uint32_t notifies = 0; uint32_t evals = 0; }; double epsilon_; std::vector<PropNode> nodes_; bool valid_id(int id) const noexcept; void detach_upstream(int id); bool recompute_node(int id); bool write_and_propagate(int id, double next_val); }; } // namespace hmiRun the check
From labs/lab_m01_qml_binding_dag/, run ./check.sh. A complete solution emits TAP version 13 with every test point marked ok and exits 0:
learner@ksa:~/gl-q03/lab-m01./check.shTAP version 131..5ok 1 - builds libcore.a and lab_cli cleanly with -std=c++20 -Wall -Wextra -Werror -pedanticok 2 - archives core.o into libcore.a and links independent TAP verifierok 3 - propagates multi-hop bindings, coalesces sub-epsilon writes, and rejects NaN/Infok 4 - detaches active declarative binding on imperative assign_literal until reboundok 5 - rejects direct and 3-node transitive BindingLoop cycles with clean value rollback# pass 5/5On an incomplete starter tree, ./check.sh exits non-zero and marks the unmet assertion not ok with an indented YAML diagnostic block:
learner@ksa:~/gl-q03/lab-m01./check.shTAP version 131..5ok 1 - builds libcore.a and lab_cli cleanly with -std=c++20 -Wall -Wextra -Werror -pedanticok 2 - archives core.o into libcore.a and links independent TAP verifiernot ok 3 - propagates multi-hop bindings, coalesces sub-epsilon writes, and rejects NaN/InfFAIL: check std::isfinite in assign_literal and skip notification when abs(value - next_val) <= epsilon_ --- message: 'got sub-epsilon write woke downstream binding (3 vs 2)' hint: 'check std::isfinite in assign_literal and skip notification when abs(value - next_val) <= epsilon_' ...not ok 4 - detaches active declarative binding on imperative assign_literal until reboundFAIL: call detach_upstream(target_id) and clear expr/bound in BindingDag::assign_literal --- message: 'got assign_literal failed to clear is_bound()' hint: 'call detach_upstream(target_id) and clear expr/bound in BindingDag::assign_literal' ...not ok 5 - rejects direct and 3-node transitive BindingLoop cycles with clean value rollbackFAIL: return false immediately when node.evaluating is true in recompute_node and roll back target_id in bind --- message: 'got accepted 3-node cycle a->b->c->a' hint: 'return false immediately when node.evaluating is true in recompute_node and roll back target_id in bind' ...# pass 2/5note: How to read TAP diagnostics: 1..N states the total test plan; each ok k confirms a passed check; not ok k pinpoints the failing check alongside message: (what went wrong) and hint: (where to look next) between --- and ....
inspect harness: check.sh
learner@ksa:~/gl-q03/lab-m01$ bat check.sh File: check.sh #!/bin/sh set -u cd "$(dirname "$0")" || exit 1 PASS=0 WORK=$(mktemp -d "${TMPDIR:-/tmp}/ksa_lab.XXXXXX") || exit 1 trap 'rm -rf "$WORK"' EXIT HUP INT TERM cp -R files "$WORK/files" printf 'TAP version 13\n1..5\n' if make -B -C "$WORK/files" >"$WORK/build.log" 2>&1; then printf 'ok 1 - builds libcore.a and lab_cli cleanly with -std=c++20 -Wall -Wextra -Werror -pedantic\n' PASS=$((PASS + 1)) else printf 'not ok 1 - builds libcore.a and lab_cli cleanly with -std=c++20 -Wall -Wextra -Werror -pedantic\n' printf 'FAIL: fix compiler warnings or errors in files/core.cpp\n' fi if [ -f "$WORK/files/libcore.a" ] && g++ -std=c++20 -Wall -Wextra -Werror -pedantic -O2 -I"$WORK/files" tests.cpp "$WORK/files/libcore.a" -o "$WORK/verify" >>"$WORK/build.log" 2>&1; then printf 'ok 2 - archives core.o into libcore.a and links independent TAP verifier\n' PASS=$((PASS + 1)) else printf 'not ok 2 - archives core.o into libcore.a and links independent TAP verifier\n' printf 'FAIL: preserve public declarations in files/core.hpp so tests.cpp links\n' fi if [ -x "$WORK/verify" ] && OUT=$("$WORK/verify" 1 2>&1); then printf 'ok 3 - propagates multi-hop bindings, coalesces sub-epsilon writes, and rejects NaN/Inf\n' PASS=$((PASS + 1)) else printf 'not ok 3 - propagates multi-hop bindings, coalesces sub-epsilon writes, and rejects NaN/Inf\n' printf 'FAIL: check std::isfinite in assign_literal and skip notification when abs(value - next_val) <= epsilon_\n' printf " ---\n message: 'got %s'\n hint: 'check std::isfinite in assign_literal and skip notification when abs(value - next_val) <= epsilon_'\n ...\n" "${OUT:-build_failed}" fi if [ -x "$WORK/verify" ] && OUT=$("$WORK/verify" 2 2>&1); then printf 'ok 4 - detaches active declarative binding on imperative assign_literal until rebound\n' PASS=$((PASS + 1)) else printf 'not ok 4 - detaches active declarative binding on imperative assign_literal until rebound\n' printf 'FAIL: call detach_upstream(target_id) and clear expr/bound in BindingDag::assign_literal\n' printf " ---\n message: 'got %s'\n hint: 'call detach_upstream(target_id) and clear expr/bound in BindingDag::assign_literal'\n ...\n" "${OUT:-build_failed}" fi if [ -x "$WORK/verify" ] && OUT=$("$WORK/verify" 3 2>&1); then printf 'ok 5 - rejects direct and 3-node transitive BindingLoop cycles with clean value rollback\n' PASS=$((PASS + 1)) else printf 'not ok 5 - rejects direct and 3-node transitive BindingLoop cycles with clean value rollback\n' printf 'FAIL: return false immediately when node.evaluating is true in recompute_node and roll back target_id in bind\n' printf " ---\n message: 'got %s'\n hint: 'return false immediately when node.evaluating is true in recompute_node and roll back target_id in bind'\n ...\n" "${OUT:-build_failed}" fi printf '# pass %d/5\n' "$PASS" [ "$PASS" -eq 5 ]Definition of done
Verification checklist
status: 0/3 checks verified
Troubleshooting
Compiler fails under -Werror on sign conversion or unused args
Use exact POSIX return types (ssize_t for read()/write(), size_t for byte counts) rather than relaxing flags.
./check.sh reports missing files in files/
Invoke ./check.sh from inside labs/lab_m01_qml_binding_dag/ so relative paths to files/ resolve directly.