Commit 59528205905a802051df711177bbc6dfab33bc48

Authored by yibo
1 parent f36e799a

docs(hermes/lifelog): api_server 纪要后端 + token 精简 + service.d 自启

记录 2026-06-04 打通的 lifelog 纪要端到端链路与两项优化:

- HERMES_AGENT_PIXEL_SETUP.md 新增 §3「作为 lifelog 纪要后端」:
  开 api_server(API_SERVER_* env,key 与 lifelog 一致)、精简 token
  (platform_toolsets.api_server=[] + skills.platform_disabled → 12,500→399)、
  Magisk service.d 自启(su 在 /system_ext/bin/su 的坑、动态 uid)、端到端验证
- HANDOFF.md §五:第5项 gateway 自启标记完成(service.d,非 Termux:Boot),
  补 token 精简必做项、Kimi provider 配置交叉引用、api_server 出口被掐排错

API key 用占位符,未写真实凭证。

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
lifelog-recorder/HANDOFF.md
@@ -166,7 +166,7 @@ VAD 参数(在 `RecorderService.runPipeline` 里调): @@ -166,7 +166,7 @@ VAD 参数(在 `RecorderService.runPipeline` 里调):
166 2. **设置页**:存储用量、模型切换、provider 切换、暂停时段配置(v1 定的 MVP 是「一键暂停就够」,但 v2 可能要细化)。 166 2. **设置页**:存储用量、模型切换、provider 切换、暂停时段配置(v1 定的 MVP 是「一键暂停就够」,但 v2 可能要细化)。
167 3. **多 anchor 平均**:现在 enroll 是单段单 anchor,可以「连选多段都标 我」自动算平均 embedding 提高匹配率。 167 3. **多 anchor 平均**:现在 enroll 是单段单 anchor,可以「连选多段都标 我」自动算平均 embedding 提高匹配率。
168 4. **回链 lemonPCRM**:Transcript 提到的人名 / 项目 → GraphStore `MENTIONED_IN` 边。 168 4. **回链 lemonPCRM**:Transcript 提到的人名 / 项目 → GraphStore `MENTIONED_IN` 边。
169 -5. **hermes gateway 自启**:当前需用户手动跑 `bash ~/start_gw.sh`,可写到 Termux:Boot 启动脚本。 169 +5. ✅ **hermes gateway 自启**(2026-06-04 完成):用 Magisk `service.d` 脚本开机拉起(Termux:Boot 未装,root 方案更稳),见下方「§ 自启(Magisk service.d)」。
170 170
171 ### hermes gateway 设置(v0.5 关键前置) 171 ### hermes gateway 设置(v0.5 关键前置)
172 172
@@ -178,8 +178,8 @@ API_SERVER_PORT=8642 @@ -178,8 +178,8 @@ API_SERVER_PORT=8642
178 API_SERVER_KEY=<openssl rand -hex 16> 178 API_SERVER_KEY=<openssl rand -hex 16>
179 API_SERVER_MODEL_NAME=kimi-for-coding 179 API_SERVER_MODEL_NAME=kimi-for-coding
180 180
181 -# 需要 anthropic SDK(kimi-for-coding 协议层走 Anthropic-compat)  
182 -~/hermes-agent/venv/bin/pip install -i https://pypi.tuna.tsinghua.edu.cn/simple/ 'anthropic>=0.39.0' 181 +# anthropic SDK:NousResearch hermes-agent 的 .[termux-all] 已自带(v0.15.1 = anthropic 0.87);
  182 +# 旧版若缺再补:~/.hermes/hermes-agent/venv/bin/pip install 'anthropic>=0.39.0'
