Skip to content

Commit 8ae1de7

Browse files
author
jobell
committed
fix: address review feedback on session resilience PR
- Revert events=events to events=[] to fix event duplication regression (Session constructor pre-populates; append_event loop would double-append) - Restore all 8 deleted get_session tests including duplication regression guards - Preserve source field in _recreate_session (alongside session_name) - Update state-loss warning to only fire for truly unknown fields - Wrap _recreate_session failure with RuntimeError preserving 404 context - Add stream timeout (10min) to resubscribe loop matching sendA2AMessage behavior - Fix setChatStatus in finally to be conditional (preserve input_required/error) - Change resubscribeTask return type from any to unknown - Narrow pendingTask.state type to 'working' | 'submitted' union; removes as TaskState cast - Add tests: retry-also-404 (no infinite recursion), recreation-fails RuntimeError, source field preservation, state-loss warning, known-fields-only no-warning Signed-off-by: jobell <jobell@ancestry.com>
1 parent 781d07b commit 8ae1de7

File tree

5 files changed

+346
-99
lines changed

5 files changed

+346
-99
lines changed

python/packages/kagent-adk/src/kagent/adk/_session_service.py

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ async def get_session(
114114
session = Session(
115115
id=session_data["id"],
116116
user_id=session_data["user_id"],
117-
events=events,
117+
events=[],
118118
app_name=app_name,
119119
state={},
120120
)
@@ -168,35 +168,40 @@ async def _recreate_session(self, session: Session) -> None:
168168
- user_id: Preserved
169169
- agent_ref: Preserved
170170
- session_name: Preserved (from session.state["session_name"])
171+
- source: Preserved (from session.state["source"])
171172
- Other session.state fields: NOT preserved (lost on recreation)
172173
173-
Note: Only session_name is currently used by the application.
174174
If additional state fields are added in the future, they must be
175-
explicitly preserved here.
175+
explicitly preserved here and added to _PRESERVED_STATE_FIELDS.
176176
177177
Args:
178178
session: The session object to recreate
179179
180180
Raises:
181181
httpx.HTTPStatusError: If recreation fails
182182
"""
183+
_PRESERVED_STATE_FIELDS = {"session_name", "source"}
184+
183185
request_data = {
184186
"id": session.id,
185187
"user_id": session.user_id,
186188
"agent_ref": session.app_name,
187189
}
188190
if session.state and session.state.get("session_name"):
189191
request_data["name"] = session.state["session_name"]
190-
191-
# Warn if session has additional state fields that won't be preserved
192-
if session.state and len(session.state) > 1:
193-
extra_fields = [k for k in session.state.keys() if k != "session_name"]
194-
logger.warning(
195-
"Session %s has additional state fields that will not be preserved during recreation: %s. "
196-
"Update _recreate_session() if these fields are critical.",
197-
session.id,
198-
extra_fields,
199-
)
192+
if session.state and session.state.get("source"):
193+
request_data["source"] = session.state["source"]
194+
195+
# Warn if session has unknown state fields that won't be preserved
196+
if session.state:
197+
extra_fields = [k for k in session.state.keys() if k not in _PRESERVED_STATE_FIELDS]
198+
if extra_fields:
199+
logger.warning(
200+
"Session %s has additional state fields that will not be preserved during recreation: %s. "
201+
"Update _recreate_session() if these fields are critical.",
202+
session.id,
203+
extra_fields,
204+
)
200205

201206
response = await self.client.post(
202207
"/api/sessions",
@@ -269,7 +274,12 @@ async def append_event(self, session: Session, event: Event) -> Event:
269274
"Session %s not found (404), attempting to recreate before retry",
270275
session.id,
271276
)
272-
await self._recreate_session(session)
277+
try:
278+
await self._recreate_session(session)
279+
except Exception as e:
280+
raise RuntimeError(
281+
f"Session {session.id} not found (404) and recreation failed"
282+
) from e
273283

274284
# Retry the append ONCE. If this retry also fails (including another 404),
275285
# raise_for_status() below will propagate the error without further attempts.

0 commit comments

Comments
 (0)