Skip to content

feat(session): filter listed sessions by branch - #1702

Merged
lizhengfeng101 merged 7 commits into
alibaba:mainfrom
OUAO-FRANK:feat/1701-session-branch-filter
Nov 16, 2026
Merged

lizhengfeng101 merged 7 commits into
alibaba:mainfrom
OUAO-FRANK:feat/1701-session-branch-filter

Conversation

@OUAO-FRANK

Copy link
Copy Markdown
Contributor

Summary

  • add an exact --branch filter to ocr session list
  • apply the branch filter before --limit
  • preserve table and JSON output, including a branch-specific empty message
  • add regression coverage for filtering and limit ordering

Validation

  • git diff --check passed
  • make check, make test, and the required ocr review --audience agent could not run locally because Go, Make, and ocr are not installed in the environment
  • GitHub Actions will provide the full build and test validation

AI disclosure

This PR was developed with GPT-5.6 through Codex, using Git and GitHub CLI. I reviewed the complete diff and validation results before submission.

Fixes #1701

@github-actions

Copy link
Copy Markdown
Contributor

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

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

At fafa8bb, ListSessions returns a nil slice when the sessions directory does not exist. The new filtered := summaries[:0] preserves that nil slice, so ocr session list --repo <unused-directory> --branch main --json still emits null, whereas #1701 requires [] for an empty filtered result.

Please initialize the filtered result as a non-nil empty slice and add a regression for a missing sessions directory. The existing empty-result test only checks text output.

Also, the filter-before-limit test selects the newest session, so it would still pass if limiting happened first. Select an older matching session behind a newer non-matching one to exercise that ordering.

@OUAO-FRANK

Copy link
Copy Markdown
Contributor Author

Fixed in 55cf505. The branch-filter regression now places an older matching session behind a newer non-matching session, so it verifies filtering happens before --limit. The non-nil empty JSON result and missing-sessions-directory regression are already included on this branch. Targeted and full cmd/opencodereview tests pass.

@OUAO-FRANK

Copy link
Copy Markdown
Contributor Author

Both points are addressed on the branch:

  1. Non-nil empty result. d8818b4 initializes filtered as make([]session.Summary, 0, len(summaries)) instead of reslicing, so a missing sessions directory now yields [] for --json.
  2. Regression for the missing directory. TestRunSessionList_BranchFilterJSONMissingSessionsDir runs --repo <empty-temp-dir> --branch main --json and asserts the output is exactly []. Checked that it bites: with the old summaries[:0] in place it fails with expected a non-nil empty JSON array, got "null\n".
  3. Filter-before-limit ordering. 55cf505 flips the fixture so the matching session (feature) is the older one and the newer session is on main, then asks for --branch feature --limit 1. Applying the limit first would select main and filter to nothing, so the test now exercises the order rather than passing either way.

gofmt -l is clean and go test ./cmd/opencodereview/ -run 'TestRunSessionList|TestRunSessionShow' passes.

Your first finding was right and my earlier test was too weak on both counts — thanks for the precise repro.

@OUAO-FRANK

Copy link
Copy Markdown
Contributor Author

Both points are already in the current head (265c0cb); the review is against fafa8bb.

Non-nil empty slice. runSessionList no longer slices the input in place — the filter allocates:

if sessionListBranch != "" {
	filtered := make([]session.Summary, 0, len(summaries))
	for _, summary := range summaries {
		if summary.GitBranch == sessionListBranch {
			filtered = append(filtered, summary)
		}
	}
	summaries = filtered
}

TestRunSessionList_BranchFilterJSONMissingSessionsDir covers exactly the repro you gave (--repo <unused dir> --branch main --json) and asserts the output is [], not null.

Filter before limit. TestRunSessionList_BranchFilterBeforeLimit now creates the matching feature session first (older) and a newer main session second, then runs --branch feature --limit 1. If limiting ran before filtering, the newest entry would be main and the result would be empty; the test asserts one feature entry.

$ GOTOOLCHAIN=go1.26.9 go test ./cmd/opencodereview -run 'TestRunSessionList_BranchFilter' -count=1
ok  	github.com/alibaba/open-code-review/cmd/opencodereview	1.864s

Could you re-review at 265c0cb?

@lizhengfeng101

Copy link
Copy Markdown
Contributor

@OUAO-FRANK Thanks for the updates. I re-reviewed the current head, 265c0cbe54b757135cc3528f44e8c687017078dd, rather than fafa8bb. The non-nil filtered slice and missing-sessions-directory regression are present, so the earlier null versus [] finding is resolved. The production filter also correctly runs before the limit.

One regression-test issue remains:

Make the filter-before-limit fixture deterministic. In TestRunSessionList_BranchFilterBeforeLimit, creating feature first and main second does not guarantee that main is newer in the persisted data. WriteSessionStart stores timestamps with second-resolution time.RFC3339, while ListSessions sorts only by that timestamp. Both sessions can therefore have equal persisted times; their random UUID filenames can leave the matching session first. In that case, an incorrect implementation that limits before filtering would still pass this test.

Please use fixtures with explicit, distinct persisted timestamps and assert that the unfiltered list starts with the newer, non-matching session. Moving the limit ahead of the filter should then make the regression fail deterministically. This is a gap in the test's regression protection, not a claim that the current production implementation fails or that the current test is flaky with the correct implementation.

Two additional follow-ups:

  • Integration prerequisite: GitHub currently reports merge conflicts against main (mergeStateStatus: DIRTY). Please resolve them and rerun CI on the resulting head; the existing head's green CI does not cover that future resolution.
  • Documentation suggestion: The CLI reference still omits --branch. Please consider adding its exact, case-sensitive matching semantics and an example combining it with --limit.

Validation scope: I inspected all three changed files and the relevant persistence/sorting paths. OCR completed with zero findings for the production file, but excluded the two test files. I also verified that CI passed for this exact head; I did not run the PR's tests locally.

Review assistance: Codex and OpenCodeReview (OCR model: gpt-6-astra).

@lizhengfeng101

Copy link
Copy Markdown
Contributor

@OUAO-FRANK I checked that your branch was still at 265c0cbe, then pushed the maintainer follow-up in 62e9f92 without rewriting your commits.

This update:

  • Merges current main (9b3e6a56) and resolves the test conflict while retaining main's missing-session-directory regression.
  • Uses explicit, distinct persisted timestamps and checks the initial ordering, so the filter-before-limit regression is deterministic.
  • Adds coverage for exact/case-sensitive matching, multiple matches and ordering, unlimited results, missing branch metadata, omitted/empty filters, empty JSON results, and table output.
  • Documents --branch, its aliases, filtering before --limit, and the saved-metadata/directory behavior.

Validation of the pushed tree:

  • make check, the full root-module test suite with the race detector, and make build passed.
  • make coverage passed with 91.7% total coverage.
  • All four session/sessions and list/ls combinations passed built-CLI smoke checks with --branch.
  • As a mutation check, moving the limit before the filter made the regression fail in 10/10 runs; the correct implementation was restored before final validation.
  • OCR completed with zero findings in its selected production-file scope; the tests and documentation were checked separately.

The previous review's remaining test concern is addressed. Checks on this new head should be used for the merge decision; I have not merged the PR.

Review and implementation assistance: Codex and OpenCodeReview (OCR model: gpt-6-astra).

@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 b71e455 into alibaba:main Nov 16, 2026
15 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.

feat(session): let me filter saved sessions by branch

3 participants