Skip to content

Keep a running shell's output across a daemon stop or kill - #1549

Merged
lydakis merged 3 commits into
mainfrom
claude/project-thread-rgzll6
Dec 3, 2026
Merged

lydakis merged 3 commits into
mainfrom
claude/project-thread-rgzll6

Conversation

@lydakis

@lydakis lydakis commented Dec 3, 2026

Copy link
Copy Markdown
Owner

Requested by George · project thread

Dec 2 feedback, item 23, built like Pi: a running shell's output survives a daemon stop or kill. Pi commits a running call's output within a second and, after a restart, answers the call with the output so far. Until now the daemon kept a command's output in memory until it passed 1 MiB, a clean stop dropped running background commands, and the next start marked them process_lost with no output.

What changes

  • Output goes to its file after the first second. A shell (foreground or background) with an outputs folder that is still running after 1 s writes its streams to the same files a 1 MiB spill uses ({outputs}/{turn}-{call}-{stream}), what it read before then first. After that it writes at most once a second, or per 256 KiB, without a sync. The usable guard (the folder must not be writable by the bot's commands) is unchanged. A command that ends within its first second never touches the disk. A time-streamed file that ends within a 64 KiB preview is removed, so that answer is exactly main's. A larger one is kept and synced as a spilled file is, and the model is shown the same text main shows ("the full output is retained").
  • A clean stop ends running commands with their output. The stop asks each running command to end (SIGTERM, then the existing grace and kill), keeps reading its pipes meanwhile, and records its end with stdout, stderr and "error":"daemon_shutdown", with the existing stopped_by_shutdown detail. The stop waits up to 5 s for those ends to be recorded. A command the stop came before never starts. At start, only rows still running become lost, as before.
  • A lost background command answers with what its files kept. At start, each lost background row reads its streams' files (no links, one name, the daemon's own, and only where the bot's commands can't write the folder). Within a preview, the text is shown whole and the file is removed. Past one, the head and tail are shown and the file is named in files/artifacts, as a finished command's are. No schema change and no migration.
  • Detached commands and terminals are unchanged.

