runact 1.2.1

A Rust-native actor runtime with BEAM-style processes
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
# Runact Runtime + Networking Plan

## 1. Objective

Extend Runact into a small, Rust-native concurrency runtime capable of powering:

1. PaperOS desktop applications
2. Remote AI-agent servers
3. Long-running agent processes
4. Async network services
5. CPU-heavy agent workloads
6. External tool execution

Runact must remain a general-purpose runtime.

It must NOT become an HTTP framework, WebSocket framework, TLS library, or application framework.

Core principle:

> Runact manages execution, concurrency, lifecycle, and low-level I/O readiness. Higher-level libraries implement protocols and applications.

---

## 2. Target Architecture

```text
                         PaperOS Desktop
                              │
                         Agent Client
                              │
                       HTTPS / WebSocket
                              │
                              ▼
                    Remote Agent Server
                              │
                       HTTP / WebSocket
                              │
                              ▼
                            Runact
        ┌─────────────────────┼─────────────────────┐
        │                     │                     │
      Actors              Async Tasks          Compute Pool
        │                     │                     │
        └─────────────────────┼─────────────────────┘
                              │
                         I/O Runtime
                              │
                           TCP
                              │
                    Linux epoll initially
```

---

## 3. Runact Responsibilities

Runact owns:

```text
Actor system
Scheduler
Async tasks
Future polling
Wakers
Cancellation
Timers
Task groups
Supervision
Compute pool
TCP
I/O readiness
Process lifecycle
Backpressure
Runtime shutdown
```

Runact does NOT own:

```text
HTTP
WebSocket
TLS
DNS
HTTP client
HTTP server
LSP protocol
Git protocol
AI provider APIs
JSON-RPC protocol
Application-level agent protocol
```

Those belong to higher-level libraries or applications.

---

## 4. Crate Architecture

Prefer separating the system into crates.

```text
runact/
├── runact-core/
│   ├── actor/
│   ├── mailbox/
│   ├── scheduler/
│   ├── supervision/
│   └── lifecycle/
│
├── runact-runtime/
│   ├── executor/
│   ├── task/
│   ├── waker/
│   ├── cancellation/
│   ├── timer/
│   ├── task_group/
│   └── shutdown/
│
├── runact-compute/
│   ├── pool/
│   └── job/
│
├── runact-net/
│   ├── tcp/
│   ├── reactor/
│   ├── readiness/
│   └── socket/
│
└── runact-process/
    ├── process/
    ├── stdin/
    ├── stdout/
    ├── stderr/
    └── cancellation/
```

The exact crate boundaries may change during implementation, but the conceptual separation should remain.

---

## 5. Async Runtime

Runact must execute standard Rust futures.

Use:

```rust
Future
Poll
Context
Waker
Pin
```

Do NOT create a custom async programming model.

Basic API:

```rust
runtime.spawn(future)
runtime.sleep(duration)
runtime.timeout(duration, future)
runtime.compute(job)
runtime.task_group()
runtime.shutdown()
```

---

## 6. Task Lifecycle

Implement:

```text
Created
   ↓
Scheduled
   ↓
Running
   ↓
Waiting
   │
   └──── wake ────→ Runnable
                         │
                         ▼
                      Running
                         │
              ┌──────────┴──────────┐
              ▼                     ▼
          Completed               Failed
```

Cancellation:

```text
Running / Waiting
       ↓
 Cancelling
       ↓
 Cancelled
```

A pending task must not continuously consume worker time.

---

## 7. Waker

Implement a Runact-specific Waker.

When a future returns:

```rust
Poll::Pending
```

the Waker must arrange for the task to become runnable again.

Conceptual flow:

```text
Future
  ↓
Poll::Pending
  ↓
Waker registered
  ↓
External event
  ↓
Waker.wake()
  ↓
Task marked runnable
  ↓
Scheduler queue
  ↓
Worker polls task
```

Prevent duplicate scheduling with an atomic runnable state.

---

## 8. Task Scheduling

The scheduler must provide:

* fairness
* bounded queues where appropriate
* wake-up efficiency
* no starvation
* task cancellation
* graceful shutdown

Do not let one CPU-heavy task block normal async work.

---

## 9. Actor Integration

Actors remain distinct from async tasks.

Actors:

```text
Mailbox
   ↓
Scheduler
   ↓
Actor execution
   ↓
Yield
```

An actor must not block waiting for network I/O.

Instead:

```text
Actor
  │
  ├── spawn async network task
  │
  └── continue processing messages
             │
             ▼
       network completes
             │
             ▼
        actor message
             │
             ▼
        actor processes response
```

Example:

```text
FetchAgentResponse
        ↓
spawn HTTP task
        ↓
Actor continues
        ↓
HTTP completes
        ↓
AgentResponseReceived
        ↓
Actor processes response
```

---

## 10. Cancellation

Implement:

```rust
CancellationToken
```

Required operations:

```rust
cancel()
is_cancelled()
child_token()
```

Cancellation must be cooperative.

Do not forcibly kill arbitrary threads.

Cancellation must propagate through structured task groups.

---

## 11. Task Groups

