localharness 0.46.0

Agents that own themselves: one Rust crate that's both an agent SDK (streaming, tools, hooks, policies, triggers, MCP) and a wallet-owning, self-sovereign agent that runs in the browser.
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
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
use std::collections::HashMap;
use crate::error_codes as codes;
use crate::rustlite::{CompileError, Span};
use crate::rustlite::ast::*;

pub fn check(module: &Module) -> Result<TypedModule, CompileError> {
    let mut ctx = TypeContext::new();
    ctx.register_module(module)?;
    ctx.check_module(module)
}

#[derive(Debug, Clone, PartialEq)]
pub enum ResolvedType {
    I32,
    I64,
    F32,
    F64,
    Bool,
    String,
    Void,
    Never,
    Struct { name: String, fields: Vec<(String, ResolvedType)> },
    Enum { name: String, variants: Vec<(String, VariantShape)> },
    Tuple(Vec<ResolvedType>),
    /// `[T; N]` — fixed-size array. The runtime value is a base pointer (i32)
    /// into linear memory.
    Array(Box<ResolvedType>, usize),
}

#[derive(Debug, Clone, PartialEq)]
pub enum VariantShape {
    Unit,
    Tuple(Vec<ResolvedType>),
    Struct(Vec<(String, ResolvedType)>),
}

#[derive(Debug, Clone)]
pub struct TypedModule {
    pub uses: Vec<UseDecl>,
    pub structs: Vec<TypedStruct>,
    pub enums: Vec<TypedEnum>,
    pub functions: Vec<TypedFn>,
    pub consts: Vec<TypedConst>,
}

#[derive(Debug, Clone)]
pub struct TypedStruct {
    pub name: String,
    pub fields: Vec<(String, ResolvedType)>,
}

#[derive(Debug, Clone)]
pub struct TypedEnum {
    pub name: String,
    pub variants: Vec<(String, VariantShape)>,
}

#[derive(Debug, Clone)]
pub struct TypedFn {
    pub name: String,
    pub params: Vec<(String, ResolvedType)>,
    pub ret_type: ResolvedType,
    pub body: TypedBlock,
}

#[derive(Debug, Clone)]
pub struct TypedConst {
    pub name: String,
    pub ty: ResolvedType,
    pub value: TypedExpr,
}

#[derive(Debug, Clone)]
pub struct TypedBlock {
    pub stmts: Vec<TypedStmt>,
    pub tail: Option<Box<TypedExpr>>,
    pub ty: ResolvedType,
}

// Variants carry whole `TypedExpr`s of differing arity (`Assign` holds two,
// `Return` an `Option`), so their sizes differ. Boxing to equalize would force
// a Box at every construction + match site across the codegen pass for no real
// gain — rustlite programs are tiny, so the per-stmt slack is irrelevant.
#[allow(clippy::large_enum_variant)]
#[derive(Debug, Clone)]
pub enum TypedStmt {
    Let { name: String, mutable: bool, ty: ResolvedType, init: TypedExpr },
    /// `place = value`. `index` is `Some(typed_i32_expr)` for an indexed array
    /// write `place[index] = value` (mirrors the read's `Index` address math),
    /// `None` for a plain variable / struct-field assignment.
    Assign { place: Place, index: Option<TypedExpr>, value: TypedExpr },
    Return { value: Option<TypedExpr> },
    Expr { expr: TypedExpr },
}

#[derive(Debug, Clone)]
pub struct TypedExpr {
    pub kind: TypedExprKind,
    pub ty: ResolvedType,
    pub span: Span,
}

#[derive(Debug, Clone)]
pub enum TypedExprKind {
    IntLit(i64),
    FloatLit(f64),
    StringLit(String),
    BoolLit(bool),
    Var(String),
    Path(Vec<String>),

    FieldAccess { object: Box<TypedExpr>, field: String, field_index: usize },

    Call { func: Box<TypedExpr>, args: Vec<TypedExpr> },
    /// A call to a `host::<module>::<func>` builtin (e.g.
    /// `display::fill_rect(...)`). Resolved against the host-function
    /// table, not the module's own functions — codegen emits it as a
    /// wasm import call. `module`/`func` are the resolved names
    /// (leading `host::` stripped); `ret_ty` is the host signature's
    /// return so codegen can declare the import type without its own
    /// table.
    HostCall { module: String, func: String, args: Vec<TypedExpr>, ret_ty: ResolvedType },
    MethodCall { object: Box<TypedExpr>, method: String, args: Vec<TypedExpr> },

    StructLit { name: String, fields: Vec<(String, TypedExpr)> },

    TupleLit(Vec<TypedExpr>),
    /// `[e0, e1, …]` — stored to a static linear-memory region at codegen time;
    /// the expression's value is the region's base pointer (i32).
    ArrayLit(Vec<TypedExpr>),
    /// `[value; count]` — reserve `count` i32 slots in a static region, fill each
    /// with `value`; the expression's value is the region's base pointer (i32).
    ArrayRepeat { value: Box<TypedExpr>, count: usize },
    /// `base[index]` — reads `i32.load(base + index*4)`.
    Index { base: Box<TypedExpr>, index: Box<TypedExpr> },

    BinOp { op: BinOp, lhs: Box<TypedExpr>, rhs: Box<TypedExpr> },
    UnaryOp { op: UnaryOp, operand: Box<TypedExpr> },
    /// `expr as <node.ty>` — numeric conversion. Source = inner expr's `.ty`.
    Cast { expr: Box<TypedExpr> },

    If { cond: Box<TypedExpr>, then_block: TypedBlock, else_block: Option<TypedElse> },
    Match { scrutinee: Box<TypedExpr>, arms: Vec<TypedMatchArm>, result_ty: ResolvedType },
    While { cond: Box<TypedExpr>, body: TypedBlock },
    Loop { body: TypedBlock },
    Break { value: Option<Box<TypedExpr>> },
    Continue,
    Block(TypedBlock),
}

#[derive(Debug, Clone)]
pub enum TypedElse {
    Block(TypedBlock),
    If(Box<TypedExpr>),
}

#[derive(Debug, Clone)]
pub struct TypedMatchArm {
    pub pattern: Pattern,
    pub body: TypedExpr,
}

#[derive(Debug, Clone)]
struct FnSig {
    params: Vec<ResolvedType>,
    ret: ResolvedType,
}

struct TypeContext {
    types: HashMap<String, ResolvedType>,
    functions: HashMap<String, FnSig>,
    locals: Vec<HashMap<String, (ResolvedType, bool)>>,
    current_return: ResolvedType,
    /// Top-level `const`s, INLINED at each reference (name → typed value). A
    /// const is a compile-time value, so a `Var` naming one returns a clone of
    /// its value expr — no runtime global, no codegen change.
    consts: HashMap<String, TypedExpr>,
}

impl TypeContext {
    fn new() -> Self {
        Self {
            types: HashMap::new(),
            functions: HashMap::new(),
            locals: Vec::new(),
            current_return: ResolvedType::Void,
            consts: HashMap::new(),
        }
    }

