Run sessions in the daemon
Add this to~/.o4/config.toml:
o4 resume <id>. The window starts the
daemon if it isn’t running, sends your messages, answers and settings to it,
and shows what it sends back. The session gets what a window session gets:
the same instructions and tools, hooks, plugins, skills, MCP servers,
permission mode, model and reasoning level, with the exceptions below. Its
commands, hooks and MCP servers start with the environment of the window that
opened it, and it calls its model with the key that window exported or the
saved account that window has active.
Close the window and the session keeps running in the daemon, along with any
response it’s writing. Run o4 resume <id> to open it again in a window.
o4 sessions live --json lists each daemon session once, with attached,
the number of windows on it; "attached": 0 is a session still running with
no window on it.
The Daemon sessions row on the General tab of /config shows whether
interactive_sessions is on. It’s read-only: change the setting in
~/.o4/config.toml. Print mode (o4 -p) never uses the daemon. See the
configuration reference for every
[daemon] key.
What runs in the daemon
Only the session a window opens with runs there. A new tab (Alt+N) runs
inside the window, as tabs always have, and ends when you close the window.
Some things don’t work in a daemon session and say so when you try them:
pasted images, fork and fork preview, the /campaign view and Ultra
reasoning. A daemon session also has no format tool. See
Language servers and formatters.
Some hooks run differently in a daemon session: for example, Stop doesn’t
fire, and hook output doesn’t appear in the window. See
Hooks in daemon sessions.
A session that’s still answering can’t be reopened. o4 resume refuses with
This session is still answering, and a window cannot join a response in progress yet. and says what to do. If the response finishes, run o4 resume
again. If you closed the window while the session was waiting on a question
or an approval, nothing can answer it: run o4 daemon stop, which stops every
response in the daemon, and then o4 resume <id>. The session reopens with
the conversation up to where the response stopped.
Safety in the daemon
A session that runs in the daemon is protected the way one that runs in the window is: its sandbox, its review mode, your permission rules, the tools/scout
turned off, and your PreToolUse and PostToolUse hooks,
MCP tools included. A rule you change in a config file takes effect when the
session is opened again.
Older versions protected it less. If you use one, leave interactive_sessions
off:
- Before o4 0.4.30, it didn’t follow your permission rules, so a
denyrule didn’t stop a call there, and its MCP tools skipped your hooks. - Before o4 0.4.27, its commands were limited less (especially on Linux), its
reviewer could approve commands in a session with no sandbox, tools
/scoutturned off were available again, and starting it inbypassshowed no warning.
SSH keys
Commands in a daemon session, such asgit push, use the SSH agent
(SSH_AUTH_SOCK) of the window you typed in last on that session: the window
where you last sent a message or answered a question or an approval, or the
one that just opened the session. When that window closes, o4 uses the window
you typed in before it. A command that’s already running keeps the agent it
started with.
With no window on the session, or none whose login has an agent, commands
start with no SSH agent. They never get the agent of the shell that started
the daemon. When a command then fails with an SSH error, o4 adds
No SSH agent: open this session in an o4 window to push or pull to what the
model sees. Run o4 resume <id> and ask for the push again.
o4 uses an agent only if it’s a socket that belongs to you.
Sessions that stay in the window
A window runs its session itself, showslocal only beside the version on
the start screen, and says why when:
- the config has a
[sandbox]or[router]section, or the session starts at Ultra reasoning; - you start o4 with
--prompt-profile,--sandbox,--cd,--add-diror--dangerously-bypass-hook-trust; - the session you reopen ran inside a window, such as one from before you
turned the setting on, or a tab. It reopens in the window with
Local only: this session ran inside an o4 window, so it reopens in this window; - the daemon can’t be started or reached, or is another o4 version or another install;
- o4 was upgraded and the daemon is finishing its own work (see After an upgrade), or the window is older than the o4 now installed;
- the window’s environment is larger than 1 MiB.
Idle sessions
A session with no window on it that has done nothing for an hour is unloaded from the daemon’s memory. Its conversation stays saved, ando4 resume <id>
loads it again. o4 runs your SessionEnd hooks when it unloads a session
and SessionStart when it loads it again. A session isn’t unloaded while it’s
answering, waiting on a question or an approval, or running a helper agent, a
background task, a background command or a background CodeMode program.
Change the time with idle_unload_secs, or set it to 0 to keep sessions
loaded.
Logging out on Linux
Closing a window or logging out doesn’t stop the daemon. The exception is a Linux machine whose login manager ends your processes when you log out. There, logging out ends the daemon and every response it’s writing. Check with:KillUserProcesses=yes, do both of these:
-
Turn on lingering for your user, once:
If that’s refused, ask an administrator to run
loginctl enable-linger <your-user>. -
Start the daemon outside your login, before you open o4:
o4 daemon start starts belongs to that login and ends with it. Start the
daemon this way again whenever it has stopped: after o4 daemon stop, a
reboot, or an o4 upgrade. o4 daemon restart starts it inside your login, so
run o4 daemon stop and then the command above instead.
After an upgrade
When you upgrade o4 while a daemon started withinteractive_sessions on
runs, the daemon notices within a minute that its binary was replaced. It then takes no new sessions and keeps
running the ones it has. Once none of them is answering, waiting, or running
anything beside it, it exits on its own. A response is never ended because of
the upgrade, except one left waiting on a question or an approval with no
window on its session for 10 minutes, which is interrupted so the daemon can
exit. The next window starts the new o4’s daemon, which loads those sessions
again when you resume them. A window still open on one of the old daemon’s
sessions says o4 was upgraded; restart o4 to continue this session.
upgrade_check_ms and upgrade_interrupt_parked_ms change these times. See
the configuration reference.
Commands
stop and restart take --yes (-y). The other commands take no options
other than --help.
Start
o4 daemon start starts the daemon as a detached background process and
waits up to 5 seconds for it to answer on its socket:
start
clears the leftover files first.
If the daemon doesn’t answer within 5 seconds, start prints
Error: the daemon did not answer within 5s; see followed by the path of
daemon.log, and exits with code 1. Check that log for the reason. Other
errors also exit with code 1.
Status
binary is the o4 file the daemon runs from. After an upgrade, status also
prints upgraded: o4 was upgraded; it takes no new session and exits once its sessions are done.
o4 daemon status exits with code 0 when the daemon is running and 3
when it isn’t, so scripts can check it without reading the output:
Stop and restart
o4 daemon stop asks the daemon to shut down with SIGTERM. If it’s still
running after the grace period (10 seconds by default), o4 sends SIGKILL.
stop and restart list those sessions first
and ask before they go on:
y or yes, o4 prints Nothing was stopped. and exits
with code 4. Without a terminal to ask in, for example in a script, stop
and restart end nothing, say to run them with --yes, and also exit with
code 4. Add --yes to end the sessions without asking.
When no daemon is running, stop prints daemon is not running and exits
with code 3.
o4 daemon restart runs stop and then start, and prints what each one
prints. If the daemon isn’t running, it prints daemon is not running, starts
it, and exits with code 0:
O4_* variables of the shell that
started it. When a later window’s differ, the window says so; run
o4 daemon restart from that terminal, once the daemon’s sessions are done,
to use them. See Environment variables.
Run in the foreground
o4 daemon serve runs the daemon in your terminal instead of in the
background. It’s the command start launches, and it isn’t listed in
o4 daemon --help. It prints nothing while it runs. o4 daemon status and
o4 daemon stop work with it as usual, so stop it from another terminal with
o4 daemon stop.
If a daemon is already running for the data folder, serve exits with code
1:
serve with Ctrl+C, it leaves its socket and daemon.pid
behind. status then reports not running, and the next start or serve
clears the leftover files.
Files
The daemon keeps its files in adaemon folder inside the o4 data folder,
which is ~/.o4 unless the O4_AGENT_RUNTIME_DATA_DIR environment variable
points somewhere else. Each data folder has at most one daemon.
The folder is readable only by you, and the socket has owner-only
permissions. The daemon also closes any connection from a process running as
a different user. It has no network listener.
Settings
You tune the daemon in the[daemon] table of your user config file,
~/.o4/config.toml:
interactive_sessions, max_sessions,
idle_unload_secs, client_queue_max_events, stop_grace_ms,
pressure_rss_mb, pressure_open_files, upgrade_check_ms and
upgrade_interrupt_parked_ms. enabled is accepted, but setting it alone
starts nothing: start the daemon with o4 daemon start, or set
interactive_sessions so windows start it.
Past max_sessions, or above the memory and open-file limits, the daemon
refuses new sessions and keeps serving the ones it has. A window whose new
session is refused doesn’t fall back to running it itself: o4 stops with
Error: Could not start hosted session '<id>': ... and the daemon’s reason,
such as the daemon already hosts 64 sessions (daemon.max_sessions = 64); close one before creating another. A client that falls
client_queue_max_events events behind is disconnected; the daemon and other
clients carry on.
The daemon reads these settings when it starts, so run o4 daemon restart
after you change them. stop_grace_ms is read by the stop command itself.
If ~/.o4/config.toml can’t be read, the daemon commands use the defaults.
o4 also reads [daemon] from the managed config, ~/.o4/managed/config.toml,
and a key there wins over the same key in your file. o4 ignores a [daemon]
table in a project’s .o4/config.toml or .o4/config.local.toml, with a
warning in the log. The settings always come from ~/.o4, even when
O4_AGENT_RUNTIME_DATA_DIR points the daemon’s files somewhere else. See
Configuration files.