@team-agent/installer 0.5.126 → 0.5.128

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 (187) hide show
  1. package/Cargo.lock +1 -1
  2. package/Cargo.toml +1 -1
  3. package/README.md +11 -0
  4. package/crates/team-agent/src/cli/adapters.rs +3 -13
  5. package/crates/team-agent/src/cli/arguments.rs +4 -0
  6. package/crates/team-agent/src/cli/diagnose.rs +68 -91
  7. package/crates/team-agent/src/cli/emit.rs +61 -28
  8. package/crates/team-agent/src/cli/grok_slot.rs +88 -3
  9. package/crates/team-agent/src/cli/intent/runtime.rs +38 -9
  10. package/crates/team-agent/src/cli/intent/runtime_tests.rs +147 -11
  11. package/crates/team-agent/src/cli/intent/tests.rs +10 -12
  12. package/crates/team-agent/src/cli/intent.rs +1 -1
  13. package/crates/team-agent/src/cli/mod.rs +38 -9
  14. package/crates/team-agent/src/cli/models.rs +789 -186
  15. package/crates/team-agent/src/cli/schedule.rs +175 -0
  16. package/crates/team-agent/src/cli/send/persist.rs +0 -36
  17. package/crates/team-agent/src/cli/send/presentation.rs +1 -1
  18. package/crates/team-agent/src/cli/send.rs +108 -110
  19. package/crates/team-agent/src/cli/spec.rs +6 -0
  20. package/crates/team-agent/src/cli/status_port/brief.rs +5 -67
  21. package/crates/team-agent/src/cli/tests/leader_watch.rs +12 -62
  22. package/crates/team-agent/src/cli/tests/mod.rs +3 -3
  23. package/crates/team-agent/src/cli/tests/named_address.rs +11 -10
  24. package/crates/team-agent/src/cli/tests/run_delegation.rs +12 -9
  25. package/crates/team-agent/src/cli/tests/status_send.rs +70 -273
  26. package/crates/team-agent/src/compiler/tests.rs +39 -27
  27. package/crates/team-agent/src/compiler.rs +206 -6
  28. package/crates/team-agent/src/contract_runtime/backend.rs +181 -11
  29. package/crates/team-agent/src/contract_runtime/commands.rs +338 -7
  30. package/crates/team-agent/src/contract_runtime/config.rs +30 -19
  31. package/crates/team-agent/src/contract_runtime/config_tests.rs +24 -4
  32. package/crates/team-agent/src/contract_runtime/discovery.rs +12 -1
  33. package/crates/team-agent/src/contract_runtime/fork.rs +3 -10
  34. package/crates/team-agent/src/contract_runtime/framework.rs +28 -14
  35. package/crates/team-agent/src/contract_runtime/mcp.rs +19 -24
  36. package/crates/team-agent/src/contract_runtime/prompt.rs +2 -2
  37. package/crates/team-agent/src/contract_runtime/registry.rs +93 -130
  38. package/crates/team-agent/src/coordinator/steps/abnormal.rs +45 -5
  39. package/crates/team-agent/src/coordinator/tests/api_error_recovery.rs +8 -29
  40. package/crates/team-agent/src/coordinator/tests/energy.rs +4 -4
  41. package/crates/team-agent/src/coordinator/tests/health_sync.rs +44 -2
  42. package/crates/team-agent/src/coordinator/tests/main_preserved.rs +2 -2
  43. package/crates/team-agent/src/coordinator/tests/spine.rs +1 -1
  44. package/crates/team-agent/src/coordinator/tests/takeover.rs +3 -3
  45. package/crates/team-agent/src/coordinator/tick.rs +1 -89
  46. package/crates/team-agent/src/db/message_store.rs +7 -65
  47. package/crates/team-agent/src/direct_delivery.rs +368 -0
  48. package/crates/team-agent/src/inbox_store.rs +169 -0
  49. package/crates/team-agent/src/leader/registry.rs +5 -0
  50. package/crates/team-agent/src/leader/start.rs +2 -1
  51. package/crates/team-agent/src/leader/tests/lease_api.rs +2 -2
  52. package/crates/team-agent/src/leader/tests/prompt_contract.rs +1 -0
  53. package/crates/team-agent/src/lib.rs +2 -0
  54. package/crates/team-agent/src/lifecycle/launch/add_agent.rs +33 -0
  55. package/crates/team-agent/src/lifecycle/launch/add_agent_state.rs +206 -1
  56. package/crates/team-agent/src/lifecycle/launch/clone_agent.rs +28 -2
  57. package/crates/team-agent/src/lifecycle/launch/fork_agent.rs +36 -179
  58. package/crates/team-agent/src/lifecycle/launch/fork_entry.rs +87 -6
  59. package/crates/team-agent/src/lifecycle/launch/mcp_config.rs +13 -488
  60. package/crates/team-agent/src/lifecycle/launch/pi_mcp.rs +351 -81
  61. package/crates/team-agent/src/lifecycle/launch/pi_mcp_extension.js +3 -4
  62. package/crates/team-agent/src/lifecycle/launch/quick_start.rs +26 -2
  63. package/crates/team-agent/src/lifecycle/launch/spawn.rs +41 -31
  64. package/crates/team-agent/src/lifecycle/launch.rs +27 -12
  65. package/crates/team-agent/src/lifecycle/profile_launch.rs +318 -64
  66. package/crates/team-agent/src/lifecycle/restart/agent.rs +1 -0
  67. package/crates/team-agent/src/lifecycle/restart/avatar.rs +1798 -0
  68. package/crates/team-agent/src/lifecycle/restart/claude_fork.rs +163 -0
  69. package/crates/team-agent/src/lifecycle/restart/common.rs +298 -133
  70. package/crates/team-agent/src/lifecycle/restart/remove.rs +129 -0
  71. package/crates/team-agent/src/lifecycle/restart/selection.rs +5 -5
  72. package/crates/team-agent/src/lifecycle/restart.rs +4 -0
  73. package/crates/team-agent/src/lifecycle/role_config.rs +79 -27
  74. package/crates/team-agent/src/lifecycle/tests/acceptance_b_batch_red.rs +1 -1
  75. package/crates/team-agent/src/lifecycle/tests/agent_ops.rs +8 -12
  76. package/crates/team-agent/src/lifecycle/tests/behavioral_diff_264_red.rs +37 -240
  77. package/crates/team-agent/src/lifecycle/tests/claude_compatible_config_red.rs +325 -0
  78. package/crates/team-agent/src/lifecycle/tests/claude_profile_launch_red.rs +320 -8
  79. package/crates/team-agent/src/lifecycle/tests/cli_argv_routing_red.rs +329 -70
  80. package/crates/team-agent/src/lifecycle/tests/clone_fork_copilot_perms_red.rs +378 -4
  81. package/crates/team-agent/src/lifecycle/tests/communication_mode_runtime_contract_red.rs +25 -39
  82. package/crates/team-agent/src/lifecycle/tests/copilot_provider_red.rs +7 -11
  83. package/crates/team-agent/src/lifecycle/tests/core.rs +39 -36
  84. package/crates/team-agent/src/lifecycle/tests/f032_startup_prompt_best_effort_red.rs +22 -8
  85. package/crates/team-agent/src/lifecycle/tests/fixtures/pi_native_mcp_host.mjs +2 -1
  86. package/crates/team-agent/src/lifecycle/tests/grok_require_explicit_model_red.rs +15 -133
  87. package/crates/team-agent/src/lifecycle/tests/harvest2_a_batch_red.rs +60 -20
  88. package/crates/team-agent/src/lifecycle/tests/host_cotenant_death_p0_contract.rs +30 -67
  89. package/crates/team-agent/src/lifecycle/tests/issue238_codex_effort_contract.rs +3 -31
  90. package/crates/team-agent/src/lifecycle/tests/lane_ops.rs +194 -169
  91. package/crates/team-agent/src/lifecycle/tests/launch_spawn.rs +164 -282
  92. package/crates/team-agent/src/lifecycle/tests/lifecycle_rollback_red.rs +15 -78
  93. package/crates/team-agent/src/lifecycle/tests/main_preserved.rs +7 -7
  94. package/crates/team-agent/src/lifecycle/tests/mcp_tool_name_format_red.rs +6 -6
  95. package/crates/team-agent/src/lifecycle/tests/phase_b_contracts.rs +75 -69
  96. package/crates/team-agent/src/lifecycle/tests/phase_golden.rs +2 -10
  97. package/crates/team-agent/src/lifecycle/tests/pi_catalog_red.rs +18 -6
  98. package/crates/team-agent/src/lifecycle/tests/pi_command_red.rs +70 -52
  99. package/crates/team-agent/src/lifecycle/tests/pi_compiler_red.rs +210 -0
  100. package/crates/team-agent/src/lifecycle/tests/pi_dual_entry_red.rs +157 -4
  101. package/crates/team-agent/src/lifecycle/tests/pi_executable_mcp_red.rs +526 -10
  102. package/crates/team-agent/src/lifecycle/tests/pi_fork_session_inheritance_red.rs +1419 -0
  103. package/crates/team-agent/src/lifecycle/tests/pi_lifecycle_doctor_red.rs +68 -1
  104. package/crates/team-agent/src/lifecycle/tests/pi_native_mcp_capability_red.rs +34 -12
  105. package/crates/team-agent/src/lifecycle/tests/pi_session_delivery_red.rs +94 -2
  106. package/crates/team-agent/src/lifecycle/tests/quick_start_worker_readiness_red.rs +14 -15
  107. package/crates/team-agent/src/lifecycle/tests/restart.rs +12 -153
  108. package/crates/team-agent/src/lifecycle/tests/restart_build_before_destroy_0540_contract.rs +27 -5
  109. package/crates/team-agent/src/lifecycle/tests/restart_liveness_red.rs +59 -10
  110. package/crates/team-agent/src/lifecycle/tests/restart_rebind_hotfix_252_red.rs +47 -6
  111. package/crates/team-agent/src/lifecycle/tests/restart_session_capture_red.rs +3 -1374
  112. package/crates/team-agent/src/lifecycle/tests/resume_recover_red.rs +27 -10
  113. package/crates/team-agent/src/lifecycle/tests/status_credential_redaction_contract.rs +7 -7
  114. package/crates/team-agent/src/lifecycle/tests/subprep_codex_trust_roundtrip_red.rs +67 -313
  115. package/crates/team-agent/src/lifecycle/tests/swallow_batch4_semantics_red.rs +254 -2
  116. package/crates/team-agent/src/lifecycle/tests/team_in_team_identity_scope_red.rs +2 -2
  117. package/crates/team-agent/src/lifecycle/tests/team_in_team_sibling_quick_start_red.rs +1 -1
  118. package/crates/team-agent/src/lifecycle/tests/team_in_team_state_scope_red.rs +2 -2
  119. package/crates/team-agent/src/lifecycle/tests/team_key_retirement_txn_red.rs +4 -4
  120. package/crates/team-agent/src/lifecycle/tests/upgrade_compat_0211_red.rs +74 -19
  121. package/crates/team-agent/src/lifecycle/tests/verify_rs031_window_consistency_red.rs +38 -39
  122. package/crates/team-agent/src/lifecycle/tests/worker_spawn_env_red.rs +51 -88
  123. package/crates/team-agent/src/lifecycle/tests.rs +3 -4
  124. package/crates/team-agent/src/lifecycle/types.rs +18 -5
  125. package/crates/team-agent/src/lifecycle/worker_command_context.rs +43 -50
  126. package/crates/team-agent/src/mcp_server/readiness.rs +130 -149
  127. package/crates/team-agent/src/mcp_server/readiness_tests.rs +86 -109
  128. package/crates/team-agent/src/mcp_server/tests/scoped.rs +21 -31
  129. package/crates/team-agent/src/mcp_server/tests/send.rs +72 -107
  130. package/crates/team-agent/src/mcp_server/tests/tools.rs +10 -12
  131. package/crates/team-agent/src/mcp_server/tools.rs +70 -196
  132. package/crates/team-agent/src/messaging/delivery.rs +143 -253
  133. package/crates/team-agent/src/messaging/delivery_consumption_tests.rs +5 -5
  134. package/crates/team-agent/src/messaging/helpers.rs +1 -19
  135. package/crates/team-agent/src/messaging/mod.rs +1 -0
  136. package/crates/team-agent/src/messaging/reminders.rs +316 -0
  137. package/crates/team-agent/src/messaging/scheduler.rs +7 -1
  138. package/crates/team-agent/src/messaging/tests/dup_inject.rs +255 -150
  139. package/crates/team-agent/src/messaging/tests/main_preserved.rs +2 -2
  140. package/crates/team-agent/src/messaging/tests/runtime.rs +9 -115
  141. package/crates/team-agent/src/messaging/tests/spine.rs +44 -24
  142. package/crates/team-agent/src/messaging/tests/wave2.rs +13 -20
  143. package/crates/team-agent/src/messaging/types.rs +1 -0
  144. package/crates/team-agent/src/provider/adapter.rs +208 -254
  145. package/crates/team-agent/src/provider/adapters/claude.rs +78 -0
  146. package/crates/team-agent/src/provider/adapters/claude_fork.rs +131 -0
  147. package/crates/team-agent/src/provider/adapters/mod.rs +7 -7
  148. package/crates/team-agent/src/provider/adapters/pi.rs +106 -0
  149. package/crates/team-agent/src/provider/classify.rs +1 -66
  150. package/crates/team-agent/src/provider/model_catalog.rs +201 -13
  151. package/crates/team-agent/src/provider/session/capture.rs +111 -111
  152. package/crates/team-agent/src/provider/session/context_fork/claude.rs +123 -0
  153. package/crates/team-agent/src/provider/session/context_fork/outcome.rs +10 -18
  154. package/crates/team-agent/src/provider/session/context_fork.rs +87 -74
  155. package/crates/team-agent/src/provider/session/mod.rs +2 -3
  156. package/crates/team-agent/src/provider/session_scan/claude.rs +523 -0
  157. package/crates/team-agent/src/provider/session_scan/common.rs +47 -47
  158. package/crates/team-agent/src/provider/session_scan/pi.rs +34 -1
  159. package/crates/team-agent/src/provider/session_scan.rs +13 -13
  160. package/crates/team-agent/src/provider/tests/adapter.rs +127 -121
  161. package/crates/team-agent/src/provider/tests/classify.rs +0 -53
  162. package/crates/team-agent/src/provider/tests/command_helpers.rs +11 -19
  163. package/crates/team-agent/src/provider/tests/idle.rs +13 -19
  164. package/crates/team-agent/src/provider/types.rs +0 -4
  165. package/crates/team-agent/src/provider/wire.rs +34 -20
  166. package/crates/team-agent/src/state/abnormal_watch/tests/mod.rs +1 -1
  167. package/crates/team-agent/src/state/persist.rs +6 -6
  168. package/crates/team-agent/src/state/projection.rs +1 -1
  169. package/crates/team-agent/src/state/repository/tests.rs +2 -2
  170. package/crates/team-agent/src/test_support/models_timeout_fixture.rs +144 -0
  171. package/crates/team-agent/src/tmux_backend/tests.rs +0 -151
  172. package/crates/team-agent/src/tmux_backend.rs +9 -43
  173. package/package.json +4 -4
  174. package/skills/team-agent/SKILL.md +7 -8
  175. package/skills/team-agent/command-coverage.json +15 -1
  176. package/skills/team-agent/docs/reference/team-agent-operator.md +5 -3
  177. package/skills/team-agent/references/team-agent-operator.md +5 -3
  178. package/crates/team-agent/src/lifecycle/launch/grok_per_seat.rs +0 -260
  179. package/crates/team-agent/src/lifecycle/tests/codex_weak_window_attribution_red.rs +0 -683
  180. package/crates/team-agent/src/lifecycle/tests/grok_effort_argv_red.rs +0 -247
  181. package/crates/team-agent/src/lifecycle/tests/grok_mcp_overlay_red.rs +0 -507
  182. package/crates/team-agent/src/lifecycle/tests/grok_restart_resume_red.rs +0 -300
  183. package/crates/team-agent/src/provider/adapters/codex.rs +0 -101
  184. package/crates/team-agent/src/provider/adapters/grok.rs +0 -67
  185. package/crates/team-agent/src/provider/session/context_fork/codex.rs +0 -514
  186. package/crates/team-agent/src/provider/session_scan/codex.rs +0 -326
  187. package/crates/team-agent/src/provider/session_scan/grok.rs +0 -288