    fn push_scope(&mut self) {
        self.locals.push(HashMap::new());
    }

    fn pop_scope(&mut self) {
        self.locals.pop();
    }

    fn define_local(&mut self, name: &str, ty: ResolvedType, mutable: bool) {
        if let Some(scope) = self.locals.last_mut() {
            scope.insert(name.to_string(), (ty, mutable));
        }
    }

    fn lookup_local(&self, name: &str) -> Option<&(ResolvedType, bool)> {
        for scope in self.locals.iter().rev() {
            if let Some(entry) = scope.get(name) {
                return Some(entry);
            }
        }
        None
    }

    fn resolve_ty(&self, ty: &Ty) -> Result<ResolvedType, CompileError> {
        match ty {
            Ty::I32 => Ok(ResolvedType::I32),
            Ty::I64 => Ok(ResolvedType::I64),
            Ty::F32 => Ok(ResolvedType::F32),
            Ty::F64 => Ok(ResolvedType::F64),
            Ty::Bool => Ok(ResolvedType::Bool),
            Ty::String => Ok(ResolvedType::String),
            Ty::Named(name) => {
                self.types.get(name).cloned()
                    .ok_or_else(|| CompileError::new_code(codes::UNKNOWN_TYPE, format!("unknown type '{name}'")))
            }
            Ty::Tuple(tys) => {
                let resolved: Result<Vec<_>, _> = tys.iter().map(|t| self.resolve_ty(t)).collect();
                Ok(ResolvedType::Tuple(resolved?))
            }
            Ty::Array(elem, n) => {
                // v1: i32 elements only (matches the array-literal restriction);
                // the runtime value is an i32 base pointer.
                let elem_ty = self.resolve_ty(elem)?;
                if elem_ty != ResolvedType::I32 {
                    return Err(CompileError::new_code(
                        codes::BAD_INDEX,
                        format!("arrays support i32 elements for now, got {elem_ty:?}"),
                    ));
                }
                Ok(ResolvedType::Array(Box::new(elem_ty), *n))
            }
        }
    }

    fn register_module(&mut self, module: &Module) -> Result<(), CompileError> {
        // First pass: register all type and fn signatures
        for item in &module.items {
            match item {
                Item::Struct(s) => {
                    let fields: Result<Vec<(String, ResolvedType)>, CompileError> = s.fields.iter()
                        .map(|f| Ok((f.name.clone(), self.resolve_ty(&f.ty)?)))
                        .collect();
                    let ty = ResolvedType::Struct { name: s.name.clone(), fields: fields? };
                    self.types.insert(s.name.clone(), ty);
                }
                Item::Enum(e) => {
                    let variants: Result<Vec<(String, VariantShape)>, CompileError> = e.variants.iter()
                        .map(|v| {
                            let shape = match &v.payload {
                                VariantPayload::Unit => VariantShape::Unit,
                                VariantPayload::Tuple(tys) => {
                                    let resolved: Result<Vec<ResolvedType>, CompileError> = tys.iter().map(|t| self.resolve_ty(t)).collect();
                                    VariantShape::Tuple(resolved?)
                                }
                                VariantPayload::Struct(fields) => {
                                    let resolved: Result<Vec<(String, ResolvedType)>, CompileError> = fields.iter()
                                        .map(|f| Ok((f.name.clone(), self.resolve_ty(&f.ty)?)))
                                        .collect();
                                    VariantShape::Struct(resolved?)
                                }
                            };
                            Ok((v.name.clone(), shape))
                        })
                        .collect();
                    let ty = ResolvedType::Enum { name: e.name.clone(), variants: variants? };
                    self.types.insert(e.name.clone(), ty);
                }
                Item::Fn(f) => {
                    let params: Result<Vec<_>, _> = f.params.iter()
                        .map(|p| self.resolve_ty(&p.ty))
                        .collect();
                    let ret = f.ret_type.as_ref()
                        .map(|t| self.resolve_ty(t))
                        .transpose()?
                        .unwrap_or(ResolvedType::Void);
                    // RETURNING an array is unsound under the static-region model:
                    // an array value is just the base pointer of a per-AST-node
                    // region that is REUSED on every call, so two live results of
                    // the same array-returning fn alias and the second call
                    // silently clobbers the first (`let a = mk(1); let b = mk(2);`
                    // makes `a` read as 2). Reject it at the signature rather than
                    // emit code that corrupts. The intended pattern is an array
                    // PARAM the callee mutates in place (C-style, shared backing).
                    if let ResolvedType::Array(_, _) = ret {
                        return Err(CompileError::at_code(
                            codes::UNSUPPORTED_FEATURE,
                            format!(
                                "fn '{}': returning an array is unsupported (the static \
                                 region a returned array points into is reused every \
                                 call, so two results would alias and corrupt). Pass a \
                                 mutable array param for the callee to fill instead.",
                                f.name
                            ),
                            f.span,
                        ));
                    }
                    self.functions.insert(f.name.clone(), FnSig { params: params?, ret });
                }
                Item::Const(_) => {} // handled in check pass
            }
        }
        Ok(())
    }

    fn check_module(&mut self, module: &Module) -> Result<TypedModule, CompileError> {
        let mut structs = Vec::new();
        let mut enums = Vec::new();
        let mut functions = Vec::new();
        let mut consts = Vec::new();

        // Consts FIRST, so a function body resolves them no matter the source
        // order; each is registered in `self.consts` and inlined at its uses.
        for item in &module.items {
            if let Item::Const(c) = item {
                let ty = self.resolve_ty(&c.ty)?;
                self.push_scope();
                let value = self.check_expr(&c.value)?;
                self.pop_scope();
                if value.ty != ty {
                    return Err(CompileError::at_code(
                        codes::TYPE_MISMATCH,
                        format!("const type mismatch: expected {ty:?}, got {:?}", value.ty),
                        c.span,
                    ));
                }
                self.consts.insert(c.name.clone(), value.clone());
                consts.push(TypedConst { name: c.name.clone(), ty, value });
            }
        }

        for item in &module.items {
            match item {
                Item::Struct(s) => {
                    if let ResolvedType::Struct { name, fields } = self.types.get(&s.name).unwrap().clone() {
                        structs.push(TypedStruct { name, fields });
                    }
                }
                Item::Enum(e) => {
                    if let ResolvedType::Enum { name, variants } = self.types.get(&e.name).unwrap().clone() {
                        enums.push(TypedEnum { name, variants });
                    }
                }
                Item::Fn(f) => {
                    functions.push(self.check_fn(f)?);
                }
                Item::Const(_) => {} // processed in the consts-first pass above
            }
        }

        Ok(TypedModule { uses: module.uses.clone(), structs, enums, functions, consts })
    }

