Skip to main content

algocline_app/service/
gendoc.rs

1//! `AppService::hub_gendoc` — embedded Lua `gen_docs` runner.
2//!
3//! Runs the bundled-packages `tools/gen_docs.lua` pipeline in an
4//! in-process `mlua` VM to produce human-readable documentation
5//! artifacts (`narrative/{pkg}.md`, `llms.txt`, `llms-full.txt`,
6//! optional `hub/{pkg}.json` / `context7.json` / `.devin/wiki.json`)
7//! from a freshly indexed `hub_index.json`.
8//!
9//! Embedding strategy:
10//!
11//! - The Lua sources under `service/lua/gendoc/` and
12//!   `service/gendoc/alc_shapes/` are pulled in with `include_str!`
13//!   at compile time and registered on `package.preload` so that
14//!   `require("tools.docs.X")` and `require("alc_shapes")` resolve
15//!   without touching the filesystem.
16//! - `alc_shapes` and all sub-modules are fully vendored via
17//!   `include_str!` from `service/gendoc/alc_shapes/*.lua`. The
18//!   `source_dir`'s on-disk `alc_shapes/` directory is never
19//!   consulted at runtime. This ensures parity for any third-party
20//!   package author invoking `alc_hub_dist` against their own source
21//!   tree without vendoring `alc_shapes` themselves.
22//! - `config_path` (optional, backward-compat) is a caller-supplied TOML file
23//!   with top-level `context7` and/or `devin` tables.  When `config_path` is
24//!   omitted and the active projections include `context7` or `devin`, the
25//!   project root's `alc.toml` is auto-explored for `[hub.context7]` /
26//!   `[hub.devin]` sections; core defaults are used when those sections are
27//!   absent.  In all cases the resolved tables are exposed under the
28//!   `tools.docs.context7_config` / `tools.docs.devin_wiki_config` module
29//!   names that `gen_docs` `require`s.  `.lua` config files are no longer
30//!   accepted (retired in v0.26); see `docs/hub-gendoc-config.md` for the
31//!   migration guide.
32//! - `print` / `io.stdout.write` / `io.stderr.write` are redirected
33//!   into Rust-side `String` buffers so callers observe the Lua
34//!   progress log through the MCP response instead of dropping it to
35//!   the server stderr.
36//! - `os.exit(code)` is overridden so it raises a structured Lua
37//!   error rather than terminating the process; non-zero exits are
38//!   converted into `Err(...)`.
39//!
40//! Per the project-level Error propagation rule
41//! (`CLAUDE.md §Service 層 Error 伝播規律`), every `mlua::Result` is
42//! surfaced via `?` with a `gendoc:` prefix — no `warn!` drops, no
43//! `unwrap_or_default`, no silent `Err(_) =>` branches.
44
45use std::path::PathBuf;
46use std::sync::{Arc, Mutex};
47
48use mlua::{Lua, Table, Value};
49use semver::{Version, VersionReq};
50use serde::Deserialize;
51use thiserror::Error;
52
53use super::hub_dist_preset::load_hub_projection_config;
54use super::project::resolve_project_root;
55use super::AppService;
56
57pub mod templates;
58
59// ── alc_shapes version pinning ────────────────────────────────────────
60
61/// Version string embedded in the vendored `alc_shapes/init.lua`.
62/// Must match the `M.VERSION` declaration in that file exactly.
63pub(crate) const EMBEDDED_ALC_SHAPES_VERSION: &str = "0.25.1";
64
65#[derive(Debug, Error)]
66enum ShapesVersionError {
67    #[error("alc_shapes version mismatch: embedded={embedded}, mirror={mirror}. {hint}")]
68    Mismatch {
69        embedded: String,
70        mirror: String,
71        hint: &'static str,
72    },
73    #[error("alc_shapes mirror init.lua at '{path}' has no parseable M.VERSION declaration")]
74    Malformed { path: PathBuf },
75}
76
77const SHAPES_VERSION_HINT: &str = "Align bundled alc_shapes/ to match core, \
78    or upgrade algocline core to the mirror version. See CHANGELOG for details.";
79
80// ── pkg compat-range error variants ──────────────────────────────
81
82#[derive(Debug, Error)]
83enum ShapesCompatError {
84    #[error(
85        "pkg '{pkg_name}': alc_shapes_compat range '{declared_range}' does not match \
86         embedded alc_shapes@{actual_version}. {hint}"
87    )]
88    Violation {
89        pkg_name: String,
90        declared_range: String,
91        actual_version: String,
92        hint: &'static str,
93    },
94    #[error(
95        "pkg '{pkg_name}': alc_shapes_compat value '{value}' is not a valid semver range: {cause}"
96    )]
97    Malformed {
98        pkg_name: String,
99        value: String,
100        cause: String,
101    },
102    #[error("I/O error reading pkg compat from '{path}': {cause}")]
103    Io { path: PathBuf, cause: String },
104}
105
106/// Top-level error type for `AppService::hub_gendoc`.
107///
108/// Wraps the typed pre-flight errors (`ShapesVersionError`,
109/// `ShapesCompatError`) via `#[from]` so callers in this module can
110/// `?`-propagate without stringifying. The MCP wire layer converts the
111/// variant to a `gendoc:`-prefixed string at the AppService boundary
112/// (`Result<String, String>`), but the structure is preserved inside
113/// this module so future consumers (telemetry, CLI JSON output) can
114/// match on the variant rather than parsing a formatted string.
115#[derive(Debug, Error)]
116enum HubGendocError {
117    #[error("{0}")]
118    ShapesVersion(#[from] ShapesVersionError),
119    #[error("{0}")]
120    ShapesCompat(#[from] ShapesCompatError),
121}
122
123const SHAPES_COMPAT_VIOLATION_HINT: &str = "Declare a wider alc_shapes_compat range in M.meta, \
124     or upgrade/downgrade algocline core to a matching version.";
125
126/// Check the mirror's `M.VERSION` against `EMBEDDED_ALC_SHAPES_VERSION`.
127///
128/// If `source_dir` is `None` or its `alc_shapes/init.lua` does not
129/// exist, returns `Ok(())` immediately (no-op path). Otherwise reads
130/// the file, extracts `M.VERSION = "x.y.z"` with a hand-rolled parser
131/// (no `regex` dep), and fails with a typed error on mismatch.
132fn check_mirror_shapes_version(source_dir: Option<&str>) -> Result<(), ShapesVersionError> {
133    let Some(dir) = source_dir else {
134        return Ok(());
135    };
136    let path: PathBuf = [dir, "alc_shapes", "init.lua"].iter().collect();
137    if !path.exists() {
138        return Ok(());
139    }
140    let src = std::fs::read_to_string(&path)
141        .map_err(|_| ShapesVersionError::Malformed { path: path.clone() })?;
142    let mirror_ver = extract_m_version(&src)
143        .ok_or_else(|| ShapesVersionError::Malformed { path: path.clone() })?;
144    if mirror_ver != EMBEDDED_ALC_SHAPES_VERSION {
145        return Err(ShapesVersionError::Mismatch {
146            embedded: EMBEDDED_ALC_SHAPES_VERSION.to_string(),
147            mirror: mirror_ver,
148            hint: SHAPES_VERSION_HINT,
149        });
150    }
151    Ok(())
152}
153
154/// Hand-rolled parser that extracts the double-quoted value after a
155/// `<marker> = "..."` pattern in a Lua source string.
156///
157/// Finds the first occurrence of `marker` followed (with optional
158/// whitespace) by `=` and then a double-quoted string. Returns the
159/// quoted content on success, `None` when the pattern is absent or
160/// malformed.
161///
162/// Both `extract_m_version` and `extract_m_meta_compat` delegate to
163/// this shared helper to avoid duplicating the parsing logic.
164fn extract_quoted_value<'a>(src: &'a str, marker: &str) -> Option<&'a str> {
165    let start = src.find(marker)?;
166    let after_marker = src[start + marker.len()..].trim_start();
167    let after_eq = after_marker.strip_prefix('=')?;
168    let after_eq = after_eq.trim_start();
169    let after_quote = after_eq.strip_prefix('"')?;
170    let end = after_quote.find('"')?;
171    Some(&after_quote[..end])
172}
173
174/// Hand-rolled parser for `M.VERSION = "x.y.z"` in a Lua source string.
175///
176/// Finds the first occurrence of `M.VERSION` followed (with optional
177/// whitespace) by `=` and then a double-quoted string. Returns the
178/// quoted content on success.
179fn extract_m_version(src: &str) -> Option<String> {
180    extract_quoted_value(src, "M.VERSION").map(str::to_string)
181}
182
183/// Hand-rolled parser for `alc_shapes_compat = "..."` in a pkg `init.lua`.
184///
185/// Matches the pattern `alc_shapes_compat` (optionally preceded by
186/// `M.meta.` or other context) followed by `= "..."`. Returns a borrow
187/// into `src` on success, `None` when the field is absent.
188fn extract_m_meta_compat(src: &str) -> Option<&str> {
189    extract_quoted_value(src, "alc_shapes_compat")
190}
191
192/// Scan every package directory under `source_dir` and verify that each
193/// package's declared `alc_shapes_compat` semver range includes the
194/// embedded alc_shapes version.
195///
196/// **Dispatch rules** (applied per package `init.lua`):
197/// - No `alc_shapes_compat` field → push a warning string (undeclared,
198///   backward compat) and continue.
199/// - Malformed range → return `Err(ShapesCompatMalformed)`.
200/// - Range declared and in-range → continue silently.
201/// - Range declared but out-of-range → return `Err(ShapesCompatViolation)`.
202///
203/// Packages whose directories do not contain an `init.lua` are silently
204/// skipped (same rule as `build_index`). The `alc_shapes/` directory
205/// (no `M.meta.name`) is naturally excluded because `extract_m_meta_compat`
206/// will return `None` and the warning path handles that without error.
207///
208/// Returns `(warnings, ())` on success; the first package that violates
209/// its declared range terminates the scan with `Err`.
210fn check_pkg_compat(source_dir: &str) -> Result<Vec<String>, ShapesCompatError> {
211    let current = Version::parse(EMBEDDED_ALC_SHAPES_VERSION)
212        .expect("EMBEDDED_ALC_SHAPES_VERSION is a valid semver constant");
213
214    let pkg_dir = std::path::Path::new(source_dir);
215    let dir_entries = std::fs::read_dir(pkg_dir).map_err(|e| ShapesCompatError::Io {
216        path: pkg_dir.to_path_buf(),
217        cause: e.to_string(),
218    })?;
219
220    let mut warnings = Vec::new();
221
222    for entry in dir_entries {
223        let entry = entry.map_err(|e| ShapesCompatError::Io {
224            path: pkg_dir.to_path_buf(),
225            cause: e.to_string(),
226        })?;
227        if !entry.path().is_dir() {
228            continue;
229        }
230        let dir_name = match entry.file_name().to_str() {
231            Some(n) if !n.starts_with('.') && !n.starts_with('_') => n.to_string(),
232            _ => continue,
233        };
234
235        let init_lua = entry.path().join("init.lua");
236        if !init_lua.exists() {
237            continue;
238        }
239
240        let src = std::fs::read_to_string(&init_lua).map_err(|e| ShapesCompatError::Io {
241            path: init_lua.clone(),
242            cause: e.to_string(),
243        })?;
244
245        match extract_m_meta_compat(&src) {
246            None => {
247                warnings.push(format!(
248                    "pkg {dir_name}: alc_shapes_compat not declared, \
249                     continuing with current alc_shapes@{EMBEDDED_ALC_SHAPES_VERSION}"
250                ));
251            }
252            Some(raw) => {
253                let range = VersionReq::parse(raw).map_err(|e| ShapesCompatError::Malformed {
254                    pkg_name: dir_name.clone(),
255                    value: raw.to_string(),
256                    cause: e.to_string(),
257                })?;
258
259                if !range.matches(&current) {
260                    return Err(ShapesCompatError::Violation {
261                        pkg_name: dir_name,
262                        declared_range: raw.to_string(),
263                        actual_version: EMBEDDED_ALC_SHAPES_VERSION.to_string(),
264                        hint: SHAPES_COMPAT_VIOLATION_HINT,
265                    });
266                }
267                // In-range: continue silently.
268            }
269        }
270    }
271
272    Ok(warnings)
273}
274
275// ── Embedded Lua sources ──────────────────────────────────────────
276
277const LUA_GEN_DOCS: &str = include_str!("lua/gendoc/gen_docs.lua");
278const LUA_DOCS_LIST: &str = include_str!("lua/gendoc/docs/list.lua");
279const LUA_DOCS_EXTRACT: &str = include_str!("lua/gendoc/docs/extract.lua");
280const LUA_DOCS_PROJECTIONS: &str = include_str!("lua/gendoc/docs/projections.lua");
281const LUA_DOCS_PKG_INFO: &str = include_str!("lua/gendoc/docs/pkg_info.lua");
282const LUA_DOCS_JSON: &str = include_str!("lua/gendoc/docs/json.lua");
283const LUA_DOCS_LINT: &str = include_str!("lua/gendoc/docs/lint.lua");
284const LUA_DOCS_ENTITY_SCHEMAS: &str = include_str!("lua/gendoc/docs/entity_schemas.lua");
285
286// ── Vendored alc_shapes (fully embedded; no disk fallback) ───────
287
288const LUA_ALC_SHAPES_INIT: &str = include_str!("gendoc/alc_shapes/init.lua");
289const LUA_ALC_SHAPES_T: &str = include_str!("gendoc/alc_shapes/t.lua");
290const LUA_ALC_SHAPES_REFLECT: &str = include_str!("gendoc/alc_shapes/reflect.lua");
291const LUA_ALC_SHAPES_CHECK: &str = include_str!("gendoc/alc_shapes/check.lua");
292const LUA_ALC_SHAPES_INSTRUMENT: &str = include_str!("gendoc/alc_shapes/instrument.lua");
293const LUA_ALC_SHAPES_LUACATS: &str = include_str!("gendoc/alc_shapes/luacats.lua");
294const LUA_ALC_SHAPES_SPEC_RESOLVER: &str = include_str!("gendoc/alc_shapes/spec_resolver.lua");
295
296/// All embedded preloads: vendored `alc_shapes` modules (in dependency
297/// order) followed by the `tools/docs/*` pipeline sources.
298///
299/// Registration order matters: sub-modules must appear before the
300/// modules that `require` them, matching the `alc_shapes/init.lua`
301/// dependency chain: `t` → `reflect` / `check` / `luacats` /
302/// `spec_resolver` → `instrument` → `init`.
303const EMBEDDED_TOOL_PRELOADS: &[(&str, &str)] = &[
304    // alc_shapes sub-modules (no intra-module deps except alc_shapes.t)
305    ("alc_shapes.t", LUA_ALC_SHAPES_T),
306    ("alc_shapes.reflect", LUA_ALC_SHAPES_REFLECT),
307    ("alc_shapes.check", LUA_ALC_SHAPES_CHECK),
308    ("alc_shapes.luacats", LUA_ALC_SHAPES_LUACATS),
309    ("alc_shapes.spec_resolver", LUA_ALC_SHAPES_SPEC_RESOLVER),
310    ("alc_shapes.instrument", LUA_ALC_SHAPES_INSTRUMENT),
311    // alc_shapes top-level (requires all sub-modules above)
312    ("alc_shapes", LUA_ALC_SHAPES_INIT),
313    ("tools.docs.list", LUA_DOCS_LIST),
314    ("tools.docs.extract", LUA_DOCS_EXTRACT),
315    ("tools.docs.projections", LUA_DOCS_PROJECTIONS),
316    ("tools.docs.pkg_info", LUA_DOCS_PKG_INFO),
317    ("tools.docs.json", LUA_DOCS_JSON),
318    ("tools.docs.lint", LUA_DOCS_LINT),
319    ("tools.docs.entity_schemas", LUA_DOCS_ENTITY_SCHEMAS),
320];
321
322/// IO / exit hooks installed into the VM right before `gen_docs.lua`
323/// runs. `_gendoc_out_append` / `_gendoc_err_append` are Rust
324/// closures registered under the same names on `_G`.
325///
326/// `io.stdout` / `io.stderr` are in mlua exposed as `FILE*` userdata
327/// whose metatable rejects arbitrary `__newindex` writes — so we
328/// cannot patch `io.stdout.write` in place. Instead we replace
329/// `io.stdout` / `io.stderr` wholesale with plain Lua tables that
330/// expose a `write` method delegating to the Rust-side append
331/// closures. Both method-style (`io.stdout:write(x)`) and
332/// function-style (`io.stdout.write(io.stdout, x)`) calls are
333/// supported; both are used in the bundled `gen_docs.lua`.
334const HOOK_SCRIPT: &str = r##"
335os.exit = function(code)
336    local c = code or 0
337    local tbl = { __gendoc_exit = c }
338    -- Attach __tostring so the raw mlua error message embeds the
339    -- code as "__gendoc_exit=N", letting the Rust side recover it
340    -- via substring match instead of walking CallbackError internals.
341    setmetatable(tbl, { __tostring = function(self)
342        return string.format("__gendoc_exit=%d", self.__gendoc_exit or 0)
343    end })
344    error(tbl, 0)
345end
346io.stdout = {
347    write = function(self, ...)
348        local args = { ... }
349        for i = 1, select("#", ...) do
350            args[i] = tostring(args[i])
351        end
352        _gendoc_out_append(table.concat(args))
353        return self
354    end,
355}
356io.stderr = {
357    write = function(self, ...)
358        local args = { ... }
359        for i = 1, select("#", ...) do
360            args[i] = tostring(args[i])
361        end
362        _gendoc_err_append(table.concat(args))
363        return self
364    end,
365}
366print = function(...)
367    local args = { ... }
368    for i = 1, select("#", ...) do
369        args[i] = tostring(args[i])
370    end
371    _gendoc_out_append(table.concat(args, "\t") .. "\n")
372end
373"##;
374
375/// Marker set on the Lua error table by the `os.exit` override.
376const EXIT_MARKER: &str = "__gendoc_exit";
377
378impl AppService {
379    /// See [`crate::EngineApi::hub_gendoc`] for parameter semantics.
380    ///
381    /// `config_path` format (TOML):
382    ///
383    /// ```toml
384    /// [context7]
385    /// projectTitle = "my project"
386    /// description = "..."
387    /// rules = []
388    ///
389    /// [devin]
390    /// project_name = "my project"
391    /// ```
392    ///
393    /// Notes:
394    /// - `context7` / `devin` are optional individually.
395    /// - When present, each key must be a table.
396    /// - TOML arrays/tables are converted recursively to Lua tables.
397    /// - See `docs/hub-gendoc-config.md` for a concrete schema example.
398    ///
399    /// Returns a JSON string of the form:
400    ///
401    /// ```json
402    /// { "source_dir": "...", "out_dir": "...", "stdout": "...", "stderr": "..." }
403    /// ```
404    ///
405    /// Non-zero `os.exit`, missing `hub_index.json`, Lua runtime
406    /// errors, and `config_path` read failures are all surfaced as
407    /// `Err` with a `gendoc:` prefix.
408    pub fn hub_gendoc(
409        &self,
410        source_dir: &str,
411        out_dir: Option<&str>,
412        projections: Option<&[String]>,
413        config_path: Option<&str>,
414        lint_strict: Option<bool>,
415    ) -> Result<String, String> {
416        let projection_flags = ProjectionFlags::from_list(projections)?;
417
418        let resolved_out_dir = out_dir
419            .map(|s| s.to_string())
420            .unwrap_or_else(|| format!("{source_dir}/docs"));
421
422        // Reject mismatched mirror before starting the VM: if the
423        // caller's source_dir has an alc_shapes/init.lua whose
424        // M.VERSION differs from the embedded constant, fail early with
425        // a structured error. Variant structure is preserved via
426        // `HubGendocError` (`?` + `#[from]`) and stringified only at
427        // this function's `Result<String, String>` boundary.
428        let compat_warnings = run_preflight(source_dir).map_err(|e| format!("gendoc: {e}"))?;
429
430        let lua = Lua::new();
431
432        register_preloads(&lua)?;
433
434        // Config injection — must be wired as preload *before* `gen_docs.lua`
435        // executes so that `require("tools.docs.context7_config")` resolves.
436        //
437        // Dispatch order:
438        //   1. `.lua` config_path → explicit error (.lua support retired in v0.26)
439        //   2. `.toml` config_path → flat [context7]/[devin] backward-compat path
440        //   3. None + context7/devin projection → alc.toml auto-explore + core defaults
441        //   4. None + no context7/devin → skip (no config needed)
442        match config_path {
443            Some(path) if path.to_lowercase().ends_with(".lua") => {
444                return Err(
445                    "gendoc: config_path extension '.lua' is no longer supported; use .toml"
446                        .to_string(),
447                );
448            }
449            Some(path) => {
450                // Backward-compat: flat [context7] / [devin] TOML file.
451                inject_config_preloads_toml(&lua, &preload_table(&lua)?, path)?;
452            }
453            None if projection_flags.context7 || projection_flags.devin => {
454                // New path: auto-explore alc.toml and merge with core defaults.
455                let project_root = resolve_project_root(Some(source_dir));
456                let merged = load_hub_projection_config(project_root.as_deref())?;
457                inject_config_subtable(
458                    &lua,
459                    &preload_table(&lua)?,
460                    Some(merged.to_context7_toml()),
461                    "context7",
462                    "_gendoc_context7_config",
463                    "tools.docs.context7_config",
464                )?;
465                inject_config_subtable(
466                    &lua,
467                    &preload_table(&lua)?,
468                    Some(merged.to_devin_toml()),
469                    "devin",
470                    "_gendoc_devin_config",
471                    "tools.docs.devin_wiki_config",
472                )?;
473            }
474            None => {
475                // No context7/devin projections requested — config injection not needed.
476            }
477        }
478
479        let out_buf: Arc<Mutex<String>> = Arc::new(Mutex::new(String::new()));
480        let err_buf: Arc<Mutex<String>> = Arc::new(Mutex::new(String::new()));
481
482        install_io_hooks(&lua, Arc::clone(&out_buf), Arc::clone(&err_buf))?;
483
484        install_argv(
485            &lua,
486            source_dir,
487            &resolved_out_dir,
488            &projection_flags,
489            lint_strict.unwrap_or(false),
490        )?;
491
492        // Run the IO / exit hook script (must come after
493        // `_gendoc_out_append` / `_gendoc_err_append` are installed,
494        // but before `gen_docs.lua` is exec'd).
495        lua.load(HOOK_SCRIPT)
496            .set_name("@embedded:gendoc/hooks.lua")
497            .exec()
498            .map_err(|e| format!("gendoc: hooks inject failed: {e}"))?;
499
500        // Execute `gen_docs.lua`. The file ends with `main(arg)`.
501        //
502        // `lua.load()` uses the string path of `luaL_loadbuffer`
503        // which does NOT strip a `#!` shebang line (the shebang is
504        // only accepted by `luaL_loadfile`). The embedded
505        // `gen_docs.lua` starts with `#!/usr/bin/env lua`, so we
506        // skip the first line before loading.
507        let gen_docs_body = strip_shebang(LUA_GEN_DOCS);
508        let exec_result = lua
509            .load(gen_docs_body)
510            .set_name("@embedded:gendoc/gen_docs.lua")
511            .exec();
512
513        let stdout_txt = read_buf(&out_buf)?;
514        let stderr_txt = read_buf(&err_buf)?;
515
516        match exec_result {
517            Ok(()) => {}
518            Err(e) => {
519                if let Some(code) = extract_exit_code(&e) {
520                    if code != 0 {
521                        return Err(format!(
522                            "gendoc: exited with code {code}\nstderr:\n{stderr_txt}"
523                        ));
524                    }
525                    // code == 0 is a clean shutdown via os.exit(0) —
526                    // fall through to the normal response.
527                } else {
528                    return Err(format!("gendoc: Lua error: {e}\nstderr:\n{stderr_txt}"));
529                }
530            }
531        }
532
533        Ok(build_response_json(
534            source_dir,
535            &resolved_out_dir,
536            &stdout_txt,
537            &stderr_txt,
538            &compat_warnings,
539        ))
540    }
541}
542
543/// Pre-flight: mirror version check, then compat-range scan.
544///
545/// Returns the list of compat warnings on success. Typed
546/// `HubGendocError` variants (`ShapesVersion` / `ShapesCompat`) are
547/// propagated via `?` and `#[from]` so the caller preserves variant
548/// structure until the MCP wire boundary stringifies.
549fn run_preflight(source_dir: &str) -> Result<Vec<String>, HubGendocError> {
550    check_mirror_shapes_version(Some(source_dir))?;
551    let warnings = check_pkg_compat(source_dir)?;
552    Ok(warnings)
553}
554
555// ── Helpers ───────────────────────────────────────────────────────
556
557#[derive(Debug, Default, Clone, Copy)]
558struct ProjectionFlags {
559    hub: bool,
560    context7: bool,
561    devin: bool,
562    lint: bool,
563    lint_only: bool,
564    luacats: bool,
565    /// narrative/{pkg}.md files are unconditionally emitted by the embedded
566    /// gen_docs.lua when lint_only=false, so this flag only acts as an
567    /// allowlist gate on the Rust side (approach A).
568    narrative: bool,
569    /// llms.txt and llms-full.txt are unconditionally emitted by the embedded
570    /// gen_docs.lua when lint_only=false, so this flag only acts as an
571    /// allowlist gate on the Rust side (approach A).
572    llms: bool,
573}
574
575impl ProjectionFlags {
576    fn from_list(projections: Option<&[String]>) -> Result<Self, String> {
577        let mut f = ProjectionFlags::default();
578        let Some(list) = projections else {
579            return Ok(f);
580        };
581        for p in list {
582            match p.as_str() {
583                "hub" => f.hub = true,
584                "context7" => f.context7 = true,
585                "devin" => f.devin = true,
586                "lint" => f.lint = true,
587                "lint_only" => {
588                    f.lint_only = true;
589                    f.lint = true;
590                }
591                "luacats" => f.luacats = true,
592                "narrative" => f.narrative = true,
593                "llms" => f.llms = true,
594                _ => {
595                    return Err(format!(
596                        "gendoc: unknown projection '{p}' (allowed: hub, context7, devin, lint, lint_only, luacats, narrative, llms)"
597                    ));
598                }
599            }
600        }
601        Ok(f)
602    }
603}
604
605/// Register all embedded `gen_docs` modules.
606///
607/// `alc_shapes` and its sub-modules are fully vendored via
608/// `include_str!` — no disk fallback. `tools/docs/*` pipeline sources
609/// are registered in the same pass.
610fn register_preloads(lua: &Lua) -> Result<(), String> {
611    let preload = preload_table(lua)?;
612    for (mod_name, src) in EMBEDDED_TOOL_PRELOADS.iter().copied() {
613        register_single_preload(lua, &preload, mod_name, src)?;
614    }
615    Ok(())
616}
617
618fn preload_table(lua: &Lua) -> Result<Table, String> {
619    // `globals().package.preload` is part of the Lua 5.4 / mlua
620    // contract; absence would indicate a VM that cannot run any
621    // meaningful Lua code, so `expect` with a justifying comment is
622    // the correct classification (see CLAUDE.md §Service 層 Error
623    // 伝播規律 "limited exceptions for unreachable VM invariants").
624    let package: Table = lua
625        .globals()
626        .get("package")
627        .map_err(|e| format!("gendoc: globals().package lookup failed: {e}"))?;
628    let preload: Table = package
629        .get("preload")
630        .map_err(|e| format!("gendoc: package.preload lookup failed: {e}"))?;
631    Ok(preload)
632}
633
634fn register_single_preload(
635    lua: &Lua,
636    preload: &Table,
637    mod_name: &'static str,
638    src: &'static str,
639) -> Result<(), String> {
640    let chunk_name = format!("@embedded:gendoc/{mod_name}.lua");
641    let loader = lua
642        .create_function(move |lua, ()| lua.load(src).set_name(chunk_name.clone()).eval::<Value>())
643        .map_err(|e| format!("gendoc: preload create_function failed for {mod_name}: {e}"))?;
644    preload
645        .set(mod_name, loader)
646        .map_err(|e| format!("gendoc: preload.set failed for {mod_name}: {e}"))?;
647    Ok(())
648}
649
650/// Extension-dispatch shim retained for unit tests.  The hot path in
651/// `hub_gendoc` dispatches directly; this function is used by the
652/// `inject_config_preloads_{lua_rejected,unknown_extension}` unit tests to
653/// verify the error messages in isolation.
654#[cfg(test)]
655fn inject_config_preloads(lua: &Lua, config_path: &str) -> Result<(), String> {
656    let ext = std::path::Path::new(config_path)
657        .extension()
658        .and_then(|e| e.to_str())
659        .unwrap_or("");
660
661    let preload = preload_table(lua)?;
662
663    if ext.eq_ignore_ascii_case("lua") {
664        Err("gendoc: config_path extension '.lua' is no longer supported; use .toml".to_string())
665    } else if ext.eq_ignore_ascii_case("toml") {
666        inject_config_preloads_toml(lua, &preload, config_path)
667    } else {
668        Err(format!(
669            "gendoc: config_path '{config_path}' unsupported extension (expected .toml)"
670        ))
671    }
672}
673
674fn inject_config_preloads_toml(
675    lua: &Lua,
676    preload: &Table,
677    config_path: &str,
678) -> Result<(), String> {
679    let src = std::fs::read_to_string(config_path)
680        .map_err(|e| format!("gendoc: config_path '{config_path}' load failed: {e}"))?;
681    let config: FlatGendocConfig = toml::from_str(&src)
682        .map_err(|e| format!("gendoc: config_path '{config_path}' parse failed: {e}"))?;
683
684    // Move the two sub-tables into the Lua registry via globals
685    // stashes so the preload closures can retrieve them on require.
686    // Using globals keeps the lifetime story simple (mlua 0.11 does
687    // not require `'static` bounds for tables stored this way).
688    inject_config_subtable(
689        lua,
690        preload,
691        config.context7,
692        "context7",
693        "_gendoc_context7_config",
694        "tools.docs.context7_config",
695    )?;
696    inject_config_subtable(
697        lua,
698        preload,
699        config.devin,
700        "devin",
701        "_gendoc_devin_config",
702        "tools.docs.devin_wiki_config",
703    )?;
704
705    Ok(())
706}
707
708/// Stash the `key` sub-table into a Lua global
709/// and register a `package.preload` loader that returns it.
710///
711/// - Missing key (`None`) is a legitimate caller choice: the
712///   preload entry is simply omitted so a downstream `require` of
713///   `module_name` raises Lua's standard "module not found" error
714///   (clearer than registering a Nil loader that produces an opaque
715///   nil-index error).
716/// - Non-table values are rejected up front with an
717///   explicit `Err` — far more actionable than letting the Lua side
718///   try to index a string/number later.
719fn inject_config_subtable(
720    lua: &Lua,
721    preload: &Table,
722    value: Option<toml::Value>,
723    key: &'static str,
724    global_key: &'static str,
725    module_name: &'static str,
726) -> Result<(), String> {
727    match value {
728        None => Ok(()),
729        Some(v) => {
730            let lua_value = toml_to_lua_value(lua, &v)
731                .map_err(|e| format!("gendoc: config '{key}' conversion failed: {e}"))?;
732            match lua_value {
733                Value::Table(_) => {
734                    lua.globals()
735                        .set(global_key, lua_value)
736                        .map_err(|e| format!("gendoc: stash {global_key} failed: {e}"))?;
737                    register_config_loader(lua, preload, module_name, global_key)
738                }
739                other => Err(format!(
740                    "gendoc: config '{key}' must be a table, got {}",
741                    other.type_name()
742                )),
743            }
744        }
745    }
746}
747
748/// Flat TOML config file shape for backward-compat `config_path=*.toml` callers.
749///
750/// Consumers using the old flat `[context7]` / `[devin]` schema continue to
751/// work unchanged.  The new `alc.toml [hub.*]` path uses
752/// `load_hub_projection_config` instead.
753#[derive(Debug, Deserialize)]
754struct FlatGendocConfig {
755    context7: Option<toml::Value>,
756    devin: Option<toml::Value>,
757}
758
759fn toml_to_lua_value(lua: &Lua, value: &toml::Value) -> Result<Value, String> {
760    match value {
761        toml::Value::String(s) => Ok(Value::String(
762            lua.create_string(s)
763                .map_err(|e| format!("create string failed: {e}"))?,
764        )),
765        toml::Value::Integer(i) => Ok(Value::Integer(*i)),
766        toml::Value::Float(f) => Ok(Value::Number(*f)),
767        toml::Value::Boolean(b) => Ok(Value::Boolean(*b)),
768        toml::Value::Datetime(dt) => Ok(Value::String(
769            lua.create_string(dt.to_string())
770                .map_err(|e| format!("create datetime string failed: {e}"))?,
771        )),
772        toml::Value::Array(arr) => {
773            let table = lua
774                .create_table()
775                .map_err(|e| format!("create array table failed: {e}"))?;
776            for (idx, item) in arr.iter().enumerate() {
777                let v = toml_to_lua_value(lua, item)?;
778                table
779                    .set((idx + 1) as i64, v)
780                    .map_err(|e| format!("set array item [{idx}] failed: {e}"))?;
781            }
782            Ok(Value::Table(table))
783        }
784        toml::Value::Table(map) => {
785            let table = lua
786                .create_table()
787                .map_err(|e| format!("create map table failed: {e}"))?;
788            for (k, v) in map {
789                let vv = toml_to_lua_value(lua, v)?;
790                table
791                    .set(k.as_str(), vv)
792                    .map_err(|e| format!("set map key '{k}' failed: {e}"))?;
793            }
794            Ok(Value::Table(table))
795        }
796    }
797}
798
799fn register_config_loader(
800    lua: &Lua,
801    preload: &Table,
802    module_name: &'static str,
803    global_key: &'static str,
804) -> Result<(), String> {
805    let loader = lua
806        .create_function(move |lua, ()| lua.globals().get::<Value>(global_key))
807        .map_err(|e| format!("gendoc: config loader for {module_name} failed: {e}"))?;
808    preload
809        .set(module_name, loader)
810        .map_err(|e| format!("gendoc: preload.set failed for {module_name}: {e}"))?;
811    Ok(())
812}
813
814fn install_io_hooks(
815    lua: &Lua,
816    out_buf: Arc<Mutex<String>>,
817    err_buf: Arc<Mutex<String>>,
818) -> Result<(), String> {
819    let out_for_closure = Arc::clone(&out_buf);
820    let append_out = lua
821        .create_function(move |_, s: String| {
822            out_for_closure
823                .lock()
824                .map_err(|e| mlua::Error::external(format!("gendoc: out buf lock: {e}")))?
825                .push_str(&s);
826            Ok(())
827        })
828        .map_err(|e| format!("gendoc: create_function _gendoc_out_append: {e}"))?;
829
830    let err_for_closure = Arc::clone(&err_buf);
831    let append_err = lua
832        .create_function(move |_, s: String| {
833            err_for_closure
834                .lock()
835                .map_err(|e| mlua::Error::external(format!("gendoc: err buf lock: {e}")))?
836                .push_str(&s);
837            Ok(())
838        })
839        .map_err(|e| format!("gendoc: create_function _gendoc_err_append: {e}"))?;
840
841    lua.globals()
842        .set("_gendoc_out_append", append_out)
843        .map_err(|e| format!("gendoc: globals set _gendoc_out_append: {e}"))?;
844    lua.globals()
845        .set("_gendoc_err_append", append_err)
846        .map_err(|e| format!("gendoc: globals set _gendoc_err_append: {e}"))?;
847
848    Ok(())
849}
850
851fn install_argv(
852    lua: &Lua,
853    source_dir: &str,
854    out_dir: &str,
855    flags: &ProjectionFlags,
856    lint_strict: bool,
857) -> Result<(), String> {
858    let argv = lua
859        .create_table()
860        .map_err(|e| format!("gendoc: create argv table: {e}"))?;
861
862    let mut idx: i64 = 1;
863    let mut push = |v: &str| -> Result<(), String> {
864        argv.set(idx, v)
865            .map_err(|e| format!("gendoc: argv set [{idx}]: {e}"))?;
866        idx += 1;
867        Ok(())
868    };
869
870    push(source_dir)?;
871    push(out_dir)?;
872    if flags.hub {
873        push("--hub")?;
874    }
875    if flags.context7 {
876        push("--context7")?;
877    }
878    if flags.devin {
879        push("--devin")?;
880    }
881    if flags.lint_only {
882        push("--lint-only")?;
883    } else if flags.lint {
884        push("--lint")?;
885    }
886    if lint_strict {
887        push("--strict")?;
888    }
889    if flags.luacats {
890        push("--luacats")?;
891    }
892
893    lua.globals()
894        .set("arg", argv)
895        .map_err(|e| format!("gendoc: globals set arg: {e}"))?;
896
897    Ok(())
898}
899
900/// Strip a leading `#!` shebang line from a Lua source.
901///
902/// `lua.load()` (buffer-based) does not strip the shebang the way
903/// `luaL_loadfile` does. The embedded `gen_docs.lua` starts with
904/// `#!/usr/bin/env lua`, so we strip the first line before feeding
905/// the buffer to the VM.
906fn strip_shebang(src: &str) -> &str {
907    if let Some(body) = src.strip_prefix("#!") {
908        match body.find('\n') {
909            Some(i) => &body[i + 1..],
910            None => "",
911        }
912    } else {
913        src
914    }
915}
916
917fn read_buf(buf: &Arc<Mutex<String>>) -> Result<String, String> {
918    Ok(buf
919        .lock()
920        .map_err(|e| format!("gendoc: buffer lock (read): {e}"))?
921        .clone())
922}
923
924/// Extract `__gendoc_exit` from a Lua error raised by the `os.exit`
925/// override. Returns `None` for unrelated errors.
926fn extract_exit_code(err: &mlua::Error) -> Option<i64> {
927    // `error(tbl, 0)` in Lua becomes `mlua::Error::RuntimeError`
928    // where the tostring serialization contains the table pointer
929    // (not useful) plus any __tostring metamethod output. For our
930    // structured exit we don't set __tostring, so the surfaced
931    // message may look like `table: 0x...`. That is not reliable.
932    //
933    // Instead, walk the error chain looking for a CallbackError /
934    // WithContext that wraps the raw value. mlua exposes the raw
935    // table via `Error::CallbackError::cause`. The Lua table itself
936    // is not exposed from `RuntimeError`, so we fall back to
937    // pattern-matching the string form as a best-effort:
938    // Lua's `error({__gendoc_exit = N}, 0)` with a table whose
939    // `__tostring` is unset renders as the table pointer, which
940    // loses the code.
941    //
942    // To make this robust we attach a __tostring metamethod to the
943    // raised table in the hook script so the error message embeds
944    // the code. See HOOK_SCRIPT.
945    let msg = err.to_string();
946    // Look for the marker substring emitted by __tostring (installed
947    // in HOOK_SCRIPT) in the format `__gendoc_exit=<N>`.
948    let needle = EXIT_MARKER;
949    let idx = msg.find(needle)?;
950    let rest = &msg[idx + needle.len()..];
951    // Skip any `=` / `:` / whitespace, then parse an integer.
952    let digits_start = rest
953        .char_indices()
954        .find(|(_, c)| c.is_ascii_digit() || *c == '-')
955        .map(|(i, _)| i)?;
956    let tail = &rest[digits_start..];
957    let digits_end = tail
958        .char_indices()
959        .find(|(_, c)| !c.is_ascii_digit() && *c != '-')
960        .map(|(i, _)| i)
961        .unwrap_or(tail.len());
962    tail[..digits_end].parse::<i64>().ok()
963}
964
965fn build_response_json(
966    source_dir: &str,
967    out_dir: &str,
968    stdout_txt: &str,
969    stderr_txt: &str,
970    warnings: &[String],
971) -> String {
972    // Keep dependency-free — we already depend on serde_json
973    // transitively but using it here avoids hand-rolled escaping
974    // bugs. Every shipped string is a plain `String` so
975    // `serde_json::Value::String` is fine.
976    let value = serde_json::json!({
977        "source_dir": source_dir,
978        "out_dir": out_dir,
979        "stdout": stdout_txt,
980        "stderr": stderr_txt,
981        "warnings": warnings,
982    });
983    value.to_string()
984}
985
986#[cfg(test)]
987mod tests {
988    use super::*;
989
990    #[test]
991    fn projection_flags_defaults_are_false() {
992        let f = ProjectionFlags::from_list(None).expect("projection parse");
993        assert!(!f.hub);
994        assert!(!f.context7);
995        assert!(!f.devin);
996        assert!(!f.lint);
997        assert!(!f.lint_only);
998    }
999
1000    #[test]
1001    fn projection_flags_parse_known_tokens() {
1002        let list = vec![
1003            "hub".to_string(),
1004            "context7".to_string(),
1005            "devin".to_string(),
1006        ];
1007        let f = ProjectionFlags::from_list(Some(&list)).expect("projection parse");
1008        assert!(f.hub);
1009        assert!(f.context7);
1010        assert!(f.devin);
1011        assert!(!f.lint);
1012    }
1013
1014    #[test]
1015    fn projection_flags_lint_only_implies_lint() {
1016        let list = vec!["lint_only".to_string()];
1017        let f = ProjectionFlags::from_list(Some(&list)).expect("projection parse");
1018        assert!(f.lint);
1019        assert!(f.lint_only);
1020    }
1021
1022    #[test]
1023    fn projection_flags_luacats_parses() {
1024        let list = vec!["luacats".to_string()];
1025        let f = ProjectionFlags::from_list(Some(&list)).expect("projection parse");
1026        assert!(f.luacats);
1027        assert!(!f.hub);
1028        assert!(!f.lint);
1029    }
1030
1031    #[test]
1032    fn projection_flags_unknown_is_rejected() {
1033        let list = vec!["nope".to_string(), "hub".to_string()];
1034        let err = ProjectionFlags::from_list(Some(&list)).expect_err("must reject unknown");
1035        assert!(err.contains("unknown projection"));
1036    }
1037
1038    #[test]
1039    fn projection_flags_narrative_and_llms_parse() {
1040        // narrative and llms are accepted as valid projections.
1041        // On the gen_docs.lua side these are unconditionally emitted when
1042        // lint_only=false, so the Rust flags act only as an allowlist gate
1043        // (approach A: no argv is pushed to gen_docs.lua for these).
1044        let list = vec!["narrative".to_string(), "llms".to_string()];
1045        let f = ProjectionFlags::from_list(Some(&list)).expect("projection parse");
1046        assert!(f.narrative, "narrative flag must be set");
1047        assert!(f.llms, "llms flag must be set");
1048        assert!(!f.hub, "hub must remain false");
1049        assert!(!f.lint, "lint must remain false");
1050    }
1051
1052    #[test]
1053    fn context7_without_config_is_rejected() {
1054        // Build a minimal AppService through the public API is
1055        // expensive; exercise the input validation logic through
1056        // `ProjectionFlags` + an explicit mirror of the early
1057        // return in `hub_gendoc`. Directly calling `hub_gendoc`
1058        // would require a full test fixture — covered in e2e.
1059        let list = vec!["context7".to_string()];
1060        let flags = ProjectionFlags::from_list(Some(&list)).expect("projection parse");
1061        assert!(flags.context7);
1062        // Simulate the guard:
1063        let err_expected =
1064            "gendoc: config_path is required when projections include context7 or devin";
1065        let err = if (flags.context7 || flags.devin) && Option::<&str>::None.is_none() {
1066            Some(err_expected.to_string())
1067        } else {
1068            None
1069        };
1070        assert_eq!(err.as_deref(), Some(err_expected));
1071    }
1072
1073    #[test]
1074    fn extract_exit_code_parses_marker_formats() {
1075        // Simulated error string; `extract_exit_code` doesn't care
1076        // about the prefix as long as the `__gendoc_exit=N` marker is
1077        // present.
1078        let err = mlua::Error::RuntimeError(
1079            "runtime error: [string \"...\"]:2: {__gendoc_exit=2}".to_string(),
1080        );
1081        assert_eq!(extract_exit_code(&err), Some(2));
1082
1083        let err = mlua::Error::RuntimeError("runtime error: __gendoc_exit: 0 (clean)".to_string());
1084        assert_eq!(extract_exit_code(&err), Some(0));
1085    }
1086
1087    #[test]
1088    fn extract_exit_code_returns_none_for_unrelated_errors() {
1089        let err = mlua::Error::RuntimeError("some other Lua error".to_string());
1090        assert!(extract_exit_code(&err).is_none());
1091    }
1092
1093    #[test]
1094    fn strip_shebang_removes_first_line_when_prefixed() {
1095        let src = "#!/usr/bin/env lua\nreturn 1\n";
1096        assert_eq!(strip_shebang(src), "return 1\n");
1097    }
1098
1099    #[test]
1100    fn strip_shebang_preserves_source_without_shebang() {
1101        let src = "-- no shebang\nreturn 1\n";
1102        assert_eq!(strip_shebang(src), src);
1103    }
1104
1105    #[test]
1106    fn strip_shebang_handles_shebang_only_without_trailing_newline() {
1107        let src = "#!/usr/bin/env lua";
1108        assert_eq!(strip_shebang(src), "");
1109    }
1110
1111    #[test]
1112    fn build_response_json_round_trips() {
1113        let out = build_response_json("/src", "/src/docs", "hi", "warn", &[]);
1114        let parsed: serde_json::Value = serde_json::from_str(&out).unwrap();
1115        assert_eq!(parsed["source_dir"], "/src");
1116        assert_eq!(parsed["out_dir"], "/src/docs");
1117        assert_eq!(parsed["stdout"], "hi");
1118        assert_eq!(parsed["stderr"], "warn");
1119        assert_eq!(parsed["warnings"], serde_json::json!([]));
1120    }
1121
1122    #[test]
1123    fn build_response_json_includes_warnings() {
1124        let warnings = vec![
1125            "pkg foo: alc_shapes_compat not declared, continuing with current alc_shapes@0.25.1"
1126                .to_string(),
1127        ];
1128        let out = build_response_json("/src", "/src/docs", "", "", &warnings);
1129        let parsed: serde_json::Value = serde_json::from_str(&out).unwrap();
1130        assert_eq!(parsed["warnings"][0], warnings[0].as_str());
1131    }
1132
1133    // ── alc_shapes version resolver unit tests ────────────────────────
1134
1135    #[test]
1136    fn extract_m_version_parses_standard_format() {
1137        let src = r#"local M = {}
1138M.VERSION = "0.25.1"
1139"#;
1140        assert_eq!(extract_m_version(src).as_deref(), Some("0.25.1"));
1141    }
1142
1143    #[test]
1144    fn extract_m_version_tolerates_no_space_around_eq() {
1145        let src = r#"M.VERSION="1.2.3""#;
1146        assert_eq!(extract_m_version(src).as_deref(), Some("1.2.3"));
1147    }
1148
1149    #[test]
1150    fn extract_m_version_tolerates_leading_whitespace() {
1151        let src = r#"  M.VERSION = "9.9.9"  "#;
1152        assert_eq!(extract_m_version(src).as_deref(), Some("9.9.9"));
1153    }
1154
1155    #[test]
1156    fn extract_m_version_returns_none_when_absent() {
1157        let src = r#"local M = {}
1158return M
1159"#;
1160        assert!(extract_m_version(src).is_none());
1161    }
1162
1163    #[test]
1164    fn check_mirror_shapes_version_ok_when_source_dir_none() {
1165        assert!(check_mirror_shapes_version(None).is_ok());
1166    }
1167
1168    #[test]
1169    fn check_mirror_shapes_version_ok_when_no_mirror_file() {
1170        // A tempdir with no alc_shapes/ subdirectory.
1171        let tmp = tempfile::tempdir().expect("tempdir");
1172        let dir = tmp.path().to_str().expect("utf-8").to_string();
1173        assert!(check_mirror_shapes_version(Some(&dir)).is_ok());
1174    }
1175
1176    #[test]
1177    fn check_mirror_shapes_version_ok_on_version_match() {
1178        let tmp = tempfile::tempdir().expect("tempdir");
1179        let alc_dir = tmp.path().join("alc_shapes");
1180        std::fs::create_dir_all(&alc_dir).expect("mkdir alc_shapes");
1181        let init = alc_dir.join("init.lua");
1182        std::fs::write(
1183            &init,
1184            format!(
1185                "local M = {{}}\nM.VERSION = \"{}\"\nreturn M\n",
1186                EMBEDDED_ALC_SHAPES_VERSION
1187            ),
1188        )
1189        .expect("write init.lua");
1190        let dir = tmp.path().to_str().expect("utf-8").to_string();
1191        assert!(check_mirror_shapes_version(Some(&dir)).is_ok());
1192    }
1193
1194    #[test]
1195    fn check_mirror_shapes_version_err_on_version_mismatch() {
1196        let tmp = tempfile::tempdir().expect("tempdir");
1197        let alc_dir = tmp.path().join("alc_shapes");
1198        std::fs::create_dir_all(&alc_dir).expect("mkdir alc_shapes");
1199        let init = alc_dir.join("init.lua");
1200        std::fs::write(&init, "local M = {}\nM.VERSION = \"9.9.9\"\nreturn M\n")
1201            .expect("write init.lua");
1202        let dir = tmp.path().to_str().expect("utf-8").to_string();
1203        let err =
1204            check_mirror_shapes_version(Some(&dir)).expect_err("must fail on version mismatch");
1205        let msg = err.to_string();
1206        assert!(
1207            msg.contains(EMBEDDED_ALC_SHAPES_VERSION),
1208            "embedded ver in msg: {msg}"
1209        );
1210        assert!(msg.contains("9.9.9"), "mirror ver in msg: {msg}");
1211        assert!(msg.contains("CHANGELOG"), "hint in msg: {msg}");
1212    }
1213
1214    #[test]
1215    fn check_mirror_shapes_version_err_on_malformed() {
1216        let tmp = tempfile::tempdir().expect("tempdir");
1217        let alc_dir = tmp.path().join("alc_shapes");
1218        std::fs::create_dir_all(&alc_dir).expect("mkdir alc_shapes");
1219        let init = alc_dir.join("init.lua");
1220        std::fs::write(&init, "-- no version here\nreturn {}\n").expect("write init.lua");
1221        let dir = tmp.path().to_str().expect("utf-8").to_string();
1222        let err = check_mirror_shapes_version(Some(&dir)).expect_err("must fail on malformed");
1223        let msg = err.to_string();
1224        assert!(msg.contains("no parseable"), "malformed msg: {msg}");
1225    }
1226
1227    /// Regression harness: vendored `alc_shapes` must satisfy the
1228    /// contracts exercised by `tools/docs/projections.lua` (sorted
1229    /// `S.fields`, `prim` / `elem` / `val` / `doc` keys). Catches the
1230    /// class of drift that collapsed bundled `llms-full.txt` generation.
1231    #[test]
1232    fn embedded_gendoc_shapes_contract_harness() {
1233        let lua = Lua::new();
1234        register_preloads(&lua).expect("register_preloads");
1235
1236        let script = r#"
1237            local S = require("alc_shapes")
1238            local T = require("alc_shapes.t")
1239            local P = require("tools.docs.projections")
1240
1241            local shape = T.shape({
1242                task = T.string:describe("Problem"),
1243                n = T.number:is_optional(),
1244            })
1245            local entries = S.fields(shape)
1246            assert(#entries == 2, "expected two fields")
1247            assert(entries[1].name == "n" and entries[1].optional == true)
1248            assert(entries[2].name == "task" and entries[2].optional == false)
1249            assert(entries[2].doc == "Problem")
1250            assert(P.shape_type_string(entries[2].type) == "string")
1251
1252            assert(P.shape_type_string(T.array_of(T.string)) == "array of string")
1253            assert(P.shape_type_string(T.map_of(T.string, T.number)) == "map of string to number")
1254
1255            local inner = T.shape({ flag = T.boolean })
1256            assert(P.shape_type_string(inner) == "shape { flag: boolean }")
1257        "#;
1258
1259        lua.load(script)
1260            .set_name("@test/embedded_gendoc_shapes_contract.lua")
1261            .exec()
1262            .expect("embedded shapes contract harness");
1263    }
1264
1265    /// Vendored `alc_shapes` must include the full shape registry so
1266    /// `projections.shape_type_string(T.ref("voted"))` resolves via the
1267    /// embedded `alc_shapes` module (no disk fallback required).
1268    #[test]
1269    fn vendored_alc_shapes_resolves_pkg_refs() {
1270        let lua = Lua::new();
1271        register_preloads(&lua).expect("register_preloads");
1272
1273        let script = r#"
1274            local S = require("alc_shapes")
1275            assert(type(S.voted) == "table" and rawget(S.voted, "kind") == "shape")
1276            local T = require("alc_shapes.t")
1277            local P = require("tools.docs.projections")
1278            assert(P.shape_type_string(T.ref("voted")) == "voted")
1279        "#;
1280
1281        lua.load(script)
1282            .set_name("@test/vendored_alc_shapes_ref.lua")
1283            .exec()
1284            .expect("vendored alc_shapes ref resolution");
1285    }
1286
1287    // ── alc_shapes_compat extraction / dispatcher unit tests ──────────
1288
1289    #[test]
1290    fn extract_quoted_value_finds_marker() {
1291        let src = r#"M.meta.alc_shapes_compat = ">=0.25.0, <0.26""#;
1292        assert_eq!(
1293            extract_quoted_value(src, "alc_shapes_compat"),
1294            Some(">=0.25.0, <0.26")
1295        );
1296    }
1297
1298    #[test]
1299    fn extract_quoted_value_returns_none_when_absent() {
1300        let src = "local M = {}\nreturn M\n";
1301        assert!(extract_quoted_value(src, "alc_shapes_compat").is_none());
1302    }
1303
1304    #[test]
1305    fn extract_m_meta_compat_finds_field() {
1306        let src = r#"M.meta = { alc_shapes_compat = ">=0.25.0, <0.26", name = "pkg" }"#;
1307        assert_eq!(extract_m_meta_compat(src), Some(">=0.25.0, <0.26"));
1308    }
1309
1310    #[test]
1311    fn extract_m_meta_compat_returns_none_when_absent() {
1312        let src = r#"M.meta = { name = "pkg", version = "0.1.0" }"#;
1313        assert!(extract_m_meta_compat(src).is_none());
1314    }
1315
1316    #[test]
1317    fn check_pkg_compat_warns_on_undeclared() {
1318        let tmp = tempfile::tempdir().expect("tempdir");
1319        let pkg_dir = tmp.path().join("pkg_foo");
1320        std::fs::create_dir_all(&pkg_dir).expect("mkdir pkg_foo");
1321        std::fs::write(
1322            pkg_dir.join("init.lua"),
1323            "local M = {}\nM.meta = { name = 'pkg_foo' }\nreturn M\n",
1324        )
1325        .expect("write init.lua");
1326
1327        let result = check_pkg_compat(tmp.path().to_str().expect("utf-8")).expect("no error");
1328        assert_eq!(result.len(), 1);
1329        assert!(
1330            result[0].contains("alc_shapes_compat not declared"),
1331            "expected undeclared warning, got: {}",
1332            result[0]
1333        );
1334    }
1335
1336    #[test]
1337    fn check_pkg_compat_ok_on_in_range() {
1338        let tmp = tempfile::tempdir().expect("tempdir");
1339        let pkg_dir = tmp.path().join("pkg_bar");
1340        std::fs::create_dir_all(&pkg_dir).expect("mkdir pkg_bar");
1341        // 0.25.1 is in >=0.25.0, <0.26 — note: double-quoted Lua strings
1342        std::fs::write(
1343            pkg_dir.join("init.lua"),
1344            "local M = {}\nM.meta = { name = \"pkg_bar\", alc_shapes_compat = \">=0.25.0, <0.26\" }\nreturn M\n",
1345        )
1346        .expect("write init.lua");
1347
1348        let result = check_pkg_compat(tmp.path().to_str().expect("utf-8")).expect("no error");
1349        assert!(result.is_empty(), "expected no warnings for in-range pkg");
1350    }
1351
1352    #[test]
1353    fn check_pkg_compat_err_on_out_of_range() {
1354        let tmp = tempfile::tempdir().expect("tempdir");
1355        let pkg_dir = tmp.path().join("pkg_baz");
1356        std::fs::create_dir_all(&pkg_dir).expect("mkdir pkg_baz");
1357        // 0.25.1 is NOT in >=0.26.0, <0.27
1358        std::fs::write(
1359            pkg_dir.join("init.lua"),
1360            "local M = {}\nM.meta = { name = \"pkg_baz\", alc_shapes_compat = \">=0.26.0, <0.27\" }\nreturn M\n",
1361        )
1362        .expect("write init.lua");
1363
1364        let err = check_pkg_compat(tmp.path().to_str().expect("utf-8"))
1365            .expect_err("must fail on out-of-range");
1366        let msg = err.to_string();
1367        assert!(msg.contains("pkg_baz"), "pkg_name in error: {msg}");
1368        assert!(msg.contains(">=0.26.0, <0.27"), "range in error: {msg}");
1369        assert!(
1370            msg.contains(EMBEDDED_ALC_SHAPES_VERSION),
1371            "version in error: {msg}"
1372        );
1373        assert!(
1374            msg.contains("ShapesCompatViolation") || msg.contains("does not match"),
1375            "violation in error: {msg}"
1376        );
1377    }
1378
1379    // ── inject_config_preloads unit tests ────────────────────────────
1380
1381    #[test]
1382    fn inject_config_preloads_lua_rejected() {
1383        let tmp = tempfile::tempdir().expect("tempdir");
1384        let cfg = tmp.path().join("config.lua");
1385        std::fs::write(&cfg, "return {}").expect("write config.lua");
1386
1387        let lua = Lua::new();
1388        let err = inject_config_preloads(&lua, cfg.to_str().expect("utf-8"))
1389            .expect_err("must fail for .lua extension");
1390        assert!(
1391            err.contains("'.lua' is no longer supported"),
1392            "expected '.lua' is no longer supported in: {err}"
1393        );
1394    }
1395
1396    #[test]
1397    fn inject_config_preloads_unknown_extension() {
1398        let tmp = tempfile::tempdir().expect("tempdir");
1399        let cfg = tmp.path().join("config.yaml");
1400        std::fs::write(&cfg, "context7:\n  projectTitle: x\n").expect("write config.yaml");
1401
1402        let lua = Lua::new();
1403        let err = inject_config_preloads(&lua, cfg.to_str().expect("utf-8"))
1404            .expect_err("must fail on unknown extension");
1405        assert!(
1406            err.contains("unsupported extension (expected .toml)"),
1407            "expected 'unsupported extension (expected .toml)' in: {err}"
1408        );
1409    }
1410
1411    #[test]
1412    fn check_pkg_compat_err_on_malformed_range() {
1413        let tmp = tempfile::tempdir().expect("tempdir");
1414        let pkg_dir = tmp.path().join("pkg_qux");
1415        std::fs::create_dir_all(&pkg_dir).expect("mkdir pkg_qux");
1416        std::fs::write(
1417            pkg_dir.join("init.lua"),
1418            "local M = {}\nM.meta = { name = \"pkg_qux\", alc_shapes_compat = \"not a semver range\" }\nreturn M\n",
1419        )
1420        .expect("write init.lua");
1421
1422        let err = check_pkg_compat(tmp.path().to_str().expect("utf-8"))
1423            .expect_err("must fail on malformed range");
1424        let msg = err.to_string();
1425        assert!(msg.contains("pkg_qux"), "pkg_name in error: {msg}");
1426        assert!(msg.contains("not a semver range"), "value in error: {msg}");
1427        assert!(
1428            msg.contains("Malformed") || msg.contains("valid semver"),
1429            "malformed label in error: {msg}"
1430        );
1431    }
1432}