algocline-core 0.40.0

algocline domain model and metrics — pure execution state machine
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
//! Canonical projection of a Lua package's `M.meta` block.
//!
//! `PkgEntity` captures the identity portion of an algocline package: the
//! fields users rely on to discover, categorize, and version-track a package.
//! It is the single source of truth for "what is this package?" and is
//! flattened into higher-level records (`IndexEntry`, `SearchResult`,
//! `hub_info` responses) so the JSON wire shape stays consistent across the
//! Hub, the manifest, and project lockfiles.
//!
//! ## Parsing contract
//!
//! [`PkgEntity::parse_from_init_lua`] is a non-Lua-VM best-effort parser over
//! the `M.meta = { ... }` block of an `init.lua`. It deliberately only
//! supports flat key–value pairs with (possibly concatenated) string
//! literals; nested tables (e.g. `tags = { ... }`) are skipped via
//! brace-depth tracking. When `M.meta.name` is absent or empty the parser
//! returns `None` — this is the **inclusion gate** for hub indexing. The
//! caller (`build_index` in `algocline-app::service::hub`) is expected to
//! drop `None` directories silently so "draft" directories like
//! `alc_shapes/` (a type DSL library, not an algocline package) do not
//! pollute the hub index.
//!
//! ## Wire format
//!
//! `Option` fields use `#[serde(default)]` but deliberately do **not** use
//! `skip_serializing_if`. A missing field deserializes as `None` and
//! serializes back as `null`. This preserves the key-presence guarantee of
//! the current `hub_index.json` consumers (Bundled-side doc generation,
//! `README.md` package-count scripts) so they do not break on field
//! absence.

use std::path::Path;
use std::str::FromStr;

use serde::{Deserialize, Serialize};

/// Package type discriminator: runnable (has `M.run`) or library (API surface only).
///
/// Used by the adviser and eval entry points to route packages correctly:
/// - `Runnable` packages are executed via `M.run(ctx)`.
/// - `Library` packages expose an API surface and must not be executed via `M.run`.
///
/// Wire format: `"runnable"` / `"library"` (lowercase via `#[serde(rename_all = "lowercase")]`).
///
/// Auto-detection: when `M.meta.type` is absent, the Lua VM path uses
/// `type(pkg.run) == "function"` at runtime (canonical), while the offline
/// `parse_from_init_lua` path uses `detect_has_run` (text-scan mirror).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum PkgType {
    /// Package has a `M.run(ctx)` entry point — can be executed via `alc_advice`.
    Runnable,
    /// Package exposes an API surface only — must not be invoked via `M.run`.
    Library,
}

impl std::fmt::Display for PkgType {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Runnable => write!(f, "runnable"),
            Self::Library => write!(f, "library"),
        }
    }
}

impl FromStr for PkgType {
    type Err = String;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s {
            "runnable" => Ok(Self::Runnable),
            "library" => Ok(Self::Library),
            other => Err(format!("unknown package type: {other:?}")),
        }
    }
}

/// Records how `pkg_type` was determined for a package.
///
/// This is stored alongside `PkgType` in `PkgEntity.type_source` so
/// downstream consumers (`alc_pkg_doctor`, `alc_pkg_list`) can distinguish
/// packages that were auto-detected as libraries from packages that
/// explicitly declared their type — and emit a targeted suggestion in the
/// former case.
///
/// Wire format: `"explicit"` / `"auto_detected_runnable"` /
/// `"auto_detected_library"` (snake_case via `#[serde(rename_all = "snake_case")]`).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum TypeSource {
    /// `M.meta.type` was present in the package source — type is authoritative.
    Explicit,
    /// `M.meta.type` was absent; the package was classified as `runnable`
    /// because `M.run` is defined (offline Rust path or Lua VM detection).
    AutoDetectedRunnable,
    /// `M.meta.type` was absent; the package was classified as `library`
    /// because no `M.run` was found. Consider adding `M.meta.type = "library"`
    /// to make this explicit.
    AutoDetectedLibrary,
}