Implement structured concurrency.

Example:

```text
AgentSession
    │
    └── TaskGroup
         ├── LLM request
         ├── tool execution
         ├── network connection
         └── timeout task
```

When an agent session terminates:

```text
AgentSession closed
       ↓
Cancel TaskGroup
       ↓
Cancel children
       ↓
Wait for children
       ↓
Release resources
```

This is particularly important for remote AI agents.

---

## 12. Timers

Implement:

```rust
runtime.sleep(duration)
runtime.timeout(duration, future)
```

Start with a simple correct timer implementation.

Do not optimize prematurely.

A timer wheel can be added later if profiling proves it necessary.

---

## 13. Compute Pool

CPU-heavy operations must not execute on ordinary async workers.

Provide:

```rust
runtime.compute(|| {
    expensive_operation()
});
```

Use cases:

```text
code indexing
search
parsing
syntax analysis
compression
hashing
large JSON processing
AI preprocessing
diff generation
```

Architecture:

```text
Runact
 ├── Actor workers
 ├── Async workers
 └── Compute workers
```

Compute work must not starve actor or async execution.

---

## 14. Minimal Network Runtime

Implement a minimal `runact-net`.

The initial goal is NOT to build a networking framework.

The goal is:

> Give Runact native async TCP and OS readiness integration.

Initial API:

```rust
TcpListener
TcpStream
```

Required operations:

```rust
TcpListener::bind()
TcpListener::accept()

TcpStream::connect()
TcpStream::read()
TcpStream::write()
TcpStream::shutdown()
```

Also provide:

```text
socket address
connection state
read readiness
write readiness
connection cancellation
```

---

## 15. I/O Reactor

This is the most important part of `runact-net`.

Architecture:

```text
TcpStream
    │
    ▼
OS socket
    │
    ▼
OS readiness API
    │
    ▼
Reactor
    │
    ▼
Runact Waker
    │
    ▼
Scheduler
    │
    ▼
Future resumes
```

Linux is the first target.

Use:

```text
epoll
```

Do not initially attempt:

```text
kqueue
IOCP
io_uring
```

Build a correct Linux implementation first.

Design the reactor abstraction so other platforms can be added later.

---

## 16. TCP Scope

Initial `runact-net` supports:

```text
TCP client
TCP server
async read
async write
partial read
partial write
connection close
timeouts
cancellation
backpressure
```

Do NOT implement:

```text
HTTP
WebSocket
TLS
DNS
HTTP/2
HTTP/3
QUIC
proxy
connection pooling
```

These remain outside Runact.

---

## 17. Backpressure

Network writes must not create unlimited memory growth.

Provide bounded buffering or explicit write-pressure handling.

Example:

```text
Agent output
     ↓
bounded channel
     ↓
WebSocket/HTTP layer
     ↓
TCP
```

If the remote client becomes slow, the agent must not allocate unlimited output buffers.

---

## 18. Process Runtime

AI coding agents need process execution.

Implement a separate process abstraction.

Required capabilities:

```text
spawn process
stdin
stdout
stderr
exit status
timeout
cancellation
graceful termination
```

Example:

```text
Agent
  ↓
run cargo test
  ↓
Process
  ├── stdout
  ├── stderr
  └── exit status
```

Runact should manage process lifecycle.

The agent layer decides what commands are allowed.

---

## 19. Process Cancellation

When:

```text
Agent cancelled
```

propagate:

```text
Agent Task
   ↓
Tool Task
   ↓
Process
```

The process must not remain orphaned after the agent terminates.

---

## 20. Remote AI Agent Architecture

The remote server should eventually look like:

```text
                    Agent Server
                         │
                    Runact Runtime
                         │
              ┌──────────┼──────────┐
              │          │          │
          AgentActor   Tasks      Compute
              │          │          │
       ┌──────┼──────────┼──────────┤
       │      │          │          │
      LLM   Tools      Files      Process
       │
       ▼
   HTTP Client
       │
       ▼
  AI Provider
```

The HTTP client itself should be implemented outside Runact.

Runact provides the async execution environment.

---

## 21. Agent Connection Architecture

The remote server can eventually expose:

```text
HTTP
WebSocket
```

but those protocols remain outside Runact.

Architecture:

```text
Internet
   │
   ▼
HTTP/WebSocket Server
   │
   ▼
Agent Gateway
   │
   ▼
Runact
   │
   ▼
AgentActor
```

The gateway translates network messages into agent commands.

---

## 22. Agent Cancellation Flow

This must work end-to-end:

```text
PaperOS
   │
   │ cancel agent
   ▼
Agent Gateway
   │
   ▼
AgentActor
   │
   ▼
CancellationToken
   │
   ├── cancel LLM task
   ├── cancel tool task
   ├── cancel network task
   └── terminate process
```

This is one of the main reasons Runact needs structured cancellation.

---

## 23. PaperOS Desktop

PaperOS should NOT require a network server just to operate locally.

Architecture:

```text
PaperOS Desktop
   │
   ├── WebView
   │
   ├── PaperOS Core
   │
   └── Runact
```

The desktop UI communicates with PaperOS through the local UI bridge.

