1use std::collections::HashMap;
2
3use algocline_core::{EngineApi, QueryResponse};
4use async_trait::async_trait;
5
6use crate::pool::{registry::with_registry_lock, PoolError, PoolRegistry};
7
8use super::list_opts::ListOpts;
9use super::AppService;
10
11#[async_trait]
18impl EngineApi for AppService {
19 async fn run(
22 &self,
23 code: Option<String>,
24 code_file: Option<String>,
25 ctx: Option<serde_json::Value>,
26 project_root: Option<String>,
27 host_mode: Option<bool>,
28 ) -> Result<String, String> {
29 AppService::run(self, code, code_file, ctx, project_root, host_mode).await
30 }
31
32 async fn advice(
33 &self,
34 strategy: &str,
35 task: Option<String>,
36 opts: Option<serde_json::Value>,
37 project_root: Option<String>,
38 ) -> Result<String, String> {
39 AppService::advice(self, strategy, task, opts, project_root).await
40 }
41
42 async fn continue_single(
43 &self,
44 session_id: &str,
45 response: String,
46 query_id: Option<&str>,
47 usage: Option<algocline_core::TokenUsage>,
48 ) -> Result<String, String> {
49 AppService::continue_single(self, session_id, response, query_id, usage).await
50 }
51
52 async fn continue_batch(
53 &self,
54 session_id: &str,
55 responses: Vec<QueryResponse>,
56 ) -> Result<String, String> {
57 AppService::continue_batch(self, session_id, responses).await
58 }
59
60 async fn status(
63 &self,
64 session_id: Option<&str>,
65 pending_filter: Option<serde_json::Value>,
66 include_history: bool,
67 ) -> Result<String, String> {
68 AppService::status(self, session_id, pending_filter, include_history).await
69 }
70
71 async fn eval(
74 &self,
75 scenario: Option<String>,
76 scenario_file: Option<String>,
77 scenario_name: Option<String>,
78 strategy: &str,
79 strategy_opts: Option<serde_json::Value>,
80 auto_card: bool,
81 ) -> Result<String, String> {
82 AppService::eval(
83 self,
84 scenario,
85 scenario_file,
86 scenario_name,
87 strategy,
88 strategy_opts,
89 auto_card,
90 )
91 .await
92 }
93
94 async fn eval_history(&self, strategy: Option<&str>, limit: usize) -> Result<String, String> {
95 AppService::eval_history(self, strategy, limit)
96 }
97
98 async fn eval_detail(&self, eval_id: &str) -> Result<String, String> {
99 AppService::eval_detail(self, eval_id)
100 }
101
102 async fn eval_compare(&self, eval_id_a: &str, eval_id_b: &str) -> Result<String, String> {
103 AppService::eval_compare(self, eval_id_a, eval_id_b).await
104 }
105
106 async fn scenario_list(&self) -> Result<String, String> {
109 AppService::scenario_list(self)
110 }
111
112 async fn scenario_show(&self, name: &str) -> Result<String, String> {
113 AppService::scenario_show(self, name)
114 }
115
116 async fn scenario_install(&self, url: String) -> Result<String, String> {
117 AppService::scenario_install(self, url).await
118 }
119
120 async fn pkg_link(
123 &self,
124 path: String,
125 name: Option<String>,
126 force: Option<bool>,
127 scope: Option<String>,
128 project_root: Option<String>,
129 ) -> Result<String, String> {
130 AppService::pkg_link(self, path, name, force, scope, project_root).await
131 }
132
133 async fn pkg_unlink(&self, name: String) -> Result<String, String> {
134 AppService::pkg_unlink(self, name).await
135 }
136
137 #[allow(clippy::too_many_arguments)]
138 async fn pkg_list(
139 &self,
140 project_root: Option<String>,
141 limit: Option<i32>,
142 sort: Option<String>,
143 filter: Option<serde_json::Value>,
144 fields: Option<Vec<String>>,
145 verbose: Option<String>,
146 ) -> Result<String, String> {
147 let filter_map = match filter {
153 None => None,
154 Some(v) => match serde_json::from_value::<HashMap<String, serde_json::Value>>(v) {
155 Ok(map) => Some(map),
156 Err(e) => {
157 tracing::warn!(error = %e, "pkg_list: filter value is not a JSON object — treating as no filter");
158 None
159 }
160 },
161 };
162
163 let opts = ListOpts {
168 limit: limit.map(|n| n.max(0) as usize),
169 sort,
170 filter: filter_map,
171 fields,
172 verbose,
173 };
174
175 AppService::pkg_list(self, project_root, opts)
176 .await
177 .map_err(|e| e.to_string())
178 }
179
180 async fn pkg_install(
181 &self,
182 url: String,
183 name: Option<String>,
184 force: Option<bool>,
185 ) -> Result<String, String> {
186 AppService::pkg_install(self, url, name, force).await
187 }
188
189 async fn pkg_remove(
190 &self,
191 name: &str,
192 project_root: Option<String>,
193 version: Option<String>,
194 scope: Option<String>,
195 ) -> Result<String, String> {
196 AppService::pkg_remove(self, name, project_root, version, scope).await
197 }
198
199 async fn pkg_repair(
200 &self,
201 name: Option<String>,
202 project_root: Option<String>,
203 ) -> Result<String, String> {
204 AppService::pkg_repair(self, name, project_root).await
205 }
206
207 async fn pkg_doctor(
208 &self,
209 name: Option<String>,
210 project_root: Option<String>,
211 ) -> Result<String, String> {
212 AppService::pkg_doctor(self, name, project_root).await
213 }
214
215 async fn add_note(
218 &self,
219 session_id: &str,
220 content: &str,
221 title: Option<&str>,
222 ) -> Result<String, String> {
223 AppService::add_note(self, session_id, content, title).await
224 }
225
226 async fn log_view(
227 &self,
228 session_id: Option<&str>,
229 limit: Option<usize>,
230 max_chars: Option<usize>,
231 ) -> Result<String, String> {
232 AppService::log_view(self, session_id, limit, max_chars).await
233 }
234
235 async fn stats(
236 &self,
237 strategy_filter: Option<&str>,
238 days: Option<u64>,
239 ) -> Result<String, String> {
240 AppService::stats(self, strategy_filter, days)
241 }
242
243 async fn init(&self, project_root: Option<String>) -> Result<String, String> {
246 AppService::init(self, project_root).await
247 }
248
249 async fn update(&self, project_root: Option<String>) -> Result<String, String> {
250 AppService::update(self, project_root).await
251 }
252
253 async fn migrate(&self, project_root: Option<String>) -> Result<String, String> {
254 AppService::migrate(self, project_root).await
255 }
256
257 async fn session_new(
260 &self,
261 project_root: Option<String>,
262 mode: Option<String>,
263 ) -> Result<String, String> {
264 let session = self.activate_session(project_root.as_deref(), mode.as_deref())?;
265 let result = serde_json::json!({
266 "session_id": session.session_id,
267 "project_root": session
268 .project_root
269 .as_ref()
270 .map(|p| p.to_string_lossy().to_string()),
271 "mode": session.mode.as_str(),
272 });
273 serde_json::to_string_pretty(&result).map_err(|e| e.to_string())
274 }
275
276 async fn card_list(&self, pkg: Option<String>) -> Result<String, String> {
279 AppService::card_list(self, pkg.as_deref())
280 }
281
282 async fn card_get(&self, card_id: &str) -> Result<String, String> {
283 AppService::card_get(self, card_id)
284 }
285
286 async fn card_find(
287 &self,
288 pkg: Option<String>,
289 where_: Option<serde_json::Value>,
290 order_by: Option<serde_json::Value>,
291 limit: Option<usize>,
292 offset: Option<usize>,
293 ) -> Result<String, String> {
294 AppService::card_find(self, pkg, where_, order_by, limit, offset)
295 }
296
297 async fn card_alias_list(&self, pkg: Option<String>) -> Result<String, String> {
298 AppService::card_alias_list(self, pkg.as_deref())
299 }
300
301 async fn card_get_by_alias(&self, name: &str) -> Result<String, String> {
302 AppService::card_get_by_alias(self, name)
303 }
304
305 async fn card_alias_set(
306 &self,
307 name: &str,
308 card_id: &str,
309 pkg: Option<String>,
310 note: Option<String>,
311 ) -> Result<String, String> {
312 AppService::card_alias_set(self, name, card_id, pkg.as_deref(), note.as_deref())
313 }
314
315 async fn card_append(
316 &self,
317 card_id: &str,
318 fields: serde_json::Value,
319 ) -> Result<String, String> {
320 AppService::card_append(self, card_id, fields)
321 }
322
323 async fn card_install(&self, url: String) -> Result<String, String> {
324 AppService::card_install(self, url).await
325 }
326
327 async fn card_samples(
328 &self,
329 card_id: &str,
330 offset: Option<usize>,
331 limit: Option<usize>,
332 where_: Option<serde_json::Value>,
333 ) -> Result<String, String> {
334 AppService::card_samples(self, card_id, offset.unwrap_or(0), limit, where_)
335 }
336
337 async fn card_lineage(
338 &self,
339 card_id: &str,
340 direction: Option<String>,
341 depth: Option<usize>,
342 include_stats: Option<bool>,
343 relation_filter: Option<Vec<String>>,
344 ) -> Result<String, String> {
345 AppService::card_lineage(
346 self,
347 card_id,
348 direction.as_deref(),
349 depth,
350 include_stats,
351 relation_filter,
352 )
353 }
354
355 async fn card_sink_backfill(&self, sink: String, dry_run: bool) -> Result<String, String> {
356 AppService::card_sink_backfill(self, super::card::SinkBackfillParams { sink, dry_run })
357 }
358
359 async fn card_analyze(&self, card_id: &str, pkg: Option<String>) -> Result<String, String> {
360 AppService::card_analyze(self, card_id, pkg).await
361 }
362
363 async fn hub_reindex(
366 &self,
367 output_path: Option<String>,
368 source_dir: Option<String>,
369 ) -> Result<String, String> {
370 let svc = self.clone();
371 tokio::task::spawn_blocking(move || {
372 AppService::hub_reindex(&svc, output_path.as_deref(), source_dir.as_deref())
373 })
374 .await
375 .map_err(|e| format!("hub_reindex task panicked: {e}"))?
376 }
377
378 async fn hub_gendoc(
379 &self,
380 source_dir: String,
381 out_dir: Option<String>,
382 projections: Option<Vec<String>>,
383 config_path: Option<String>,
384 lint_strict: Option<bool>,
385 ) -> Result<String, String> {
386 let svc = self.clone();
387 tokio::task::spawn_blocking(move || {
388 crate::AppService::hub_gendoc(
389 &svc,
390 &source_dir,
391 out_dir.as_deref(),
392 projections.as_deref(),
393 config_path.as_deref(),
394 lint_strict,
395 )
396 })
397 .await
398 .map_err(|e| format!("hub_gendoc task panicked: {e}"))?
399 }
400
401 async fn hub_dist(
402 &self,
403 source_dir: String,
404 output_path: Option<String>,
405 out_dir: Option<String>,
406 preset: Option<String>,
407 project_root: Option<String>,
408 projections: Option<Vec<String>>,
409 config_path: Option<String>,
410 lint_strict: Option<bool>,
411 ) -> Result<String, String> {
412 let svc = self.clone();
413 tokio::task::spawn_blocking(move || {
414 AppService::hub_dist(
415 &svc,
416 &source_dir,
417 output_path.as_deref(),
418 out_dir.as_deref(),
419 preset.as_deref(),
420 project_root.as_deref(),
421 projections.as_deref(),
422 config_path.as_deref(),
423 lint_strict,
424 )
425 })
426 .await
427 .map_err(|e| format!("hub_dist task panicked: {e}"))?
428 }
429
430 async fn hub_info(&self, pkg: String) -> Result<String, String> {
431 let svc = self.clone();
432 tokio::task::spawn_blocking(move || AppService::hub_info(&svc, &pkg))
433 .await
434 .map_err(|e| format!("hub_info task panicked: {e}"))?
435 }
436
437 #[allow(clippy::too_many_arguments)]
438 async fn hub_search(
439 &self,
440 query: Option<String>,
441 category: Option<String>,
442 installed_only: Option<bool>,
443 limit: Option<i32>,
444 sort: Option<String>,
445 filter: Option<serde_json::Value>,
446 fields: Option<Vec<String>>,
447 verbose: Option<String>,
448 ) -> Result<String, String> {
449 let svc = self.clone();
450
451 let filter_map = match filter {
459 None => None,
460 Some(v) => match serde_json::from_value::<HashMap<String, serde_json::Value>>(v) {
461 Ok(map) => Some(map),
462 Err(e) => {
463 tracing::warn!(error = %e, "hub_search: filter value is not a JSON object — treating as no filter");
464 None
465 }
466 },
467 };
468
469 let opts = ListOpts {
474 limit: limit.map(|n| n.max(0) as usize),
475 sort,
476 filter: filter_map,
477 fields,
478 verbose,
479 };
480
481 tokio::task::spawn_blocking(move || {
482 AppService::hub_search(
483 &svc,
484 query.as_deref(),
485 category.as_deref(),
486 installed_only,
487 opts,
488 )
489 })
490 .await
491 .map_err(|e| format!("hub_search task panicked: {e}"))?
492 }
493
494 async fn pkg_read_init_lua(&self, name: &str) -> Result<String, String> {
497 AppService::pkg_read_init_lua(self, name, None)
498 }
499
500 async fn pkg_get_narrative_md(&self, name: &str) -> Result<Option<String>, String> {
501 AppService::pkg_get_narrative_md(self, name).await
502 }
503
504 async fn pkg_meta(&self, name: &str) -> Result<String, String> {
505 let filter = serde_json::json!({ "name": name });
506 let json_str = EngineApi::pkg_list(
507 self,
508 None,
509 None,
510 None,
511 Some(filter),
512 None,
513 Some("full".to_string()),
514 )
515 .await?;
516 let val: serde_json::Value = serde_json::from_str(&json_str)
517 .map_err(|e| format!("pkg_meta: failed to parse pkg_list response: {e}"))?;
518 let pkgs = val
519 .get("packages")
520 .and_then(|p| p.as_array())
521 .ok_or_else(|| "pkg_meta: pkg_list response missing 'packages' field".to_string())?;
522 if pkgs.is_empty() {
523 return Err(format!("pkg not found: {name}"));
524 }
525 serde_json::to_string(&pkgs[0]).map_err(|e| format!("pkg_meta: serialize entry: {e}"))
526 }
527
528 async fn pkg_scaffold(
531 &self,
532 name: String,
533 target_dir: Option<String>,
534 category: Option<String>,
535 description: Option<String>,
536 ) -> Result<String, String> {
537 let svc = self.clone();
538 tokio::task::spawn_blocking(move || {
539 AppService::pkg_scaffold(
540 &svc,
541 &name,
542 target_dir.as_deref(),
543 category.as_deref(),
544 description.as_deref(),
545 )
546 })
547 .await
548 .map_err(|e| format!("pkg_scaffold task panicked: {e}"))?
549 }
550
551 async fn hub_index_aggregate(&self) -> Result<String, String> {
561 let svc = self.clone();
562 let (index, warnings) = tokio::task::spawn_blocking(move || {
563 AppService::aggregate_index(&svc).map_err(|e| e.to_string())
564 })
565 .await
566 .map_err(|e| format!("hub_index_aggregate task panicked: {e}"))??;
567
568 let mut json = serde_json::to_value(&index)
569 .map_err(|e| format!("hub_index_aggregate: serialize index: {e}"))?;
570 if !warnings.is_empty() {
571 if let Some(obj) = json.as_object_mut() {
572 obj.insert("warnings".to_string(), serde_json::json!(warnings));
573 }
574 }
575 serde_json::to_string(&json)
576 .map_err(|e| format!("hub_index_aggregate: serialize final: {e}"))
577 }
578
579 async fn info(&self) -> String {
582 AppService::info(self)
583 }
584
585 async fn pool_ensure(&self) -> Result<String, String> {
588 AppService::pool_ensure_impl(self).await
589 }
590
591 async fn pool_status(&self, sid: Option<String>) -> Result<String, String> {
592 AppService::pool_status_impl(self, sid).await
593 }
594
595 async fn pool_stop(&self, sid: Option<String>) -> Result<String, String> {
596 AppService::pool_stop_impl(self, sid).await
597 }
598}
599
600impl AppService {
603 pub(crate) async fn pool_ensure_impl(&self) -> Result<String, String> {
608 let reg_path = self.pool_reg_path.clone();
609 let lock_path = self.pool_lock_path.clone();
610
611 let sessions =
612 tokio::task::spawn_blocking(move || -> Result<Vec<serde_json::Value>, PoolError> {
613 with_registry_lock(&lock_path, || {
614 let mut reg = PoolRegistry::load_or_default(®_path)?;
615 let survivors = reg.scan_and_gc()?;
616 reg.save(®_path)?;
618 let entries = survivors
619 .iter()
620 .map(|e| {
621 serde_json::json!({
622 "sid": e.sid,
623 "pid": e.pid,
624 "sock": e.sock.to_string_lossy(),
625 "version": e.version,
626 })
627 })
628 .collect::<Vec<_>>();
629 Ok(entries)
630 })
631 })
632 .await
633 .map_err(|e| format!("pool_ensure: task panicked: {e}"))?
634 .map_err(|e| e.to_string())?;
635
636 let pool_version = env!("CARGO_PKG_VERSION");
637 serde_json::to_string(&serde_json::json!({
638 "sessions": sessions,
639 "pool_version": pool_version,
640 }))
641 .map_err(|e| format!("pool_ensure: serialize: {e}"))
642 }
643
644 pub(crate) async fn pool_status_impl(&self, sid: Option<String>) -> Result<String, String> {
649 let reg_path = self.pool_reg_path.clone();
650 let lock_path = self.pool_lock_path.clone();
651
652 let sessions =
653 tokio::task::spawn_blocking(move || -> Result<Vec<serde_json::Value>, PoolError> {
654 with_registry_lock(&lock_path, || {
655 let mut reg = PoolRegistry::load_or_default(®_path)?;
656 let _ = reg.scan_and_gc()?;
658 reg.save(®_path)?;
659
660 let entries: Vec<serde_json::Value> = reg
661 .sessions
662 .iter()
663 .filter(|e| sid.as_deref().map(|s| e.sid == s).unwrap_or(true))
664 .map(|e| {
665 serde_json::json!({
666 "sid": e.sid,
667 "pid": e.pid,
668 "sock": e.sock.to_string_lossy(),
669 "version": e.version,
670 "created_at": e.created_at,
671 "status": "running",
673 })
674 })
675 .collect();
676 Ok(entries)
677 })
678 })
679 .await
680 .map_err(|e| format!("pool_status: task panicked: {e}"))?
681 .map_err(|e| e.to_string())?;
682
683 let pool_version = env!("CARGO_PKG_VERSION");
684 serde_json::to_string(&serde_json::json!({
685 "sessions": sessions,
686 "pool_version": pool_version,
687 }))
688 .map_err(|e| format!("pool_status: serialize: {e}"))
689 }
690
691 pub(crate) async fn pool_stop_impl(&self, sid: Option<String>) -> Result<String, String> {
697 let reg_path = self.pool_reg_path.clone();
698 let lock_path = self.pool_lock_path.clone();
699
700 let result = tokio::task::spawn_blocking(
701 move || -> Result<(Vec<String>, Vec<String>), PoolError> {
702 with_registry_lock(&lock_path, || {
703 let mut reg = PoolRegistry::load_or_default(®_path)?;
704
705 let targets: Vec<_> = reg
707 .sessions
708 .iter()
709 .filter(|e| sid.as_deref().map(|s| e.sid == s).unwrap_or(true))
710 .cloned()
711 .collect();
712
713 let mut stopped: Vec<String> = Vec::new();
714 let mut errors: Vec<String> = Vec::new();
715
716 for entry in &targets {
717 #[cfg(unix)]
718 {
719 let pid_t = match i32::try_from(entry.pid) {
723 Ok(p) if p > 0 => p,
724 Ok(_) => {
725 errors.push(format!(
726 "sid={}: pid={} is not a valid POSIX target pid (must be > 0); skipping SIGTERM",
727 entry.sid, entry.pid
728 ));
729 reg.remove(&entry.sid);
730 continue;
731 }
732 Err(_) => {
733 errors.push(format!(
734 "sid={}: pid={} exceeds i32::MAX, cannot send SIGTERM (K-52)",
735 entry.sid, entry.pid
736 ));
737 reg.remove(&entry.sid);
739 continue;
740 }
741 };
742
743 let ret = unsafe { libc::kill(pid_t, libc::SIGTERM) };
747 if ret == 0 {
748 stopped.push(entry.sid.clone());
749 } else {
750 let os_err = std::io::Error::last_os_error();
751 if os_err.raw_os_error() == Some(libc::ESRCH) {
752 stopped.push(entry.sid.clone());
754 } else {
755 errors.push(format!(
756 "sid={}: SIGTERM failed: {}",
757 entry.sid, os_err
758 ));
759 }
760 }
761 }
762 #[cfg(not(unix))]
763 {
764 errors.push(format!(
766 "sid={}: SIGTERM not supported on this platform",
767 entry.sid
768 ));
769 }
770 reg.remove(&entry.sid);
773 }
774
775 reg.save(®_path)?;
777
778 Ok((stopped, errors))
779 })
780 },
781 )
782 .await
783 .map_err(|e| format!("pool_stop: task panicked: {e}"))?
784 .map_err(|e| e.to_string())?;
785
786 let (stopped, errors) = result;
787 serde_json::to_string(&serde_json::json!({
788 "stopped": stopped,
789 "errors": errors,
790 }))
791 .map_err(|e| format!("pool_stop: serialize: {e}"))
792 }
793}
794
795#[cfg(test)]
798mod tests {
799 use super::super::test_support::make_app_service_at;
800
801 #[tokio::test]
808 #[cfg(unix)]
809 async fn pool_stop_pid_zero_is_rejected() {
810 let tmp = tempfile::tempdir().expect("tempdir");
813 let root = tmp.path().to_path_buf();
814 let svc = make_app_service_at(root.clone()).await;
815
816 let pool_reg_path = root.join("state").join("pool").join("registry.json");
819 std::fs::create_dir_all(pool_reg_path.parent().unwrap()).expect("create pool dir");
820
821 let seeded = serde_json::json!({
822 "sessions": [{
823 "sid": "zero-pid-session",
824 "pid": 0u32,
825 "sock": "/tmp/alc-pool/zero.sock",
826 "version": "0.30.0",
827 "created_at": "2026-01-01T00:00:00Z"
828 }]
829 });
830 std::fs::write(&pool_reg_path, seeded.to_string()).expect("seed registry.json");
831
832 let json_str = svc.pool_stop_impl(None).await.expect("pool_stop_impl");
834 let result: serde_json::Value =
835 serde_json::from_str(&json_str).expect("response is valid JSON");
836
837 let errors = result["errors"].as_array().expect("errors array");
839 assert!(
840 !errors.is_empty(),
841 "expected at least one error for pid=0 entry"
842 );
843 let err_msg = errors[0].as_str().unwrap_or("");
844 assert!(
845 err_msg.contains("not a valid POSIX target pid"),
846 "unexpected error message: {err_msg}"
847 );
848
849 let stopped = result["stopped"].as_array().expect("stopped array");
851 assert!(
852 stopped.is_empty(),
853 "pid=0 entry must not appear in stopped list"
854 );
855
856 let on_disk: serde_json::Value =
858 serde_json::from_str(&std::fs::read_to_string(&pool_reg_path).expect("read registry"))
859 .expect("parse registry");
860 let sessions = on_disk["sessions"].as_array().expect("sessions array");
861 assert!(
862 sessions.is_empty(),
863 "pid=0 entry must be removed from on-disk registry"
864 );
865
866 }
869}