feat(retrieval): a task's work logs join the document it is embedded as (#4251)
CI & Build / Python lint (push) Successful in 3s
CI & Build / Plugin hooks (push) Successful in 16s
CI & Build / TypeScript typecheck (push) Successful in 53s
CI & Build / integration (push) Successful in 59s
CI & Build / Python tests (push) Successful in 1m38s
CI & Build / Build & push image (push) Successful in 27s

Step 1 of #4251. A work log is the richest prose Scribe holds about WHY
something is the way it is — written during the work, recording what was tried
and ruled out. #4241 made it readable from the agent's door. It was still not
findable, so "has anyone tried this approach?" — precisely the question a log
answers — could not reach one. The cost is not hypothetical: #4208 was rebuilt
in this session because its logs were unreachable.

THE DESIGN QUESTION the issue left open was whether logs embed as part of their
task's document or as rows of their own. As their own rows, a hit has to be
resolved back to a task to be worth anything, and it needs a fourth search, a
fourth result shape and a fourth arm. As part of the task, the objection is
that a long log drowns a short title.

That objection was true before #280 and is not true now. Chunking made one
record into one vector per section, so each log becomes its own title-anchored
chunk, scored separately, and the task's own prose keeps the chunk it always
had — a task is as findable as its best-matching log rather than as the average
of everything in it. The other half is this session's other build: a search
hands back the chunk that won (#4243), so a hit earned by a log shows that
log's passage under the task's title. Without that a reader would have got
body[:240] of the task — the opening of a record whose relevance lives three
hundred lines further down.

So `task_document(title, body, logs)` sits beside `rule_document`: a
synthesised embed-time shape, because the stored record is the task row and the
logs live in their own table, so the document that should be searchable exists
nowhere until it is built. A task with no logs is returned untouched — most
notes are not tasks and most tasks carry no log, and their vectors are the
corpus every tuned number here was measured against.

CHUNKER_VERSION 1 → 2, and its comment now says what the version actually
means. It used to read "whenever chunk_document's output can change for the
same input", which this change would slip past: `chunk_document` is untouched
and every task with a log now embeds differently while its title, body and the
chunker all stand still. The invariant is the document a record is embedded as.
The startup backfill re-embeds on that.

Create, edit and delete of a log all refresh the task through `embed_note`, the
one path every writer shares — an edited log whose vectors still carry its old
wording keeps matching what it no longer says.

No new kind enters the auto-inject menu: tasks were always in it, and this
makes recall on them better rather than changing what the menu spans. The
calibration stamp will now report shape_version 2 against numbers measured at
1, which is exactly the report #4104 built it to make.

Also promoted `session_returning` into tests/helpers beside `make_mock_session`
(#2834) — two files had spelled it out identically and a third was about to.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01821k5B3Ysecp9fNYs92Kuy
This commit is contained in:
2026-09-21 11:20:27 -04:00
co-authored by Claude Opus 5
parent 5fb41af9b0
commit aa95c109ea
7 changed files with 439 additions and 31 deletions
+34
View File
@@ -14,6 +14,36 @@ logger = logging.getLogger(__name__)
_UNSET = object()
async def _refresh_task_document(session, task_id: int) -> None:
"""Re-embed the task whose work log just changed (#4251).
A task's embedded document carries its logs, so a log written and not
indexed is #4241's half-surface wearing different clothes: the entry is
readable and unfindable, and the next session rebuilds what this one ruled
out. Create, edit and delete all go through here — an edited log that keeps
matching its old wording is the stale-vector problem `upsert_note_embedding`
already refuses to leave behind for a body.
Loads the NOTE rather than synthesising one. `embed_note` reads
`title`, `body` and the OWNER's `user_id` off what it is handed, so a
stand-in row would index the logs under an empty title — throwing away the
per-chunk topical anchor that makes any of this discriminative — and file
the vectors under the wrong user.
Failure is swallowed the way `embed_note`'s own is: a log that saved must
not fail on its index refresh. The startup backfill is the backstop.
"""
from scribe.services.notes import embed_note
try:
result = await session.execute(select(Note).where(Note.id == task_id))
note = result.scalars().first()
if note is not None:
embed_note(note)
except Exception: # noqa: BLE001 - indexing never breaks a write
logger.exception("embedding refresh failed for task %s", task_id)
async def create_log(
user_id: int,
task_id: int,
@@ -36,6 +66,7 @@ async def create_log(
session.add(log)
await session.commit()
await session.refresh(log)
await _refresh_task_document(session, task_id)
return log
@@ -144,6 +175,7 @@ async def update_log(
log.updated_at = datetime.now(timezone.utc)
await session.commit()
await session.refresh(log)
await _refresh_task_document(session, log.task_id)
return log
@@ -155,6 +187,8 @@ async def delete_log(user_id: int, log_id: int) -> bool:
log = result.scalars().first()
if log is None:
return False
task_id = log.task_id
await session.delete(log)
await session.commit()
await _refresh_task_document(session, task_id)
return True