New model-facing text (needs George's per-diff OK)

Only one new string. It goes in the omitted-bytes marker of a lost command's stdout or stderr when its file holds more than a preview:

[... {N} bytes omitted; what the daemon read of it before it ended is retained: see this result's artifacts and read them with the read tool ...]

New result fields:

  • A lost background command now carries stdout/stderr, plus files/artifacts when its files hold more than a preview. Before, it was just {"error":"process_lost"}.
  • A command a clean stop ended carries {"stdout","stderr","exit_code":null,"success":false,"error":"daemon_shutdown","detail":"the daemon stopped this command before it finished, so it may have had effects; nothing has run it again","wall_time_ms"}. The detail is the existing string.

docs/RUST_PROTOTYPE.md said a clean stop records process_lost. It now describes the new behavior.

How the daemon stays fast and light

  • No timer and no file for a command without output or without an outputs folder. Commands that end within their first second take the same path as main.
  • Within 1 MiB, nothing is copied per read. The bytes already kept for the answer are handed to the writer once a second, through try_reserve, so a slow disk never makes a pipe wait. Past 1 MiB, the reader waits for the writer exactly as on main.
  • Writes run on blocking workers only, as one writev per batch, with no buffer kept per file. The open goes with the first write and the last write goes with the close, so a 3 s command costs about three trips to a worker. On a slow disk, a batch takes everything already waiting.

Bench (bench.shell_streams, new; full tables in docs/DAEMON_MEASUREMENTS.md)

These are medians of 3 alternated runs on a 4-CPU container, main 2f67e9b against this change, both built the same way.

Case (50 agents unless noted) CPU per command Peak RSS Command p50 Loop p99
storm, 30 agents, short commands back to back 4.41 / 4.49 ms 30.8 / 30.8 MiB 113 / 115 ms 7.7 / 7.5 ms
storm at 10,000 agents 4.54 / 4.53 ms 30.6 / 30.8 MiB 115 / 114 ms 7.9 / 7.6 ms
short_bg, 0.3 s background 7.99 / 8.07 ms 31.2 / 31.4 MiB 401 / 402 ms 3.9 / 4.9 ms
trickle300, 3 s, a line per 0.1 s 6.63 / 7.13 ms 58.4 / 59.9 MiB 3,287 / 3,316 ms 34.9 / 37.7 ms
trickle1000 6.03 / 6.59 ms 72.3 / 72.5 MiB 3,415 / 3,427 ms 57.6 / 62.2 ms
medium300, 300 KiB over 3 s 8.63 / 9.00 ms 93.6 / 100.1 MiB 4,394 / 4,426 ms 148 / 127 ms
flood30, 64 MB 114 / 97 ms 95.8 / 77.2 MiB 4,227 / 4,264 ms 6.3 / 9.6 ms
flood100 103 / 99 ms 350 / 265 MiB 8,810 / 8,570 ms 17.4 / 16.3 ms
flood30 on a slow disk (200 ms per write) 177 / 134 ms 161 / 134 MiB 35,593 / 17,269 ms 259 / 396 ms
  • Short commands are unchanged at 50, 5,000 and 10,000 stored agents.
  • Commands that stream past their first second cost 0.3 to 0.6 ms more daemon CPU each (2 to 9%). This is the open, write and unlink main never did. Their latency is unchanged.
  • medium300 peaks 6.5 MiB higher, because 300 streams hand over their first second at once.
  • Floods use 15% less CPU and 18 to 85 MiB less memory. On a slow disk they finish in half the time.
  • A clean stop with 30 running commands takes 32 ms on both.

These costs come from writing the output to disk, which is the durability this change adds. The rest is even or better.

Tests

  • Rust:
    • A command past its first second goes to its file, and the file is removed when it ends within a preview.
    • The daemon's stop ends a command with its output, and a command the stop came before is refused.
    • A lost command's file shows its ends cut on characters, and links and second names are not read.
    • Decoding in place matches decoding by copy.
  • Python (tests/test_wait.py):
    • A clean stop records a running background command with what it wrote.
    • A killed daemon leaves what a running command wrote, and the next start answers with it.
    • A short lost output is shown whole and its file is removed.
    • A command that ends within its first second never touches the disk.
  • cargo clippy -D warnings, cargo fmt, and the full lib suite (328) pass.

Follow-up, not in this PR: an in-turn foreground call cut off by a stop or crash keeps today's answer.

Requested by George


Generated by Claude Code

A shell with an outputs folder that runs past its first second writes its
streams to their files as it runs, at most a second behind and without a
sync, as Pi keeps a running call's output; one that ends sooner never
touches the disk, and a file that ends within a preview is removed. A clean
stop ends running commands with their output as daemon_shutdown, and a
background command a killed daemon lost answers at the next start with
what its files kept.

Within 1 MiB the kept bytes are handed to the writer once a second without
a copy per read or a wait on the disk; writes are one writev per batch on
blocking workers, with no buffer kept per file. bench.shell_streams
measures it against main; results in docs/DAEMON_MEASUREMENTS.md.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_013jkdGFB8684xSdg7nKsRqk
@lydakis lydakis self-assigned this Dec 3, 2026
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Dec 3, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-12-03T05:47:54.860169Z 0a923a6 New commits
🔒 Security Review ✅ Completed 2026-12-03T05:23:13.127550Z dc13bba PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

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

Copy link
Copy Markdown

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: dc13bbaa3f

ℹ️ About Codex in GitHub

Your team has set up Codex to 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 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/server/mod.rs Resolved
Comment thread src/store/db.rs Resolved
Comment thread src/tools.rs Resolved
Comment thread src/tools.rs Resolved
Comment thread src/tools.rs Resolved
…-rgzll6

# Conflicts:
#	docs/DAEMON_MEASUREMENTS.md
#	src/store.rs
A turn parked on a background command a killed daemon lost was restored,
and its result read, before the start put the command's kept output in
that result: the recovery now runs first. A lost command's file opens
without waiting, so a pipe put in its place is rejected rather than
holding the start.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_013jkdGFB8684xSdg7nKsRqk
@lydakis
lydakis merged commit 388c9c0 into main Dec 3, 2026
7 checks passed
@lydakis
lydakis deleted the claude/project-thread-rgzll6 branch December 3, 2026 06:18
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