/ restai / llms / tools / delete_routine.py
delete_routine.py
 1  def delete_routine(routine_id: int, **kwargs) -> str:
 2      """Delete a scheduled routine on the project this agent belongs to.
 3  
 4      Use `list_routines` first to find the id of the routine to delete.
 5      The tool will refuse to delete a routine that belongs to a different
 6      project — agents can only manage their own.
 7  
 8      Args:
 9          routine_id (int): The id of the routine to delete (from
10              list_routines).
11      """
12      project_id = kwargs.get("_project_id")
13      if project_id is None:
14          return "ERROR: delete_routine requires a project context."
15  
16      try:
17          rid = int(routine_id)
18      except (TypeError, ValueError):
19          return "ERROR: routine_id must be an integer."
20  
21      from restai.database import get_db_wrapper
22  
23      db = get_db_wrapper()
24      try:
25          routine = db.get_project_routine_by_id(rid)
26          if routine is None:
27              return f"ERROR: routine {rid} not found."
28          if routine.project_id != int(project_id):
29              # Defense in depth — the tool's project context comes from the
30              # agent runtime and shouldn't ever leak across projects, but
31              # double-check before mutating.
32              return f"ERROR: routine {rid} does not belong to this project."
33          name = routine.name
34          ok = db.delete_project_routine(rid)
35          if not ok:
36              return f"ERROR: failed to delete routine {rid}."
37          return f"Deleted routine id={rid} name='{name}'."
38      except Exception as e:
39          return f"ERROR: failed to delete routine: {e}"
40      finally:
41          db.db.close()