cccc icon indicating copy to clipboard operation
cccc copied to clipboard

Starting a stopped Actor when Mail arrives for it (asking for direction)

Open chriscoveries opened this issue 4 weeks ago • 4 comments

We've hesitated to bring this one up. It touches Actor lifecycle, which is the core of CCCC, and the prototype we built is far too large to offer as a pull request. But we keep running into the problem, we're convinced the feature is valuable, and we need your help to get it right.

Evidence, scripts and raw proof data: https://gist.github.com/chriscoveries/735bf4969c8f2b7b5b9790a35f2aba7a

The problem

When a managed Actor stops — its process exits, it finishes a turn and the runtime closes, or someone stops it to free memory — Mail addressed to it waits until a person starts it again. Nothing tells the senders. Other Actors keep handing it work, and the work stalls.

The idle Mail notice (mail_notice_idle_after_seconds) is a real help for an Actor that is running but busy. It cannot help an Actor that is not running at all, because there is no process to notify.

In our groups this is the most common way work silently stops: a lane finishes, exits and sits stopped; a coordinator mails it the next task; the Mail is never read. We only notice hours later, when someone asks why nothing moved.

How often it happens (real data)

Correction (2026-11-04): the numbers in this section were wrong. Our script treated each mail.read as reading only the Mail it names, but it is a watermark covering earlier Mail in the batch. Recounted, every Mail was read; the median wait was 15.9 min to a stopped Actor vs 1.2 min to a running one. See the correction comment below. The original section is kept struck through for the record.

Original (superseded) numbers

We measured it on our busiest group's ledger, 2026-09-21 to 2026-11-04: every Mail addressed to exactly one Actor, split by whether that Actor was stopped or running when the Mail was committed. Mail to Actors that were later removed is excluded.

addressee when the Mail was committed Mail read never read median wait to read 90th percentile
running 3,564 2,712 852 (24%) 0.7 min 5.4 min
stopped 187 105 82 (44%) 5.7 min 30.9 min

Mail wait: stopped vs running addressees

Mail to a stopped Actor was nearly twice as likely never to be read, and the Mail that was read waited about eight times longer. Those 187 messages are also an undercount of the cost: each unread one is a task some other Actor thought it had handed off. The script that produced this (mailwait.py in the gist) runs on any group's ledger.jsonl and prints aggregates only.

What we would like

An opt-in setting per Actor. When committed Mail is explicitly addressed to it, and it is enabled but stopped, CCCC starts it; the Mail is then delivered through the normal path. It should never:

  • start an Actor that a person stopped by hand, or one in a stopped or paused group;
  • start for broadcast selectors such as @all, @peers or @foreman — only for Mail that names the Actor;
  • start without a bound — a shared limit on automatic starts, so a Mail storm cannot become a start storm;
  • consume, move or lose the Mail, whether the start succeeds or fails.

What we built, and how it behaved

We built a prototype on our fork against v0.4.47 (rules in behaviour.md in the gist). In short: a wake_on_mail Actor field, default off; eligibility rechecked under the group lock just before starting; attempts for the same Actor coalesced; a durable limiter shared across groups; an ordinary actor.start with reason: "mail_wake" and the Mail's id as source_event_id; and a normal Mail notice after the start, without consuming the Mail.

We tested it in four isolated runs, each in a fresh CCCC home with one disposable Actor and a real captured Mail (body omitted):

run build setup result
candidate prototype opted in, enabled, stopped actor.start reason=mail_wake 0.37 s after the Mail
manual prototype stopped by hand, then opted in not started (20 s watched)
optout prototype enabled, stopped, default off not started (65 s watched)
control unmodified v0.4.47 same Actor, same Mail not started (30 s watched)

Proof timeline: only the opted-in Actor was started

Candidate run, event by event
+s from the Mail event by detail
-14.64 actor.stop system reason=process_exit
+0.00 chat.message user message_mode=mail
+0.37 actor.start system reason=mail_wake, source_event_id=(the Mail)
+0.37 system.notify system Mail notice to the started Actor
+0.37 runtime.delivery system
+10.36 actor.stop system reason=process_exit (the placeholder process ended)

All four timelines and the raw events are in proof-timeline.md and proof-*.json in the gist.

Being honest about the limits: the Actor was a placeholder process (/usr/bin/sleep 3), not a provider runtime; the watch windows were short; and the run says nothing about restart recovery or storm limits under real load. Those have named unit tests in the prototype, nothing more.

Why we are not sending the prototype as a PR

It is about 6,000 lines: 4,547 in a new mail_wake module, 1,403 across 45 existing files, and only 118 lines of tests (prototype-size.md in the gist). Most of that is crash-recovery, budget and ownership bookkeeping we had to build because we were working from outside your design — for example, it only activates in a freshly created home because we had no safe way to migrate an existing one. You know where Actor lifecycle state lives and what it already tracks. We expect the right version is a small fraction of this size, and we'd rather not ask you to review the wrong one.

Where we need your help

  • Does this fit CCCC, and is opt-in per Actor the right shape?
  • Where should it live: a field on the Actor, a Mail delivery policy, or the runtime layer that already knows when and why an Actor stopped?
  • What is the smallest safe version you'd accept? We'd be glad to start with only "named Mail starts an opted-in, enabled, stopped Actor, at most N per minute, never after a manual Stop", and leave everything else for later.

If it fits, we'll follow your design and send a small PR, or step back if you'd rather build it yourself. The prototype source is available on request.

chriscoveries avatar Nov 04 '26 11:11 chriscoveries

