Skip to content

feat(cmd): add detached background mode and document usage#2927

Open
forlornMWS wants to merge 4 commits intorouter-for-me:devfrom
forlornMWS:feat/windows-background-mode
Open

feat(cmd): add detached background mode and document usage#2927
forlornMWS wants to merge 4 commits intorouter-for-me:devfrom
forlornMWS:feat/windows-background-mode

Conversation

@forlornMWS
Copy link
Copy Markdown

Summary

This PR adds a Windows-only detached background startup mode so cli-proxy-api.exe can keep running after the launching terminal is closed.

Previously, when starting the server from a terminal on Windows, closing the terminal would terminate the server process. This change introduces a --background flag that relaunches the process in detached mode and exits the parent process immediately after successful spawn. It also keeps behavior unchanged on non-Windows platforms and updates multilingual docs for discoverability.

Key Changes

  • Add new CLI flag in cmd/server/main.go:
    • --background / -background
    • filter the flag before relaunch to avoid recursive respawn
    • support filtering both plain and assignment forms (--background, -background, --background=true, -background=true)
    • call cmd.StartDetachedIfRequested(...) and return early in the parent process
  • Add Windows implementation:
    • internal/cmd/background_windows.go
    • start child process with detached creation flags
    • redirect stdio to os.DevNull
    • print detached child PID on success
  • Add non-Windows no-op implementation:
    • internal/cmd/background_nonwindows.go
    • keep cross-platform build compatibility without changing current non-Windows behavior
  • Update docs:
    • README.md: add Windows Background Mode section with usage and behavior
    • README_CN.md: add Windows 后台运行 section
    • README_JA.md: add Windows バックグラウンド実行 section and align feature wording with Codex support

Files Changed

  • cmd/server/main.go
  • internal/cmd/background_windows.go (new)
  • internal/cmd/background_nonwindows.go (new)
  • README.md
  • README_CN.md
  • README_JA.md

Behavior Notes

  • --background is effective on Windows (detached relaunch).
  • On non-Windows, current behavior remains unchanged (no relaunch).
  • Detached child suppresses terminal IO by design (stdin/stdout/stderr -> null device).
  • Other startup flags (for example --config) are preserved and passed through to the detached child process.

Test Plan

  • Build verification: go build -o cli-proxy-api.exe ./cmd/server
  • On Windows, run cli-proxy-api.exe --background
  • Confirm command exits quickly and prints detached PID
  • Close terminal and verify proxy process remains alive
  • Call health/API endpoint to confirm service is reachable after terminal close
  • Run normal startup (without --background) to confirm no regression

@github-actions github-actions Bot changed the base branch from main to dev April 20, 2026 11:18
@github-actions
Copy link
Copy Markdown

This pull request targeted main.

The base branch has been automatically changed to dev.

Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 519ceb145a

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread cmd/server/main.go Outdated
Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request introduces a --background flag for the Windows version of the CLI proxy, allowing it to run as a detached process. Documentation has been updated across multiple languages to reflect this change. The feedback suggests using syscall constants instead of magic numbers for process creation flags, utilizing os.Executable() for more reliable process relaunching, and removing a redundant check for the child process object.

Comment thread internal/cmd/background_windows.go Outdated
Comment thread internal/cmd/background_windows.go Outdated
Comment thread internal/cmd/background_windows.go Outdated
- Added a note in `README.md`, `README_CN.md`, and `README_JA.md` to specify that the `--background` flag should not be combined with interactive/login/import/TUI modes.
- Updated the implementation in `main.go` to enforce this restriction and log an error if violated.
- Refactored the detached process creation logic in `background_windows.go` for improved clarity and error handling.
Copy link
Copy Markdown
Collaborator

@luispater luispater left a comment

Choose a reason for hiding this comment

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

Summary

This PR adds a Windows-only --background flag to relaunch cli-proxy-api.exe as a detached child process so it keeps running after the launching terminal is closed. It also documents the behavior in README (EN/CN/JA) and blocks combining --background with interactive/login/import/TUI modes.

Blocking

  • The parent process filters out --background/-background and --background=.../-background=..., but it does not handle the valid Go flag form --background true (or -background true). In that case, the standalone true token will be forwarded to the detached child, which can cause flag.Parse() to stop early and ignore subsequent flags like --config.

    Suggestion: when filtering, if you see --background or -background, also skip the next arg if it looks like a boolean value (true/false/1/0, etc.), or otherwise adjust the approach to ensure no stray value is forwarded.

Non-blocking notes / suggestions

  • Non-Windows currently accepts --background but performs no relaunch. Consider warning/erroring (or explicitly documenting that it is a no-op off Windows).
  • internal/cmd/background_windows.go uses a magic 0x00000008 for detached process creation. Consider using a named constant (if available) or add a short comment, and confirm whether you want CREATE_NO_WINDOW / HideWindow behavior.
  • The detached child redirects stdin/stdout/stderr to NUL, so logs are dropped from the console. Consider documenting log expectations (or where logs should go) for background mode.

Test plan (recommended)

  • Windows: go build -o cli-proxy-api.exe ./cmd/server
  • Run: cli-proxy-api.exe --background --config <path> and confirm:
    • parent exits quickly and prints detached PID
    • child stays running after terminal is closed
    • service is reachable (health/API endpoint)
  • Regression for the blocking issue after fix:
    • cli-proxy-api.exe --background true --config <path> should behave equivalently to --background and still honor --config.

Ensure detached relaunch strips standalone boolean values after --background so child parsing keeps subsequent flags like --config, and add unit tests to lock behavior.

Made-with: Cursor
@forlornMWS forlornMWS requested a review from luispater April 25, 2026 13:01
Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 4ccf3c1b97

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread cmd/server/main.go Outdated
Reject --background true/false style before flag parsing to prevent parse-short-circuit bypasses, and adjust background arg filtering/tests to keep detached relaunch safe for interactive flags.

Made-with: Cursor
@forlornMWS
Copy link
Copy Markdown
Author

@codex review

@chatgpt-codex-connector
Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Nice work!

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

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.

2 participants