Skip to content

Commit b8a9dfb

Browse files
authored
Add in-memory trace analysis commands (#13)
## Summary - add in-memory trace sessions so `trace stop` analyzes captured events and agents can inspect them without reading raw trace files - add `trace status|list|summary|tracks|entries|entry` commands with compact, filterable output for user timings and custom DevTools tracks - keep raw trace recording opt-in and backward-compatible by preserving `trace start` / `trace stop [--file PATH]` while routing parsing into a separate `src/trace/` module ## Backward Compatibility - Existing trace capture remains explicit: tracing still starts only when `trace start` is invoked. - `trace stop --file PATH` still exports raw `traceEvents` JSON; it now also creates an in-memory analyzed session and returns a richer summary. - Existing daemon lifecycle, target/session flow, and newline-delimited IPC remain unchanged. - The CLI surface only adds new `trace` subcommands and does not change defaults for other command families. ## Risks - User timing parsing currently relies on a lightweight normalization layer rather than a full DevTools handler port, so uncommon trace event shapes may still be missed. - Track grouping and console timestamp range reconstruction are derived from recorded trace data; unusual producer behavior could land entries on the default track. - In-memory session retention is bounded, which keeps token and memory usage under control but means older trace sessions are evicted after the cap is reached. ## Manual testing - Setup: run the daemon, select a Chrome target, and load a page that emits `performance.measure`, `performance.mark`, and optional custom DevTools track metadata. - Start capture: run `agent-cdp trace start`, exercise the page, then run `agent-cdp trace stop --file /tmp/trace.json`. - Verify summary: run `agent-cdp trace summary` and confirm it reports a new session with entry counts and top tracks. - Verify navigation: run `agent-cdp trace tracks`, then `agent-cdp trace entries --track Timings`, and finally `agent-cdp trace entry --id <ENTRY_ID>` to confirm drill-down output stays compact and filterable. - Verify compatibility: open `/tmp/trace.json` and confirm raw `traceEvents` were still exported. ## Validation - `pnpm format` - `pnpm lint` - `pnpm typecheck` - `pnpm test` - `pnpm build` Closes #12
1 parent 1292566 commit b8a9dfb

18 files changed

Lines changed: 1887 additions & 19 deletions

File tree

packages/agent-cdp/README.md

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ agent-cdp target clear
9292
9393
- **Console** — list and fetch log lines: `console list`, `console get <id>`
9494
- **Network** — bounded live capture plus persisted sessions: `network status`, `network start`, `network summary`, `network list`, `network request`, `network request-headers`, `network response-headers`, `network request-body`, `network response-body`
95-
- **Trace**`trace start` / `trace stop [--file PATH]` for raw trace capture
95+
- **Trace**explicit trace capture plus in-memory session analysis for `performance.measure`, `performance.mark`, `console.timeStamp`, and custom DevTools tracks: `trace start`, `trace stop`, `trace summary`, `trace tracks`, `trace entries`, `trace entry`
9696
- **Memory (raw)**`memory capture --file PATH` for a heap snapshot file
9797
- **Heap snapshot tools**`mem-snapshot` commands to capture, load, summarize, diff snapshots, inspect classes/instances/retainers, and triage leak-style comparisons
9898
- **JS heap monitor**`js-memory` commands for sampling, summaries, diffs, trends, and leak-oriented signals
@@ -160,3 +160,56 @@ Current limitations:
160160
- WebSocket visibility is limited to handshake metadata in v1.
161161
- There is no throttling, blocking, mocking, replay, or HAR export in v1.
162162
- Timing, size, protocol, cache, and remote-endpoint metadata may be partial or absent depending on target behavior.
163+
164+
## Trace inspection
165+
166+
Use `trace` when you need to capture a bounded trace, then navigate the analyzed results in small, filterable chunks rather than dumping raw `traceEvents` into the terminal.
167+
168+
Quick start:
169+
170+
```sh
171+
agent-cdp trace start
172+
# reproduce the interaction you want to inspect
173+
agent-cdp trace stop
174+
agent-cdp trace summary
175+
agent-cdp trace tracks
176+
agent-cdp trace entries
177+
agent-cdp trace entry --id te_1
178+
```
179+
180+
What the current trace tooling can inspect:
181+
182+
- plain `performance.measure()` entries
183+
- plain `performance.mark()` entries
184+
- `console.timeStamp()` entries, including custom tracks and groups emitted through DevTools timeline events
185+
- custom track and group metadata attached through DevTools-style `detail.devtools` payloads on user timing entries
186+
- custom traces emitted by tools like React DevTools when they surface track-based timeline data through the trace stream
187+
188+
Default behavior:
189+
190+
- Tracing is explicit. The daemon does not start recording on startup; use `trace start` when you want a capture.
191+
- `trace stop` stores an analyzed trace session in daemon memory so agents can query it immediately.
192+
- `trace stop --file PATH` can still export the raw trace JSON when you need to inspect the underlying `traceEvents` directly.
193+
- When `--session` is omitted, trace queries read from the latest analyzed session.
194+
- `trace summary` gives a compact overview first.
195+
- `trace tracks` lists discovered built-in and custom tracks with pagination/filtering.
196+
- `trace entries` is the main drill-down command; it defaults to measures and supports `--track`, `--type`, `--text`, `--start-ms`, `--end-ms`, `--limit`, `--offset`, and `--sort`.
197+
- `trace entry` shows the full details for exactly one selected entry.
198+
199+
Examples:
200+
201+
```sh
202+
agent-cdp trace summary
203+
agent-cdp trace tracks --group "Scheduler ⚛"
204+
agent-cdp trace entries --track "Image Processing" --limit 10
205+
agent-cdp trace entries --type mark --text boot
206+
agent-cdp trace entries --start-ms 0 --end-ms 100 --sort duration
207+
agent-cdp trace entry --id te_16
208+
```
209+
210+
Current limitations:
211+
212+
- Trace analysis is optimized for compact CLI inspection, not full flamechart rendering.
213+
- Default track output reports active time on a track; use verbose output when you need the broader span between first and last entry.
214+
- The parser handles common user timing and DevTools custom-track shapes seen in Chrome, React DevTools, and similar tools, but uncommon trace producers may still emit unsupported event forms.
215+
- Trace sessions are stored in a bounded in-memory history rather than persisted automatically.

packages/agent-cdp/skills/core.md

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,13 +91,25 @@ That skill contains session behavior, common workflows, body inspection guidance
9191
9292
## Trace recording
9393
94+
For trace workflows, run:
95+
96+
```bash
97+
agent-cdp skills get trace
98+
```
99+
100+
That skill contains trace session behavior, user-timing/custom-track inspection, token-efficient navigation guidance, and raw export guidance.
101+
102+
Minimal commands:
103+
94104
```bash
95105
agent-cdp trace start # begin recording a performance trace
96-
agent-cdp trace stop [--file PATH] # stop and save (or auto-name) the trace
106+
agent-cdp trace stop [--file PATH] # stop, analyze in memory, and optionally export the raw trace
107+
agent-cdp trace summary
108+
agent-cdp trace tracks
109+
agent-cdp trace entries
97110
```
98111
99-
Produces a `.json` file loadable in Chrome DevTools Performance tab or
100-
`chrome://tracing`.
112+
Use `--file PATH` only when you need the raw trace JSON for direct inspection or external tools.
101113
102114
## Raw memory capture
103115

packages/agent-cdp/skills/trace.md

Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
---
2+
name: trace
3+
description: Trace inspection workflows for agent-cdp. Use after reading the core skill and selecting a target. Covers explicit trace capture, in-memory trace sessions, user timing inspection, custom DevTools tracks, token-efficient navigation, and raw trace export when needed.
4+
allowed-tools: Bash(agent-cdp:*)
5+
---
6+
7+
# agent-cdp trace
8+
9+
Focused guide for trace capture and inspection after the daemon is running and a target has been selected.
10+
11+
Prerequisite:
12+
13+
```bash
14+
agent-cdp skills get core
15+
agent-cdp start
16+
agent-cdp target list --url URL
17+
agent-cdp target select <id> --url URL
18+
```
19+
20+
## Mental model
21+
22+
- Tracing is explicit. The daemon does not start recording on startup.
23+
- `trace start` begins a raw CDP trace capture for the selected target.
24+
- `trace stop` ends capture, analyzes the trace in memory, and stores a queryable trace session in the daemon.
25+
- `trace stop --file PATH` also exports the raw `traceEvents` JSON if you need the underlying trace file.
26+
- Without `--session`, trace queries read from the latest analyzed session.
27+
- Default trace output is intentionally compact so agents can navigate the data in chunks.
28+
29+
## Commands
30+
31+
```bash
32+
agent-cdp trace start
33+
agent-cdp trace stop [--file PATH]
34+
35+
agent-cdp trace status
36+
agent-cdp trace list [--limit N] [--offset N]
37+
agent-cdp trace summary [--session ID]
38+
agent-cdp trace tracks [--session ID] [--limit N] [--offset N] [--text TEXT] [--group NAME]
39+
agent-cdp trace entries [--session ID] [--track NAME] [--type measure|mark|stamp] [--text TEXT] [--start-ms N] [--end-ms N] [--limit N] [--offset N] [--sort time|duration|name]
40+
agent-cdp trace entry --id ENTRY_ID [--session ID]
41+
```
42+
43+
## What trace analysis can inspect
44+
45+
- plain `performance.measure()` entries
46+
- plain `performance.mark()` entries
47+
- `console.timeStamp()` entries
48+
- custom track and group metadata attached through DevTools-style `detail.devtools` payloads on user timing entries
49+
- custom tracks emitted through `devtools.timeline` `TimeStamp` events, including React DevTools timeline data
50+
51+
## Workflow: Capture And Inspect A Fresh Trace
52+
53+
```bash
54+
agent-cdp trace start
55+
# reproduce the interaction you want to inspect
56+
agent-cdp trace stop
57+
agent-cdp trace summary
58+
agent-cdp trace tracks
59+
agent-cdp trace entries
60+
```
61+
62+
Use this when you want the latest interaction summarized without dumping raw trace JSON into the terminal.
63+
64+
## Workflow: Navigate In Small Chunks
65+
66+
Start broad, then narrow.
67+
68+
```bash
69+
agent-cdp trace summary
70+
agent-cdp trace tracks --limit 10
71+
agent-cdp trace entries --limit 10
72+
agent-cdp trace entry --id te_1
73+
```
74+
75+
This is the preferred agent loop because it minimizes tokens while preserving drill-down.
76+
77+
## Workflow: Focus On A Specific Track
78+
79+
```bash
80+
agent-cdp trace tracks --group "Scheduler ⚛"
81+
agent-cdp trace entries --track "Blocking" --limit 10
82+
agent-cdp trace entries --track "Image Processing" --sort duration
83+
```
84+
85+
Use `trace tracks` to discover the exact track names first, then filter `trace entries` by `--track`.
86+
87+
## Workflow: Focus On A Specific Entry Type
88+
89+
```bash
90+
agent-cdp trace entries --type measure --limit 20
91+
agent-cdp trace entries --type mark --text boot
92+
agent-cdp trace entries --type stamp --track "Console Track"
93+
```
94+
95+
Guidance:
96+
- `measure` is usually the best default when triaging performance work
97+
- `mark` is useful for lifecycle waypoints and custom markers
98+
- `stamp` is useful for DevTools-style custom timeline entries
99+
100+
## Workflow: Time-Window Inspection
101+
102+
```bash
103+
agent-cdp trace entries --start-ms 0 --end-ms 100 --sort duration
104+
agent-cdp trace entries --track "Blocking" --start-ms 100 --end-ms 250
105+
```
106+
107+
Use time windows to cut down noisy sessions before drilling into a specific entry id.
108+
109+
## Workflow: Inspect One Entry Fully
110+
111+
```bash
112+
agent-cdp trace entries --track "Image Processing" --limit 5
113+
agent-cdp trace entry --id te_16
114+
```
115+
116+
`trace entry` is where you should expect to see the richest details such as tooltip text, custom properties, and any preserved user detail payload.
117+
118+
## Workflow: Export The Raw Trace
119+
120+
Use raw export only when the analyzed views do not answer the question or when you need to compare exact event shapes.
121+
122+
```bash
123+
agent-cdp trace start
124+
# reproduce the interaction
125+
agent-cdp trace stop --file /tmp/trace.json
126+
```
127+
128+
The exported file can be inspected directly or loaded in tools that understand Chrome trace JSON.
129+
130+
## Token-Efficient Navigation Tips
131+
132+
- Prefer `trace summary` before any list command.
133+
- Use `trace tracks` to discover candidate tracks before scanning entries.
134+
- Use `--limit` and `--offset` on `trace list`, `trace tracks`, and `trace entries`.
135+
- Use `--text`, `--type`, `--track`, `--start-ms`, and `--end-ms` to narrow the result set before printing.
136+
- Use `trace entry --id ...` for full detail on a single item rather than increasing list limits.
137+
138+
## Output Semantics
139+
140+
- `trace summary` reports a compact session overview with entry counts and top tracks.
141+
- `trace tracks` reports active time by default, not the broader span between the first and last entry on that track.
142+
- `trace tracks --verbose` includes the broader track span in addition to active time.
143+
- `trace entries` defaults to measures for a narrower, more actionable list.
144+
145+
## Caveats
146+
147+
- Trace analysis is optimized for CLI summaries and drill-down, not flamechart rendering.
148+
- Support is strongest for common user timing and DevTools custom-track shapes seen in Chrome and React DevTools.
149+
- Some trace producers may emit unsupported event forms.
150+
- Trace sessions are kept in a bounded in-memory history rather than persisted automatically.
151+
152+
## Suggested Agent Loop
153+
154+
When debugging a performance issue, prefer this order:
155+
156+
```bash
157+
agent-cdp trace start
158+
# reproduce the issue
159+
agent-cdp trace stop
160+
agent-cdp trace summary
161+
agent-cdp trace tracks --limit 10
162+
agent-cdp trace entries --track TRACK_NAME --limit 10
163+
agent-cdp trace entry --id ENTRY_ID
164+
```
165+
166+
Only export the raw file when the analyzed commands are not enough.

packages/agent-cdp/src/__tests__/cli.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ describe("cli", () => {
2424
expect(usage()).toContain("target select <id> [--url URL]");
2525
expect(usage()).toContain("network start [--name NAME] [--preserve-across-navigation]");
2626
expect(usage()).toContain("network response-body --id REQ_ID [--session ID] [--file PATH]");
27+
expect(usage()).toContain("trace status");
28+
expect(usage()).toContain("trace entries [--session ID] [--track NAME]");
2729
expect(usage()).toContain("js-allocation start");
2830
expect(usage()).toContain("js-allocation-timeline start");
2931
});

0 commit comments

Comments
 (0)