    fn check_fn(&mut self, f: &FnDecl) -> Result<TypedFn, CompileError> {
        let sig = self.functions.get(&f.name).unwrap().clone();
        self.current_return = sig.ret.clone();

        self.push_scope();
        let mut params = Vec::new();
        for (param, ty) in f.params.iter().zip(sig.params.iter()) {
            // An array param is an i32 base pointer aliasing the caller's
            // backing region (rustlite has no `&`/`&mut` distinction), so an
            // indexed write THROUGH it mutates shared memory — like C. Mark the
            // binding mutable so `a[i] = v` in the callee is allowed and visible
            // to the caller. Scalar params stay immutable (no reassignment).
            let mutable = matches!(ty, ResolvedType::Array(_, _));
            self.define_local(&param.name, ty.clone(), mutable);
            params.push((param.name.clone(), ty.clone()));
        }

        let body = self.check_block(&f.body)?;
        self.pop_scope();

        if sig.ret != ResolvedType::Void && body.ty != sig.ret && body.ty != ResolvedType::Never {
            return Err(CompileError::at_code(
                codes::TYPE_MISMATCH,
                format!("fn '{}': body returns {:?}, expected {:?}", f.name, body.ty, sig.ret),
                f.span,
            ));
        }

        Ok(TypedFn { name: f.name.clone(), params, ret_type: sig.ret, body })
    }

    fn check_block(&mut self, block: &Block) -> Result<TypedBlock, CompileError> {
        self.push_scope();
        let mut stmts = Vec::new();

        for stmt in &block.stmts {
            stmts.push(self.check_stmt(stmt)?);
        }

        let (tail, ty) = if let Some(tail_expr) = &block.tail {
            let typed = self.check_expr(tail_expr)?;
            let ty = typed.ty.clone();
            (Some(Box::new(typed)), ty)
        } else {
            (None, ResolvedType::Void)
        };

        self.pop_scope();
        Ok(TypedBlock { stmts, tail, ty })
    }

    fn check_stmt(&mut self, stmt: &Stmt) -> Result<TypedStmt, CompileError> {
        match stmt {
            Stmt::Let { name, mutable, ty, init, span } => {
                let init_typed = self.check_expr(init)?;
                let resolved_ty = if let Some(declared) = ty {
                    let declared = self.resolve_ty(declared)?;
                    if init_typed.ty != declared {
                        return Err(CompileError::at_code(
                            codes::TYPE_MISMATCH,
                            format!("let type mismatch: declared {:?}, got {:?}", declared, init_typed.ty),
                            *span,
                        ));
                    }
                    declared
                } else {
                    init_typed.ty.clone()
                };
                self.define_local(name, resolved_ty.clone(), *mutable);
                Ok(TypedStmt::Let { name: name.clone(), mutable: *mutable, ty: resolved_ty, init: init_typed })
            }
            Stmt::Assign { place, value, span } => {
                let (local_ty, is_mut) = self.lookup_local(&place.root)
                    .ok_or_else(|| CompileError::at_code(codes::UNDEFINED_VARIABLE, format!("undefined variable '{}'", place.root), *span))?
                    .clone();
                if !is_mut {
                    return Err(CompileError::at_code(codes::NOT_MUTABLE, format!("'{}' is not mutable", place.root), *span));
                }
                // Resolve the base type at `root[.fields…]`.
                let mut target_ty = local_ty;
                for field in &place.fields {
                    target_ty = self.field_type(&target_ty, field, *span)?;
                }
                // Indexed write: the base must be an array, the index an i32, and
                // the value the element type. Mirrors `ExprKind::Index` reads.
                let typed_index = if let Some(index_expr) = &place.index {
                    // v1: index a NAMED array local directly (`arr[i] = v`).
                    // Writing through a struct field (`s.grid[i] = v`) needs
                    // working struct-in-memory codegen (not yet present), so
                    // reject it cleanly rather than emit wrong stores.
                    if !place.fields.is_empty() {
                        return Err(CompileError::at_code(
                            codes::INVALID_ASSIGN_TARGET,
                            "indexed write through a struct field is not yet supported",
                            *span,
                        ));
                    }
                    let elem_ty = match &target_ty {
                        ResolvedType::Array(elem, _) => (**elem).clone(),
                        other => {
                            return Err(CompileError::at_code(
                                codes::BAD_INDEX,
                                format!("cannot index into {other:?} (only arrays are indexable)"),
                                *span,
                            ))
                        }
                    };
                    let index_t = self.check_expr(index_expr)?;
                    if index_t.ty != ResolvedType::I32 {
                        return Err(CompileError::at_code(
                            codes::BAD_INDEX,
                            format!("array index must be i32, got {:?}", index_t.ty),
                            *span,
                        ));
                    }
                    // From here the value must match the ELEMENT type, not the array.
                    target_ty = elem_ty;
                    Some(index_t)
                } else {
                    None
                };
                let val = self.check_expr(value)?;
                if val.ty != target_ty {
                    return Err(CompileError::at_code(
                        codes::TYPE_MISMATCH,
                        format!("assignment type mismatch: expected {:?}, got {:?}", target_ty, val.ty),
                        *span,
                    ));
                }
                Ok(TypedStmt::Assign { place: place.clone(), index: typed_index, value: val })
            }
            Stmt::Return { value, span } => {
                let val = value.as_ref().map(|v| self.check_expr(v)).transpose()?;
                let ret_ty = val.as_ref().map(|v| v.ty.clone()).unwrap_or(ResolvedType::Void);
                if ret_ty != self.current_return {
                    return Err(CompileError::at_code(
                        codes::TYPE_MISMATCH,
                        format!("return type mismatch: expected {:?}, got {:?}", self.current_return, ret_ty),
                        *span,
                    ));
                }
                Ok(TypedStmt::Return { value: val })
            }
            Stmt::Expr { expr, .. } => {
                let typed = self.check_expr(expr)?;
                Ok(TypedStmt::Expr { expr: typed })
            }
        }
    }

    fn field_type(&self, ty: &ResolvedType, field: &str, span: Span) -> Result<ResolvedType, CompileError> {
        match ty {
            ResolvedType::Struct { fields, .. } => {
                fields.iter()
                    .find(|(name, _)| name == field)
                    .map(|(_, ty)| ty.clone())
                    .ok_or_else(|| CompileError::at_code(codes::BAD_FIELD_ACCESS, format!("no field '{field}' on struct"), span))
            }
            _ => Err(CompileError::at_code(codes::BAD_FIELD_ACCESS, format!("field access on non-struct type {:?}", ty), span)),
        }
    }

