iron-shell 14.0.0

The world's first multi-agent shell with typed functional pipelines and multi-modal AI
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
//! Bounded extraction of one Rust function's top-level tail expression, and
//! its inverse: inlining one immutable local into its single following use.
use crate::{safety, value::Value};
use anyhow::Result;
use quote::{quote, ToTokens};
use syn::visit::Visit;
fn bad(name: &str, message: &str) -> anyhow::Error {
    safety::arg_err(format!("{name}: {message}"))
}
fn budget(name: &str) -> anyhow::Error {
    safety::budget_exceeded(name, "input/output exceeds syntax-edit limits".into(), "supply source <=64 KiB/256 punctuation, name <=256 bytes/32 punctuation, type <=16 KiB/128 punctuation")
}
fn text<'a>(name: &str, value: &'a Value, bytes: usize, punctuation: usize) -> Result<&'a str> {
    let Value::Str(value) = value else {
        return Err(bad(name, "arguments must be Strings"));
    };
    if value.len() > bytes || value.bytes().filter(u8::is_ascii_punctuation).count() > punctuation {
        return Err(budget(name));
    }
    Ok(value)
}
fn normalized(ident: &syn::Ident) -> String {
    ident.to_string().trim_start_matches("r#").to_string()
}
struct Guard<'a> {
    name: &'a str,
    count: usize,
    opaque: bool,
}
impl<'ast> Visit<'ast> for Guard<'_> {
    fn visit_ident(&mut self, ident: &'ast syn::Ident) {
        self.count += usize::from(normalized(ident) == self.name);
    }
    fn visit_macro(&mut self, _: &'ast syn::Macro) {
        self.opaque = true;
    }
    fn visit_item(&mut self, _: &'ast syn::Item) {
        self.opaque = true;
    }
    fn visit_attribute(&mut self, attribute: &'ast syn::Attribute) {
        let doc = attribute.path().is_ident("doc")
            && matches!(&attribute.meta, syn::Meta::NameValue(value) if matches!(&value.value, syn::Expr::Lit(value) if matches!(value.lit, syn::Lit::Str(_))));
        self.opaque |= !doc;
    }
    fn visit_expr(&mut self, value: &'ast syn::Expr) {
        if matches!(value, syn::Expr::Verbatim(_)) {
            self.opaque = true;
        } else {
            syn::visit::visit_expr(self, value);
        }
    }
    fn visit_pat(&mut self, value: &'ast syn::Pat) {
        if matches!(value, syn::Pat::Verbatim(_)) {
            self.opaque = true;
        } else {
            syn::visit::visit_pat(self, value);
        }
    }
    fn visit_type(&mut self, value: &'ast syn::Type) {
        if matches!(value, syn::Type::Verbatim(_)) {
            self.opaque = true;
        } else {
            syn::visit::visit_type(self, value);
        }
    }
}
pub(super) fn run(name: &str, args: Vec<Value>, input: Option<Value>) -> Result<Value> {
    if name == "refactor_inline" {
        return inline(name, args, input);
    }
    let constant = name == "refactor_extract_constant";
    if args.len() != if constant { 3 } else { 2 } || input.is_some() {
        return Err(bad(name, "invalid argument count or pipeline input"));
    }
    let source = text(name, &args[0], 65536, 256)?;
    let mut function = syn::parse_str::<syn::ItemFn>(source)
        .map_err(|_| bad(name, "expected exactly one Rust free function with body"))?;
    if function.sig.variadic.is_some()
        || function
            .sig
            .inputs
            .iter()
            .any(|arg| matches!(arg, syn::FnArg::Receiver(_)))
    {
        return Err(bad(name, "receivers and variadics are unsupported"));
    }
    let binding = syn::parse_str::<syn::Ident>(text(name, &args[1], 256, 32)?)
        .map_err(|_| bad(name, "expected Rust binding identifier"))?;
    let binding_name = normalized(&binding);
    let mut guard = Guard {
        name: &binding_name,
        count: 0,
        opaque: false,
    };
    guard.visit_item_fn(&function);
    let ty = if constant {
        let ty = syn::parse_str::<syn::Type>(text(name, &args[2], 16384, 128)?)
            .map_err(|_| bad(name, "invalid explicit constant type"))?;
        guard.visit_type(&ty);
        Some(ty)
    } else {
        None
    };
    if guard.count > 0 || guard.opaque {
        return Err(bad(
            name,
            "binding identifier already occurs or syntax has opaque macros/attributes/nested items",
        ));
    }
    let Some(syn::Stmt::Expr(_, None)) = function.block.stmts.last() else {
        return Err(bad(
            name,
            "function requires a top-level tail expression without semicolon",
        ));
    };
    let syn::Stmt::Expr(expression, _) = function.block.stmts.pop().unwrap() else {
        unreachable!()
    };
    let declaration = if let Some(ty) = ty {
        quote!(const #binding: #ty = #expression;)
    } else {
        quote!(let #binding = #expression;)
    };
    let declaration = syn::parse2::<syn::Stmt>(declaration)
        .map_err(|_| bad(name, "cannot construct binding declaration"))?;
    function.block.stmts.push(declaration);
    function.block.stmts.push(syn::Stmt::Expr(
        syn::parse2(quote!(#binding)).map_err(|_| bad(name, "cannot construct tail expression"))?,
        None,
    ));
    let output = function.to_token_stream().to_string();
    if output.len() > 1048576 {
        return Err(budget(name));
    }
    syn::parse_str::<syn::ItemFn>(&output)
        .map_err(|_| bad(name, "output is not a Rust function"))?;
    Ok(Value::Str(output))
}
/// Literals and place reads: evaluating one earlier cannot cause another
/// expression's effects, and cannot observe them unless that expression is
/// itself effectful.
fn inert(expr: &syn::Expr) -> bool {
    match expr {
        syn::Expr::Lit(_) | syn::Expr::Path(_) => true,
        syn::Expr::Paren(e) => inert(&e.expr),
        syn::Expr::Group(e) => inert(&e.expr),
        syn::Expr::Field(e) => inert(&e.base),
        syn::Expr::Reference(e) => e.mutability.is_none() && inert(&e.expr),
        _ => false,
    }
}
/// Syntax without calls, assignments, blocks, mutable borrows or control
/// flow; operator traits are assumed not to mutate unrelated locals.
fn effect_free(expr: &syn::Expr) -> bool {
    match expr {
        syn::Expr::Lit(_) | syn::Expr::Path(_) => true,
        syn::Expr::Paren(e) => effect_free(&e.expr),
        syn::Expr::Group(e) => effect_free(&e.expr),
        syn::Expr::Field(e) => effect_free(&e.base),
        syn::Expr::Reference(e) => e.mutability.is_none() && effect_free(&e.expr),
        syn::Expr::Unary(e) => !matches!(e.op, syn::UnOp::Deref(_)) && effect_free(&e.expr),
        syn::Expr::Binary(e) => !assignment(&e.op) && effect_free(&e.left) && effect_free(&e.right),
        syn::Expr::Cast(e) => effect_free(&e.expr),
        syn::Expr::Tuple(e) => e.elems.iter().all(effect_free),
        syn::Expr::Array(e) => e.elems.iter().all(effect_free),
        _ => false,
    }
}
fn assignment(op: &syn::BinOp) -> bool {
    use syn::BinOp::*;
    matches!(
        op,
        AddAssign(_)
            | SubAssign(_)
            | MulAssign(_)
            | DivAssign(_)
            | RemAssign(_)
            | BitXorAssign(_)
            | BitAndAssign(_)
            | BitOrAssign(_)
            | ShlAssign(_)
            | ShrAssign(_)
    )
}
/// Primary and postfix forms, which need no parentheses as an operand.
/// Block-like forms are excluded: one could begin an expression statement.
fn atomic(expr: &syn::Expr) -> bool {
    matches!(
        expr,
        syn::Expr::Lit(_)
            | syn::Expr::Path(_)
            | syn::Expr::Paren(_)
            | syn::Expr::Call(_)
            | syn::Expr::MethodCall(_)
            | syn::Expr::Field(_)
            | syn::Expr::Index(_)
            | syn::Expr::Tuple(_)
            | syn::Expr::Array(_)
            | syn::Expr::Try(_)
    )
}
/// Whether a pattern takes its whole initializer by value: `_`, `ref` and
/// destructuring leave the place alive until the end of its scope.
fn moves_whole(pat: &syn::Pat) -> bool {
    match pat {
        syn::Pat::Ident(p) => p.by_ref.is_none() && p.subpat.is_none(),
        syn::Pat::Type(p) => moves_whole(&p.pat),
        _ => false,
    }
}
/// Operators that consume their operands; comparisons borrow them.
fn by_value(op: &syn::BinOp) -> bool {
    use syn::BinOp::*;
    matches!(
        op,
        Add(_)
            | Sub(_)
            | Mul(_)
            | Div(_)
            | Rem(_)
            | BitXor(_)
            | BitAnd(_)
            | BitOr(_)
            | Shl(_)
            | Shr(_)
    )
}
struct Inline<'a> {
    name: &'a str,
    value: Option<syn::Expr>,
    skipped: bool,
}
impl Inline<'_> {
    fn is_use(&self, expr: &syn::Expr) -> bool {
        matches!(expr, syn::Expr::Path(p) if p.qself.is_none()
            && p.path.get_ident().is_some_and(|i| normalized(i) == self.name))
    }
    /// Operands in evaluation order: descend into the first one that is not
    /// inert, recording whether inert operands now evaluate before the value.
    fn operands<'e>(
        &mut self,
        operands: impl IntoIterator<Item = &'e mut syn::Expr>,
        operator: bool,
    ) -> bool {
        for operand in operands {
            if self.replace(operand, operator) {
                return true;
            }
            if !inert(operand) {
                return false;
            }
            self.skipped = true;
        }
        false
    }
    /// Replace the use only where it is evaluated first, exactly once,
    /// unconditionally and consumed by value, so that neither evaluation
    /// order nor drop timing changes. An operator's operand is parenthesized
    /// unless the value is atomic.
    fn replace(&mut self, expr: &mut syn::Expr, operator: bool) -> bool {
        if self.is_use(expr) {
            let value = self.value.take().unwrap();
            *expr = if operator && !atomic(&value) {
                syn::Expr::Paren(syn::ExprParen {
                    attrs: Vec::new(),
                    paren_token: Default::default(),
                    expr: Box::new(value),
                })
            } else {
                value
            };
            return true;
        }
        match expr {
            syn::Expr::Paren(e) => self.replace(&mut e.expr, false),
            syn::Expr::Group(e) => self.replace(&mut e.expr, operator),
            syn::Expr::Unary(e) if !matches!(e.op, syn::UnOp::Deref(_)) => {
                self.replace(&mut e.expr, true)
            }
            syn::Expr::Cast(e) => self.replace(&mut e.expr, true),
            syn::Expr::Try(e) => self.replace(&mut e.expr, true),
            syn::Expr::Return(e) => e.expr.as_mut().is_some_and(|e| self.replace(e, false)),
            syn::Expr::Break(e) => e.expr.as_mut().is_some_and(|e| self.replace(e, false)),
            syn::Expr::If(e) => {
                !matches!(*e.cond, syn::Expr::Let(_)) && self.replace(&mut e.cond, false)
            }
            // The right operand of && and || is conditional.
            syn::Expr::Binary(e) if matches!(e.op, syn::BinOp::And(_) | syn::BinOp::Or(_)) => {
                self.replace(&mut e.left, true)
            }
            syn::Expr::Binary(e) if by_value(&e.op) => {
                self.operands([&mut *e.left, &mut *e.right], true)
            }
            syn::Expr::Index(e) if inert(&e.expr) && !self.is_use(&e.expr) => {
                self.skipped = true;
                self.replace(&mut e.index, false)
            }
            syn::Expr::Call(e) if inert(&e.func) && !self.is_use(&e.func) => {
                self.skipped = true;
                self.operands(e.args.iter_mut(), false)
            }
            syn::Expr::MethodCall(e) if inert(&e.receiver) && !self.is_use(&e.receiver) => {
                self.skipped = true;
                self.operands(e.args.iter_mut(), false)
            }
            syn::Expr::Tuple(e) => self.operands(e.elems.iter_mut(), false),
            syn::Expr::Array(e) => self.operands(e.elems.iter_mut(), false),
            syn::Expr::Struct(e) if e.rest.is_none() => {
                self.operands(e.fields.iter_mut().map(|field| &mut field.expr), false)
            }
            _ => false,
        }
    }
}
fn inline(name: &str, args: Vec<Value>, input: Option<Value>) -> Result<Value> {
    if args.len() != 2 || input.is_some() {
        return Err(bad(name, "invalid argument count or pipeline input"));
    }
    let source = text(name, &args[0], 65536, 256)?;
    let mut function = syn::parse_str::<syn::ItemFn>(source)
        .map_err(|_| bad(name, "expected exactly one Rust free function with body"))?;
    let binding = syn::parse_str::<syn::Ident>(text(name, &args[1], 256, 32)?)
        .map_err(|_| bad(name, "expected Rust binding identifier"))?;
    let binding_name = normalized(&binding);
    let mut guard = Guard {
        name: &binding_name,
        count: 0,
        opaque: false,
    };
    guard.visit_item_fn(&function);
    if guard.opaque {
        return Err(bad(
            name,
            "syntax has opaque macros/attributes/nested items",
        ));
    }
    if guard.count != 2 {
        return Err(bad(
            name,
            "identifier must occur exactly twice in the function: its binding and one use",
        ));
    }
    let position = function.block.stmts.iter().position(|stmt| {
        matches!(stmt, syn::Stmt::Local(local)
            if matches!(&local.pat, syn::Pat::Ident(p) if normalized(&p.ident) == binding_name))
    });
    let Some(position) = position else {
        return Err(bad(name, "no top-level let binding of the identifier"));
    };
    let syn::Stmt::Local(local) = function.block.stmts.remove(position) else {
        unreachable!()
    };
    let syn::Pat::Ident(pattern) = &local.pat else {
        unreachable!()
    };
    let simple = pattern.by_ref.is_none()
        && pattern.mutability.is_none()
        && pattern.subpat.is_none()
        && local.attrs.is_empty();
    let Some(init) = local.init.filter(|init| simple && init.diverge.is_none()) else {
        return Err(bad(
            name,
            "binding must be let NAME = EXPR; without mut, ref, subpattern, type, attributes or else",
        ));
    };
    let value = *init.expr;
    let effect_free = effect_free(&value);
    let mut inline = Inline {
        name: &binding_name,
        value: Some(value),
        skipped: false,
    };
    let replaced = match function.block.stmts.get_mut(position) {
        Some(syn::Stmt::Local(next)) => next.init.as_mut().is_some_and(|init| {
            (moves_whole(&next.pat) || !inline.is_use(&init.expr))
                && inline.replace(&mut init.expr, false)
        }),
        // A place statement `NAME;` does not move, so the value outlives it.
        Some(syn::Stmt::Expr(next, semicolon)) => {
            (semicolon.is_none() || !inline.is_use(next)) && inline.replace(next, false)
        }
        _ => false,
    };
    if !replaced {
        return Err(bad(
            name,
            "the use must be in the next statement, evaluated first, once, unconditionally and by value",
        ));
    }
    if inline.skipped && !effect_free {
        return Err(bad(
            name,
            "an effectful value cannot move after operands that are evaluated before its use",
        ));
    }
    let output = function.to_token_stream().to_string();
    if output.len() > 1048576 {
        return Err(budget(name));
    }
    syn::parse_str::<syn::ItemFn>(&output)
        .map_err(|_| bad(name, "output is not a Rust function"))?;
    Ok(Value::Str(output))
}