open-code-review/internal/session
历代星辰 e95bdda4f2
Some checks are pending
CI / test (push) Waiting to run
CI / windows (push) Waiting to run
CI / cross-compile (amd64, darwin) (push) Waiting to run
CI / cross-compile (amd64, windows) (push) Waiting to run
CI / cross-compile (arm64, darwin) (push) Waiting to run
CI / cross-compile (arm64, linux) (push) Waiting to run
CI / cross-compile (arm64, windows) (push) Waiting to run
CodeQL Advanced / Analyze (go) (push) Waiting to run
CodeQL Advanced / Analyze (actions) (push) Waiting to run
CodeQL Advanced / Analyze (javascript-typescript) (push) Waiting to run
Deploy Pages / build (push) Waiting to run
Deploy Pages / deploy (push) Blocked by required conditions
feat(cli): add --output flag to write review/scan results to a file (#852)
* feat(cli): add --output flag to write review/scan results to a file

Add `--output <path>` / `-o` to `ocr review` and `ocr scan`, writing the
result JSON or text directly to a UTF-8 file instead of stdout. The file
is created lazily on the first write so a failed run never truncates an
existing target; text-mode files are ANSI-stripped so terminal color
codes never pollute the result file. A `[ocr] Results written to <path>`
hint is printed to stderr once the file is actually created, and failure
output keeps going to stderr so agents always find the failure reason.

Closes #851

Signed-off-by: 历代星辰

* test(cli): cover --output flag parsing and file output behavior

Add tests for --output/-o flag parsing, the stripAnsiWriter state machine
(including escape sequences split across Write calls), lazy file creation
(failed runs leave existing targets untouched, never-written targets are
not created), and the Results-written stderr hint. Adapt existing
emitRunResult / renderComment / outputPreview call sites to the new
io.Writer parameter.

Signed-off-by: 历代星辰

* fix(test): isolate USERPROFILE so Windows tests never touch the real OCR home

os.UserHomeDir() prefers USERPROFILE over HOME on Windows, so
t.Setenv("HOME", ...) alone left tests reading and writing the
developer's real ~/.opencodereview: config tests overwrote config.json
and session/agent tests polluted the sessions store. Add a setTestHome
helper (per affected package) that also overrides USERPROFILE, and route
every scattered HOME override through it.

Signed-off-by: 历代星辰

* fix(cli): propagate output write failures and strip multi-byte ANSI escapes

Address the ocr review findings on the --output feature:
- lazyFileWriter now records the first write error (Err()) and emits the
  "Results written" hint only after a successful write; emitRunResult and
  outputPreview check it after text rendering, so a failed --output write
  (permission, disk full) exits non-zero like JSON mode already does
  instead of silently exiting 0 with no file.
- stripAnsiWriter keeps multi-byte escapes (ESC + intermediate byte,
  DCS/PM/APC strings) inside the escape state so trailing bytes are
  discarded with the sequence instead of leaking into the result file.

Signed-off-by: 历代星辰

* fix(cli): re-parse trailing byte after bare-ESC OSC termination and fix Write return semantics

Addresses review comments on #852:

- stripAnsiWriter ansiOSCEsc: a non-ST byte after ESC is no longer dropped.
  The OSC ends at a bare ESC terminator and the trailing byte is re-parsed —
  an ESC starts a new escape sequence, any other byte is forwarded as text.
  Previously the first byte after a bare-ESC-terminated OSC was silently lost.
- stripAnsiWriter Write: report the underlying dst error but return len(p),
  since the state machine has already consumed the input; returning 0 made a
  caller retrying on n < len(p) feed the same bytes through twice.
- Clarify --output help text: default is stdout and '-' also means stdout.

Adds regression tests for both bugs (bare-ESC + trailing text, bare-ESC + new
escape, and dst-failure return contract).

Signed-off-by: 历代星辰

* fix(cli): reinforce ANSI stripper state machine and validate format flag

* docs(cli): document output flag across localized references and READMEs

* fix(cli): cap escape intermediate bytes and normalize format in output handlers

---------

Signed-off-by: 历代星辰
2026-08-24 15:37:29 +08:00
..
comments.go chore: add SPDX license headers and automated verification (#740) 2026-08-05 21:26:27 +08:00
comments_test.go feat(cli): add --output flag to write review/scan results to a file (#852) 2026-08-24 15:37:29 +08:00
final_manifest_test.go test: raise statement coverage to 90% and enforce it in CI (#747) 2026-08-06 12:55:44 +08:00
history.go feat(resume): add trusted resume validation and transition lineage (#786) (#845) 2026-08-14 11:31:40 +08:00
history_test.go chore: add SPDX license headers and automated verification (#740) 2026-08-05 21:26:27 +08:00
list.go feat(resume): add trusted resume validation and transition lineage (#786) (#845) 2026-08-14 11:31:40 +08:00
list_error_test.go feat(cli): add --output flag to write review/scan results to a file (#852) 2026-08-24 15:37:29 +08:00
list_more_test.go test: raise statement coverage to 90% and enforce it in CI (#747) 2026-08-06 12:55:44 +08:00
list_test.go feat(cli): add --output flag to write review/scan results to a file (#852) 2026-08-24 15:37:29 +08:00
manifest.go chore: add SPDX license headers and automated verification (#740) 2026-08-05 21:26:27 +08:00
manifest_guards_test.go test: raise statement coverage to 90% and enforce it in CI (#747) 2026-08-06 12:55:44 +08:00
manifest_test.go chore(ci): fail CI when unapproved non-English text appears in source files (#876) 2026-08-13 14:43:55 +08:00
persist.go feat(resume): add trusted resume validation and transition lineage (#786) (#845) 2026-08-14 11:31:40 +08:00
persist_test.go feat(cli): add --output flag to write review/scan results to a file (#852) 2026-08-24 15:37:29 +08:00
resume.go feat(resume): add trusted resume validation and transition lineage (#786) (#845) 2026-08-14 11:31:40 +08:00
resume_identity.go feat(resume): add trusted resume validation and transition lineage (#786) (#845) 2026-08-14 11:31:40 +08:00
resume_identity_test.go feat(resume): add trusted resume validation and transition lineage (#786) (#845) 2026-08-14 11:31:40 +08:00
resume_orphan_request_test.go Feat/llm retry report:SDK retry-attempt observability for review (#785) (#790) 2026-08-13 14:31:18 +08:00
resume_test.go feat(cli): add --output flag to write review/scan results to a file (#852) 2026-08-24 15:37:29 +08:00
test_home_test.go feat(cli): add --output flag to write review/scan results to a file (#852) 2026-08-24 15:37:29 +08:00
testing.go chore: add SPDX license headers and automated verification (#740) 2026-08-05 21:26:27 +08:00
validate_scan_options_test.go test: raise statement coverage to 90% and enforce it in CI (#747) 2026-08-06 12:55:44 +08:00