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        // Auto-install bundled packages if the requested strategy is missing
320        let app_dir = self.log_config.app_dir();
321        if !is_package_installed(&app_dir, strategy) {
322            self.auto_install_bundled_packages().await?;
323            if !is_package_installed(&app_dir, strategy) {
324                return Err(format!(
325                    "Package '{strategy}' not found after installing bundled collection. \
326                     Use alc_pkg_install to install it manually."
327                ));
328            }
329        }
330
331        // Guard: reject library packages before make_require_code (= M.run invocation)
332        if let Some((PkgType::Library, _)) = self.resolve_pkg_type_lua(strategy).await? {
333            return Err(format!(
334                "Package '{strategy}' is a library package (type = \"library\"). \
335                 Library packages provide reusable modules and do not have a run() entry point. \
336                 Use alc_run with custom code to import this library."
337            ));
338        }
339
340        let code = make_require_code(strategy);
341
342        let opts = opts.map(normalize_stringified_json_object);
343        let mut ctx_map = match opts {
344            Some(serde_json::Value::Object(m)) => m,
345            _ => serde_json::Map::new(),
346        };
347        if let Some(task) = task {
348            ctx_map.insert("task".into(), serde_json::Value::String(task));
349        }
350        let ctx = serde_json::Value::Object(ctx_map);
351
352        let (extra, extra_warnings) = self.resolve_extra_lib_paths(project_root.as_deref());
353        let (variants, variant_warnings) = self.resolve_variant_pkgs(project_root.as_deref());
354        let mut warnings: Vec<String> = extra_warnings;
355        warnings.extend(variant_warnings);
356        // advice() does not accept ctx.env; pass an empty map so AlcEnv is
357        // present but empty (no env vars visible to advice strategies).
358        let env_map = Arc::new(HashMap::new());
359        let json = self
360            .start_and_tick(env_map, code, ctx, Some(strategy), extra, variants)
361            .await?;
362        Ok(splice_response_warnings(
363            &json,
364            "lib_path_warnings",
365            &warnings,
366        ))
367    }
368
369    /// Resolve the package type and provenance via Lua VM (`eval_simple`).
370    ///
371    /// # Returns
372    ///
373    /// - `Ok(Some((PkgType, TypeSource)))` — type and provenance both parsed
374    ///   successfully.
375    /// - `Ok(None)` — eval succeeded but either the `type` or `type_source`
376    ///   field could not be parsed (unknown value or absent field); the caller
377    ///   treats this as a legacy passthrough and does not apply the library
378    ///   guard.
379    /// - `Err(String)` — `eval_simple` failed; propagated to the caller.
380    ///
381    /// # Provenance
382    ///
383    /// The Lua snippet (`LUA_TYPE_AUTODETECT`) sets `meta.type_source` to one
384    /// of `"auto_detected_runnable"` / `"auto_detected_library"` before control
385    /// returns to Rust, so both fields are always populated when the snippet
386    /// runs without error.
387    pub(crate) async fn resolve_pkg_type_lua(
388        &self,
389        name: &str,
390    ) -> Result<Option<(PkgType, TypeSource)>, String> {
391        let auto = super::resolve::LUA_TYPE_AUTODETECT;
392        let code = format!(
393            r#"package.loaded["{name}"] = nil; local pkg = require("{name}"); local meta = pkg.meta or {{}}; {auto}; return {{ type = meta.type, type_source = meta.type_source }}"#,
394            name = name,
395            auto = auto,
396        );
397        let val = self.executor.eval_simple(code).await?;
398        let result = val.as_object().and_then(|obj| {
399            let pkg_type =
400                obj.get("type")
401                    .and_then(|v| v.as_str())
402                    .and_then(|s| match s.parse::<PkgType>() {
403                        Ok(t) => Some(t),
404                        Err(e) => {
405                            tracing::warn!(
406                                package = name,
407                                raw_type = s,
408                                error = %e,
409                                "unknown pkg type string; treating as legacy passthrough"
410                            );
411                            None
412                        }
413                    });
414            let type_source = obj
415                .get("type_source")
416                .and_then(|v| v.as_str())
417                .and_then(|s| s.parse::<TypeSource>().ok());
418            match (pkg_type, type_source) {
419                (Some(t), Some(src)) => Some((t, src)),
420                _ => None,
421            }
422        });
423        Ok(result)
424    }
425
426    /// Continue a paused execution — batch feed.
427    ///
428    /// For pool sessions (`session_id` found in registry.json), each response
429    /// in the batch is forwarded to the worker via `PoolClient::send_request`.
430    /// For in-MCP sessions, the existing `SessionRegistry::feed_response` path
431    /// is used unchanged.
432    pub async fn continue_batch(
433        &self,
434        session_id: &str,
435        responses: Vec<QueryResponse>,
436    ) -> Result<String, String> {
437        // ── Pool path check (same registry lookup as continue_single) ─────────
438        let pool_entry = {
439            let reg = self.pool_registry.read().await;
440            reg.find(session_id).cloned()
441        };
442
443        let pool_entry = if pool_entry.is_some() {
444            pool_entry
445        } else {
446            match crate::pool::PoolRegistry::load_or_default(&self.pool_reg_path) {
447                Ok(reg) => {
448                    let entry = reg.find(session_id).cloned();
449                    if entry.is_some() {
450                        let mut guard = self.pool_registry.write().await;
451                        *guard = reg;
452                    }
453                    entry
454                }
455                Err(e) => {
456                    return Err(format!("Continue failed: {e}"));
457                }
458            }
459        };
460
461        if let Some(entry) = pool_entry {
462            // ── Pool routing ────────────────────────────────────────────────────
463            let mut last_json = None;
464            for qr in responses {
465                let json =
466                    continue_via_pool(&entry, session_id, qr.response, Some(qr.query_id), qr.usage)
467                        .await
468                        .map_err(|e| format!("Continue failed: {e}"))?;
469                last_json = Some(json);
470            }
471            return last_json.ok_or_else(|| "Empty responses array".to_string());
472        }
473
474        // ── In-MCP path ────────────────────────────────────────────────────────
475        let mut last_result = None;
476        for qr in responses {
477            let qid = QueryId::parse(&qr.query_id);
478            let result = self
479                .registry
480                .feed_response(session_id, &qid, qr.response, qr.usage.as_ref())
481                .await
482                .map_err(|e| format!("Continue failed: {e}"))?;
483            last_result = Some(result);
484        }
485        let result = last_result.ok_or("Empty responses array")?;
486        let transcript_warning = self.maybe_log_transcript(&result, session_id);
487        let json = result.to_json(session_id).to_string();
488        let json = splice_transcript_warning(&json, transcript_warning);
489        let save_warning = self.maybe_save_eval(&result, session_id, &json);
490        Ok(splice_save_warning(&json, save_warning))
491    }
492
493    /// Continue a paused execution — single response (with optional query_id).
494    ///
495    /// Routing is automatic: if `session_id` is found in `registry.json`
496    /// (pool path), the call is proxied via `PoolClient` over UDS. If not
497    /// found (in-MCP path), the existing `SessionRegistry::feed_response`
498    /// is used. Both paths never coexist for the same `session_id`.
499    ///
500    /// # Concurrency
501    ///
502    /// **Pool path**: acquires `RwLock<PoolRegistry>` (read) to look up the
503    /// session entry, then acquires `tokio::sync::Mutex` inside `PoolClient`
504    /// to serialize the UDS write. Neither lock is held across the UDS await.
505    ///
506    /// **In-MCP path**: acquires lock C in the two-phase pattern documented on
507    /// `SessionRegistry::feed_response`.
508    ///
509    /// **Cancel safety**: cancelling mid-await on the pool path leaves the
510    /// worker subprocess running (UDS send may have been partially written;
511    /// `read_line` is not cancel-safe — a partial line in the buffer renders
512    /// the connection unusable and `PoolClient` must reconnect).
513    pub async fn continue_single(
514        &self,
515        session_id: &str,
516        response: String,
517        query_id: Option<&str>,
518        usage: Option<algocline_core::TokenUsage>,
519    ) -> Result<String, String> {
520        // ── Pool path: check in-memory registry, then disk registry ───────────
521        // K-4: acquire read lock, clone the entry, release lock BEFORE await.
522        let pool_entry = {
523            let reg = self.pool_registry.read().await;
524            reg.find(session_id).cloned()
525        }; // read lock released here
526
527        // If in-memory cache missed, check disk (e.g. after MCP restart).
528        let pool_entry = if pool_entry.is_some() {
529            pool_entry
530        } else {
531            match crate::pool::PoolRegistry::load_or_default(&self.pool_reg_path) {
532                Ok(reg) => {
533                    let entry = reg.find(session_id).cloned();
534                    if entry.is_some() {
535                        // Warm the in-memory cache.
536                        let mut guard = self.pool_registry.write().await;
537                        *guard = reg;
538                    }
539                    entry
540                }
541                Err(e) => {
542                    // Corrupt registry: propagate to MCP wire per §Error 伝播規律.
543                    return Err(format!("Continue failed: {e}"));
544                }
545            }
546        };
547
548        if let Some(entry) = pool_entry {
549            // ── Pool routing (Crux: MCP thin proxy IPC boundary) ──────────────
550            let json = continue_via_pool(
551                &entry,
552                session_id,
553                response,
554                query_id.map(str::to_string),
555                usage,
556            )
557            .await
558            .map_err(|e| format!("Continue failed: {e}"))?;
559            return Ok(json);
560        }
561
562        // ── In-MCP path ────────────────────────────────────────────────────────
563        let query_id = match query_id {
564            Some(qid) => QueryId::parse(qid),
565            None => self
566                .registry
567                .resolve_sole_pending_id(session_id)
568                .await
569                .map_err(|e| format!("Continue failed: {e}"))?,
570        };
571
572        let result = self
573            .registry
574            .feed_response(session_id, &query_id, response, usage.as_ref())
575            .await
576            .map_err(|e| format!("Continue failed: {e}"))?;
577
578        let transcript_warning = self.maybe_log_transcript(&result, session_id);
579        let json = result.to_json(session_id).to_string();
580        let json = splice_transcript_warning(&json, transcript_warning);
581        let save_warning = self.maybe_save_eval(&result, session_id, &json);
582        Ok(splice_save_warning(&json, save_warning))
583    }
584
585    // ─── Internal ───────────────────────────────────────────────
586
587    pub(super) fn maybe_log_transcript(
588        &self,
589        result: &FeedResult,
590        session_id: &str,
591    ) -> Option<String> {
592        if let FeedResult::Finished(exec_result) = result {
593            // Mutex poison means a previous thread panicked while holding the lock.
594            // Strategy name is non-critical for correctness, but the failure must be
595            // surfaced to MCP callers so it is observable, not silently dropped.
596            // See CLAUDE.md §Service 層の Error 伝播規律.
597            let strategy = match self.session_strategies.lock() {
598                Ok(mut map) => map.remove(session_id),
599                Err(e) => {
600                    tracing::warn!(
601                        "session_strategies mutex poisoned for '{}': {}",
602                        session_id,
603                        e
604                    );
605                    // Return warning immediately; transcript cannot be written
606                    // without strategy context being reliably recoverable.
607                    return Some(format!(
608                        "session_strategies mutex poisoned for '{session_id}': {e}"
609                    ));
610                }
611            };
612            // write_transcript_log returns Ok(Some(warning)) when meta write
613            // failed but the main log succeeded, so both Err and meta warning
614            // are surfaced as transcript_warning on the wire response.
615            match write_transcript_log(
616                &self.log_config,
617                session_id,
618                &exec_result.metrics,
619                strategy.as_deref(),
620            ) {
621                Err(e) => Some(e.to_string()),
622                Ok(meta_warning) => meta_warning,
623            }
624        } else {
625            None
626        }
627    }
628
629    /// Persist eval result for a finished session, returning any storage
630    /// failure as `Some(msg)` so the caller can surface it on the wire
631    /// response. `None` covers both "not an eval session" and
632    /// "successfully saved" — they are indistinguishable to the caller
633    /// because both produce the same wire shape.
634    pub(super) fn maybe_save_eval(
635        &self,
636        result: &FeedResult,
637        session_id: &str,
638        result_json: &str,
639    ) -> Option<String> {
640        if !matches!(result, FeedResult::Finished(_)) {
641            return None;
642        }
643        let strategy = {
644            let mut map = self.eval_sessions.lock().unwrap_or_else(|e| e.into_inner());
645            map.remove(session_id)
646        };
647        strategy.and_then(|s| {
648            super::eval_store::save_eval_result(&self.log_config.app_dir(), &s, result_json).err()
649        })
650    }
651
652    /// Start a Lua session with the given env snapshot and tick until the first
653    /// pause or completion.
654    ///
655    /// # Arguments
656    ///
657    /// - `env_map` — frozen env snapshot built by `resolve_env`; passed to
658    ///   `executor.start_session_with_env` so `alc.env` is populated before any
659    ///   Lua code runs (TIME boundary: snapshot is taken before this call).
660    /// - `code` — Lua source to execute.
661    /// - `ctx` — JSON context accessible as `ctx` global in the Lua VM.
662    /// - `strategy` — optional strategy name (used to correlate eval sessions).
663    /// - `extra_lib_paths` — additional `require` search paths.
664    /// - `variant_pkgs` — variant package overrides.
665    ///
666    /// # Errors
667    ///
668    /// Returns `Err(String)` if session spawn or initial execution fails.
669    pub(super) async fn start_and_tick(
670        &self,
671        env_map: Arc<HashMap<String, String>>,
672        code: String,
673        ctx: serde_json::Value,
674        strategy: Option<&str>,
675        extra_lib_paths: Vec<std::path::PathBuf>,
676        variant_pkgs: Vec<VariantPkg>,
677    ) -> Result<String, String> {
678        let scenarios_dir = self.log_config.app_dir().scenarios_dir();
679        let session = self
680            .executor
681            .start_session_with_env(
682                env_map,
683                code,
684                ctx,
685                extra_lib_paths,
686                variant_pkgs,
687                Arc::clone(&self.state_store),
688                Arc::clone(&self.card_store),
689                scenarios_dir,
690            )
691            .await?;
692        let (session_id, result) = self
693            .registry
694            .start_execution(session)
695            .await
696            .map_err(|e| format!("Execution failed: {e}"))?;
697        if let Some(s) = strategy {
698            if let Ok(mut map) = self.session_strategies.lock() {
699                map.insert(session_id.clone(), s.to_string());
700            }
701        }
702        let transcript_warning = self.maybe_log_transcript(&result, &session_id);
703        let json = result.to_json(&session_id).to_string();
704        Ok(splice_transcript_warning(&json, transcript_warning))
705    }
706}
707
708#[cfg(test)]
709mod tests {
710    use std::path::PathBuf;
711    use std::sync::Arc;
712
713    use algocline_core::{
714        AppDir, ExecutionMetrics, ExecutionObserver, LlmQuery, QueryId, TerminalState,
715    };
716    use algocline_engine::{ExecutionResult, FeedResult};
717
718    use super::super::config::{AppConfig, LogDirSource};
719    use super::{splice_transcript_warning, AppService};
720
721    fn make_metrics_with_transcript() -> ExecutionMetrics {
722        let metrics = ExecutionMetrics::new();
723        let observer = metrics.create_observer();
724        observer.on_paused(&[LlmQuery {
725            id: QueryId::single(),
726            prompt: "test prompt".into(),
727            system: None,
728            max_tokens: 100,
729            grounded: false,
730            underspecified: false,
731        }]);
732        metrics
733    }
734
735    fn make_finished_result(metrics: ExecutionMetrics) -> FeedResult {
736        FeedResult::Finished(ExecutionResult {
737            state: TerminalState::Completed {
738                result: serde_json::json!({"ok": true}),
739            },
740            metrics,
741        })
742    }
743
744    /// Build a minimal AppService with log_enabled and a custom log_dir.
745    async fn make_app_service_with_log_dir(log_dir: PathBuf) -> AppService {
746        let executor = Arc::new(
747            algocline_engine::Executor::new(vec![])
748                .await
749                .expect("executor"),
750        );
751        let tmp_app = tempfile::tempdir().expect("test tempdir");
752        let log_config = AppConfig {
753            log_dir: Some(log_dir),
754            log_dir_source: LogDirSource::EnvVar,
755            log_enabled: true,
756            prompt_preview_chars: 200,
757            app_dir: Arc::new(AppDir::new(tmp_app.path().to_path_buf())),
758        };
759        std::mem::forget(tmp_app);
760        AppService::new(executor, log_config, vec![])
761    }
762
763    // ── (b) maybe_log_transcript returns Some when write fails ──────────
764
765    #[tokio::test]
766    async fn maybe_log_transcript_returns_some_on_write_failure() {
767        let tmp = tempfile::tempdir().expect("test tempdir");
768        let log_dir = tmp.path().to_path_buf();
769        // Block write by creating a directory at the session file path.
770        std::fs::create_dir_all(log_dir.join("fail-session.json"))
771            .expect("pre-create dir to block write");
772        let svc = make_app_service_with_log_dir(log_dir).await;
773        let metrics = make_metrics_with_transcript();
774        let result = make_finished_result(metrics);
775        let warning = svc.maybe_log_transcript(&result, "fail-session");
776        assert!(warning.is_some(), "expected Some warning on write failure");
777        let msg = warning.unwrap();
778        assert!(
779            msg.contains("transcript"),
780            "warning should mention 'transcript', got: {msg}"
781        );
782    }
783
784    #[tokio::test]
785    async fn maybe_log_transcript_returns_none_on_non_finished() {
786        let tmp = tempfile::tempdir().expect("test tempdir");
787        let svc = make_app_service_with_log_dir(tmp.path().to_path_buf()).await;
788        let result = FeedResult::Accepted { remaining: 1 };
789        let warning = svc.maybe_log_transcript(&result, "any-session");
790        assert!(warning.is_none(), "Accepted result should return None");
791    }
792
793    // ── (c) splice_transcript_warning inserts field into JSON ───────────
794
795    #[test]
796    fn splice_transcript_warning_injects_field_when_some() {
797        let json = r#"{"status":"finished","result":{}}"#;
798        let out = splice_transcript_warning(json, Some("write failed".to_string()));
799        let v: serde_json::Value = serde_json::from_str(&out).expect("valid JSON");
800        assert_eq!(
801            v["transcript_warning"].as_str(),
802            Some("write failed"),
803            "transcript_warning field should be present"
804        );
805        // Original fields are preserved.
806        assert_eq!(v["status"].as_str(), Some("finished"));
807    }
808
809    #[test]
810    fn splice_transcript_warning_passthrough_when_none() {
811        let json = r#"{"status":"finished"}"#;
812        let out = splice_transcript_warning(json, None);
813        let v: serde_json::Value = serde_json::from_str(&out).expect("valid JSON");
814        assert!(
815            v.get("transcript_warning").is_none(),
816            "transcript_warning must be absent when warning is None"
817        );
818    }
819
820    // ── ST6: pool registry routing tests ────────────────────────────────────
821
822    use crate::pool::PoolSessionEntry;
823
824    /// T1: continue_single falls through to in-MCP path when session is not in pool registry.
825    ///
826    /// An unknown session ID should not be found in the pool registry and
827    /// should reach the `SessionRegistry::feed_response` path, which returns
828    /// an error because no session exists in the in-memory registry either.
829    #[tokio::test]
830    async fn continue_single_in_mcp_path_on_registry_miss() {
831        let tmp = tempfile::tempdir().expect("test tempdir");
832        let svc = make_app_service_with_log_dir(tmp.path().to_path_buf()).await;
833
834        // The pool registry on disk and in-memory is empty (no workers registered).
835        // continue_single should fall through to the in-MCP path and return
836        // "not found" because the in-memory session registry also has nothing.
837        let result = svc
838            .continue_single(
839                "nonexistent-session-id",
840                "some response".to_string(),
841                None,
842                None,
843            )
844            .await;
845        assert!(
846            result.is_err(),
847            "unknown session must return Err on in-MCP path"
848        );
849        let msg = result.unwrap_err();
850        assert!(
851            msg.contains("not found") || msg.contains("Continue failed"),
852            "error must indicate session not found, got: {msg}"
853        );
854    }
855
856    /// T2: AppService::new initialises pool_registry as empty when pool dir absent.
857    ///
858    /// Verifies that startup GC with a missing registry.json (normal first-run)
859    /// produces an empty PoolRegistry (not an error).
860    #[tokio::test]
861    async fn app_service_new_initialises_empty_pool_registry() {
862        let tmp = tempfile::tempdir().expect("test tempdir");
863        let svc = make_app_service_with_log_dir(tmp.path().to_path_buf()).await;
864
865        let reg = svc.pool_registry.read().await;
866        assert!(
867            reg.sessions.is_empty(),
868            "pool registry must be empty on first-run (no registry.json)"
869        );
870    }
871
872    /// T2b: AppService correctly stores pool registry paths derived from app_dir.
873    ///
874    /// Verifies that pool_dir / pool_reg_path / pool_lock_path are
875    /// non-empty paths derived from state_dir/pool/*.
876    #[tokio::test]
877    async fn app_service_pool_paths_correctly_derived() {
878        let tmp = tempfile::tempdir().expect("test tempdir");
879        let svc = make_app_service_with_log_dir(tmp.path().to_path_buf()).await;
880
881        assert!(
882            svc.pool_dir.ends_with("pool"),
883            "pool_dir must end in 'pool', got: {}",
884            svc.pool_dir.display()
885        );
886        assert!(
887            svc.pool_reg_path.ends_with("pool/registry.json"),
888            "pool_reg_path must end in 'pool/registry.json', got: {}",
889            svc.pool_reg_path.display()
890        );
891        assert!(
892            svc.pool_lock_path.ends_with("pool/registry.lock"),
893            "pool_lock_path must end in 'pool/registry.lock', got: {}",
894            svc.pool_lock_path.display()
895        );
896    }
897
898    /// T3: continue_single propagates PoolError::RegistryCorrupted to MCP wire.
899    ///
900    /// When registry.json is corrupt and there is a cache miss, continue_single
901    /// must return Err (not silently proceed with empty registry). This verifies
902    /// the CLAUDE.md §Error 伝播規律 invariant — no unwrap_or_default() swallowing.
903    #[tokio::test]
904    async fn continue_single_propagates_corrupted_registry_error() {
905        let tmp = tempfile::tempdir().expect("test tempdir");
906        let svc = make_app_service_with_log_dir(tmp.path().to_path_buf()).await;
907
908        // Write a corrupt registry.json to the pool directory.
909        let pool_dir = svc.pool_dir.clone();
910        std::fs::create_dir_all(&pool_dir).expect("create pool dir");
911        std::fs::write(pool_dir.join("registry.json"), b"{ not valid json !!!")
912            .expect("write corrupt registry");
913
914        // The in-memory cache is empty (startup GC failed on the corrupt file,
915        // so pool_registry is empty default).  The disk read in continue_single
916        // will hit the corrupt file and must propagate the error.
917        let result = svc
918            .continue_single("any-session-id", "response".to_string(), None, None)
919            .await;
920        assert!(
921            result.is_err(),
922            "corrupted registry must cause Err, not silent empty fallback"
923        );
924        let msg = result.unwrap_err();
925        assert!(
926            msg.contains("corrupted") || msg.contains("parse") || msg.contains("Continue failed"),
927            "error must mention registry problem, got: {msg}"
928        );
929    }
930
931    // ── pool_cache_reload_warning splice tests ───────────────────────────────
932
933    use super::super::eval_store::splice_response_string;
934
935    /// T1 (happy path): splice_response_string inserts pool_cache_reload_warning
936    /// into a valid JSON object response.
937    ///
938    /// Verifies the crux-card constraint: cache-reload failure must surface on
939    /// the MCP wire as an additive field, not remain warn!-only.
940    #[test]
941    fn splice_response_string_injects_cache_reload_warning() {
942        let json = r#"{"status":"finished","result":{"ok":true}}"#;
943        let msg = "failed to reload pool registry: No such file or directory";
944        let out = splice_response_string(json, "pool_cache_reload_warning", msg);
945        let v: serde_json::Value = serde_json::from_str(&out).expect("valid JSON");
946        assert_eq!(
947            v["pool_cache_reload_warning"].as_str(),
948            Some(msg),
949            "pool_cache_reload_warning must be present in response"
950        );
951        // Original fields preserved (additive, not destructive).
952        assert_eq!(v["status"].as_str(), Some("finished"));
953    }
954
955    /// T2 (edge case): splice_response_string is a no-op when input is not a
956    /// JSON object (e.g. bare string or array).
957    ///
958    /// Guards against panics when strategy output is malformed.
959    #[test]
960    fn splice_response_string_passthrough_on_non_object_json() {
961        let non_object = r#""just a string""#;
962        let out = splice_response_string(non_object, "pool_cache_reload_warning", "err");
963        // Must return original unchanged.
964        assert_eq!(out, non_object);
965    }
966
967    /// T3 (error path / None branch): when cache_reload_warning is None, the
968    /// pool_cache_reload_warning field must NOT appear in the response JSON.
969    ///
970    /// Verifies the None arm of the new match block in run.rs leaves the JSON
971    /// untouched, consistent with the pool_save_error pattern.
972    #[test]
973    fn splice_response_string_not_called_when_none() {
974        let json = r#"{"status":"finished"}"#;
975        // Simulate the None branch: we simply do not call splice_response_string.
976        let v: serde_json::Value = serde_json::from_str(json).expect("valid JSON");
977        assert!(
978            v.get("pool_cache_reload_warning").is_none(),
979            "pool_cache_reload_warning must be absent when no cache-reload error occurred"
980        );
981    }
982
983    /// T1b: in-memory pool registry lookup finds an entry and routes to pool path.
984    ///
985    /// Inserts a live entry (current process PID) into the in-memory registry
986    /// and verifies that continue_single attempts the pool path (fails with
987    /// connection error because no real worker socket exists, not "not found").
988    #[tokio::test]
989    async fn continue_single_routes_to_pool_on_registry_hit() {
990        let tmp = tempfile::tempdir().expect("test tempdir");
991        let svc = make_app_service_with_log_dir(tmp.path().to_path_buf()).await;
992
993        // Insert a fake entry pointing to a non-existent socket.
994        // This simulates the case where a pool session was started.
995        let fake_sock = tmp.path().join("nonexistent.sock");
996        let entry = PoolSessionEntry::new(
997            "test-pool-session",
998            std::process::id(), // live PID — survives GC
999            fake_sock.clone(),
1000            env!("CARGO_PKG_VERSION"),
1001        );
1002        {
1003            let mut reg = svc.pool_registry.write().await;
1004            reg.add(entry);
1005        }
1006
1007        // continue_single should find the entry and attempt pool path.
1008        // The UDS connect will fail (no socket file) → PoolError::Connect → Err.
1009        // Importantly, the error is a connection error, NOT a "session not found" error.
1010        let result = svc
1011            .continue_single("test-pool-session", "response".to_string(), None, None)
1012            .await;
1013        assert!(
1014            result.is_err(),
1015            "pool path must fail with connect error (no real worker)"
1016        );
1017        let msg = result.unwrap_err();
1018        // The error must come from pool path (UDS connect), not from SessionRegistry.
1019        // "not found" would indicate the in-MCP path was taken instead.
1020        assert!(
1021            !msg.contains("session not found") || msg.contains("Continue failed"),
1022            "error must be from pool path (UDS connect), got: {msg}"
1023        );
1024    }
1025
1026    // ── resolve_env unit tests ──────────────────────────────────────────────────
1027
1028    use super::resolve_env;
1029
1030    /// T1 (happy path): inject keys are accessible in the resolved map.
1031    ///
1032    /// Verifies the SPACE boundary inject path: keys supplied via
1033    /// `ctx.env.inject` appear in the frozen snapshot.
1034    #[test]
1035    fn resolve_env_inject_keys_readable() {
1036        let ctx = serde_json::json!({
1037            "env": {
1038                "inject": { "FOO": "bar", "BAZ": "qux" }
1039            }
1040        });
1041        let map = resolve_env(&ctx, None, None).expect("resolve_env should succeed");
1042        assert_eq!(map.get("FOO").map(String::as_str), Some("bar"));
1043        assert_eq!(map.get("BAZ").map(String::as_str), Some("qux"));
1044    }
1045
1046    /// T1b (happy path): empty ctx produces an empty env map.
1047    ///
1048    /// Verifies that `alc.env` is always present (empty map is valid) even
1049    /// when no env configuration is supplied in the invocation context.
1050    #[test]
1051    fn resolve_env_empty_ctx_produces_empty_map() {
1052        let ctx = serde_json::Value::Null;
1053        let map = resolve_env(&ctx, None, None).expect("resolve_env with null ctx should succeed");
1054        assert!(map.is_empty(), "empty ctx must produce an empty env map");
1055    }
1056
1057    /// T1c (happy path): inject priority over dotenv file for same key.
1058    ///
1059    /// Verifies the SOURCE boundary: inject (Layer 3) overwrites dotenv (Layer 2).
1060    #[test]
1061    fn resolve_env_inject_overwrites_dotenv() {
1062        let tmp = tempfile::tempdir().expect("test tempdir");
1063        let env_file = tmp.path().join(".env");
1064        // The .env file declares PRIORITY=from_dotenv.
1065        std::fs::write(&env_file, b"PRIORITY=from_dotenv\n").expect("write .env");
1066
1067        let ctx = serde_json::json!({
1068            "env": {
1069                "dotenv": env_file.to_str().expect("valid path"),
1070                "inject": { "PRIORITY": "from_inject" }
1071            }
1072        });
1073        let map = resolve_env(&ctx, None, None).expect("resolve_env should succeed");
1074        // inject (higher priority) must win over dotenv.
1075        assert_eq!(
1076            map.get("PRIORITY").map(String::as_str),
1077            Some("from_inject"),
1078            "inject must shadow dotenv for the same key"
1079        );
1080    }
1081
1082    /// T1d (happy path): dotenv file keys are loaded when path is absolute.
1083    ///
1084    /// Verifies Layer 2 (dotenv) of the SOURCE boundary merge chain.
1085    #[test]
1086    fn resolve_env_dotenv_absolute_path_loaded() {
1087        let tmp = tempfile::tempdir().expect("test tempdir");
1088        let env_file = tmp.path().join(".env");
1089        std::fs::write(&env_file, b"DOTENV_KEY=dotenv_val\n").expect("write .env");
1090
1091        let ctx = serde_json::json!({
1092            "env": {
1093                "dotenv": env_file.to_str().expect("valid path")
1094            }
1095        });
1096        let map = resolve_env(&ctx, None, None).expect("resolve_env should succeed");
1097        assert_eq!(
1098            map.get("DOTENV_KEY").map(String::as_str),
1099            Some("dotenv_val"),
1100            "key from dotenv file must be accessible"
1101        );
1102    }
1103
1104    /// T1e (happy path): allowlist filter retains only listed keys.
1105    ///
1106    /// Verifies alc.toml [env].allow filtering is applied after 3-source merge.
1107    #[test]
1108    fn resolve_env_allowlist_filters_inject_keys() {
1109        let ctx = serde_json::json!({
1110            "env": {
1111                "inject": { "ALLOWED": "yes", "BLOCKED": "no" }
1112            }
1113        });
1114        let allow = vec!["ALLOWED".to_string()];
1115        let map =
1116            resolve_env(&ctx, None, Some(allow.as_slice())).expect("resolve_env should succeed");
1117        assert_eq!(map.get("ALLOWED").map(String::as_str), Some("yes"));
1118        assert!(
1119            map.get("BLOCKED").is_none(),
1120            "BLOCKED key must be excluded by allowlist"
1121        );
1122    }
1123
1124    /// T2 (boundary): allow_os=false (default) must not include any OS env vars.
1125    ///
1126    /// Verifies the SOURCE boundary: OS env is excluded unless explicitly opted in.
1127    #[test]
1128    fn resolve_env_allow_os_false_excludes_os_vars() {
1129        // PATH is nearly always set in the test environment.
1130        let ctx = serde_json::json!({ "env": { "allow_os": false } });
1131        let map = resolve_env(&ctx, None, None).expect("resolve_env should succeed");
1132        // Even if PATH is set in the OS, it must not appear in the snapshot.
1133        assert!(
1134            map.get("PATH").is_none(),
1135            "OS env must not leak when allow_os is false"
1136        );
1137    }
1138
1139    /// T2b (boundary): relative dotenv path without project_root returns Err.
1140    ///
1141    /// Verifies the plan Risks #3 decision: relative path + None project_root = Err.
1142    #[test]
1143    fn resolve_env_relative_dotenv_without_project_root_errors() {
1144        let ctx = serde_json::json!({
1145            "env": { "dotenv": ".env" }
1146        });
1147        let result = resolve_env(&ctx, None, None);
1148        assert!(
1149            result.is_err(),
1150            "relative dotenv path without project_root must return Err"
1151        );
1152        let msg = result.unwrap_err();
1153        assert!(
1154            msg.contains("project_root"),
1155            "error must mention project_root, got: {msg}"
1156        );
1157    }
1158
1159    /// T2c (boundary): relative dotenv path with project_root is resolved correctly.
1160    ///
1161    /// Verifies that a relative path is joined against project_root.
1162    #[test]
1163    fn resolve_env_relative_dotenv_with_project_root_resolved() {
1164        let tmp = tempfile::tempdir().expect("test tempdir");
1165        std::fs::write(tmp.path().join(".env"), b"REL_KEY=rel_val\n").expect("write .env");
1166
1167        let ctx = serde_json::json!({ "env": { "dotenv": ".env" } });
1168        let map = resolve_env(&ctx, Some(tmp.path()), None).expect("resolve_env should succeed");
1169        assert_eq!(
1170            map.get("REL_KEY").map(String::as_str),
1171            Some("rel_val"),
1172            "relative dotenv path must be resolved against project_root"
1173        );
1174    }
1175
1176    /// T2d (boundary): empty allowlist (None) means no filtering — all keys pass through.
1177    #[test]
1178    fn resolve_env_none_allowlist_keeps_all_inject_keys() {
1179        let ctx = serde_json::json!({
1180            "env": { "inject": { "A": "1", "B": "2" } }
1181        });
1182        let map = resolve_env(&ctx, None, None).expect("resolve_env should succeed");
1183        assert_eq!(
1184            map.len(),
1185            2,
1186            "all inject keys must be retained when allowlist is None"
1187        );
1188    }
1189
1190    /// T3 (error path): inject value that is not a string returns Err.
1191    ///
1192    /// Verifies that non-string inject values propagate as Result::Err (no silent drop).
1193    #[test]
1194    fn resolve_env_inject_non_string_value_errors() {
1195        let ctx = serde_json::json!({
1196            "env": { "inject": { "BAD": 42 } }
1197        });
1198        let result = resolve_env(&ctx, None, None);
1199        assert!(result.is_err(), "non-string inject value must return Err");
1200        let msg = result.unwrap_err();
1201        assert!(
1202            msg.contains("BAD"),
1203            "error must mention the offending key, got: {msg}"
1204        );
1205    }
1206
1207    /// T3b (error path): inject object that is not an object returns Err.
1208    #[test]
1209    fn resolve_env_inject_not_an_object_errors() {
1210        let ctx = serde_json::json!({
1211            "env": { "inject": ["not", "an", "object"] }
1212        });
1213        let result = resolve_env(&ctx, None, None);
1214        assert!(result.is_err(), "non-object inject value must return Err");
1215    }
1216
1217    /// T3c (error path): missing dotenv file propagates as Err (no silent skip).
1218    ///
1219    /// Verifies SOURCE boundary: dotenv I/O errors are surfaced, not swallowed.
1220    #[test]
1221    fn resolve_env_missing_dotenv_file_errors() {
1222        let ctx = serde_json::json!({
1223            "env": { "dotenv": "/nonexistent/path/to/.env" }
1224        });
1225        let result = resolve_env(&ctx, None, None);
1226        assert!(
1227            result.is_err(),
1228            "missing dotenv file must return Err, not empty map"
1229        );
1230        let msg = result.unwrap_err();
1231        assert!(
1232            msg.contains("dotenv"),
1233            "error must mention dotenv, got: {msg}"
1234        );
1235    }
1236
1237    // ── resolve_pkg_type_lua unit tests (ST2) ───────────────────────────────
1238
1239    use algocline_core::pkg::{PkgType, TypeSource};
1240
1241    /// Build a temporary package directory with the given `init.lua` content
1242    /// and return the parent directory (the one to pass as a lib_path to
1243    /// `Executor::new`).
1244    fn make_temp_pkg(parent: &std::path::Path, pkg_name: &str, init_lua: &str) -> PathBuf {
1245        let pkg_dir = parent.join(pkg_name);
1246        std::fs::create_dir_all(&pkg_dir).expect("create pkg dir");
1247        std::fs::write(pkg_dir.join("init.lua"), init_lua).expect("write init.lua");
1248        parent.to_path_buf()
1249    }
1250
1251    /// Build a minimal `AppService` whose executor can `require` packages from
1252    /// `pkg_root` (the parent directory that contains `<pkg_name>/init.lua`).
1253    async fn make_svc_with_pkg_root(pkg_root: PathBuf) -> AppService {
1254        let executor = Arc::new(
1255            algocline_engine::Executor::new(vec![pkg_root])
1256                .await
1257                .expect("executor"),
1258        );
1259        // `AppConfig::default()` (test-only) creates a fresh leaked tempdir and
1260        // sets log_enabled = false — suitable for unit tests.
1261        let log_config = super::super::config::AppConfig::default();
1262        AppService::new(executor, log_config, vec![])
1263    }
1264
1265    /// T1 (boundary): `M.run` defined + no `meta.type` → `Some((Runnable, AutoDetectedRunnable))`.
1266    ///
1267    /// When `meta.type` is absent and `pkg.run` is a function, the snippet
1268    /// sets `meta.type = "runnable"` and `meta.type_source = "auto_detected_runnable"`.
1269    #[tokio::test]
1270    async fn resolve_pkg_type_lua_returns_auto_runnable() {
1271        let tmp = tempfile::tempdir().expect("test tempdir");
1272        let pkg_root = make_temp_pkg(
1273            tmp.path(),
1274            "auto_runnable",
1275            r#"local M = {}
1276M.meta = { name = "auto_runnable" }
1277M.run = function(ctx) return "ok" end
1278return M
1279"#,
1280        );
1281        let svc = make_svc_with_pkg_root(pkg_root).await;
1282        let result = svc
1283            .resolve_pkg_type_lua("auto_runnable")
1284            .await
1285            .expect("eval must succeed");
1286        assert_eq!(
1287            result,
1288            Some((PkgType::Runnable, TypeSource::AutoDetectedRunnable)),
1289            "M.run present + no meta.type must produce (Runnable, AutoDetectedRunnable)"
1290        );
1291    }
1292
1293    /// T3 (error path): no `M.run` + no `meta.type` → `Some((Library, AutoDetectedLibrary))`.
1294    ///
1295    /// When `meta.type` is absent and `pkg.run` is not a function, the snippet
1296    /// sets `meta.type = "library"` and `meta.type_source = "auto_detected_library"`.
1297    /// This is the warn-eligible case — the crux constraint requires this specific
1298    /// `TypeSource` variant so `alc_pkg_doctor` / `alc_pkg_list` can fire the
1299    /// unmarked-library warning only for `AutoDetectedLibrary`, not for `None`.
1300    #[tokio::test]
1301    async fn resolve_pkg_type_lua_returns_auto_library() {
1302        let tmp = tempfile::tempdir().expect("test tempdir");
1303        let pkg_root = make_temp_pkg(
1304            tmp.path(),
1305            "auto_library",
1306            r#"local M = {}
1307M.meta = { name = "auto_library" }
1308-- no M.run defined
1309return M
1310"#,
1311        );
1312        let svc = make_svc_with_pkg_root(pkg_root).await;
1313        let result = svc
1314            .resolve_pkg_type_lua("auto_library")
1315            .await
1316            .expect("eval must succeed");
1317        assert_eq!(
1318            result,
1319            Some((PkgType::Library, TypeSource::AutoDetectedLibrary)),
1320            "no M.run + no meta.type must produce (Library, AutoDetectedLibrary)"
1321        );
1322    }
1323}