Skip to content

fix(inspector): order read-trace steps by when they happened - #490

Open
gouyu-hou wants to merge 2 commits into
Tencent-Hunyuan:mainfrom
gouyu-hou:fix/read-trace-step-order
Open

gouyu-hou wants to merge 2 commits into
Tencent-Hunyuan:mainfrom
gouyu-hou:fix/read-trace-step-order

Conversation

@gouyu-hou

Copy link
Copy Markdown

变更

MemoryInspector.async_get_read_trace() 的注释写的是「按 event_at_ms 升序;缺省回落 created_at 保持稳定」,但代码是:

read_logs.sort(key=lambda log: (log.get("event_at_ms") or 0, log.get("created_at") or ""))

event_at_ms 对补列之前写入的历史行是 0(cache_sqlite._migrate_pipeline_logs_columns 给旧库 ALTER 出来的列默认 0),于是这些行的排序键变成 (0, created_at),恒定排在所有带真实时刻的行前面,跟注释承诺的回退语义正好相反。

改成一个小的取键函数:有 event_at_ms 用它,没有就解析 created_at 成同一量纲,两者都拿不到时排到最后(list.sort 稳定,其余行顺序不变)。

用户影响

get_full_trace / get_read_trace 的 steps、by_step 顺序,以及 UI 按这个顺序渲染的 read pipeline 时间线。一次 search 的 trace 里如果混有历史行(升级前写的库),READ_REQUEST 会显示在它之后发生的 recall 步骤后面,读起来像"请求发生在召回之后"。

只影响 trace 的展示顺序,不改变任何检索结果或落库内容。

验证

  • 先写或更新验收:新增 tests/test_read_trace_step_order.py(3 例)
用例 改前 改后
test_legacy_rows_without_event_time_do_not_jump_to_the_front FAILED passed
test_rows_with_event_time_keep_their_occurrence_order passed passed
test_unparsable_timestamps_sort_last_instead_of_first FAILED passed
  • 回归:test_op_trace_logs.py + test_pipeline_observability.py + test_pipeline_log_user_lookup.py → 32 passed
  • 变异验证:只回退 hy_memory/inspector.py,上面两例立刻变红;恢复后 3 passed

隐私与安全

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

回滚

一个排序键 + 一个测试文件,可整体 revert。

get_read_trace() sorted rows by ("event_at_ms or 0", created_at) while its own
comment promised to fall back to created_at when the event time is missing.
Rows written before the event_at_ms column existed carry 0, so every one of
them sorted ahead of all current rows no matter when it actually ran: a trace
that mixed legacy and current steps reported READ_REQUEST after the recall
steps it preceded.

Use the event time when present, otherwise the created_at instant, and put rows
with neither at the end so the rest of the order is untouched.
Two gaps in the first pass, both on rows that lack event_at_ms:

- created_at can be written as "...Z" or with a space separator. Python
  3.9/3.10's fromisoformat accepts neither and raises, so those rows all fell
  into the "unparsable" bucket and sorted last as a group instead of taking
  their place on the timeline.
- the tie-breaker was the raw created_at value, so a missing one produced
  (inf, None) alongside (inf, ""), and sort() raised TypeError comparing None
  with str.

Normalize both spellings before parsing and coerce the tie-breaker to str.
The 3.9 behaviour is pinned by a test that swaps in a strict parser.
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