Skip to content

fix(browser): reuse last network snapshot for browser network --detail#1051

Closed
freemandealer wants to merge 1 commit intojackwener:mainfrom
freemandealer:fix/browser-network-detail-cache
Closed

fix(browser): reuse last network snapshot for browser network --detail#1051
freemandealer wants to merge 1 commit intojackwener:mainfrom
freemandealer:fix/browser-network-detail-cache

Conversation

@freemandealer
Copy link
Copy Markdown
Contributor

@freemandealer freemandealer commented Apr 16, 2026

Summary

  • cache the last browser network snapshot and reuse it for browser network --detail <index>
  • add coverage for cached detail lookup and out-of-range indexes

Problem

browser network --detail <index> could consume a fresh capture batch instead of the list the user had just seen, so the selected index could immediately stop matching the request they meant to inspect.

Validation

  • npx vitest run --project unit src/cli.test.ts
  • npm run typecheck

The previous browser network flow made follow-up inspection unstable.

`opencli browser network --detail <index>` could read a fresh capture batch instead of the list the user had just seen. That broke index alignment immediately after the first read, so users could not reliably inspect the request they selected from `browser network`.

Keep this fix in the CLI only and leave daemon/extension window-idle behavior unchanged.

- persist the last listed `browser network` snapshot in the browser cache directory
- make `--detail` load that snapshot first so indexes stay stable across follow-up inspection
- surface a usage error when the requested index is outside the cached snapshot
- add unit coverage for cached detail lookup and out-of-range indexes

Before:
- `opencli browser network` listed requests
- `opencli browser network --detail 3` could consume a different batch and make `3` point at the wrong request

After:
- `opencli browser network` lists requests and saves that snapshot
- `opencli browser network --detail 3` inspects request jackwener#3 from the same snapshot
@freemandealer freemandealer force-pushed the fix/browser-network-detail-cache branch from 2d0fb34 to b38eaef Compare April 17, 2026 15:18
@jackwener
Copy link
Copy Markdown
Owner

Thanks @freemandealer for spotting this — the index-mismatch symptom is real and I hit it myself this week while building a Twitter adapter. The cache approach here correctly identifies that --detail needs to refer back to what the user just saw, not a fresh capture batch.

We've decided to fold this into a larger refactor of browser network that addresses several related issues together:

  1. Stable request references--detail <key> using URL path + operation name instead of array index, so references survive across network re-runs
  2. Body shape output in the default list view (JSON path tree + types), so agents can identify the target request without needing --detail round-trips
  3. Remove arbitrary truncations on URL (80 char), body preview (60 char), and get html (50000 char) that hurt agent consumers
  4. TTL / invalidation for the cache so stale snapshots from previous sessions don't leak
  5. Per-tab workspace keys for future multi-tab support

The new PR will be co-authored with you (using the commit email from this branch) since you identified the root problem and the cache-to-disk direction is correct. Closing this one in favor of the consolidated version — will link it here when it's up.

@jackwener
Copy link
Copy Markdown
Owner

Heads up — opened #1100 as the full-featured replacement (per discussion on closing this one). You are credited as co-author on the commit. The new PR keeps your TTL/cache idea, adds stable keys (GraphQL operationName or METHOD host+pathname), and swaps the default listing from full bodies to a compact shape-only preview so agents are not paying body tokens unless they explicitly --detail the key. Thanks for the prototype — it shaped the direction.

jackwener added a commit that referenced this pull request Apr 20, 2026
* feat(browser): rewrite network command for agent-native discovery

Replace the index-based list + pretty-printed --detail flow with a
structured JSON interface built around stable keys, body-shape previews,
and a persistent capture cache. Agents can now reference captured
requests by operationName (GraphQL) or `METHOD host+pathname` (REST)
instead of array indexes that shift on every rerun.

- `browser network` now emits JSON: `{workspace, captured_at, count,
  filtered_out, entries: [{key, method, status, url, ct, size, shape}],
  detail_hint}` — no body payloads by default
- Shape inference (src/browser/shape.ts) walks response JSON into a
  flat path -> descriptor map with depth cap 6 and a 2KB budget per
  entry, so agents see structure without paying body tokens
- Stable key generator (src/browser/network-key.ts) derives
  `operationName` from graphql URLs and `METHOD host+pathname`
  elsewhere, disambiguating collisions with `#N` suffixes
- Persistent cache (src/browser/network-cache.ts) snapshots every
  capture to `~/.opencli/cache/browser-network/<workspace>.json` with
  a 24h TTL, so `--detail <key>` survives later commands
- `--detail <key>` returns `{key, url, method, status, ct, size, shape,
  body}` with structured error codes (cache_missing / cache_expired /
  cache_corrupt / key_not_found, the latter including available_keys)