    fn check_expr(&mut self, expr: &Expr) -> Result<TypedExpr, CompileError> {
        let span = expr.span;
        match &expr.kind {
            ExprKind::IntLit(n) => Ok(TypedExpr { kind: TypedExprKind::IntLit(*n), ty: ResolvedType::I32, span }),
            ExprKind::FloatLit(n) => Ok(TypedExpr { kind: TypedExprKind::FloatLit(*n), ty: ResolvedType::F64, span }),
            ExprKind::StringLit(s) => Ok(TypedExpr { kind: TypedExprKind::StringLit(s.clone()), ty: ResolvedType::String, span }),
            ExprKind::BoolLit(b) => Ok(TypedExpr { kind: TypedExprKind::BoolLit(*b), ty: ResolvedType::Bool, span }),

            ExprKind::Var(name) => {
                if let Some((ty, _)) = self.lookup_local(name) {
                    Ok(TypedExpr { kind: TypedExprKind::Var(name.clone()), ty: ty.clone(), span })
                } else if let Some(value) = self.consts.get(name) {
                    // A top-level const → inline a clone of its typed value.
                    Ok(value.clone())
                } else if self.functions.contains_key(name) {
                    // Could be a function name
                    Ok(TypedExpr { kind: TypedExprKind::Var(name.clone()), ty: ResolvedType::Void, span })
                } else {
                    Err(CompileError::at_code(codes::UNDEFINED_VARIABLE, format!("undefined variable '{name}'"), span))
                }
            }

            ExprKind::Path(segments) => {
                // Could be an enum variant constructor
                if segments.len() == 2 {
                    if let Some(ResolvedType::Enum { name, variants }) = self.types.get(&segments[0]).cloned() {
                        if let Some((_, shape)) = variants.iter().find(|(vn, _)| *vn == segments[1]) {
                            if matches!(shape, VariantShape::Unit) {
                                return Ok(TypedExpr {
                                    kind: TypedExprKind::Path(segments.clone()),
                                    ty: ResolvedType::Enum { name, variants },
                                    span,
                                });
                            }
                        }
                    }
                }
                Ok(TypedExpr { kind: TypedExprKind::Path(segments.clone()), ty: ResolvedType::Void, span })
            }

            ExprKind::FieldAccess { object, field } => {
                let obj = self.check_expr(object)?;
                let field_ty = self.field_type(&obj.ty, field, span)?;
                let field_index = match &obj.ty {
                    ResolvedType::Struct { fields, .. } => {
                        fields.iter().position(|(n, _)| n == field).unwrap_or(0)
                    }
                    _ => 0,
                };
                Ok(TypedExpr {
                    ty: field_ty,
                    kind: TypedExprKind::FieldAccess { object: Box::new(obj), field: field.clone(), field_index },
                    span,
                })
            }

            ExprKind::Call { func, args } => {
                let checked_args: Result<Vec<_>, _> = args.iter().map(|a| self.check_expr(a)).collect();
                let checked_args = checked_args?;

                // Resolve function name
                let fn_name = match &func.kind {
                    ExprKind::Var(name) => name.clone(),
                    ExprKind::Path(segments) => segments.join("::"),
                    _ => return Err(CompileError::at_code(codes::UNKNOWN_FUNCTION, "cannot call non-function", span)),
                };

                if let Some(sig) = self.functions.get(&fn_name).cloned() {
                    if checked_args.len() != sig.params.len() {
                        return Err(CompileError::at_code(
                            codes::ARITY_MISMATCH,
                            format!("fn '{fn_name}' expects {} args, got {}", sig.params.len(), checked_args.len()),
                            span,
                        ));
                    }
                    let func_typed = self.check_expr(func)?;
                    Ok(TypedExpr {
                        ty: sig.ret.clone(),
                        kind: TypedExprKind::Call { func: Box::new(func_typed), args: checked_args },
                        span,
                    })
                } else if let Some((module, func_name, params, ret)) = resolve_host_fn(&fn_name) {
                    if checked_args.len() != params.len() {
                        return Err(CompileError::at_code(
                            codes::ARITY_MISMATCH,
                            format!("host fn '{fn_name}' expects {} args, got {}", params.len(), checked_args.len()),
                            span,
                        ));
                    }
                    for (i, (arg, expected)) in checked_args.iter().zip(params.iter()).enumerate() {
                        if arg.ty != *expected {
                            return Err(CompileError::at_code(
                                codes::TYPE_MISMATCH,
                                format!("host fn '{fn_name}' arg {i}: expected {expected:?}, got {:?}", arg.ty),
                                span,
                            ));
                        }
                    }
                    Ok(TypedExpr {
                        ty: ret.clone(),
                        kind: TypedExprKind::HostCall { module, func: func_name, args: checked_args, ret_ty: ret },
                        span,
                    })
                } else {
                    // Enum variant constructor call (tuple variant)
                    let func_typed = self.check_expr(func)?;
                    Ok(TypedExpr {
                        ty: ResolvedType::Void,
                        kind: TypedExprKind::Call { func: Box::new(func_typed), args: checked_args },
                        span,
                    })
                }
            }

            ExprKind::MethodCall { object, method, args } => {
                let obj = self.check_expr(object)?;
                let checked_args: Result<Vec<_>, _> = args.iter().map(|a| self.check_expr(a)).collect();
                Ok(TypedExpr {
                    ty: ResolvedType::Void, // host resolves method types
                    kind: TypedExprKind::MethodCall { object: Box::new(obj), method: method.clone(), args: checked_args? },
                    span,
                })
            }

            ExprKind::StructLit { path, fields } => {
                let type_name = path.last().unwrap().clone();
                let struct_ty = self.types.get(&type_name)
                    .ok_or_else(|| CompileError::at_code(codes::UNKNOWN_STRUCT, format!("unknown struct '{type_name}'"), span))?
                    .clone();

                let mut typed_fields = Vec::new();
                for fi in fields {
                    let value = if let Some(v) = &fi.value {
                        self.check_expr(v)?
                    } else {
                        // Shorthand: field name = variable name
                        self.check_expr(&Expr { kind: ExprKind::Var(fi.name.clone()), span: fi.span })?
                    };
                    typed_fields.push((fi.name.clone(), value));
                }

                Ok(TypedExpr {
                    ty: struct_ty,
                    kind: TypedExprKind::StructLit { name: type_name, fields: typed_fields },
                    span,
                })
            }

            ExprKind::TupleLit(exprs) => {
                let typed: Result<Vec<_>, _> = exprs.iter().map(|e| self.check_expr(e)).collect();
                let typed = typed?;
                let tys: Vec<_> = typed.iter().map(|e| e.ty.clone()).collect();
                Ok(TypedExpr {
                    ty: ResolvedType::Tuple(tys),
                    kind: TypedExprKind::TupleLit(typed),
                    span,
                })
            }

            ExprKind::ArrayLit(elems) => {
                if elems.is_empty() {
                    return Err(CompileError::at_code(codes::BAD_INDEX, "empty array literal is unsupported", span));
                }
                let typed: Result<Vec<_>, _> = elems.iter().map(|e| self.check_expr(e)).collect();
                let typed = typed?;
                let elem_ty = typed[0].ty.clone();
                // v1: i32 elements only (colours, coords, tile/lookup tables).
                if elem_ty != ResolvedType::I32 {
                    return Err(CompileError::at_code(
                        codes::BAD_INDEX,
                        format!("arrays support i32 elements for now, got {elem_ty:?}"),
                        span,
                    ));
                }
                if typed.iter().any(|e| e.ty != elem_ty) {
                    return Err(CompileError::at_code(codes::BAD_INDEX, "array elements must all be the same type", span));
                }
                let n = typed.len();
                Ok(TypedExpr {
                    ty: ResolvedType::Array(Box::new(elem_ty), n),
                    kind: TypedExprKind::ArrayLit(typed),
                    span,
                })
            }

            ExprKind::ArrayRepeat { value, count } => {
                let val = self.check_expr(value)?;
                // v1: i32 elements only, mirroring the array-literal restriction.
                if val.ty != ResolvedType::I32 {
                    return Err(CompileError::at_code(
                        codes::BAD_INDEX,
                        format!("arrays support i32 elements for now, got {:?}", val.ty),
                        span,
                    ));
                }
                if *count == 0 {
                    return Err(CompileError::at_code(codes::BAD_INDEX, "empty array (`[v; 0]`) is unsupported", span));
                }
                Ok(TypedExpr {
                    ty: ResolvedType::Array(Box::new(val.ty.clone()), *count),
                    kind: TypedExprKind::ArrayRepeat { value: Box::new(val), count: *count },
                    span,
                })
            }

            ExprKind::Index { base, index } => {
                let base_t = self.check_expr(base)?;
                let index_t = self.check_expr(index)?;
                let elem_ty = match &base_t.ty {
                    ResolvedType::Array(elem, _) => (**elem).clone(),
                    other => {
                        return Err(CompileError::at_code(
                            codes::BAD_INDEX,
                            format!("cannot index into {other:?} (only arrays are indexable)"),
                            span,
                        ))
                    }
                };
                if index_t.ty != ResolvedType::I32 {
                    return Err(CompileError::at_code(
                        codes::BAD_INDEX,
                        format!("array index must be i32, got {:?}", index_t.ty),
                        span,
                    ));
                }
                Ok(TypedExpr {
                    ty: elem_ty,
                    kind: TypedExprKind::Index { base: Box::new(base_t), index: Box::new(index_t) },
                    span,
                })
            }

            ExprKind::BinOp { op, lhs, rhs } => {
                let l = self.check_expr(lhs)?;
                let r = self.check_expr(rhs)?;

                let result_ty = match op {
                    BinOp::Add | BinOp::Sub | BinOp::Mul | BinOp::Div | BinOp::Mod => {
                        if l.ty != r.ty {
                            return Err(CompileError::at_code(
                                codes::TYPE_MISMATCH,
                                format!("binary op type mismatch: {:?} vs {:?}", l.ty, r.ty),
                                span,
                            ));
                        }
                        l.ty.clone()
                    }
                    BinOp::Shl | BinOp::Shr | BinOp::BitAnd | BinOp::BitOr | BinOp::BitXor => {
                        if l.ty != r.ty {
                            return Err(CompileError::at_code(
                                codes::TYPE_MISMATCH,
                                format!("bitwise op type mismatch: {:?} vs {:?}", l.ty, r.ty),
                                span,
                            ));
                        }
                        if l.ty != ResolvedType::I32 && l.ty != ResolvedType::I64 {
                            return Err(CompileError::at_code(
                                codes::TYPE_MISMATCH,
                                format!("bitwise/shift ops require integers, got {:?}", l.ty),
                                span,
                            ));
                        }
                        l.ty.clone()
                    }
                    BinOp::Eq | BinOp::Ne | BinOp::Lt | BinOp::Gt | BinOp::Le | BinOp::Ge => {
                        ResolvedType::Bool
                    }
                    BinOp::And | BinOp::Or => ResolvedType::Bool,
                };

                Ok(TypedExpr {
                    ty: result_ty,
                    kind: TypedExprKind::BinOp { op: *op, lhs: Box::new(l), rhs: Box::new(r) },
                    span,
                })
            }

            ExprKind::UnaryOp { op, operand } => {
                let operand = self.check_expr(operand)?;
                let ty = match op {
                    UnaryOp::Neg => operand.ty.clone(),
                    UnaryOp::Not => ResolvedType::Bool,
                };
                Ok(TypedExpr {
                    ty,
                    kind: TypedExprKind::UnaryOp { op: *op, operand: Box::new(operand) },
                    span,
                })
            }

            ExprKind::Cast { expr, ty } => {
                let inner = self.check_expr(expr)?;
                let target = self.resolve_ty(ty)?;
                let numeric = |t: &ResolvedType| {
                    matches!(
                        t,
                        ResolvedType::I32 | ResolvedType::I64 | ResolvedType::F32 | ResolvedType::F64
                    )
                };
                if !numeric(&inner.ty) || !numeric(&target) {
                    return Err(CompileError::at_code(
                        codes::BAD_CAST,
                        format!("`as` converts between numbers, not {:?} -> {:?}", inner.ty, target),
                        span,
                    ));
                }
                Ok(TypedExpr {
                    ty: target,
                    kind: TypedExprKind::Cast { expr: Box::new(inner) },
                    span,
                })
            }

            ExprKind::If { cond, then_block, else_block } => {
                let cond = self.check_expr(cond)?;
                let then_typed = self.check_block(then_block)?;
                let else_typed = match else_block {
                    Some(ElseBranch::Block(b)) => Some(TypedElse::Block(self.check_block(b)?)),
                    Some(ElseBranch::If(e)) => Some(TypedElse::If(Box::new(self.check_expr(e)?))),
                    None => None,
                };
                // An `if` WITHOUT an `else` is a statement, never a value (Rust
                // semantics): the missing branch can't produce a value, so the
                // whole `if` is `Void`. A non-void tail in the `then` block would
                // therefore be dropped on the floor on the false path — codegen
                // would have to emit an `(if (result T))` frame with no else,
                // which is stack-imbalanced (invalid) wasm. Reject it here so the
                // emitter always stays on the void path (BLOCK_VOID).
                let ty = if else_typed.is_some() {
                    then_typed.ty.clone()
                } else {
                    if then_typed.ty != ResolvedType::Void && then_typed.ty != ResolvedType::Never {
                        return Err(CompileError::at_code(
                            codes::TYPE_MISMATCH,
                            format!(
                                "`if` without `else` evaluates to {:?}, but an else-less `if` is a statement (add an `else` branch to use its value)",
                                then_typed.ty
                            ),
                            span,
                        ));
                    }
                    ResolvedType::Void
                };
                Ok(TypedExpr {
                    ty,
                    kind: TypedExprKind::If { cond: Box::new(cond), then_block: then_typed, else_block: else_typed },
                    span,
                })
            }

            ExprKind::Match { scrutinee, arms } => {
                let scrutinee = self.check_expr(scrutinee)?;
                let mut typed_arms = Vec::new();
                let mut result_ty = ResolvedType::Void;

                for (i, arm) in arms.iter().enumerate() {
                    // An irrefutable arm (`_` or a bare binding) matches
                    // everything, so any arm after it is dead — and codegen lowers
                    // a non-last match to a chain of `if/else` frames that assumes
                    // the irrefutable arm is the terminal `else`. A non-last
                    // wildcard/binding would emit a stack-imbalanced (invalid)
                    // module, so reject it here (the arm must be moved last).
                    if is_irrefutable_pattern(&arm.pattern) && i != arms.len() - 1 {
                        return Err(CompileError::at_code(
                            codes::TYPE_MISMATCH,
                            "a `_`/binding match arm matches everything; move it last (arms after it are unreachable)",
                            arm.span,
                        ));
                    }

                    self.push_scope();
                    self.bind_pattern(&arm.pattern, &scrutinee.ty)?;
                    let body = self.check_expr(&arm.body)?;
                    self.pop_scope();

                    if i == 0 {
                        result_ty = body.ty.clone();
                    }

                    typed_arms.push(TypedMatchArm { pattern: arm.pattern.clone(), body });
                }

                Ok(TypedExpr {
                    ty: result_ty.clone(),
                    kind: TypedExprKind::Match { scrutinee: Box::new(scrutinee), arms: typed_arms, result_ty },
                    span,
                })
            }

            ExprKind::While { cond, body } => {
                let cond = self.check_expr(cond)?;
                let body = self.check_block(body)?;
                Ok(TypedExpr {
                    ty: ResolvedType::Void,
                    kind: TypedExprKind::While { cond: Box::new(cond), body },
                    span,
                })
            }

            ExprKind::Loop { body } => {
                let body = self.check_block(body)?;
                Ok(TypedExpr {
                    ty: ResolvedType::Void,
                    kind: TypedExprKind::Loop { body },
                    span,
                })
            }

            ExprKind::Break { value } => {
                let val = value.as_ref().map(|v| self.check_expr(v)).transpose()?;
                Ok(TypedExpr {
                    ty: ResolvedType::Never,
                    kind: TypedExprKind::Break { value: val.map(Box::new) },
                    span,
                })
            }

            ExprKind::Continue => {
                Ok(TypedExpr { kind: TypedExprKind::Continue, ty: ResolvedType::Never, span })
            }

            ExprKind::Block(block) => {
                let typed = self.check_block(block)?;
                let ty = typed.ty.clone();
                Ok(TypedExpr { kind: TypedExprKind::Block(typed), ty, span })
            }
        }
    }

