Skip to content

fix(risk-treatment): re-embed tasks with missing vectors in upstash#3450

Closed
tofikwest wants to merge 1 commit into
mainfrom
tofik/cs-681-bug-draft-plan-suggest-links
Closed

fix(risk-treatment): re-embed tasks with missing vectors in upstash#3450
tofikwest wants to merge 1 commit into
mainfrom
tofik/cs-681-bug-draft-plan-suggest-links

Conversation

@tofikwest

@tofikwest tofikwest commented Jul 17, 2026

Copy link
Copy Markdown
Contributor

Problem

When drafting a treatment plan for a risk, the AI tool returns "0 tasks and 0 controls" even though the org has existing tasks. This affects multiple risks in the customer's account, blocking compliance progress.

Root cause

The linkage process fetches task vectors from Upstash to find similar tasks. Two previous fixes (#3340 and #3359) improved retrieval but could not recover vectors that don't exist in the store. The real gap: upsertEntityEmbeddings skips re-embedding a task whenever its embeddingHash matches a stored value, assuming the vector still exists in Upstash. Once a vector is deleted or lost while the hash persists, runLinkage will skip that task forever during the next run. The org then enumerates zero vectors and returns zero suggestions.

Fix

Added a reconciliation step in the linkage flow to verify that vectors actually exist in Upstash before trusting the hash. If a task's hash is present but its vector is missing from the store, re-embed it immediately. This ensures fetchOrgTaskVectors finds all in-scope tasks even after vector loss, restoring the suggestion pipeline.

Explicitly NOT touched

The reranker, link threshold logic, API routes, and general suggestion scoring remain unchanged. This is a retrieval-only fix that does not alter ranking or filtering.

Verification

Added regression test asserting that tasks with valid hashes but missing vectors are re-embedded and then found during linkage. Unit tests for run-linkage and index reconciliation pass locally. ✅

Fixes CS-681


Summary by cubic

Restores task/control suggestions in Risk Treatment by reconciling missing Upstash vectors: hash-matched tasks are now checked and re-embedded when their vectors are absent. Addresses Linear CS-681.

  • Bug Fixes
    • Verify existence of hash-matched vectors using @upstash/vector Index.fetch; re-embed and upsert when missing.
    • Batch vector checks (up to 1000 ids) to respect Upstash limits.
    • Add regression test that re-embeds a task with a valid hash but missing vector and confirms it’s discoverable in linkage.

Written for commit 2e773c0. Summary will update on new commits.

Review in cubic

## Problem

When drafting a treatment plan for a risk, the AI tool returns "0 tasks and 0 controls" even though the org has existing tasks. This affects multiple risks in the customer's account, blocking compliance progress.

## Root cause

The linkage process fetches task vectors from Upstash to find similar tasks. Two previous fixes (#3340 and #3359) improved retrieval but could not recover vectors that don't exist in the store. The real gap: `upsertEntityEmbeddings` skips re-embedding a task whenever its `embeddingHash` matches a stored value, assuming the vector still exists in Upstash. Once a vector is deleted or lost while the hash persists, `runLinkage` will skip that task forever during the next run. The org then enumerates zero vectors and returns zero suggestions.

## Fix

Added a reconciliation step in the linkage flow to verify that vectors actually exist in Upstash before trusting the hash. If a task's hash is present but its vector is missing from the store, re-embed it immediately. This ensures `fetchOrgTaskVectors` finds all in-scope tasks even after vector loss, restoring the suggestion pipeline.

## Explicitly NOT touched

The reranker, link threshold logic, API routes, and general suggestion scoring remain unchanged. This is a retrieval-only fix that does not alter ranking or filtering.

## Verification

Added regression test asserting that tasks with valid hashes but missing vectors are re-embedded and then found during linkage. Unit tests for run-linkage and index reconciliation pass locally. ✅
@linear

linear Bot commented Jul 17, 2026

Copy link
Copy Markdown

CS-681

@vercel

vercel Bot commented Jul 17, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
app Ready Ready Preview, Comment Jul 17, 2026 10:00pm
comp-framework-editor Ready Ready Preview, Comment Jul 17, 2026 10:00pm
1 Skipped Deployment
Project Deployment Actions Updated (UTC)
portal Skipped Skipped Jul 17, 2026 10:00pm

Request Review

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

cubic analysis

No issues found across 2 files

Confidence score: 5/5

  • Automated review surfaced no issues in the provided summaries.
  • No files require special attention.

Linked issue analysis

Linked issue: CS-681: [Bug] - "Draft Plan & Suggest Links" Returns 0 Tasks and 0 Controls on Risk Treatment Plan

Status Acceptance criteria Notes
Verify the linkage flow checks Upstash for the existence of hash-matched vectors and re-embeds any that are missing (heals hash→vector desync). The PR adds findEntitiesWithoutVectors and changes upsertEntityEmbeddings to call it and include missing-vector entities in the re-embed set, so entities whose hash matches but whose vectors are absent are re-embedded.
Batch vector existence checks within Upstash limits (handle up to 1000 ids per fetch). The implementation batches ID fetches using VECTOR_FETCH_BATCH_SIZE = 1000 and iterates over candidates in slices before calling index.fetch, respecting Upstash limits.
Add regression test asserting that a task with a valid hash but a missing Upstash vector is re-embedded and becomes discoverable during linkage. A new unit/regression test simulates a missing vector via fetchMock, runs upsertEntityEmbeddings, and asserts upsert was called for the missing vector and that the result reports the applied hash.
Fix results in treatment-plan linkage returning previously-suppressed suggestions (i.e., restores finding existing tasks/controls for affected risks) without changing reranking/threshold logic. PR explicitly states it's a retrieval-only fix (no changes to reranker or thresholds). The code changes ensure missing vectors are recreated so the linkage path can enumerate those tasks; the regression test demonstrates the entity becomes discoverable in linkage.

Re-trigger cubic

@tofikwest tofikwest closed this Jul 20, 2026
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