@@ -5,7 +5,7 @@
5
5
  //! - name: WorkerCommandAgent
6
6
  //! what: 从 YAML 或 JSON 读出的单 agent 命令上下文
7
7
  //! - name: compile_worker_system_prompt
8
- //! what: 按身份、runtime 契约、通信模式、角色正文、输出契约、权限说明拼系统提示词
8
+ //! what: 按身份、runtime 契约、通信模式、角色正文拼系统提示词
9
9
  //! depends:
10
10
  //! - crate::communication_mode
11
11
  //! boundary:
@@ -29,10 +29,9 @@ All communication must go through Team Agent MCP tools.
29
29
 
30
30
  - Teammate: {send_message}(to='<agent_id>', content='...')
31
31
  - Broadcast: {send_message}(to='*', content='...')
32
- - Complete: {report_result}(summary='...') — call exactly once
33
32
  - Optional roster/status: {get_team_status} — use only when useful; it is not a communication or completion prerequisite.
34
33
 
35
- Only these three Team Agent MCP tools are available. Lifecycle management remains on the separately authorized operator CLI.
34
+ Use {send_message} exclusively for findings, progress, questions, and completion updates. Lifecycle management remains on the separately authorized operator CLI.
36
35
 
37
36
  ## Rules:
38
37
 
