Repository navigation
Description
Problem
KubernetesService.read_pod_logs (src/devops_cli/k8s/service.py:272-302) returns the whole log as a str when follow=False, and a PodLogStream when follow=True (line 298). PodLogStream.__iter__ (service.py:37-43) yields each urllib3 chunk as it arrives and decodes each chunk on its own (line 40). Neither mode yields lines. The three callers each go wrong in a different way. Every case below was reproduced on the release/v0.2.25 tip (9c879ae) with mocked responses.
-
devops argo workflows logsprints one character per line by default.--followis off by default (src/devops_cli/commands/argo.py:440). The loop atargo.py:454-455iterates whateverread_pod_logsreturns. On astr, that walks the characters. It writes each one with a newline added, and writes each\nas a blank line. For a log of"a\nb\n", the output after the pod header is"a\n\nb\n\n". The loop dates from cb75261 (v0.2.22). -
Followed output splits lines at chunk boundaries. A chunk can hold several lines or end mid-line. Chunks
b"a\nb"andb"c\n"come out ofread_pod_logs(follow=True)as["a\nb", "c\n"].argo.py:455then adds a newline afterb, so the linebcprints as two lines.write_stdoutmasks secrets on each write (src/devops_cli/output/console.py:119-136), somask_secretsnever sees a token that is split across two chunks as a whole. -
A multi-byte character split across two chunks ends the stream. Chunks
b"caf\xc3"andb"\xa9\n"raiseUnicodeDecodeErroratservice.py:40. Each caller then fails differently:- the dashboard pane shows
stream ended: UnicodeDecodeError(src/devops_cli/ui/widgets.py:756-758); devops k8s logs --followfalls back tokubectl logs --follow(src/devops_cli/commands/k8s/cluster_context.py:335-344), which prints the tail a second time.
- the dashboard pane shows
-
Logs read without follow can come back as Python reprs. Without follow,
read_pod_logssets_preload_content=True(service.py:291). kubernetes 36.0.3 then runs the body throughjson.loadsbefore it callsstr()(kubernetes/client/api_client.py:280-285,611-624). So a log that is a single JSON document comes back as a Python repr:{"level": "info", "msg": "up"}\ncomes back as{'level': 'info', 'msg': 'up'};true\ncomes back asTrue.
--tail 1on any pod that logs JSON hits this. -
The dashboard log pane stores each chunk as one line.
PodLogSource(src/devops_cli/ui/refresh.py:105-145) passes the stream toLogPane._produce(widgets.py:742)._append(widgets.py:788-794) adds each item as a singleVirtualLogBufferentry (src/devops_cli/ui/log_buffer.py:56). A chunk that holds several lines becomes one entry with embedded newlines, so the status count, viewport and scrolling all treat it as one line. A line split across two chunks becomes two entries.refresh.py:132-133handles astrwithsplitlines(). That method also splits on\x0c,\x1c-\x1e,\x85and\u2028. -
devops k8s logs <pod>is a caller the original body missed. Its native path (cluster_context.py:319-336) prints with the consoleprintat levelraw. Thatprinttakes**kwargs(console.py:172) but never passes them toConsole.printon the raw path (console.py:299-302), soend=""is dropped. It also passes log text through as Rich markup, becausesafeis off. With mocked logs:- followed chunks
"a\nb"and"c\n"print as"a\nb\nc\n\n", which splitsbcand adds a blank line; - without follow, an extra blank line follows the log;
- a line longer than the console is hard-wrapped (at 80 columns when stdout is not a terminal);
- a line containing
[/bold]raisesMarkupError.except Exception: pass(cluster_context.py:335-336) swallows it, and the read runs again throughkubectl logs.
This caller has to change once
read_pod_logsyields lines anyway, becauseprint(str(log_output))(line 333) assumes astr. - followed chunks
No test reaches the argo log loop. tests/test_argo.py:226-229 passes --follow. #961 added tests/test_argo.py:286-328, which runs without it. But both tests mock a workflow with "nodes": {} (test_argo.py:192, 297), so workflow_pod_names returns no pods and the loop never runs. Two other sets of tests pin the current behaviour:
tests/test_k8s_service.py:153-165and307-323assert thestrreturn and the raw chunks;tests/test_ui_reactive.py:1775-1853replacesKubernetesServicewith fakes that already yield lines, so no test exercises the framing.
Since this was filed on 2026-10-01, only the line numbers have moved. read_pod_logs moved from 278 to 272. The argo loop moved from 458 to 454 after #961 removed _crd. #961 changed only how the argo commands choose a namespace, and the defect is unchanged.
Acceptance criteria
-
read_pod_logsreturns aPodLogStreamin both modes. Iterating it yields complete lines without their trailing newline. Both modes read the response unpreloaded (_preload_content=False) through one decode-and-split path. Pre-1.0, there is no shim: thestrreturn goes, and so do theisinstance(..., str)branches atrefresh.py:132-133andcluster_context.py:329. - Offline unit tests in
tests/test_k8s_service.pywith a mockedCoreV1Apiresponse:- followed chunks
b"a\nb"andb"c\n"yield["a", "bc"]; - chunks
b"a\n"andb"tail"yield["a", "tail"], so a final partial line is flushed when the stream ends; - chunks
b"caf\xc3"andb"\xa9\n"yield["café"]; - chunk
b"\xff\nok\n"yields["\ufffd", "ok"], and the stream does not end; - a chunk with no newline that is longer than the line cap yields a line of exactly the cap's length, and the rest follows as the next line;
"a\x0cb\n"yields one line, because only\nends a line;- without follow, a body of
b'{"level": "info"}\n'yields['{"level": "info"}'], and the call passesfollow=Falseand_preload_content=False.
- followed chunks
- The two
close()tests (test_k8s_service.py:350-392) still pass with["first"]and["only"]in place of"first\n"and"only\n". A read blocked on a quiet container still ends whenclose()is called from another thread, and the response is still closed and its connection released. This is the behaviour feat(k8s): truthful pod status, filters, container-aware logs and pod inspector in the dashboard (tab-k8s) #686 added. -
devops argo workflows logs my-wf: the test's workflow has onePodnode, so the loop runs, and the service is mocked.- Without
--follow, a log of"a\nb\n"prints exactlya\nb\nafter the pod header. - With
--follow, chunks"a\nb"and"c\n"print exactlya\nbc\n.
- Without
-
devops k8s logs pod-1, with and without--follow, writes each line throughwrite_stdout(no Rich markup or wrapping, secrets masked) with exactly one newline after it.- Chunks
"a\nb"and"c\n"printa\nbc\n. - A line containing
[/bold]prints literally. - A 200-character line prints unwrapped.
- No case calls the kubectl fallback (
run_subprocessand_run_cmdpatched and asserted not called).
- Chunks
- Dashboard: a
LogPanethat streamspod_log_source("web-0", "shop")reads a mockedCoreV1Apiresponse whose chunks areb"a\nb"andb"c\nd\n". Its buffer ends with the entries["a", "bc", "d"]. The test for the removedstrbranch (test_ui_reactive.py:1800-1815) goes. - Tests never touch the network or a cluster.
uv run devops cipasses.changelog.d/864.mdhas a### Fixedentry. - Person-run check, not in CI: on the workstation k3s cluster, pick a pod that has stopped writing, such as a Completed job pod.
diff <(uv run devops k8s logs <pod> -n <ns> --tail 50) <(kubectl logs <pod> -n <ns> --tail=50)prints nothing.- In
uv run devops dashboard, opening that pod's logs shows the same lines askubectl logs <pod> -n <ns> --tail=100(the pane's tail isDEFAULT_LOG_TAIL_LINES, 100), one row per line, and the status line readsN retained of Nwhere N is that line count.
Key questions
- Should lines keep their trailing newline? Proposed: no. Yield them without it, and have each writer add exactly one. The pane's
rstrip("\n")(widgets.py:793) then has nothing to do, and can stay or go. - What does
read_pod_logsreturn? Proposed: aPodLogStreamin both modes. Reading both modes unpreloaded removes thejson.loadsrepr defect (item 4), gives both modes one decoder and one splitter, and letsclose()work on a long non-follow read as well. Callers lose theirstrbranches. - What ends a line? Proposed:
\nonly, as the API andkubectl logsdo. A trailing\rstays in the line, askubectlprints it.str.splitlines()is not used, because it also splits on form feeds,\x1c-\x1e,\x85and\u2028. - How are bytes decoded? Proposed: a
codecsincremental UTF-8 decoder witherrors="replace", kept across chunks. A split character then decodes whole, and an invalid byte becomes U+FFFD instead of ending the stream. - Is a line's length bounded? Proposed: yes. Buffering adds a new memory risk: a container that writes without newlines, such as a
\rprogress bar or binary output, would grow the pending text without limit. A pending line that reaches a newDEFAULT_LOG_MAX_LINE_CHARS(65536, insrc/devops_cli/config/defaults.py) is yielded as a line, and reading continues. - Is a partial line flushed when the stream ends? Proposed: yes, whenever iteration ends without an exception, which includes an end of stream after
close(). When the read raises, the exception propagates as it does today, and the partial line is dropped. - Does the
devops k8s logswriter change here or in fix(cli): docker and sandbox output render remote text as rich markup #955? Proposed: here. This item has to rewrite those lines (cluster_context.py:329-333) for the new return type. The fix is the samewrite_stdoutthat argo already uses. fix(cli): docker and sandbox output render remote text as rich markup #955 covers only docker and sandbox output. - Does the kubectl fallback in
devops k8s logs(cluster_context.py:335-346) change? Proposed: no, it is out of scope. Once the lines are framed and written raw, a markup or decode error no longer triggers it. - Does this item make
devops argo workflows logswork against a real Argo install? No. That needs a separate item, so the argo criteria above use mocks. Two problems stop the command before it reaches these lines:workflow_pod_names(src/devops_cli/argo/crd.py:286-296) returns node IDs. Argo's default pod naming since v3.4 (POD_NAMES=v2) is<workflow>-<template>-<hash>, so a node ID is not a pod name and the read returns 404.- Argo pods run
waitandmaincontainers, andargo.py:454names no container, so the API server answers 400.
Order
Milestone v0.2.29 (P1, due before v0.3.0).
Depends on: #686 (closed in v0.2.24), which added PodLogStream and its close(). No open item in v0.2.25 to v0.2.29 changes service.py:25-302, argo.py:435-455, refresh.py:105-145, widgets.py:724-794 or cluster_context.py:289-346, so this item can land at any point in v0.2.29.
Related: #955 (v0.2.25, the same Rich-markup defect in docker and sandbox output), #548 (v0.2.26, the Loki path of devops k8s logs, cluster_context.py:254-286), #961 (closed, added the argo test that does not reach the loop). If the argo pod-name and container defect is filed, it lands after this item, because both change argo.py:450-455.
Update 2026-10-08: amended criteria (assessed against release/v0.2.29, 910b823)
The Problem claims hold, and the core defects were reproduced offline with kubernetes 36.0.3 and urllib3 2.8.0. Four points change the criteria.
write_stdoutis not a raw writer.console.py:130-143still prints through a Rich Console. That strips\r, BEL, BS, VT and FF, and turns:shortcode:text into emoji, so the key question's "\rstays in the line, as kubectl prints it" is false at the CLI.- The person-run
diffagainstkubectl logsuses a pod whose log has no control characters and no:shortcode:text. Alternatively the log writers stop rewriting those; if so, the task file says which was chosen.
- The person-run
- The default mode gets a new failure path. A non-follow read used to be preloaded inside
read_pod_logs, so a body error fell back to kubectl before anything was printed. Now the body is read inside the write loop._execute_legacy_kubectl_logs(cluster_context.py:319-346) wraps both the read and the loop inexcept Exception: passand then rerunskubectl logs, which would print the tail twice.- Only a failure to open the native read falls back to kubectl. An error after lines have been written is raised with its message: no swallowed error and no duplicate output. A test covers a mid-body error in non-follow mode.
- The line cap must not depend on chunk boundaries. "Reaches the cap" yields a spurious empty line when a line of exactly the cap's length has its
\nin the next chunk.- Pending text is cut only when it is longer than
DEFAULT_LOG_MAX_LINE_CHARS. A test case[b"x" * cap, b"\n"]yields exactly one line.
- Pending text is cut only when it is longer than
- Libraries first.
io.TextIOWrapper(resp, encoding="utf-8", errors="replace", newline="\n").readline(cap)is the stdlib way to frame lines from a streaming response.- Use it, unless the
close()criterion cannot hold with it. That would be because urllib3'sread1path differs fromread_chunkedon shutdown. If so, the task file records why the small framer was kept, with that evidence rather than mock fixtures.
- Use it, unless the
Smaller corrections:
- Remove
# noqa: C901from_execute_legacy_kubectl_logs. Its complexity measures exactly 10 after the change, andRUF100is selected.write_stdoutis already imported incluster_context.py. - With real urllib3,
shutdown(SHUT_RD)during a followed chunked read ends inProtocolError("Response ended prematurely"). So a realclose()drops a partial last line. Don't describe flushing afterclose()as product behaviour; it holds only for the mocks. - Per-line masking fixes tokens split across chunks, but it can no longer match the multi-line private-key pattern (
sanitizer.py:229-233) that a single-chunk write could match inargo --follow. Note this in the risk list.devops k8s logswas not masked at all before, so it still gains. - Citations that moved:
widgets.py_produceis at:778, the "stream ended" message at:810-812, and_append/rstripat:842-847. Inconsole.py,write_streamis at:130-143,print's**kwargsat:175, and the raw path at:304-305. - fix(cli): devops argo workflows logs finds no pod and names no container on a real argo install #988 (backlog) will later change the argo fixture that treats a workflow node id as a pod name, and
argo.py:450-455.
Out of scope here (noted for a later item):
k8s/doctor.py:605reads pod logs with preload and has the same JSON-repr defect with--tail 1. It also swallows errors withexcept Exception: pass.
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsDone
Activity
dan-petty commentedon Oct 2, 2026
Grooming (2026-10-02): Priority p2-medium → p1-high. In its default mode (no --follow),
devops argo workflows logsprints every character on its own line, which leaves the command unusable. Policy makes a broken command at least P1.Checked against the code at the release/v0.2.25 tip.
dan-petty commentedon Oct 2, 2026
Placed in v0.2.29 on 2026-10-02. v0.2.29 holds P1 work that has to land before v0.3.0, which v0.2.25 to v0.2.28 had no room for. Backlog P1 items were ranked by Value, then Effort, with defects and security fixes first at a tie. This item ranked in the top ten, and two of the release's twelve slots stay open for P1 items that surface before then.
dan-petty commentedon Oct 2, 2026
Refined to Ready on 2026-10-02 for v0.2.29. The body was checked claim by claim against release/v0.2.25 at 9c879ae, and a second agent then tried to refute it; every major objection was resolved in a revision. Cited lines were updated for code that moved, and the scope was corrected where merged work changed it. Acceptance criteria were tightened so each fails on the current code, and every key question now has a proposed answer.
dan-petty commentedon Oct 8, 2026
The acceptance criteria were amended on 2026-10-08 (see "Update 2026-10-08" at the end of the body). The changes:
write_stdoutis not raw, an error partway through the default mode must not print the tail twice, the line cap must not depend on chunk boundaries, and a stdlibio.TextIOWrappershould be tried before the hand-rolled framer.fix(k8s): stream-framing for pod logs (#864) (#1408)
dan-petty-bot commentedon Oct 8, 2026
What changed
#1408, merged into
release/v0.2.29as 01c5022.10 file(s) changed.
How it was verified
Check runs at 84e682b:
Acceptance Criteria of
docs/agent/tasks/task-864-stream-framing-pod-logs.md:read_pod_logsreturns aPodLogStreamin both modes. Iterating it yields complete lines without their trailing newline. Both modes read the response unpreloaded (_preload_content=False) through one decode-and-split path. Pre-1.0, there is no shim: thestrreturn goes, and so do theisinstance(..., str)branches atrefresh.py:132-133andcluster_context.py:329.tests/test_k8s_service.pywith a mockedCoreV1Apiresponse:b"a\nb"andb"c\n"yield["a", "bc"];b"a\n"andb"tail"yield["a", "tail"], so a final partial line is flushed when the stream ends;b"caf\xc3"andb"\xa9\n"yield["café"];b"\xff\nok\n"yields["\ufffd", "ok"], and the stream does not end;DEFAULT_LOG_MAX_LINE_CHARS: a test case[b"x" * cap, b"\n"]yields exactly one line;"a\x0cb\n"yields one line, because only\nends a line;b'{"level": "info"}\n'yields['{"level": "info"}'], and the call passesfollow=Falseand_preload_content=False.close()tests (test_k8s_service.py:350-392) still pass with["first"]and["only"]in place of"first\n"and"only\n". A read blocked on a quiet container still ends whenclose()is called from another thread, and the response is still closed and its connection released.devops argo workflows logs my-wf: the test's workflow has onePodnode, so the loop runs, and the service is mocked.--follow, a log of"a\nb\n"prints exactlya\nb\nafter the pod header.--follow, chunks"a\nb"and"c\n"print exactlya\nbc\n.devops k8s logs pod-1, with and without--follow, writes each line throughwrite_stdout(no Rich markup or wrapping, secrets masked) with exactly one newline after it."a\nb"and"c\n"printa\nbc\n.[/bold]prints literally.run_subprocessand_run_cmdpatched and asserted not called).LogPanethat streamspod_log_source("web-0", "shop")reads a mockedCoreV1Apiresponse whose chunks areb"a\nb"andb"c\nd\n". Its buffer ends with the entries["a", "bc", "d"]. The test for the removedstrbranch (test_ui_reactive.py:1800-1815) goes.uv run devops cipasses.changelog.d/864.mdhas a### Fixedentry.:shortcode:text).diff <(uv run devops k8s logs <pod> -n <ns> --tail 50) <(kubectl logs <pod> -n <ns> --tail=50)prints nothing.uv run devops dashboard, opening that pod's logs shows the same lines askubectl logs <pod> -n <ns> --tail=100(the pane's tail isDEFAULT_LOG_TAIL_LINES, 100), one row per line, and the status line readsN retained of Nwhere N is that line count.feat(release): v0.2.29 (#1455)