As offered, here is the prototype, shared for reference rather than for review as a pull request.

  • Patch: wake-on-mail-prototype.patch, a single diff against v0.4.47 (58 files, +6068/−52). Apply it with git apply on a v0.4.47 checkout.
  • Branch: chriscoveries/cccc feat/wake-on-mail-v047, eight commits, fork CI green at the head (443613eb).
  • Where to look first: crates/cccc-daemon/src/ops/mail_wake/ holds the new module (eligibility in mod.rs, the group-lock recheck in group.rs, the shared start limiter in budget.rs). The changes to existing files are mostly the hooks that tell it an Actor started, stopped or was restarted.

The same evidence set (charts, measurement script, proof runs) is in the gist linked above. We know much of this is scaffolding that a design inside CCCC wouldn't need, and we'd use your answer on the questions above to cut it down before proposing anything.

chriscoveries avatar Nov 04 '26 13:11 chriscoveries

Thank you for raising this, for measuring it, and for holding the 6,000-line prototype back as a reference rather than a pull request. The write-up made the design question clear.

The symptom is real: work handed to a stopped Actor can sit unnoticed. We don’t plan to make Mail start Actors, even as an opt-in, because the existing design separates the two cases, and much of what you describe is already covered:

Mail is deliberately the “read it when you next check” channel. CCCS 6.1 stores it “without immediately invoking, waking, steering, or writing to the recipient runtime”, and our tests hold that. Making some Mail start its recipient would change that contract for every sender who relies on it. A handoff that needs the recipient to act should use Send, or request_reply when a reply is needed. Send goes through the normal delivery path, which can attempt to start an enabled managed Actor whose process exited; a disabled Actor’s delivery waits until it is started again. A manual Stop disables the Actor, so another agent’s Send does not restart something a person stopped. Two differences from your rules: a Send that the user addresses directly to an Actor may re-enable it and resume a paused group, by design; and delivery can still fail or end with an unknown outcome, which is reported rather than retried blindly. Mail that is already waiting can be promoted with cccc_message_deliver (cccc deliver on the command line), by its original sender or the user. It requests delivery to enabled recipients, which may start them, and refuses disabled ones. Unlike your rules, when it reserves new delivery work it also resumes a paused or stopped group. cccc inbox-summary --group --cutoff shows the Mail each Actor is still awaiting, including stopped and disabled Actors, which can help find stalled handoffs. It leaves out Mail that was already replied to, or whose own delivery was accepted or may have been. About the measurement: mail.read records the last Mail of each inbox_read batch, and it covers the earlier Mail returned in that batch (CCCS 6.2). mailwait.py counts only the Mail whose ID equals the watermark, so earlier Mail in a batch is counted as never read. In a synthetic ledger with two Mails to one Actor and one mail.read pointing at the second, the script reports one read and one never read. Actors with no lifecycle event before a Mail are also counted as running, and Mail still unread when the window ends has not been read yet rather than never. So the 44% / 24% split and the wait ratio shouldn’t be taken as measured differences yet. A recount would expand each watermark over the Mail it covers for that Actor, within the same Actor generation and recipient eligibility. None of this changes that you see stalls in practice.

We agree that the sender should be told. We plan a small change which would be released soon: when Mail is addressed to a local Actor that appears to be stopped or disabled when the send is checked, the send result will say so and point to Send or deliver. When the state can’t be determined it will say nothing rather than guess, and it starts nothing and does not change Mail itself.

If, after moving handoffs that need action to Send, there is still a case Send cannot serve, please describe it. That would be the place to start a design, and it would belong in the Actor lifecycle rather than in Mail.

ChesterRa avatar Nov 04 '26 16:11 ChesterRa

Thank you, both for the clear answer and for checking our numbers. You were right about the measurement, and we want to correct it plainly.

mailwait.py treated each mail.read as reading only the Mail it names. As you pointed out, it is a watermark that covers every earlier Mail returned in that batch, so most of what we called "never read" had in fact been read. We also counted Actors with no lifecycle event as running, and counted Mail that was simply too recent as never read.

We recounted with those three fixed (mailwait2.py in the gist; Mail from the last 24 hours left out):

addressee when the Mail was committed Mail read median wait 90th percentile
running 3,473 3,473 1.2 min 10.1 min
stopped 183 183 15.9 min 47.9 min

Corrected Mail wait

So nothing was lost: every counted Mail was eventually read. What remains is the delay. Mail to a stopped Actor waited about 13 times longer at the median, which matches the stalls we see. The 44% / 24% figures and the first chart in the issue were wrong; I've marked them in the description.

On the design, your answer makes sense and we're adopting it. Handoffs that need the recipient to act now go as Send, or request_reply when a reply is needed; Mail stays for information. We'll use cccc deliver for Mail that is already waiting, and cccc inbox-summary --cutoff to find stalled handoffs. The send-result warning you describe would have caught exactly our case, so thank you for that too.

If Send leaves a gap after we've switched, we'll come back with a concrete case under Actor lifecycle, as you suggest. Until then, please feel free to close this.

chriscoveries avatar Nov 04 '26 21:11 chriscoveries

Thank you for recounting, and for correcting the numbers so plainly. The corrected figures fit what we expected: no Mail was lost, and the cost of Mail to a stopped Actor is the wait. We're glad the split between Send, request_reply and Mail fits your workflow. Closing this for now; if Send leaves a gap after you switch, please open a new issue with the concrete case.

ChesterRa avatar Nov 05 '26 12:11 ChesterRa