perf(core): reuse written note content after writes (#717)

Signed-off-by: phernandez <paul@basicmachines.co>
This commit is contained in:
Paul Hernandez
2026-04-04 00:16:22 -05:00
committed by GitHub
parent 6f207c20c0
commit 69808b23ca
8 changed files with 250 additions and 56 deletions
@@ -306,8 +306,13 @@ async def create_entity(
):
if fast:
entity = await entity_service.fast_write_entity(data)
written_content = None
search_content = None
else:
entity = await entity_service.create_entity(data)
write_result = await entity_service.create_entity_with_content(data)
entity = write_result.entity
written_content = write_result.content
search_content = write_result.search_content
if fast:
with telemetry.scope(
@@ -329,7 +334,7 @@ async def create_entity(
action="create_entity",
phase="search_index",
):
await search_service.index_entity(entity)
await search_service.index_entity(entity, content=search_content)
with telemetry.scope(
"api.knowledge.create_entity.vector_sync",
domain="knowledge",
@@ -352,8 +357,15 @@ async def create_entity(
domain="knowledge",
action="create_entity",
phase="read_content",
source="file" if fast else "memory",
):
content = await file_service.read_file_content(entity.file_path)
if fast:
content = await file_service.read_file_content(entity.file_path)
else:
# Non-fast writes already captured the markdown in memory. Reuse it here
# instead of re-reading the file; format_on_save is the one config that can
# still make the persisted file diverge because write_file only returns a checksum.
content = written_content
result = result.model_copy(update={"content": content})
logger.info(
@@ -421,18 +433,28 @@ async def update_entity_by_id(
):
if fast:
entity = await entity_service.fast_write_entity(data, external_id=entity_id)
written_content = None
search_content = None
response.status_code = 200 if existing else 201
else:
if existing:
entity = await entity_service.update_entity(existing, data)
write_result = await entity_service.update_entity_with_content(existing, data)
entity = write_result.entity
written_content = write_result.content
search_content = write_result.search_content
response.status_code = 200
else:
entity = await entity_service.create_entity(data)
write_result = await entity_service.create_entity_with_content(data)
entity = write_result.entity
written_content = write_result.content
search_content = write_result.search_content
if entity.external_id != entity_id:
entity = await entity_repository.update(
entity.id,
{"external_id": entity_id},
)
# external_id fixup only changes the DB row. The file content is unchanged,
# so the markdown captured during the write remains valid downstream.
if not entity:
raise HTTPException(
status_code=404,
@@ -461,7 +483,7 @@ async def update_entity_by_id(
action="update_entity",
phase="search_index",
):
await search_service.index_entity(entity)
await search_service.index_entity(entity, content=search_content)
with telemetry.scope(
"api.knowledge.update_entity.vector_sync",
domain="knowledge",
@@ -484,8 +506,15 @@ async def update_entity_by_id(
domain="knowledge",
action="update_entity",
phase="read_content",
source="file" if fast else "memory",
):
content = await file_service.read_file_content(entity.file_path)
if fast:
content = await file_service.read_file_content(entity.file_path)
else:
# Non-fast writes already captured the markdown in memory. Reuse it here
# instead of re-reading the file; format_on_save is the one config that can
# still make the persisted file diverge because write_file only returns a checksum.
content = written_content
result = result.model_copy(update={"content": content})
logger.info(
@@ -563,9 +592,11 @@ async def edit_entity_by_id(
find_text=data.find_text,
expected_replacements=data.expected_replacements,
)
written_content = None
search_content = None
else:
identifier = entity.permalink or entity.file_path
updated_entity = await entity_service.edit_entity(
write_result = await entity_service.edit_entity_with_content(
identifier=identifier,
operation=data.operation,
content=data.content,
@@ -573,6 +604,9 @@ async def edit_entity_by_id(
find_text=data.find_text,
expected_replacements=data.expected_replacements,
)
updated_entity = write_result.entity
written_content = write_result.content
search_content = write_result.search_content
if fast:
with telemetry.scope(
@@ -594,7 +628,7 @@ async def edit_entity_by_id(
action="edit_entity",
phase="search_index",
):
await search_service.index_entity(updated_entity)
await search_service.index_entity(updated_entity, content=search_content)
with telemetry.scope(
"api.knowledge.edit_entity.vector_sync",
domain="knowledge",
@@ -617,8 +651,15 @@ async def edit_entity_by_id(
domain="knowledge",
action="edit_entity",
phase="read_content",
source="file" if fast else "memory",
):
content = await file_service.read_file_content(updated_entity.file_path)
if fast:
content = await file_service.read_file_content(updated_entity.file_path)
else:
# Non-fast writes already captured the markdown in memory. Reuse it here
# instead of re-reading the file; format_on_save is the one config that can
# still make the persisted file diverge because write_file only returns a checksum.
content = written_content
result = result.model_copy(update={"content": content})
logger.info(