Skip to content

MARSOHS-2587: print logs as JSON with -o json, add --pretty - #2110

Merged
SSharma-10 merged 12 commits into
digitalocean:mainfrom
nveerdixit:marsohs-2587-logs-json
Nov 25, 2026
Merged

SSharma-10 merged 12 commits into
digitalocean:mainfrom
nveerdixit:marsohs-2587-logs-json

Conversation

@nveerdixit

@nveerdixit nveerdixit commented Nov 9, 2026 •

Copy link
Copy Markdown
Contributor

Summary

doctl harness-runtime logs <session> -o json ignored the output flag and printed text, so tools like jq failed on it. logs now prints JSON when asked, for scripts and AI agents that read a session's history as data.

Flags Behaviour
-o json Newline-delimited JSON: one object per stored event, one per line. Each object holds a subset of the API's event fields: event_id, run_id, session_id, timestamp, seq, source_event_id, source_event_type, type and data. Token chunks are not merged, and there is no colour or markdown. Stream health frames are skipped, as in text mode. Setting output: json in the config file works the same as passing the -o json flag.
--pretty Implies JSON and indents each event, so the output is no longer one object per line.
--pretty -o text Refused before anything is fetched.
-o with any value other than text or json Refused before anything is fetched.
Error before any event With -o json, doctl's standard {"errors":[...]} object on stdout and a non-zero exit, as for other commands. Otherwise the usual error on stderr.
Error partway through Lines already printed stay. The error goes to stderr, nothing extra goes to stdout, and the exit code is non-zero.
Text mode Unchanged.

The help text now states the history limits (the newest 2000 events or 4 MiB, from the last 14 days, with no marker when older events are dropped). It also says that JSON output includes the full prompt, tool arguments and log fields, which text output leaves out.

Behaviour changes

  • logs -o yaml (or any value other than text/json) used to print text silently. It now exits non-zero.
  • JSON output includes prompts and tool arguments that text mode hid. Take care when capturing it in CI logs.

Known limitations

  • The JSON is a subset of the event envelope. godo does not keep tenant_id, config_id or trace_parent when it decodes an event, so these fields are not printed, and logs --help says so. Adding them needs a godo change.
  • Timestamps are printed as UTC RFC 3339 with nanoseconds, re-encoded from the parsed time, so trailing zeros in fractional seconds are not kept. A missing timestamp prints as null.

Testing

  • New tests cover:
    • every -o json line parsing, with no merging and no ANSI;
    • --pretty indentation, and decoding as a JSON stream;
    • refusals with empty stdout, including through the real cobra flag parsing;
    • a mid-stream error that keeps earlier lines;
    • -o json errors before any event printing the standard error envelope on stdout (including a 402 with the add-funds message), and errors after events going to stderr;
    • a config-file output: json producing JSON lines;
    • the help naming every printed field and every dropped field;
    • integration tests against the built binary for the config-file case and the error envelope;
    • null data and timestamp;
    • text mode still merging token chunks.
  • go test ./commands/ passes except TestRegistryLogout, which also fails on unmodified main in my environment (it needs the OS keychain).

Refs: MARSOHS-2587

🤖 Generated with Claude Code

nveerdixit and others added 3 commits November 9, 2026 10:48
`doctl harness-runtime logs -o json` ignored the output flag and printed
text, so jq could not parse it. With -o json, logs now prints each stored
event as one JSON object per line, using the API's event field names.
--pretty indents each event and implies JSON. Output values other than
text and json are refused, as is --pretty with -o text. In JSON mode,
errors go to stderr so stdout holds only events, and the exit code is
non-zero.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The spell check flagged the split "hel" + "lo" chunks.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@nveerdixit

Copy link
Copy Markdown
Contributor Author

Live check against the API, built from this branch.

logs -o json: every line is one valid JSON event (76 of 76 parsed).

logs json

logs --pretty: indented JSON for reading.

logs pretty

--pretty -o text is refused before streaming, with nothing on stdout.

pretty refused

@BadhriRavi91

Copy link
Copy Markdown

JSON-mode errors bypass the repo’s -o json error envelope, so scripts parsing errors get nothing on stdout.

Every other doctl command tells scripts about failures in a standard JSON shape on stdout. This one prints a human-formatted error on stderr instead, so a script that handles errors the standard way will see an empty stdout and no reason for the failure. The fix is to use the standard error shape when no events have been printed yet, and only fall back to stderr once the stream has started.

@BadhriRavi91

Copy link
Copy Markdown

The JSON schema is hand-rolled and silently incomplete, but the help text promises “the API’s event field names”.

The JSON looks like a complete copy of what the server sent, but a few fields are missing because the library drops them. Since scripts will rely on this format, the help text should say it is a subset, and the field names should be settled before release.

@BadhriRavi91

Copy link
Copy Markdown

Output vs outputFormat() mismatch: a config-file output: json makes logs print text while errors are rendered as JSON.

doctl can learn the user’s chosen format from the flag or from their config file. This code looks at only one of those. Someone with output: json in their config could get text logs, or the wrong error format. Use the shared “what format did the user ask for?” helper, and add a test for the config-file case.

nveerdixit and others added 6 commits November 20, 2026 20:49
logs reads the -o flag variable, which a config file never sets, while
checkErr reads viper. With output: json in the config file, logs prints
text. These tests fail until logs reads the same value as checkErr.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
logs chose text or JSON from the -o flag variable, but checkErr renders
errors from outputFormat, which also reads the config file. A config
file with output: json then gave text logs and JSON errors. logs now
reads outputFormat too, so both agree.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Under -o json, logs printed every error on stderr and left stdout empty,
so a script that parses doctl's {"errors":[...]} envelope saw nothing.
These tests expect the envelope on stdout when no event has been printed
yet, and stderr only once events are on stdout.

