algocline_app/service/mod.rs
1pub(crate) mod alc_toml;
2mod error;
3pub(crate) use error::{
4 HubRegistriesError, PkgListError, ProjectFilesError, ServiceError, TranscriptError,
5};
6mod card;
7mod config;
8mod dist;
9mod engine_api_impl;
10mod eval;
11mod eval_store;
12mod execution_service_impl;
13pub(crate) mod gendoc;
14mod hub;
15pub mod hub_dist_preset;
16mod init;
17pub(crate) mod list_opts;
18pub(crate) mod lock;
19pub(crate) mod lockfile;
20mod logging;
21pub(crate) mod manifest;
22mod migrate;
23pub(crate) mod path;
24mod pkg;
25mod pkg_link;
26mod pkg_scaffold;
27mod pkg_unlink;
28pub(crate) mod project;
29pub mod resolve;
30mod run;
31mod scenario;
32pub(crate) mod session;
33pub(crate) mod source;
34mod status;
35mod transcript;
36mod update;
37
38#[cfg(test)]
39mod test_support;
40#[cfg(test)]
41mod tests;
42
43use std::path::Path;
44use std::sync::Arc;
45
46use crate::pool::{registry::with_registry_lock, PoolError, PoolRegistry};
47use algocline_engine::{Executor, FileCardStore, JsonFileStore, SessionRegistry, VariantPkg};
48
49pub use algocline_core::{EngineApi, TokenUsage};
50pub use config::{AppConfig, LogDirSource};
51pub use resolve::{QueryResponse, SearchPath};
52
53// ─── Application Service ────────────────────────────────────────
54
55/// Tracks in-flight eval sessions: session_id → strategy name.
56///
57/// Kept between `alc_eval` invocation and eventual completion (which may
58/// arrive via `alc_continue` after LLM round-trips). Used by
59/// `run.rs::maybe_save_eval` to persist the result to `~/.algocline/evals/`.
60/// Card emission is handled by `alc.eval()` Lua-side — no Rust tracking needed.
61///
62/// `std::sync::Mutex` is used (not tokio) because all operations are
63/// single HashMap insert/remove/get completing in microseconds, and no
64/// `.await` is held across the lock. Poison is silently skipped.
65type EvalSessions = std::sync::Mutex<std::collections::HashMap<String, String>>;
66
67/// Tracks session_id → strategy name for all strategy-based sessions (advice, eval).
68///
69/// Same locking rationale as `EvalSessions`. Used by `alc_status` and
70/// transcript logging. Poison is silently skipped — strategy name is
71/// non-critical metadata for observability.
72type SessionStrategies = std::sync::Mutex<std::collections::HashMap<String, String>>;
73
74#[derive(Clone)]
75pub struct AppService {
76 executor: Arc<Executor>,
77 registry: Arc<SessionRegistry>,
78 /// V2 execution registry for [`algocline_core::execution::ExecutionService`] impl.
79 ///
80 /// Coexists with the legacy `registry` field; legacy paths continue to use
81 /// `registry`, new paths (`execution_service_impl.rs`) use this field.
82 /// `Arc` makes `Clone` cheap.
83 pub(crate) execution_registry: Arc<algocline_engine::execution::SessionRegistryV2>,
84 log_config: AppConfig,
85 /// Package search paths in priority order (first = highest).
86 search_paths: Vec<resolve::SearchPath>,
87 /// Persistent KV store backing `alc.state.*`.
88 ///
89 /// Rooted at `log_config.app_dir().state_dir()` and resolved once at
90 /// construction; `Arc`-wrapped so per-session clones are cheap.
91 state_store: Arc<JsonFileStore>,
92 /// Card store backing `alc.card.*`.
93 ///
94 /// Rooted at `log_config.app_dir().cards_dir()`, same `Arc` pattern.
95 card_store: Arc<FileCardStore>,
96 /// session_id → strategy name for eval sessions (cleared on completion).
97 eval_sessions: Arc<EvalSessions>,
98 /// session_id → strategy name for log/stats tracking (cleared on session completion).
99 session_strategies: Arc<SessionStrategies>,
100 /// Pool worker registry (persistent, backed by registry.json).
101 ///
102 /// `RwLock` because multiple concurrent callers may check for pool sessions
103 /// while a single writer spawns a new worker. The lock is never held across
104 /// an `.await` boundary (K-4: clone-then-release pattern).
105 pub(crate) pool_registry: Arc<tokio::sync::RwLock<PoolRegistry>>,
106 /// Filesystem paths for pool registry management.
107 ///
108 /// Stored here so `run.rs` / `engine_api_impl.rs` can reach them without
109 /// re-computing from `AppConfig` on every call.
110 pub(crate) pool_reg_path: std::path::PathBuf,
111 pub(crate) pool_lock_path: std::path::PathBuf,
112 pub(crate) pool_dir: std::path::PathBuf,
113 /// Activated session pin for `alc_session_new` (#1776627475). For
114 /// stdio MCP transport this is functionally a per-connection
115 /// pin (one process = one connection). `None` when no session
116 /// has been activated; callers fall back to the existing
117 /// `resolve_project_root` chain (P > E > W).
118 ///
119 /// `std::sync::Mutex` because all access is a single
120 /// load/store completing in microseconds, with no `.await` held
121 /// across the lock. Poison maps to "no session pin" so a
122 /// poisoned lock degrades to legacy behaviour rather than
123 /// breaking every subsequent tool call.
124 pub(crate) session: Arc<std::sync::Mutex<Option<session::AlcSession>>>,
125}
126
127impl AppService {
128 pub fn new(
129 executor: Arc<Executor>,
130 log_config: AppConfig,
131 search_paths: Vec<resolve::SearchPath>,
132 ) -> Self {
133 let registry = Arc::new(SessionRegistry::new());
134 // TTL = 3 hours. Complex strategies may run 30–60 min; 3h covers
135 // legitimate paused sessions while eventually reclaiming abandoned ones.
136 registry.spawn_gc_task(std::time::Duration::from_secs(10800));
137
138 let app_dir = log_config.app_dir();
139 let state_store = Arc::new(JsonFileStore::new(app_dir.state_dir()));
140 let card_store = Arc::new(FileCardStore::new(app_dir.cards_dir()));
141
142 // V2 execution registry — shares the Executor + AppConfig-derived
143 // storage paths with the legacy `start_and_tick` path so a v2 caller
144 // produces the same on-disk side effects as a legacy caller.
145 let execution_registry = Arc::new(algocline_engine::execution::SessionRegistryV2::new(
146 Arc::clone(&executor),
147 Arc::clone(&state_store),
148 Arc::clone(&card_store),
149 app_dir.scenarios_dir(),
150 ));
151
152 // ─── Pool registry setup ───────────────────────────────────────────────
153 // Paths: ~/.algocline/state/pool/{registry.json, registry.lock}
154 // No pool_dir() helper in AppDir (not in scope); derive manually.
155 let pool_dir = app_dir.state_dir().join("pool");
156 let pool_reg_path = pool_dir.join("registry.json");
157 let pool_lock_path = pool_dir.join("registry.lock");
158
159 // Startup GC: remove dead worker entries accumulated from previous
160 // MCP sessions. Runs synchronously in new() (called before any
161 // tokio tasks spawn) so spawn_blocking is not needed (K-110).
162 //
163 // If GC fails (corrupt registry, lock I/O error), we start with an
164 // empty registry and emit a tracing::warn. This is justified for
165 // startup housekeeping only: the worker processes themselves remain
166 // alive — they accumulate as orphans until the next restart, which
167 // is acceptable for a POC. A corrupt registry.json on startup is
168 // surfaced via the tracing warn so operators can investigate.
169 //
170 // NOTE: this is the ONLY place in AppService that uses
171 // tracing::warn without propagating to MCP wire. It is defensible
172 // because new() has no return type capable of carrying the error, and
173 // GC failure has no correctness impact on the current session.
174 let pool_registry = match with_registry_lock(&pool_lock_path, || {
175 let mut reg = PoolRegistry::load_or_default(&pool_reg_path)?;
176 let _ = reg.scan_and_gc()?;
177 reg.save(&pool_reg_path)?;
178 Ok::<_, PoolError>(reg)
179 }) {
180 Ok(reg) => reg,
181 Err(e) => {
182 tracing::warn!("pool registry startup GC failed (workers may accumulate): {e}");
183 PoolRegistry::default()
184 }
185 };
186
187 Self {
188 executor,
189 registry,
190 execution_registry,
191 log_config,
192 search_paths,
193 state_store,
194 card_store,
195 eval_sessions: Arc::new(std::sync::Mutex::new(std::collections::HashMap::new())),
196 session_strategies: Arc::new(std::sync::Mutex::new(std::collections::HashMap::new())),
197 pool_registry: Arc::new(tokio::sync::RwLock::new(pool_registry)),
198 pool_reg_path,
199 pool_lock_path,
200 pool_dir,
201 session: Arc::new(std::sync::Mutex::new(None)),
202 }
203 }
204
205 /// Activate (or replace) the session pin. Returns the new
206 /// `AlcSession`. See `session::AlcSession` for lifecycle
207 /// semantics. Wired through `EngineApi::session_new` to the
208 /// MCP `alc_session_new` tool.
209 pub(crate) fn activate_session(
210 &self,
211 project_root: Option<&str>,
212 mode: Option<&str>,
213 ) -> Result<session::AlcSession, String> {
214 let pinned = project::resolve_project_root(project_root);
215 let mode = session::SessionMode::parse(mode)?;
216 let new = session::AlcSession::new(pinned, mode);
217 let mut guard = self
218 .session
219 .lock()
220 .map_err(|_| "alc_session_new: session lock poisoned".to_string())?;
221 *guard = Some(new.clone());
222 Ok(new)
223 }
224
225 /// Snapshot the currently activated session, if any.
226 ///
227 /// Returns `None` when no session has been activated or when
228 /// the session lock is poisoned (degrades to legacy
229 /// `resolve_project_root` behaviour rather than failing every
230 /// subsequent tool call).
231 pub(crate) fn current_session(&self) -> Option<session::AlcSession> {
232 self.session.lock().ok().and_then(|g| g.clone())
233 }
234
235 /// Resolve the project root for a tool call, consulting the
236 /// activated session pin between the explicit per-call argument
237 /// and the `ALC_PROJECT_ROOT` env var (issue #1776627475 §6:
238 /// P > S > E > W).
239 ///
240 /// MCP tool entry points should use this instead of
241 /// `project::resolve_project_root` so the activated session
242 /// pin is honoured uniformly. Lower-level free functions
243 /// (hub_dist_preset, etc.) continue to use the legacy free
244 /// helper because their callers have already routed through
245 /// this method when invoked from the AppService layer.
246 pub(crate) fn resolve_root(&self, explicit: Option<&str>) -> Option<std::path::PathBuf> {
247 let session_pin = self.current_session().and_then(|s| s.project_root);
248 project::resolve_project_root_with_session(explicit, session_pin.as_deref())
249 }
250
251 /// Returns the log directory, or an error if file logging is unavailable.
252 fn require_log_dir(&self) -> Result<&Path, String> {
253 self.log_config
254 .log_dir
255 .as_deref()
256 .ok_or_else(|| "File logging is not available (no writable log directory)".to_string())
257 }
258
259 /// Resolve extra lib paths for a request.
260 ///
261 /// Merges two layers in priority order (first = highest = prepended
262 /// by the Executor to `package.path`):
263 ///
264 /// 1. `alc.local.toml` path entries — worktree-scoped override
265 /// (git-ignored, not persisted to alc.lock, loaded every call).
266 /// 2. `alc.lock` path entries — alc.toml-derived, git-managed.
267 ///
268 /// Returns `(paths, warnings)`. An empty `paths` is returned when no project
269 /// root is found. Corruption errors (parse failures) are returned as warning
270 /// strings in the second element rather than dropped silently — callers are
271 /// responsible for surfacing them on the MCP wire response. File-absent is
272 /// `Ok(None)` in the underlying loaders and produces no warning.
273 pub(crate) fn resolve_extra_lib_paths(
274 &self,
275 project_root: Option<&str>,
276 ) -> (Vec<std::path::PathBuf>, Vec<String>) {
277 let Some(root) = self.resolve_root(project_root) else {
278 return (vec![], vec![]);
279 };
280
281 let mut warnings: Vec<String> = Vec::new();
282
283 // Local override layer (highest priority) — merged every call,
284 // never persisted to alc.lock (decisions.md FsResolver priority).
285 let local_paths: Vec<std::path::PathBuf> = match alc_toml::load_alc_local_toml(&root) {
286 Ok(Some(local)) => alc_toml::resolve_local_path_entries(&root, &local),
287 Ok(None) => Vec::new(),
288 Err(e) => {
289 warnings.push(format!(
290 "failed to load alc.local.toml at {}: {e}",
291 root.display()
292 ));
293 Vec::new()
294 }
295 };
296
297 // Existing alc.lock layer.
298 let lock_paths: Vec<std::path::PathBuf> = match lockfile::load_lockfile(&root) {
299 Ok(Some(lock)) => {
300 self.warn_toml_lock_mismatch(&root, &lock);
301 let (paths, path_warnings) = lockfile::resolve_path_entries(&root, &lock);
302 warnings.extend(path_warnings);
303 paths
304 }
305 Ok(None) => Vec::new(),
306 Err(e) => {
307 warnings.push(format!(
308 "failed to load alc.lock at {}: {e}",
309 root.display()
310 ));
311 Vec::new()
312 }
313 };
314
315 let mut merged = local_paths;
316 merged.extend(lock_paths);
317 (merged, warnings)
318 }
319
320 /// Resolve variant pkg overrides for a request.
321 ///
322 /// Reads `alc.local.toml` (worktree-scoped, gitignored) and emits one
323 /// [`VariantPkg`] per `[packages.{name}] path = "..."` entry, preserving
324 /// the explicit `(name, pkg_dir)` mapping. Returns `(pkgs, warnings)` —
325 /// an empty `pkgs` when no project root is found or `alc.local.toml` is
326 /// absent. Corruption (parse failures) is returned as a warning string so
327 /// callers can surface it on the MCP wire response.
328 pub(crate) fn resolve_variant_pkgs(
329 &self,
330 project_root: Option<&str>,
331 ) -> (Vec<VariantPkg>, Vec<String>) {
332 let Some(root) = self.resolve_root(project_root) else {
333 return (vec![], vec![]);
334 };
335
336 match alc_toml::load_alc_local_toml(&root) {
337 Ok(Some(local)) => (alc_toml::resolve_local_variant_pkgs(&root, &local), vec![]),
338 Ok(None) => (Vec::new(), vec![]),
339 Err(e) => {
340 let msg = format!("failed to load alc.local.toml at {}: {e}", root.display());
341 (Vec::new(), vec![msg])
342 }
343 }
344 }
345
346 fn warn_toml_lock_mismatch(&self, root: &Path, lock: &lockfile::LockFile) {
347 let toml = match alc_toml::load_alc_toml(root) {
348 Ok(Some(t)) => t,
349 _ => return,
350 };
351
352 use std::collections::BTreeSet;
353 let toml_names: BTreeSet<&str> = toml.packages.keys().map(|s| s.as_str()).collect();
354 let lock_names: BTreeSet<&str> = lock.packages.iter().map(|p| p.name.as_str()).collect();
355
356 for name in toml_names.difference(&lock_names) {
357 eprintln!(
358 "warning: '{name}' is declared in alc.toml but missing from alc.lock. Run `alc_update` to sync."
359 );
360 }
361 for name in lock_names.difference(&toml_names) {
362 eprintln!("warning: '{name}' is in alc.lock but not declared in alc.toml.");
363 }
364 }
365}