fix(browser): reuse last network snapshot for browser network --detail#1051
fix(browser): reuse last network snapshot for browser network --detail#1051freemandealer wants to merge 1 commit intojackwener:mainfrom
Conversation
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
2d0fb34 to
b38eaef
Compare
|
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 We've decided to fold this into a larger refactor of
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. |
|
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. |
* 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>
…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)
Summary
browser networksnapshot and reuse it forbrowser network --detail <index>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.tsnpm run typecheck