@@ -40,23 +39,12 @@ Only these three Team Agent MCP tools are available. Lifecycle management remain
40
39
  - Do not reply to pure ACKs, greetings, or unchanged status notices (such as "paused" or "waiting"); after reporting a blocker once or completing a task, remain silent until a new actionable instruction arrives.
41
40
  - On 500/529/rate limits, retry only after 1-2 minutes."#;
42
41
 
43
- // 0.4.11 trimmed: the runtime contract section above already covers
44
- // send_message signatures and report_result exactly-once. The output
45
- // contract now only carries the RESULT-ENVELOPE-SPECIFIC delivery
46
- // semantics (leader-attach dependence + fallback status) that the
47
- // generic runtime section deliberately leaves out.
48
- const RESULT_ENVELOPE_OUTPUT_CONTRACT: &str =
49
- "Final completion must call {report_result} exactly once with a short summary \
50
- and optional status/changes/tests; the MCP runtime injects the result into the attached leader pane. \
51
- If no leader is attached, the tool returns a fallback/failed result instead of completion.";
52
-
53
42
  pub(crate) struct WorkerCommandAgent {
54
43
  id: Option<String>,
55
44
  provider: Provider,
56
45
  role: Option<String>,
57
46
  system_prompt_inline: Option<String>,
58
47
  system_prompt_file: Option<String>,
59
- output_contract_format: Option<String>,
60
48
  communication_mode: CommunicationMode,
61
49
  /// Missing `dangerously_skip_permissions` defaults to false; only explicit true enables bypass.
62
50
  dangerously_skip_permissions: bool,
@@ -100,11 +88,6 @@ impl WorkerCommandAgent {
100
88
  .filter(|value| value.is_truthy())
101
89
  .and_then(crate::model::yaml::Value::as_str)
102
90
  .map(str::to_string),
103
- output_contract_format: agent
104
- .get("output_contract")
105
- .and_then(|contract| contract.get("format"))
106
- .and_then(crate::model::yaml::Value::as_str)
107
- .map(str::to_string),
108
91
  communication_mode: communication_mode(
109
92
  agent
110
93
  .get("communication_mode")
@@ -154,11 +137,6 @@ impl WorkerCommandAgent {
154
137
  .and_then(serde_json::Value::as_str)
155
138
  .filter(|value| !value.is_empty())
156
139
  .map(str::to_string),
157
- output_contract_format: agent
158
- .get("output_contract")
159
- .and_then(|contract| contract.get("format"))
160
- .and_then(serde_json::Value::as_str)
161
- .map(str::to_string),
162
140
  communication_mode: communication_mode(
163
141
  agent
164
142
  .get("communication_mode")
@@ -189,19 +167,14 @@ pub(crate) fn compile_worker_system_prompt(
189
167
  "{}()",
190
168
  mcp_tool_name(agent.provider, "team_orchestrator", "get_team_status")
191
169
  );
192
- let report_result = mcp_tool_name(agent.provider, "team_orchestrator", "report_result");
193
- let runtime_contract =
194
- runtime_contract_section(&send_message, &report_result, &get_team_status);
170
+ let runtime_contract = runtime_contract_section(&send_message, &get_team_status);
195
171
  let communication_contract = agent.communication_mode.runtime_contract(&send_message);
196
- let mut chunks = vec![
172
+ let chunks = [
197
173
  identity_section(agent),
198
174
  runtime_contract,
199
175
  communication_contract,
200
176
  role_body(agent)?,
201
177
  ];
202
- if let Some(contract) = output_contract(agent, &report_result) {
203
- chunks.push(contract);
204
- }
205
178
  Ok(chunks
206
179
  .into_iter()
207
180
  .filter(|chunk| !chunk.is_empty())
@@ -234,14 +207,9 @@ fn mcp_tool_name(provider: Provider, server: &str, tool: &str) -> String {
234
207
  }
235
208
  }
236
209
 
237
- pub(crate) fn runtime_contract_section(
238
- send_message: &str,
239
- report_result: &str,
240
- get_team_status: &str,
241
- ) -> String {
210
+ pub(crate) fn runtime_contract_section(send_message: &str, get_team_status: &str) -> String {
242
211
  RUNTIME_CONTRACT_SECTION
243
212
  .replace("{send_message}", send_message)
244
- .replace("{report_result}", report_result)
245
213
  .replace("{get_team_status}", get_team_status)
246
214
  }
247
215
 
@@ -281,11 +249,6 @@ fn role_body(agent: &WorkerCommandAgent) -> Result<String, LifecycleError> {
281
249
  Ok(chunks.join("\n\n"))
282
250
  }
283
251
 
284
- fn output_contract(agent: &WorkerCommandAgent, report_result: &str) -> Option<String> {
285
- (agent.output_contract_format.as_deref() == Some("result_envelope_v1"))
286
- .then(|| RESULT_ENVELOPE_OUTPUT_CONTRACT.replace("{report_result}", report_result))
287
- }
288
-
289
252
  #[cfg(test)]
290
253
  mod tests {
291
254
  use super::*;
@@ -293,7 +256,7 @@ mod tests {
293
256
  #[test]
294
257
  fn system_prompt_uses_identity_then_runtime_contract_python_order() {
295
258
  // #264 D6: Python truth source (prompt.py:39, live ps confirmed) builds
296
- // chunks = [identity, TEAMMATE_SYSTEM_PROMPT, role_body, output].
259
+ // chunks = [identity, TEAMMATE_SYSTEM_PROMPT, role_body].
297
260
  // The previous assertion locked the inverted contract-first order with no
298
261
  // Python evidence; this is the corrected golden.
299
262
  // 0.3.5 union (copilot v2 C-1 / B2 MUST-4 行为层守): identity 必须 FIRST —
@@ -305,7 +268,6 @@ mod tests {
305
268
  role: Some("Runtime Developer".to_string()),
306
269
  system_prompt_inline: Some("Implement the assigned slice.".to_string()),
307
270
  system_prompt_file: None,
308
- output_contract_format: Some("result_envelope_v1".to_string()),
309
271
  communication_mode: CommunicationMode::default(),
310
272
  dangerously_skip_permissions: false,
311
273
  };
@@ -320,18 +282,45 @@ mod tests {
320
282
  .find(RUNTIME_CONTRACT_SECTION.lines().next().unwrap_or(""))
321
283
  .unwrap();
322
284
  let role = prompt.find("Implement the assigned slice.").unwrap();
323
- let output = prompt
324
- .find("Final completion must call team_orchestrator.report_result exactly once")
325
- .unwrap();
326
- assert!(identity < runtime && runtime < role && role < output);
285
+ assert!(identity < runtime && runtime < role);
286
+ assert!(!prompt.contains("report_result"));
327
287
  let slowdown_phrase = format!("500/{}", 500 + 29);
328
288
  assert!(prompt.contains(&slowdown_phrase));
329
289
  assert!(prompt.contains("Runtime Developer"));
330
290
  }
331
291
 
292
+ #[test]
293
+ fn output_contract_metadata_does_not_add_completion_instructions() {
294
+ let json = serde_json::json!({
295
+ "id": "worker",
296
+ "output_contract": { "format": "result_envelope_v1" },
297
+ });
298
+ let yaml = crate::model::yaml::loads(
299
+ "id: worker\noutput_contract:\n format: result_envelope_v1\n",
300
+ )
301
+ .unwrap();
302
+ for agent in [
303
+ WorkerCommandAgent::from_json(&json, None, Provider::Codex).unwrap(),
304
+ WorkerCommandAgent::from_yaml(&yaml, None, Provider::Codex).unwrap(),
305
+ ] {
306
+ let prompt = compile_worker_system_prompt(&agent).unwrap();
307
+ assert!(!prompt.contains("report_result"));
308
+ assert!(prompt.contains("send_message"));
309
+ }
310
+ }
311
+
332
312
  #[test]
333
313
  fn all_provider_mode_prompts_share_silence_contract_without_unconditional_replies() {
334
- for provider in [Provider::Codex, Provider::Pi] {
314
+ for provider in [
315
+ Provider::Claude,
316
+ Provider::ClaudeCode,
317
+ Provider::Codex,
318
+ Provider::Copilot,
319
+ Provider::GeminiCli,
320
+ Provider::Grok,
321
+ Provider::Pi,
322
+ Provider::Fake,
323
+ ] {
335
324
  for mode in CommunicationMode::ALL.iter().copied() {
336
325
  let agent = WorkerCommandAgent {
337
326
  id: Some("worker".to_string()),
@@ -339,11 +328,15 @@ mod tests {
339
328
  role: Some("developer".to_string()),
340
329
  system_prompt_inline: Some("worker body".to_string()),
341
330
  system_prompt_file: None,
342
- output_contract_format: Some("result_envelope_v1".to_string()),
343
331
  communication_mode: mode,
344
332
  dangerously_skip_permissions: false,
345
333
  };
346
334
  let prompt = compile_worker_system_prompt(&agent).unwrap();
335
+ assert!(!prompt.contains("report_result"));
336
+ let send = mcp_tool_name(provider, "team_orchestrator", "send_message");
337
+ assert!(prompt.contains(&format!(
338
+ "Use {send} exclusively for findings, progress, questions, and completion updates."
339
+ )));
347
340
  assert!(prompt.contains(
348
341
  "Do not reply to pure ACKs, greetings, or unchanged status notices (such as \"paused\" or \"waiting\"); after reporting a blocker once or completing a task, remain silent until a new actionable instruction arrives."
349
342
  ));
@@ -1,11 +1,11 @@
1
- //! A flushed MCP handshake is admission to first business, not client consumption.
2
- //! Receipts are scope- and live-process-bound; no pane input or tools/call probe.
1
+ //! Flushed MCP handshakes are diagnostic evidence, not admission to pane input.
2
+ //! Each server owns a receipt; foreign writers cannot replace a live server's evidence.
3
3
 
4
4
  use std::fs::{File, OpenOptions};
5
5
  use std::io::{Read, Write};
6
6
  use std::os::unix::fs::{DirBuilderExt, MetadataExt, OpenOptionsExt};
7
7
  use std::path::{Path, PathBuf};
8
- use std::time::Duration;
8
+ use std::time::{Duration, Instant};
9
9
 
10
10
  use chrono::{DateTime, Utc};
11
11
  use serde::{Deserialize, Serialize};
@@ -105,30 +105,38 @@ fn read_json(path: &Path, require_private: bool) -> Result<Value> {
105
105
  serde_json::from_slice(&data).map_err(|_| "mcp_handshake_invalid")
106
106
  }
107
107
 
108
- /// Per-server receipts plus the pre-#372 single-slot file, if one remains.
109
- fn receipt_paths(dir: &Path, name: &str) -> Vec<PathBuf> {
110
- let Ok(entries) = std::fs::read_dir(dir) else {
111
- return Vec::new();
112
- };
113
- entries
114
- .flatten()
115
- .filter_map(|entry| {
116
- let file = entry.file_name().into_string().ok()?;
117
- let rest = file.strip_prefix(name)?.strip_suffix(".json")?;
118
- let per_server = rest
119
- .strip_prefix('.')
120
- .is_some_and(|pid| pid.parse::<u32>().is_ok());
121
- (rest.is_empty() || per_server).then(|| entry.path())
122
- })
123
- .collect()
108
+ struct ReceiptLock(File);
109
+ impl Drop for ReceiptLock {
110
+ fn drop(&mut self) {
111
+ let _ = crate::platform::file_lock::unlock(&self.0);
112
+ }
124
113
  }
125
114
 
126
- fn read_receipt(path: &Path) -> Result<Handshake> {
127
- serde_json::from_value(read_json(path, true)?).map_err(|_| "mcp_handshake_invalid")
115
+ fn lock(path: &Path) -> Result<ReceiptLock> {
116
+ let file = OpenOptions::new()
117
+ .create(true)
118
+ .truncate(false)
119
+ .read(true)
120
+ .write(true)
121
+ .mode(0o600)
122
+ .custom_flags(libc::O_NOFOLLOW)
123
+ .open(path)
124
+ .map_err(|_| "mcp_handshake_lock")?;
125
+ private_metadata(path, false)?;
126
+ let start = Instant::now();
127
+ loop {
128
+ match crate::platform::file_lock::try_lock_once_nonblocking(&file) {
129
+ Ok(true) => return Ok(ReceiptLock(file)),
130
+ Ok(false) if start.elapsed() < Duration::from_millis(500) => {
131
+ std::thread::sleep(Duration::from_millis(10));
132
+ }
133
+ _ => return Err("mcp_handshake_lock"),
134
+ }
135
+ }
128
136
  }
129
137
 
130
138
  /// Only the actual stdio writer calls this after initialize and tools/list flush.
131
- /// File publication failure stays unready; it never becomes an input bypass.
139
+ /// Publication failure affects readiness evidence only, not message delivery.
132
140
  pub(crate) fn publish(
133
141
  workspace: &Path,
134
142
  owner: &str,
@@ -189,29 +197,31 @@ pub(crate) fn publish(
189
197
  };
190
198
  let dir = directory(&workspace, true)?;
191
199
  let before = private_metadata(&dir, true)?;
192
- let name = key(owner, agent);
193
- // #372: each server owns its receipt, so a foreign or exited server that
194
- // inherited this seat's identity can never erase the seat's live evidence.
195
- let path = dir.join(format!("{name}.{}.json", std::process::id()));
200
+ let name = format!("{}.{}", key(owner, agent), std::process::id());
201
+ let path = dir.join(format!("{name}.json"));
202
+ let _lock = lock(&dir.join(format!("{name}.lock")))?;
196
203
  match std::fs::symlink_metadata(&path) {
197
204
  Ok(_) => {
198
- private_metadata(&path, false)?;
205
+ let old: Handshake = serde_json::from_value(read_json(&path, true)?)
206
+ .map_err(|_| "mcp_handshake_invalid")?;
207
+ if old.workspace != workspace
208
+ || old.owner != owner
209
+ || old.agent != agent
210
+ || old.schema != 1
211
+ {
212
+ return Err("mcp_handshake_scope_mismatch");
213
+ }
214
+ let old_started = DateTime::parse_from_rfc3339(&old.started_at)
215
+ .map_err(|_| "mcp_handshake_invalid")?;
216
+ if old.server.identity.birth_identity == receipt.server.identity.birth_identity
217
+ && old_started > started_at
218
+ {
219
+ return Ok(());
220
+ }
199
221
  }
200
222
  Err(error) if error.kind() == std::io::ErrorKind::NotFound => {}
201
223
  Err(_) => return Err("mcp_handshake_invalid"),
202
224
  }
203
- for sibling in receipt_paths(&dir, &name) {
204
- let exited = sibling != path
205
- && read_receipt(&sibling).is_ok_and(|old| {
206
- matches!(
207
- sample_process(&old.server),
208
- ProcessState::Exited | ProcessState::Replaced
209
- )
210
- });
211
- if exited {
212
- let _ = std::fs::remove_file(&sibling);
213
- }
214
- }
215
225
  let temp = dir.join(format!(
216
226
  "{name}.{}-{}.tmp",
217
227
  std::process::id(),
@@ -239,23 +249,10 @@ pub(crate) fn publish(
239
249
  })();
240
250
  if result.is_err() {
241
251
  let _ = std::fs::remove_file(&temp);
242
- return result;
243
252
  }
244
- resume_parked(&workspace, owner, agent);
245
253
  result
246
254
  }
247
255
 
248
- /// A fresh receipt resumes this seat's parked messages. A foreign server's
249
- /// receipt only causes one cheap re-park: delivery re-runs full preflight.
250
- fn resume_parked(workspace: &Path, owner: &str, agent: &str) {
251
- if !workspace.join(".team/runtime/team.db").exists() {
252
- return;
253
- }
254
- if let Ok(store) = crate::message_store::MessageStore::open(workspace) {
255
- let _ = store.requeue_parked_deliveries(agent, owner);
256
- }
257
- }
258
-
259
256
  #[derive(Debug)]
260
257
  pub(crate) struct Admission {
261
258
  server: ProcessStamp,
@@ -264,8 +261,7 @@ pub(crate) struct Admission {
264
261
  pane_id: String,
265
262
  }
266
263
 
267
- /// Observation-only preflight, before claiming a delivery attempt. Missing facts
268
- /// perform no input; the delivery owner releases only its exact claim for retry.
264
+ /// Observe readiness without gating delivery. A bad candidate never hides a good one.
269
265
  pub(crate) fn preflight(
270
266
  workspace: &Path,
271
267
  owner: Option<&str>,
@@ -298,11 +294,62 @@ pub(crate) fn preflight(
298
294
  .canonicalize()
299
295
  .map_err(|_| "mcp_handshake_workspace")?;
300
296
  let dir = directory(&workspace, false)?;
297
+ let name = key(owner, agent_id);
298
+ // Older servers publish one slot per seat. Keep it readable during upgrades.
299
+ let mut paths = vec![dir.join(format!("{name}.json"))];
300
+ let prefix = format!("{name}.");
301
+ for entry in std::fs::read_dir(&dir).map_err(|_| "mcp_handshake_directory")? {
302
+ let entry = entry.map_err(|_| "mcp_handshake_directory")?;
303
+ let filename = entry.file_name();
304
+ let filename = filename.to_string_lossy();
305
+ if filename
306
+ .strip_prefix(&prefix)
307
+ .and_then(|suffix| suffix.strip_suffix(".json"))
308
+ .and_then(|pid| pid.parse::<u32>().ok())
309
+ .is_some()
310
+ {
311
+ paths.push(entry.path());
312
+ }
313
+ }
314
+ let mut error = "mcp_handshake_missing";
315
+ for path in paths {
316
+ match validate_receipt(&path, &workspace, owner, agent_id, agent, config) {
317
+ Ok(admission) => return Ok(Some(admission)),
318
+ Err(reason) => error = reason,
319
+ }
320
+ }
321
+ Err(error)
322
+ }
323
+
324
+ fn validate_receipt(
325
+ path: &Path,
326
+ workspace: &Path,
327
+ owner: &str,
328
+ agent_id: &str,
329
+ agent: &Value,
330
+ config: &str,
331
+ ) -> Result<Admission> {
332
+ let receipt: Handshake =
333
+ serde_json::from_value(read_json(path, true)?).map_err(|_| "mcp_handshake_invalid")?;
334
+ if receipt.schema != 1
335
+ || receipt.workspace != workspace
336
+ || receipt.owner != owner
337
+ || receipt.agent != agent_id
338
+ {
339
+ return Err("mcp_handshake_scope_mismatch");
340
+ }
301
341
  let spawned_at = agent
302
342
  .get("spawned_at")
303
343
  .and_then(Value::as_str)
304
344
  .and_then(|s| DateTime::parse_from_rfc3339(s).ok())
305
345
  .ok_or("mcp_handshake_spawn_missing")?;
346
+ let started_at =
347
+ DateTime::parse_from_rfc3339(&receipt.started_at).map_err(|_| "mcp_handshake_invalid")?;
348
+ let flushed_at =
349
+ DateTime::parse_from_rfc3339(&receipt.flushed_at).map_err(|_| "mcp_handshake_invalid")?;
350
+ if started_at < spawned_at || flushed_at < started_at {
351
+ return Err("mcp_handshake_stale_spawn");
352
+ }
306
353
  let expected_config = workspace
307
354
  .join(".team/runtime/mcp")
308
355
  .join(format!("{agent_id}.json"));
@@ -327,12 +374,14 @@ pub(crate) fn preflight(
327
374
  .get("command")
328
375
  .and_then(Value::as_str)
329
376
  .ok_or("mcp_handshake_config_missing")?;
330
- if !Path::new(expected).is_absolute() {
377
+ if !Path::new(expected).is_absolute()
378
+ || Path::new(expected)
379
+ .canonicalize()
380
+ .map_err(|_| "mcp_handshake_image")?
381
+ != receipt.server.identity.executable
382
+ {
331
383
  return Err("mcp_handshake_image");
332
384
  }
333
- let expected = Path::new(expected)
334
- .canonicalize()
335
- .map_err(|_| "mcp_handshake_image")?;
336
385
  let env = server_config
337
386
  .get("env")
338
387
  .ok_or("mcp_handshake_config_missing")?;
@@ -342,8 +391,8 @@ pub(crate) fn preflight(
342
391
  .get("TEAM_AGENT_WORKSPACE")
343
392
  .and_then(Value::as_str)
344
393
  .and_then(|s| Path::new(s).canonicalize().ok())
345
- .as_ref()
346
- != Some(&workspace)
394
+ .as_deref()
395
+ != Some(workspace)
347
396
  {
348
397
  return Err("mcp_handshake_config_scope");
349
398
  }
@@ -359,99 +408,32 @@ pub(crate) fn preflight(
359
408
  .ok_or("mcp_handshake_carrier_missing")?;
360
409
  let carrier_birth =
361
410
  process_birth_identity(carrier).map_err(|_| "mcp_handshake_carrier_missing")?;
362
- let admit = |receipt: &Handshake| -> Result<()> {
363
- if receipt.schema != 1
364
- || receipt.workspace != workspace
365
- || receipt.owner != owner
366
- || receipt.agent != agent_id
411
+ if sample_process(&receipt.server) != ProcessState::Alive {
412
+ return Err("mcp_handshake_server_not_live");
413
+ }
414
+ let mut parent = receipt.server.parent;
415
+ let mut matched = false;
416
+ for ancestor in receipt.ancestors.iter().take(MAX_NATIVE_PARENT_HOPS) {
417
+ if ancestor.pid != parent
418
+ || process_birth_identity(parent).ok().as_deref() != Some(ancestor.birth.as_str())
367
419
  {
368
- return Err("mcp_handshake_scope_mismatch");
369
- }
370
- let started_at = DateTime::parse_from_rfc3339(&receipt.started_at)
371
- .map_err(|_| "mcp_handshake_invalid")?;
372
- let flushed_at = DateTime::parse_from_rfc3339(&receipt.flushed_at)
373
- .map_err(|_| "mcp_handshake_invalid")?;
374
- if started_at < spawned_at || flushed_at < started_at {
375
- return Err("mcp_handshake_stale_spawn");
376
- }
377
- if receipt.server.identity.executable != expected {
378
- return Err("mcp_handshake_image");
379
- }
380
- if sample_process(&receipt.server) != ProcessState::Alive {
381
- return Err("mcp_handshake_server_not_live");
382
- }
383
- let mut parent = receipt.server.parent;
384
- let mut matched = false;
385
- for ancestor in receipt.ancestors.iter().take(MAX_NATIVE_PARENT_HOPS) {
386
- if ancestor.pid != parent
387
- || process_birth_identity(parent).ok().as_deref() != Some(ancestor.birth.as_str())
388
- {
389
- return Err("mcp_handshake_ancestry");
390
- }
391
- if parent == carrier {
392
- matched = true;
393
- break;
394
- }
395
- parent = crate::platform::argv::parent_pid(parent).unwrap_or(0);
396
- }
397
- if !matched || sample_process(&receipt.server) != ProcessState::Alive {
398
420
  return Err("mcp_handshake_ancestry");
399
421
  }
400
- Ok(())
401
- };
402
- // #372: admit the newest live receipt descended from this seat's pane.
403
- // Receipts of foreign servers (inherited identity, other process trees)
404
- // are dropped before any process probe; they cannot mask the seat's own.
405
- let mut receipts: Vec<_> = receipt_paths(&dir, &key(owner, agent_id))
406
- .into_iter()
407
- .map(|path| read_receipt(&path))
408
- .collect();
409
- receipts.sort_by_key(|receipt| {
410
- std::cmp::Reverse(
411
- receipt
412
- .as_ref()
413
- .ok()
414
- .and_then(|r| DateTime::parse_from_rfc3339(&r.started_at).ok()),
415
- )
416
- });
417
- let (mut own_refusal, mut refusal) = (None, None);
418
- for receipt in receipts {
419
- let receipt = match receipt {
420
- Ok(receipt) => receipt,
421
- Err(reason) => {
422
- refusal = refusal.or(Some(reason));
423
- continue;
424
- }
425
- };
426
- if !receipt.ancestors.iter().any(|ancestor| ancestor.pid == carrier) {
427
- refusal = refusal.or(Some("mcp_handshake_ancestry"));
428
- continue;
429
- }
430
- match admit(&receipt) {
431
- Ok(()) => {
432
- return Ok(Some(Admission {
433
- server: receipt.server,
434
- carrier,
435
- carrier_birth,
436
- pane_id: pane_id.to_string(),
437
- }))
438
- }
439
- Err(reason) => own_refusal = own_refusal.or(Some(reason)),
422
+ if parent == carrier {
423
+ matched = true;
424
+ break;
440
425
  }
426
+ parent = crate::platform::argv::parent_pid(parent).unwrap_or(0);
441
427
  }
442
- Err(own_refusal.or(refusal).unwrap_or("mcp_handshake_missing"))
443
- }
444
-
445
- /// Diagnostic only, never admission: live `mcp-server` processes under a pane.
446
- pub(crate) fn live_servers_under(carrier: u32) -> Vec<u32> {
447
- crate::platform::process::process_tree(carrier)
448
- .unwrap_or_default()
449
- .into_iter()
450
- .filter(|pid| {
451
- crate::platform::argv::argv_tokens(*pid)
452
- .is_some_and(|argv| argv.iter().any(|arg| arg == "mcp-server"))
453
- })
454
- .collect()
428
+ if !matched || sample_process(&receipt.server) != ProcessState::Alive {
429
+ return Err("mcp_handshake_ancestry");
430
+ }
431
+ Ok(Admission {
432
+ server: receipt.server,
433
+ carrier,
434
+ carrier_birth,
435
+ pane_id: pane_id.to_string(),
436
+ })
455
437
  }
456
438
 
457
439
  /// Recheck against the actual resolved target immediately before the input path.
@@ -529,7 +511,6 @@ pub(crate) fn test_ready_worker(
529
511
  pane_pid: Some(carrier),
530
512
  leader_env: Default::default(),
531
513
  };
532
- // Like delivery, a provider without a worker handshake gate has nothing to revalidate.
533
514
  if let Some(admission) = preflight(&workspace, Some(owner), agent, state).unwrap() {
534
515
  revalidate(
535
516
  &admission,