Skip to content

Add -d/--detach flag for background daemon mode#716

Open
ReSyncc wants to merge 3 commits intoRightNow-AI:mainfrom
ReSyncc:fix/daemon-background-by-default
Open

Add -d/--detach flag for background daemon mode#716
ReSyncc wants to merge 3 commits intoRightNow-AI:mainfrom
ReSyncc:fix/daemon-background-by-default

Conversation

@ReSyncc
Copy link

@ReSyncc ReSyncc commented Mar 18, 2026

Problem

OpenFang is an autonomous agent OS — it should run as a background service without requiring a dedicated terminal session. Currently, openfang start blocks the terminal and the daemon dies when the terminal is closed. Users have to manually background it with nohup or &, which is a poor experience for an always-on system.

This contradicts the core design: agents, channels (Telegram, Discord, etc.), scheduled jobs, and workflows should keep running independently of any terminal session.

Solution

  • openfang start now spawns the daemon in the background using the existing start_daemon_background() function (which was already used by openfang dashboard but not exposed to users) and returns immediately, printing the API URL and dashboard link
  • openfang start when a daemon is already running now reports the existing daemon's URL instead of erroring out
  • openfang start --foreground preserves the old blocking behavior for Docker containers and debugging
  • Dockerfile updated to CMD ["start", "--foreground"] since containers need the foreground process to stay alive

Benefits

  • Users can run openfang start and close their terminal — the daemon keeps running
  • Agents stay active, channels remain connected, scheduled jobs continue executing
  • No more confusing "already running" errors — idempotent start behavior
  • Docker still works correctly with explicit --foreground
  • Aligns CLI behavior with what users expect from a daemon/service command

Test plan

  • cargo build -p openfang-cli compiles
  • openfang start returns immediately, daemon accessible at printed URL
  • openfang start again prints "already running" with URL
  • openfang start --foreground blocks as before
  • openfang stop stops the background daemon
  • Docker container stays alive with start --foreground

🤖 Generated with Claude Code

Copy link
Member

@jaberjaber23 jaberjaber23 left a comment

Choose a reason for hiding this comment

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

Breaking behavior change that needs more work:

  1. --config / -c is not forwarded to the background process. If a user runs openfang start -c /custom/config.toml, the background process starts with default config, not the custom one.

  2. Exit code contract change — "already running" changed from exit(1) to success return. Scripts checking exit codes will break silently.

  3. This is a breaking change for every user, tutorial, and script that uses openfang start expecting foreground behavior. Needs discussion and a deprecation warning at minimum.

  4. Test plan items are unchecked — the PR was not actually tested.

Addresses review feedback on RightNow-AI#716:

1. **Non-breaking**: `openfang start` remains foreground (unchanged behavior).
   Background mode is opt-in via `-d`/`--detach`.

2. **Config forwarding**: `--config` and `--yolo` flags are forwarded to
   the background process. `openfang start -d --config /custom/config.toml`
   works correctly.

3. **Exit codes preserved**: "already running" still exits with code 1,
   matching existing script expectations.

4. **Docs updated**: cli-reference.md documents the new flag with examples
   for foreground, detached, and combined usage.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@ReSyncc ReSyncc force-pushed the fix/daemon-background-by-default branch from 11873cb to 29e301e Compare March 19, 2026 23:05
- Foreground start now shows: "Tip: use openfang start -d to run in the background"
- README quickstart blocks mention -d flag alongside openfang start

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@ReSyncc
Copy link
Author

ReSyncc commented Mar 19, 2026

Breaking behavior change that needs more work:

1. `--config` / `-c` is not forwarded to the background process. If a user runs `openfang start -c /custom/config.toml`, the background process starts with default config, not the custom one.

2. Exit code contract change — "already running" changed from exit(1) to success return. Scripts checking exit codes will break silently.

3. This is a breaking change for every user, tutorial, and script that uses `openfang start` expecting foreground behavior. Needs discussion and a deprecation warning at minimum.

4. Test plan items are unchecked — the PR was not actually tested.

Great point, ended up going to a -d command style similar to detach apart of docker run and docker compose to avoid breaking behavior.

  • openfang start remains foreground (no breaking change)
  • --config and --yolo flags forwarded to the background process
  • "Already running" keeps exit(1) (preserves script behavior)
  • Fixed all callers of start_daemon_background() (main.rs, init_wizard.rs, cmd_dashboard)
  • Updated docs/cli-reference.md with new flag, examples, and output for both modes
  • Added comment to openfang start in README.md
  • Added tip apart of openfang start command to let users know there is a -d arg to detach.

@ReSyncc ReSyncc requested a review from jaberjaber23 March 19, 2026 23:15
- stdout/stderr redirected to ~/.openfang/daemon.log (append mode)
  so startup errors are debuggable. Falls back to /dev/null if log
  file can't be opened.
- Unix: pre_exec hook calls setsid() to create a new session, making
  the daemon fully independent of the launching terminal. Prevents
  SIGHUP from killing the daemon when the terminal closes.
- Detach output now shows log file path.
- Timeout error message points user to daemon.log.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@ReSyncc
Copy link
Author

ReSyncc commented Mar 19, 2026

  • Daemon stdout/stderr redirected to ~/.openfang/daemon.log (append mode) instead of /dev/null
  • Unix: setsid() via pre_exec hook so daemon survives terminal close
  • libc added as Unix-only dependency for setsid
  • Detach success output now shows log file path
  • Timeout error message points user to daemon.log for debugging
  • Falls back to /dev/null if log file can't be opened

@ReSyncc ReSyncc changed the title Make openfang start daemonize by default Add -d/--detach flag for background daemon mode Mar 19, 2026
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