    fn bind_pattern(&mut self, pattern: &Pattern, scrutinee_ty: &ResolvedType) -> Result<(), CompileError> {
        match &pattern.kind {
            PatternKind::Wildcard => Ok(()),
            PatternKind::Literal(_) => Ok(()),
            PatternKind::IntRange { .. } => Ok(()),
            PatternKind::Binding(name) => {
                self.define_local(name, scrutinee_ty.clone(), false);
                Ok(())
            }
            PatternKind::Path(_) => Ok(()),
            PatternKind::TupleVariant { path, fields } => {
                if let ResolvedType::Enum { variants, .. } = scrutinee_ty {
                    let variant_name = path.last().unwrap();
                    if let Some((_, VariantShape::Tuple(tys))) = variants.iter().find(|(n, _)| n == variant_name) {
                        for (pat, ty) in fields.iter().zip(tys.iter()) {
                            self.bind_pattern(pat, ty)?;
                        }
                    }
                }
                Ok(())
            }
            PatternKind::StructVariant { path, fields } => {
                if let ResolvedType::Enum { variants, .. } = scrutinee_ty {
                    let variant_name = path.last().unwrap();
                    if let Some((_, VariantShape::Struct(field_tys))) = variants.iter().find(|(n, _)| n == variant_name) {
                        for fp in fields {
                            if let Some((_, ty)) = field_tys.iter().find(|(n, _)| n == &fp.name) {
                                if let Some(inner_pat) = &fp.pattern {
                                    self.bind_pattern(inner_pat, ty)?;
                                } else {
                                    self.define_local(&fp.name, ty.clone(), false);
                                }
                            }
                        }
                    }
                }
                Ok(())
            }
        }
    }
}

