Skip to content

Commit 6b90a3d

Browse files
committed
Python: Simplify background task retention without generation tracking (#8760)
1 parent 6a12a17 commit 6b90a3d

2 files changed

Lines changed: 110 additions & 186 deletions

File tree

‎python/packages/core/agent_framework/_harness/_background_agents.py‎

Lines changed: 24 additions & 135 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,7 @@ class BackgroundTaskInfo(SerializationMixin):
6565
status: BackgroundTaskStatus
6666
result_text: str | None
6767
error_text: str | None
68-
generation: int
69-
__slots__ = ("agent_name", "description", "error_text", "generation", "id", "result_text", "status")
68+
__slots__ = ("agent_name", "description", "error_text", "id", "result_text", "status")
7069

7170
def __init__(
7271
self,
@@ -76,7 +75,6 @@ def __init__(
7675
status: BackgroundTaskStatus = BackgroundTaskStatus.RUNNING,
7776
result_text: str | None = None,
7877
error_text: str | None = None,
79-
generation: int = 1,
8078
) -> None:
8179
"""Initialize a background task info entry."""
8280
self.id = id
@@ -85,7 +83,6 @@ def __init__(
8583
self.status = status
8684
self.result_text = result_text
8785
self.error_text = error_text
88-
self.generation = generation
8986

9087
def to_dict(self, *, exclude: set[str] | None = None, exclude_none: bool = True) -> dict[str, Any]:
9188
"""Serialize for session state persistence."""
@@ -95,7 +92,6 @@ def to_dict(self, *, exclude: set[str] | None = None, exclude_none: bool = True)
9592
"agent_name": self.agent_name,
9693
"description": self.description,
9794
"status": self.status.value,
98-
"generation": self.generation,
9995
}
10096
if not exclude_none or self.result_text is not None:
10197
data["result_text"] = self.result_text
@@ -113,125 +109,24 @@ def from_dict(cls, data: MutableMapping[str, Any], **kwargs: Any) -> BackgroundT
113109
status=BackgroundTaskStatus(data["status"]),
114110
result_text=data.get("result_text"),
115111
error_text=data.get("error_text"),
116-
generation=data.get("generation", 1),
117112
)
118113

119114

120-
_TaskKey = tuple[int, int] | int
121-
122-
123-
class _InFlightTasks(dict[_TaskKey, asyncio.Task[AgentResponse[Any]]]):
124-
"""Non-serializable container for tracked in-flight tasks.
125-
126-
Supports dual keying:
127-
- Composite key ``(task_id, generation)``: isolates discrete continuation
128-
executions under the same reusable task ID so older session snapshots
129-
reconcile with their own generation rather than a later continuation.
130-
- Integer key ``task_id``: direct fallback lookup and backwards-compatible
131-
access for callers and existing unit tests.
132-
"""
133-
134-
def __getitem__(self, key: _TaskKey) -> asyncio.Task[AgentResponse[Any]]:
135-
if super().__contains__(key):
136-
return super().__getitem__(key)
137-
if isinstance(key, int):
138-
matches: list[tuple[int, asyncio.Task[AgentResponse[Any]]]] = []
139-
for k, v in self.items():
140-
if isinstance(k, tuple) and k[0] == key:
141-
matches.append((k[1], v))
142-
if matches:
143-
matches.sort(key=lambda item: item[0], reverse=True)
144-
return matches[0][1]
145-
raise KeyError(key)
146-
147-
def get(self, key: _TaskKey, default: Any = None, /) -> Any:
148-
try:
149-
return self[key]
150-
except KeyError:
151-
return default
152-
153-
def get_exact(self, key: _TaskKey) -> asyncio.Task[AgentResponse[Any]] | None:
154-
"""Retrieve task for an exact key without composite fallback."""
155-
if super().__contains__(key):
156-
return super().__getitem__(key)
157-
return None
158-
159-
def __contains__(self, key: object) -> bool:
160-
if super().__contains__(key):
161-
return True
162-
if isinstance(key, int):
163-
for k in self:
164-
if isinstance(k, tuple) and k[0] == key:
165-
return True
166-
return False
167-
168-
def pop(self, key: _TaskKey, *args: Any) -> Any:
169-
if super().__contains__(key):
170-
val = super().pop(key)
171-
if isinstance(key, int):
172-
composites: list[tuple[int, int]] = [
173-
k for k in list(self.keys()) if isinstance(k, tuple) and k[0] == key
174-
]
175-
for ck in composites:
176-
super().pop(ck, None)
177-
return val
178-
if isinstance(key, int):
179-
composites_to_pop: list[tuple[int, int]] = [
180-
k for k in list(self.keys()) if isinstance(k, tuple) and k[0] == key
181-
]
182-
if composites_to_pop:
183-
composites_to_pop.sort(key=lambda k: k[1])
184-
popped_val: asyncio.Task[AgentResponse[Any]] | None = None
185-
for ck in composites_to_pop:
186-
popped_val = super().pop(ck)
187-
return popped_val
188-
if args:
189-
return args[0]
190-
raise KeyError(key)
191-
192-
193115
@dataclass
194116
class _RuntimeState:
195117
"""Non-serializable per-session runtime state for background tasks."""
196118

197-
in_flight_tasks: _InFlightTasks = field(default_factory=_InFlightTasks)
119+
in_flight_tasks: dict[int, asyncio.Task[AgentResponse[Any]]] = field(default_factory=lambda: {})
198120
background_sessions: dict[int, AgentSession] = field(default_factory=lambda: {})
199121
closed: bool = False
200122

201-
def track_task(
202-
self,
203-
task_id: int,
204-
task: asyncio.Task[AgentResponse[Any]],
205-
*,
206-
generation: int = 1,
207-
) -> None:
123+
def track_task(self, task_id: int, task: asyncio.Task[AgentResponse[Any]]) -> None:
208124
"""Track a background task if this runtime is still open."""
209125
if self.closed:
210126
task.cancel()
211127
raise RuntimeError("Session runtime is closed; cannot start background task.")
212128

213-
self.in_flight_tasks[task_id, generation] = task
214-
215-
def get_task(
216-
self,
217-
task_id: int,
218-
generation: int | None = None,
219-
) -> asyncio.Task[AgentResponse[Any]] | None:
220-
"""Retrieve a task for a given task ID and optional generation."""
221-
if generation is not None:
222-
task = self.in_flight_tasks.get_exact((task_id, generation))
223-
if task is not None:
224-
return task
225-
for k in self.in_flight_tasks:
226-
if isinstance(k, tuple) and k[0] == task_id:
227-
return None
228-
return self.in_flight_tasks.get_exact(task_id)
229-
return self.in_flight_tasks.get(task_id)
230-
231-
def clear_task(self, task_id: int) -> None:
232-
"""Remove all executions and background sessions associated with a task ID."""
233-
self.in_flight_tasks.pop(task_id, None)
234-
self.background_sessions.pop(task_id, None)
129+
self.in_flight_tasks[task_id] = task
235130

236131

237132
# ---------------------------------------------------------------------------
@@ -332,12 +227,8 @@ def _finalize_task(
332227
call to :func:`_refresh_task_state` a missing entry would be misidentified
333228
as ``LOST``, discarding a result that is already available.
334229
335-
Furthermore, each execution records a monotonic ``generation`` in both
336-
``task_info`` and ``runtime.in_flight_tasks[(task_id, generation)]`` so
337-
that restoring an earlier snapshot of a continued task will reconcile
338-
with that snapshot's execution rather than a later continuation.
339-
340230
Tasks are removed from ``in_flight_tasks`` by two callers:
231+
341232
- :func:`background_agents_clear_completed_task` — explicit caller-driven cleanup.
342233
- :meth:`BackgroundAgentsProvider.release_session` — bulk cleanup on session teardown.
343234
"""
@@ -352,8 +243,6 @@ def _finalize_task(
352243
else:
353244
task_info.status = BackgroundTaskStatus.COMPLETED
354245
task_info.result_text = completed_task.result().text
355-
# NOTE: do NOT pop task from runtime.in_flight_tasks here.
356-
# See docstring above for the reasoning.
357246

358247

359248
def _refresh_task_state(
@@ -367,7 +256,7 @@ def _refresh_task_state(
367256
if task_info.status != BackgroundTaskStatus.RUNNING:
368257
continue
369258

370-
in_flight = runtime.get_task(task_info.id, task_info.generation)
259+
in_flight = runtime.in_flight_tasks.get(task_info.id)
371260
if in_flight is None:
372261
task_info.status = BackgroundTaskStatus.LOST
373262
changed = True
@@ -613,23 +502,21 @@ def background_agents_start_task(agent_name: str, input: str, description: str)
613502

614503
sub_session = bg_agent.create_session()
615504

616-
task_info = BackgroundTaskInfo(
617-
id=task_id,
618-
agent_name=agent_name,
619-
description=description,
620-
generation=1,
621-
)
622-
623505
async_task = asyncio.create_task(_run_agent(bg_agent.run(input, session=sub_session)))
624506
try:
625-
runtime.track_task(task_id, async_task, generation=task_info.generation)
507+
runtime.track_task(task_id, async_task)
626508
except RuntimeError as exc:
627509
return f"Error: {exc}"
628510

629511
runtime.background_sessions[task_id] = sub_session
630512

631513
provider_state["next_task_id"] = task_id + 1
632514

515+
task_info = BackgroundTaskInfo(
516+
id=task_id,
517+
agent_name=agent_name,
518+
description=description,
519+
)
633520
tasks = _get_tasks(provider_state)
634521
tasks.append(task_info)
635522
_save_tasks(provider_state, tasks)
@@ -655,12 +542,12 @@ async def background_agents_wait_for_first_completion(task_ids: list[int]) -> st
655542
# Collect in-flight tasks matching the requested IDs.
656543
waitable: list[tuple[int, asyncio.Task[AgentResponse[Any]]]] = []
657544
tasks = _get_tasks(provider_state)
545+
running_ids = {t.id for t in tasks if t.status == BackgroundTaskStatus.RUNNING}
658546
for tid in task_ids:
659-
t_info = next((t for t in tasks if t.id == tid and t.status == BackgroundTaskStatus.RUNNING), None)
660-
gen = t_info.generation if t_info is not None else None
661-
in_flight = runtime.get_task(tid, gen)
662-
if in_flight is not None:
663-
waitable.append((tid, in_flight))
547+
if tid in running_ids:
548+
in_flight = runtime.in_flight_tasks.get(tid)
549+
if in_flight is not None:
550+
waitable.append((tid, in_flight))
664551

665552
if not waitable:
666553
# Refresh state to catch any that completed.
@@ -697,7 +584,7 @@ async def background_agents_wait_for_first_completion(task_ids: list[int]) -> st
697584
tasks = _get_tasks(provider_state)
698585
task_info = next((t for t in tasks if t.id == completed_id), None)
699586
if task_info is not None and completed_id is not None:
700-
completed_task = runtime.get_task(completed_id, task_info.generation)
587+
completed_task = runtime.in_flight_tasks.get(completed_id)
701588
if completed_task is not None:
702589
_finalize_task(task_info, completed_task, runtime)
703590
_save_tasks(provider_state, tasks)
@@ -768,10 +655,9 @@ def background_agents_continue_task(task_id: int, text: str) -> str:
768655

769656
bg_agent = self._agents[key]
770657

771-
task_info.generation += 1
772658
async_task = asyncio.create_task(_run_agent(bg_agent.run(text, session=sub_session)))
773659
try:
774-
runtime.track_task(task_id, async_task, generation=task_info.generation)
660+
runtime.track_task(task_id, async_task)
775661
except RuntimeError as exc:
776662
return f"Error: {exc}"
777663
task_info.status = BackgroundTaskStatus.RUNNING
@@ -803,8 +689,11 @@ def background_agents_clear_completed_task(task_id: int) -> str:
803689
tasks = [t for t in tasks if t.id != task_id]
804690
_save_tasks(provider_state, tasks)
805691

806-
# Clean up runtime references.
807-
runtime.clear_task(task_id)
692+
# Clean up runtime references only if the local task is done or absent.
693+
in_flight = runtime.in_flight_tasks.get(task_id)
694+
if in_flight is None or in_flight.done():
695+
runtime.in_flight_tasks.pop(task_id, None)
696+
runtime.background_sessions.pop(task_id, None)
808697

809698
_save_provider_state(session, provider_state, source_id=source_id)
810699
return f"Task {task_id} cleared."

0 commit comments

Comments
 (0)