AQIT 0.1.0
Loading...
Searching...
No Matches
daemon_main.py
Go to the documentation of this file.
1# Copyright (c) 2025-present Aquin Labs Private Limited. All Rights Reserved.
2# This file is part of the Aquin Engine. Unauthorized copying, modification,
3# distribution, or use of this file, via any medium, is strictly prohibited.
4# Proprietary and confidential. See LICENSE for terms.
5
6"""
7Standalone persistent model daemon.
8
9Started detached by `aquin load --model` (see aquin.engine.model_daemon). It holds
10one model resident in VRAM across CLI invocations and serves generic tool dispatch
11plus model lifecycle over localhost, so commands like `aquin attention` do not
12reload the model every time.
13
14Run: python -m aquin.engine.daemon_main [--port PORT] [--model <id>]
15Default port: AQUIN_ENGINE_PORT → ~/.aquin/engine-info.json → 17832 (not 8002).
16"""
17from __future__ import annotations
18
19import argparse
20import os
21import sys
22
23
24def main() -> None:
25 from aquin.engine.engine_info import resolve_engine_port, set_daemon_state
26
27 parser = argparse.ArgumentParser(prog="aquin-daemon")
28 parser.add_argument("--port", type=int, default=None)
29 parser.add_argument("--model", default=None, help="optional model to preload")
30 ns = parser.parse_args()
31 port = ns.port if ns.port is not None else resolve_engine_port()
32
33 # Mark this process as the daemon so in-process loaders do not try to free
34 # "another" daemon's VRAM (that guard is meant for external short-lived procs).
35 os.environ["AQUIN_DAEMON"] = "1"
36
37 try:
38 from aquin.quiet import install_cli_quiet_mode
39 install_cli_quiet_mode()
40 except Exception:
41 pass
42
43 # Ensure tool stubs are registered and the loader shim is applied.
44 try:
45 from aquin.engine.tools.registry import _load_stubs
46 _load_stubs()
47 except Exception as exc:
48 print(f"[daemon] tool registry load warning: {exc}", flush=True)
49 try:
50 from aquin.compute.loader_shim import apply as _shim_apply
51 _shim_apply()
52 except Exception as exc:
53 print(f"[daemon] loader shim warning: {exc}", flush=True)
54
55 if ns.model:
56 try:
57 from aquin.compute.model_runtime import load_weights
58
59 print(f"[daemon] preloading {ns.model}...", flush=True)
60 load_weights(ns.model)
61 print("[daemon] preload complete", flush=True)
62 except Exception as exc:
63 print(f"[daemon] preload failed: {exc}", flush=True)
64
65 try:
66 set_daemon_state(
67 status="running",
68 pid=os.getpid(),
69 port=port,
70 model_id=ns.model,
71 )
72 except Exception as exc:
73 print(f"[daemon] engine-info write warning: {exc}", flush=True)
74
75 from aquin.engine.local_server import run_forever
76 try:
77 run_forever(port)
78 finally:
79 try:
80 set_daemon_state(status="stopped", pid=None, started_at=None, model_id=None)
81 except Exception:
82 pass
83
84
85if __name__ == "__main__":
86 sys.exit(main())