The checkErr harness now mirrors the runner: it beautifies the error,
sets -o in both Output and viper, and returns the process's whole stdout.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
logs now returns an error to the runner when no event has reached
stdout yet, so checkErr prints doctl's standard {"errors":[...]}
envelope under -o json. Once an event is on stdout, the error still goes
to stderr with a non-zero exit, so stdout holds only events.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
logs -o json prints a subset of the server's event envelope: godo drops
tenant_id, config_id and trace_parent while decoding. This test feeds a
frame with every envelope field and fails until the help names each
field logs prints and each one it leaves out.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The help now names the fields logs -o json prints, the three it leaves
out (tenant_id, config_id, trace_parent: godo drops them while
decoding), and which ones are omitted when empty or printed as null. It
also says where errors go under -o json, before and after the first
event.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@nveerdixit

Copy link
Copy Markdown
Contributor Author

Re the JSON error envelope:

JSON-mode errors bypass the repo's -o json error envelope

Fixed in 0c59a70, with the tests in 1c4187b.

  • Before any event. An error raised before any event is printed now goes back to the runner and through checkErr. So with -o json it comes out as the standard {"errors":[...]} envelope on stdout, a 402 included. The runner rewrites the error once, so a 402 still says "add funds".
  • After events. Once events are on stdout, the error still goes to stderr with a non-zero exit, so the stream holds only events.
  • Tests. 1c4187b includes an integration test that sends a 404 to the built binary.
  • --pretty alone. With only --pretty, the error stays as text on stderr. checkErr sees the text format there, and --pretty is the flag for reading output.

@nveerdixit

Copy link
Copy Markdown
Contributor Author

Re the hand-rolled schema:

the help text should say it is a subset, and the field names should be settled before release

Fixed in b927b13, with the test in 3ad1fbe.

  • Help text. logs --help now says the JSON is a subset of the API's event fields. It lists the nine fields it prints and names the three it leaves out: tenant_id, config_id and trace_parent. godo drops those three when it decodes an event, so adding them needs a godo change first.
  • Test. It feeds a frame that contains every field the API sends. It fails if the printed fields and the help ever disagree, and that keeps the names settled.

@nveerdixit

Copy link
Copy Markdown
Contributor Author

Re Output vs outputFormat():

Use the shared "what format did the user ask for?" helper, and add a test for the config-file case.

Fixed in 6670545, with the tests in a1e2053.

  • Fix. logs now decides its format with outputFormat(), the same helper checkErr uses. So output: json in the config file now gives JSON logs and JSON errors together.
  • Tests. a1e2053 adds a unit test and an integration test. The integration test runs the built binary with a config file containing output: json. Both fail on the previous head.
  • Other commands. CmdConfig.Display and several other agents commands still read Output directly, so they have the same mismatch. That is outside this PR, and I can file it separately.

@sanpj2292

Copy link
Copy Markdown
Contributor

Review notes (non-blocking). The logic looks correct: the -o json error envelope is used before the first event, and after it errors go to stderr with a non-zero exit.

  1. Help hard-codes server limits. commands/agents_help.go:167 says "newest 2000 events or 4 MiB, from the last 14 days". doctl doesn't enforce these, so the text goes stale when the server changes. Suggest keeping the old wording ("very old or long histories may show only recent events").
  2. --pretty may not be needed. commands/agents.go:573 adds a flag plus conflict rules (-o text vs --pretty) for what doctl ... logs -o json | jq . already does. Consider dropping it to keep the surface smaller.
  3. Sensitive data in JSON. The help says JSON includes the full prompt and tool arguments, which text output leaves out. Tool arguments can carry secrets the agent used, and nothing is masked, even though doctl masks secrets elsewhere unless --show is passed. Worth a quick security sign-off.

@sdharavath sdharavath left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Co-authored-by: Cursor <cursoragent@cursor.com>
@nveerdixit

Copy link
Copy Markdown
Contributor Author

Re the review notes, thanks.

  1. Hard-coded server limits. Fixed in d2379f2. The help now says "Very old or long histories may show only recent events", the wording from before this PR, and still notes that dropped events leave no marker. doctl does not enforce those limits, so the numbers would have gone stale.
  2. --pretty. I'm keeping it. It saves a jq install for someone reading a history by hand, and the conflict rule is one check: --pretty -o text is refused before streaming, with nothing on stdout. Scripts don't need it, because -o json stays one object per line. If we'd rather keep the surface smaller before release, it's a small removal.
  3. Sensitive data in JSON. Unchanged here. JSON is opt-in, and the help says that it includes the full prompt, tool arguments and log fields. Masking would mean picking which fields to redact inside free-form tool arguments, so I'd rather get the security sign-off you suggest than guess at a rule in this PR.

@sanpj2292 sanpj2292 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. The hard-coded server limits are gone from the help (d2379f20), and keeping --pretty is fine.

One open item, for reviewers rather than the author: a security sign-off on JSON output carrying the full prompt and tool arguments unmasked. The help already says so, and it's opt-in through -o json.

Merge note: this PR conflicts with #2112 and #2115 in commands/agents_test.go.

@SSharma-10
SSharma-10 merged commit 9da0fb1 into digitalocean:main Nov 25, 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.

6 participants