impl FromStr for TypeSource {
    type Err = String;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s {
            "explicit" => Ok(Self::Explicit),
            "auto_detected_runnable" => Ok(Self::AutoDetectedRunnable),
            "auto_detected_library" => Ok(Self::AutoDetectedLibrary),
            other => Err(format!("unknown type_source: {other:?}")),
        }
    }
}

/// Canonical projection of a Lua package's `M.meta` block.
///
/// `name` is required (= hub-index inclusion gate). Other fields are
/// optional and degrade UI / discoverability when absent, following the
/// BP convention of Cargo / JSR / npm.
#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq, Eq)]
pub struct PkgEntity {
    pub name: String,
    #[serde(default)]
    pub version: Option<String>,
    #[serde(default)]
    pub description: Option<String>,
    #[serde(default)]
    pub category: Option<String>,
    #[serde(default)]
    pub docstring: Option<String>,
    #[serde(default)]
    pub tags: Option<Vec<String>>,
    /// Package type (`"runnable"` or `"library"`). Serialized as `"type"` on
    /// the wire. `None` means the type was not determined (legacy entries).
    #[serde(default, rename = "type")]
    pub pkg_type: Option<PkgType>,
    /// Records how `pkg_type` was determined. `None` for legacy entries that
    /// pre-date provenance tracking (`#[serde(default)]` ensures backward
    /// compatibility with existing `hub_index.json` consumers).
    #[serde(default)]
    pub type_source: Option<TypeSource>,
}

impl PkgEntity {
    /// Parse `M.meta` + leading `---` docstring from an `init.lua`.
    ///
    /// Returns `None` when the file cannot be read, `M.meta` is absent, or
    /// `M.meta.name` is empty. Callers treat `None` as "not a package" and
    /// drop the directory silently from the hub index.
    ///
    /// The parser is **not** a full Lua evaluator:
    ///
    /// - Only flat key–value pairs inside `M.meta` are extracted.
    /// - Nested tables (e.g. `tags = { ... }`) are skipped via brace-depth
    ///   tracking; their keys are not reachable from here.
    /// - Values must be string literals (`"..."`), optionally joined by `..`
    ///   concatenation with whitespace between operators.
    /// - Occurrences of `M.meta` inside single-line comments (`-- ...`)
    ///   are ignored, so docstrings mentioning the key do not hijack the
    ///   search.
    pub fn parse_from_init_lua(path: &Path) -> Option<Self> {
        let content = std::fs::read_to_string(path).ok()?;
        let parsed = parse_meta(&content)?;
        let docstring = extract_docstring_from(&content);
        // Resolve type and provenance: explicit M.meta.type wins; otherwise
        // fall back to Rust text-scan (build_index / offline batch path) and
        // record that the type was auto-detected.
        let (pkg_type, type_source) = match parsed.pkg_type {
            Some(t) => (Some(t), Some(TypeSource::Explicit)),
            None => {
                if detect_has_run(&content) {
                    (
                        Some(PkgType::Runnable),
                        Some(TypeSource::AutoDetectedRunnable),
                    )
                } else {
                    (
                        Some(PkgType::Library),
                        Some(TypeSource::AutoDetectedLibrary),
                    )
                }
            }
        };
        Some(PkgEntity {
            name: parsed.name,
            version: option_from_str(parsed.version),
            description: option_from_str(parsed.description),
            category: option_from_str(parsed.category),
            docstring: option_from_str(docstring),
            tags: if parsed.tags.is_empty() {
                None
            } else {
                Some(parsed.tags)
            },
            pkg_type,
            type_source,
        })
    }
}

/// Return `None` for empty strings, `Some(s)` otherwise. Kept inline with
/// `parse_from_init_lua` so the "empty field = absent" projection rule is
/// applied uniformly to every optional column.
fn option_from_str(s: String) -> Option<String> {
    if s.is_empty() {
        None
    } else {
        Some(s)
    }
}

