Skip to content

Report a malformed trace line as the exit-2 document, not a traceback - #37

Merged
Shashankss1205 merged 1 commit into
mainfrom
fix/issue-15
Jul 31, 2026
Merged

Report a malformed trace line as the exit-2 document, not a traceback#37
Shashankss1205 merged 1 commit into
mainfrom
fix/issue-15

Conversation

@Shashankss1205

Copy link
Copy Markdown
Collaborator

What

grapharc trace, grapharc metrics and grapharc viz crashed with a raw pydantic ValidationError traceback (exit 1, empty stdout even in --json mode) when the trace file contained a line that is not a valid TraceEvent — a truncated final line from a process killed mid-write, a hand edit, any stray line. The CLI's own contract in grapharc/cli/output.py names "an unreadable trace" as EXIT_UNAVAILABLE = 2, and exit 1 is reserved for "the command ran and the answer was negative" — so scripts drew the wrong conclusion, and replay/diff answered the same broken file politely while their three siblings crashed.

How

  • grapharc/observe/trace.py: read_events now raises TraceReadError(path, line_number, cause) when a line does not parse, with a message naming the file and the 1-based line — unreadable trace file: /tmp/bad.jsonl: line 1 is not a trace event. It keeps raising rather than skipping: a partially-read audit trail presented as complete would be worse than a refusal. Exported from grapharc.observe alongside TraceRecorder.
  • grapharc/cli/main.py: _cmd_trace, _cmd_metrics and _cmd_viz catch it and route through fail(...) with the default EXIT_UNAVAILABLE, following the _existing_trace / ReplayError-handler shape already in the file. Text mode: error: … on stderr, empty stdout. JSON mode: the failure is the one document on stdout, stderr empty.
  • tests/test_cli.py: two parametrized tests over all three commands — text mode asserts exit 2 with the exact error: line naming the file and line number, JSON mode asserts exit 2 with exactly one {"ok": false, ...} document and empty stderr. Verified red with the source fix reverted (all six raise ValidationError on today's tree).

Out of scope, per the issue: replay/diff, skipping bad lines, the server's trace endpoints, and thread_summary's incremental index are untouched. Well-formed traces read byte-identically to before, so no README/cookbook transcript changes.

Verification

  • pytest: 1647 passed, 12 deselected (including the byte-compared README/cookbook transcripts)
  • ruff check .: clean
  • Manual repro from the issue: all three commands now exit 2 with the report, no traceback

Fixes #15

🤖 Generated with Claude Code

A trace file with one line that is not a TraceEvent — a process killed
mid-write, a hand edit — made `grapharc trace`, `grapharc metrics` and
`grapharc viz` crash with a raw pydantic ValidationError: exit 1,
nothing on stdout even in --json mode. The CLI's own contract in
grapharc/cli/output.py names "an unreadable trace" as EXIT_UNAVAILABLE
= 2, and exit 1 is reserved for "the command ran and the answer was
negative" — so a script watching a run concluded the run differed
rather than that the file was unreadable, while `replay` and `diff`
answered the same broken file politely.

`read_events` now raises TraceReadError(path, line_number, cause)
naming the file and the 1-based line — "unreadable trace file:
/tmp/bad.jsonl: line 1 is not a trace event" — instead of letting the
pydantic field listing escape. It still raises rather than skips: a
partially-read audit trail presented as complete would be worse than a
refusal. The three reading commands catch it and route through
fail(...) with the default EXIT_UNAVAILABLE, the same shape
_existing_trace and viz's ReplayError handler already use: text mode
puts `error: …` on stderr with empty stdout, JSON mode makes the
failure the one document on stdout with empty stderr. Well-formed
traces read exactly as before.

Fixes #15

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@Shashankss1205
Shashankss1205 merged commit 6f36c51 into main Jul 31, 2026
6 checks passed
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.

cli: a malformed trace file crashes trace/metrics/viz with a traceback instead of the exit-2 report

1 participant