bep40 commited on
Commit
8101daf
·
verified ·
1 Parent(s): c99a4bb

Upload patch_fix_approval_waiting.py

Browse files
Files changed (1) hide show
  1. patch_fix_approval_waiting.py +119 -35
patch_fix_approval_waiting.py CHANGED
@@ -3,18 +3,24 @@
3
  Patch: Fix 'waiting approval' after Space rebuild/session restore.
4
 
5
  Root cause:
6
- When a Space restarts, sessions are restored from the HF dataset
7
- (via patch_catch_up_context_restore.py). However, the auto_approval
8
- dict that patch_agent_backend.py sets in Session.__init__ is NOT
9
- re-applied to restored sessions. The frontend then shows
10
- 'waiting-approval' for every tool call because auto_approval.enabled
11
- is missing/False after dataset restore.
 
 
 
 
12
 
13
  Fix:
14
- 1. In session_manager.py's ensure_session_loaded (the dataset restore path),
15
- set auto_approval = {'enabled': True, ...} on the restored session metadata.
16
- 2. In session.py, add ensure_auto_approval() method to guarantee auto_approval.
17
- 3. In routes/agent.py, include auto_approval in SSE session update events.
 
 
18
 
19
  Runs during Docker build -- never fails the build.
20
  """
@@ -41,7 +47,7 @@ def _replace(content, old, new):
41
  return content, False
42
 
43
 
44
- # --- Patch session_manager.py: set auto_approval on restored sessions ---
45
  SESSION_MANAGER = "/app/backend/session_manager.py"
46
 
47
  if os.path.exists(SESSION_MANAGER):
@@ -61,13 +67,16 @@ if os.path.exists(SESSION_MANAGER):
61
  " logger.debug(\"Dataset restore fallback failed for %s: %s\", session_id, e)\n"
62
  "\n"
63
  " # === PATCH: fix-approval-waiting ===\n"
64
- " # Ensure auto_approval is always enabled for restored sessions\n"
65
- " # so the frontend never shows 'waiting approval' after a restart.\n"
66
  " if loaded:\n"
67
  " if isinstance(loaded, dict):\n"
68
  " meta = loaded.setdefault('metadata', {})\n"
69
  " if isinstance(meta, dict):\n"
70
- " meta.setdefault('auto_approval', {'enabled': True, 'cost_cap_usd': None, 'estimated_spend_usd': 0.0, 'remaining_usd': None})\n"
 
 
 
 
 
71
  " if not loaded:\n"
72
  " return None\n"
73
  )
@@ -75,30 +84,15 @@ if os.path.exists(SESSION_MANAGER):
75
  if ok:
76
  print("+ ensure_session_loaded sets auto_approval on restored sessions")
77
  changed = True
78
- else:
79
- old_alt = " if not loaded:\n return None\n"
80
- new_alt = (
81
- " # === PATCH: fix-approval-waiting ===\n"
82
- " if loaded and isinstance(loaded, dict):\n"
83
- " meta = loaded.setdefault('metadata', {})\n"
84
- " if isinstance(meta, dict):\n"
85
- " meta.setdefault('auto_approval', {'enabled': True, 'cost_cap_usd': None, 'estimated_spend_usd': 0.0, 'remaining_usd': None})\n"
86
- " if not loaded:\n"
87
- " return None\n"
88
- )
89
- content, ok = _replace(content, old_alt, new_alt)
90
- if ok:
91
- print("+ Added auto_approval default to session restore path (alt)")
92
- changed = True
93
 
94
- # 2. Patch seed_from_summary to set auto_approval on session object
95
- old_seed_end = (
96
  " if seeded_from_dataset:\n"
97
  " self._touch(agent_session)\n"
98
  " await self.persist_session_snapshot(agent_session, runtime_state=\"idle\")\n"
99
  " return seeded_from_dataset\n"
100
  )
101
- new_seed_end = (
102
  " if seeded_from_dataset:\n"
103
  " # === PATCH: fix-approval-waiting ===\n"
104
  " if not getattr(agent_session, 'auto_approval', None):\n"
@@ -112,9 +106,69 @@ if os.path.exists(SESSION_MANAGER):
112
  " await self.persist_session_snapshot(agent_session, runtime_state=\"idle\")\n"
113
  " return seeded_from_dataset\n"
114
  )
115
- content, ok = _replace(content, old_seed_end, new_seed_end)
116
  if ok:
117
- print("+ seed_from_summary sets auto_approval on session object")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
118
  changed = True
119
 
120
  if changed:
@@ -169,13 +223,38 @@ if os.path.exists(SESSION):
169
  " self.auto_approval.setdefault('enabled', True)\n"
170
  " elif isinstance(self.auto_approval, bool):\n"
171
  " self.auto_approval = {'enabled': bool(self.auto_approval),'cost_cap_usd': None,'estimated_spend_usd': 0.0,'remaining_usd': None}\n"
172
- "\n"
173
  )
174
  content = content[:best_idx] + helper + content[best_idx:]
175
  print("+ session.py: added ensure_auto_approval() method")
176
  changed = True
177
  break
178
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
179
  if changed:
180
  try:
181
  ast.parse(content)
@@ -230,4 +309,9 @@ else:
230
  print("SKIP: routes/agent.py not found")
231
 
232
 
 
 
 
 
 
233
  print("\n=== All fix-approval-waiting patches applied ===")
 
3
  Patch: Fix 'waiting approval' after Space rebuild/session restore.
4
 
5
  Root cause:
6
+ When a Space restarts, sessions are restored from the HF dataset via:
7
+ - patch_catch_up_context_restore.py -> ensure_session_loaded() + seed_from_summary()
8
+ - patch_restore_from_dataset.py -> seed_from_dataset()
9
+
10
+ However, the auto_approval dict (set by patch_agent_backend.py in
11
+ Session.__init__) is NOT preserved in these restore paths. The frontend
12
+ checks auto_approval.enabled to auto-approve tool calls, and without it
13
+ the frontend shows 'waiting approval' and the accept/reject buttons
14
+ never appear (because the model never actually makes tool calls when
15
+ stuck in approval limbo).
16
 
17
  Fix:
18
+ 1. session_manager.py: In ensure_session_loaded(), set auto_approval
19
+ on restored session metadata.
20
+ 2. session_manager.py: In seed_from_summary() and seed_from_dataset(),
21
+ set auto_approval on the session.agent_session object.
22
+ 3. session.py: Add ensure_auto_approval() method to guarantee validity.
23
+ 4. routes/agent.py: Include auto_approval in SSE session update events.
24
 
25
  Runs during Docker build -- never fails the build.
26
  """
 