/// Extract leading `---` doc-comment lines from an init.lua source. Blank
/// lines within the block are tolerated; the first non-doc content line
/// terminates the block.
fn extract_docstring_from(content: &str) -> String {
    let mut lines = Vec::new();
    for line in content.lines() {
        let trimmed = line.trim_start();
        if let Some(rest) = trimmed.strip_prefix("---") {
            lines.push(rest.trim().to_string());
        } else if trimmed.is_empty() {
            continue;
        } else {
            break;
        }
    }
    lines.join("\n")
}

/// Intermediate result of parsing `M.meta = { ... }` from an `init.lua`.
/// All fields are owned. `pkg_type` is `None` when the `type` key is absent
/// or has an unrecognized value (caller applies auto-detect fallback).
struct ParsedMeta {
    name: String,
    version: String,
    description: String,
    category: String,
    tags: Vec<String>,
    pkg_type: Option<PkgType>,
}

/// Detect whether the init.lua source declares `M.run`.
///
/// Used by `parse_from_init_lua` as the offline (non-VM) fallback for
/// `build_index` when `M.meta.type` is absent. The Lua VM path (`pkg_list`,
/// `resolve_pkg_type_lua`) uses `type(pkg.run) == "function"` at runtime and
/// is the canonical source; this function is the Rust mirror for offline batch.
///
/// Comment exclusion: for each line, only the portion before the first `--`
/// is considered, so `-- M.run = ...` or `-- function M.run(ctx)` do not
/// trigger a match.
fn detect_has_run(content: &str) -> bool {
    for line in content.lines() {
        // Strip inline comment suffix.
        let effective = line.split("--").next().unwrap_or(line);
        if effective.contains("M.run") {
            return true;
        }
    }
    false
}

/// Parse `M.meta = { ... }` out of `content`. Returns `None` if the block is
/// missing, unparseable, or `name` is empty.
fn parse_meta(content: &str) -> Option<ParsedMeta> {
    let head = content;

    // Find M.meta = { ... } block (with brace-depth tracking).
    // Skip occurrences inside Lua line comments (`-- ...`) so that
    // docstrings mentioning "M.meta" do not hijack the search.
    let mut search_from = 0;
    let meta_start = loop {
        let rel = head[search_from..].find("M.meta")?;
        let pos = search_from + rel;
        let line_start = head[..pos].rfind('\n').map(|i| i + 1).unwrap_or(0);
        if !head[line_start..pos].contains("--") {
            break pos;
        }
        search_from = pos + "M.meta".len();
    };
    let brace_start = head[meta_start..].find('{')? + meta_start;

    // Track brace depth so nested tables do not terminate the block.
    let mut depth = 0;
    let mut brace_end = None;
    for (i, ch) in head[brace_start..].char_indices() {
        match ch {
            '{' => depth += 1,
            '}' => {
                depth -= 1;
                if depth == 0 {
                    brace_end = Some(brace_start + i);
                    break;
                }
            }
            _ => {}
        }
    }
    let brace_end = brace_end?;
    let block = &head[brace_start + 1..brace_end];

    let extract = |field: &str| -> String {
        // Match: field = "value" [.. "value" ...] with word-boundary check.
        // Walk through all occurrences of `field`, skipping matches inside
        // longer identifiers (e.g. "short_description"). On the first valid
        // occurrence, collect one or more `"..."` string literals joined by
        // `..` concatenation operators.
        let mut search_from = 0;
        while let Some(rel) = block[search_from..].find(field) {
            let pos = search_from + rel;
            let word_boundary = pos == 0 || {
                let prev = block.as_bytes()[pos - 1];
                !(prev.is_ascii_alphanumeric() || prev == b'_')
            };
            if word_boundary {
                let after = &block[pos + field.len()..];
                let mut collected = String::new();
                let mut cursor = 0usize;
                let mut found_any = false;
                loop {
                    let rest = &after[cursor..];
                    let Some(q_start_rel) = rest.find('"') else {
                        break;
                    };
                    if found_any {
                        // Between the prior closing quote and this opening
                        // quote, only whitespace and a single `..` operator
                        // are allowed. Anything else (comma, another field,
                        // etc.) ends the value.
                        let between = &rest[..q_start_rel];
                        if between.trim() != ".." {
                            break;
                        }
                    }
                    let lit_start = cursor + q_start_rel + 1;
                    let Some(q_end_rel) = after[lit_start..].find('"') else {
                        break;
                    };
                    collected.push_str(&after[lit_start..lit_start + q_end_rel]);
                    cursor = lit_start + q_end_rel + 1;
                    found_any = true;
                }
                if found_any {
                    return collected;
                }
            }
            search_from = pos + field.len();
        }
        String::new()
    };

    let name = extract("name");
    if name.is_empty() {
        return None;
    }
    let tags = extract_string_array(block, "tags");
    // Parse `type` field; unrecognized values fall back to None (auto-detect).
    let pkg_type = {
        let raw = extract("type");
        if raw.is_empty() {
            None
        } else {
            raw.parse::<PkgType>().ok()
        }
    };
    Some(ParsedMeta {
        name,
        version: extract("version"),
        description: extract("description"),
        category: extract("category"),
        tags,
        pkg_type,
    })
}

