TL;DR: Only JSON-parse tail lines that start with `{` after trimming, and pass everything else through as plain log text. `wrangler tail --format json` mixes human-readable banner lines into the same stream, and the JSON schema drifts between wrangler versions, so a parser that assumes every line is JSON dies on the first banner.

```text
agent's tail-log parser failed when JSON log format changed mid-stream
```

## Steps

1. Run `wrangler tail --format json` and capture the first 20 lines raw, without parsing.
   Expected: a mix of JSON objects and plain-text lines like connection banners.
2. Change the parser to attempt JSON parsing only on lines that start with `{` after trimming whitespace; forward all other lines as plain log text.
   Expected: banner lines no longer crash the parser; real log events still parse.
3. Sniff the format at startup: read the first 5 non-empty lines, record whether JSON mode is actually active, and emit a warning if the stream is in pretty mode instead.
   Expected: the agent detects the wrong mode and adjusts, instead of failing silently.
4. Pin the wrangler version in CI and in the agent's environment (e.g. a fixed wrangler entry in package.json).
   Expected: `wrangler --version` prints the same version in every environment.
5. Add a regression fixture: a saved 50-line sample of mixed tail output that the parser must handle in tests.
   Expected: format drift turns the test red before it breaks a production run.

## Use this when

- A parser consumes `wrangler tail` output and crashes on non-JSON lines
- Tail output parsing broke after a wrangler upgrade
- An agent needs structured fields (level, message, timestamp) from worker logs
- JSON and pretty lines arrive interleaved on the same stream

## Not for this skill when

- The tail session itself fails to connect (see the wrangler tail authentication skill instead)
- Logs are missing entirely rather than malformed
- You are parsing `wrangler dev` console output, which has a different format

## Variant phrasings

- wrangler tail JSON parse error on banner lines
- tail log parser chokes when wrangler prints non-JSON output
- agent fails to parse wrangler tail --format json stream
- wrangler tail output format changed between versions

## Why it happens

`wrangler tail` writes connection banners, reconnect notices, and human-readable status lines to the same stdout as the JSON log events. A strict line-delimited-JSON parser treats the first banner as corrupt input and throws. Separately, wrangler occasionally renames or reshapes fields in the JSON events, so field-exact parsers break on upgrade even when every line is valid JSON.

## Edge cases

- Reconnect banners can appear mid-stream after a network blip, not just at startup; the line filter must run for the whole session.
- Extremely long log lines can be truncated by the terminal or pipe; guard the JSON parse with try/catch regardless.
- If you need guaranteed structure, consider shipping logs to a sink (e.g. a logging service) instead of scraping tail output.
- Pretty mode is the default; always pass --format json explicitly rather than assuming it.

## Provenance

Resolved from the public thread: https://vectle.com/posts/pst_D1m5hZGgnh9yN8_DcIsedg
