conglinyizhi/screen-kacha/moonutil does not have a README file

    RuntimePaths

    pub struct RuntimePaths {
    state_dir : String?
    access_conf : String
    runtime_dir : String
    app_runtime_dir : String
    }

    screen-kacha 使用的用户路径。CLI 显式路径(如 --shm、--data、--access)不在这里。

    TrayOwner

    pub struct TrayOwner {
    pid : Int
    script : String
    helper : Int
    ts_ms : Int64
    miss : Int
    }

    托盘的心跳记录。

    托盘每 5 秒整份重写一次(跟着它的 tick),服务读它判断托盘还在不在。 放在 moonutil 而不是两边各写一份:格式只有一种,写方(托盘)和读方 (服务)不会各自演化出一个不兼容的版本。

    TrayOwner::new

    fn TrayOwner::new(pid : Int, script : String, helper : Int, ts_ms : Int64, miss : Int) -> TrayOwner

    造一条记录。

    字段对别的包是只读的,所以得走这个构造器 —— 读的人不该随手改出 一条自相矛盾的记录(比如换了 pid 却没换脚本名)。

    TrayOwner::parse

    fn TrayOwner::parse(text : String) -> TrayOwner?

    解析心跳记录。

    pid 或 ts_ms 缺失、不是数字、不是正数,一律返回 None: 没有 pid 没法清场,没有时间戳判不了死活,半条记录比没有更危险 (比如 pid 读成 0 还照着它去 kill,kill 的是整个进程组)。

    其余字段宽松处理:script 缺失就是空串,helper / miss 缺失或 读成垃圾、负数,都退到 0 —— 这两处「什么都不做」正是对的默认。

    TrayOwner::render

    fn TrayOwner::render(self : TrayOwner) -> String

    序列化成逐行 key=value。

    逐行的理由:cat 一下就能看懂,坏一行也只坏一个字段; 而且托盘的字段和服务写的 miss 分开在两行,谁改哪一行一眼就看得出来。

    TRAY_OWNER_MAX_BYTES

    let TRAY_OWNER_MAX_BYTES : Int

    一条记录的字符数上限。

    读的时候超过这个长度就整条丢掉:tray.owner 只有五个短字段, 长成这样说明读到的是别的东西(写坏的、别的进程占了这个路径), 拿去判死活只会得出奇怪的结论。

    access_conf_path

    fn access_conf_path() -> String

    access_conf_path_with

    fn access_conf_path_with(xdg : String?, home : String?) -> String

    访问控制配置文件的路径。

    认 XDG_CONFIG_HOME:服务读、托盘写,两边必须算到同一个路径,所以这段 逻辑只能有一份。

    b64_encode

    fn b64_encode(data : Bytes) -> String

    标准 base64 编码。

    MCP 的图片内容块要 base64 字符串,生成器那边也要用同一份实现。

    cstr

    fn cstr(s : String) -> Bytes

    MoonBit 字符串 → 带结尾 0 的 C 字符串。

    这个文件里三样东西原来是每个包各抄一份:cstr 五份、parse_int 三份、 pick 两份。抄得越多,改一处漏三处的机会越大。

    current_uid

    fn current_uid() -> Int

    error_log_path

    fn error_log_path() -> String?

    出错记录文件的位置。只有这一份,不存在第二个。

    instance_error_log_path

    fn instance_error_log_path(instance : String) -> String?

    instance_state_dir

    fn instance_state_dir(instance : String) -> String?

    instance_state_dir_with

    fn instance_state_dir_with(xdg_state : String?, home : String?, instance : String?) -> String?

    now_ms

    fn now_ms() -> Int64

    当前时间:自 epoch 的毫秒数。

    托盘和服务要拿同一个时钟比「心跳过去多久了」。托盘是同步循环,用不了 async 包里的 @async.now();两边都调这一个函数,就不可能出现 「一边写 CLOCK_MONOTONIC、一边读墙上时钟」那种负值。

    底层是 @env.now(),和 @async.now() 同一个来源(epoch 毫秒)。

    parse_flag

    fn parse_flag(v : String) -> Bool

    配置里的布尔值。大小写不敏感,认 true/1/yes/on。

    服务和托盘原来各有一份实现(parse_flag_value 和 flag_value), 规则一样、名字不同,改一处忘另一处就是两边行为分叉。

    parse_int

    fn parse_int(s : StringView, dflt : Int) -> Int

    解析十进制整数,非法返回默认值。

    严格:空串、带符号、夹了别的东西一律算非法。命令行参数写错了就该退回 默认值,而不是把 12a 读成 12。

    pick

    fn pick(m :
    Matches
    , name : String, dflt : String) -> String

    从命令行匹配结果里取选项值,没给就用默认值。

    runtime_dir_with

    fn runtime_dir_with(xdg : String?, uid : Int) -> String

    runtime_paths

    fn runtime_paths() -> RuntimePaths

    runtime_paths_with

    fn runtime_paths_with(xdg_state : String?, xdg_config : String?, xdg_runtime : String?, home : String?, uid : Int) -> RuntimePaths

    state_dir

    fn state_dir() -> String?

    state_dir_with

    fn state_dir_with(xdg : String?, home : String?) -> String?

    用户状态目录:$XDG_STATE_HOME/screen-kacha,否则 $HOME/.local/state/screen-kacha。

    拿不到就返回 None。不要在这里再编一个 /tmp 兜底:多一份文件就多一个 排查时看错的地方,不如干脆不写,界面上照常显示这次的错误。

    write_atomic

    fn write_atomic(path : String, data : Bytes) -> Int

    原子写一段字节。

    write_atomic_text

    fn write_atomic_text(path : String, text : String) -> Int

    原子写一段文本(按 UTF-8 编码)。配置文件、端口号这种都是文本。