Skip to content

refactor(conversations): relevance follow-ups — trigger-derived mode, stored-flag visibility, backfill, desktop rules, neighbor context - #17089

Merged
Git-on-my-level merged 6 commits into
mainfrom
fix/relevance-followups
Sep 23, 2026
Merged

Git-on-my-level merged 6 commits into
mainfrom
fix/relevance-followups

Conversation

@Git-on-my-level

@Git-on-my-level Git-on-my-level commented Sep 23, 2026 •

Copy link
Copy Markdown
Member

Why

Follow-ups to #16587 (one relevance decision for every processed conversation), closing the five items it deferred.

What

1. Processing mode comes from the trigger alone (utils/conversations/processing_trigger.py)
force_process, is_reprocess and bypass_jit_first_open were independent booleans every caller had to combine correctly — "run now" silently implying "skip discard" was the bug behind #16587. One row per ProcessingTrigger now fixes run-now, reprocess, JIT bypass and relevance policy together; process_conversation takes only the trigger. Every existing call site's flags equal its trigger's row, so behaviour is unchanged (the free-tier entrypoint matrix now derives its expectations from the same table). Finalization jobs persist processing_trigger; the legacy force_process bit is still mirrored for a rollback and read as a fallback for jobs in flight.

2. Desktop captures that skip enrichment are assessed too
Free-tier desktop stores (legacy deferral, deterministic minimum, projection) never reached the relevance step. The deterministic rules cost nothing, so those stores now run them with the model withheld: rule discards hide the capture (recoverable); what the rules cannot settle is kept with reason model_withheld. Any wake-word match skips the rules.

3. The stored discarded flag is the only visibility authority
Legacy sync review rows were hidden by a read-time projection that forced every default list into a Python-filtered scan, added a supplemental count query, and widened MCP and Typesense reads. Readers now trust the stored flag alone: default lists regain server-side offset, counts are one aggregate, MCP/Typesense read only what they return. Any successful explicit reprocess of a hidden row now records the user's keep.

4. One-time backfill (scripts/conversation_relevance_backfill.py)
Stamps legacy review rows, and runs the free rules over conversations never assessed (writing only rule discards; everything the rules cannot settle is left alone). Dry-run by default. Writes go through a new lifecycle-owned discard_by_relevance that re-reads each row in a transaction and refuses deleted, hidden, or user-restored rows. User curation (starred, renamed, restored, shared, foldered, photos) and stored calendar evidence protect a row. Needs the production ENCRYPTION_SECRET to read enhanced transcripts; without it, encrypted rows read as ambiguous and are left alone (never wrongly discarded).

