Skip to content

fix(semantic-index): keep Lance vector rows keyed by project and observation id - #410

Merged
AVIDS2 merged 2 commits into
AVIDS2:mainfrom
ruan-cat:fix/lance-vector-project-id
Nov 27, 2026
Merged

AVIDS2 merged 2 commits into
AVIDS2:mainfrom
ruan-cat:fix/lance-vector-project-id

Conversation

@ruan-cat

Copy link
Copy Markdown
Contributor

摘要

Lance 向量表被所有项目共用,但合并时只拿观察记录的数字编号当键。后一次写入会把这一行的项目号改掉,当前项目的向量查询又按项目号过滤,于是正文在、向量也在,查询却看不见。

事故经过

2026-11-24 21:47(UTC+8),在 D:\code\ruan-cat\monorepo 执行 memorix memory store --cwd。SQLite 里 #6468、#6469、#6470、#6471 的 projectId 都是 ruan-cat/monorepo。同一批编号在 Lance 表 observations_d8ae324f124db681a0b4 里的 project_id 却是 ruan-cat/common,向量维度是 2560。

当时这张表共 2912 行,其中 monorepo 367 行,common 402 行。不是全部向量都进了 common。6434–6452 的 monorepo 行仍然标着 monorepo。6457–6460 的正文是 monorepo,Lance 里没有行。6472–6482 在 SQLite 里不存在(当时最大编号是 6471),Lance 里却有,而且项目号全是 ruan-cat/common。

向量查询只扫当前项目的行,所以这四条进不了 monorepo 的向量结果。文本整词仍能从 SQLite 命中,例如 commit-types、gcq、49c5a8ef。项目别名表里这两个仓库是独立分组,这次也不是 --cwd 传错。

这和 2026-11-23 那次不是同一件事。那次是正文本身被写进了 common。这次正文项目是对的,错在向量行上的项目号。

原因

src/search/semantic-index.ts 用 mergeInsert('observation_id'),再 whenMatchedUpdateAll()。编号相同就整行覆盖,项目号只是字段,不是键。进程内的待写批次其实已经用「项目:编号」区分,刷进 Lance 时这个区分被丢掉了。

listSemanticVectorIds 只收集编号。标错项目的行也会让这个编号看起来已经有向量,补写因此被跳过。查询又按 project_id 过滤,标错的行对真正的项目不可见。

修复

  • 合并键改为 project_id 加 observation_id。同一个编号、不同项目各留一行。
  • 写入某个项目的编号时,先删掉这个编号上项目号不一致的旧行,再写入。避免错误标签继续被那个项目的查询扫到。没有冲突行时不删除,避免无谓抬高表版本。
  • 判断「已经有向量」改为看「项目:编号」。别的项目占着同一个编号,不再挡住当前项目补写。
  • 按项目删除向量时可以带上项目号,不再不分项目把这个编号整行删掉。

不改打分,不改 fuzzy 是否算命中,不改 embedding 模型,也不在这次提交里改用户机器上已经写错的活库。

测试

pnpm exec vitest run --no-file-parallelism --maxWorkers=1 tests/search/semantic-index.test.ts

2026-11-24 23:34 在本分支结果:1 个文件,7 条通过。新增用例先把 6468 写成 ruan-cat/common,再写成 ruan-cat/monorepo。之后 monorepo 的向量查询只看到 monorepo 那一行,common 的查询为空。

The shared semantic index matched only on observation_id, so a write from
another project could replace the project label and hide the vector from
the project that owns the observation.

Co-authored-by: Cursor <cursoragent@cursor.com>
@ruan-cat ruan-cat changed the title 修复 Lance 向量只按编号覆盖,项目号会被写错 fix(semantic-index): keep Lance vector rows keyed by project and observation id Nov 24, 2026
Cold-start backfill now treats a Lance row as present only for its own project, so the persisted-id fixture must use that key. The handle fixtures expired on 2026-11-20 and failed once CI ran after that date.

Co-authored-by: Cursor <cursoragent@cursor.com>
@AVIDS2
AVIDS2 merged commit 6bdc736 into AVIDS2:main Nov 27, 2026
6 of 7 checks passed
@AVIDS2

AVIDS2 commented Nov 27, 2026

Copy link
Copy Markdown
Owner

已审查并整合到 #411,保留原贡献提交。采用联合 merge key、项目范围的缓存/补写检查和 scoped delete。

审查时补了一条反例:原 deleteMismatchedProjectRows 在写入 B 项目的同编号向量时会删除 A 项目行,与联合键的隔离语义冲突,已移除这种自动删除。回归测试现要求两项目的同编号行均保留,按项目删除只影响目标项目。

检索端同时核对 Lance 项目与 canonical SQLite 项目,hybrid/vector 和全局检索均覆盖错标向量,避免旧错标行仅凭编号参与其他项目的排序。已有缺失向量按正确项目 key 补写;没有修改报告者机器上的活库。随 1.10.5 交付。

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.

2 participants