183 183
184 # 启动:必须用 setsid + disown 才能在 adb shell 之外存活 184 # 启动:必须用 setsid + disown 才能在 adb shell 之外存活
185 cat > ~/start_gw.sh <<'EOF' 185 cat > ~/start_gw.sh <<'EOF'
@@ -196,8 +196,42 @@ chmod 755 ~/start_gw.sh @@ -196,8 +196,42 @@ chmod 755 ~/start_gw.sh
196 bash ~/start_gw.sh 196 bash ~/start_gw.sh
197 ``` 197 ```
198 198
199 -lifelog 的 `keys.properties` 里 `hermes.apiUrl` / `hermes.apiKey` 要跟这里  
200 -对得上才能调通。`network_security_config.xml` 已开 127.0.0.1 cleartext 豁免。 199 +lifelog 的 `keys.properties` 里 `hermes.apiUrl`(http://127.0.0.1:8642) / `hermes.apiKey`
  200 +要跟 `API_SERVER_KEY` 对得上才能调通。`network_security_config.xml` 已开 127.0.0.1 cleartext 豁免。
  201 +
  202 +> [!IMPORTANT] hermes 的 Kimi provider 配置(v0.5 实测)
  203 +> hermes-agent(NousResearch)原生支持 Kimi Coding Plan,配成 `model.provider=kimi-coding` +
  204 +> `model.base_url=https://api.kimi.com/coding` + `model.default=kimi-for-coding`,`.env` 放
  205 +> `KIMI_API_KEY=sk-kimi-...`。运行时自动切 Anthropic 协议 + 注入 claude-code UA。
  206 +> 完整流程见 `wechat_plain/scripts/local-testkit/docs/HERMES_AGENT_PIXEL_SETUP.md`。
  207 +
  208 +### ⚠️ 必做:精简 api_server 的 token 开销(2026-06-04 实测)
  209 +
  210 +api_server **默认走 hermes 完整 agent**,每次纪要 prompt 注入 tools(37.8KB/17工具) + skills index(9.1KB)
  211 += **~12,500 token**,纯属浪费(纪要只要纯文本总结)。只对 api_server 关掉 tools+skills(不影响 CLI/微信 bot):
  212 +
  213 +```yaml
  214 +# ~/.hermes/config.yaml
  215 +platform_toolsets:
  216 + api_server: [] # 关 tools
  217 +skills:
  218 + platform_disabled:
  219 + api_server: [<列出 ~/.hermes/skills 下所有 skill 名>] # 关 skills index
  220 +```
  221 +用 hermes 自己的 config 模块改最稳(`load_config`/`save_config`,保留其他配置)。改完重启 gateway。
  222 +**实测:prompt 12,500 → 399 token(-97%),纪要正常生成。**
  223 +
  224 +### 自启(Magisk service.d)
  225 +
  226 +Termux:Boot 未装(且无匹配签名 APK);已 root,用 Magisk service.d 更稳(不需开 Termux app)。
  227 +`/data/adb/service.d/start-hermes-gw.sh`(root, 755):等 `boot_completed` → sleep 25 →
  228 +动态推导 Termux uid + su 路径(`/system_ext/bin/su`,**不是** `/system/bin/su`)→ 以 Termux 用户
  229 +(带 `-G 3003 -G 9997 -G 20<L> -G 50<L>`)跑 `~/start_gw.sh`。实测手动执行 `launched rc=0`,
  230 +gateway 以 `u0_a302` 属主起来、api_server 正常。**冷启动慢 ~3–5 分钟**(初始化时连被墙的
  231 +openrouter/nous metadata 超时),对开机服务可接受。
  232 +
  233 +> 验证端到端:`adb shell "su -g 10302 -G 3003 -G 9997 -G 20302 -G 50302 10302 -c '…venv/bin/python ~/test_api.py'"`
  234 +> 或 lifelog 纪要 tab「现在跑一次」(需最近 1h 有 ≥60s 语音)。
201 235
202 ### 存储预算(v0.2 后) 236 ### 存储预算(v0.2 后)
203 237
wechat_plain/scripts/local-testkit/docs/HERMES_AGENT_PIXEL_SETUP.md
@@ -143,6 +143,67 @@ hermes -z &quot;用中文只回答一个数字: 1+1=?&quot; @@ -143,6 +143,67 @@ hermes -z &quot;用中文只回答一个数字: 1+1=?&quot;
143 | `HTTP 403 access_terminated_error` | 走了 OpenAI `/chat/completions` 路径;必须 kimi-coding(anthropic 协议,hermes 自动加 claude-code UA) | 143 | `HTTP 403 access_terminated_error` | 走了 OpenAI `/chat/completions` 路径;必须 kimi-coding(anthropic 协议,hermes 自动加 claude-code UA) |
144 | `no final response was produced` | config 残留错的 `model.base_url`(如 openrouter)或 provider 不对;按 2.2 重设三项 | 144 | `no final response was produced` | config 残留错的 `model.base_url`(如 openrouter)或 provider 不对;按 2.2 重设三项 |
145 | `curl: Could not resolve host` | `su` 切 uid 时漏了 `-G 3003`(inet 组) | 145 | `curl: Could not resolve host` | `su` 切 uid 时漏了 `-G 3003`(inet 组) |
  146 +| `APIConnectionError / Connection error`(api_server 返回 200 但内容报错) | 出口被掐:VPN(ClashMeta) 死节点把 TCP 全断,或 WiFi 没真连上(`UnderlyingNetworks: []`)。停 VPN / 修网即恢复 |
  147 +
  148 +---
  149 +
  150 +## 3. 作为 lifelog 纪要后端:开 api_server + 省 token + 自启
  151 +
  152 +hermes gateway 内建一个 OpenAI 兼容的 **api_server**,lifelog 的 SummaryWorker 用它跑纪要
  153 +(`POST http://127.0.0.1:8642/v1/chat/completions`)。2026-06-04 实测打通。
  154 +
  155 +### 3.1 开 api_server
  156 +
  157 +`~/.hermes/.env` 追加(`API_SERVER_KEY` **必须**等于 lifelog `keys.properties` 的 `hermes.apiKey`):
  158 +```
  159 +API_SERVER_ENABLED=true
  160 +API_SERVER_HOST=127.0.0.1
  161 +API_SERVER_PORT=8642
  162 +API_SERVER_KEY=<与 lifelog hermes.apiKey 一致>
  163 +API_SERVER_MODEL_NAME=kimi-for-coding
  164 +```
  165 +启动:`hermes gateway run`(前台);常驻用 `~/start_gw.sh`(`setsid hermes gateway run` + disown,
  166 +先 `set -a; . ~/.hermes/.env; set +a`)。监听后日志:`[Api_Server] API server listening on http://127.0.0.1:8642`。
  167 +
  168 +### 3.2 ⚠️ 必做:精简 token(12,500 → 399)
  169 +
  170 +api_server 默认走**完整 agent**:每请求注入 tools(37.8KB/17工具) + skills index(9.1KB) ≈ **12,500 token**,
  171 +纯总结根本用不上。只对 api_server 关掉(不影响 CLI / 微信 bot):
  172 +```yaml
  173 +# ~/.hermes/config.yaml
  174 +platform_toolsets:
  175 + api_server: []
  176 +skills:
  177 + platform_disabled:
  178 + api_server: [<~/.hermes/skills 下所有 skill 名>]
  179 +```
  180 +> 用 hermes 的 `load_config`/`save_config` 改最稳(保留其余配置)。源码依据:`gateway/platforms/api_server.py`
  181 +> 的 `enabled_toolsets = _get_platform_tools(config, "api_server")`;skills 走 `skills.platform_disabled`。
  182 +> 改完重启 gateway,实测 **prompt 399 token**。
  183 +
  184 +### 3.3 开机自启(Magisk service.d)
  185 +
  186 +Termux:Boot 未装;已 root → 用 `/data/adb/service.d/start-hermes-gw.sh`(root, 755):
  187 +```sh
  188 +#!/system/bin/sh
  189 +LOG=/data/adb/hermes-boot.log
  190 +{ echo "=== $(date) ==="
  191 + i=0; while [ "$(getprop sys.boot_completed)" != "1" ] && [ "$i" -lt 60 ]; do sleep 3; i=$((i+1)); done
  192 + sleep 25
  193 + T=/data/data/com.termux; GW=$T/files/home/start_gw.sh; [ -f "$GW" ] || exit 0
  194 + SU=$(command -v su); [ -x "$SU" ] || for p in /system_ext/bin/su /system/bin/su /debug_ramdisk/su; do [ -x "$p" ] && SU=$p && break; done
  195 + U=$(stat -c %u "$T"); L=$((U-10000))
  196 + "$SU" -g "$U" -G 3003 -G 9997 -G "20$L" -G "50$L" "$U" -c "/data/data/com.termux/files/usr/bin/bash $GW"
  197 +} >> "$LOG" 2>&1
  198 +```
  199 +**坑**:这台机 Magisk 的 su 在 **`/system_ext/bin/su`**(`/system/bin/su` 不存在)→ 必须 `command -v su` 动态解析。
  200 +uid 动态推导(Termux 重装会变)。gateway 以 Termux 用户跑(保属主),冷启动慢 ~3–5 分钟(连被墙 metadata 超时)。
  201 +
  202 +### 3.4 验证端到端
  203 +
  204 +- 直打:`hermes` venv python POST `/v1/chat/completions`(Bearer=API_SERVER_KEY, model=kimi-for-coding)→ 200 + 纪要。
  205 +- 整链:lifelog 纪要 tab「现在跑一次」(需最近 1h ≥60s 语音)→ logcat `SummaryWorker: summary saved` → 纪要入库。
  206 +- **坑**:api_server 收到请求会回 200,但若上游连不上(VPN/断网)正文是 `API call failed ... Connection error`——查 `~/.hermes/logs/gateway.log`。
146 207
147 --- 208 ---
148 209