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
//! The `Worker` trait — the shared interface for the execution units
//! each spawner keeps internally.
//!
//! ## Roles — the boundary between the Engine view and the Spawner's
//! internal view
//!
//! - **Engine view.** Only `SpawnerAdapter` is visible. The engine does
//! not know about Workers, and it does not care about their shape.
//! - **Spawner's internal view.** Each `SpawnerAdapter::spawn` builds a
//! concrete Worker internally (`ChildProcessWorker` / `ClosureWorker`
//! / `OperatorWorker` and friends), type-erases it as
//! `Box<dyn Worker>`, and returns that. This trait fixes the interface
//! so the Worker shape does not drift between spawner implementations.
//!
//! ## Worker lifetime semantics
//!
//! The current contract is "one spawn = one worker = one join". At
//! `spawn()` time the worker has already spun up an internal tokio task
//! (it is already running); the caller just needs to `join()` and wait
//! for the completion signal. The value comes from
//! `engine.output_tail(task_id, attempt)` via `OutputEvent::Final`
//! — the oneshot channel carries the signal, not the
//! value.
//!
//! Extending to "one worker = N invocations" (calling the same worker
//! multiple times while the token's TTL is alive) is a carry on a
//! separate axis. That was the original design intent
//! v6.md:174, but the shape was collapsed for the sake of implementation
//! simplicity. The route when it is needed: add
//! `async fn invoke(&mut self, token, prompt) -> WorkerResult` to the
//! trait and redefine `join` as "the last invocation's completion plus
//! cleanup".
//!
//! ## `WorkerJoinHandler` — the canonical shape shared by the three
//! spawners today
//!
//! All three current spawners (Shell / InProc / Operator) call
//! `tokio::spawn` internally and push their completion signal through a
//! oneshot channel. `WorkerJoinHandler` is the helper that wraps that
//! shape into a `dyn Worker`. We share the helper until spawner
//! implementations need to define their own Worker structs; further
//! specialisation is a future carry.
use crateWorkerId;
use crateWorkerError;
use async_trait;
use oneshot;
use CancellationToken;
/// Shared interface for the execution units spawners launch internally.
///
/// Every spawner implementation returns a concrete Worker struct that
/// implements this trait (today that is `WorkerJoinHandler`) as a
/// `Box<dyn Worker>`. The caller (the engine) only interacts with
/// workers through three operations: `id()` / `cancel_token()` /
/// `join()`.
/// **Handler for a Worker's async completion signal.** A building
/// block; it does not implement `Worker` itself. Holds the
/// `(worker_id, cancel token, oneshot receiver)` triple and is embedded
/// by every per-kind Worker (`AgentBlockWorker` / `LuaWorker` /
/// `RustFnWorker` / `ProcessWorker` / `OperatorWorker`).
///
/// "The Worker that actually does the work" and "the mechanism that
/// waits for its async completion" are two different concepts. This
/// struct is dedicated to the latter; the former is expressed by
/// per-kind Worker structs — one type per `AgentKind`, each hiding its
/// kind-specific state (SDK quirks, VM state, child-process handles,
/// etc.) inside itself.
/// Generic Worker used only on the middleware (`wrap_join`) wrap path,
/// so kind-agnostic post-processing wrap results can be returned as
/// `Box<dyn Worker>`. Unlike a per-kind Worker, this does not represent
/// "a specific kind's execution" — it is a thin wrapper that layers a
/// post-processor on top of an existing Worker.
///
/// Named after its role: the "Worker for the middleware path" — the
/// type boxed as the return value by `wrap_join` consumers (Audit /
/// MainAI / Senior / LongHold / Lua after-hook, and so on).
/// Helper that wraps the inner Worker's `join()` completion signal in a
/// post-processor and returns a fresh `Box<dyn Worker>`. All the
/// middleware wrap paths (Audit / MainAI / Senior / LongHold / Lua
/// after-hook, and so on) go through this helper for consistency.
///
/// The cancel token is inherited from the inner Worker verbatim, so
/// cancelling from outside the engine still reaches the inner Worker.
/// `worker_id` is also carried over from the inner Worker.