/// Whether a match pattern matches EVERY value (so any later arm is dead code).
/// Mirrors codegen's `is_wildcard`: a bare `_` or a binding (`x =>`) catches all;
/// literals, ranges, and variant patterns are refutable. Codegen lowers the
/// terminal catch-all to a plain `else`, so a non-last irrefutable arm is
/// rejected in the typechecker (it would otherwise emit invalid wasm).
fn is_irrefutable_pattern(pattern: &Pattern) -> bool {
    matches!(pattern.kind, PatternKind::Wildcard | PatternKind::Binding(_))
}

/// Resolve a `module::func` path against the host-function table.
///
/// `fn_name` is the call path joined with `::` (e.g. `display::clear`
/// or `host::display::clear`); a leading `host::` is stripped. Returns
/// `(module, func, param_types, return_type)` for known host builtins.
///
/// These are the **Orbclient-style** drawing primitives a cartridge
/// uses to draw onto the host-owned framebuffer (see `src/app/display.rs`
/// for the matching imports). Colours are `0xRRGGBB` (opaque).
fn resolve_host_fn(fn_name: &str) -> Option<(String, String, Vec<ResolvedType>, ResolvedType)> {
    use ResolvedType::*;
    let stripped = fn_name.strip_prefix("host::").unwrap_or(fn_name);
    // Every host builtin lives in the `display` module today. Accept the
    // module-elided spellings — `state_get`, `host::state_get`, or (after
    // `use host::display;`) a bare `state_get` — by defaulting the module
    // to `display`. Without this, `host::state_get` resolved to nothing,
    // fell through to the enum-variant branch, and got typed `Void` — the
    // "declared I32, got Void" bug that blocked all stateful cartridges.
    // NB: `use ResolvedType::*` above shadows the std `String` type with
    // the `String` variant, so don't annotate this `let` with `String`.
    let key = if stripped.contains("::") {
        stripped.to_string()
    } else {
        format!("display::{stripped}")
    };
    let (params, ret): (Vec<ResolvedType>, ResolvedType) = match key.as_str() {
        "display::clear" => (vec![I32], Void),
        "display::set_pixel" => (vec![I32, I32, I32], Void),
        "display::fill_rect" => (vec![I32, I32, I32, I32, I32], Void),
        "display::draw_char" => (vec![I32, I32, I32, I32, I32], Void),
        "display::draw_number" => (vec![I32, I32, I32, I32, I32], Void),
        // --- software 3D (FB#12b): framebuffer primitives, integer ABI, same
        // pixel/viewport model as the other display fns (no WebGL/iframe).
        // `draw_line(x0,y0,x1,y1,rgb)`; `fill_triangle(x0,y0,x1,y1,x2,y2,rgb)`
        // flat-fill (painter's order — depth/overlap is the cartridge's job; a
        // per-pixel z-buffered fill needs a packed ABI to fit the host closure
        // arity limit, so it's deferred to v2). See `web/cartridge-worker.js`
        // host_display + `src/raster.rs`.
        "display::draw_line" => (vec![I32, I32, I32, I32, I32], Void),
        "display::fill_triangle" => (vec![I32, I32, I32, I32, I32, I32, I32], Void),
        "display::present" => (vec![], Void),
        "display::width" => (vec![], I32),
        "display::height" => (vec![], I32),
        "display::pointer_x" => (vec![], I32),
        "display::pointer_y" => (vec![], I32),
        "display::pointer_down" => (vec![], I32),
        "display::state_get" => (vec![I32], I32),
        "display::state_set" => (vec![I32, I32], Void),
        // --- networking (host_net): WebSocket-backed multiplayer/sync I/O.
        // Strings (URL, message bodies) use the same length-prefixed memory
        // layout as the loader's `read_string`: 4 bytes LE length, then UTF-8
        // payload, at the given cartridge-memory pointer.
        //
        // `open(url_ptr) -> handle`   open a WebSocket to the url at `url_ptr`;
        //                             returns a handle >= 0, or -1 on error.
        // `send(handle, ptr) -> ok`   send the length-prefixed message at `ptr`;
        //                             returns 1 if queued, 0 if not (closed/bad).
        // `poll(handle, out_ptr, max) -> len`  copy the next inbound message
        //                             (length-prefixed) into memory at `out_ptr`,
        //                             writing at most `max` payload bytes; returns
        //                             the payload byte length, 0 if the inbox is
        //                             empty, or -1 on a bad handle.
        // `status(handle) -> i32`     0 connecting, 1 open, 2 closing, 3 closed,
        //                             -1 bad handle.
        // `close(handle)`             close the socket and drop its inbox.
        "net::open" => (vec![I32], I32),
        "net::send" => (vec![I32, I32], I32),
        "net::poll" => (vec![I32, I32, I32], I32),
        "net::status" => (vec![I32], I32),
        "net::close" => (vec![I32], Void),
        // --- audio (host_audio): Web Audio (AudioContext) playback. Integer
        // ABI, fire-and-forget like host_net. `wave`: 0 sine, 1 square,
        // 2 sawtooth, 3 triangle. A handle >= 0 names a voice for `stop`;
        // `stop(-1)` stops every voice. `set_volume(pct)` sets master gain
        // (0..=100). Audio is silent until the first user gesture (the browser
        // AudioContext rule) — a cartridge only runs after the user opens it,
        // so the first `tone` resumes the context. See `src/app/display.rs`
        // `mod audio` for the host implementation.
        //
        // `tone(freq_hz, dur_ms, wave) -> handle`        play a tone now.
        // `tone_at(freq_hz, dur_ms, wave, delay_ms) -> handle`  schedule a
        //                             tone `delay_ms` in the future (sequencing
        //                             a bar of notes from one frame).
        // `noise(dur_ms) -> handle`   white-noise burst (hats/explosions).
        // `stop(handle)`              stop one voice; `stop(-1)` stops all.
        // `set_volume(pct)`           master gain 0..=100 (clamped).
        "audio::tone" => (vec![I32, I32, I32], I32),
        "audio::tone_at" => (vec![I32, I32, I32, I32], I32),
        "audio::noise" => (vec![I32], I32),
        "audio::stop" => (vec![I32], Void),
        "audio::set_volume" => (vec![I32], Void),
        // --- agent (host_agent): the cartridge<->platform bridge (feedback
        // #66/#103). Lets a published cartridge reach the platform it runs
        // inside, within a deliberately narrow + safe v1 surface.
        //
        // `notify(title, body) -> i32`  show a LOCAL system notification to
        //   the CURRENT viewer (never other users — that P2P-subscriber push
        //   is the named follow-up). Strings are length-prefixed pointers
        //   (same ABI as host_net). Permission-GATED (never prompts; silently
        //   dropped if the viewer hasn't already allowed notifications) and
        //   RATE-LIMITED (~1 / 3s). Returns 1 if posted, 0 if dropped/limited.
        //   Use on a user gesture (a button press) — e.g. "Ready Up!".
        // `viewer_is_owner() -> i32`    1 if THIS device controls (owns) the
        //   subdomain the cartridge is published under, else 0 — gate
        //   host-only controls (the "host triggers" in a Ready-Up app).
        // `viewer_has_identity() -> i32` 1 if the viewer has a local wallet.
        "agent::notify" => (vec![String, String], I32),
        "agent::viewer_is_owner" => (vec![], I32),
        "agent::viewer_has_identity" => (vec![], I32),
        // --- subscriber feed (the "Ready Up" loop, feedback #103). The feed
        // is THIS cartridge's own subdomain (SubscribeFacet on-chain). Writes
        // are fire-and-forget (sponsored tx on the main thread); reads are
        // load-time context refreshed after a subscribe/unsubscribe.
        //
        // `subscribe() -> i32`    subscribe the viewer to this feed; 1 if the
        //   request went out, 0 if the viewer has no identity yet.
        // `unsubscribe() -> i32`  leave the feed.
        // `is_subscribed() -> i32` 1 if the viewer is on the feed (cached).
        // `subscriber_count() -> i32`  members on the feed (cached) — the
        //   "member count" for the UI.
        // `broadcast(title, body) -> i32`  THE READY UP: push a notification
        //   to EVERY subscriber's device (via the proxy). NOT owner-gated —
        //   anyone with an identity can fire it; rate-limited per feed. 1 if
        //   the request went out, 0 if no identity.
        // `broadcast_compose(title, default_body) -> i32`  `broadcast`, but
        //   the host first opens a text input over the canvas (prefilled with
        //   `default_body`) so the presser can type a CUSTOM message before it
        //   goes out — a cartridge is pixels-only and can't summon a mobile
        //   keyboard itself. [send] broadcasts the typed body under `title`;
        //   [cancel] sends nothing. 1 if the composer opened, 0 if no identity.
        // `request_identity() -> i32`  ensure the viewer has a wallet (creates
        //   a local one if missing — the "this app needs an identity" path);
        //   returns 1 if they now have one. Gate subscribe/broadcast on this
        //   for a sybil-resistant app.
        "agent::subscribe" => (vec![], I32),
        "agent::unsubscribe" => (vec![], I32),
        "agent::is_subscribed" => (vec![], I32),
        "agent::subscriber_count" => (vec![], I32),
        "agent::broadcast" => (vec![String, String], I32),
        "agent::broadcast_compose" => (vec![String, String], I32),
        "agent::request_identity" => (vec![], I32),
        // --- compose (host_compose): cartridge-in-cartridge composition. A
        // PARENT cartridge mounts another subdomain's published `app.wasm` as a
        // CHILD bound to a sub-rectangle of the parent's framebuffer, with the
        // child running in its OWN buffer (its native dims) and the host
        // blitting it into the rect (nearest-neighbour scale — see
        // `crate::compose::blit_child`). Pointer input routes into the focused
        // child via `crate::compose::map_pointer_into_child`. NO iframes — pure
        // pixel composition. Integer-only, poll-model, the SAME conventions as
        // host_net/host_agent: the child name is a length-prefixed string
        // pointer into the parent's linear memory (4-byte LE len + UTF-8). See
        // `web/cartridge-worker.js` host_compose + `src/app/display.rs`
        // (compose_spawn round-trip) + `design/host-compose.md` for the model.
        //
        // `spawn_module(name, x, y, w, h) -> handle`  fetch <name>'s on-chain
        //   app.wasm, instantiate a child bound to rect (x,y,w,h) in the CALLER's
        //   framebuffer coords. `name` is a string literal (a length-prefixed
        //   pointer into the caller's memory, same ABI as agent::notify).
        //   Returns a handle >= 0 (LOADING; it ticks once the bytes arrive) or a
        //   negative reject. RECURSIVE: the child gets its OWN compose table and
        //   may spawn grandchildren — handles are per-node (a child's handle 0 is
        //   distinct from the parent's). The fractal terminates at the depth cap
        //   (a node there returns -1); also bounded by per-node child / total-node
        //   / total-byte caps (ComposeBudget). A self-spawning cartridge nests
        //   into a Droste image.
        // `status(handle) -> i32`  -1 bad handle, 0 LOADING, 1 READY (ticking),
        //   2 FAILED (no app.wasm, bad bytes, trap, or budget-refused).
        // `move_module(handle, x, y, w, h) -> i32`  re-bind the child's rect
        //   (the window manager moves/resizes a panel). 1 ok, 0 bad handle.
        // `focus_module(handle) -> i32`  make `handle` the focused child — the
        //   only one fed pointer input. Pass -1 to focus the parent itself.
        //   1 ok, 0 bad handle.
        // `focused() -> i32`  the currently focused handle, or -1 for the parent.
        // `close_module(handle) -> i32`  tear the child down (drop its instance
        //   + buffer + rect; free the slot, never aliased). 1 ok, 0 bad handle.
        // `module_count() -> i32`  number of live (non-closed) children.
        "compose::spawn_module" => (vec![String, I32, I32, I32, I32], I32),
        "compose::status" => (vec![I32], I32),
        "compose::move_module" => (vec![I32, I32, I32, I32, I32], I32),
        "compose::focus_module" => (vec![I32], I32),
        "compose::focused" => (vec![], I32),
        "compose::close_module" => (vec![I32], I32),
        "compose::module_count" => (vec![], I32),
        _ => return None,
    };
    let (module, func) = key.split_once("::")?;
    Some((module.to_string(), func.to_string(), params, ret))
}

