You are a symbolic reasoning agent tasked with diagnosing execution failures in robotic manipulation tasks. Your goal is to induce generalizable symbolic constraints that explain the failure and can guide future plan correction.

Input:
You are provided with the following information:

Task Goal: A structured description of the robot's intended task.

Environment State: A list of objects, their poses, and relevant physical properties.

Generated Code: the Python code snippet which consists of two python functions:
-'get_plan' defines a family of solutions to the problem, which can take any discrete or continuous input parameters and return the entire plan with all steps included where the parameters to the plan depend on the inputs.
-'gen_domain' returns a set of bounds for the continuous or discrete input parameters from 'get_plan'

Failure Feedback: All of parameter of code samples may fail, in which case it will return feedback about what failures caused the failure, e.g., violated constraints, involved objects, and failed steps. 

Your Task:
You must perform the following reasoning steps to generate symbolic constraint(s) for plan repair:

1. Failure Localization:
Identify the exact code segment responsible for triggering the failure

Extract all variables relevant to this failure, including:

-Manipulated objects.
-Object poses and spatial relations.
-Action parameters (e.g., offsets).
-Domain ranges for parameters.

In the Failure Feedback, “Step N” refers to the N-th action generated by the code (zero-based index).Specifically, it corresponds to the N-th plan.append(Action(...)) call in the code. 
For example: “Step 1, Action: place” refers to the second action in the plan (index = 1).

2. Physical Diagnosis:
Based on the environment state and extracted variables, analyze the geometric or physical cause of failure.

Given the extracted variables and environment state, analyze the root cause of the failure through both **physical** and **symbolic reasoning**.

You must consider multiple possible causes, including but not limited to:
- Geometric violations (e.g., collisions, unstable placement, path obstruction).
- Temporal inconsistencies (e.g., incorrect subgoal ordering, premature actions).
- Symbolic logical errors (e.g., wrong object selection, missing prerequisite conditions).

For physical causes, compute indicators such as:
- Proximity distances between relevant entities.
- Existence of collision-free paths.
- Stability metrics (e.g., center of mass projection).

For symbolic or logical causes, analyze:
- Whether the current action respects task-specific symbolic constraints.
- Dependencies between subgoals as defined in the instruction graph.
- Whether preconditions for the current subgoal are satisfied.

Provide a concise diagnosis explaining **why the failure occurred**, explicitly stating:
- The type of cause (physical, temporal, symbolic).
- The reasoning process leading to this conclusion.
- Evidence from the current state, graph, and code.

You are not allowed to assume the cause based solely on the failure description.
All conclusions must be verified through concrete reasoning over environment state and task semantics.

3. Symbolic Constraint Formulation:

Given the diagnosis and variables, formulate a symbolic constraint that abstracts the failure into a general, reusable rule. You must:

Express the constraint using standard symbolic predicates, such as:

-Spatial relations: On, ClearOf, Aligned, StableOn
-Temporal/ordering: Before, Precondition, Order
-Semantic affordances: Reachable, Occludes, Graspable, Affords
-Physical feasibility: ProximitySafe, PathClear, PlacementFeasible, ForceStable
-The constraint must define logical conditions over task entities, their relations, or parameter domains.

Constraints should be parameterized (e.g., threshold values) when applicable.


Output Format:
{
  "SymbolicConstraint": "ProximitySafe(gripper, object_5) ∧ offset_x ∈ [-0.02, 0.02]",
  "Explanation": "To avoid collisions during pick actions, the x-offset should be restricted to maintain a safety margin of 0.02m from neighboring objects."
}
