Skip to main content

algocline_app/service/
run.rs

1use std::collections::HashMap;
2use std::sync::Arc;
3
4use algocline_core::pkg::{PkgType, TypeSource};
5use algocline_core::QueryId;
6use algocline_engine::{FeedResult, VariantPkg};
7
8use super::alc_toml::load_alc_toml;
9use super::eval_store::{splice_response_string, splice_response_warnings};
10use super::resolve::{is_package_installed, make_require_code, resolve_code, QueryResponse};
11use super::transcript::write_transcript_log;
12use super::AppService;
13use crate::pool::dispatch::{continue_via_pool, run_via_pool};
14
15/// Recover from MCP clients that JSON-stringify an object-typed field
16/// before sending. The MCP `inputSchema` for `ctx` / `opts` /
17/// `strategy_opts` declares `type: object`, but some clients send a
18/// JSON-encoded string instead. Without this normalisation the value
19/// reaches Lua as a string and breaks pkgs that require a table
20/// (issue 1778656404-63015).
21///
22/// Only `Value::String` payloads that parse into a JSON object or
23/// array are replaced; ordinary strings and primitive scalars pass
24/// through untouched.
25pub(crate) fn normalize_stringified_json_object(v: serde_json::Value) -> serde_json::Value {
26    match v {
27        serde_json::Value::String(ref s) => match serde_json::from_str::<serde_json::Value>(s) {
28            Ok(parsed @ serde_json::Value::Object(_)) => parsed,
29            Ok(parsed @ serde_json::Value::Array(_)) => parsed,
30            _ => v,
31        },
32        other => other,
33    }
34}
35
36/// Splice `save_warning` into the JSON `result` when the optional
37/// warning is `Some(_)`. Returns the original string unchanged when
38/// there is no warning.
39fn splice_save_warning(result_json: &str, warning: Option<String>) -> String {
40    match warning {
41        Some(msg) => splice_response_string(result_json, "save_warning", &msg),
42        None => result_json.to_string(),
43    }
44}
45
46/// Splice `transcript_warning` into the JSON `result` when the optional
47/// warning is `Some(_)`. Returns the original string unchanged when
48/// there is no warning.
49fn splice_transcript_warning(result_json: &str, warning: Option<String>) -> String {
50    match warning {
51        Some(msg) => splice_response_string(result_json, "transcript_warning", &msg),
52        None => result_json.to_string(),
53    }
54}
55
56/// Build a frozen env snapshot from up to three sources and an optional allowlist.
57///
58/// # Sources (applied in priority order, lower overwritten by higher)
59///
60/// 1. **OS environment** (`ctx.env.allow_os = true` only) — `std::env::vars()` snapshot.
61/// 2. **dotenv file** (`ctx.env.dotenv`) — parsed via `dotenvy`; keys overwrite OS layer.
62/// 3. **inject** (`ctx.env.inject`) — explicit key/value map; highest priority, overwrites all.
63///
64/// # Arguments
65///
66/// - `ctx` — the full `alc_run` context value; `ctx["env"]` is extracted here.
67/// - `project_root` — required when `ctx.env.dotenv` is a relative path.
68///   `None` with a relative dotenv path is an error (avoids CWD ambiguity).
69/// - `alc_toml_allow` — optional allowlist from `alc.toml [env].allow`.
70///   When `Some`, the merged map is filtered to only keys present in the list.
71///   `None` means no filtering (all resolved keys are kept).
72///
73/// # Errors
74///
75/// Returns `Err(String)` for:
76/// - `ctx.env.inject` values that are not JSON strings
77/// - `ctx.env.dotenv` is a relative path but `project_root` is `None`
78/// - `dotenvy` I/O error opening the dotenv file
79/// - `dotenvy` parse error for any entry in the dotenv file
80pub(super) fn resolve_env(
81    ctx: &serde_json::Value,
82    project_root: Option<&std::path::Path>,
83    alc_toml_allow: Option<&[String]>,
84) -> Result<Arc<HashMap<String, String>>, String> {
85    let env_obj = ctx.get("env").and_then(|v| v.as_object());
86
87    // ── Layer 3 (highest): inject ──────────────────────────────────────────────
88    let inject: HashMap<String, String> = if let Some(obj) = env_obj {
89        if let Some(inject_val) = obj.get("inject") {
90            match inject_val.as_object() {
91                Some(m) => {
92                    let mut map = HashMap::new();
93                    for (k, v) in m {
94                        match v.as_str() {
95                            Some(s) => {
96                                map.insert(k.clone(), s.to_string());
97                            }
98                            None => {
99                                return Err(format!(
100                                    "ctx.env.inject: value for key '{k}' must be a string, got {v}"
101                                ));
102                            }
103                        }
104                    }
105                    map
106                }
107                None => {
108                    return Err(format!(
109                        "ctx.env.inject must be an object, got {}",
110                        inject_val
111                    ));
112                }
113            }
114        } else {
115            HashMap::new()
116        }
117    } else {
118        HashMap::new()
119    };
120
121    // Resolved dotenv path (if any)
122    let dotenv_path: Option<std::path::PathBuf> = if let Some(p) = env_obj
123        .and_then(|o| o.get("dotenv"))
124        .and_then(|v| v.as_str())
125    {
126        let path = std::path::Path::new(p);
127        if path.is_absolute() {
128            Some(path.to_path_buf())
129        } else {
130            match project_root {
131                Some(root) => Some(root.join(p)),
132                None => {
133                    return Err(format!(
134                        "ctx.env.dotenv: relative path '{p}' requires project_root to be set"
135                    ));
136                }
137            }
138        }
139    } else {
140        None
141    };
142
143    let allow_os = env_obj
144        .and_then(|o| o.get("allow_os"))
145        .and_then(|v| v.as_bool())
146        .unwrap_or(false);
147
148    let mut merged: HashMap<String, String> = HashMap::new();
149
150    // ── Layer 1 (lowest): OS environment ──────────────────────────────────────
151    if allow_os {
152        for (k, v) in std::env::vars() {
153            merged.insert(k, v);
154        }
155    }
156
157    // ── Layer 2: dotenv file ───────────────────────────────────────────────────
158    if let Some(ref full) = dotenv_path {
159        let iter = dotenvy::from_path_iter(full)
160            .map_err(|e| format!("ctx.env.dotenv: failed to open '{}': {e}", full.display()))?;
161        for item in iter {
162            let (k, v) = item
163                .map_err(|e| format!("ctx.env.dotenv: parse error in '{}': {e}", full.display()))?;
164            merged.insert(k, v);
165        }
166    }
167
168    // ── Layer 3: inject (overwrite, highest priority) ──────────────────────────
169    for (k, v) in inject {
170        merged.insert(k, v);
171    }
172
173    // ── Optional allowlist filter (alc.toml [env].allow) ──────────────────────
174    if let Some(allow) = alc_toml_allow {
175        if !allow.is_empty() {
176            let allowset: std::collections::HashSet<&String> = allow.iter().collect();
177            merged.retain(|k, _| allowset.contains(k));
178        }
179    }
180
181    Ok(Arc::new(merged))
182}
183
184impl AppService {
185    /// Execute Lua code with optional JSON context.
186    ///
187    /// When `host_mode: Some(true)` is passed, the call is proxied via
188    /// `PoolClient` to a long-lived worker subprocess over a Unix domain socket.
189    /// When `host_mode` is `None` or `Some(false)` the existing in-process
190    /// `Executor::start_session` path is used unchanged.
191    ///
192    /// # Concurrency
193    ///
194    /// **host_mode=false (default)**: No additional locking beyond `SessionRegistry`
195    /// lock C. `AppService` itself holds no long-lived lock during this call.
196    ///
197    /// **host_mode=true**: Acquires `RwLock<PoolRegistry>` (write) and advisory
198    /// `fs4::FileExt::lock_exclusive` to update `registry.json`. These locks are
199    /// **not** held across the UDS round-trip await.
200    ///
201    /// **Cancel safety**: cancelling this `.await` mid-UDS-request leaves the
202    /// worker subprocess running. The registry entry persists; callers can
203    /// reconnect via `alc_continue` after MCP restart.
204    pub async fn run(
205        &self,
206        code: Option<String>,
207        code_file: Option<String>,
208        ctx: Option<serde_json::Value>,
209        project_root: Option<String>,
210        host_mode: Option<bool>,
211    ) -> Result<String, String> {
212        let code = resolve_code(code, code_file)?;
213        let ctx = normalize_stringified_json_object(ctx.unwrap_or(serde_json::Value::Null));
214        let (extra, extra_warnings) = self.resolve_extra_lib_paths(project_root.as_deref());
215        let (variants, variant_warnings) = self.resolve_variant_pkgs(project_root.as_deref());
216        let mut warnings: Vec<String> = extra_warnings;
217        warnings.extend(variant_warnings);
218
219        if host_mode == Some(true) {
220            // ── Pool path (Crux: MCP thin proxy IPC boundary) ─────────────────
221            // Worker subprocess is spawned and communicated via UDS.
222            // SessionRegistry (in-memory) is NOT touched on this path.
223            let (session_id, json, pool_save_error) = run_via_pool(
224                &self.pool_dir,
225                &self.pool_reg_path,
226                &self.pool_lock_path,
227                extra,
228                code,
229                ctx,
230            )
231            .await
232            .map_err(|e| e.to_string())?;
233
234            // session_id is stored in the JSON by the worker; update the
235            // in-memory registry so this MCP instance can route continues
236            // without another disk read.
237            // Load the just-persisted entry from disk to keep in-memory
238            // registry in sync.  This is a best-effort convenience cache;
239            // the disk state is authoritative.  Failure is surfaced to the
240            // MCP wire response as `pool_cache_reload_warning` so the caller
241            // can observe stale-cache conditions; tracing::warn! is kept for
242            // operator visibility in logs.
243            let cache_reload_warning: Option<String> =
244                match crate::pool::PoolRegistry::load_or_default(&self.pool_reg_path) {
245                    Ok(reg) => {
246                        let mut guard = self.pool_registry.write().await;
247                        *guard = reg;
248                        None
249                    }
250                    Err(e) => {
251                        tracing::warn!(
252                            error = %e,
253                            "failed to reload pool registry after run; in-memory cache may be stale"
254                        );
255                        Some(e.to_string())
256                    }
257                };
258
259            let json = splice_response_warnings(&json, "lib_path_warnings", &warnings);
260            let json = match pool_save_error {
261                Some(msg) => splice_response_string(&json, "pool_save_error", &msg),
262                None => json,
263            };
264            let json = match cache_reload_warning {
265                Some(msg) => splice_response_string(&json, "pool_cache_reload_warning", &msg),
266                None => json,
267            };
268            let _ = session_id; // session_id is embedded in the JSON response
269            return Ok(json);
270        }
271
272        // ── In-process path (default) ──────────────────────────────────────────
273
274        // Build the frozen env snapshot at alc_run invocation time (TIME boundary).
275        // Load alc.toml to get the optional env.allow allowlist.
276        let alc_toml_allow_list: Vec<String> = if let Some(root) = project_root.as_deref() {
277            let root_path = std::path::Path::new(root);
278            match load_alc_toml(root_path) {
279                Ok(Some(t)) => t.env.map(|e| e.allow).unwrap_or_default(),
280                Ok(None) => Vec::new(),
281                Err(e) => return Err(format!("alc.toml load error: {e}")),
282            }
283        } else {
284            Vec::new()
285        };
286        let alc_toml_allow = if alc_toml_allow_list.is_empty() {
287            None
288        } else {
289            Some(alc_toml_allow_list.as_slice())
290        };
291
292        let project_root_path = project_root.as_deref().map(std::path::Path::new);
293        let env_map = resolve_env(&ctx, project_root_path, alc_toml_allow)?;
294
295        let json = self
296            .start_and_tick(env_map, code, ctx, None, extra, variants)
297            .await?;
298        Ok(splice_response_warnings(
299            &json,
300            "lib_path_warnings",
301            &warnings,
302        ))
303    }
304
305    /// Apply a built-in strategy to a task.
306    ///
307    /// If the requested package is not installed, automatically installs the
308    /// bundled package collection from GitHub before executing.
309    ///
310    /// `project_root` — optional absolute path to the project root containing
311    /// `alc.lock`. Falls back to `ALC_PROJECT_ROOT` env or ancestor walk.
312    pub async fn advice(
313        &self,
314        strategy: &str,
315        task: Option<String>,
316        opts: Option<serde_json::Value>,
317        project_root: Option<String>,
318    ) -> Result<String, String> {
319        // Hoist variant-scope resolution before install check so alc.local.toml linked
320        // packages short-circuit auto_install / not-found error.
321        let app_dir = self.log_config.app_dir();
322        let (variants, variant_warnings) = self.resolve_variant_pkgs(project_root.as_deref());
323        let strategy_in_variant = variants.iter().any(|v| v.name == strategy);
324
325        // Auto-install bundled packages if the requested strategy is missing in all tiers
326        if !strategy_in_variant && !is_package_installed(&app_dir, strategy) {
327            self.auto_install_bundled_packages().await?;
328            if !is_package_installed(&app_dir, strategy) {
329                return Err(format!(
330                    "Package '{strategy}' not found after installing bundled collection. \
331                     Use alc_pkg_install to install it manually."
332                ));
333            }
334        }
335
336        // Guard: reject library packages before make_require_code (= M.run invocation)
337        if let Some((PkgType::Library, _)) = self.resolve_pkg_type_lua(strategy, &variants).await? {
338            return Err(format!(
339                "Package '{strategy}' is a library package (type = \"library\"). \
340                 Library packages provide reusable modules and do not have a run() entry point. \
341                 Use alc_run with custom code to import this library."
342            ));
343        }
344
345        let code = make_require_code(strategy);
346
347        let opts = opts.map(normalize_stringified_json_object);
348        let mut ctx_map = match opts {
349            Some(serde_json::Value::Object(m)) => m,
350            _ => serde_json::Map::new(),
351        };
352        if let Some(task) = task {
353            ctx_map.insert("task".into(), serde_json::Value::String(task));
354        }
355        let ctx = serde_json::Value::Object(ctx_map);
356
357        let (extra, extra_warnings) = self.resolve_extra_lib_paths(project_root.as_deref());
358        let mut warnings: Vec<String> = extra_warnings;
359        warnings.extend(variant_warnings);
360        // advice() does not accept ctx.env; pass an empty map so AlcEnv is
361        // present but empty (no env vars visible to advice strategies).
362        let env_map = Arc::new(HashMap::new());
363        let json = self
364            .start_and_tick(env_map, code, ctx, Some(strategy), extra, variants)
365            .await?;
366        Ok(splice_response_warnings(
367            &json,
368            "lib_path_warnings",
369            &warnings,
370        ))
371    }
372
373    /// Resolve the package type and provenance via a dedicated Lua VM.
374    ///
375    /// Runs `LUA_TYPE_AUTODETECT` in a VM that has variant-scope packages
376    /// registered so that packages linked via `alc.local.toml` are reachable.
377    ///
378    /// # Arguments
379    ///
380    /// * `name` - The package name to probe (used in `require`).
381    /// * `variants` - Variant-scope packages to register into the VM. Pass
382    ///   `&[]` when the caller has no variant context (e.g. the `eval` path).
383    ///
384    /// # Returns
385    ///
386    /// - `Ok(Some((PkgType, TypeSource)))` — type and provenance both parsed
387    ///   successfully.
388    /// - `Ok(None)` — eval succeeded but either the `type` or `type_source`
389    ///   field could not be parsed; the caller treats this as a legacy
390    ///   passthrough and does not apply the library guard.
391    /// - `Err(String)` — the Lua VM returned an error; propagated to the
392    ///   caller.
393    ///
394    /// # Provenance
395    ///
396    /// The Lua snippet (`LUA_TYPE_AUTODETECT`) sets `meta.type_source` to one
397    /// of `"auto_detected_runnable"` / `"auto_detected_library"` before
398    /// control returns to Rust, so both fields are always populated when the
399    /// snippet runs without error.
400    pub(crate) async fn resolve_pkg_type_lua(
401        &self,
402        name: &str,
403        variants: &[VariantPkg],
404    ) -> Result<Option<(PkgType, TypeSource)>, String> {
405        let auto = super::resolve::LUA_TYPE_AUTODETECT;
406        let code = format!(
407            r#"package.loaded["{name}"] = nil; local pkg = require("{name}"); local meta = pkg.meta or {{}}; {auto}; return {{ type = meta.type, type_source = meta.type_source }}"#,
408            name = name,
409            auto = auto,
410        );
411        let val = self
412            .executor
413            .eval_simple_with_paths(code, vec![], variants.to_vec())
414            .await?;
415        let result = val.as_object().and_then(|obj| {
416            let pkg_type =
417                obj.get("type")
418                    .and_then(|v| v.as_str())
419                    .and_then(|s| match s.parse::<PkgType>() {
420                        Ok(t) => Some(t),
421                        Err(e) => {
422                            tracing::warn!(
423                                package = name,
424                                raw_type = s,
425                                error = %e,
426                                "unknown pkg type string; treating as legacy passthrough"
427                            );
428                            None
429                        }
430                    });
431            let type_source = obj
432                .get("type_source")
433                .and_then(|v| v.as_str())
434                .and_then(|s| s.parse::<TypeSource>().ok());
435            match (pkg_type, type_source) {
436                (Some(t), Some(src)) => Some((t, src)),
437                _ => None,
438            }
439        });
440        Ok(result)
441    }
442
443    /// Continue a paused execution — batch feed.
444    ///
445    /// For pool sessions (`session_id` found in registry.json), each response
446    /// in the batch is forwarded to the worker via `PoolClient::send_request`.
447    /// For in-MCP sessions, the existing `SessionRegistry::feed_response` path
448    /// is used unchanged.
449    pub async fn continue_batch(
450        &self,
451        session_id: &str,
452        responses: Vec<QueryResponse>,
453    ) -> Result<String, String> {
454        // ── Pool path check (same registry lookup as continue_single) ─────────
455        let pool_entry = {
456            let reg = self.pool_registry.read().await;
457            reg.find(session_id).cloned()
458        };
459
460        let pool_entry = if pool_entry.is_some() {
461            pool_entry
462        } else {
463            match crate::pool::PoolRegistry::load_or_default(&self.pool_reg_path) {
464                Ok(reg) => {
465                    let entry = reg.find(session_id).cloned();
466                    if entry.is_some() {
467                        let mut guard = self.pool_registry.write().await;
468                        *guard = reg;
469                    }
470                    entry
471                }
472                Err(e) => {
473                    return Err(format!("Continue failed: {e}"));
474                }
475            }
476        };
477
478        if let Some(entry) = pool_entry {
479            // ── Pool routing ────────────────────────────────────────────────────
480            let mut last_json = None;
481            for qr in responses {
482                let json =
483                    continue_via_pool(&entry, session_id, qr.response, Some(qr.query_id), qr.usage)
484                        .await
485                        .map_err(|e| format!("Continue failed: {e}"))?;
486                last_json = Some(json);
487            }
488            return last_json.ok_or_else(|| "Empty responses array".to_string());
489        }
490
491        // ── In-MCP path ────────────────────────────────────────────────────────
492        let mut last_result = None;
493        for qr in responses {
494            let qid = QueryId::parse(&qr.query_id);
495            let result = self
496                .registry
497                .feed_response(session_id, &qid, qr.response, qr.usage.as_ref())
498                .await
499                .map_err(|e| format!("Continue failed: {e}"))?;
500            last_result = Some(result);
501        }
502        let result = last_result.ok_or("Empty responses array")?;
503        let transcript_warning = self.maybe_log_transcript(&result, session_id);
504        let json = result.to_json(session_id).to_string();
505        let json = splice_transcript_warning(&json, transcript_warning);
506        let save_warning = self.maybe_save_eval(&result, session_id, &json);
507        Ok(splice_save_warning(&json, save_warning))
508    }
509
510    /// Continue a paused execution — single response (with optional query_id).
511    ///
512    /// Routing is automatic: if `session_id` is found in `registry.json`
513    /// (pool path), the call is proxied via `PoolClient` over UDS. If not
514    /// found (in-MCP path), the existing `SessionRegistry::feed_response`
515    /// is used. Both paths never coexist for the same `session_id`.
516    ///
517    /// # Concurrency
518    ///
519    /// **Pool path**: acquires `RwLock<PoolRegistry>` (read) to look up the
520    /// session entry, then acquires `tokio::sync::Mutex` inside `PoolClient`
521    /// to serialize the UDS write. Neither lock is held across the UDS await.
522    ///
523    /// **In-MCP path**: acquires lock C in the two-phase pattern documented on
524    /// `SessionRegistry::feed_response`.
525    ///
526    /// **Cancel safety**: cancelling mid-await on the pool path leaves the
527    /// worker subprocess running (UDS send may have been partially written;
528    /// `read_line` is not cancel-safe — a partial line in the buffer renders
529    /// the connection unusable and `PoolClient` must reconnect).
530    pub async fn continue_single(
531        &self,
532        session_id: &str,
533        response: String,
534        query_id: Option<&str>,
535        usage: Option<algocline_core::TokenUsage>,
536    ) -> Result<String, String> {
537        // ── Pool path: check in-memory registry, then disk registry ───────────
538        // K-4: acquire read lock, clone the entry, release lock BEFORE await.
539        let pool_entry = {
540            let reg = self.pool_registry.read().await;
541            reg.find(session_id).cloned()
542        }; // read lock released here
543
544        // If in-memory cache missed, check disk (e.g. after MCP restart).
545        let pool_entry = if pool_entry.is_some() {
546            pool_entry
547        } else {
548            match crate::pool::PoolRegistry::load_or_default(&self.pool_reg_path) {
549                Ok(reg) => {
550                    let entry = reg.find(session_id).cloned();
551                    if entry.is_some() {
552                        // Warm the in-memory cache.
553                        let mut guard = self.pool_registry.write().await;
554                        *guard = reg;
555                    }
556                    entry
557                }
558                Err(e) => {
559                    // Corrupt registry: propagate to MCP wire per §Error 伝播規律.
560                    return Err(format!("Continue failed: {e}"));
561                }
562            }
563        };
564
565        if let Some(entry) = pool_entry {
566            // ── Pool routing (Crux: MCP thin proxy IPC boundary) ──────────────
567            let json = continue_via_pool(
568                &entry,
569                session_id,
570                response,
571                query_id.map(str::to_string),
572                usage,
573            )
574            .await
575            .map_err(|e| format!("Continue failed: {e}"))?;
576            return Ok(json);
577        }
578
579        // ── In-MCP path ────────────────────────────────────────────────────────
580        let query_id = match query_id {
581            Some(qid) => QueryId::parse(qid),
582            None => self
583                .registry
584                .resolve_sole_pending_id(session_id)
585                .await
586                .map_err(|e| format!("Continue failed: {e}"))?,
587        };
588
589        let result = self
590            .registry
591            .feed_response(session_id, &query_id, response, usage.as_ref())
592            .await
593            .map_err(|e| format!("Continue failed: {e}"))?;
594
595        let transcript_warning = self.maybe_log_transcript(&result, session_id);
596        let json = result.to_json(session_id).to_string();
597        let json = splice_transcript_warning(&json, transcript_warning);
598        let save_warning = self.maybe_save_eval(&result, session_id, &json);
599        Ok(splice_save_warning(&json, save_warning))
600    }
601
602    // ─── Internal ───────────────────────────────────────────────
603
604    pub(super) fn maybe_log_transcript(
605        &self,
606        result: &FeedResult,
607        session_id: &str,
608    ) -> Option<String> {
609        if let FeedResult::Finished(exec_result) = result {
610            // Mutex poison means a previous thread panicked while holding the lock.
611            // Strategy name is non-critical for correctness, but the failure must be
612            // surfaced to MCP callers so it is observable, not silently dropped.
613            // See CLAUDE.md §Service 層の Error 伝播規律.
614            let strategy = match self.session_strategies.lock() {
615                Ok(mut map) => map.remove(session_id),
616                Err(e) => {
617                    tracing::warn!(
618                        "session_strategies mutex poisoned for '{}': {}",
619                        session_id,
620                        e
621                    );
622                    // Return warning immediately; transcript cannot be written
623                    // without strategy context being reliably recoverable.
624                    return Some(format!(
625                        "session_strategies mutex poisoned for '{session_id}': {e}"
626                    ));
627                }
628            };
629            // write_transcript_log returns Ok(Some(warning)) when meta write
630            // failed but the main log succeeded, so both Err and meta warning
631            // are surfaced as transcript_warning on the wire response.
632            match write_transcript_log(
633                &self.log_config,
634                session_id,
635                &exec_result.metrics,
636                strategy.as_deref(),
637            ) {
638                Err(e) => Some(e.to_string()),
639                Ok(meta_warning) => meta_warning,
640            }
641        } else {
642            None
643        }
644    }
645
646    /// Persist eval result for a finished session, returning any storage
647    /// failure as `Some(msg)` so the caller can surface it on the wire
648    /// response. `None` covers both "not an eval session" and
649    /// "successfully saved" — they are indistinguishable to the caller
650    /// because both produce the same wire shape.
651    pub(super) fn maybe_save_eval(
652        &self,
653        result: &FeedResult,
654        session_id: &str,
655        result_json: &str,
656    ) -> Option<String> {
657        if !matches!(result, FeedResult::Finished(_)) {
658            return None;
659        }
660        let strategy = {
661            let mut map = self.eval_sessions.lock().unwrap_or_else(|e| e.into_inner());
662            map.remove(session_id)
663        };
664        strategy.and_then(|s| {
665            super::eval_store::save_eval_result(&self.log_config.app_dir(), &s, result_json).err()
666        })
667    }
668
669    /// Start a Lua session with the given env snapshot and tick until the first
670    /// pause or completion.
671    ///
672    /// # Arguments
673    ///
674    /// - `env_map` — frozen env snapshot built by `resolve_env`; passed to
675    ///   `executor.start_session_with_env` so `alc.env` is populated before any
676    ///   Lua code runs (TIME boundary: snapshot is taken before this call).
677    /// - `code` — Lua source to execute.
678    /// - `ctx` — JSON context accessible as `ctx` global in the Lua VM.
679    /// - `strategy` — optional strategy name (used to correlate eval sessions).
680    /// - `extra_lib_paths` — additional `require` search paths.
681    /// - `variant_pkgs` — variant package overrides.
682    ///
683    /// # Errors
684    ///
685    /// Returns `Err(String)` if session spawn or initial execution fails.
686    pub(super) async fn start_and_tick(
687        &self,
688        env_map: Arc<HashMap<String, String>>,
689        code: String,
690        ctx: serde_json::Value,
691        strategy: Option<&str>,
692        extra_lib_paths: Vec<std::path::PathBuf>,
693        variant_pkgs: Vec<VariantPkg>,
694    ) -> Result<String, String> {
695        let scenarios_dir = self.log_config.app_dir().scenarios_dir();
696        let session = self
697            .executor
698            .start_session_with_env(
699                env_map,
700                code,
701                ctx,
702                extra_lib_paths,
703                variant_pkgs,
704                Arc::clone(&self.state_store),
705                Arc::clone(&self.card_store),
706                scenarios_dir,
707            )
708            .await?;
709        let (session_id, result) = self
710            .registry
711            .start_execution(session)
712            .await
713            .map_err(|e| format!("Execution failed: {e}"))?;
714        if let Some(s) = strategy {
715            if let Ok(mut map) = self.session_strategies.lock() {
716                map.insert(session_id.clone(), s.to_string());
717            }
718        }
719        let transcript_warning = self.maybe_log_transcript(&result, &session_id);
720        let json = result.to_json(&session_id).to_string();
721        Ok(splice_transcript_warning(&json, transcript_warning))
722    }
723}
724
725#[cfg(test)]
726mod tests {
727    use std::path::PathBuf;
728    use std::sync::Arc;
729
730    use algocline_core::{
731        AppDir, ExecutionMetrics, ExecutionObserver, LlmQuery, QueryId, TerminalState,
732    };
733    use algocline_engine::{ExecutionResult, FeedResult};
734
735    use super::super::config::{AppConfig, LogDirSource};
736    use super::{splice_transcript_warning, AppService};
737
738    fn make_metrics_with_transcript() -> ExecutionMetrics {
739        let metrics = ExecutionMetrics::new();
740        let observer = metrics.create_observer();
741        observer.on_paused(&[LlmQuery {
742            id: QueryId::single(),
743            prompt: "test prompt".into(),
744            system: None,
745            max_tokens: 100,
746            grounded: false,
747            underspecified: false,
748        }]);
749        metrics
750    }
751
752    fn make_finished_result(metrics: ExecutionMetrics) -> FeedResult {
753        FeedResult::Finished(ExecutionResult {
754            state: TerminalState::Completed {
755                result: serde_json::json!({"ok": true}),
756            },
757            metrics,
758        })
759    }
760
761    /// Build a minimal AppService with log_enabled and a custom log_dir.
762    async fn make_app_service_with_log_dir(log_dir: PathBuf) -> AppService {
763        let executor = Arc::new(
764            algocline_engine::Executor::new(vec![])
765                .await
766                .expect("executor"),
767        );
768        let tmp_app = tempfile::tempdir().expect("test tempdir");
769        let log_config = AppConfig {
770            log_dir: Some(log_dir),
771            log_dir_source: LogDirSource::EnvVar,
772            log_enabled: true,
773            prompt_preview_chars: 200,
774            app_dir: Arc::new(AppDir::new(tmp_app.path().to_path_buf())),
775        };
776        std::mem::forget(tmp_app);
777        AppService::new(executor, log_config, vec![])
778    }
779
780    // ── (b) maybe_log_transcript returns Some when write fails ──────────
781
782    #[tokio::test]
783    async fn maybe_log_transcript_returns_some_on_write_failure() {
784        let tmp = tempfile::tempdir().expect("test tempdir");
785        let log_dir = tmp.path().to_path_buf();
786        // Block write by creating a directory at the session file path.
787        std::fs::create_dir_all(log_dir.join("fail-session.json"))
788            .expect("pre-create dir to block write");
789        let svc = make_app_service_with_log_dir(log_dir).await;
790        let metrics = make_metrics_with_transcript();
791        let result = make_finished_result(metrics);
792        let warning = svc.maybe_log_transcript(&result, "fail-session");
793        assert!(warning.is_some(), "expected Some warning on write failure");
794        let msg = warning.unwrap();
795        assert!(
796            msg.contains("transcript"),
797            "warning should mention 'transcript', got: {msg}"
798        );
799    }
800
801    #[tokio::test]
802    async fn maybe_log_transcript_returns_none_on_non_finished() {
803        let tmp = tempfile::tempdir().expect("test tempdir");
804        let svc = make_app_service_with_log_dir(tmp.path().to_path_buf()).await;
805        let result = FeedResult::Accepted { remaining: 1 };
806        let warning = svc.maybe_log_transcript(&result, "any-session");
807        assert!(warning.is_none(), "Accepted result should return None");
808    }
809
810    // ── (c) splice_transcript_warning inserts field into JSON ───────────
811
812    #[test]
813    fn splice_transcript_warning_injects_field_when_some() {
814        let json = r#"{"status":"finished","result":{}}"#;
815        let out = splice_transcript_warning(json, Some("write failed".to_string()));
816        let v: serde_json::Value = serde_json::from_str(&out).expect("valid JSON");
817        assert_eq!(
818            v["transcript_warning"].as_str(),
819            Some("write failed"),
820            "transcript_warning field should be present"
821        );
822        // Original fields are preserved.
823        assert_eq!(v["status"].as_str(), Some("finished"));
824    }
825
826    #[test]
827    fn splice_transcript_warning_passthrough_when_none() {
828        let json = r#"{"status":"finished"}"#;
829        let out = splice_transcript_warning(json, None);
830        let v: serde_json::Value = serde_json::from_str(&out).expect("valid JSON");
831        assert!(
832            v.get("transcript_warning").is_none(),
833            "transcript_warning must be absent when warning is None"
834        );
835    }
836
837    // ── ST6: pool registry routing tests ────────────────────────────────────
838
839    use crate::pool::PoolSessionEntry;
840
841    /// T1: continue_single falls through to in-MCP path when session is not in pool registry.
842    ///
843    /// An unknown session ID should not be found in the pool registry and
844    /// should reach the `SessionRegistry::feed_response` path, which returns
845    /// an error because no session exists in the in-memory registry either.
846    #[tokio::test]
847    async fn continue_single_in_mcp_path_on_registry_miss() {
848        let tmp = tempfile::tempdir().expect("test tempdir");
849        let svc = make_app_service_with_log_dir(tmp.path().to_path_buf()).await;
850
851        // The pool registry on disk and in-memory is empty (no workers registered).
852        // continue_single should fall through to the in-MCP path and return
853        // "not found" because the in-memory session registry also has nothing.
854        let result = svc
855            .continue_single(
856                "nonexistent-session-id",
857                "some response".to_string(),
858                None,
859                None,
860            )
861            .await;
862        assert!(
863            result.is_err(),
864            "unknown session must return Err on in-MCP path"
865        );
866        let msg = result.unwrap_err();
867        assert!(
868            msg.contains("not found") || msg.contains("Continue failed"),
869            "error must indicate session not found, got: {msg}"
870        );
871    }
872
873    /// T2: AppService::new initialises pool_registry as empty when pool dir absent.
874    ///
875    /// Verifies that startup GC with a missing registry.json (normal first-run)
876    /// produces an empty PoolRegistry (not an error).
877    #[tokio::test]
878    async fn app_service_new_initialises_empty_pool_registry() {
879        let tmp = tempfile::tempdir().expect("test tempdir");
880        let svc = make_app_service_with_log_dir(tmp.path().to_path_buf()).await;
881
882        let reg = svc.pool_registry.read().await;
883        assert!(
884            reg.sessions.is_empty(),
885            "pool registry must be empty on first-run (no registry.json)"
886        );
887    }
888
889    /// T2b: AppService correctly stores pool registry paths derived from app_dir.
890    ///
891    /// Verifies that pool_dir / pool_reg_path / pool_lock_path are
892    /// non-empty paths derived from state_dir/pool/*.
893    #[tokio::test]
894    async fn app_service_pool_paths_correctly_derived() {
895        let tmp = tempfile::tempdir().expect("test tempdir");
896        let svc = make_app_service_with_log_dir(tmp.path().to_path_buf()).await;
897
898        assert!(
899            svc.pool_dir.ends_with("pool"),
900            "pool_dir must end in 'pool', got: {}",
901            svc.pool_dir.display()
902        );
903        assert!(
904            svc.pool_reg_path.ends_with("pool/registry.json"),
905            "pool_reg_path must end in 'pool/registry.json', got: {}",
906            svc.pool_reg_path.display()
907        );
908        assert!(
909            svc.pool_lock_path.ends_with("pool/registry.lock"),
910            "pool_lock_path must end in 'pool/registry.lock', got: {}",
911            svc.pool_lock_path.display()
912        );
913    }
914
915    /// T3: continue_single propagates PoolError::RegistryCorrupted to MCP wire.
916    ///
917    /// When registry.json is corrupt and there is a cache miss, continue_single
918    /// must return Err (not silently proceed with empty registry). This verifies
919    /// the CLAUDE.md §Error 伝播規律 invariant — no unwrap_or_default() swallowing.
920    #[tokio::test]
921    async fn continue_single_propagates_corrupted_registry_error() {
922        let tmp = tempfile::tempdir().expect("test tempdir");
923        let svc = make_app_service_with_log_dir(tmp.path().to_path_buf()).await;
924
925        // Write a corrupt registry.json to the pool directory.
926        let pool_dir = svc.pool_dir.clone();
927        std::fs::create_dir_all(&pool_dir).expect("create pool dir");
928        std::fs::write(pool_dir.join("registry.json"), b"{ not valid json !!!")
929            .expect("write corrupt registry");
930
931        // The in-memory cache is empty (startup GC failed on the corrupt file,
932        // so pool_registry is empty default).  The disk read in continue_single
933        // will hit the corrupt file and must propagate the error.
934        let result = svc
935            .continue_single("any-session-id", "response".to_string(), None, None)
936            .await;
937        assert!(
938            result.is_err(),
939            "corrupted registry must cause Err, not silent empty fallback"
940        );
941        let msg = result.unwrap_err();
942        assert!(
943            msg.contains("corrupted") || msg.contains("parse") || msg.contains("Continue failed"),
944            "error must mention registry problem, got: {msg}"
945        );
946    }
947
948    // ── pool_cache_reload_warning splice tests ───────────────────────────────
949
950    use super::super::eval_store::splice_response_string;
951
952    /// T1 (happy path): splice_response_string inserts pool_cache_reload_warning
953    /// into a valid JSON object response.
954    ///
955    /// Verifies the crux-card constraint: cache-reload failure must surface on
956    /// the MCP wire as an additive field, not remain warn!-only.
957    #[test]
958    fn splice_response_string_injects_cache_reload_warning() {
959        let json = r#"{"status":"finished","result":{"ok":true}}"#;
960        let msg = "failed to reload pool registry: No such file or directory";
961        let out = splice_response_string(json, "pool_cache_reload_warning", msg);
962        let v: serde_json::Value = serde_json::from_str(&out).expect("valid JSON");
963        assert_eq!(
964            v["pool_cache_reload_warning"].as_str(),
965            Some(msg),
966            "pool_cache_reload_warning must be present in response"
967        );
968        // Original fields preserved (additive, not destructive).
969        assert_eq!(v["status"].as_str(), Some("finished"));
970    }
971
972    /// T2 (edge case): splice_response_string is a no-op when input is not a
973    /// JSON object (e.g. bare string or array).
974    ///
975    /// Guards against panics when strategy output is malformed.
976    #[test]
977    fn splice_response_string_passthrough_on_non_object_json() {
978        let non_object = r#""just a string""#;
979        let out = splice_response_string(non_object, "pool_cache_reload_warning", "err");
980        // Must return original unchanged.
981        assert_eq!(out, non_object);
982    }
983
984    /// T3 (error path / None branch): when cache_reload_warning is None, the
985    /// pool_cache_reload_warning field must NOT appear in the response JSON.
986    ///
987    /// Verifies the None arm of the new match block in run.rs leaves the JSON
988    /// untouched, consistent with the pool_save_error pattern.
989    #[test]
990    fn splice_response_string_not_called_when_none() {
991        let json = r#"{"status":"finished"}"#;
992        // Simulate the None branch: we simply do not call splice_response_string.
993        let v: serde_json::Value = serde_json::from_str(json).expect("valid JSON");
994        assert!(
995            v.get("pool_cache_reload_warning").is_none(),
996            "pool_cache_reload_warning must be absent when no cache-reload error occurred"
997        );
998    }
999
1000    /// T1b: in-memory pool registry lookup finds an entry and routes to pool path.
1001    ///
1002    /// Inserts a live entry (current process PID) into the in-memory registry
1003    /// and verifies that continue_single attempts the pool path (fails with
1004    /// connection error because no real worker socket exists, not "not found").
1005    #[tokio::test]
1006    async fn continue_single_routes_to_pool_on_registry_hit() {
1007        let tmp = tempfile::tempdir().expect("test tempdir");
1008        let svc = make_app_service_with_log_dir(tmp.path().to_path_buf()).await;
1009
1010        // Insert a fake entry pointing to a non-existent socket.
1011        // This simulates the case where a pool session was started.
1012        let fake_sock = tmp.path().join("nonexistent.sock");
1013        let entry = PoolSessionEntry::new(
1014            "test-pool-session",
1015            std::process::id(), // live PID — survives GC
1016            fake_sock.clone(),
1017            env!("CARGO_PKG_VERSION"),
1018        );
1019        {
1020            let mut reg = svc.pool_registry.write().await;
1021            reg.add(entry);
1022        }
1023
1024        // continue_single should find the entry and attempt pool path.
1025        // The UDS connect will fail (no socket file) → PoolError::Connect → Err.
1026        // Importantly, the error is a connection error, NOT a "session not found" error.
1027        let result = svc
1028            .continue_single("test-pool-session", "response".to_string(), None, None)
1029            .await;
1030        assert!(
1031            result.is_err(),
1032            "pool path must fail with connect error (no real worker)"
1033        );
1034        let msg = result.unwrap_err();
1035        // The error must come from pool path (UDS connect), not from SessionRegistry.
1036        // "not found" would indicate the in-MCP path was taken instead.
1037        assert!(
1038            !msg.contains("session not found") || msg.contains("Continue failed"),
1039            "error must be from pool path (UDS connect), got: {msg}"
1040        );
1041    }
1042
1043    // ── resolve_env unit tests ──────────────────────────────────────────────────
1044
1045    use super::resolve_env;
1046
1047    /// T1 (happy path): inject keys are accessible in the resolved map.
1048    ///
1049    /// Verifies the SPACE boundary inject path: keys supplied via
1050    /// `ctx.env.inject` appear in the frozen snapshot.
1051    #[test]
1052    fn resolve_env_inject_keys_readable() {
1053        let ctx = serde_json::json!({
1054            "env": {
1055                "inject": { "FOO": "bar", "BAZ": "qux" }
1056            }
1057        });
1058        let map = resolve_env(&ctx, None, None).expect("resolve_env should succeed");
1059        assert_eq!(map.get("FOO").map(String::as_str), Some("bar"));
1060        assert_eq!(map.get("BAZ").map(String::as_str), Some("qux"));
1061    }
1062
1063    /// T1b (happy path): empty ctx produces an empty env map.
1064    ///
1065    /// Verifies that `alc.env` is always present (empty map is valid) even
1066    /// when no env configuration is supplied in the invocation context.
1067    #[test]
1068    fn resolve_env_empty_ctx_produces_empty_map() {
1069        let ctx = serde_json::Value::Null;
1070        let map = resolve_env(&ctx, None, None).expect("resolve_env with null ctx should succeed");
1071        assert!(map.is_empty(), "empty ctx must produce an empty env map");
1072    }
1073
1074    /// T1c (happy path): inject priority over dotenv file for same key.
1075    ///
1076    /// Verifies the SOURCE boundary: inject (Layer 3) overwrites dotenv (Layer 2).
1077    #[test]
1078    fn resolve_env_inject_overwrites_dotenv() {
1079        let tmp = tempfile::tempdir().expect("test tempdir");
1080        let env_file = tmp.path().join(".env");
1081        // The .env file declares PRIORITY=from_dotenv.
1082        std::fs::write(&env_file, b"PRIORITY=from_dotenv\n").expect("write .env");
1083
1084        let ctx = serde_json::json!({
1085            "env": {
1086                "dotenv": env_file.to_str().expect("valid path"),
1087                "inject": { "PRIORITY": "from_inject" }
1088            }
1089        });
1090        let map = resolve_env(&ctx, None, None).expect("resolve_env should succeed");
1091        // inject (higher priority) must win over dotenv.
1092        assert_eq!(
1093            map.get("PRIORITY").map(String::as_str),
1094            Some("from_inject"),
1095            "inject must shadow dotenv for the same key"
1096        );
1097    }
1098
1099    /// T1d (happy path): dotenv file keys are loaded when path is absolute.
1100    ///
1101    /// Verifies Layer 2 (dotenv) of the SOURCE boundary merge chain.
1102    #[test]
1103    fn resolve_env_dotenv_absolute_path_loaded() {
1104        let tmp = tempfile::tempdir().expect("test tempdir");
1105        let env_file = tmp.path().join(".env");
1106        std::fs::write(&env_file, b"DOTENV_KEY=dotenv_val\n").expect("write .env");
1107
1108        let ctx = serde_json::json!({
1109            "env": {
1110                "dotenv": env_file.to_str().expect("valid path")
1111            }
1112        });
1113        let map = resolve_env(&ctx, None, None).expect("resolve_env should succeed");
1114        assert_eq!(
1115            map.get("DOTENV_KEY").map(String::as_str),
1116            Some("dotenv_val"),
1117            "key from dotenv file must be accessible"
1118        );
1119    }
1120
1121    /// T1e (happy path): allowlist filter retains only listed keys.
1122    ///
1123    /// Verifies alc.toml [env].allow filtering is applied after 3-source merge.
1124    #[test]
1125    fn resolve_env_allowlist_filters_inject_keys() {
1126        let ctx = serde_json::json!({
1127            "env": {
1128                "inject": { "ALLOWED": "yes", "BLOCKED": "no" }
1129            }
1130        });
1131        let allow = vec!["ALLOWED".to_string()];
1132        let map =
1133            resolve_env(&ctx, None, Some(allow.as_slice())).expect("resolve_env should succeed");
1134        assert_eq!(map.get("ALLOWED").map(String::as_str), Some("yes"));
1135        assert!(
1136            map.get("BLOCKED").is_none(),
1137            "BLOCKED key must be excluded by allowlist"
1138        );
1139    }
1140
1141    /// T2 (boundary): allow_os=false (default) must not include any OS env vars.
1142    ///
1143    /// Verifies the SOURCE boundary: OS env is excluded unless explicitly opted in.
1144    #[test]
1145    fn resolve_env_allow_os_false_excludes_os_vars() {
1146        // PATH is nearly always set in the test environment.
1147        let ctx = serde_json::json!({ "env": { "allow_os": false } });
1148        let map = resolve_env(&ctx, None, None).expect("resolve_env should succeed");
1149        // Even if PATH is set in the OS, it must not appear in the snapshot.
1150        assert!(
1151            map.get("PATH").is_none(),
1152            "OS env must not leak when allow_os is false"
1153        );
1154    }
1155
1156    /// T2b (boundary): relative dotenv path without project_root returns Err.
1157    ///
1158    /// Verifies the plan Risks #3 decision: relative path + None project_root = Err.
1159    #[test]
1160    fn resolve_env_relative_dotenv_without_project_root_errors() {
1161        let ctx = serde_json::json!({
1162            "env": { "dotenv": ".env" }
1163        });
1164        let result = resolve_env(&ctx, None, None);
1165        assert!(
1166            result.is_err(),
1167            "relative dotenv path without project_root must return Err"
1168        );
1169        let msg = result.unwrap_err();
1170        assert!(
1171            msg.contains("project_root"),
1172            "error must mention project_root, got: {msg}"
1173        );
1174    }
1175
1176    /// T2c (boundary): relative dotenv path with project_root is resolved correctly.
1177    ///
1178    /// Verifies that a relative path is joined against project_root.
1179    #[test]
1180    fn resolve_env_relative_dotenv_with_project_root_resolved() {
1181        let tmp = tempfile::tempdir().expect("test tempdir");
1182        std::fs::write(tmp.path().join(".env"), b"REL_KEY=rel_val\n").expect("write .env");
1183
1184        let ctx = serde_json::json!({ "env": { "dotenv": ".env" } });
1185        let map = resolve_env(&ctx, Some(tmp.path()), None).expect("resolve_env should succeed");
1186        assert_eq!(
1187            map.get("REL_KEY").map(String::as_str),
1188            Some("rel_val"),
1189            "relative dotenv path must be resolved against project_root"
1190        );
1191    }
1192
1193    /// T2d (boundary): empty allowlist (None) means no filtering — all keys pass through.
1194    #[test]
1195    fn resolve_env_none_allowlist_keeps_all_inject_keys() {
1196        let ctx = serde_json::json!({
1197            "env": { "inject": { "A": "1", "B": "2" } }
1198        });
1199        let map = resolve_env(&ctx, None, None).expect("resolve_env should succeed");
1200        assert_eq!(
1201            map.len(),
1202            2,
1203            "all inject keys must be retained when allowlist is None"
1204        );
1205    }
1206
1207    /// T3 (error path): inject value that is not a string returns Err.
1208    ///
1209    /// Verifies that non-string inject values propagate as Result::Err (no silent drop).
1210    #[test]
1211    fn resolve_env_inject_non_string_value_errors() {
1212        let ctx = serde_json::json!({
1213            "env": { "inject": { "BAD": 42 } }
1214        });
1215        let result = resolve_env(&ctx, None, None);
1216        assert!(result.is_err(), "non-string inject value must return Err");
1217        let msg = result.unwrap_err();
1218        assert!(
1219            msg.contains("BAD"),
1220            "error must mention the offending key, got: {msg}"
1221        );
1222    }
1223
1224    /// T3b (error path): inject object that is not an object returns Err.
1225    #[test]
1226    fn resolve_env_inject_not_an_object_errors() {
1227        let ctx = serde_json::json!({
1228            "env": { "inject": ["not", "an", "object"] }
1229        });
1230        let result = resolve_env(&ctx, None, None);
1231        assert!(result.is_err(), "non-object inject value must return Err");
1232    }
1233
1234    /// T3c (error path): missing dotenv file propagates as Err (no silent skip).
1235    ///
1236    /// Verifies SOURCE boundary: dotenv I/O errors are surfaced, not swallowed.
1237    #[test]
1238    fn resolve_env_missing_dotenv_file_errors() {
1239        let ctx = serde_json::json!({
1240            "env": { "dotenv": "/nonexistent/path/to/.env" }
1241        });
1242        let result = resolve_env(&ctx, None, None);
1243        assert!(
1244            result.is_err(),
1245            "missing dotenv file must return Err, not empty map"
1246        );
1247        let msg = result.unwrap_err();
1248        assert!(
1249            msg.contains("dotenv"),
1250            "error must mention dotenv, got: {msg}"
1251        );
1252    }
1253
1254    // ── resolve_pkg_type_lua unit tests (ST2) ───────────────────────────────
1255
1256    use algocline_core::pkg::{PkgType, TypeSource};
1257
1258    /// Build a temporary package directory with the given `init.lua` content
1259    /// and return the parent directory (the one to pass as a lib_path to
1260    /// `Executor::new`).
1261    fn make_temp_pkg(parent: &std::path::Path, pkg_name: &str, init_lua: &str) -> PathBuf {
1262        let pkg_dir = parent.join(pkg_name);
1263        std::fs::create_dir_all(&pkg_dir).expect("create pkg dir");
1264        std::fs::write(pkg_dir.join("init.lua"), init_lua).expect("write init.lua");
1265        parent.to_path_buf()
1266    }
1267
1268    /// Build a minimal `AppService` whose executor can `require` packages from
1269    /// `pkg_root` (the parent directory that contains `<pkg_name>/init.lua`).
1270    async fn make_svc_with_pkg_root(pkg_root: PathBuf) -> AppService {
1271        let executor = Arc::new(
1272            algocline_engine::Executor::new(vec![pkg_root])
1273                .await
1274                .expect("executor"),
1275        );
1276        // `AppConfig::default()` (test-only) creates a fresh leaked tempdir and
1277        // sets log_enabled = false — suitable for unit tests.
1278        let log_config = super::super::config::AppConfig::default();
1279        AppService::new(executor, log_config, vec![])
1280    }
1281
1282    /// T1 (boundary): `M.run` defined + no `meta.type` → `Some((Runnable, AutoDetectedRunnable))`.
1283    ///
1284    /// When `meta.type` is absent and `pkg.run` is a function, the snippet
1285    /// sets `meta.type = "runnable"` and `meta.type_source = "auto_detected_runnable"`.
1286    #[tokio::test]
1287    async fn resolve_pkg_type_lua_returns_auto_runnable() {
1288        let tmp = tempfile::tempdir().expect("test tempdir");
1289        let pkg_root = make_temp_pkg(
1290            tmp.path(),
1291            "auto_runnable",
1292            r#"local M = {}
1293M.meta = { name = "auto_runnable" }
1294M.run = function(ctx) return "ok" end
1295return M
1296"#,
1297        );
1298        let svc = make_svc_with_pkg_root(pkg_root).await;
1299        let result = svc
1300            .resolve_pkg_type_lua("auto_runnable", &[])
1301            .await
1302            .expect("eval must succeed");
1303        assert_eq!(
1304            result,
1305            Some((PkgType::Runnable, TypeSource::AutoDetectedRunnable)),
1306            "M.run present + no meta.type must produce (Runnable, AutoDetectedRunnable)"
1307        );
1308    }
1309
1310    /// T3 (error path): no `M.run` + no `meta.type` → `Some((Library, AutoDetectedLibrary))`.
1311    ///
1312    /// When `meta.type` is absent and `pkg.run` is not a function, the snippet
1313    /// sets `meta.type = "library"` and `meta.type_source = "auto_detected_library"`.
1314    /// This is the warn-eligible case — the crux constraint requires this specific
1315    /// `TypeSource` variant so `alc_pkg_doctor` / `alc_pkg_list` can fire the
1316    /// unmarked-library warning only for `AutoDetectedLibrary`, not for `None`.
1317    #[tokio::test]
1318    async fn resolve_pkg_type_lua_returns_auto_library() {
1319        let tmp = tempfile::tempdir().expect("test tempdir");
1320        let pkg_root = make_temp_pkg(
1321            tmp.path(),
1322            "auto_library",
1323            r#"local M = {}
1324M.meta = { name = "auto_library" }
1325-- no M.run defined
1326return M
1327"#,
1328        );
1329        let svc = make_svc_with_pkg_root(pkg_root).await;
1330        let result = svc
1331            .resolve_pkg_type_lua("auto_library", &[])
1332            .await
1333            .expect("eval must succeed");
1334        assert_eq!(
1335            result,
1336            Some((PkgType::Library, TypeSource::AutoDetectedLibrary)),
1337            "no M.run + no meta.type must produce (Library, AutoDetectedLibrary)"
1338        );
1339    }
1340}