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
//! `alc.fork()` — parallel multi-VM strategy execution.
//!
//! Spawns N independent Lua VMs, each running one strategy with the same ctx.
//! LLM requests from all children are batched and sent through the parent's
//! llm_tx, achieving true LLM parallelism.
use std::path::PathBuf;
use std::sync::Arc;
use algocline_core::{BudgetHandle, ExecutionMetrics, QueryId};
use mlua::prelude::*;
use mlua::LuaSerdeExt;
use mlua_isle::{AsyncIsle, AsyncIsleDriver};
use mlua_pkg::Registry;
use super::{register, BridgeConfig, PRELUDE};
use crate::card::FileCardStore;
use crate::llm_bridge::{LlmRequest, QueryRequest};
use crate::resolver_factory::make_resolver;
use crate::state::JsonFileStore;
use crate::variant_pkg::{register_variant_pkgs, VariantPkg};
/// Event from a child VM during fork execution.
enum ForkEvent {
/// Child VM emitted an LLM request.
Request {
vm_index: usize,
queries: Vec<ForkQuery>,
},
/// Child VM completed execution.
Completed {
vm_index: usize,
result: Result<String, String>,
},
}
/// A single LLM query from a fork child, with the child's response channel.
struct ForkQuery {
prompt: String,
system: Option<String>,
max_tokens: u32,
grounded: bool,
underspecified: bool,
child_resp_tx: tokio::sync::oneshot::Sender<Result<String, String>>,
}
/// Register `alc.fork(strategies, ctx, opts?)` onto the given table.
///
/// Lua usage:
/// local results = alc.fork({"cot", "reflect", "cove"}, ctx)
/// -- results = { {strategy="cot", result=...}, {strategy="reflect", result=...}, ... }
///
/// local results = alc.fork({"cot", "reflect"}, ctx, { on_error = "skip" })
#[allow(clippy::too_many_arguments)]
pub(crate) fn register_fork(
lua: &Lua,
alc_table: &LuaTable,
llm_tx: tokio::sync::mpsc::Sender<LlmRequest>,
budget: BudgetHandle,
lib_paths: Vec<PathBuf>,
variant_pkgs: Vec<VariantPkg>,
state_store: Arc<JsonFileStore>,
card_store: Arc<FileCardStore>,
scenarios_dir: PathBuf,
) -> LuaResult<()> {
let fork_fn = lua.create_async_function(
move |lua, (strategies, ctx, opts): (LuaTable, LuaTable, Option<LuaTable>)| {
let parent_tx = llm_tx.clone();
let bh = budget.clone();
let paths = lib_paths.clone();
let variants = variant_pkgs.clone();
let state_store = Arc::clone(&state_store);
let card_store = Arc::clone(&card_store);
let scenarios_dir = scenarios_dir.clone();
async move {
let n = strategies.len()? as usize;
if n == 0 {
return Err(LuaError::external(
"alc.fork: strategies must be a non-empty array",
));
}
let on_error = opts
.as_ref()
.and_then(|o| o.get::<String>("on_error").ok())
.unwrap_or_else(|| "skip".into());
// Collect strategy names (validated to prevent Lua injection)
let mut strategy_names = Vec::with_capacity(n);
for i in 1..=n {
let name: String = strategies.get(i)?;
if !name.chars().all(|c| c.is_ascii_alphanumeric() || c == '_') {
return Err(LuaError::external(format!(
"alc.fork: invalid strategy name '{}' \
(only alphanumeric and underscore allowed)",
name
)));
}
strategy_names.push(name);
}
// Serialize ctx to JSON for child VMs
let ctx_json: serde_json::Value = lua.from_value(LuaValue::Table(ctx))?;
// Aggregated event channel
let (event_tx, mut event_rx) =
tokio::sync::mpsc::channel::<ForkEvent>(16 * n.max(1));
// Spawn child VMs and their event-forwarding tasks.
// drivers must stay alive to keep child VMs running.
let mut drivers: Vec<AsyncIsleDriver> = Vec::with_capacity(n);
for (vm_idx, strategy) in strategy_names.iter().enumerate() {
let (child_llm_tx, mut child_llm_rx) =
tokio::sync::mpsc::channel::<LlmRequest>(16);
// Spawn child VM
let child_paths = paths.clone();
let child_variants = variants.clone();
let (child_isle, child_driver) = AsyncIsle::spawn(move |child_lua| {
let mut reg = Registry::new();
// Variant pkgs first (highest priority — alc.local.toml wins).
register_variant_pkgs(&mut reg, &child_variants);
for path in &child_paths {
// Use the shared resolver factory so fork children honour
// the same ALC_PKG_STRICT / SymlinkAwareSandbox policy as
// the parent session's Executor-built VM. Prior to this,
// fork always took the strict `FsResolver::new` path,
// silently diverging when the parent was sandboxed.
if let Some(resolver) = make_resolver(path) {
reg.add(resolver);
} else {
tracing::warn!(
"alc.fork: resolver init failed for {}",
path.display()
);
}
}
reg.install(child_lua)?;
Ok(())
})
.await
.map_err(|e| {
LuaError::external(format!(
"alc.fork: VM spawn failed for '{strategy}': {e}"
))
})?;
// Setup child VM: register alc.*, set ctx, load prelude
let child_ctx = ctx_json.clone();
let child_metrics = ExecutionMetrics::new();
let child_config = BridgeConfig {
llm_tx: Some(child_llm_tx),
ns: format!("fork-{vm_idx}"),
custom_metrics: child_metrics.custom_metrics_handle(),
budget: bh.clone(),
progress: child_metrics.progress_handle(),
lib_paths: vec![], // Children don't need to fork further
variant_pkgs: vec![], // Children don't need to fork further
state_store: Arc::clone(&state_store),
card_store: Arc::clone(&card_store),
scenarios_dir: scenarios_dir.clone(),
};
child_isle
.exec(move |child_lua| {
let alc_table = child_lua.create_table()?;
register(child_lua, &alc_table, child_config)?;
child_lua.globals().set("alc", alc_table)?;
let ctx_value = child_lua.to_value(&child_ctx)?;
child_lua.globals().set("ctx", ctx_value)?;
child_lua.load(PRELUDE).exec().map_err(|e| {
mlua_isle::IsleError::Lua(format!("Prelude load failed: {e}"))
})?;
Ok("ok".to_string())
})
.await
.map_err(|e| {
LuaError::external(format!(
"alc.fork: setup failed for '{strategy}': {e}"
))
})?;
// Execute strategy as coroutine
let code = format!(
"return alc.json_encode((function() \
local s = require('{}'); return s.run(ctx) \
end)())",
strategy
);
let exec_task = child_isle.spawn_coroutine_eval(&code);
drop(child_isle); // Release isle handle; driver keeps VM alive
drivers.push(child_driver);
// Spawn event-forwarding task for this child.
// Task terminates naturally when event_tx is dropped (channel close).
let evt_tx = event_tx.clone();
tokio::spawn(async move {
let mut exec_task = exec_task;
loop {
tokio::select! {
biased;
result = &mut exec_task => {
let mapped = match result {
Ok(json_str) => Ok(json_str),
Err(e) => Err(e.to_string()),
};
let _ = evt_tx.send(ForkEvent::Completed {
vm_index: vm_idx,
result: mapped,
}).await;
return;
}
Some(req) = child_llm_rx.recv() => {
let fork_queries = req.queries.into_iter().map(|qr| {
ForkQuery {
prompt: qr.prompt,
system: qr.system,
max_tokens: qr.max_tokens,
grounded: qr.grounded,
underspecified: qr.underspecified,
child_resp_tx: qr.resp_tx,
}
}).collect();
let _ = evt_tx.send(ForkEvent::Request {
vm_index: vm_idx,
queries: fork_queries,
}).await;
}
}
}
});
}
drop(event_tx); // Only child tasks hold senders now
// Multiplexer: collect child events, batch LLM requests, distribute responses
let mut results: Vec<Option<Result<serde_json::Value, String>>> = vec![None; n];
let mut seq_counter: Vec<usize> = vec![0; n];
while results.iter().any(|r| r.is_none()) {
// Wait for first event
let first = match event_rx.recv().await {
Some(evt) => evt,
None => break, // All senders dropped
};
// Collect first + drain any immediately ready events
let mut events = vec![first];
while let Ok(evt) = event_rx.try_recv() {
events.push(evt);
}
// Process events: separate completions from requests
let mut batch_queries: Vec<QueryRequest> = Vec::new();
let mut parent_resp_rxs: Vec<
tokio::sync::oneshot::Receiver<Result<String, String>>,
> = Vec::new();
let mut child_resp_txs: Vec<
tokio::sync::oneshot::Sender<Result<String, String>>,
> = Vec::new();
for event in events {
match event {
ForkEvent::Completed { vm_index, result } => {
results[vm_index] = Some(match result {
Ok(json_str) => serde_json::from_str(&json_str)
.map_err(|e| format!("JSON parse: {e}")),
Err(e) => Err(e),
});
}
ForkEvent::Request { vm_index, queries } => {
for fq in queries {
let fork_id = QueryId::fork(vm_index, seq_counter[vm_index]);
seq_counter[vm_index] += 1;
let (parent_resp_tx, parent_resp_rx) =
tokio::sync::oneshot::channel();
parent_resp_rxs.push(parent_resp_rx);
child_resp_txs.push(fq.child_resp_tx);
batch_queries.push(QueryRequest {
id: fork_id,
prompt: fq.prompt,
system: fq.system,
max_tokens: fq.max_tokens,
grounded: fq.grounded,
underspecified: fq.underspecified,
resp_tx: parent_resp_tx,
});
}
}
}
}
if batch_queries.is_empty() {
continue;
}
// Send batch to parent session (causes parent to pause)
parent_tx
.send(LlmRequest {
queries: batch_queries,
})
.await
.map_err(|e| {
LuaError::external(format!("alc.fork: LLM bridge send failed: {e}"))
})?;
// Await all responses from host, forward to children
for (parent_rx, child_tx) in parent_resp_rxs.into_iter().zip(child_resp_txs) {
match parent_rx.await {
Ok(result) => {
let _ = child_tx.send(result);
}
Err(e) => {
let _ = child_tx.send(Err(format!("alc.fork: response lost: {e}")));
}
}
}
}
// Keep drivers alive until all results collected
drop(drivers);
// Build result table
let result_table = lua.create_table()?;
for (i, (name, result)) in
strategy_names.iter().zip(results.into_iter()).enumerate()
{
let entry = lua.create_table()?;
entry.set("strategy", name.as_str())?;
match result {
Some(Ok(val)) => {
let lua_val = lua.to_value(&val)?;
entry.set("result", lua_val)?;
entry.set("ok", true)?;
}
Some(Err(err)) => {
if on_error == "abort" {
return Err(LuaError::external(format!(
"alc.fork: strategy '{}' failed: {}",
name, err
)));
}
entry.set("error", err)?;
entry.set("ok", false)?;
}
None => {
entry.set("error", "no result (channel closed)")?;
entry.set("ok", false)?;
}
}
result_table.set(i + 1, entry)?;
}
Ok(result_table)
}
},
)?;
alc_table.set("fork", fork_fn)?;
Ok(())
}