5. A short fragment is judged in the context of its adjacent conversation
When the relevance step reaches the model, it looks up a kept conversation inside the same 120 s boundary gap that splits conversations, tells the model the gap and position (never the neighbor's content — titles may be ciphertext, and no extra user content should reach the model), and a discard records neighbor_id. It links rather than merges: moving speech between conversations would change the boundary contract live capture uses, race live writers, and force a reprocess of the neighbor. One bounded, registered query, only for the ambiguous middle, fail-open.

Product invariants affected

  • INV-MEM-4 — matched by path only. Discarded conversations produce no memory intake, as before; kept ones take the unchanged Short-term route. No promotion path is added.
  • INV-TASK-2 — matched by path only. Extraction, the proposing/writing split and prompts are untouched; the relevance step runs before extraction and can only prevent it.

Deploy precondition

Run the backfill before this is deployed, or legacy filler rows reappear in lists until it runs (it is safe to run against the current production code: it only stamps rows that are already hidden or that the rules settle):

python scripts/conversation_relevance_backfill.py --uid <uid>          # dry run, one account
python scripts/conversation_relevance_backfill.py --limit 50           # dry run, first 50 users
python scripts/conversation_relevance_backfill.py --apply              # after review of the dry run

It needs production Firestore access and ENCRYPTION_SECRET; it has not been run.

Verification

  • Full backend unit suite (1,230 files) run file-isolated via test.sh; the 10 files that failed under the saturated full run were CPU-duration guards and pass in isolation, together with all 172 files touching these modules.
  • New: test_conversation_relevance_backfill.py (what it hides and never more), neighbor lookup and linking, model-withheld tier, deferred desktop filler, trigger-derived mode in the JIT/source contract tests, legacy-job trigger fallback.
  • pyright: 0 errors in every changed production module.
  • Not measured: live behaviour; the backfill has not been run anywhere.

Failure-Class: FC-run-now-flag-skips-content-gate

Line-Count-Exception: backend/utils/conversations/process_conversation.py | 3236 -> 3280 | desktop store paths take the trigger and run the rules-only relevance tier; neighbor wiring at the model call (I/O helpers moved to relevance_io.py)
Line-Count-Exception: backend/database/conversation_finalization_jobs.py | 2218 -> 2222 | jobs persist processing_trigger with a rollback mirror of force_process
Line-Count-Exception: backend/utils/llm/conversation_processing.py | 1976 -> 1988 | discard prompt describes an adjacent conversation by gap and position

🤖 Generated with Claude Code

Review in cubic

Git-on-my-level and others added 5 commits September 23, 2026 10:24
force_process, is_reprocess and bypass_jit_first_open were independent
booleans every caller had to combine correctly; "run now" silently implying
"skip discard" was one bad combination. utils/conversations/processing_trigger.py
now holds one row per ProcessingTrigger (run now, reprocess, JIT bypass,
relevance policy) and process_conversation takes only the trigger. Every
existing call site's flags equal its trigger's row, so behaviour is unchanged.

Finalization jobs persist `processing_trigger` instead of implying it from
force_process; the legacy bit is still mirrored for a rollback and read as a
fallback for jobs already in flight.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…that skip enrichment

Free-tier desktop captures (the legacy deferral and the deterministic
minimum / projection stores) never reached the relevance step, so a desktop
"Mm-hmm" was stored and listed like a conversation. The deterministic tier
costs nothing, so those stores now run it with the model withheld: rule
discards hide the capture (recoverable), and whatever the rules cannot settle
is kept with reason `model_withheld`. Any wake-word match skips the rules.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…bility authority

Readers hid legacy sync review rows with a read-time projection
(is_effectively_discarded), which forced every default list into a
Python-filtered scan, added a supplemental count query, widened MCP and
Typesense reads with visibility metadata, and let "hidden" mean two things.
Readers now trust the stored `discarded` flag alone: default lists regain
server-side offset, counts are one aggregate, MCP and Typesense read only the
fields they return.

scripts/conversation_relevance_backfill.py brings stored rows onto that
contract: it stamps legacy review rows, and runs the free relevance rules over
conversations never assessed (writing only rule discards). It is dry-run by
default and writes through a new lifecycle-owned discard_by_relevance, which
re-reads each row in a transaction and refuses deleted, hidden, or
user-restored rows. User curation and stored calendar evidence protect a row.

Deploy precondition: run the backfill (--apply) before this reader change is
live, or legacy filler rows reappear until it runs.

Also: any successful explicit reprocess of a hidden row now records the
user's keep, not only for sync review rows.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…acent conversation

A slice split off a real conversation ("got a new pad for you. Come on.")
looks trivial judged alone. When the relevance step reaches the model, it now
looks up a kept conversation within the same 120 s boundary gap that splits
conversations, tells the model the gap and position (never the neighbor's
content), and a discard records `neighbor_id` so the fragment is linked to the
conversation it belongs to rather than lost.

It links instead of merging transcripts on purpose: moving speech between
conversations would change the boundary contract live capture uses, race live
writers, and force a reprocess of the neighbor. The lookup is one bounded,
registered Firestore query, only for the ambiguous middle, and fails open.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…rdinator

Neighbor lookup, the model-withheld tier, and decision recording move to
utils/conversations/relevance_io.py; relevance.py stays pure and
process_conversation only wires them.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agentic security review of this PR found one medium-severity integrity issue in the relevance backfill: decrypt/decode failures are coerced to an empty transcript and then stamped as discards, which contradicts the advertised fail-open for unread enhanced rows.

Open in Web View Automation 

Sent by Cursor Security Agent: Security Reviewer

Comment on lines +75 to +82
segments = conversation.get('transcript_segments') or []
if not isinstance(segments, list):
return None
if find_wake_word_matches(segments):
return None
texts = [str(segment.get('text') or '') for segment in segments if isinstance(segment, Mapping)]
verdict, rule = deterministic_relevance(texts, None)
return rule if verdict == 'discard' else None

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔒 Agentic Security Review
Severity: MEDIUM

verdict_for treats an empty transcript_segments list as a rule discard (empty_transcript). Production iteration decrypts via _prepare_conversation_for_read → _decrypt_conversation_data, which replaces failed decrypt/JSON decode with [] instead of leaving ciphertext as a non-list. The isinstance(segments, list) fail-open therefore never sees an undecodable enhanced row.

This PR states that without a working ENCRYPTION_SECRET, encrypted rows stay ambiguous and are never wrongly discarded. A present-but-wrong secret still starts the process; decrypt returns ciphertext, the read helper coerces it to [], and --apply stamps discarded=True on completed unassessed conversations. Default lists, search, MCP, and Typesense now trust that stored flag.

Impact: A key mismatch or a corrupt enhanced blob can hide real conversations for every scanned user. Discards are recoverable, but they become immediately user-visible.

Fix in Cursor Fix in Web

Reviewed by Cursor Security Reviewer for commit 824952b. Configure here.

The parity contract was flipped to expect a Python-side scan only because of
the legacy review read projection; with the stored flag authoritative, the
indexed query pages with limit/offset as it did before.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@Git-on-my-level
Git-on-my-level merged commit a8975fa into main Sep 23, 2026
61 checks passed
@Git-on-my-level
Git-on-my-level deleted the fix/relevance-followups branch September 23, 2026 05:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant