Deadman922 commited on
Commit
e102f6d
·
verified ·
1 Parent(s): 924e140

Upload environment.py

Browse files
Files changed (1) hide show
  1. server/environment.py +17 -17
server/environment.py CHANGED
@@ -208,33 +208,33 @@ class SQLQualityEnvironment:
208
 
209
  def _handle_query(self, action: SQLAction) -> StepResponse:
210
  """
211
- Handle a read-only QUERY action.
212
 
213
- Executes the SQL on the episode connection (read-only SELECT enforced).
214
  Returns a result preview in the observation. Does NOT end the episode.
215
  Awards small step_reward (0.05) for queries that target the right area.
216
  """
217
  sql = (action.sql or "").strip()
218
 
219
- # Enforce read-only: only SELECT allowed
220
- if not sql.upper().startswith("SELECT"):
221
- step_reward = 0.0
222
- result_preview = "Error: QUERY action only accepts SELECT statements."
223
- else:
224
- try:
225
- cursor = self._original_conn.execute(sql)
226
  rows = cursor.fetchmany(5)
227
  cols = [d[0] for d in cursor.description]
228
  result_preview = str([dict(zip(cols, row)) for row in rows])[:300]
229
- # Small reward for targeted queries
230
- from server.graders import _query_reveals_issue
 
 
 
231
 
232
- step_reward = (
233
- 0.05 if _query_reveals_issue(sql, self._ground_truth) else 0.01
234
- )
235
- except sqlite3.Error as e:
236
- result_preview = f"SQL Error: {e}"
237
- step_reward = 0.0
238
 
239
  # Add to trajectory
240
  self._trajectory.append(
 
208
 
209
  def _handle_query(self, action: SQLAction) -> StepResponse:
210
  """
211
+ Handle a QUERY action.
212
 
213
+ Executes the SQL on the episode connection.
214
  Returns a result preview in the observation. Does NOT end the episode.
215
  Awards small step_reward (0.05) for queries that target the right area.
216
  """
217
  sql = (action.sql or "").strip()
218
 
219
+ try:
220
+ cursor = self._original_conn.execute(sql)
221
+ # Check if query returns rows (SELECT) or just modifies (INSERT/UPDATE/DELETE)
222
+ if cursor.description:
 
 
 
223
  rows = cursor.fetchmany(5)
224
  cols = [d[0] for d in cursor.description]
225
  result_preview = str([dict(zip(cols, row)) for row in rows])[:300]
226
+ else:
227
+ # For INSERT/UPDATE/DELETE, show affected rows
228
+ result_preview = f"Query executed successfully. Rows affected: {cursor.rowcount}"
229
+ # Small reward for targeted queries
230
+ from server.graders import _query_reveals_issue
231
 
232
+ step_reward = (
233
+ 0.05 if _query_reveals_issue(sql, self._ground_truth) else 0.01
234
+ )
235
+ except sqlite3.Error as e:
236
+ result_preview = f"SQL Error: {e}"
237
+ step_reward = 0.0
238
 
239
  # Add to trajectory
240
  self._trajectory.append(