#[cfg(test)]
mod host_fn_tests {
    use super::*;

    #[test]
    fn host_agent_signatures_resolve() {
        use ResolvedType::{String as Str, I32};
        let (m, f, p, r) = resolve_host_fn("host::agent::notify").expect("notify resolves");
        assert_eq!((m.as_str(), f.as_str()), ("agent", "notify"));
        assert_eq!(p, vec![Str, Str]);
        assert_eq!(r, I32, "notify returns posted/dropped flag");

        for name in ["host::agent::viewer_is_owner", "host::agent::viewer_has_identity"] {
            let (m, _f, p, r) = resolve_host_fn(name).unwrap_or_else(|| panic!("{name}"));
            assert_eq!(m, "agent");
            assert!(p.is_empty(), "{name} takes no args");
            assert_eq!(r, I32);
        }
        // The module-elision default (display) must NOT swallow agent fns.
        assert!(resolve_host_fn("agent::notify").is_some());
    }

    #[test]
    fn host_compose_signatures_resolve() {
        use ResolvedType::{String as Str, I32};
        // spawn_module(name, x, y, w, h) -> handle (name is a string literal).
        let (m, f, p, r) = resolve_host_fn("host::compose::spawn_module").expect("spawn resolves");
        assert_eq!((m.as_str(), f.as_str()), ("compose", "spawn_module"));
        assert_eq!(p, vec![Str, I32, I32, I32, I32], "name string + rect");
        assert_eq!(r, I32, "spawn returns a child handle");

        // Single-handle ops: status / focus / close return i32.
        for name in [
            "host::compose::status",
            "host::compose::focus_module",
            "host::compose::close_module",
        ] {
            let (m, _f, p, r) = resolve_host_fn(name).unwrap_or_else(|| panic!("{name}"));
            assert_eq!(m, "compose");
            assert_eq!(p, vec![I32], "{name} takes one handle");
            assert_eq!(r, I32);
        }

        // move_module(handle, x, y, w, h) -> i32 (all integer).
        let (_m, _f, p, r) = resolve_host_fn("host::compose::move_module").unwrap();
        assert_eq!(p, vec![I32, I32, I32, I32, I32]);
        assert_eq!(r, I32);

        // No-arg readers: focused() / module_count() -> i32.
        for name in ["host::compose::focused", "host::compose::module_count"] {
            let (m, _f, p, r) = resolve_host_fn(name).unwrap_or_else(|| panic!("{name}"));
            assert_eq!(m, "compose");
            assert!(p.is_empty(), "{name} takes no args");
            assert_eq!(r, I32);
        }
        // The module-elision default (display) must NOT swallow compose fns.
        assert!(resolve_host_fn("compose::spawn_module").is_some());
    }