47
  return content, False
48
 
49
 
50
+ # --- Patch session_manager.py ---
51
  SESSION_MANAGER = "/app/backend/session_manager.py"
52
 
53
  if os.path.exists(SESSION_MANAGER):
 
67
  " logger.debug(\"Dataset restore fallback failed for %s: %s\", session_id, e)\n"
68
  "\n"
69
  " # === PATCH: fix-approval-waiting ===\n"
 
 
70
  " if loaded:\n"
71
  " if isinstance(loaded, dict):\n"
72
  " meta = loaded.setdefault('metadata', {})\n"
73
  " if isinstance(meta, dict):\n"
74
+ " meta.setdefault('auto_approval', {\n"
75
+ " 'enabled': True,\n"
76
+ " 'cost_cap_usd': None,\n"
77
+ " 'estimated_spend_usd': 0.0,\n"
78
+ " 'remaining_usd': None,\n"
79
+ " })\n"
80
  " if not loaded:\n"
81
  " return None\n"
82
  )
 
84
  if ok:
85
  print("+ ensure_session_loaded sets auto_approval on restored sessions")
86
  changed = True
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
87
 
88
+ # 2. Patch seed_from_summary to set auto_approval
89
+ old_seed_dataset = (
90
  " if seeded_from_dataset:\n"
91
  " self._touch(agent_session)\n"
92
  " await self.persist_session_snapshot(agent_session, runtime_state=\"idle\")\n"
93
  " return seeded_from_dataset\n"
94
  )
95
+ new_seed_dataset = (
96
  " if seeded_from_dataset:\n"
97
  " # === PATCH: fix-approval-waiting ===\n"
98
  " if not getattr(agent_session, 'auto_approval', None):\n"
 
106
  " await self.persist_session_snapshot(agent_session, runtime_state=\"idle\")\n"
107
  " return seeded_from_dataset\n"
108
  )
109
+ content, ok = _replace(content, old_seed_dataset, new_seed_dataset)
110
  if ok:
