@team-agent/installer 0.5.123 → 0.5.125

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 (107) hide show
  1. package/Cargo.lock +1 -8
  2. package/Cargo.toml +1 -1
  3. package/README.md +2 -2
  4. package/README.zh.md +2 -2
  5. package/crates/team-agent/Cargo.toml +0 -1
  6. package/crates/team-agent/src/cli/diagnose.rs +39 -1
  7. package/crates/team-agent/src/cli/emit.rs +41 -15
  8. package/crates/team-agent/src/cli/intent/runtime.rs +30 -54
  9. package/crates/team-agent/src/cli/intent/runtime_tests.rs +59 -118
  10. package/crates/team-agent/src/cli/leader.rs +2 -3
  11. package/crates/team-agent/src/cli/leader_prompt.rs +1 -1
  12. package/crates/team-agent/src/cli/mod.rs +6 -5
  13. package/crates/team-agent/src/cli/models.rs +93 -128
  14. package/crates/team-agent/src/cli/route.rs +1 -1
  15. package/crates/team-agent/src/cli/send/persist.rs +36 -0
  16. package/crates/team-agent/src/cli/send/presentation.rs +7 -1
  17. package/crates/team-agent/src/cli/send.rs +3 -1
  18. package/crates/team-agent/src/cli/spec.rs +1 -2
  19. package/crates/team-agent/src/cli/status_port/brief.rs +66 -0
  20. package/crates/team-agent/src/cli/tests/base.rs +4 -11
  21. package/crates/team-agent/src/cli/tests/status_send.rs +17 -0
  22. package/crates/team-agent/src/compiler/tests.rs +21 -5
  23. package/crates/team-agent/src/compiler.rs +24 -1
  24. package/crates/team-agent/src/contract_runtime/backend.rs +30 -6
  25. package/crates/team-agent/src/contract_runtime/config.rs +34 -24
  26. package/crates/team-agent/src/contract_runtime/discovery.rs +79 -56
  27. package/crates/team-agent/src/contract_runtime/framework.rs +89 -34
  28. package/crates/team-agent/src/contract_runtime/registry.rs +299 -24
  29. package/crates/team-agent/src/coordinator/runtime_detectors.rs +0 -1
  30. package/crates/team-agent/src/coordinator/tick.rs +89 -1
  31. package/crates/team-agent/src/db/message_store.rs +78 -3
  32. package/crates/team-agent/src/layout/worker_window_helpers.rs +0 -1
  33. package/crates/team-agent/src/leader/prompt.rs +1 -2
  34. package/crates/team-agent/src/leader/provider_attribution.rs +0 -52
  35. package/crates/team-agent/src/leader/start.rs +0 -1
  36. package/crates/team-agent/src/leader/tests/prompt_contract.rs +2 -3
  37. package/crates/team-agent/src/lifecycle/launch/add_agent.rs +91 -0
  38. package/crates/team-agent/src/lifecycle/launch/add_agent_state.rs +14 -1
  39. package/crates/team-agent/src/lifecycle/launch/agent_state.rs +0 -9
  40. package/crates/team-agent/src/lifecycle/launch/fork_agent.rs +0 -1
  41. package/crates/team-agent/src/lifecycle/launch/mcp_config.rs +2 -2
  42. package/crates/team-agent/src/lifecycle/launch/pi_mcp.rs +19 -101
  43. package/crates/team-agent/src/lifecycle/launch/spawn.rs +0 -28
  44. package/crates/team-agent/src/lifecycle/launch/spec_state.rs +0 -1
  45. package/crates/team-agent/src/lifecycle/launch/worker_env.rs +0 -108
  46. package/crates/team-agent/src/lifecycle/launch.rs +2 -20
  47. package/crates/team-agent/src/lifecycle/profile_launch.rs +11 -39
  48. package/crates/team-agent/src/lifecycle/profile_smoke.rs +1 -2
  49. package/crates/team-agent/src/lifecycle/restart/common.rs +1 -63
  50. package/crates/team-agent/src/lifecycle/role_config.rs +42 -3
  51. package/crates/team-agent/src/lifecycle/tests/cli_argv_routing_red.rs +43 -13
  52. package/crates/team-agent/src/lifecycle/tests/clone_fork_copilot_perms_red.rs +6 -2
  53. package/crates/team-agent/src/lifecycle/tests/pi_catalog_red.rs +1 -3
  54. package/crates/team-agent/src/lifecycle/tests/pi_command_red.rs +4 -16
  55. package/crates/team-agent/src/lifecycle/tests/pi_compiler_red.rs +1 -3
  56. package/crates/team-agent/src/lifecycle/tests/pi_executable_mcp_red.rs +84 -53
  57. package/crates/team-agent/src/lifecycle/tests/pi_fork_session_inheritance_red.rs +19 -1
  58. package/crates/team-agent/src/lifecycle/tests/pi_session_delivery_red.rs +21 -0
  59. package/crates/team-agent/src/lifecycle/tests/restart_rebind_hotfix_252_red.rs +0 -1
  60. package/crates/team-agent/src/lifecycle/tests/worker_spawn_env_red.rs +0 -137
  61. package/crates/team-agent/src/lifecycle/tests.rs +0 -3
  62. package/crates/team-agent/src/lifecycle/worker_command_context.rs +2 -4
  63. package/crates/team-agent/src/mcp_server/readiness.rs +148 -97
  64. package/crates/team-agent/src/mcp_server/readiness_tests.rs +115 -16
  65. package/crates/team-agent/src/messaging/delivery.rs +186 -38
  66. package/crates/team-agent/src/messaging/delivery_consumption_tests.rs +4 -4
  67. package/crates/team-agent/src/messaging/enter_rescue_tests.rs +1 -1
  68. package/crates/team-agent/src/messaging/helpers.rs +19 -0
  69. package/crates/team-agent/src/messaging/tests/dup_inject.rs +66 -4
  70. package/crates/team-agent/src/messaging/tests/runtime.rs +110 -4
  71. package/crates/team-agent/src/messaging/tests/spine.rs +22 -6
  72. package/crates/team-agent/src/model/enums.rs +1 -8
  73. package/crates/team-agent/src/provider/adapter.rs +1 -67
  74. package/crates/team-agent/src/provider/adapters/mod.rs +0 -1
  75. package/crates/team-agent/src/provider/adapters/pi.rs +4 -4
  76. package/crates/team-agent/src/provider/argv_route.rs +1 -5
  77. package/crates/team-agent/src/provider/bypass_flags.rs +3 -11
  78. package/crates/team-agent/src/provider/classify.rs +68 -3
  79. package/crates/team-agent/src/provider/faults.rs +1 -1
  80. package/crates/team-agent/src/provider/model_catalog.rs +2 -73
  81. package/crates/team-agent/src/provider/session/capture.rs +1 -1
  82. package/crates/team-agent/src/provider/session/context_fork.rs +4 -4
  83. package/crates/team-agent/src/provider/session_scan/common.rs +1 -2
  84. package/crates/team-agent/src/provider/session_scan/pi.rs +32 -19
  85. package/crates/team-agent/src/provider/session_scan.rs +0 -4
  86. package/crates/team-agent/src/provider/submit_now.rs +5 -28
  87. package/crates/team-agent/src/provider/tests/adapter.rs +5 -184
  88. package/crates/team-agent/src/provider/tests/classify.rs +53 -0
  89. package/crates/team-agent/src/provider/tests/command_helpers.rs +3 -6
  90. package/crates/team-agent/src/provider/types.rs +1 -1
  91. package/crates/team-agent/src/provider/wire.rs +48 -8
  92. package/crates/team-agent/src/tmux_backend/native_input_tests.rs +2 -178
  93. package/crates/team-agent/src/tmux_backend/tests.rs +151 -0
  94. package/crates/team-agent/src/tmux_backend.rs +43 -8
  95. package/crates/team-agent/src/transport/native_input.rs +2 -231
  96. package/npm/install.mjs +40 -3
  97. package/package.json +4 -4
  98. package/skills/team-agent/SKILL.md +1 -1
  99. package/crates/team-agent/src/lifecycle/launch/cursor_create_chat.rs +0 -229
  100. package/crates/team-agent/src/lifecycle/launch/cursor_mcp.rs +0 -522
  101. package/crates/team-agent/src/lifecycle/launch/cursor_mcp_iso.rs +0 -282
  102. package/crates/team-agent/src/lifecycle/tests/cursor_mcp_overlay.rs +0 -576
  103. package/crates/team-agent/src/lifecycle/tests/cursor_require_explicit_model_red.rs +0 -222
  104. package/crates/team-agent/src/lifecycle/tests/cursor_restart_resume_red.rs +0 -511
  105. package/crates/team-agent/src/provider/adapters/cursor_agent.rs +0 -44
  106. package/crates/team-agent/src/provider/session_scan/cursor.rs +0 -538
  107. package/crates/team-agent/src/provider/testdata/cursor-list-models-native.stdout +0 -250
