Repository navigation
fix(session): emit [] instead of null from session list --json - #1742
Conversation
|
✅ OpenCodeReview: Review complete: 0 finding(s) across 1 selected item(s). |
abhisheksharma2411
left a comment
There was a problem hiding this comment.
Had a look through ListSessions and this is the only nil path - every other return builds make([]Summary, 0, len(entries)), so [] was already right everywhere else. Good, narrow fix.
Also agree with the reasoning in the test comment. Asserting on the wire text instead of unmarshalling is the only thing that actually pins this, since json.Unmarshal will happily accept null into a slice. That's an easy one to get wrong by writing the "nicer" test.
One aside, definitely not for this PR: internal/viewer/store.go has its own ListSessions that starts from var summaries []SessionSummary and can return nil the same way. It only feeds an HTML template today so nothing prints null, but it's the same shape if that ever grows a JSON endpoint.
A repository with no sessions directory answered null while one whose directory exists but is empty answered []: ListSessions returned a nil slice on the not-exist path, and encoding/json marshals nil as null. The two states are the same answer, and the function's own doc comment already promises "an empty slice with no error". TestListSessions_EmptyRepoReturnsNil checked len only despite its name, so it is renamed and strengthened to pin non-nil and [] on the wire; a cmd-level test asserts the raw --json output, the only place null is visible, because json.Unmarshal accepts null into a slice. Fixes alibaba#1696
cd38c43 to
f0d0154
Compare
|
Thanks for the careful read — confirming the nil-path audit was exactly the check I wanted this claim to survive. On the aside: agreed, and deliberately left out. I re-read |
abhisheksharma2411
left a comment
There was a problem hiding this comment.
Agreed on keeping it out, and thanks for actually going and reading internal/viewer/store.go rather than taking my word for it. Your description matches what I saw: nil slice after the sort, invisible today because it only feeds the template, and it would reproduce this exact bug the moment a JSON endpoint sat on top.
Scoping it out of a one-line fix is the right call.
NanaseInori
left a comment
There was a problem hiding this comment.
Reviewed all three changed files, the complete ListSessions return paths, #1696 and the discussion. The duplicate search found closed/unmerged #1699 and later open #1809 addressing the same return value; merged #1792 changes session show instead and does not resolve this list-path defect.
Returning []Summary{} only for os.IsNotExist makes successful empty results consistent with the existing initialized-slice path. Other filesystem errors are still returned, and enumeration/sorting behavior is unchanged. The unit test now checks both non-nil and encoded shape, while the command regression asserts literal [] rather than accepting null through slice unmarshalling. No blocking defect found.
This approval concerns the inspected implementation and regression design. I did not independently run the repository tests. The separate viewer collection discussed in the thread need not be changed as part of this narrowly scoped CLI JSON fix.
This review was conducted by NanaseInori's review bot, using the model GPT-6 Astra Pro. If you need a human review, please manually @NanaseInori.
Fixes #1696
What
ocr session list --jsonprintednullfor a repository whose sessions directory does not exist, and[]for one whose directory exists but is empty:Observed against the released 1.12.13 binary before any code was touched.
Why it matters
The two states are the same answer — "this repository has no sessions" — but JSON consumers see different types:
jq 'length'errors onnull, strict decoders reject it, and every caller must special-case a state that carries no information.ListSessions' own doc comment already promises "Missing directories return an empty slice with no error"; the not-exist path returned a nil slice instead, andencoding/jsonmarshals nil asnull.Change
One line at the source: the not-exist path in
internal/session/list.goreturns[]Summary{}instead ofnil, so every caller that encodes the result agrees. All existing callers uselen/rangeonly, so nil-vs-empty is invisible to them; the only observable change is the wire output this PR fixes. No command interface changes.Tests
TestListSessions_EmptyRepoReturnsNilcheckedlen == 0despite its name, so it is renamed toTestListSessions_MissingDirReturnsEmptySliceand strengthened to pin non-nil and[]afterjson.Marshal.TestRunSessionList_JSONMissingDirPrintsEmptyArrayasserts the raw--jsonstdout is[]— the only placenullis visible, sincejson.Unmarshalacceptsnullinto a slice and would let a decode-based test pass on the bug.Checks run locally
scripts/verify-license.sh,scripts/verify-english-only.go,go mod tidy,gofmt -s,go vetover all packages except/extensions/,go test -count=1over all packages,go build— all pass.makeand a C compiler are unavailable on this Windows box, sogo test -racewas not run locally; it is left to CI, as are the cross-compile and CodeQL jobs.AI disclosure (CONTRIBUTING, AI-Assisted Development)
This change was developed with AI assistance: the Qoder CLI coding agent helped locate the code, draft the diff and design the tests. I reproduced the behaviour against the released binary myself, ran every check above myself, reviewed every line before submitting, and can explain each hunk — the one-line return, the comment naming the JSON asymmetry, the renamed and strengthened unit test, and the new wire-text test. No commit attributes authorship to an AI/LLM.