Skip to content

fix(cache): match a namespaced user_id in pipeline log queries - #489

Open
gouyu-hou wants to merge 2 commits into
Tencent-Hunyuan:mainfrom
gouyu-hou:fix/pipeline-log-namespaced-user
Open

gouyu-hou wants to merge 2 commits into
Tencent-Hunyuan:mainfrom
gouyu-hou:fix/pipeline-log-namespaced-user

Conversation

@gouyu-hou

Copy link
Copy Markdown

变更

store_pipeline_log() 写入时按第一个 "__" 把 user_id 拆成 namespace 与 user_id 两列落库(cache_base 的接口注释就是这个契约),但 get_pipeline_logs() 拿调用方传进来的原始串直接比 user_id:

if user_id:
    conditions.append("user_id = ?")
    params.append(user_id)

所以 user_id="proj__u1" 永远比不过刚写进去的 namespace='proj', user_id='u1'。查询侧改成同口径拆分并按 (namespace, user_id) 成对匹配;cache_sqlite 与 cache_mysql 同错同改。

旧库里经 _migrate_pipeline_logs_columns 补列的历史行是 namespace='' + 完整 user_id,所以请求带 "__" 时额外放行 namespace='' AND user_id=<原始串>,不让改口径把老数据查丢。不带 "__" 的 user_id 行为不变。

用户影响

MemoryInspector.get_pipeline_logs(user_id=...) 是文档里给出的公开用法,多租户/多项目(proj__u1 这种 SDK user_id)下按 user_id 查 pipeline 日志恒返回空列表,看起来像"这个用户没有任何 EXTRACT / SEARCH_QUERY 记录"。改后能查到自己的日志,且不会跨 namespace 串(proj__u1 与 other__u1 各查各的)。

按 request_id 查询不受影响,写侧数据一直是对的 —— 坏的只有读侧这一条过滤条件。

验证

  • 先写或更新验收:新增 tests/test_pipeline_log_user_lookup.py(5 例)
  • SDK 变更:受影响文件单独跑,结果如下(本机 Windows + Python 3.12)
用例 改前 改后
test_namespaced_user_id_finds_its_own_logs FAILED passed
test_namespaced_query_stays_inside_its_namespace FAILED passed
test_step_filter_composes_with_a_namespaced_user_id FAILED passed
test_bare_user_id_still_finds_unnamespaced_logs passed passed
test_rows_written_before_the_namespace_split_are_still_found passed passed
  • 回归:tests/test_pipeline_observability.py + tests/test_op_trace_logs.py → 27 passed
  • 变异验证:只回退这两个后端文件的改动,上面 3 例立刻变红;恢复后 5 passed
  • 真实复现(改前):写入 user_id="proj__u1" 后 get_pipeline_logs(user_id="proj__u1") → 0 行,get_pipeline_logs(user_id="u1") → 1 行。

一个没改的点,想听你的意见

不带 "__" 的 user_id 我保持了原行为(只比 user_id 列),这意味着 get_pipeline_logs(user_id="u1") 仍会同时返回 namespace='proj' 和 namespace='other' 下 user_id 都是 u1 的行。要不要把它也收成 namespace='' AND user_id=?(即严格按租户隔离),或者反过来给 get_pipeline_logs 加一个显式 namespace 参数?这属于接口语义选择,我没有替你决定。

隐私与安全

  • 不新增屏幕、AXTree、OCR、键鼠、剪贴板或后台录音采集
  • 不包含密钥、内部地址或真实用户数据(用例只写 tmp_path)
  • 新依赖已记录许可证(无新依赖)

回滚

两处 SQL 过滤条件 + 一个测试文件,可整体 revert;表结构不变,无需迁移。

store_pipeline_log() splits user_id on the first "__" and writes namespace and
user_id as separate columns, which is the documented contract in cache_base.
get_pipeline_logs() compared the raw string against user_id only, so
user_id="proj__u1" never matched the row it had just written: every namespaced
user got an empty log list back from MemoryInspector.

Split the requested id the same way and match the (namespace, user_id) pair on
both backends. Rows written before the namespace column existed keep the full
id in user_id with namespace='', so they are still matched when the requested id
contains "__"; a bare id keeps the previous behaviour.
The fix touched two backends but the tests only exercised sqlite; mysql needs
a server, so CI cannot reach that branch at all. Capture what
get_pipeline_logs hands to _fetchall instead — the predicate text and the
placeholder order are exactly what a half-applied change gets wrong, and a
wrong order returns another tenant's rows without raising.

Red on main (no namespace condition there), green with the fix.

Also document the user_id contract on the mysql method, matching sqlite.
@gouyu-hou

Copy link
Copy Markdown
Author

跟进一下(不催合,只是同步状态):

这条正文最后留了一个需要你拍板的接口语义问题(不带 __ 的 user_id 是否也收成 namespace='' 隔离,还是给 get_pipeline_logs 加显式 namespace 参数)。你定了我当天就能改,默认我保持现状不动。

另外报一个我这边无法自助解决的事:本仓库 ci.yml 对我这批 fork 来的 PR 全部停在 action_required(#489 #490 #495 #496 #497 #498 #499 #500 #509 #510 共 10 条),需要维护者点一次批准才会真正跑。本地我是按改动模块单文件跑的,结果都贴在各自正文里;如果你希望 CI 上有一份绿的记录再 review,麻烦放行一下这一批。

这批 PR 都已对齐 11-18 的 main(d8138b5)、无冲突。如果粒度上你更希望按模块合成几条,我也可以重排。

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