feat(trace): 统一时间线——操作 + 网络请求录制,用于 site adapter 创建 - #230
Merged
Merged
Conversation
…reation Replaces the empty trace skeleton with a working recording system that captures command actions, human browser interactions, network requests, and responses in a single timeline with causal inference (triggerSeq). Key changes: - protocol.ts: TraceEntry union type (TraceAction/Request/Response/Navigation) - tab-state.ts: TraceSession on TabStateManager, recordAction() with detail - command-dispatch.ts: trace start/stop/status/events/body handlers - cdp-connection.ts: network/navigation events pushed to trace timeline, human action capture via Runtime.addBinding + injected JS listeners, new tabs auto-join trace session via Target.targetCreated openerId - commands.ts: trace accepts 5 subcommands with filtering params - cli trace.ts: formatted timeline output, --type/--filter/--since/--limit Verified: actions (command + human), network requests with triggerSeq, trace events query with filters, trace body fetch, backward compat with existing network/console/errors commands.
During cross-origin navigation, Chrome detaches the old CDP session and attaches a new one for the same target. The old code called tabManager.removeTab() in Target.detachedFromTarget, destroying the tab's event buffers (network requests, console, errors, trace data). Fix: - Target.detachedFromTarget: only clean session maps, do NOT remove tab state. Tab removal is now exclusively handled by Target.targetDestroyed. - Target.attachedToTarget: re-enable CDP domains (Network, Page, Runtime, DOM, Accessibility) on the new session when the tab already exists. Also re-inject human capture script for traced tabs. This fixes a long-standing bug where network requests after page navigation were silently dropped from both the network buffer and the trace timeline.
- Help text: list all 5 trace subcommands with descriptions - Options section: add --since/--type/--filter/--limit/--request-id - Error message already updated in previous commit
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
背景
创建 site adapter 时,需要理解"用户操作 → 网络请求"的因果关系。现有的
trace命令是空壳(traceEvents永远为空),recordAction()只存最后一个 seq 数字,人工在 Chrome 里的操作完全不可见。改动
1. 统一时间线(
feat(trace))将空壳
trace命令改造为完整的录制系统,在同一条时间线里记录:recordAction()传入操作详情Runtime.addBinding+ 注入 JS 监听器捕获triggerSeq)新增子命令:
trace events—— 查询时间线,支持--tab/--type/--since/--filter/--limittrace body <requestId>—— 获取某个请求的 response body设计要点:
Target.targetCreated的openerId自动加入trace stop只停止采集,数据保留,可反复查询network/console/errors命令零改动,完全向后兼容2. 修复页面导航后网络事件丢失(
fix(cdp))这是一个长期存在的 bug,不限于 trace 功能。
根因:
Target.detachedFromTarget在同 tab 跨域导航时被触发,handler 误调tabManager.removeTab(),导致 tab 的所有 event buffer(network/console/errors)被销毁。新 session attach 后事件无处写入,被静默丢弃。修复:
Target.detachedFromTarget:只清 session 映射,不删 tab state。tab 删除交给Target.targetDestroyedTarget.attachedToTarget:检测到已有 tab 时,重新 enable CDP domains(Network/Page/Runtime/DOM/Accessibility)3. Help text 更新
CLI help 和选项说明同步更新。
改动文件
protocol.tsTraceEvent→TraceEntryunion 类型;Request 新增 traceType/requestId;ResponseData 新增 traceBodyindex.ts(shared)commands.tstab-state.tscommand-dispatch.tscdp-connection.tstrace.ts(cli)index.ts(cli)验证
network requests等命令不受影响Agent 使用示例