Skip to content

fix(session): emit [] instead of null from session list --json - #1742

Merged
lizhengfeng101 merged 2 commits into
alibaba:mainfrom
castorhrio:fix/session-list-empty-json
Nov 12, 2026
Merged

lizhengfeng101 merged 2 commits into
alibaba:mainfrom
castorhrio:fix/session-list-empty-json

Conversation

@castorhrio

Copy link
Copy Markdown
Contributor

Fixes #1696

What

ocr session list --json printed null for a repository whose sessions directory does not exist, and [] for one whose directory exists but is empty:

$ ocr session list --json --repo <repo with no sessions dir>
null
$ mkdir -p ~/.opencodereview/sessions/<slug>   # same repo, still no sessions
$ ocr session list --json --repo <same repo>
[]

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 on null, 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, and encoding/json marshals nil as null.

Change

One line at the source: the not-exist path in internal/session/list.go returns []Summary{} instead of nil, so every caller that encodes the result agrees. All existing callers use len/range only, 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_EmptyRepoReturnsNil checked len == 0 despite its name, so it is renamed to TestListSessions_MissingDirReturnsEmptySlice and strengthened to pin non-nil and [] after json.Marshal.
  • New TestRunSessionList_JSONMissingDirPrintsEmptyArray asserts the raw --json stdout is [] — the only place null is visible, since json.Unmarshal accepts null into a slice and would let a decode-based test pass on the bug.
  • Both fail without the one-line fix (verified by stashing it and re-running).

Checks run locally

scripts/verify-license.sh, scripts/verify-english-only.go, go mod tidy, gofmt -s, go vet over all packages except /extensions/, go test -count=1 over all packages, go build — all pass. make and a C compiler are unavailable on this Windows box, so go test -race was 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.

@CLAassistant

CLAassistant commented Oct 13, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@github-actions

Copy link
Copy Markdown
Contributor

✅ OpenCodeReview: Review complete: 0 finding(s) across 1 selected item(s).

@abhisheksharma2411 abhisheksharma2411 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.

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
@castorhrio
castorhrio force-pushed the fix/session-list-empty-json branch from cd38c43 to f0d0154 Compare October 14, 2026 11:36
@castorhrio

Copy link
Copy Markdown
Contributor Author

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 internal/viewer/store.go while writing this: its ListSessions starts from var summaries []SessionSummary, so a repo dir with no .jsonl entries returns a nil slice after the sort. Today handler.go only feeds the HTML template, where nil ranges to nothing, so there is no visible null; a JSON endpoint on top of it would reproduce this PR's bug exactly. I kept it out here because mixing it in would make this a two-contract change instead of a one-line contract fix — happy to track it separately if you think it warrants an issue.

@abhisheksharma2411 abhisheksharma2411 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.

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 NanaseInori left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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.

@lizhengfeng101 lizhengfeng101 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

@lizhengfeng101
lizhengfeng101 merged commit 6386895 into alibaba:main Nov 12, 2026
12 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.

bug(session): session list --json prints null for a repo with no sessions, but [] once the directory exists

5 participants