Code-as-Monitor: Constraint-aware Visual Programming for Reactive and Proactive Robotic Failure Detection

Enshen Zhou, Qi Su, Cheng Chi, Zhizheng Zhang, Zhongyuan Wang, Tiejun Huang, Lu Sheng, He Wang

Summary

Code-as-Monitor (CaM) is a novel framework that transforms robotic failure detection into a real-time visual programming task. By leveraging Vision-Language Models (VLMs) to generate Python scripts that monitor geometric constraints, robots can detect unforeseen failures both reactively and proactively, significantly improving success rates in dynamic environments.

Introduction: The Open-Set Failure Challenge

Robots operating in unstructured, real-world environments frequently encounter "open-set" failures—unpredictable events that were not present during training. While modern VLMs like GPT-4o exhibit impressive reasoning, they are often too slow for frame-by-frame monitoring and lack the 3D spatial precision required to detect subtle physical deviations, such as a slight tilt in a container. Current systems typically fall into two categories: reactive-only monitors that detect failures after they occur, or slow inference loops that prevent real-time control.

The CaM Framework: From Text to Geometry

The CaM framework addresses these limitations by decomposing high-level tasks into precise geometric monitoring scripts. It consists of three primary modules:

  1. Constraint Generator: A VLM (GPT-4o) analyzes the task and scene to derive subgoals and textual constraints (e.g., "the pan must remain horizontal").
  2. Constraint Painter (ConSeg): A specialized multi-granularity segmentation model that translates textual constraints into geometric primitives, such as 3D coordinates for points, lines, or surfaces.
  3. Constraint Monitor: The VLM generates a Python snippet that uses these geometric elements to calculate real-time status.
Overview of the CaM framework across simulated and real-world environments.

By employing an off-the-shelf point tracker (CoTracker), the system follows these geometric elements across frames at high speed, eliminating the need for continuous VLM calls and enabling real-time "closed-loop" monitoring.

Proactive vs. Reactive Detection

A core insight of CaM is that many robotic failures are preceded by the violation of a spatial constraint. By monitoring these constraints—such as the angle of a teapot or the position of a lobster in a pan—the system can intervene before a failure occurs.

Comparison of reactive (left) and proactive (right) monitoring strategies.

As shown in the comparison above, a reactive system only identifies a failure once the object has left the pan ($t^R_4$). In contrast, the proactive CaM system detects the tilt at $t^P_3$ and triggers a corrective action ($t^{P'}_3$), ensuring the task continues successfully. This "geometry as a universal language" approach allows the robot to generalize to novel objects by focusing on abstract spatial relationships rather than raw visual features.

Experimental Results and Real-World Robustness

The authors evaluated CaM across three major simulators (CLIPort, Omnigibson, and RLBench) and a real-world UR5 robotic arm setup. Key findings include:

  • Higher Success Rates: CaM achieved a 28.7% higher success rate than baselines across various simulators under severe disturbances.
  • Efficiency: Execution time was reduced by 31.8%, with a significant decrease in VLM token usage because the VLM only generates the monitor code once per subgoal.
  • Precision: In tasks like "Pour Tea," CaM was uniquely capable of detecting failures caused by surface-level disturbances that VQA-based baselines missed entirely.
  • Real-World Adaptability: The system successfully handled cluttered scenes and dynamic human interference, adjusting targets in real-time as objects were moved mid-task.

Conclusion

Code-as-Monitor demonstrates that visual programming is a powerful tool for robotic safety. By translating high-level reasoning into low-level geometric scripts, CaM provides a bridge between the cognitive capabilities of VLMs and the precision requirements of real-time robotic control. Future work will focus on tighter integration with low-level control policies and improving tracking robustness under extreme occlusion.

Summary updated

BibTeX
@inproceedings{zhou2025code,
  author = {Enshen Zhou and Qi Su and Cheng Chi and Zhizheng Zhang and Zhongyuan Wang and Tiejun Huang and Lu Sheng and He Wang},
  title = {{Code-as-Monitor: Constraint-aware Visual Programming for Reactive and Proactive Robotic Failure Detection}},
  booktitle = {IEEE/CVF Conference on Computer Vision and Pattern Recognition},
  year = {2025},
  doi = {10.1109/cvpr52734.2025.00649},
}
Figure