@@ -212,6 +212,47 @@ async def test_build_context_with_observations(context_service, test_graph):
212212 assert "Root note" in note_observation .content
213213
214214
215+ @pytest .mark .asyncio
216+ async def test_build_context_observation_permalinks_match_search_index (
217+ context_service , search_service , entity_service
218+ ):
219+ """Regression test for #929: observation permalinks must match the search index.
220+
221+ build_context used to rebuild the synthetic observation permalink inline,
222+ without the 200-char truncation (#446) or the content digest (#931) that
223+ Observation.permalink applies, so for long observations it returned
224+ permalinks the search index doesn't contain.
225+ """
226+ from basic_memory .schemas .base import Entity as EntitySchema
227+ from basic_memory .schemas .search import SearchQuery
228+
229+ long_observation = "x" * 210 + " LONG_OBS_MARKER"
230+ entity , _ = await entity_service .create_or_update_entity (
231+ EntitySchema (
232+ title = "Long Obs Entity" ,
233+ note_type = "test" ,
234+ directory = "test" ,
235+ content = f"# Long Obs Entity\n - [note] { long_observation } \n " ,
236+ )
237+ )
238+ await search_service .index_entity (entity )
239+
240+ url = memory_url .validate_strings (f"memory://{ entity .permalink } " )
241+ context_result = await context_service .build_context (url , include_observations = True )
242+ assert len (context_result .results ) == 1
243+ context_item = context_result .results [0 ]
244+ assert len (context_item .observations ) == 1
245+ obs_row = context_item .observations [0 ]
246+
247+ # The model property is the single definition of the permalink format
248+ assert obs_row .permalink == entity .observations [0 ].permalink
249+
250+ # The search index row for this observation carries the same permalink
251+ index_rows = await search_service .search (SearchQuery (text = "LONG_OBS_MARKER" ))
252+ obs_permalinks = [r .permalink for r in index_rows if r .type == SearchItemType .OBSERVATION .value ]
253+ assert obs_permalinks == [obs_row .permalink ]
254+
255+
215256@pytest .mark .asyncio
216257async def test_build_context_not_found (context_service ):
217258 """Test handling non-existent permalinks."""
0 commit comments