Code Models are Zero-shot Precondition Reasoners

Lajanugen Logeswaran, Sungryull Sohn, Yiwei Lyu, Anthony Z. Liu, Dong-Ki Kim, Dongsub Shim, Moontae Lee, Honglak Lee

Abstract

This paper introduces a framework that leverages pre-trained code models to automatically infer "preconditions"—the logical requirements that must be met before an action can be taken—from expert demonstrations. By representing agent trajectories as Python programs and using execution-based verification, the authors significantly improve the reliability and planning capabilities of AI agents in task-oriented dialog and embodied environments.

Introduction

In sequential decision-making, an agent (like a robot or a chatbot) needs to understand affordance: knowing which actions are plausible at any given moment. For example, a restaurant-booking agent shouldn't try to "Reserve" a table before it has "Found" a restaurant and confirmed the "Time."

Traditionally, these preconditions are either manually hard-coded (which is tedious and doesn't scale) or learned via trial and error. While Large Language Models (LLMs) can guess these rules using natural language, their reasoning is often inconsistent and impossible to verify formally. This paper addresses the gap in creating a systematic, verifiable, and zero-shot way to extract these rules from a few examples of successful behavior.

Methodology

The authors propose a three-stage pipeline that treats precondition inference as a code completion and verification problem.

Overview of the Precondition Inference and Prediction Pipeline
  1. Trajectory as Code: Agent interactions are represented as Python programs. Observations and actions are mapped to function calls that modify a set of state variables (e.g., self.user.informed_slot['city'] = True).
  2. Precondition Inference Pipeline:
    • Candidate Generation: A code model (e.g., StarCoder or CodeGen) is prompted to generate assert statements within action functions based on demonstration trajectories.
  3. Validation with Execution: Because the rules are code, they can be executed. Any generated assertion that fails when run against the valid expert demonstrations is immediately discarded.
  4. Ranking: A logical ranking mechanism identifies the most "discriminative" assertions—those that best define the specific boundaries of when an action is allowed.
  5. Precondition-Aware Sampling: During deployment, if the agent's policy suggests an action that violates an inferred precondition (i.e., the assert fails), the system rejects that action and resamples until a valid one is found.

Experimental Evaluation

The approach was evaluated on two distinct benchmarks: Schema-Guided Dialogue (SGD), for task-oriented conversations across 10 domains, and ALFworld, a benchmark for embodied agents performing complex physical tasks in a text-based world.

Key Results

  • High Precision: On the ALFworld benchmark, the proposed method achieved 1.0 precision in precondition inference, meaning every rule it extracted was a valid logical constraint.
  • Improved Success Rates: Augmenting a "ReAct" (Reason + Act) prompting strategy with inferred preconditions improved the task success rate from 44% to 48% on ALFworld.
  • Precondition Compatibility: The "Cmp." metric (how often the agent's predicted action followed ground-truth rules) jumped from 81% to 96% on ALFworld when using the framework.
  • Few-Shot Efficiency: The system works remarkably well with as few as one or two demonstrations, making it highly practical for new domains where data is scarce.

Discussion and Insights

Code as a Logical Guardrail

Unlike natural language rationales, code is executable. This allows for a "generate-and-test" loop where the environment itself acts as the ground truth for whether a rule is correct. Code models already possess significant knowledge about logic and procedural dependencies from their training data. They can often guess the correct precondition for a Reserve() function simply because they've seen similar logic in real-world APIs.

One of the most interesting findings is that providing preconditions helps smaller models (2B parameters) significantly close the performance gap with much larger models (16B parameters). Preconditions act as a "logical guardrail" that compensates for the smaller model's weaker inherent reasoning.

Limitations and Future Work

Currently, a human must still define the "state variables" (the class attributes like self.inventory) and the function signatures. Future work could involve LLMs automatically discovering these variables. Additionally, while the system works well on structured tasks, applying this to highly open-ended, non-programmatic environments remains a challenge.

Summary updated

BibTeX
@inproceedings{logeswaran2024code,
  author = {Lajanugen Logeswaran and Sungryull Sohn and Yiwei Lyu and Anthony Z. Liu and Dong-Ki Kim and Dongsub Shim and Moontae Lee and Honglak Lee},
  title = {{Code Models are Zero-shot Precondition Reasoners}},
  booktitle = {Conference of the North American Chapter of the Association for Computational Linguistics},
  year = {2024},
  doi = {10.18653/v1/2024.naacl-long.317},
}
Figure