- Add `--raw` for agents that want every full body inline, `--ttl` for
  cache lookups
- Update opencli-adapter-author + opencli-autofix skill docs to
  reference `--detail <key>` and the shape-first discovery flow

Supersedes the cache prototype in #1051.

Co-authored-by: freemandealer <freeman.zhang1992@gmail.com>

* fix(browser): structured errors for capture/save, shape budget guard

Self-review findings on the network refactor:

- captureNetworkItems throwing (browser crashed / CDP dropped) now emits
  `error.code: capture_failed` on stdout rather than leaking a bare
  stderr line from browserAction's generic handler — agents get a
  parseable JSON blob on every failure path, matching the design goal.
- saveNetworkCache throwing (disk full, read-only path) is a soft
  failure: the captured data is already in hand, so surface a
  `cache_warning` field in the envelope and keep going instead of
  aborting. `--detail` lookups on that run will miss the cache but the
  listing still reaches the agent.
- shape.ts: guard the sub-walk on `add()`'s return value so the
  "budget hits on the array/object descriptor itself" path can never
  emit a stray child without its parent marker.
- network-key.ts: document that `#N` suffixes start at `#2` — the first
  occurrence stays bare, there is no `#1`. Matches test + code.

Added regression tests: `capture_failed` on readNetworkCapture throw,
`cache_warning` on persistence failure, shape budget hit on array descriptor.

Co-authored-by: freemandealer <freeman.zhang1992@gmail.com>

---------

Co-authored-by: freemandealer <freeman.zhang1992@gmail.com>
luxiaolei pushed a commit to luxiaolei/OpenCLI that referenced this pull request Apr 21, 2026
…1100)

* feat(browser): rewrite network command for agent-native discovery

Replace the index-based list + pretty-printed --detail flow with a
structured JSON interface built around stable keys, body-shape previews,
and a persistent capture cache. Agents can now reference captured
requests by operationName (GraphQL) or `METHOD host+pathname` (REST)
instead of array indexes that shift on every rerun.

- `browser network` now emits JSON: `{workspace, captured_at, count,
  filtered_out, entries: [{key, method, status, url, ct, size, shape}],
  detail_hint}` — no body payloads by default
- Shape inference (src/browser/shape.ts) walks response JSON into a
  flat path -> descriptor map with depth cap 6 and a 2KB budget per
  entry, so agents see structure without paying body tokens
- Stable key generator (src/browser/network-key.ts) derives
  `operationName` from graphql URLs and `METHOD host+pathname`
  elsewhere, disambiguating collisions with `#N` suffixes
- Persistent cache (src/browser/network-cache.ts) snapshots every
  capture to `~/.opencli/cache/browser-network/<workspace>.json` with
  a 24h TTL, so `--detail <key>` survives later commands
- `--detail <key>` returns `{key, url, method, status, ct, size, shape,
  body}` with structured error codes (cache_missing / cache_expired /
  cache_corrupt / key_not_found, the latter including available_keys)
- Add `--raw` for agents that want every full body inline, `--ttl` for
  cache lookups
- Update opencli-adapter-author + opencli-autofix skill docs to
  reference `--detail <key>` and the shape-first discovery flow

Supersedes the cache prototype in jackwener#1051.

Co-authored-by: freemandealer <freeman.zhang1992@gmail.com>

* fix(browser): structured errors for capture/save, shape budget guard

Self-review findings on the network refactor:

- captureNetworkItems throwing (browser crashed / CDP dropped) now emits
  `error.code: capture_failed` on stdout rather than leaking a bare
  stderr line from browserAction's generic handler — agents get a
  parseable JSON blob on every failure path, matching the design goal.
- saveNetworkCache throwing (disk full, read-only path) is a soft
  failure: the captured data is already in hand, so surface a
  `cache_warning` field in the envelope and keep going instead of
  aborting. `--detail` lookups on that run will miss the cache but the
  listing still reaches the agent.
- shape.ts: guard the sub-walk on `add()`'s return value so the
  "budget hits on the array/object descriptor itself" path can never
  emit a stray child without its parent marker.
- network-key.ts: document that `#N` suffixes start at `#2` — the first
  occurrence stays bare, there is no `#1`. Matches test + code.

Added regression tests: `capture_failed` on readNetworkCapture throw,
`cache_warning` on persistence failure, shape budget hit on array descriptor.

Co-authored-by: freemandealer <freeman.zhang1992@gmail.com>

---------

Co-authored-by: freemandealer <freeman.zhang1992@gmail.com>
(cherry picked from commit 7fd8bd6)
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