111
+ print("+ seed_from_summary sets auto_approval on session")
112
+ changed = True
113
+
114
+ # 3. Patch seed_from_dataset (from patch_restore_from_dataset.py)
115
+ old_seed_from_ds = (
116
+ " self._touch(agent_session)\n"
117
+ " await self.persist_session_snapshot(agent_session, runtime_state=\"idle\")\n"
118
+ " try:\n"
119
+ " await self.record_catchup_done(session_id)\n"
120
+ " except Exception:\n"
121
+ " pass\n"
122
+ " return seeded\n"
123
+ )
124
+ new_seed_from_ds = (
125
+ " self._touch(agent_session)\n"
126
+ " await self.persist_session_snapshot(agent_session, runtime_state=\"idle\")\n"
127
+ " try:\n"
128
+ " await self.record_catchup_done(session_id)\n"
129
+ " except Exception:\n"
130
+ " pass\n"
131
+ " # === PATCH: fix-approval-waiting ===\n"
132
+ " if not getattr(agent_session, 'auto_approval', None):\n"
133
+ " agent_session.auto_approval = {\n"
134
+ " 'enabled': True,\n"
135
+ " 'cost_cap_usd': None,\n"
136
+ " 'estimated_spend_usd': 0.0,\n"
137
+ " 'remaining_usd': None,\n"
138
+ " }\n"
139
+ " return seeded\n"
140
+ )
141
+ content, ok = _replace(content, old_seed_from_ds, new_seed_from_ds)
142
+ if ok:
143
+ print("+ seed_from_dataset sets auto_approval on session")
144
+ changed = True
145
+
146
+ # 4. Also patch the fallback in seed_from_summary
147
+ old_fallback = (
148
+ " for msg in parsed:\n"
149
+ " session.context_manager.items.append(msg)\n"
150
+ " self._touch(agent_session)\n"
151
+ " await self.persist_session_snapshot(agent_session, runtime_state=\"idle\")\n"
152
+ " return len(parsed)\n"
153
+ )
154
+ new_fallback = (
155
+ " for msg in parsed:\n"
156
+ " session.context_manager.items.append(msg)\n"
157
+ " # === PATCH: fix-approval-waiting ===\n"
158
+ " if not getattr(agent_session, 'auto_approval', None):\n"
159
+ " agent_session.auto_approval = {\n"
160
+ " 'enabled': True,\n"
161
+ " 'cost_cap_usd': None,\n"
162
+ " 'estimated_spend_usd': 0.0,\n"
163
+ " 'remaining_usd': None,\n"
164
+ " }\n"
165
+ " self._touch(agent_session)\n"
166
+ " await self.persist_session_snapshot(agent_session, runtime_state=\"idle\")\n"
167
+ " return len(parsed)\n"
168
+ )
169
+ content, ok = _replace(content, old_fallback, new_fallback)
170
+ if ok:
171
+ print("+ seed_from_summary fallback sets auto_approval on session")
172
  changed = True
173
 
174
  if changed:
 
223
  " self.auto_approval.setdefault('enabled', True)\n"
224
  " elif isinstance(self.auto_approval, bool):\n"
225
  " self.auto_approval = {'enabled': bool(self.auto_approval),'cost_cap_usd': None,'estimated_spend_usd': 0.0,'remaining_usd': None}\n"
226
+ " # === PATCH: fix-approval-waiting ===\n"
227
  )
228
  content = content[:best_idx] + helper + content[best_idx:]
229
  print("+ session.py: added ensure_auto_approval() method")
230
  changed = True
231
  break
232
 
233
+ # Add marker after auto_approval init
234
+ old_auto = (
235
+ " # === PATCH: Auto-approve for free models ===\n"
236
+ " self.auto_approval = {\n"
237
+ ' "enabled": True,\n'
238
+ ' "cost_cap_usd": None,\n'
239
+ ' "estimated_spend_usd": 0.0,\n'
240
+ ' "remaining_usd": None,\n'
241
+ " }\n"
242
+ )
243
+ new_auto = (
244
+ " # === PATCH: Auto-approve for free models ===\n"
245
+ " self.auto_approval = {\n"
246
+ ' "enabled": True,\n'
247
+ ' "cost_cap_usd": None,\n'
248
+ ' "estimated_spend_usd": 0.0,\n'
249
+ ' "remaining_usd": None,\n'
250
+ " }\n"
251
+ " # === PATCH: fix-approval-waiting ===\n"
252
+ )
253
+ content, ok = _replace(content, old_auto, new_auto)
254
+ if ok:
255
+ print("+ session.py: added fix-approval marker")
256
+ changed = True
257
+
258
  if changed:
259
  try:
260
  ast.parse(content)
 
309
  print("SKIP: routes/agent.py not found")
310
 
311
 
312
+ # --- Patch Dockerfile: move patch_fix_approval_waiting.py AFTER restore patches ---
313
+ DOCKERFILE = "/Dockerfile"
314
+ # Note: This won't exist in the build container, but we need to check if the
315
+ # Dockerfile in the Space repo has the correct ordering
316
+
317
  print("\n=== All fix-approval-waiting patches applied ===")