nexusos-xdr 1.2.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (71) hide show
  1. nexusos_xdr-1.2.0/LICENSE +21 -0
  2. nexusos_xdr-1.2.0/PKG-INFO +191 -0
  3. nexusos_xdr-1.2.0/README.md +161 -0
  4. nexusos_xdr-1.2.0/pyproject.toml +66 -0
  5. nexusos_xdr-1.2.0/setup.cfg +4 -0
  6. nexusos_xdr-1.2.0/src/nexusos_xdr/__init__.py +53 -0
  7. nexusos_xdr-1.2.0/src/nexusos_xdr/_mem_compat.py +28 -0
  8. nexusos_xdr-1.2.0/src/nexusos_xdr/_vault_deps.py +71 -0
  9. nexusos_xdr-1.2.0/src/nexusos_xdr/_vault_env.py +95 -0
  10. nexusos_xdr-1.2.0/src/nexusos_xdr/ack.py +271 -0
  11. nexusos_xdr-1.2.0/src/nexusos_xdr/ack_policy.py +289 -0
  12. nexusos_xdr-1.2.0/src/nexusos_xdr/agents/__init__.py +10 -0
  13. nexusos_xdr-1.2.0/src/nexusos_xdr/agents/base.py +448 -0
  14. nexusos_xdr-1.2.0/src/nexusos_xdr/agents/public.py +89 -0
  15. nexusos_xdr-1.2.0/src/nexusos_xdr/broadcast_router.py +271 -0
  16. nexusos_xdr-1.2.0/src/nexusos_xdr/callback/__init__.py +18 -0
  17. nexusos_xdr-1.2.0/src/nexusos_xdr/callback/cred_gate.py +244 -0
  18. nexusos_xdr-1.2.0/src/nexusos_xdr/callback/handler.py +1041 -0
  19. nexusos_xdr-1.2.0/src/nexusos_xdr/callback/outcome_autofill.py +202 -0
  20. nexusos_xdr-1.2.0/src/nexusos_xdr/cli.py +324 -0
  21. nexusos_xdr-1.2.0/src/nexusos_xdr/cred_reaper.py +108 -0
  22. nexusos_xdr-1.2.0/src/nexusos_xdr/credential.py +439 -0
  23. nexusos_xdr-1.2.0/src/nexusos_xdr/dispatch/__init__.py +31 -0
  24. nexusos_xdr-1.2.0/src/nexusos_xdr/dispatch/main.py +1092 -0
  25. nexusos_xdr-1.2.0/src/nexusos_xdr/dispatchd.py +913 -0
  26. nexusos_xdr-1.2.0/src/nexusos_xdr/email_inbound.py +513 -0
  27. nexusos_xdr-1.2.0/src/nexusos_xdr/exp_injector.py +600 -0
  28. nexusos_xdr-1.2.0/src/nexusos_xdr/fault_http_server.py +121 -0
  29. nexusos_xdr-1.2.0/src/nexusos_xdr/guards.py +275 -0
  30. nexusos_xdr-1.2.0/src/nexusos_xdr/hooks/__init__.py +11 -0
  31. nexusos_xdr-1.2.0/src/nexusos_xdr/hooks/agent_prompt_guard.py +85 -0
  32. nexusos_xdr-1.2.0/src/nexusos_xdr/hooks/broadcast_guard.py +217 -0
  33. nexusos_xdr-1.2.0/src/nexusos_xdr/hooks/commit_msg_advisor.py +235 -0
  34. nexusos_xdr-1.2.0/src/nexusos_xdr/hooks/completion_guard.py +158 -0
  35. nexusos_xdr-1.2.0/src/nexusos_xdr/hooks/dispatch_waitcallback_guard.py +60 -0
  36. nexusos_xdr-1.2.0/src/nexusos_xdr/hooks/dup_window_guard.py +94 -0
  37. nexusos_xdr-1.2.0/src/nexusos_xdr/hooks/interaction_guard.py +78 -0
  38. nexusos_xdr-1.2.0/src/nexusos_xdr/hooks/mem_first_guard.py +65 -0
  39. nexusos_xdr-1.2.0/src/nexusos_xdr/hooks/oplog_guard.py +77 -0
  40. nexusos_xdr-1.2.0/src/nexusos_xdr/hooks/selfcheck_evidence_guard.py +139 -0
  41. nexusos_xdr-1.2.0/src/nexusos_xdr/hooks/sensitive_ops.py +558 -0
  42. nexusos_xdr-1.2.0/src/nexusos_xdr/hooks/sensitive_ops_guard.py +219 -0
  43. nexusos_xdr-1.2.0/src/nexusos_xdr/hooks/state_transition_guard.py +224 -0
  44. nexusos_xdr-1.2.0/src/nexusos_xdr/hooks/waitcallback_bg_guard.py +64 -0
  45. nexusos_xdr-1.2.0/src/nexusos_xdr/httpapi/__init__.py +22 -0
  46. nexusos_xdr-1.2.0/src/nexusos_xdr/httpapi/routes.py +452 -0
  47. nexusos_xdr-1.2.0/src/nexusos_xdr/layer2_calibrate.py +425 -0
  48. nexusos_xdr-1.2.0/src/nexusos_xdr/mcp_auth.py +207 -0
  49. nexusos_xdr-1.2.0/src/nexusos_xdr/mcp_server.py +800 -0
  50. nexusos_xdr-1.2.0/src/nexusos_xdr/needdecision_notify.py +202 -0
  51. nexusos_xdr-1.2.0/src/nexusos_xdr/notify_push.py +1150 -0
  52. nexusos_xdr-1.2.0/src/nexusos_xdr/p02_hardblock.py +517 -0
  53. nexusos_xdr-1.2.0/src/nexusos_xdr/patrol/__init__.py +46 -0
  54. nexusos_xdr-1.2.0/src/nexusos_xdr/patrol/loop.py +1447 -0
  55. nexusos_xdr-1.2.0/src/nexusos_xdr/patrol/state.py +57 -0
  56. nexusos_xdr-1.2.0/src/nexusos_xdr/presence/__init__.py +40 -0
  57. nexusos_xdr-1.2.0/src/nexusos_xdr/presence/routes.py +427 -0
  58. nexusos_xdr-1.2.0/src/nexusos_xdr/proc_origin.py +528 -0
  59. nexusos_xdr-1.2.0/src/nexusos_xdr/proclog_filter.py +199 -0
  60. nexusos_xdr-1.2.0/src/nexusos_xdr/session_state.py +1044 -0
  61. nexusos_xdr-1.2.0/src/nexusos_xdr/spill.py +154 -0
  62. nexusos_xdr-1.2.0/src/nexusos_xdr/stall_watch.py +617 -0
  63. nexusos_xdr-1.2.0/src/nexusos_xdr/task_aliases.py +54 -0
  64. nexusos_xdr-1.2.0/src/nexusos_xdr/task_state.py +782 -0
  65. nexusos_xdr-1.2.0/src/nexusos_xdr/tools/__init__.py +54 -0
  66. nexusos_xdr-1.2.0/src/nexusos_xdr/tools/registry.py +838 -0
  67. nexusos_xdr-1.2.0/src/nexusos_xdr.egg-info/PKG-INFO +191 -0
  68. nexusos_xdr-1.2.0/src/nexusos_xdr.egg-info/SOURCES.txt +69 -0
  69. nexusos_xdr-1.2.0/src/nexusos_xdr.egg-info/dependency_links.txt +1 -0
  70. nexusos_xdr-1.2.0/src/nexusos_xdr.egg-info/requires.txt +10 -0
  71. nexusos_xdr-1.2.0/src/nexusos_xdr.egg-info/top_level.txt +1 -0
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 wangyong
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,191 @@
1
+ Metadata-Version: 2.4
2
+ Name: nexusos-xdr
3
+ Version: 1.2.0
4
+ Summary: nexusos-xdr — NEXUS agent dispatch bus (dispatch / callback / presence / patrol, with an MCP endpoint)
5
+ Author: NEXUS Contributors
6
+ License: MIT
7
+ Project-URL: Homepage, https://github.com/wangshuyongyhm/nexusos-xdr
8
+ Classifier: License :: OSI Approved :: MIT License
9
+ Classifier: Development Status :: 4 - Beta
10
+ Classifier: Intended Audience :: Developers
11
+ Classifier: Programming Language :: Python :: 3
12
+ Classifier: Programming Language :: Python :: 3.10
13
+ Classifier: Programming Language :: Python :: 3.11
14
+ Classifier: Programming Language :: Python :: 3.12
15
+ Classifier: Operating System :: MacOS
16
+ Classifier: Topic :: Software Development :: Libraries :: Python Modules
17
+ Requires-Python: >=3.10
18
+ Description-Content-Type: text/markdown
19
+ License-File: LICENSE
20
+ Requires-Dist: nexusos-gate>=1.2.0
21
+ Requires-Dist: nexusos-common<2,>=1.0
22
+ Requires-Dist: fastmcp<3.5,>=3.4.4
23
+ Requires-Dist: anyio>=4.0
24
+ Requires-Dist: starlette>=1.0
25
+ Requires-Dist: psycopg2-binary>=2.9
26
+ Requires-Dist: pyyaml>=5.4
27
+ Provides-Extra: mem
28
+ Requires-Dist: nexusos-mem; extra == "mem"
29
+ Dynamic: license-file
30
+
31
+ # nexusos-xdr
32
+
33
+ **NEXUS-XDR — Agent 调度总线(dispatch / callback / presence / patrol + MCP :19998)**
34
+
35
+ 多 Agent 体系的通信协议层与任务调度中枢:主控 Agent 通过它把任务派给执行 Agent(写任务书 → dispatch 注册 → 等待回调 → 验收),执行 Agent 通过它回报进度与结果。所有任务状态走单一真相源 `task_state`,派任务/回调/监控/救援全链路可追踪。
36
+
37
+ ## 核心特性
38
+
39
+ - **任务全生命周期**:`/dispatch` 注册任务(签发 dispatch/search 凭据、自动注入相关经验)→ `started/progress/done/failed/need_decision` 四态回调 → patrol 监控超时/卡死/完成检测
40
+ - **P0-1 ACK 回执链(2611111762)**:dispatch 副本自动注入回执协议(执行者收到任务书第一动作 `POST /ack` 带幂等键);`ack` 字段状态机 pending→acked,超窗无回执无物证→同键重投一次→仍无→`delivery_failed` 终态(ACK 窗 300s 与执行超时分离);steer 同幂等键(`POST /steer` 登记 + `/ack kind=steer` 回执,jsonl 台账);取代 patrol 旧盲重投(180s 无回调即重投的双投源,#615 探针实证)
41
+ - **MCP + HTTP 双协议**:同一服务(默认 `127.0.0.1:19998`)同时暴露 MCP 工具面(`/mcp`,Agent 直接调)和 HTTP API(`/dispatch`、`/callback`、`/ack`、`/steer`、`/task/<id>`,脚本/任意语言可调)
42
+ - **PRESENCE 在场感知**:`session/register`、`session/active`、`session/events`、op-log 操作意图声明——多主控并存时的冲突检测与协调基础
43
+ - **凭据完整性体系**:dispatch 契约凭据 + search 契约凭据签发/校验/防伪/定时清理(cred_reaper)
44
+ - **门禁集成**:关键动作前自动过 nexusos-gate(dispatch 前查任务书规范、done 前查五层落盘)
45
+ - **删除护栏**:`guards` 模块提供全体系唯一硬底线防护
46
+
47
+ ## 安装
48
+
49
+ ```bash
50
+ # 发布至 PyPI 前:本地路径安装(仓库根目录执行;三包目录并列时同命令安装三件套)
51
+ pip install ./nexusos-gate ./nexusos-mem ./nexusos-xdr
52
+ # 验证(1597 干净 venv 实证口径:十模块 import 全绿)
53
+ python -c "import nexusos_xdr.mcp_server, nexusos_xdr.mcp_auth, nexusos_mem, nexusos_gate; print('ok')"
54
+ ```
55
+
56
+ 要求 Python >= 3.10。**依赖 nexusos-gate**(体系内本地包,须同环境安装);`nexusos-mem` 为可选依赖(`[mem]` extra——PyPI 无此包,硬依赖会让干净环境安装失败;缺席时相关功能用到才报错并给出安装指引)。另依赖 `fastmcp>=3.4.4,<3.5`、`anyio>=4.0`、`starlette>=1.0`、`psycopg2-binary>=2.9`、`pyyaml>=5.4`。
57
+
58
+ PyPI 发布后可用 `pip install nexusos-xdr`(当前包未发布,请勿直接照抄)。
59
+
60
+ 本包**没有** `[full]` extras(那是 nexusos-mem 的向量能力包)。运行时需要 PostgreSQL(凭据/任务状态落库)。
61
+
62
+ ## 快速上手
63
+
64
+ 一次完整的任务调度生命周期(HTTP 方式):
65
+
66
+ ```bash
67
+ # 1. 派任务(注册 task_id,写任务书后调用)
68
+ curl -s -X POST http://127.0.0.1:19998/dispatch \
69
+ -H "Content-Type: application/json" \
70
+ -d '{"task_dir":"/path/to/task/input/task.md","target_agent":"my-agent",...}'
71
+
72
+ # 2. 执行方回调进度
73
+ curl -s -X POST http://127.0.0.1:19998/callback \
74
+ -H "Content-Type: application/json" \
75
+ -d '{"task_id":"xdr-agent-a-xxx","source":"agent-a","status":"progress","summary":"..."}'
76
+
77
+ # 3. 查询任务状态
78
+ curl -s "http://127.0.0.1:19998/task_state?agent=agent-a"
79
+
80
+ # 3.5 执行者投递回执(P0-1 ACK 链:任务书副本顶部的回执 curl 原样执行即可)
81
+ curl -s -X POST http://127.0.0.1:19998/ack \
82
+ -H "Content-Type: application/json" \
83
+ -d '{"task_id":"xdr-agent-a-xxx","key":"ack-xxxx","agent":"agent-a","kind":"dispatch"}'
84
+
85
+ # 4. 在场感知:注册自己 + 看谁在线
86
+ curl -s -X POST http://127.0.0.1:19998/session/register \
87
+ -H "Content-Type: application/json" \
88
+ -d '{"agent":"my-agent","role":"executor","session_id":"...","status":"init"}'
89
+ curl -s http://127.0.0.1:19998/session/active
90
+ ```
91
+
92
+ Python 方式(门禁便捷导出):
93
+
94
+ ```python
95
+ from nexusos_xdr import gate_check
96
+ result = gate_check(scenario="dispatch", context={...})
97
+ ```
98
+
99
+ ## 配套脚本(`~/.nexus/xdr/scripts/`,非包内,pip 不安装)
100
+
101
+ 派任务链路上的两个客户端脚本,语义以本文为准(2611172042 B2 / 2611172044 B4 后):
102
+
103
+ - `wait_callback.sh`:主控侧等待/感知通道。**超时只发 `timeout_warn` 告警,不改 task_state**(终态判定权归 patrol 的 grace 区间);重挂时用「挂载快照(status/callbacks 长度/末条 ts)」识别残影——挂载即终态且无新回调会继续等待而不是秒退重消费;tmux 目标一律精确(`has-session -t "=name"`、`send-keys`/`capture-pane -t "=name:"`)。
104
+ - `steer.sh`:救援投递 + 消费验证。判据四通道:草稿滞留判否(kimi/kimi2 只认框线内 `│ >` 草稿,裸 `❯` 回显排除)/ 推理态指示(braille spinner + `ctrl-s to add guidance`)/ transcript 新增行 / CodeWhale 排队态;窗口 5 拍×2s、失败自动重投 1 次。脚本单源在 `~/.hermes/scripts/steer.sh`(`~/.nexus/xdr/scripts/steer.sh` 为 symlink),`--dry-run` 只打印不投递。
105
+
106
+ ## 依赖分层
107
+
108
+ | 层 | 内容 |
109
+ |----|------|
110
+ | 硬依赖 | nexusos-gate(体系内)、fastmcp / anyio / starlette / psycopg2-binary / pyyaml |
111
+ | 可选依赖 | nexusos-mem(`[mem]` extra——检索注入/经验回填用,缺席容错) |
112
+ | 数据层 | PostgreSQL(凭据、任务状态、校准数据) |
113
+ | 服务层 | 本服务 :19998;联动 nexusos-mem api :11537 / embed :11436 |
114
+ | 包结构 | `mcp_server` 双协议入口 + `dispatch` / `callback` / `presence` / `patrol` / `tools` 五大子包(`agents/` 驱动层不在发布包内——bring your own agent drivers,见下) |
115
+
116
+ ## 环境变量(常用)
117
+
118
+ | 变量 | 默认值 | 作用 |
119
+ |------|--------|------|
120
+ | `NEXUS_VAULT_ROOT` | env > 包位置哨兵推导 > 占位 | Vault 根目录(任务书/落盘路径解析用,干净环境可缺省) |
121
+ | `NEXUS_OUTCOME_AUTOFILL` | `0`(关) | 经验套用 outcome 自动回填开关(`1` 开) |
122
+ | `NEXUS_P02_HARDBLOCK` | `1`(开) | P0-2 REJECT 硬拦开关(`0` 关,回退 Task492 只记 warning 行为) |
123
+ | `P02_POOL_FILE` | `<vault>/06-Memory/未决问题池.md` | 认领行目标池(测试缝,fixture 用) |
124
+ | `P02_EVENTS_URL` | `http://127.0.0.1:19998/session/event` | gate_blocked 事件端点(测试缝,fixture 用) |
125
+ | `NEXUS_P02_FALLBACK_LEDGER` | `<NEXUS_HOME>/xdr/gate-block-fallback.jsonl` | 池写失败降级台账(B5,测试缝 fixture 用) |
126
+
127
+ ## P0-2 REJECT 硬拦(Task 2611111759)
128
+
129
+ done 回调后台 gate(`_bg_gate_check`)发现 REJECT 级 fail 时,`p02_hardblock`
130
+ 模块在收口链上执行四件事(全部后台线程,callback 热路径零改动):
131
+
132
+ 1. **触发快照**:REJECT 明细 + 检查器版本/文件指纹(done.yaml sha256 + `_checks/*.py`
133
+ mtime)+ gate 输出全量 JSON → 任务目录 `output/gate-block-snapshot.json`(原子写)。
134
+ 历史归因唯一解:主控代补抹形态/检查器活体修改致重放漂移(Task 2611111750 实证)。
135
+ 2. **硬拦翻转**:`end_reason: completed → gate_blocked`(status 仍 DONE、result 不动;
136
+ 锁内 RMW,幂等——复核重扫不覆盖首次 `gate_block` 元数据)。
137
+ 3. **复核通道(防死锁)**:blocked≠死,三路出路——owner 认领行入未决问题池
138
+ (pool_write_channel 锁内 append)+ need_decision 回调记录(caller 可见)+
139
+ need_decision 即时通知(Task909 降级链)。
140
+ 4. **G16 联动**:监控侧 `check_G16_gate_residue.sh` 残留行升级带 owner/task/快照
141
+ 认领单(task 归属在消费侧从 task_state 反解,事件 detail 无 task_id)。
142
+
143
+ 出路闭环(Task 2611111774 补全):owner 修复产出重发 done → bg-gate PASS 分支
144
+ `maybe_clear_on_pass` 清账(end_reason 回 completed + `gate_block.cleared_at`
145
+ 留痕 + 池行自动翻 [x] 闭环)——重发 done 即真销账;清账同步清 `p02_hardblocked`
146
+ 幂等标志(生命周期对称:清账后可再拦)。无出路场景(任务被 FAILED/CANCELLED
147
+ 抢先终态)硬拦自动放弃只留快照。幂等双保险(1774 P0②):编排头 `once_flag`
148
+ 挡同进程重复 done 副作用 + 池行 lookaround 锚挡跨进程重复入池;池写失败降级
149
+ jsonl 台账(B5,`NEXUS_P02_FALLBACK_LEDGER` 可覆盖)。
150
+
151
+ context 组装单点(Task 2611111775):`handler._build_gate_context` 是 bg-gate
152
+ context 的唯一组装点——callback 载荷的 evidence/files/summary/experiences/
153
+ source 全量转发,与 CLI 直调 gate 等价。1774 残留边角实证:旧闭包内联组装漏
154
+ `evidence`(G7/EADC REJECT 级消费)致重发 done 场景降级文本检测、
155
+ `maybe_clear_on_pass` 不触发、出路断头只能函数直调清账;单点化后断链不可复发,
156
+ 旧载荷无 evidence 键不注入(兼容判据不变)。
157
+
158
+ ## 配置样例(bring your own agent drivers)
159
+
160
+ 发布包不含 `agents/` 驱动层(CLI Agent 的 tmux 适配器、弹窗救援等与具体工具链强绑定)——接入自有 Agent 驱动时:
161
+
162
+ - 参照 `agents` 驱动的接入协议(register/callback/task_state 三接口)实现适配器,字段含义见服务端 API 文档;
163
+ - 驱动所需的外部工具配置(如启动脚本路径、弹窗关键词)由驱动自带,服务端核心不读取;
164
+ - 核心 HTTP 链(`/dispatch` 注册、`/callback`、`presence`)不依赖任何驱动配置,无驱动也可用。
165
+
166
+ ## OTel GenAI semconv 对齐(2611111758)
167
+
168
+ 事件流写入侧(`session_state.append_event` 单一咽喉点)自动为每条事件派生 `otel_*` 对齐字段——**只增不改**:既有字段(`ts` / `ts_local` / `agent` / `type` / `detail` / `target` / extra 透传键)名与语义零变动;调用方 extra 显式提供的 `otel_*` 优先(可覆盖派生值)。不引入 OTel SDK/采集器,纯字段对齐,未来接 Langfuse/LangSmith/Arize 等观测平台零改造。
169
+
170
+ | xdr 事件字段 | OTel GenAI semconv | 对齐方式 | 落地字段 |
171
+ |---|---|---|---|
172
+ | (常量) | `gen_ai.system` | 直映(产生事件的系统身份) | `otel_gen_ai_system="nexus"` |
173
+ | `agent` | `gen_ai.agent.name` | 直映 | `otel_gen_ai_agent_name` |
174
+ | `type` | `gen_ai.operation.name` | 语义映射(xdr 事件类型即操作语义) | `otel_gen_ai_operation_name` |
175
+ | `type` | `event.name` | 命名空间化 | `otel_event_name="nexus.<type>"` |
176
+ | (常量) | `event.domain` | 直映 | `otel_event_domain="gen_ai"` |
177
+ | `extra.session_id` / 注册表缓存 | `gen_ai.session.id` | 需补字段:extra 顶层优先,缺席时回退内存提示缓存(register/heartbeat 顺手维护,注销时清除),仍无则省略 | `otel_gen_ai_session_id` |
178
+ | `ts` | Span/Log 时间戳 | 直映(ISO 8601 UTC,OTel 原生可解析) | 不新增字段 |
179
+ | `detail` | event payload / attributes | NEXUS 特有,无强对应——保留原字段,平台侧自行映射 | 不映射 |
180
+ | `target` | (无对应) | NEXUS 特有(op 意图目标/冲突检测) | 不映射 |
181
+ | `ts_local` | (无对应) | NEXUS 特有(本地时区可读性) | 不映射 |
182
+
183
+ 回溯说明:历史事件(本特性上线前)无 `otel_*` 字段——观测平台侧按缺省维度处理;字段派生只在写入时发生,不做存量回填。
184
+
185
+ ## 与另两件的关系
186
+
187
+ nexusos-xdr 是三件套的**编排者**,声明依赖另外两个:关键动作过 nexusos-gate 门禁(dispatch/done 场景的强制检查),dispatch 时从 nexusos-mem 检索注入相关经验、回调时回填经验套用结果。反向地,gate/mem 内部有少量函数级懒 import 本包的 session_state / credential(历史耦合,批4 后逐步解耦)。
188
+
189
+ ## License
190
+
191
+ MIT License。详见仓库根目录 [LICENSE](./LICENSE)。
@@ -0,0 +1,161 @@
1
+ # nexusos-xdr
2
+
3
+ **NEXUS-XDR — Agent 调度总线(dispatch / callback / presence / patrol + MCP :19998)**
4
+
5
+ 多 Agent 体系的通信协议层与任务调度中枢:主控 Agent 通过它把任务派给执行 Agent(写任务书 → dispatch 注册 → 等待回调 → 验收),执行 Agent 通过它回报进度与结果。所有任务状态走单一真相源 `task_state`,派任务/回调/监控/救援全链路可追踪。
6
+
7
+ ## 核心特性
8
+
9
+ - **任务全生命周期**:`/dispatch` 注册任务(签发 dispatch/search 凭据、自动注入相关经验)→ `started/progress/done/failed/need_decision` 四态回调 → patrol 监控超时/卡死/完成检测
10
+ - **P0-1 ACK 回执链(2611111762)**:dispatch 副本自动注入回执协议(执行者收到任务书第一动作 `POST /ack` 带幂等键);`ack` 字段状态机 pending→acked,超窗无回执无物证→同键重投一次→仍无→`delivery_failed` 终态(ACK 窗 300s 与执行超时分离);steer 同幂等键(`POST /steer` 登记 + `/ack kind=steer` 回执,jsonl 台账);取代 patrol 旧盲重投(180s 无回调即重投的双投源,#615 探针实证)
11
+ - **MCP + HTTP 双协议**:同一服务(默认 `127.0.0.1:19998`)同时暴露 MCP 工具面(`/mcp`,Agent 直接调)和 HTTP API(`/dispatch`、`/callback`、`/ack`、`/steer`、`/task/<id>`,脚本/任意语言可调)
12
+ - **PRESENCE 在场感知**:`session/register`、`session/active`、`session/events`、op-log 操作意图声明——多主控并存时的冲突检测与协调基础
13
+ - **凭据完整性体系**:dispatch 契约凭据 + search 契约凭据签发/校验/防伪/定时清理(cred_reaper)
14
+ - **门禁集成**:关键动作前自动过 nexusos-gate(dispatch 前查任务书规范、done 前查五层落盘)
15
+ - **删除护栏**:`guards` 模块提供全体系唯一硬底线防护
16
+
17
+ ## 安装
18
+
19
+ ```bash
20
+ # 发布至 PyPI 前:本地路径安装(仓库根目录执行;三包目录并列时同命令安装三件套)
21
+ pip install ./nexusos-gate ./nexusos-mem ./nexusos-xdr
22
+ # 验证(1597 干净 venv 实证口径:十模块 import 全绿)
23
+ python -c "import nexusos_xdr.mcp_server, nexusos_xdr.mcp_auth, nexusos_mem, nexusos_gate; print('ok')"
24
+ ```
25
+
26
+ 要求 Python >= 3.10。**依赖 nexusos-gate**(体系内本地包,须同环境安装);`nexusos-mem` 为可选依赖(`[mem]` extra——PyPI 无此包,硬依赖会让干净环境安装失败;缺席时相关功能用到才报错并给出安装指引)。另依赖 `fastmcp>=3.4.4,<3.5`、`anyio>=4.0`、`starlette>=1.0`、`psycopg2-binary>=2.9`、`pyyaml>=5.4`。
27
+
28
+ PyPI 发布后可用 `pip install nexusos-xdr`(当前包未发布,请勿直接照抄)。
29
+
30
+ 本包**没有** `[full]` extras(那是 nexusos-mem 的向量能力包)。运行时需要 PostgreSQL(凭据/任务状态落库)。
31
+
32
+ ## 快速上手
33
+
34
+ 一次完整的任务调度生命周期(HTTP 方式):
35
+
36
+ ```bash
37
+ # 1. 派任务(注册 task_id,写任务书后调用)
38
+ curl -s -X POST http://127.0.0.1:19998/dispatch \
39
+ -H "Content-Type: application/json" \
40
+ -d '{"task_dir":"/path/to/task/input/task.md","target_agent":"my-agent",...}'
41
+
42
+ # 2. 执行方回调进度
43
+ curl -s -X POST http://127.0.0.1:19998/callback \
44
+ -H "Content-Type: application/json" \
45
+ -d '{"task_id":"xdr-agent-a-xxx","source":"agent-a","status":"progress","summary":"..."}'
46
+
47
+ # 3. 查询任务状态
48
+ curl -s "http://127.0.0.1:19998/task_state?agent=agent-a"
49
+
50
+ # 3.5 执行者投递回执(P0-1 ACK 链:任务书副本顶部的回执 curl 原样执行即可)
51
+ curl -s -X POST http://127.0.0.1:19998/ack \
52
+ -H "Content-Type: application/json" \
53
+ -d '{"task_id":"xdr-agent-a-xxx","key":"ack-xxxx","agent":"agent-a","kind":"dispatch"}'
54
+
55
+ # 4. 在场感知:注册自己 + 看谁在线
56
+ curl -s -X POST http://127.0.0.1:19998/session/register \
57
+ -H "Content-Type: application/json" \
58
+ -d '{"agent":"my-agent","role":"executor","session_id":"...","status":"init"}'
59
+ curl -s http://127.0.0.1:19998/session/active
60
+ ```
61
+
62
+ Python 方式(门禁便捷导出):
63
+
64
+ ```python
65
+ from nexusos_xdr import gate_check
66
+ result = gate_check(scenario="dispatch", context={...})
67
+ ```
68
+
69
+ ## 配套脚本(`~/.nexus/xdr/scripts/`,非包内,pip 不安装)
70
+
71
+ 派任务链路上的两个客户端脚本,语义以本文为准(2611172042 B2 / 2611172044 B4 后):
72
+
73
+ - `wait_callback.sh`:主控侧等待/感知通道。**超时只发 `timeout_warn` 告警,不改 task_state**(终态判定权归 patrol 的 grace 区间);重挂时用「挂载快照(status/callbacks 长度/末条 ts)」识别残影——挂载即终态且无新回调会继续等待而不是秒退重消费;tmux 目标一律精确(`has-session -t "=name"`、`send-keys`/`capture-pane -t "=name:"`)。
74
+ - `steer.sh`:救援投递 + 消费验证。判据四通道:草稿滞留判否(kimi/kimi2 只认框线内 `│ >` 草稿,裸 `❯` 回显排除)/ 推理态指示(braille spinner + `ctrl-s to add guidance`)/ transcript 新增行 / CodeWhale 排队态;窗口 5 拍×2s、失败自动重投 1 次。脚本单源在 `~/.hermes/scripts/steer.sh`(`~/.nexus/xdr/scripts/steer.sh` 为 symlink),`--dry-run` 只打印不投递。
75
+
76
+ ## 依赖分层
77
+
78
+ | 层 | 内容 |
79
+ |----|------|
80
+ | 硬依赖 | nexusos-gate(体系内)、fastmcp / anyio / starlette / psycopg2-binary / pyyaml |
81
+ | 可选依赖 | nexusos-mem(`[mem]` extra——检索注入/经验回填用,缺席容错) |
82
+ | 数据层 | PostgreSQL(凭据、任务状态、校准数据) |
83
+ | 服务层 | 本服务 :19998;联动 nexusos-mem api :11537 / embed :11436 |
84
+ | 包结构 | `mcp_server` 双协议入口 + `dispatch` / `callback` / `presence` / `patrol` / `tools` 五大子包(`agents/` 驱动层不在发布包内——bring your own agent drivers,见下) |
85
+
86
+ ## 环境变量(常用)
87
+
88
+ | 变量 | 默认值 | 作用 |
89
+ |------|--------|------|
90
+ | `NEXUS_VAULT_ROOT` | env > 包位置哨兵推导 > 占位 | Vault 根目录(任务书/落盘路径解析用,干净环境可缺省) |
91
+ | `NEXUS_OUTCOME_AUTOFILL` | `0`(关) | 经验套用 outcome 自动回填开关(`1` 开) |
92
+ | `NEXUS_P02_HARDBLOCK` | `1`(开) | P0-2 REJECT 硬拦开关(`0` 关,回退 Task492 只记 warning 行为) |
93
+ | `P02_POOL_FILE` | `<vault>/06-Memory/未决问题池.md` | 认领行目标池(测试缝,fixture 用) |
94
+ | `P02_EVENTS_URL` | `http://127.0.0.1:19998/session/event` | gate_blocked 事件端点(测试缝,fixture 用) |
95
+ | `NEXUS_P02_FALLBACK_LEDGER` | `<NEXUS_HOME>/xdr/gate-block-fallback.jsonl` | 池写失败降级台账(B5,测试缝 fixture 用) |
96
+
97
+ ## P0-2 REJECT 硬拦(Task 2611111759)
98
+
99
+ done 回调后台 gate(`_bg_gate_check`)发现 REJECT 级 fail 时,`p02_hardblock`
100
+ 模块在收口链上执行四件事(全部后台线程,callback 热路径零改动):
101
+
102
+ 1. **触发快照**:REJECT 明细 + 检查器版本/文件指纹(done.yaml sha256 + `_checks/*.py`
103
+ mtime)+ gate 输出全量 JSON → 任务目录 `output/gate-block-snapshot.json`(原子写)。
104
+ 历史归因唯一解:主控代补抹形态/检查器活体修改致重放漂移(Task 2611111750 实证)。
105
+ 2. **硬拦翻转**:`end_reason: completed → gate_blocked`(status 仍 DONE、result 不动;
106
+ 锁内 RMW,幂等——复核重扫不覆盖首次 `gate_block` 元数据)。
107
+ 3. **复核通道(防死锁)**:blocked≠死,三路出路——owner 认领行入未决问题池
108
+ (pool_write_channel 锁内 append)+ need_decision 回调记录(caller 可见)+
109
+ need_decision 即时通知(Task909 降级链)。
110
+ 4. **G16 联动**:监控侧 `check_G16_gate_residue.sh` 残留行升级带 owner/task/快照
111
+ 认领单(task 归属在消费侧从 task_state 反解,事件 detail 无 task_id)。
112
+
113
+ 出路闭环(Task 2611111774 补全):owner 修复产出重发 done → bg-gate PASS 分支
114
+ `maybe_clear_on_pass` 清账(end_reason 回 completed + `gate_block.cleared_at`
115
+ 留痕 + 池行自动翻 [x] 闭环)——重发 done 即真销账;清账同步清 `p02_hardblocked`
116
+ 幂等标志(生命周期对称:清账后可再拦)。无出路场景(任务被 FAILED/CANCELLED
117
+ 抢先终态)硬拦自动放弃只留快照。幂等双保险(1774 P0②):编排头 `once_flag`
118
+ 挡同进程重复 done 副作用 + 池行 lookaround 锚挡跨进程重复入池;池写失败降级
119
+ jsonl 台账(B5,`NEXUS_P02_FALLBACK_LEDGER` 可覆盖)。
120
+
121
+ context 组装单点(Task 2611111775):`handler._build_gate_context` 是 bg-gate
122
+ context 的唯一组装点——callback 载荷的 evidence/files/summary/experiences/
123
+ source 全量转发,与 CLI 直调 gate 等价。1774 残留边角实证:旧闭包内联组装漏
124
+ `evidence`(G7/EADC REJECT 级消费)致重发 done 场景降级文本检测、
125
+ `maybe_clear_on_pass` 不触发、出路断头只能函数直调清账;单点化后断链不可复发,
126
+ 旧载荷无 evidence 键不注入(兼容判据不变)。
127
+
128
+ ## 配置样例(bring your own agent drivers)
129
+
130
+ 发布包不含 `agents/` 驱动层(CLI Agent 的 tmux 适配器、弹窗救援等与具体工具链强绑定)——接入自有 Agent 驱动时:
131
+
132
+ - 参照 `agents` 驱动的接入协议(register/callback/task_state 三接口)实现适配器,字段含义见服务端 API 文档;
133
+ - 驱动所需的外部工具配置(如启动脚本路径、弹窗关键词)由驱动自带,服务端核心不读取;
134
+ - 核心 HTTP 链(`/dispatch` 注册、`/callback`、`presence`)不依赖任何驱动配置,无驱动也可用。
135
+
136
+ ## OTel GenAI semconv 对齐(2611111758)
137
+
138
+ 事件流写入侧(`session_state.append_event` 单一咽喉点)自动为每条事件派生 `otel_*` 对齐字段——**只增不改**:既有字段(`ts` / `ts_local` / `agent` / `type` / `detail` / `target` / extra 透传键)名与语义零变动;调用方 extra 显式提供的 `otel_*` 优先(可覆盖派生值)。不引入 OTel SDK/采集器,纯字段对齐,未来接 Langfuse/LangSmith/Arize 等观测平台零改造。
139
+
140
+ | xdr 事件字段 | OTel GenAI semconv | 对齐方式 | 落地字段 |
141
+ |---|---|---|---|
142
+ | (常量) | `gen_ai.system` | 直映(产生事件的系统身份) | `otel_gen_ai_system="nexus"` |
143
+ | `agent` | `gen_ai.agent.name` | 直映 | `otel_gen_ai_agent_name` |
144
+ | `type` | `gen_ai.operation.name` | 语义映射(xdr 事件类型即操作语义) | `otel_gen_ai_operation_name` |
145
+ | `type` | `event.name` | 命名空间化 | `otel_event_name="nexus.<type>"` |
146
+ | (常量) | `event.domain` | 直映 | `otel_event_domain="gen_ai"` |
147
+ | `extra.session_id` / 注册表缓存 | `gen_ai.session.id` | 需补字段:extra 顶层优先,缺席时回退内存提示缓存(register/heartbeat 顺手维护,注销时清除),仍无则省略 | `otel_gen_ai_session_id` |
148
+ | `ts` | Span/Log 时间戳 | 直映(ISO 8601 UTC,OTel 原生可解析) | 不新增字段 |
149
+ | `detail` | event payload / attributes | NEXUS 特有,无强对应——保留原字段,平台侧自行映射 | 不映射 |
150
+ | `target` | (无对应) | NEXUS 特有(op 意图目标/冲突检测) | 不映射 |
151
+ | `ts_local` | (无对应) | NEXUS 特有(本地时区可读性) | 不映射 |
152
+
153
+ 回溯说明:历史事件(本特性上线前)无 `otel_*` 字段——观测平台侧按缺省维度处理;字段派生只在写入时发生,不做存量回填。
154
+
155
+ ## 与另两件的关系
156
+
157
+ nexusos-xdr 是三件套的**编排者**,声明依赖另外两个:关键动作过 nexusos-gate 门禁(dispatch/done 场景的强制检查),dispatch 时从 nexusos-mem 检索注入相关经验、回调时回填经验套用结果。反向地,gate/mem 内部有少量函数级懒 import 本包的 session_state / credential(历史耦合,批4 后逐步解耦)。
158
+
159
+ ## License
160
+
161
+ MIT License。详见仓库根目录 [LICENSE](./LICENSE)。
@@ -0,0 +1,66 @@
1
+ [build-system]
2
+ requires = ["setuptools>=68"]
3
+ build-backend = "setuptools.build_meta"
4
+
5
+ [project]
6
+ name = "nexusos-xdr"
7
+ version = "1.2.0"
8
+ description = "nexusos-xdr — NEXUS agent dispatch bus (dispatch / callback / presence / patrol, with an MCP endpoint)"
9
+ readme = "README.md"
10
+ requires-python = ">=3.10"
11
+ # PEP 621 旧式 text 表(非 SPDX 表达式)——保留 License classifier 不被新版
12
+ # setuptools(≥77, PEP639) 拒绝:license="MIT" 字符串形态会与 classifier 冲突致
13
+ # 干净 venv 安装失败(1609 实测 InvalidConfigError)。
14
+ license = {text = "MIT"}
15
+ authors = [{name = "NEXUS Contributors"}]
16
+ classifiers = [
17
+ "License :: OSI Approved :: MIT License",
18
+ "Development Status :: 4 - Beta",
19
+ "Intended Audience :: Developers",
20
+ "Programming Language :: Python :: 3",
21
+ "Programming Language :: Python :: 3.10",
22
+ "Programming Language :: Python :: 3.11",
23
+ "Programming Language :: Python :: 3.12",
24
+ "Operating System :: MacOS",
25
+ "Topic :: Software Development :: Libraries :: Python Modules",
26
+ ]
27
+ # 逐文件实测顶层 import(任务书⚠️批1 pyyaml 教训):
28
+ # fastmcp(mcp_server) + anyio/starlette(mcp_server/callback/httpapi/presence 直用同栈)
29
+ # + psycopg2(credential/layer2_calibrate/callback顶层, mcp_server懒) + pyyaml(agents/base 懒)。
30
+ # fastmcp 按当前运行版本 minor 锁定(陷阱2:MCP 行为漂移防护)。
31
+ # nexusos-gate 以本地兄弟包满足(三件套同环境/同命令安装;Task668 fail-closed
32
+ # 设计保持硬依赖)。nexusos-mem 降为 extra(Task1597 炸点①:mem 不在 PyPI,
33
+ # 硬依赖让干净环境单装 xdr 直接解析失败;mem 接入点已容错——
34
+ # nexusos_xdr._mem_compat,缺包时用到才报清晰安装指引)。
35
+ dependencies = [
36
+ "nexusos-gate>=1.2.0",
37
+ "nexusos-common>=1.0,<2", # 1957 锚点单源
38
+ "fastmcp>=3.4.4,<3.5",
39
+ "anyio>=4.0",
40
+ "starlette>=1.0",
41
+ "psycopg2-binary>=2.9",
42
+ "pyyaml>=5.4",
43
+ ]
44
+
45
+ [project.optional-dependencies]
46
+ # 伴随包可选组:三件套同装用 `pip install ./pypi/nexusos-gate ./pypi/nexusos-mem ./nexusos-xdr`
47
+ # 一条命令同解析(顺序无关);只想要 xdr+mem 组合用 [mem]。
48
+ mem = ["nexusos-mem"]
49
+
50
+ # 开源仓库落位(M1 脱敏建仓)后更新为真实主页;切片器不再补注(单一真源)
51
+ [project.urls]
52
+ Homepage = "https://github.com/wangshuyongyhm/nexusos-xdr"
53
+
54
+ [tool.setuptools.packages.find]
55
+ where = ["src"]
56
+
57
+ [tool.setuptools.package-data]
58
+ # agents 运行面 shell(broadcast_router spawn 的 on_mention 链)
59
+ "nexusos_xdr.agents.zcode" = ["*.sh"]
60
+ "nexusos_xdr.agents.hermes" = ["*.sh"]
61
+ "nexusos_xdr.agents.kimiapp" = ["*.sh"]
62
+ "nexusos_xdr.agents.cursor" = ["*.sh"]
63
+ # 守卫 canonical 源(1963 单5):hooks 模块 .py 随包分发,部署位 $NEXUS_HOME/xdr/hooks/
64
+ "nexusos_xdr.hooks" = ["*.py"]
65
+ # 配置样例模板(脱敏,随包分发——README 配置样例节引用)
66
+ "nexusos_xdr" = ["config.example.yaml"]
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+
@@ -0,0 +1,53 @@
1
+ #!/usr/bin/env python3
2
+ # -*- coding: utf-8 -*-
3
+ """
4
+ nexusos-xdr — 所有 Agent 的通信协议层
5
+
6
+ 与 nexusos-mem(记忆层)并列,负责 Agent 之间的:
7
+ - 任务派发(dispatch)
8
+ - 状态同步(task lifecycle)
9
+ - 审批代理(approval routing)
10
+ - 过程落盘(auto-archive)
11
+
12
+ 入口: python3 -m nexusos_xdr.mcp_server(或 mcp_server.py 直接运行)
13
+ 端口: 19998(MCP /mcp + HTTP /dispatch /task/<id>)
14
+
15
+ 作者: ZCode Agent 日期: 2026-07-13
16
+ Task631/633 迁包: 正源自 vault 内 nexusos_xdr/ 迁入 pip 包(旧位 __init__ 以
17
+ __path__ 重定向到本包——旧位 shim/配置文件作为子模块回退源继续可用)。
18
+ """
19
+
20
+ __version__ = "1.2.0"
21
+
22
+ # ── 路径自愈(批3 过渡层,批2 nexusos_mem 同款)────────────────────
23
+ # 迁移件仍引用 vault 级平铺件与 mem 域包(nexus.paths_setup 副作用、
24
+ # nexus.constants、nexus.shared、nexus_mem_utils、reembed_nulls、
25
+ # gate_engine 旧位 shim 等)。editable 安装时本包位于 vault 内
26
+ # pypi/nexusos-xdr/src/nexusos_xdr/,向上 4 层即 自动化脚本/,先入 sys.path
27
+ # 再复用 nexus.paths_setup 单源(_AUTO_DIR+_XDR_DIR+SCRIPTS_DIR[.nexus/xdr/scripts,1960])。
28
+ # 批4 wheel 独立部署时本目录无 nexus/ → 跳过(由 pip 依赖承接)。
29
+ import sys as _sys
30
+ from pathlib import Path as _Path
31
+
32
+ _AUTO_DIR = _Path(__file__).resolve().parents[4]
33
+ _OLD_PKG_DIR = _AUTO_DIR / "nexusos_xdr"
34
+ if (_AUTO_DIR / "nexus").is_dir():
35
+ # Task668 ②: 包内自足版(_vault_env.setup_sys_path 内部同款逻辑——vault
36
+ # 布局才副作用,干净环境 no-op;替代对 vault 级 nexus.paths_setup 的顶层依赖)
37
+ from nexusos_xdr import _vault_env as _ve_init # noqa: E402
38
+ _ve_init.setup_sys_path()
39
+ if _OLD_PKG_DIR.is_dir():
40
+ # 旧位目录作为子模块回退源(etc/gates symlink、etc/hooks、agents 配置域
41
+ # 脚本、gate_engine/env_scrub shim 只存在于旧位)——pip 解析上下文也可达
42
+ __path__.append(str(_OLD_PKG_DIR))
43
+
44
+
45
+ def gate_check(scenario: str, context: dict, agent: str = "") -> dict:
46
+ """过场景门禁(便捷导出,Task572 未决#2)。
47
+
48
+ 与 dispatchd.py 的 `from nexusos_gate.gate_engine import check` 完全等价,
49
+ 让用户/Agent 直觉写法 `from nexusos_xdr import gate_check` 可用。
50
+ 返回 {passed, failures, hard_fails, warns}。
51
+ """
52
+ from nexusos_gate.gate_engine import check
53
+ return check(scenario, context, agent=agent)
@@ -0,0 +1,28 @@
1
+ #!/usr/bin/env python3
2
+ # -*- coding: utf-8 -*-
3
+ """nexusos_xdr._mem_compat — nexusos-mem 伴随包的容错接入(Task1597 炸点①)。
4
+
5
+ nexusos-mem 不发 PyPI(三件套同仓安装),nexusos-xdr 已把对它的依赖降为
6
+ [mem] extra——干净环境单装 xdr 时它可能缺席。这里把「顶层 import 不炸 +
7
+ 用到时报人话错误」收敛到一个真源:缺包时 env 净化等接入点抛出带安装
8
+ 指引的 RuntimeError,而不是让用户对着 ModuleNotFoundError 一脸懵。
9
+ """
10
+ try:
11
+ # 正源在伴随包(pyproject [mem] extra;Task668 ② 包路径口径)
12
+ from nexusos_mem.env_scrub import scrubbed_env
13
+ except ImportError:
14
+ scrubbed_env = None
15
+
16
+ MEM_MISSING_MSG = (
17
+ "nexusos-mem 未安装(nexusos-xdr 的伴随包,非 PyPI 分发):"
18
+ "pip install ./pypi/nexusos-mem 或 pip install 'nexusos-xdr[mem]';"
19
+ "三件套安装顺序见 README 安装节。"
20
+ )
21
+ _MISSING_MSG = MEM_MISSING_MSG # 兼容别名(本模块内部用)
22
+
23
+
24
+ def scrubbed_env_or_raise() -> dict:
25
+ """env 净化入口:缺伴随包时抛带安装指引的错(fail-loud 但说人话)。"""
26
+ if scrubbed_env is None:
27
+ raise RuntimeError(_MISSING_MSG)
28
+ return scrubbed_env()
@@ -0,0 +1,71 @@
1
+ """nexusos_xdr._vault_deps — vault 级 nexus.tools_meta/checkpoint/auto_approval 的
2
+ 包内安全解析(Task674 ②,照 _vault_env.py 模式)。
3
+
4
+ 问题(668 发现):dispatchd 的 Task295 预检(nexus.tools_meta/auto_approval)
5
+ 与 mcp_server 的 state_transition checkpoint 钩子(nexus.checkpoint)是顶层
6
+ `from nexus.xxx import ...`——依赖 sys.path 里有 `自动化脚本/`。flip 到 venv 后
7
+ 包从 site-packages 加载,vault sys.path 不在 → import 全失败 → 钩子静默关闭
8
+ (恢复锚功能受损)。
9
+
10
+ 解法(与 _vault_env 同模式):
11
+ 1. 先直接 import(vault 内运行时 sys.path 本就有——零变化);
12
+ 2. 失败 → 按 NEXUS_VAULT_ROOT(env 优先,缺省同 _vault_env)定位 vault 的
13
+ `自动化脚本/` 塞 sys.path 再试一次(同机 flip 场景:vault 在机,功能保留);
14
+ 3. 仍失败(真干净环境,无 vault)→ WARN + 返 None,调用方维持原有优雅降级
15
+ 语义(Task295 预检关/checkpoint 钩子降级——干净环境本无 tools.yaml/LOOP
16
+ 目录,降级是正确行为而非 bug)。1957:降级从静默改为 WARN(design §3.2
17
+ 可降级但禁静默)。
18
+ """
19
+ import importlib
20
+ import logging
21
+ import sys
22
+ from pathlib import Path
23
+
24
+ from nexusos_common import VAULT_MISSING_REMEDIATION
25
+
26
+ logger = logging.getLogger(__name__)
27
+
28
+ _VAULT_ROOT = Path(__import__("nexusos_xdr._vault_env", fromlist=["VAULT_ROOT"]).VAULT_ROOT)
29
+
30
+
31
+ def _vault_nexus(module: str):
32
+ """安全 import vault 级 nexus.<module>:vault 在→模块;不在→WARN+None。"""
33
+ try:
34
+ return importlib.import_module(module)
35
+ except Exception:
36
+ pass
37
+ auto_dir = _VAULT_ROOT / "05-System" / "自动化脚本"
38
+ if not (auto_dir / "nexus").is_dir():
39
+ logger.warning(
40
+ f"vault 级 {module} 不可用(干净环境降级 None:预检/checkpoint 降级)。"
41
+ + VAULT_MISSING_REMEDIATION
42
+ )
43
+ return None # 真干净环境(无 vault):降级(1957 起带 WARN)
44
+ if str(auto_dir) not in sys.path:
45
+ sys.path.insert(0, str(auto_dir))
46
+ try:
47
+ return importlib.import_module(module)
48
+ except Exception:
49
+ return None
50
+
51
+
52
+ def _pick(module: str, names):
53
+ """从 vault 级模块挑符号;模块不可用则全 None。"""
54
+ mod = _vault_nexus(module)
55
+ if mod is None:
56
+ return (None,) * len(names)
57
+ try:
58
+ return tuple(getattr(mod, n) for n in names)
59
+ except AttributeError:
60
+ return (None,) * len(names)
61
+
62
+
63
+ # --- Task295:dispatchd 预检 API ---
64
+ should_auto_approve, precheck_for_agent = _pick("nexus.tools_meta",
65
+ ("should_auto_approve", "precheck_for_agent"))
66
+ (log_tool_call,) = _pick("nexus.auto_approval", ("log_tool_call",))
67
+ TOOLS_META_READY = should_auto_approve is not None and precheck_for_agent is not None
68
+
69
+ # --- state_transition checkpoint 钩子 API ---
70
+ write_checkpoint, recover = _pick("nexus.checkpoint", ("write_checkpoint", "recover"))
71
+ CHECKPOINT_READY = write_checkpoint is not None and recover is not None