    #[test]
    fn state_get_resolves_to_i32_in_every_spelling() {
        for name in [
            "state_get",
            "host::state_get",
            "display::state_get",
            "host::display::state_get",
        ] {
            let (module, func, params, ret) =
                resolve_host_fn(name).unwrap_or_else(|| panic!("{name} did not resolve"));
            assert_eq!(module, "display");
            assert_eq!(func, "state_get");
            assert_eq!(params, vec![ResolvedType::I32]);
            assert_eq!(ret, ResolvedType::I32, "{name} must return i32, not Void");
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::rustlite::{lexer, parser};

    fn check_str(s: &str) -> TypedModule {
        let tokens = lexer::lex(s).unwrap();
        let module = parser::parse(&tokens).unwrap();
        check(&module).unwrap()
    }

    #[test]
    fn check_simple_fn() {
        let m = check_str("fn add(a: i32, b: i32) -> i32 { a + b }");
        assert_eq!(m.functions.len(), 1);
        assert_eq!(m.functions[0].ret_type, ResolvedType::I32);
    }

    #[test]
    fn check_struct_and_field_access() {
        let m = check_str(r#"
            struct Point { x: i32, y: i32 }
            fn get_x(p: Point) -> i32 { p.x }
        "#);
        assert_eq!(m.structs.len(), 1);
        assert_eq!(m.functions[0].ret_type, ResolvedType::I32);
    }

    #[test]
    fn check_let_and_assign() {
        let m = check_str("fn f() { let mut x: i32 = 0; x = 42; }");
        assert_eq!(m.functions.len(), 1);
    }

    #[test]
    fn check_type_mismatch() {
        let tokens = lexer::lex("fn f() -> i32 { true }").unwrap();
        let module = parser::parse(&tokens).unwrap();
        assert!(check(&module).is_err());
    }

    #[test]
    fn check_immutable_assign() {
        let tokens = lexer::lex("fn f() { let x: i32 = 0; x = 1; }").unwrap();
        let module = parser::parse(&tokens).unwrap();
        assert!(check(&module).is_err());
    }
}