@@ -180,6 +180,56 @@ async def get_memory_context(
180180 Returns:
181181 GraphContext with the entity and its related context
182182 """
183+ deferred_http_error : tuple [int , str , BaseException ] | None = None
184+ result : GraphContext | None = None
185+
186+ async def _build_context () -> GraphContext | None :
187+ nonlocal deferred_http_error
188+ since = parse_timeframe (timeframe ) if timeframe else None
189+ limit = page_size
190+ offset = (page - 1 ) * page_size
191+
192+ with logfire .span (
193+ "api.memory.build_context.build_context" ,
194+ domain = "memory" ,
195+ action = "build_context" ,
196+ phase = "build_context" ,
197+ page = page ,
198+ page_size = page_size ,
199+ ):
200+ try :
201+ context = await context_service .build_context (
202+ memory_url ,
203+ depth = depth ,
204+ since = since ,
205+ limit = limit ,
206+ offset = offset ,
207+ max_related = max_related ,
208+ include_observations = include_observations ,
209+ )
210+ except AmbiguousIdentifierError as exc :
211+ # A title URL that names several notes has no single context to build.
212+ # Defer the caller-visible 409 past this span so Logfire does not
213+ # record the locator content as an exception.
214+ deferred_http_error = (status .HTTP_409_CONFLICT , str (exc ), exc )
215+ return None
216+
217+ with logfire .span (
218+ "api.memory.build_context.shape_response" ,
219+ domain = "memory" ,
220+ action = "build_context" ,
221+ phase = "shape_response" ,
222+ result_count = len (context .results ),
223+ ):
224+ async with db .scoped_session (session_maker ) as session :
225+ return await to_graph_context (
226+ context ,
227+ entity_repository = entity_repository ,
228+ session = session ,
229+ page = page ,
230+ page_size = page_size ,
231+ )
232+
183233 with logfire .span (
184234 "api.request.memory.build_context" ,
185235 entrypoint = "api" ,
@@ -200,57 +250,12 @@ async def get_memory_context(
200250 # Why: normalize_memory_url raises ValueError for caller input; letting
201251 # it escape reported a client mistake as an internal server error (#1879).
202252 # Outcome: a bounded 400 with the normalizer's specific reason before
203- # any context traversal.
204- raise HTTPException (
205- status_code = status .HTTP_400_BAD_REQUEST ,
206- detail = str (exc ),
207- ) from exc
208-
209- since = parse_timeframe (timeframe ) if timeframe else None
210- limit = page_size
211- offset = (page - 1 ) * page_size
212-
213- with logfire .span (
214- "api.memory.build_context.build_context" ,
215- domain = "memory" ,
216- action = "build_context" ,
217- phase = "build_context" ,
218- page = page ,
219- page_size = page_size ,
220- ):
221- try :
222- context = await context_service .build_context (
223- memory_url ,
224- depth = depth ,
225- since = since ,
226- limit = limit ,
227- offset = offset ,
228- max_related = max_related ,
229- include_observations = include_observations ,
230- )
231- except AmbiguousIdentifierError as exc :
232- # A title URL that names several notes has no single context to build.
233- raise HTTPException (
234- status_code = status .HTTP_409_CONFLICT , detail = str (exc )
235- ) from exc
236- with logfire .span (
237- "api.memory.build_context.shape_response" ,
238- domain = "memory" ,
239- action = "build_context" ,
240- phase = "shape_response" ,
241- result_count = len (context .results ),
242- ):
243- async with db .scoped_session (session_maker ) as session :
244- result = await to_graph_context (
245- context ,
246- entity_repository = entity_repository ,
247- session = session ,
248- page = page ,
249- page_size = page_size ,
250- )
253+ # any context traversal or span exception.
254+ deferred_http_error = (status .HTTP_400_BAD_REQUEST , str (exc ), exc )
255+ else :
256+ result = await _build_context ()
251257 except HTTPException as exc :
252- # Malformed input (400) and ambiguous titles (409) are both caller-visible
253- # outcomes; only a server-side status would be a failure.
258+ # Unexpected HTTP errors keep the original in-span outcome handling.
254259 span .set_attribute (
255260 "outcome" ,
256261 "input_rejected" if exc .status_code < 500 else "failed" ,
@@ -264,5 +269,13 @@ async def get_memory_context(
264269 span .set_attribute ("outcome" , "failed" )
265270 raise
266271
267- span .set_attribute ("outcome" , "success" )
268- return result
272+ span .set_attribute (
273+ "outcome" ,
274+ "input_rejected" if deferred_http_error is not None else "success" ,
275+ )
276+
277+ if deferred_http_error is not None :
278+ status_code , detail , cause = deferred_http_error
279+ raise HTTPException (status_code = status_code , detail = detail ) from cause
280+ assert result is not None
281+ return result
0 commit comments