@@ -1,522 +0,0 @@
1
- //! ---
2
- //! purpose: 把 team_orchestrator 身份写进 cursor 实际会读的 mcp.json
3
- //! contract:
4
- //! provides:
5
- //! - name: apply_cursor_mcp_overlay
6
- //! what: 写 <workspace>/.cursor/mcp.json,env 必须带 TEAM_AGENT_ID
7
- //! - name: cursor_mcp_enable_argv
8
- //! what: 组 `agent mcp enable team_orchestrator`(不认 --workspace)
9
- //! - name: enable_cursor_workspace_mcp
10
- //! what: 在工程根(可选)或物理工作目录跑 enable;测试隔离下跳过以免写 ~/.cursor
11
- //! - name: apply_cursor_spawn_workspace_pointers
12
- //! what: 隔离开时 --workspace 指 per-seat 工程根,--add-dir 指真 workspace
13
- //! - name: physical_workspace_path
14
- //! what: pwd -P 等价路径;mcp enable 按 getcwd 分片
15
- //! - name: refuse_second_cursor_occupant
16
- //! what: 同一物理 workspace 第二 CursorAgent 拒绝(mcp.json last-writer)
17
- //! boundary:
18
- //! - 同 workspace 第二 CursorAgent 拒绝;U-07 过前不装「已支持多席」
19
- //! - 不把身份从 json env 删掉(cursor 不继承父进程 TEAM_AGENT_*)
20
- //! - 不写 ~/.cursor/mcp.json 全局文件
21
- //! - 不调 --approve-mcps(未验证能代替 enable)
22
- //! maturity: wired
23
- //! ---
24
-
25
- use std::collections::BTreeMap;
26
- use std::path::{Path, PathBuf};
27
- use std::process::Command;
28
-
29
- use crate::lifecycle::LifecycleError;
30
- use crate::model::yaml::Value as YamlValue;
31
- use crate::provider::wire::command_name;
32
- use crate::provider::Provider;
33
-
34
- use super::cursor_mcp_iso::{
35
- cursor_mcp_isolation_enabled, cursor_mcp_project_dir, materialize_cursor_mcp_project,
36
- validate_cursor_mcp_project_root,
37
- };
38
-
39
- /// Keys that must appear in mcp.json env. Cursor strips parent env down to
40
- /// HOME/PATH/TERM/… — TEAM_AGENT_ID only survives if it is in this table.
41
- const REQUIRED_IDENTITY_KEYS: &[&str] = &[
42
- "TEAM_AGENT_WORKSPACE",
43
- "TEAM_AGENT_ID",
44
- "TEAM_AGENT_OWNER_TEAM_ID",
45
- "TEAM_AGENT_AUTH_MODE",
46
- ];
47
-
48
- /// ---
49
- /// purpose: 给出 workspace 的物理路径
50
- /// returns: 能 canonicalize 就用它,否则原样返回
51
- /// ---
52
- pub fn physical_workspace_path(workspace: &Path) -> PathBuf {
53
- std::fs::canonicalize(workspace).unwrap_or_else(|_| workspace.to_path_buf())
54
- }
55
-
56
- fn status_is_live(status: &str) -> bool {
57
- !matches!(
58
- status,
59
- "stopped" | "stopping" | "removed" | "spawn_failed" | "failed"
60
- )
61
- }
62
-
63
- fn agent_is_cursor(provider: Option<&str>) -> bool {
64
- matches!(
65
- provider.and_then(crate::lifecycle::profile_launch::parse_provider),
66
- Some(Provider::CursorAgent)
67
- )
68
- }
69
-
70
- /// ---
71
- /// purpose: 同一物理 workspace 拒绝第二 CursorAgent
72
- /// params:
73
- /// workspace: 物理工作目录
74
- /// incoming_id: 正要起的席位 id
75
- /// spec: 可选 yaml spec,用来在 state 尚未写入时看见第二席
76
- /// returns: 已有其它 CursorAgent 则 RequirementUnmet,文案含 TEAM_AGENT_ID 与 mcp.json
77
- /// contract:
78
- /// provides:
79
- /// - name: refuse_second_cursor_occupant
80
- /// what: mcp.json last-writer 闸;U-07 过前不装多席
81
- /// boundary:
82
- /// - 不改 grok 独占实现
83
- /// ---
84
- pub fn refuse_second_cursor_occupant(
85
- workspace: &Path,
86
- incoming_id: &str,
87
- spec: Option<&YamlValue>,
88
- ) -> Result<(), LifecycleError> {
89
- let mut others = Vec::new();
90
- if let Some(spec) = spec {
91
- if let Some(agents) = spec.get("agents").and_then(YamlValue::as_list) {
92
- for agent in agents {
93
- let Some(id) = agent.get("id").and_then(YamlValue::as_str) else {
94
- continue;
95
- };
96
- if id == incoming_id {
97
- continue;
98
- }
99
- if agent_is_paused_yaml(agent) {
100
- continue;
101
- }
102
- if agent_is_cursor(agent.get("provider").and_then(YamlValue::as_str)) {
103
- others.push(id.to_string());
104
- }
105
- }
106
- }
107
- }
108
- if let Ok(state) = crate::state::persist::load_runtime_state(workspace) {
109
- if let Some(agents) = state.get("agents").and_then(serde_json::Value::as_object) {
110
- for (id, agent) in agents {
111
- if id == incoming_id {
112
- continue;
113
- }
114
- let provider = agent.get("provider").and_then(serde_json::Value::as_str);
115
- if !agent_is_cursor(provider) {
116
- continue;
117
- }
118
- let status = agent
119
- .get("status")
120
- .and_then(serde_json::Value::as_str)
121
- .unwrap_or("running");
122
- if status_is_live(status) {
123
- others.push(id.clone());
124
- }
125
- }
126
- }
127
- }
128
- others.sort();
129
- others.dedup();
130
- if others.is_empty() {
131
- return Ok(());
132
- }
133
- Err(LifecycleError::RequirementUnmet(format!(
134
- "error: cursor_agent seat already occupies this workspace\n\
135
- incoming: {incoming_id}\n\
136
- occupants: {}\n\
137
- reason: <workspace>/.cursor/mcp.json is directory-scoped; a second seat overwrites TEAM_AGENT_ID (last-writer)\n\
138
- workspace: {}\n\
139
- action: do not add another CursorAgent in this workspace until per-seat MCP identity is isolated",
140
- others.join(", "),
141
- workspace.display(),
142
- )))
143
- }
144
-
145
- fn agent_is_paused_yaml(agent: &YamlValue) -> bool {
146
- matches!(agent.get("paused"), Some(YamlValue::Bool(true)))
147
- }
148
-
149
- /// ---
150
- /// purpose: 写 workspace 下 cursor 会读的 mcp.json,env 里必须带席位身份键
151
- /// params:
152
- /// mcp_config: 已解析的 MCP 配置,须含 team_orchestrator 与 command
153
- /// returns: 写出的文件路径
154
- /// errors: 缺条目或缺 command 时返回 StatePersist,写盘失败也返回 StatePersist
155
- /// ---
156
- pub fn apply_cursor_mcp_overlay(
157
- workspace: &Path,
158
- mcp_config: &crate::provider::McpConfig,
159
- ) -> Result<PathBuf, LifecycleError> {
160
- let server = mcp_config
161
- .raw
162
- .get("team_orchestrator")
163
- .or_else(|| mcp_config.raw.get("team-agent"))
164
- .ok_or_else(|| {
165
- LifecycleError::StatePersist(
166
- "cursor MCP overlay requires team_orchestrator in resolved mcp config".to_string(),
167
- )
168
- })?;
169
- let command = server
170
- .get("command")
171
- .and_then(serde_json::Value::as_str)
172
- .filter(|value| !value.is_empty())
173
- .ok_or_else(|| {
174
- LifecycleError::StatePersist("cursor MCP overlay missing command".to_string())
175
- })?;
176
- let args = server
177
- .get("args")
178
- .and_then(serde_json::Value::as_array)
179
- .map(|items| {
180
- items
181
- .iter()
182
- .filter_map(serde_json::Value::as_str)
183
- .map(str::to_string)
184
- .collect::<Vec<_>>()
185
- })
186
- .unwrap_or_default();
187
- let env = server
188
- .get("env")
189
- .and_then(serde_json::Value::as_object)
190
- .map(|map| {
191
- map.iter()
192
- .filter_map(|(key, value)| {
193
- value.as_str().map(|text| (key.clone(), text.to_string()))
194
- })
195
- .collect::<BTreeMap<_, _>>()
196
- })
197
- .unwrap_or_default();
198
- for key in REQUIRED_IDENTITY_KEYS {
199
- match env.get(*key).map(String::as_str) {
200
- Some(value) if !value.trim().is_empty() && !value.contains('{') => {}
201
- _ => {
202
- return Err(LifecycleError::StatePersist(format!(
203
- "cursor MCP overlay missing resolved {key} in json env \
204
- (cursor does not inherit parent TEAM_AGENT_* into the MCP child)"
205
- )));
206
- }
207
- }
208
- }
209
-
210
- let dir = workspace.join(".cursor");
211
- std::fs::create_dir_all(&dir)
212
- .map_err(|e| LifecycleError::StatePersist(format!("{}: {e}", dir.display())))?;
213
- let path = dir.join("mcp.json");
214
- let mut root = read_existing_mcp_json(&path);
215
- let servers = root
216
- .as_object_mut()
217
- .ok_or_else(|| {
218
- LifecycleError::StatePersist(format!(
219
- "{}: cursor mcp.json root must be an object",
220
- path.display()
221
- ))
222
- })?
223
- .entry("mcpServers")
224
- .or_insert_with(|| serde_json::json!({}));
225
- let servers = servers.as_object_mut().ok_or_else(|| {
226
- LifecycleError::StatePersist(format!("{}: mcpServers must be an object", path.display()))
227
- })?;
228
- // 0.5.67 overlay 曾误用 team-agent 作 inbound key。cursor 按 server 名
229
- // 给工具加命名空间,旧名会变成另一套工具。写新表时摘掉旧名。
230
- servers.remove("team-agent");
231
- let mut env_json = serde_json::Map::new();
232
- for (key, value) in &env {
233
- env_json.insert(key.clone(), serde_json::Value::String(value.clone()));
234
- }
235
- servers.insert(
236
- "team_orchestrator".to_string(),
237
- serde_json::json!({
238
- "command": command,
239
- "args": args,
240
- "env": env_json,
241
- }),
242
- );
243
- let body = serde_json::to_string_pretty(&root)
244
- .map_err(|e| LifecycleError::StatePersist(format!("serialize cursor mcp.json: {e}")))?;
245
- let tmp = dir.join("mcp.json.tmp");
246
- std::fs::write(&tmp, body.as_bytes())
247
- .map_err(|e| LifecycleError::StatePersist(format!("{}: {e}", tmp.display())))?;
248
- std::fs::rename(&tmp, &path)
249
- .map_err(|e| LifecycleError::StatePersist(format!("{}: {e}", path.display())))?;
250
- Ok(path)
251
- }
252
-
253
- /// ---
254
- /// purpose: 组出启用 team_orchestrator 的 cursor 命令行
255
- /// returns: 不带 workspace 参数的 argv,该命令按进程工作目录分片
256
- /// ---
257
- pub fn cursor_mcp_enable_argv() -> Vec<String> {
258
- vec![
259
- command_name(Provider::CursorAgent).to_string(),
260
- "mcp".to_string(),
261
- "enable".to_string(),
262
- "team_orchestrator".to_string(),
263
- ]
264
- }
265
-
266
- /// ---
267
- /// purpose: 在物理工作目录下执行 cursor 的 MCP 启用命令
268
- /// returns: 测试隔离环境或显式跳过标志下直接成功,避免写用户全局配置
269
- /// errors: 命令跑不起来或退出码非零时返回 RequirementUnmet,错误里只记输出长度不记内容
270
- /// ---
271
- pub fn cursor_mcp_enable_working_dir(workspace: &Path, project_root: Option<&Path>) -> PathBuf {
272
- match project_root {
273
- Some(root) => physical_workspace_path(root),
274
- None => physical_workspace_path(workspace),
275
- }
276
- }
277
-
278
- pub fn prepare_cursor_seat_mcp(
279
- workspace: &Path,
280
- agent_id: &str,
281
- mcp_config: &crate::provider::McpConfig,
282
- ) -> Result<Option<PathBuf>, LifecycleError> {
283
- if cursor_mcp_isolation_enabled() {
284
- let server = mcp_config
285
- .raw
286
- .get("team_orchestrator")
287
- .or_else(|| mcp_config.raw.get("team-agent"));
288
- let env = server.and_then(|s| s.get("env"));
289
- let field = |key| {
290
- env.and_then(|v| v.get(key))
291
- .and_then(serde_json::Value::as_str)
292
- };
293
- let physical = physical_workspace_path(workspace);
294
- if field("TEAM_AGENT_ID") != Some(agent_id)
295
- || field("TEAM_AGENT_WORKSPACE") != physical.to_str()
296
- {
297
- return Err(LifecycleError::RequirementUnmet(
298
- "cursor MCP resolved identity does not match the selected workspace/seat".into(),
299
- ));
300
- }
301
- let owner = field("TEAM_AGENT_OWNER_TEAM_ID")
302
- .filter(|s| !s.trim().is_empty())
303
- .ok_or_else(|| {
304
- LifecycleError::RequirementUnmet("cursor MCP missing owner team".into())
305
- })?;
306
- let project = materialize_cursor_mcp_project(&physical, agent_id, owner)?;
307
- apply_cursor_mcp_overlay(&project, mcp_config)?;
308
- validate_cursor_project_mcp(&physical, &project)?;
309
- Ok(Some(project))
310
- } else {
311
- apply_cursor_mcp_overlay(workspace, mcp_config)?;
312
- Ok(None)
313
- }
314
- }
315
-
316
- pub fn enable_cursor_workspace_mcp(
317
- workspace: &Path,
318
- project_root: Option<&Path>,
319
- ) -> Result<(), LifecycleError> {
320
- enable_cursor_workspace_mcp_with_profile(workspace, project_root, None)
321
- }
322
-
323
- /// Run Cursor's MCP enable command with the same profile-local environment
324
- /// policy as the worker spawn. In particular, subscription direct mode must
325
- /// not reintroduce proxy URL keys during this preparatory command.
326
- pub fn enable_cursor_workspace_mcp_with_profile(
327
- workspace: &Path,
328
- project_root: Option<&Path>,
329
- profile_launch: Option<&crate::provider::ProviderProfileLaunch>,
330
- ) -> Result<(), LifecycleError> {
331
- let physical = cursor_mcp_enable_working_dir(workspace, project_root);
332
- if project_root.is_some() {
333
- validate_cursor_project_mcp(&physical_workspace_path(workspace), &physical)?;
334
- }
335
- if skip_cursor_mcp_enable() {
336
- return Ok(());
337
- }
338
- let argv = cursor_mcp_enable_argv();
339
- let mut command = Command::new(&argv[0]);
340
- command.args(&argv[1..]).current_dir(&physical);
341
- apply_cursor_mcp_enable_profile_env(&mut command, profile_launch);
342
- let output = command.output().map_err(|e| {
343
- LifecycleError::RequirementUnmet(format!(
344
- "error: cannot run `{} mcp enable team_orchestrator`\n\
345
- reason: {e}\n\
346
- action: install cursor-agent on PATH (same binary as `agent`) and retry",
347
- argv[0]
348
- ))
349
- })?;
350
- if output.status.success() {
351
- // Approval is not native tool readiness. Recheck our root/config after
352
- // the external command; native loading/calls still need their own proof.
353
- if project_root.is_some() {
354
- validate_cursor_project_mcp(&physical_workspace_path(workspace), &physical)?;
355
- }
356
- return Ok(());
357
- }
358
- let stderr = String::from_utf8_lossy(&output.stderr);
359
- let stdout = String::from_utf8_lossy(&output.stdout);
360
- Err(LifecycleError::RequirementUnmet(format!(
361
- "error: `{} mcp enable team_orchestrator` failed (exit {:?})\n\
362
- reason: without enable, cursor keeps MCP as not loaded (needs approval)\n\
363
- cwd: {}\n\
364
- stdout_len: {}\n\
365
- stderr_len: {}\n\
366
- action: from that directory run `{} mcp enable team_orchestrator`",
367
- argv[0],
368
- output.status.code(),
369
- physical.display(),
370
- stdout.len(),
371
- stderr.len(),
372
- argv[0]
373
- )))
374
- }
375
-
376
- fn validate_cursor_project_mcp(workspace: &Path, project: &Path) -> Result<(), LifecycleError> {
377
- use std::io::Read;
378
- use std::os::unix::fs::{MetadataExt, OpenOptionsExt};
379
- validate_cursor_mcp_project_root(project)?;
380
- let refuse = || {
381
- LifecycleError::RequirementUnmet(
382
- "cursor MCP private config is missing, mismatched, or its server executable is unavailable; native tools are not ready".into(),
383
- )
384
- };
385
- let file = std::fs::OpenOptions::new()
386
- .read(true)
387
- .custom_flags(libc::O_NOFOLLOW | libc::O_CLOEXEC)
388
- .open(project.join(".cursor/mcp.json"))
389
- .map_err(|_| refuse())?;
390
- let metadata = file.metadata().map_err(|_| refuse())?;
391
- if !metadata.is_file() || metadata.nlink() != 1 || metadata.uid() != unsafe { libc::geteuid() }
392
- {
393
- return Err(refuse());
394
- }
395
- let mut bytes = Vec::new();
396
- file.take(65537)
397
- .read_to_end(&mut bytes)
398
- .map_err(|_| refuse())?;
399
- if bytes.len() > 65536 {
400
- return Err(refuse());
401
- }
402
- let config: serde_json::Value = serde_json::from_slice(&bytes).map_err(|_| refuse())?;
403
- let server = config
404
- .get("mcpServers")
405
- .and_then(|v| v.get("team_orchestrator"))
406
- .ok_or_else(refuse)?;
407
- let env = server.get("env").ok_or_else(refuse)?;
408
- for key in REQUIRED_IDENTITY_KEYS {
409
- if env
410
- .get(*key)
411
- .and_then(serde_json::Value::as_str)
412
- .is_none_or(|s| s.trim().is_empty() || s.contains('{'))
413
- {
414
- return Err(refuse());
415
- }
416
- }
417
- let seat = env
418
- .get("TEAM_AGENT_ID")
419
- .and_then(serde_json::Value::as_str)
420
- .ok_or_else(refuse)?;
421
- let owner = env
422
- .get("TEAM_AGENT_OWNER_TEAM_ID")
423
- .and_then(serde_json::Value::as_str)
424
- .ok_or_else(refuse)?;
425
- if env
426
- .get("TEAM_AGENT_WORKSPACE")
427
- .and_then(serde_json::Value::as_str)
428
- != workspace.to_str()
429
- || cursor_mcp_project_dir(workspace, seat)? != project
430
- || materialize_cursor_mcp_project(workspace, seat, owner)? != project
431
- {
432
- return Err(refuse());
433
- }
434
- let command = server
435
- .get("command")
436
- .and_then(serde_json::Value::as_str)
437
- .ok_or_else(refuse)?;
438
- let metadata = std::fs::metadata(command).map_err(|_| refuse())?;
439
- let current = std::env::current_exe()
440
- .and_then(std::fs::canonicalize)
441
- .map_err(|_| refuse())?;
442
- if !Path::new(command).is_absolute()
443
- || !metadata.is_file()
444
- || metadata.mode() & 0o111 == 0
445
- || std::fs::canonicalize(command).map_err(|_| refuse())? != current
446
- || server.get("args") != Some(&serde_json::json!(["mcp-server", "--workspace", workspace]))
447
- {
448
- return Err(refuse());
449
- }
450
- Ok(())
451
- }
452
-
453
- pub(crate) fn apply_cursor_mcp_enable_profile_env(
454
- command: &mut Command,
455
- profile_launch: Option<&crate::provider::ProviderProfileLaunch>,
456
- ) {
457
- let Some(profile_launch) = profile_launch else {
458
- return;
459
- };
460
- for key in &profile_launch.env_unset {
461
- command.env_remove(key);
462
- }
463
- for (key, value) in &profile_launch.env_overlay {
464
- command.env(key, value);
465
- }
466
- }
467
-
468
- /// ---
469
- /// purpose: 把 argv 里 workspace 参数的值换成物理路径
470
- /// params:
471
- /// argv: 就地改写;没有该参数时什么都不做
472
- /// ---
473
- pub fn apply_cursor_workspace_physical_path(argv: &mut [String], workspace: &Path) {
474
- let physical = physical_workspace_path(workspace);
475
- let Some(index) = argv.iter().position(|arg| arg == "--workspace") else {
476
- return;
477
- };
478
- if let Some(value) = argv.get_mut(index.saturating_add(1)) {
479
- *value = physical.to_string_lossy().into_owned();
480
- }
481
- }
482
-
483
- /// ---
484
- /// purpose: 隔离开时把 cursor --workspace 指到 per-seat 工程根,并用 --add-dir 挂上真 workspace
485
- /// ---
486
- pub fn apply_cursor_spawn_workspace_pointers(
487
- argv: &mut Vec<String>,
488
- workspace: &Path,
489
- agent_id: &str,
490
- ) -> Result<(), LifecycleError> {
491
- if cursor_mcp_isolation_enabled() {
492
- let project = physical_workspace_path(&cursor_mcp_project_dir(workspace, agent_id)?);
493
- apply_cursor_workspace_physical_path(argv, &project);
494
- let team = physical_workspace_path(workspace);
495
- let already = argv
496
- .windows(2)
497
- .any(|pair| pair[0] == "--add-dir" && Path::new(&pair[1]) == team.as_path());
498
- if !already {
499
- argv.push("--add-dir".to_string());
500
- argv.push(team.to_string_lossy().into_owned());
501
- }
502
- } else {
503
- apply_cursor_workspace_physical_path(argv, workspace);
504
- }
505
- Ok(())
506
- }
507
-
508
- fn skip_cursor_mcp_enable() -> bool {
509
- cfg!(test)
510
- || std::env::var_os("TEAM_AGENT_TEST_TMP").is_some()
511
- || matches!(
512
- std::env::var("TEAM_AGENT_SKIP_CURSOR_MCP_ENABLE").as_deref(),
513
- Ok("1") | Ok("true") | Ok("yes")
514
- )
515
- }
516
-
517
- fn read_existing_mcp_json(path: &Path) -> serde_json::Value {
518
- let Ok(text) = std::fs::read_to_string(path) else {
519
- return serde_json::json!({"mcpServers": {}});
520
- };
521
- serde_json::from_str(&text).unwrap_or_else(|_| serde_json::json!({"mcpServers": {}}))
522
- }