Repository navigation
MARSOHS-2587: print logs as JSON with -o json, add --pretty - #2110
Conversation
`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>
|
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. |
|
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. |
|
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. |
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>
Fixed in 0c59a70, with the tests in 1c4187b.
|
Fixed in b927b13, with the test in 3ad1fbe.
|
Fixed in 6670545, with the tests in a1e2053.
|
|
Review notes (non-blocking). The logic looks correct: the
|
Co-authored-by: Cursor <cursoragent@cursor.com>
|
Re the review notes, thanks.
|
sanpj2292
left a comment
There was a problem hiding this comment.
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.



Summary
doctl harness-runtime logs <session> -o jsonignored the output flag and printed text, so tools likejqfailed on it.logsnow prints JSON when asked, for scripts and AI agents that read a session's history as data.-o jsonevent_id,run_id,session_id,timestamp,seq,source_event_id,source_event_type,typeanddata. Token chunks are not merged, and there is no colour or markdown. Stream health frames are skipped, as in text mode. Settingoutput: jsonin the config file works the same as passing the-o jsonflag.--pretty--pretty -o text-owith any value other thantextorjson-o json, doctl's standard{"errors":[...]}object on stdout and a non-zero exit, as for other commands. Otherwise the usual error on stderr.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 thantext/json) used to print text silently. It now exits non-zero.Known limitations
tenant_id,config_idortrace_parentwhen it decodes an event, so these fields are not printed, andlogs --helpsays so. Adding them needs a godo change.null.Testing
-o jsonline parsing, with no merging and no ANSI;--prettyindentation, and decoding as a JSON stream;-o jsonerrors 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;output: jsonproducing JSON lines;dataand timestamp;go test ./commands/passes exceptTestRegistryLogout, which also fails on unmodifiedmainin my environment (it needs the OS keychain).Refs: MARSOHS-2587
🤖 Generated with Claude Code