/// Extract a string array from a nested table like `tags = { "a", "b" }`.
/// Returns an empty Vec if the field is absent or has no string elements.
fn extract_string_array(block: &str, field: &str) -> Vec<String> {
    let mut result = Vec::new();
    let mut search_from = 0;
    while let Some(rel) = block[search_from..].find(field) {
        let pos = search_from + rel;
        let word_boundary = pos == 0 || {
            let prev = block.as_bytes()[pos - 1];
            !(prev.is_ascii_alphanumeric() || prev == b'_')
        };
        if word_boundary {
            let after = &block[pos + field.len()..];
            if let Some(brace_start) = after.find('{') {
                let inner_start = brace_start + 1;
                let mut depth = 1;
                let mut brace_end = None;
                for (i, ch) in after[inner_start..].char_indices() {
                    match ch {
                        '{' => depth += 1,
                        '}' => {
                            depth -= 1;
                            if depth == 0 {
                                brace_end = Some(inner_start + i);
                                break;
                            }
                        }
                        _ => {}
                    }
                }
                if let Some(end) = brace_end {
                    let inner = &after[inner_start..end];
                    let mut cursor = 0;
                    while let Some(q_start) = inner[cursor..].find('"') {
                        let lit_start = cursor + q_start + 1;
                        if let Some(q_end) = inner[lit_start..].find('"') {
                            let s = &inner[lit_start..lit_start + q_end];
                            if !s.is_empty() {
                                result.push(s.to_string());
                            }
                            cursor = lit_start + q_end + 1;
                        } else {
                            break;
                        }
                    }
                }
            }
            break;
        }
        search_from = pos + field.len();
    }
    result
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::fs;

    fn write_init_lua(dir: &Path, body: &str) -> std::path::PathBuf {
        let path = dir.join("init.lua");
        fs::write(&path, body).unwrap();
        path
    }

    #[test]
    fn parse_flat_meta() {
        let tmp = tempfile::tempdir().unwrap();
        let path = write_init_lua(
            tmp.path(),
            r#"
local M = {}
M.meta = {
    name = "my_pkg",
    version = "1.0.0",
    description = "A test package",
    category = "reasoning",
}
return M
"#,
        );

        let pkg = PkgEntity::parse_from_init_lua(&path).expect("should parse");
        assert_eq!(pkg.name, "my_pkg");
        assert_eq!(pkg.version.as_deref(), Some("1.0.0"));
        assert_eq!(pkg.description.as_deref(), Some("A test package"));
        assert_eq!(pkg.category.as_deref(), Some("reasoning"));
    }

    #[test]
    fn parse_tags_from_nested_table() {
        let tmp = tempfile::tempdir().unwrap();
        let path = write_init_lua(
            tmp.path(),
            r#"
local M = {}
M.meta = {
    name = "nested_pkg",
    tags = { "a", "b" },
    description = "After nested",
}
return M
"#,
        );

        let pkg = PkgEntity::parse_from_init_lua(&path).expect("should parse");
        assert_eq!(pkg.name, "nested_pkg");
        assert_eq!(pkg.description.as_deref(), Some("After nested"));
        assert_eq!(
            pkg.tags.as_deref(),
            Some(vec!["a".to_string(), "b".to_string()].as_slice())
        );
    }

    #[test]
    fn parse_tags_absent() {
        let tmp = tempfile::tempdir().unwrap();
        let path = write_init_lua(
            tmp.path(),
            r#"
local M = {}
M.meta = {
    name = "no_tags_pkg",
    description = "No tags",
}
return M
"#,
        );

        let pkg = PkgEntity::parse_from_init_lua(&path).expect("should parse");
        assert_eq!(pkg.name, "no_tags_pkg");
        assert!(pkg.tags.is_none());
    }

    #[test]
    fn parse_tags_empty_array() {
        let tmp = tempfile::tempdir().unwrap();
        let path = write_init_lua(
            tmp.path(),
            r#"
local M = {}
M.meta = {
    name = "empty_tags_pkg",
    tags = {},
    description = "Empty tags",
}
return M
"#,
        );

        let pkg = PkgEntity::parse_from_init_lua(&path).expect("should parse");
        assert_eq!(pkg.name, "empty_tags_pkg");
        assert!(pkg.tags.is_none());
    }

    #[test]
    fn parse_concat_string_literals() {
        let tmp = tempfile::tempdir().unwrap();
        let path = write_init_lua(
            tmp.path(),
            r#"
local M = {}
M.meta = {
    name = "concat_pkg",
    version = "0.1.0",
    description = "foo "
        .. "bar "
        .. "baz",
    category = "reasoning",
}
return M
"#,
        );

        let pkg = PkgEntity::parse_from_init_lua(&path).expect("should parse");
        assert_eq!(pkg.description.as_deref(), Some("foo bar baz"));
    }

    #[test]
    fn parse_word_boundary_for_description() {
        let tmp = tempfile::tempdir().unwrap();
        let path = write_init_lua(
            tmp.path(),
            r#"
local M = {}
M.meta = {
    name = "wb_pkg",
    short_description = "should not match",
    description = "correct one",
}
return M
"#,
        );

        let pkg = PkgEntity::parse_from_init_lua(&path).expect("should parse");
        assert_eq!(pkg.name, "wb_pkg");
        assert_eq!(pkg.description.as_deref(), Some("correct one"));
    }

    #[test]
    fn parse_meta_large_leading_docstring() {
        let tmp = tempfile::tempdir().unwrap();
        let mut content = String::new();
        for i in 0..120 {
            content.push_str(&format!("--- line {i}: long doc comment\n"));
        }
        content.push_str(
            r#"
local M = {}
M.meta = {
    name = "late_meta_pkg",
    version = "0.2.0",
    description = "Located past 2KB",
    category = "test",
}
return M
"#,
        );
        assert!(content.len() > 2048, "fixture should exceed 2KB");
        let path = write_init_lua(tmp.path(), &content);

        let pkg = PkgEntity::parse_from_init_lua(&path).expect("should parse");
        assert_eq!(pkg.name, "late_meta_pkg");
        assert_eq!(pkg.version.as_deref(), Some("0.2.0"));
        assert_eq!(pkg.description.as_deref(), Some("Located past 2KB"));
        assert_eq!(pkg.category.as_deref(), Some("test"));
    }

    #[test]
    fn parse_returns_none_without_meta_block() {
        // Mirrors the alc_shapes case: an init.lua with no M.meta block at
        // all. This is the **silent exclusion gate** — the caller drops
        // these directories from the hub index without warning.
        let tmp = tempfile::tempdir().unwrap();
        let path = write_init_lua(
            tmp.path(),
            r#"
--- alc_shapes — type DSL (not a package)
local M = {}
return M
"#,
        );

        assert!(PkgEntity::parse_from_init_lua(&path).is_none());
    }

    #[test]
    fn parse_returns_none_when_name_empty() {
        let tmp = tempfile::tempdir().unwrap();
        let path = write_init_lua(
            tmp.path(),
            r#"
local M = {}
M.meta = {
    name = "",
    version = "1.0.0",
}
return M
"#,
        );

        assert!(PkgEntity::parse_from_init_lua(&path).is_none());
    }

    #[test]
    fn parse_returns_none_when_file_missing() {
        let tmp = tempfile::tempdir().unwrap();
        let path = tmp.path().join("nonexistent.lua");
        assert!(PkgEntity::parse_from_init_lua(&path).is_none());
    }

    #[test]
    fn extracts_docstring_and_meta() {
        let tmp = tempfile::tempdir().unwrap();
        let path = write_init_lua(
            tmp.path(),
            r#"--- cascade — Multi-level routing with confidence gating
--- Based on: "FrugalGPT" (Chen et al., 2023)

local M = {}
M.meta = {
    name = "cascade",
    version = "0.1.0",
    description = "Multi-level routing",
    category = "meta",
}
return M
"#,
        );

        let pkg = PkgEntity::parse_from_init_lua(&path).expect("should parse");
        assert_eq!(pkg.name, "cascade");
        let doc = pkg.docstring.expect("docstring should be present");
        assert!(doc.contains("FrugalGPT"));
        assert!(doc.contains("Multi-level"));
        assert!(!doc.contains("local M"));
    }

    #[test]
    fn docstring_absent_when_no_leading_comments() {
        let tmp = tempfile::tempdir().unwrap();
        let path = write_init_lua(
            tmp.path(),
            r#"local M = {}
M.meta = { name = "nodoc" }
return M
"#,
        );
        let pkg = PkgEntity::parse_from_init_lua(&path).expect("should parse");
        assert!(pkg.docstring.is_none());
    }

    #[test]
    fn m_dot_meta_inside_comment_is_ignored() {
        // A `M.meta` reference inside a `--` comment must not hijack the
        // parser. The real block below it should still be found.
        let tmp = tempfile::tempdir().unwrap();
        let path = write_init_lua(
            tmp.path(),
            r#"
-- example: M.meta = { name = "decoy" }
local M = {}
M.meta = {
    name = "real",
}
return M
"#,
        );
        let pkg = PkgEntity::parse_from_init_lua(&path).expect("should parse");
        assert_eq!(pkg.name, "real");
    }

    #[test]
    fn serde_round_trip_preserves_none_vs_empty() {
        // Wire format contract: None is serialized as null; empty string
        // deserializes as Some("") (not None). Keep these separable so the
        // consumer can distinguish "field absent" from "field present but
        // empty".
        let pkg = PkgEntity {
            name: "p".into(),
            version: None,
            description: Some(String::new()),
            category: Some("meta".into()),
            docstring: None,
            tags: None,
            pkg_type: None,
            type_source: None,
        };
        let json = serde_json::to_string(&pkg).unwrap();
        assert!(json.contains("\"version\":null"), "version null: {json}");
        assert!(
            json.contains("\"description\":\"\""),
            "description empty string: {json}"
        );
        assert!(
            json.contains("\"docstring\":null"),
            "docstring null: {json}"
        );

        let back: PkgEntity = serde_json::from_str(&json).unwrap();
        assert_eq!(back, pkg);
    }

    #[test]
    fn serde_deserialize_accepts_missing_optional_fields() {
        // Legacy hub_index.json entries may omit every optional field;
        // they must deserialize as None (not error).
        let json = r#"{"name":"minimal"}"#;
        let pkg: PkgEntity = serde_json::from_str(json).unwrap();
        assert_eq!(pkg.name, "minimal");
        assert!(pkg.version.is_none());
        assert!(pkg.description.is_none());
        assert!(pkg.category.is_none());
        assert!(pkg.docstring.is_none());
    }

    // ─── PkgType / pkg_type field tests ──────────────────────────

    #[test]
    fn parse_type_from_meta() {
        // Acceptance criterion 5: M.meta.type = "library" → PkgType::Library
        let tmp = tempfile::tempdir().unwrap();
        let path = write_init_lua(
            tmp.path(),
            r#"
local M = {}
M.meta = {
    name = "lib_pkg",
    type = "library",
    version = "1.0.0",
}
return M
"#,
        );

        let pkg = PkgEntity::parse_from_init_lua(&path).expect("should parse");
        assert_eq!(pkg.name, "lib_pkg");
        assert_eq!(pkg.pkg_type, Some(PkgType::Library));
    }

    #[test]
    fn parse_type_runnable_explicit() {
        // Acceptance criterion 6: M.meta.type = "runnable" → PkgType::Runnable
        let tmp = tempfile::tempdir().unwrap();
        let path = write_init_lua(
            tmp.path(),
            r#"
local M = {}
M.meta = {
    name = "run_pkg",
    type = "runnable",
}
function M.run(ctx) end
return M
"#,
        );

        let pkg = PkgEntity::parse_from_init_lua(&path).expect("should parse");
        assert_eq!(pkg.pkg_type, Some(PkgType::Runnable));
    }

    #[test]
    fn auto_detect_type_from_m_run() {
        // Acceptance criterion 7: no M.meta.type, but M.run is present → Runnable
        let tmp = tempfile::tempdir().unwrap();
        let path = write_init_lua(
            tmp.path(),
            r#"
local M = {}
M.meta = {
    name = "auto_run_pkg",
}
function M.run(ctx)
    return alc.llm("hello")
end
return M
"#,
        );

        let pkg = PkgEntity::parse_from_init_lua(&path).expect("should parse");
        assert_eq!(
            pkg.pkg_type,
            Some(PkgType::Runnable),
            "M.run present → Runnable"
        );
    }

    #[test]
    fn auto_detect_type_library_no_m_run() {
        // Acceptance criterion 8: no M.meta.type, no M.run → Library
        let tmp = tempfile::tempdir().unwrap();
        let path = write_init_lua(
            tmp.path(),
            r#"
local M = {}
M.meta = {
    name = "auto_lib_pkg",
    description = "A pure library with no run entry point",
}
function M.create(opts)
    return {}
end
return M
"#,
        );

        let pkg = PkgEntity::parse_from_init_lua(&path).expect("should parse");
        assert_eq!(pkg.pkg_type, Some(PkgType::Library), "no M.run → Library");
    }

    #[test]
    fn detect_has_run_ignores_comments() {
        // Acceptance criterion 9: M.run in a comment must not trigger detection.
        assert!(
            !detect_has_run("-- M.run = function(ctx) end\nlocal M = {}\n"),
            "commented-out M.run should not be detected"
        );
        // But a real M.run assignment on the same line after a comment is still
        // in the non-comment portion before '--', so it IS detected.
        assert!(
            detect_has_run("local M = {}\nM.run = function(ctx) end\n"),
            "real M.run should be detected"
        );
        // Edge case: M.run after inline comment on same line — not detected
        // because the effective portion before '--' does not contain M.run.
        assert!(
            !detect_has_run("local x = 1 -- M.run is described here\n"),
            "M.run inside inline comment should not be detected"
        );
    }

    #[test]
    fn serde_round_trip_with_pkg_type() {
        // Acceptance criterion 10: PkgType::Library survives a JSON round-trip.
        let pkg = PkgEntity {
            name: "lib".into(),
            version: Some("0.1.0".into()),
            description: None,
            category: None,
            docstring: None,
            tags: None,
            pkg_type: Some(PkgType::Library),
            type_source: None,
        };
        let json = serde_json::to_string(&pkg).unwrap();
        assert!(
            json.contains("\"type\":\"library\""),
            "wire key must be 'type': {json}"
        );
        let back: PkgEntity = serde_json::from_str(&json).unwrap();
        assert_eq!(back.pkg_type, Some(PkgType::Library));
        assert_eq!(back, pkg);
    }

    #[test]
    fn serde_deserialize_missing_pkg_type() {
        // Acceptance criterion 11: JSON without "type" key → pkg_type = None
        let json = r#"{"name":"legacy_pkg","version":"1.0.0"}"#;
        let pkg: PkgEntity = serde_json::from_str(json).unwrap();
        assert_eq!(pkg.name, "legacy_pkg");
        assert!(
            pkg.pkg_type.is_none(),
            "missing 'type' key must deserialize as None"
        );
    }

    // ─── TypeSource / type_source provenance tests ───────────────

    #[test]
    fn parse_explicit_type_sets_source_explicit() {
        // Acceptance criterion: M.meta.type = "library" explicit → type_source == Explicit
        let tmp = tempfile::tempdir().unwrap();
        let path = write_init_lua(
            tmp.path(),
            r#"
local M = {}
M.meta = {
    name = "explicit_lib",
    type = "library",
    version = "1.0.0",
}
return M
"#,
        );

        let pkg = PkgEntity::parse_from_init_lua(&path).expect("should parse");
        assert_eq!(pkg.pkg_type, Some(PkgType::Library));
        assert_eq!(
            pkg.type_source,
            Some(TypeSource::Explicit),
            "explicit M.meta.type must yield TypeSource::Explicit"
        );
    }

    #[test]
    fn parse_auto_detect_runnable_sets_source() {
        // Acceptance criterion: type absent + M.run present → AutoDetectedRunnable
        let tmp = tempfile::tempdir().unwrap();
        let path = write_init_lua(
            tmp.path(),
            r#"
local M = {}
M.meta = {
    name = "auto_run",
    version = "0.1.0",
}
function M.run(ctx)
    return alc.llm("hello")
end
return M
"#,
        );

        let pkg = PkgEntity::parse_from_init_lua(&path).expect("should parse");
        assert_eq!(pkg.pkg_type, Some(PkgType::Runnable));
        assert_eq!(
            pkg.type_source,
            Some(TypeSource::AutoDetectedRunnable),
            "M.run present without explicit type must yield AutoDetectedRunnable"
        );
    }

    #[test]
    fn parse_auto_detect_library_sets_source() {
        // Acceptance criterion: type absent + no M.run → AutoDetectedLibrary
        let tmp = tempfile::tempdir().unwrap();
        let path = write_init_lua(
            tmp.path(),
            r#"
local M = {}
M.meta = {
    name = "auto_lib",
    description = "A pure library",
}
function M.create(opts)
    return {}
end
return M
"#,
        );

        let pkg = PkgEntity::parse_from_init_lua(&path).expect("should parse");
        assert_eq!(pkg.pkg_type, Some(PkgType::Library));
        assert_eq!(
            pkg.type_source,
            Some(TypeSource::AutoDetectedLibrary),
            "no M.run and no explicit type must yield AutoDetectedLibrary"
        );
    }

    #[test]
    fn serde_round_trip_with_type_source() {
        // Acceptance criterion: TypeSource::AutoDetectedLibrary JSON round-trip
        // → wire string "auto_detected_library"
        let tmp = tempfile::tempdir().unwrap();
        let path = write_init_lua(
            tmp.path(),
            r#"
local M = {}
M.meta = {
    name = "rt_lib",
}
return M
"#,
        );
        let pkg = PkgEntity::parse_from_init_lua(&path).expect("should parse");
        assert_eq!(pkg.type_source, Some(TypeSource::AutoDetectedLibrary));

        let json = serde_json::to_string(&pkg).unwrap();
        assert!(
            json.contains("\"type_source\":\"auto_detected_library\""),
            "wire string must be 'auto_detected_library': {json}"
        );

        let back: PkgEntity = serde_json::from_str(&json).unwrap();
        assert_eq!(back.type_source, Some(TypeSource::AutoDetectedLibrary));
        assert_eq!(back, pkg);
    }

    #[test]
    fn serde_deserialize_missing_type_source_is_none() {
        // Acceptance criterion: JSON without "type_source" key → type_source == None
        // (backward compat for legacy hub_index.json entries)
        let json = r#"{"name":"legacy_no_source","type":"library"}"#;
        let pkg: PkgEntity = serde_json::from_str(json).unwrap();
        assert_eq!(pkg.name, "legacy_no_source");
        assert_eq!(pkg.pkg_type, Some(PkgType::Library));
        assert!(
            pkg.type_source.is_none(),
            "missing 'type_source' key must deserialize as None (legacy compat)"
        );
    }
}