Remote AI is optional:

```text
PaperOS
   │
   └── Agent Client
          │
          │ HTTPS/WSS
          ▼
       AI Server
```

Therefore `runact-net` is not required merely to render the PaperOS desktop UI.

---

## 24. Remote Agent Client

PaperOS should eventually have an agent client abstraction:

```rust
trait AgentClient {
    async fn start(...);
    async fn send(...);
    async fn cancel(...);
}
```

Implementations can include:

```text
LocalAgent
RemoteAgent
```

The UI does not need to know which one is being used.

---

## 25. Runtime Adapter Principle

Runact should remain independent of higher-level protocol implementations.

Architecture:

```text
Runact
   │
   ├── TCP
   │
   ├── Process
   │
   ├── Futures
   │
   └── Scheduler
          │
          ▼
Higher-level libraries
   │
   ├── HTTP
   ├── WebSocket
   ├── TLS
   └── DNS
```

Do not add protocol-specific APIs to Runact.

---

## 26. Tokio Decision

Do not make Tokio a mandatory Runact dependency.

If an external library requires Tokio, create an adapter:

```text
runact-tokio
```

Possible architecture:

```text
Runact
   │
   ├── native runtime
   │
   └── optional adapters
          └── Tokio
```

The long-term goal is that Runact can operate without Tokio.

For the first native networking implementation, do not introduce Tokio just to implement TCP.

---

## 27. Development Phases

Development phases are tracked in [Roadmap](roadmap.md). The roadmap is the authoritative source for phase numbering and status.

This document describes the architecture and design for the runtime + networking extension. For implementation status, see the roadmap.

### Phase 9 — Runtime + Networking Extension (roadmap.md)

This phase extends Runact with TCP networking, process runtime, cancellation, task groups, and async timers. All sub-phases are complete:

- **9a** — Cancellation & Task Groups ✅
- **9b** — Timers ✅
- **9c** — Actor ↔ Async Integration ✅
- **9d** — Process Runtime ✅
- **9e** — TCP Reactor ✅
- **9f** — Stress Testing ✅

### Next: Phase 10 — HTTP (roadmap.md)

Build `runact-web` as a separate workspace member. See [Development Plan](development-plan.md) §48 for architectural details.

---

## 28. Performance Goals

Do not optimize before measuring.

Initial goals:

```text
No blocking actor workers
No blocking async workers
Bounded memory under slow clients
Low scheduling overhead
Efficient wakeups
Predictable cancellation
No orphaned tasks
No orphaned processes
```

Measure:

```text
task scheduling latency
wake latency
TCP readiness latency
throughput
memory per connection
CPU utilization
actor fairness
```

---

## 29. Testing Strategy

Unit tests:

```text
Future
Waker
Task
Cancellation
Timer
TaskGroup
Actor integration
TCP
Reactor
Process
```

Integration tests:

```text
TCP client/server
multiple connections
cancellation
timeouts
process execution
agent lifecycle
```

Stress tests:

```text
10k tasks
10k wakeups
1k TCP connections
slow clients
large concurrent workloads
mixed actors + tasks + TCP + compute
```

Failure tests:

```text
actor panic
future panic
connection reset
process crash
client disconnect
LLM timeout
task cancellation
runtime shutdown
```

---

## 30. Architectural Invariants

The canonical invariant set is in [Architecture](architecture.md) §35. The networking-relevant invariants are:

1. Runact is not an HTTP framework.
2. Runact is not a WebSocket framework.
3. Runact is not a TLS library.
4. Runact is not an AI framework.
5. Runact executes standard Rust futures.
6. Actors and async tasks remain distinct concepts.
7. CPU-heavy work uses the compute pool.
8. Async I/O never blocks an actor worker.
9. Cancellation is cooperative.
10. Structured concurrency prevents orphaned tasks.
11. Process cancellation prevents orphaned child processes.
12. TCP is the lowest-level network primitive in Runact.
13. HTTP/WebSocket remain outside Runact.
14. Tokio is optional, not fundamental.

See [Architecture §35](architecture.md#35-architectural-invariants) for the full set (27 invariants).

---

## 31. Final Target

The final conceptual architecture is:

```text
                         PAPEROS
                            │
             ┌──────────────┴──────────────┐
             │                             │
        Desktop UI                    Remote Agent
             │                             │
          WebView                    HTTPS / WSS
             │                             │
             └──────────────┬──────────────┘
                            │
                       PAPEROS CORE
                            │
                          RUNACT
                            │
       ┌────────────────────┼────────────────────┐
       │                    │                    │
     Actors              Async Tasks         Compute
       │                    │                    │
       ├────────────────────┼────────────────────┤
       │                    │                    │
    Process              Timers             Cancellation
       │
       └────────────────────┐
                            │
                         I/O Runtime
                            │
                           TCP
                            │
                         epoll
```

The guiding principle is:

> **Runact should provide the machinery required to safely and efficiently run concurrent work. It should not provide the application protocols that use that machinery.**

Build the smallest runtime that can power PaperOS and a remote AI-agent server, then expand only when a real workload proves that a new primitive belongs in Runact.