alibaba/open-code-review · error

finalize session: %w

Error message

finalize session: %w

What it means

When the scan finds no reviewable files, Run still persists the session end (session_end must reach disk so the run can be 'claimed'). If session.Finalize() fails during that clean-skip path, the skip is abandoned and this 'finalize session: %w' error is returned, signaling the delivery contract was not satisfied.

Source

Thrown at internal/scan/agent.go:343

	a.injectScanContentMap()
	a.args.Tools.Freeze()

	totalDiscovered := len(a.items)
	a.items = a.filterScanItems(a.items)
	a.items = a.filterLargeScans(a.items)

	reviewable := len(a.items)
	fmt.Fprintf(stdout.Writer(), "[ocr] full-scan: %d file(s) discovered, reviewing %d in %s\n",
		totalDiscovered, reviewable, a.args.RepoDir)

	if reviewable == 0 {
		fmt.Fprintln(stdout.Writer(), "[ocr] No reviewable files. Skipping scan.")
		telemetry.Event(ctx, "scan.no.files")
		// A clean skip still has to reach disk: if session_end never persisted,
		// the skip cannot be claimed. Scan has no manifest builder, but the
		// session_end delivery contract still applies.
		if ferr := a.session.Finalize(); ferr != nil {
			return []model.LlmComment{}, fmt.Errorf("finalize session: %w", ferr)
		}
		return []model.LlmComment{}, nil
	}

	// Pre-run cost projection so users aren't surprised by a large scan.
	est := estimateCost(a.items, a.planEnabled(), a.dedupEnabled(), a.summaryEnabled())
	fmt.Fprintf(stdout.Writer(), "[ocr] estimated cost: %s\n", est)
	if a.args.MaxTokensBudget > 0 {
		fmt.Fprintf(stdout.Writer(), "[ocr] token budget: %s (dispatch stops once exceeded)\n", humanTokens(a.args.MaxTokensBudget))
		if est.TotalTokens > a.args.MaxTokensBudget {
			fmt.Fprintf(stdout.Writer(), "[ocr] WARNING: estimate (%s) exceeds budget (%s); scan will stop partway\n",
				humanTokens(est.TotalTokens), humanTokens(a.args.MaxTokensBudget))
		}
	}

	a.currentDate = time.Now().Format("2006-01-02 15:04")
	telemetry.Event(ctx, "scan.started",
		telemetry.AnyToAttr("file.count", totalDiscovered),

View on GitHub (pinned to 5cf97d0d15)

Solutions

  1. Check writability and free space of the directory where ocr persists session state (fix permissions or free disk space)
  2. Re-run the scan after fixing storage; a persisted session_end is required for the run to be claimable
  3. Investigate the wrapped ferr for the exact OS-level cause (path, errno)

Example fix

// before — state dir not writable
$ ocr review  # finalize session: open .../session_end.json: permission denied
// after
$ chmod u+w ~/.ocr/state && ocr review
Defensive patterns

Strategy: try-catch

Validate before calling

// ensure the state directory is writable before scanning
dir := sessionStateDir()
if err := os.MkdirAll(dir, 0o755); err != nil { return err }
probe := filepath.Join(dir, ".write_probe")
if err := os.WriteFile(probe, nil, 0o644); err != nil {
    return fmt.Errorf("state dir %s not writable: %w", dir, err)
}
os.Remove(probe)

Try / catch

comments, err := agent.Run(ctx)
if err != nil && strings.Contains(err.Error(), "finalize session") {
    // session_end did not reach disk; fix storage/permissions and re-run
    // so the run can be claimed
}

Prevention

When it happens

Trigger: A scan where all files were filtered out ("No reviewable files. Skipping scan.") while a.session.Finalize() fails — typically a filesystem write failure to the session/state directory.

Common situations: Read-only filesystem or full disk where ocr persists session state; permissions problem on the state directory; state directory removed concurrently (e.g. another ocr process cleaned it).

Related errors


AI-assisted analysis of alibaba/open-code-review@5cf97d0d15 (2026-09-02). Data as JSON: /api/errors/910b7cb1b0884ddc. Report an issue: GitHub.