@team-agent/installer 0.5.127 → 0.6.0

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 (421) hide show
  1. package/README.md +15 -4
  2. package/README.zh.md +1 -1
  3. package/package.json +4 -8
  4. package/skills/team-agent/SKILL.md +41 -26
  5. package/skills/team-agent/command-coverage.json +170 -184
  6. package/skills/team-agent/docs/reference/team-agent-operator.md +66 -32
  7. package/skills/team-agent/references/recovery-runbook.md +1 -1
  8. package/skills/team-agent/references/team-agent-operator.md +66 -32
  9. package/templates/team_state.md +1 -1
  10. package/Cargo.lock +0 -1063
  11. package/Cargo.toml +0 -36
  12. package/crates/team-agent/Cargo.toml +0 -69
  13. package/crates/team-agent/src/app_server_test_support.rs +0 -258
  14. package/crates/team-agent/src/cli/adapters.rs +0 -1298
  15. package/crates/team-agent/src/cli/arguments.rs +0 -401
  16. package/crates/team-agent/src/cli/attach_app_server_leader.rs +0 -98
  17. package/crates/team-agent/src/cli/diagnose.rs +0 -1675
  18. package/crates/team-agent/src/cli/emit.rs +0 -2898
  19. package/crates/team-agent/src/cli/grok_slot.rs +0 -384
  20. package/crates/team-agent/src/cli/helpers.rs +0 -91
  21. package/crates/team-agent/src/cli/intent/runtime.rs +0 -1189
  22. package/crates/team-agent/src/cli/intent/runtime_tests.rs +0 -821
  23. package/crates/team-agent/src/cli/intent/tests.rs +0 -691
  24. package/crates/team-agent/src/cli/intent.rs +0 -664
  25. package/crates/team-agent/src/cli/leader.rs +0 -300
  26. package/crates/team-agent/src/cli/leader_prompt.rs +0 -261
  27. package/crates/team-agent/src/cli/leaders.rs +0 -418
  28. package/crates/team-agent/src/cli/mod.rs +0 -6528
  29. package/crates/team-agent/src/cli/models.rs +0 -449
  30. package/crates/team-agent/src/cli/named_address.rs +0 -1651
  31. package/crates/team-agent/src/cli/profile.rs +0 -342
  32. package/crates/team-agent/src/cli/route.rs +0 -212
  33. package/crates/team-agent/src/cli/send/coordinator.rs +0 -164
  34. package/crates/team-agent/src/cli/send/mailbox.rs +0 -145
  35. package/crates/team-agent/src/cli/send/persist.rs +0 -192
  36. package/crates/team-agent/src/cli/send/presentation.rs +0 -370
  37. package/crates/team-agent/src/cli/send/resolve.rs +0 -573
  38. package/crates/team-agent/src/cli/send.rs +0 -201
  39. package/crates/team-agent/src/cli/shutdown_socket.rs +0 -288
  40. package/crates/team-agent/src/cli/spec.rs +0 -159
  41. package/crates/team-agent/src/cli/status.rs +0 -280
  42. package/crates/team-agent/src/cli/status_port/agents.rs +0 -358
  43. package/crates/team-agent/src/cli/status_port/approvals.rs +0 -79
  44. package/crates/team-agent/src/cli/status_port/brief.rs +0 -2043
  45. package/crates/team-agent/src/cli/status_port/compact.rs +0 -240
  46. package/crates/team-agent/src/cli/status_port/format.rs +0 -146
  47. package/crates/team-agent/src/cli/status_port/inbox.rs +0 -62
  48. package/crates/team-agent/src/cli/status_port/runtime.rs +0 -197
  49. package/crates/team-agent/src/cli/status_port/snapshot.rs +0 -208
  50. package/crates/team-agent/src/cli/status_port/store.rs +0 -416
  51. package/crates/team-agent/src/cli/status_port/tests.rs +0 -90
  52. package/crates/team-agent/src/cli/status_port.rs +0 -92
  53. package/crates/team-agent/src/cli/tests/base.rs +0 -885
  54. package/crates/team-agent/src/cli/tests/divergence.rs +0 -518
  55. package/crates/team-agent/src/cli/tests/lane_c.rs +0 -453
  56. package/crates/team-agent/src/cli/tests/leader_watch.rs +0 -372
  57. package/crates/team-agent/src/cli/tests/main_preserved.rs +0 -430
  58. package/crates/team-agent/src/cli/tests/missing_subcommands.rs +0 -336
  59. package/crates/team-agent/src/cli/tests/mod.rs +0 -137
  60. package/crates/team-agent/src/cli/tests/named_address.rs +0 -760
  61. package/crates/team-agent/src/cli/tests/parser_safety.rs +0 -383
  62. package/crates/team-agent/src/cli/tests/run_delegation.rs +0 -481
  63. package/crates/team-agent/src/cli/tests/shutdown_kill_plan.rs +0 -2179
  64. package/crates/team-agent/src/cli/tests/status_brief.rs +0 -1514
  65. package/crates/team-agent/src/cli/tests/status_send.rs +0 -1091
  66. package/crates/team-agent/src/cli/tests/verb_install_skill.rs +0 -93
  67. package/crates/team-agent/src/cli/tests/verb_profile.rs +0 -392
  68. package/crates/team-agent/src/cli/triage.rs +0 -234
  69. package/crates/team-agent/src/cli/types.rs +0 -591
  70. package/crates/team-agent/src/codex_app_server.rs +0 -546
  71. package/crates/team-agent/src/communication_mode/mod.rs +0 -56
  72. package/crates/team-agent/src/compiler/tests.rs +0 -842
  73. package/crates/team-agent/src/compiler.rs +0 -691
  74. package/crates/team-agent/src/conpty/backend.rs +0 -829
  75. package/crates/team-agent/src/conpty/mod.rs +0 -33
  76. package/crates/team-agent/src/contract_runtime/backend.rs +0 -1610
  77. package/crates/team-agent/src/contract_runtime/commands.rs +0 -671
  78. package/crates/team-agent/src/contract_runtime/config.rs +0 -354
  79. package/crates/team-agent/src/contract_runtime/config_tests.rs +0 -56
  80. package/crates/team-agent/src/contract_runtime/discovery.rs +0 -438
  81. package/crates/team-agent/src/contract_runtime/fork.rs +0 -368
  82. package/crates/team-agent/src/contract_runtime/forward.rs +0 -288
  83. package/crates/team-agent/src/contract_runtime/framework.rs +0 -350
  84. package/crates/team-agent/src/contract_runtime/mcp.rs +0 -399
  85. package/crates/team-agent/src/contract_runtime/mod.rs +0 -17
  86. package/crates/team-agent/src/contract_runtime/prompt.rs +0 -216
  87. package/crates/team-agent/src/contract_runtime/pump.rs +0 -271
  88. package/crates/team-agent/src/contract_runtime/registry.rs +0 -589
  89. package/crates/team-agent/src/contract_runtime/workspace.rs +0 -228
  90. package/crates/team-agent/src/coordinator/backoff.rs +0 -741
  91. package/crates/team-agent/src/coordinator/conpty_shim.rs +0 -805
  92. package/crates/team-agent/src/coordinator/health.rs +0 -1185
  93. package/crates/team-agent/src/coordinator/mod.rs +0 -119
  94. package/crates/team-agent/src/coordinator/orphan.rs +0 -110
  95. package/crates/team-agent/src/coordinator/runtime_detectors.rs +0 -798
  96. package/crates/team-agent/src/coordinator/runtime_observation.rs +0 -84
  97. package/crates/team-agent/src/coordinator/steps/abnormal.rs +0 -2309
  98. package/crates/team-agent/src/coordinator/steps/delivery.rs +0 -15
  99. package/crates/team-agent/src/coordinator/steps/health_sync.rs +0 -15
  100. package/crates/team-agent/src/coordinator/steps/mod.rs +0 -126
  101. package/crates/team-agent/src/coordinator/steps/persist.rs +0 -15
  102. package/crates/team-agent/src/coordinator/steps/runtime_prompts.rs +0 -15
  103. package/crates/team-agent/src/coordinator/steps/session_gate.rs +0 -16
  104. package/crates/team-agent/src/coordinator/tests/a0_lostupdate.rs +0 -158
  105. package/crates/team-agent/src/coordinator/tests/abnormal.rs +0 -312
  106. package/crates/team-agent/src/coordinator/tests/api_error_recovery.rs +0 -338
  107. package/crates/team-agent/src/coordinator/tests/basics.rs +0 -211
  108. package/crates/team-agent/src/coordinator/tests/daemon.rs +0 -514
  109. package/crates/team-agent/src/coordinator/tests/energy.rs +0 -549
  110. package/crates/team-agent/src/coordinator/tests/health_sync.rs +0 -461
  111. package/crates/team-agent/src/coordinator/tests/main_preserved.rs +0 -186
  112. package/crates/team-agent/src/coordinator/tests/mod.rs +0 -295
  113. package/crates/team-agent/src/coordinator/tests/s3_bounded.rs +0 -207
  114. package/crates/team-agent/src/coordinator/tests/spine.rs +0 -308
  115. package/crates/team-agent/src/coordinator/tests/takeover.rs +0 -286
  116. package/crates/team-agent/src/coordinator/tests/tick_core.rs +0 -237
  117. package/crates/team-agent/src/coordinator/tick.rs +0 -3112
  118. package/crates/team-agent/src/coordinator/types.rs +0 -540
  119. package/crates/team-agent/src/db/agent_health_capture.rs +0 -126
  120. package/crates/team-agent/src/db/message_store.rs +0 -1585
  121. package/crates/team-agent/src/db/migration.rs +0 -984
  122. package/crates/team-agent/src/db/mod.rs +0 -26
  123. package/crates/team-agent/src/db/schema.rs +0 -663
  124. package/crates/team-agent/src/diagnose/comms.rs +0 -242
  125. package/crates/team-agent/src/diagnose/mod.rs +0 -44
  126. package/crates/team-agent/src/diagnose/orphans.rs +0 -889
  127. package/crates/team-agent/src/event_log.rs +0 -502
  128. package/crates/team-agent/src/fake_worker.rs +0 -303
  129. package/crates/team-agent/src/kill_audit.rs +0 -106
  130. package/crates/team-agent/src/layout/manager.rs +0 -248
  131. package/crates/team-agent/src/layout/mod.rs +0 -36
  132. package/crates/team-agent/src/layout/overlay.rs +0 -134
  133. package/crates/team-agent/src/layout/placement.rs +0 -52
  134. package/crates/team-agent/src/layout/recovery.rs +0 -106
  135. package/crates/team-agent/src/layout/runtime_sessions.rs +0 -317
  136. package/crates/team-agent/src/layout/sessions.rs +0 -294
  137. package/crates/team-agent/src/layout/tmux_endpoint.rs +0 -165
  138. package/crates/team-agent/src/layout/worker_env.rs +0 -721
  139. package/crates/team-agent/src/layout/worker_window_helpers.rs +0 -37
  140. package/crates/team-agent/src/leader/helpers.rs +0 -178
  141. package/crates/team-agent/src/leader/incident.rs +0 -6
  142. package/crates/team-agent/src/leader/lease.rs +0 -3356
  143. package/crates/team-agent/src/leader/mod.rs +0 -104
  144. package/crates/team-agent/src/leader/owner_bind.rs +0 -400
  145. package/crates/team-agent/src/leader/prompt.rs +0 -905
  146. package/crates/team-agent/src/leader/provider_attribution.rs +0 -247
  147. package/crates/team-agent/src/leader/rediscover/tests.rs +0 -689
  148. package/crates/team-agent/src/leader/rediscover.rs +0 -1192
  149. package/crates/team-agent/src/leader/registry.rs +0 -883
  150. package/crates/team-agent/src/leader/start.rs +0 -4500
  151. package/crates/team-agent/src/leader/takeover.rs +0 -65
  152. package/crates/team-agent/src/leader/tests/basics.rs +0 -337
  153. package/crates/team-agent/src/leader/tests/byte_findings.rs +0 -355
  154. package/crates/team-agent/src/leader/tests/claim_leader_mailbox_flush_contract.rs +0 -544
  155. package/crates/team-agent/src/leader/tests/historical_inventory_upgrade_gate_red.rs +0 -624
  156. package/crates/team-agent/src/leader/tests/identity.rs +0 -465
  157. package/crates/team-agent/src/leader/tests/identity_session_names.rs +0 -42
  158. package/crates/team-agent/src/leader/tests/idle.rs +0 -113
  159. package/crates/team-agent/src/leader/tests/lease_api.rs +0 -520
  160. package/crates/team-agent/src/leader/tests/lease_claim.rs +0 -629
  161. package/crates/team-agent/src/leader/tests/mod.rs +0 -106
  162. package/crates/team-agent/src/leader/tests/prompt_contract.rs +0 -267
  163. package/crates/team-agent/src/leader/tests/rediscover.rs +0 -403
  164. package/crates/team-agent/src/leader/tests/rehomed_env.rs +0 -83
  165. package/crates/team-agent/src/leader/tests/team_in_team_state_scope_red.rs +0 -1273
  166. package/crates/team-agent/src/leader/tests/terminal_and_replay_vs_rerender_invariants_red.rs +0 -417
  167. package/crates/team-agent/src/leader/tests/wake_start_owner.rs +0 -168
  168. package/crates/team-agent/src/leader/types.rs +0 -534
  169. package/crates/team-agent/src/lib.rs +0 -145
  170. package/crates/team-agent/src/lifecycle/helpers.rs +0 -212
  171. package/crates/team-agent/src/lifecycle/launch/add_agent.rs +0 -848
  172. package/crates/team-agent/src/lifecycle/launch/add_agent_state.rs +0 -393
  173. package/crates/team-agent/src/lifecycle/launch/agent_state.rs +0 -332
  174. package/crates/team-agent/src/lifecycle/launch/clone_agent.rs +0 -178
  175. package/crates/team-agent/src/lifecycle/launch/fork_agent.rs +0 -82
  176. package/crates/team-agent/src/lifecycle/launch/fork_entry.rs +0 -63
  177. package/crates/team-agent/src/lifecycle/launch/identity.rs +0 -674
  178. package/crates/team-agent/src/lifecycle/launch/layout.rs +0 -350
  179. package/crates/team-agent/src/lifecycle/launch/leader_context.rs +0 -589
  180. package/crates/team-agent/src/lifecycle/launch/mcp_config.rs +0 -152
  181. package/crates/team-agent/src/lifecycle/launch/ownership.rs +0 -105
  182. package/crates/team-agent/src/lifecycle/launch/pi_mcp.rs +0 -629
  183. package/crates/team-agent/src/lifecycle/launch/pi_mcp_extension.js +0 -226
  184. package/crates/team-agent/src/lifecycle/launch/plan.rs +0 -269
  185. package/crates/team-agent/src/lifecycle/launch/quick_start.rs +0 -5176
  186. package/crates/team-agent/src/lifecycle/launch/quick_start_transport.rs +0 -368
  187. package/crates/team-agent/src/lifecycle/launch/readiness.rs +0 -519
  188. package/crates/team-agent/src/lifecycle/launch/role_source.rs +0 -287
  189. package/crates/team-agent/src/lifecycle/launch/spawn.rs +0 -450
  190. package/crates/team-agent/src/lifecycle/launch/spec_state.rs +0 -801
  191. package/crates/team-agent/src/lifecycle/launch/state_projection.rs +0 -624
  192. package/crates/team-agent/src/lifecycle/launch/worker_env.rs +0 -558
  193. package/crates/team-agent/src/lifecycle/launch.rs +0 -534
  194. package/crates/team-agent/src/lifecycle/lock.rs +0 -350
  195. package/crates/team-agent/src/lifecycle/mod.rs +0 -79
  196. package/crates/team-agent/src/lifecycle/pane_input_lock.rs +0 -239
  197. package/crates/team-agent/src/lifecycle/profile_launch.rs +0 -757
  198. package/crates/team-agent/src/lifecycle/profile_smoke.rs +0 -557
  199. package/crates/team-agent/src/lifecycle/restart/agent.rs +0 -2198
  200. package/crates/team-agent/src/lifecycle/restart/common.rs +0 -2052
  201. package/crates/team-agent/src/lifecycle/restart/orchestrator.rs +0 -65
  202. package/crates/team-agent/src/lifecycle/restart/preflight.rs +0 -113
  203. package/crates/team-agent/src/lifecycle/restart/rebuild.rs +0 -5135
  204. package/crates/team-agent/src/lifecycle/restart/remove.rs +0 -1504
  205. package/crates/team-agent/src/lifecycle/restart/selection.rs +0 -559
  206. package/crates/team-agent/src/lifecycle/restart/team_state.rs +0 -329
  207. package/crates/team-agent/src/lifecycle/restart.rs +0 -223
  208. package/crates/team-agent/src/lifecycle/role_config.rs +0 -1205
  209. package/crates/team-agent/src/lifecycle/tests/acceptance_b_batch_red.rs +0 -1021
  210. package/crates/team-agent/src/lifecycle/tests/agent_ops.rs +0 -1205
  211. package/crates/team-agent/src/lifecycle/tests/b0_legacy_snapshot_nonauthority_contract.rs +0 -265
  212. package/crates/team-agent/src/lifecycle/tests/b0_reader_hideone_audit_contract.rs +0 -490
  213. package/crates/team-agent/src/lifecycle/tests/behavioral_diff_264_red.rs +0 -656
  214. package/crates/team-agent/src/lifecycle/tests/claude_profile_launch_red.rs +0 -135
  215. package/crates/team-agent/src/lifecycle/tests/cli_argv_routing_red.rs +0 -1415
  216. package/crates/team-agent/src/lifecycle/tests/clone_agent_preserves_source_tools.rs +0 -427
  217. package/crates/team-agent/src/lifecycle/tests/clone_fork_copilot_perms_red.rs +0 -396
  218. package/crates/team-agent/src/lifecycle/tests/communication_mode_runtime_contract_red.rs +0 -418
  219. package/crates/team-agent/src/lifecycle/tests/copilot_provider_red.rs +0 -2039
  220. package/crates/team-agent/src/lifecycle/tests/core.rs +0 -1298
  221. package/crates/team-agent/src/lifecycle/tests/f032_startup_prompt_best_effort_red.rs +0 -315
  222. package/crates/team-agent/src/lifecycle/tests/fixtures/pi_list_models_g0.stdout.txt +0 -222
  223. package/crates/team-agent/src/lifecycle/tests/fixtures/pi_list_models_spaced_ids.stdout.txt +0 -9
  224. package/crates/team-agent/src/lifecycle/tests/fixtures/pi_native_mcp_host.mjs +0 -71
  225. package/crates/team-agent/src/lifecycle/tests/gate_fixtures.rs +0 -562
  226. package/crates/team-agent/src/lifecycle/tests/grok_require_explicit_model_red.rs +0 -110
  227. package/crates/team-agent/src/lifecycle/tests/harvest2_a_batch_red.rs +0 -472
  228. package/crates/team-agent/src/lifecycle/tests/host_cotenant_death_p0_contract.rs +0 -921
  229. package/crates/team-agent/src/lifecycle/tests/issue238_codex_effort_contract.rs +0 -68
  230. package/crates/team-agent/src/lifecycle/tests/lane_ops.rs +0 -1851
  231. package/crates/team-agent/src/lifecycle/tests/launch_spawn.rs +0 -3981
  232. package/crates/team-agent/src/lifecycle/tests/lifecycle_lock.rs +0 -339
  233. package/crates/team-agent/src/lifecycle/tests/lifecycle_rollback_red.rs +0 -596
  234. package/crates/team-agent/src/lifecycle/tests/main_preserved.rs +0 -513
  235. package/crates/team-agent/src/lifecycle/tests/mcp_tool_name_format_red.rs +0 -69
  236. package/crates/team-agent/src/lifecycle/tests/phase_b_contracts.rs +0 -824
  237. package/crates/team-agent/src/lifecycle/tests/phase_golden.rs +0 -723
  238. package/crates/team-agent/src/lifecycle/tests/pi_catalog_red.rs +0 -152
  239. package/crates/team-agent/src/lifecycle/tests/pi_command_red.rs +0 -182
  240. package/crates/team-agent/src/lifecycle/tests/pi_dual_entry_red.rs +0 -374
  241. package/crates/team-agent/src/lifecycle/tests/pi_executable_mcp_red.rs +0 -546
  242. package/crates/team-agent/src/lifecycle/tests/pi_lifecycle_doctor_red.rs +0 -31
  243. package/crates/team-agent/src/lifecycle/tests/pi_native_mcp_capability_red.rs +0 -528
  244. package/crates/team-agent/src/lifecycle/tests/pi_session_delivery_red.rs +0 -134
  245. package/crates/team-agent/src/lifecycle/tests/quick_start_worker_readiness_red.rs +0 -333
  246. package/crates/team-agent/src/lifecycle/tests/realmachine_clusters_1_6_red.rs +0 -786
  247. package/crates/team-agent/src/lifecycle/tests/realmachine_residual_g1_g4_red.rs +0 -648
  248. package/crates/team-agent/src/lifecycle/tests/restart.rs +0 -185
  249. package/crates/team-agent/src/lifecycle/tests/restart_build_before_destroy_0540_contract.rs +0 -1053
  250. package/crates/team-agent/src/lifecycle/tests/restart_liveness_red.rs +0 -679
  251. package/crates/team-agent/src/lifecycle/tests/restart_rebind_hotfix_252_red.rs +0 -1126
  252. package/crates/team-agent/src/lifecycle/tests/restart_session_capture_red.rs +0 -153
  253. package/crates/team-agent/src/lifecycle/tests/resume_recover_red.rs +0 -427
  254. package/crates/team-agent/src/lifecycle/tests/stale_team_saveconflict_contract.rs +0 -431
  255. package/crates/team-agent/src/lifecycle/tests/startup_latency_contract.rs +0 -1066
  256. package/crates/team-agent/src/lifecycle/tests/status_credential_redaction_contract.rs +0 -876
  257. package/crates/team-agent/src/lifecycle/tests/subprep_codex_trust_roundtrip_red.rs +0 -1026
  258. package/crates/team-agent/src/lifecycle/tests/swallow_batch4_semantics_red.rs +0 -90
  259. package/crates/team-agent/src/lifecycle/tests/team_in_team_identity_scope_red.rs +0 -543
  260. package/crates/team-agent/src/lifecycle/tests/team_in_team_sibling_quick_start_red.rs +0 -1103
  261. package/crates/team-agent/src/lifecycle/tests/team_in_team_state_scope_red.rs +0 -1276
  262. package/crates/team-agent/src/lifecycle/tests/team_key_retirement_roster_red.rs +0 -145
  263. package/crates/team-agent/src/lifecycle/tests/team_key_retirement_txn_red.rs +0 -603
  264. package/crates/team-agent/src/lifecycle/tests/test_isolation_escape_contract.rs +0 -936
  265. package/crates/team-agent/src/lifecycle/tests/upgrade_compat_0211_red.rs +0 -970
  266. package/crates/team-agent/src/lifecycle/tests/verify_rs031_window_consistency_red.rs +0 -996
  267. package/crates/team-agent/src/lifecycle/tests/worker_spawn_env_red.rs +0 -827
  268. package/crates/team-agent/src/lifecycle/tests.rs +0 -102
  269. package/crates/team-agent/src/lifecycle/types.rs +0 -861
  270. package/crates/team-agent/src/lifecycle/worker_command_context.rs +0 -370
  271. package/crates/team-agent/src/main.rs +0 -52
  272. package/crates/team-agent/src/mcp_server/helpers.rs +0 -510
  273. package/crates/team-agent/src/mcp_server/lifecycle_tools/mod.rs +0 -6
  274. package/crates/team-agent/src/mcp_server/lifecycle_tools/state_status.rs +0 -43
  275. package/crates/team-agent/src/mcp_server/mod.rs +0 -119
  276. package/crates/team-agent/src/mcp_server/normalize.rs +0 -485
  277. package/crates/team-agent/src/mcp_server/readiness.rs +0 -546
  278. package/crates/team-agent/src/mcp_server/readiness_tests.rs +0 -369
  279. package/crates/team-agent/src/mcp_server/tests/golden.rs +0 -285
  280. package/crates/team-agent/src/mcp_server/tests/normalize.rs +0 -247
  281. package/crates/team-agent/src/mcp_server/tests/scoped.rs +0 -168
  282. package/crates/team-agent/src/mcp_server/tests/send.rs +0 -388
  283. package/crates/team-agent/src/mcp_server/tests/tools.rs +0 -343
  284. package/crates/team-agent/src/mcp_server/tests/wire.rs +0 -254
  285. package/crates/team-agent/src/mcp_server/tests.rs +0 -43
  286. package/crates/team-agent/src/mcp_server/tools.rs +0 -1015
  287. package/crates/team-agent/src/mcp_server/types.rs +0 -396
  288. package/crates/team-agent/src/mcp_server/wire.rs +0 -867
  289. package/crates/team-agent/src/messaging/activity.rs +0 -461
  290. package/crates/team-agent/src/messaging/address.rs +0 -87
  291. package/crates/team-agent/src/messaging/delivery.rs +0 -3785
  292. package/crates/team-agent/src/messaging/delivery_consumption_tests.rs +0 -234
  293. package/crates/team-agent/src/messaging/enter_rescue.rs +0 -362
  294. package/crates/team-agent/src/messaging/enter_rescue_tests.rs +0 -633
  295. package/crates/team-agent/src/messaging/helpers.rs +0 -354
  296. package/crates/team-agent/src/messaging/leader_channel.rs +0 -287
  297. package/crates/team-agent/src/messaging/leader_receiver.rs +0 -786
  298. package/crates/team-agent/src/messaging/mod.rs +0 -166
  299. package/crates/team-agent/src/messaging/persist.rs +0 -377
  300. package/crates/team-agent/src/messaging/presentation.rs +0 -417
  301. package/crates/team-agent/src/messaging/results/collection_tests.rs +0 -719
  302. package/crates/team-agent/src/messaging/results.rs +0 -1415
  303. package/crates/team-agent/src/messaging/scheduler.rs +0 -217
  304. package/crates/team-agent/src/messaging/selftest.rs +0 -315
  305. package/crates/team-agent/src/messaging/send.rs +0 -981
  306. package/crates/team-agent/src/messaging/tests/basic.rs +0 -346
  307. package/crates/team-agent/src/messaging/tests/dup_inject.rs +0 -551
  308. package/crates/team-agent/src/messaging/tests/e23.rs +0 -49
  309. package/crates/team-agent/src/messaging/tests/leader_channel.rs +0 -169
  310. package/crates/team-agent/src/messaging/tests/leader_inject_acceptance.rs +0 -817
  311. package/crates/team-agent/src/messaging/tests/main_preserved.rs +0 -85
  312. package/crates/team-agent/src/messaging/tests/mod.rs +0 -306
  313. package/crates/team-agent/src/messaging/tests/runtime.rs +0 -3655
  314. package/crates/team-agent/src/messaging/tests/spine.rs +0 -837
  315. package/crates/team-agent/src/messaging/tests/wave2.rs +0 -381
  316. package/crates/team-agent/src/messaging/trust.rs +0 -215
  317. package/crates/team-agent/src/messaging/types.rs +0 -471
  318. package/crates/team-agent/src/messaging/wait.rs +0 -366
  319. package/crates/team-agent/src/messaging/watchers.rs +0 -916
  320. package/crates/team-agent/src/model/enums.rs +0 -356
  321. package/crates/team-agent/src/model/errors.rs +0 -18
  322. package/crates/team-agent/src/model/ids.rs +0 -165
  323. package/crates/team-agent/src/model/mod.rs +0 -29
  324. package/crates/team-agent/src/model/name_similarity.rs +0 -252
  325. package/crates/team-agent/src/model/pane_authority_refusal.rs +0 -359
  326. package/crates/team-agent/src/model/paths.rs +0 -257
  327. package/crates/team-agent/src/model/routing.rs +0 -387
  328. package/crates/team-agent/src/model/spec.rs +0 -1184
  329. package/crates/team-agent/src/model/task_graph.rs +0 -321
  330. package/crates/team-agent/src/model/testdata/fuzz.golden.yaml +0 -43
  331. package/crates/team-agent/src/model/testdata/fuzz.yaml +0 -43
  332. package/crates/team-agent/src/model/testdata/spec_invalid_a.yaml +0 -209
  333. package/crates/team-agent/src/model/testdata/team.spec.golden.yaml +0 -208
  334. package/crates/team-agent/src/model/testdata/team.spec.yaml +0 -208
  335. package/crates/team-agent/src/model/yaml/tests.rs +0 -188
  336. package/crates/team-agent/src/model/yaml.rs +0 -802
  337. package/crates/team-agent/src/os_probe.rs +0 -280
  338. package/crates/team-agent/src/packaging/install.rs +0 -219
  339. package/crates/team-agent/src/packaging/migrate.rs +0 -38
  340. package/crates/team-agent/src/packaging/mod.rs +0 -91
  341. package/crates/team-agent/src/packaging/repair.rs +0 -33
  342. package/crates/team-agent/src/packaging/tests.rs +0 -822
  343. package/crates/team-agent/src/packaging/types.rs +0 -417
  344. package/crates/team-agent/src/platform/argv.rs +0 -331
  345. package/crates/team-agent/src/platform/errors.rs +0 -104
  346. package/crates/team-agent/src/platform/file_lock.rs +0 -279
  347. package/crates/team-agent/src/platform/mod.rs +0 -5
  348. package/crates/team-agent/src/platform/process.rs +0 -545
  349. package/crates/team-agent/src/provider/adapter.rs +0 -898
  350. package/crates/team-agent/src/provider/adapters/copilot.rs +0 -75
  351. package/crates/team-agent/src/provider/adapters/copilot_fork.rs +0 -307
  352. package/crates/team-agent/src/provider/adapters/fake.rs +0 -18
  353. package/crates/team-agent/src/provider/adapters/mod.rs +0 -28
  354. package/crates/team-agent/src/provider/approvals/mod.rs +0 -3
  355. package/crates/team-agent/src/provider/approvals/parsing.rs +0 -586
  356. package/crates/team-agent/src/provider/approvals/runtime_prompts.rs +0 -205
  357. package/crates/team-agent/src/provider/argv_route.rs +0 -405
  358. package/crates/team-agent/src/provider/bypass_flags.rs +0 -116
  359. package/crates/team-agent/src/provider/classify.rs +0 -773
  360. package/crates/team-agent/src/provider/command_helpers.rs +0 -45
  361. package/crates/team-agent/src/provider/faults.rs +0 -165
  362. package/crates/team-agent/src/provider/helpers.rs +0 -52
  363. package/crates/team-agent/src/provider/mod.rs +0 -78
  364. package/crates/team-agent/src/provider/model_catalog.rs +0 -722
  365. package/crates/team-agent/src/provider/session/capture.rs +0 -2631
  366. package/crates/team-agent/src/provider/session/context_fork/outcome.rs +0 -168
  367. package/crates/team-agent/src/provider/session/context_fork.rs +0 -264
  368. package/crates/team-agent/src/provider/session/mod.rs +0 -36
  369. package/crates/team-agent/src/provider/session/resume.rs +0 -269
  370. package/crates/team-agent/src/provider/session_scan/common/tests.rs +0 -316
  371. package/crates/team-agent/src/provider/session_scan/common.rs +0 -618
  372. package/crates/team-agent/src/provider/session_scan/copilot.rs +0 -280
  373. package/crates/team-agent/src/provider/session_scan/pi.rs +0 -152
  374. package/crates/team-agent/src/provider/session_scan.rs +0 -61
  375. package/crates/team-agent/src/provider/startup_prompt.rs +0 -757
  376. package/crates/team-agent/src/provider/submit_now.rs +0 -71
  377. package/crates/team-agent/src/provider/testdata/copilot-ready-marker.txt +0 -5
  378. package/crates/team-agent/src/provider/testdata/copilot-trust-prompt.txt +0 -19
  379. package/crates/team-agent/src/provider/testdata/grok-models-authenticated.stdout +0 -9
  380. package/crates/team-agent/src/provider/testdata/grok-models-unauthenticated.stdout +0 -7
  381. package/crates/team-agent/src/provider/tests/adapter.rs +0 -354
  382. package/crates/team-agent/src/provider/tests/classify.rs +0 -201
  383. package/crates/team-agent/src/provider/tests/command_helpers.rs +0 -26
  384. package/crates/team-agent/src/provider/tests/copilot_fork.rs +0 -191
  385. package/crates/team-agent/src/provider/tests/faults.rs +0 -200
  386. package/crates/team-agent/src/provider/tests/idle.rs +0 -140
  387. package/crates/team-agent/src/provider/tests/wire.rs +0 -213
  388. package/crates/team-agent/src/provider/tests.rs +0 -32
  389. package/crates/team-agent/src/provider/types.rs +0 -529
  390. package/crates/team-agent/src/provider/wire.rs +0 -406
  391. package/crates/team-agent/src/redaction.rs +0 -259
  392. package/crates/team-agent/src/state/abnormal_watch/tests/mod.rs +0 -248
  393. package/crates/team-agent/src/state/abnormal_watch.rs +0 -144
  394. package/crates/team-agent/src/state/identity.rs +0 -914
  395. package/crates/team-agent/src/state/identity_keys.rs +0 -132
  396. package/crates/team-agent/src/state/mod.rs +0 -74
  397. package/crates/team-agent/src/state/owner_gate.rs +0 -482
  398. package/crates/team-agent/src/state/ownership.rs +0 -563
  399. package/crates/team-agent/src/state/paths.rs +0 -395
  400. package/crates/team-agent/src/state/persist.rs +0 -3811
  401. package/crates/team-agent/src/state/projection.rs +0 -1508
  402. package/crates/team-agent/src/state/repository/intent.rs +0 -14
  403. package/crates/team-agent/src/state/repository/tests.rs +0 -184
  404. package/crates/team-agent/src/state/repository.rs +0 -866
  405. package/crates/team-agent/src/state/selector.rs +0 -160
  406. package/crates/team-agent/src/state/testdata/state-rich.canonical.json +0 -133
  407. package/crates/team-agent/src/tmux_backend/enter_rescue.rs +0 -325
  408. package/crates/team-agent/src/tmux_backend/enter_rescue_tests.rs +0 -432
  409. package/crates/team-agent/src/tmux_backend/native_input.rs +0 -513
  410. package/crates/team-agent/src/tmux_backend/native_input_tests.rs +0 -742
  411. package/crates/team-agent/src/tmux_backend/tests.rs +0 -4254
  412. package/crates/team-agent/src/tmux_backend.rs +0 -4527
  413. package/crates/team-agent/src/topology.rs +0 -537
  414. package/crates/team-agent/src/transport/enter_rescue.rs +0 -166
  415. package/crates/team-agent/src/transport/native_input.rs +0 -491
  416. package/crates/team-agent/src/transport/test_support.rs +0 -743
  417. package/crates/team-agent/src/transport/tests/behavior.rs +0 -326
  418. package/crates/team-agent/src/transport/tests/mod.rs +0 -194
  419. package/crates/team-agent/src/transport/tests/wire.rs +0 -574
  420. package/crates/team-agent/src/transport.rs +0 -1655
  421. package/crates/team-agent/src/transport_factory.rs +0 -819
@@ -1,1205 +0,0 @@
1
- //! CLI convenience is a role-document transaction, not a provider override.
2
- //! Existing file compilation and process/session lifecycle remain authoritative.
3
-
4
- use std::fs::{self, OpenOptions};
5
- use std::io::Write;
6
- use std::path::{Path, PathBuf};
7
- use std::sync::atomic::{AtomicU64, Ordering};
8
-
9
- use crate::model::ids::AgentId;
10
- use crate::model::yaml::{self, Value};
11
- use crate::state::repository::{StateRepository, StateWriteIntent};
12
- use crate::state::selector::{SelectedTeam, SelectorMode};
13
- use crate::transport::Transport;
14
-
15
- use super::lock::{acquire_agent_lifecycle_lock, LifecycleLockRequest};
16
- use super::{AddAgentReport, LifecycleError, StartAgentOutcome};
17
-
18
- #[derive(Debug, Clone, Default, PartialEq, Eq)]
19
- pub struct RoleConfigPatch {
20
- pub provider: Option<String>,
21
- pub model: Option<String>,
22
- pub effort: Option<String>,
23
- pub bypass: Option<bool>,
24
- pub prompt: Option<String>,
25
- pub profile: Option<String>,
26
- }
27
-
28
- pub fn add_agent_from_role(
29
- workspace: &Path,
30
- agent_id: &AgentId,
31
- source: Option<&Path>,
32
- patch: &RoleConfigPatch,
33
- force: bool,
34
- team: Option<&str>,
35
- ) -> Result<AddAgentReport, LifecycleError> {
36
- if let Some(path) = source {
37
- if !path.is_file() {
38
- return Err(LifecycleError::Compile(format!(
39
- "role file not found: {}",
40
- path.display()
41
- )));
42
- }
43
- }
44
- let selected = select(workspace, team)?;
45
- with_selected(&selected, agent_id, |selected| {
46
- let transport = super::restart::lifecycle_worker_tmux_backend_selection_for_state(
47
- &selected.run_workspace,
48
- &selected.state,
49
- )?
50
- .backend;
51
- add_at_selected(selected, agent_id, source, patch, force, &transport)
52
- })
53
- }
54
-
55
- #[cfg(test)]
56
- pub(crate) fn add_agent_from_role_with_transport(
57
- workspace: &Path,
58
- agent_id: &AgentId,
59
- source: Option<&Path>,
60
- patch: &RoleConfigPatch,
61
- force: bool,
62
- transport: &dyn Transport,
63
- ) -> Result<AddAgentReport, LifecycleError> {
64
- with_selected(&select(workspace, None)?, agent_id, |selected| {
65
- add_at_selected(selected, agent_id, source, patch, force, transport)
66
- })
67
- }
68
-
69
- pub fn start_agent_from_role(
70
- workspace: &Path,
71
- agent_id: &AgentId,
72
- patch: &RoleConfigPatch,
73
- allow_fresh: bool,
74
- team: Option<&str>,
75
- ) -> Result<StartAgentOutcome, LifecycleError> {
76
- let selected = select(workspace, team)?;
77
- with_selected(&selected, agent_id, |selected| {
78
- let transport = super::restart::lifecycle_worker_tmux_backend_selection_for_state(
79
- &selected.run_workspace,
80
- &selected.state,
81
- )?
82
- .backend;
83
- start_at_selected(selected, agent_id, patch, allow_fresh, &transport)
84
- })
85
- }
86
-
87
- fn select(workspace: &Path, team: Option<&str>) -> Result<SelectedTeam, LifecycleError> {
88
- crate::state::selector::resolve_active_team_readonly(workspace, team, SelectorMode::RuntimeOnly)
89
- .map_err(|error| LifecycleError::TeamSelect(error.to_string()))
90
- }
91
-
92
- fn with_selected<T>(
93
- selected: &SelectedTeam,
94
- agent_id: &AgentId,
95
- operation: impl FnOnce(&SelectedTeam) -> Result<T, LifecycleError>,
96
- ) -> Result<T, LifecycleError> {
97
- validate_id(agent_id)?;
98
- let _lock = acquire_agent_lifecycle_lock(LifecycleLockRequest {
99
- workspace: &selected.run_workspace,
100
- operation: "role-config",
101
- team: Some(&selected.team_key),
102
- agent_id: Some(agent_id),
103
- })?;
104
- let latest = select(&selected.run_workspace, Some(&selected.team_key))?;
105
- super::launch::ensure_owner_allowed_for_state(&latest.state, Some(agent_id))?;
106
- operation(&latest)
107
- }
108
-
109
- fn add_at_selected(
110
- selected: &SelectedTeam,
111
- agent_id: &AgentId,
112
- source: Option<&Path>,
113
- patch: &RoleConfigPatch,
114
- force: bool,
115
- transport: &dyn Transport,
116
- ) -> Result<AddAgentReport, LifecycleError> {
117
- let recreate = admit_add(&selected.state, agent_id, force, transport)?;
118
- let spec = read_spec(selected)?;
119
- if !recreate && spec_has_agent(&spec, agent_id) {
120
- return Err(LifecycleError::RequirementUnmet(format!(
121
- "agent id already exists: {agent_id}"
122
- )));
123
- }
124
- let role_path = role_path(selected, agent_id);
125
- // --force re-adopts the role file a removed seat left behind (#391): CLI
126
- // values override its frontmatter and its body is kept.
127
- let adopted = !recreate
128
- && force
129
- && source.is_none()
130
- && fs::symlink_metadata(&role_path).is_ok_and(|meta| meta.is_file());
131
- if !recreate && !adopted {
132
- check_destination(&role_path, source)?;
133
- }
134
- let text = match source {
135
- Some(path) => read_text(path)?,
136
- None if adopted => patched_role(&read_text(&role_path)?, agent_id, patch, false)?,
137
- None => String::new(),
138
- };
139
- let generated = source.is_none() && !adopted;
140
- #[allow(unused_mut)]
141
- let mut reconciled = reconcile_creation(&text, patch)?;
142
- // A generated legacy role falls back to its id as instructions at compile
143
- // time; a contract role requires a body, so give it a neutral default.
144
- #[cfg(unix)]
145
- if generated
146
- && reconciled.prompt.is_none()
147
- && crate::contract_runtime::framework::declares_contract(
148
- None,
149
- reconciled.provider.as_deref(),
150
- )
151
- {
152
- reconciled.prompt = Some(format!(
153
- "You are {agent_id}. Complete the tasks assigned by the leader.\n"
154
- ));
155
- }
156
- let next = patched_role(&text, agent_id, &reconciled, generated)?;
157
- let snapshot = RoleSnapshot::capture(selected, &role_path)?;
158
- let result = (|| {
159
- if snapshot.role.bytes.as_deref() != Some(next.as_bytes()) {
160
- atomic_write(&role_path, next.as_bytes())?;
161
- }
162
- // Reuse existing lifecycle transactions; force never admits a live seat.
163
- let add = if recreate {
164
- super::launch::force_recreate_with_transport_locked
165
- } else {
166
- super::launch::add_agent_with_transport_at_paths_locked
167
- };
168
- add(
169
- &selected.run_workspace,
170
- &selected.team_dir,
171
- agent_id,
172
- &role_path,
173
- true,
174
- Some(&selected.team_key),
175
- transport,
176
- )
177
- })();
178
- match result {
179
- Ok(report) => Ok(report),
180
- Err(error) => Err(snapshot.rollback(selected, agent_id, error)),
181
- }
182
- }
183
-
184
- // Disaster recovery is opt-in and requires positive death of a recorded pane.
185
- // An absent/unknown pane binding is not permission to replace an existing seat.
186
- fn admit_add(
187
- state: &serde_json::Value,
188
- agent_id: &AgentId,
189
- force: bool,
190
- transport: &dyn Transport,
191
- ) -> Result<bool, LifecycleError> {
192
- let Some(agent) = state
193
- .get("agents")
194
- .and_then(|agents| agents.get(agent_id.as_str()))
195
- else {
196
- return Ok(false);
197
- };
198
- let duplicate =
199
- || LifecycleError::RequirementUnmet(format!("agent id already exists: {agent_id}"));
200
- if !force {
201
- return Err(duplicate());
202
- }
203
- super::restart::ensure_agent_not_running(state, agent_id, transport)?;
204
- let Some(pane) = agent
205
- .get("pane_id")
206
- .and_then(serde_json::Value::as_str)
207
- .filter(|value| !value.is_empty())
208
- else {
209
- return Err(duplicate());
210
- };
211
- let pane = crate::transport::PaneId::new(pane);
212
- let dead = match transport.has_pane(&pane) {
213
- Ok(Some(present)) => !present,
214
- Ok(None) | Err(_) => matches!(
215
- transport.liveness(&pane),
216
- Ok(crate::model::enums::PaneLiveness::Dead)
217
- ),
218
- };
219
- if dead {
220
- Ok(true)
221
- } else {
222
- Err(duplicate())
223
- }
224
- }
225
-
226
- fn start_at_selected(
227
- selected: &SelectedTeam,
228
- agent_id: &AgentId,
229
- patch: &RoleConfigPatch,
230
- allow_fresh: bool,
231
- transport: &dyn Transport,
232
- ) -> Result<StartAgentOutcome, LifecycleError> {
233
- let old_agent = selected
234
- .state
235
- .get("agents")
236
- .and_then(|agents| agents.get(agent_id.as_str()))
237
- .ok_or_else(|| LifecycleError::RequirementUnmet(format!("agent {agent_id} not found")))?;
238
- super::restart::ensure_agent_not_running(&selected.state, agent_id, transport)?;
239
- if old_agent.get("paused").and_then(serde_json::Value::as_bool) == Some(true) {
240
- return Err(LifecycleError::RequirementUnmet(format!(
241
- "agent {agent_id} is paused"
242
- )));
243
- }
244
- let source = super::launch::role_source::resolve_role_source(
245
- &selected.run_workspace,
246
- &selected.team_dir,
247
- &selected.state,
248
- agent_id,
249
- )?;
250
- let role_path = role_path(selected, agent_id);
251
- check_destination(&role_path, Some(&source))?;
252
- let text = read_text(&source)?;
253
- let next = patched_role(&text, agent_id, patch, false)?;
254
- // Compare to the established engine, never to a freshly edited spec or model name.
255
- let old_provider = old_agent
256
- .get("provider")
257
- .and_then(serde_json::Value::as_str)
258
- .and_then(crate::provider::wire::parse_provider)
259
- .ok_or_else(|| {
260
- LifecycleError::RequirementUnmet("established provider is unknown".into())
261
- })?;
262
- let meta = crate::compiler::read_front_matter(&source)
263
- .map_err(|error| LifecycleError::Compile(error.to_string()))?
264
- .0;
265
- // Preserve the traditional compiler's interpretation of omitted metadata.
266
- let provider = patch
267
- .provider
268
- .as_deref()
269
- .or_else(|| meta.get("provider").and_then(Value::as_str))
270
- .filter(|value| !value.trim().is_empty())
271
- .unwrap_or("pi");
272
- let provider = crate::provider::wire::parse_provider(provider)
273
- .ok_or_else(|| LifecycleError::Compile("unknown role provider".into()))?;
274
- if provider != old_provider {
275
- return Err(LifecycleError::RequirementUnmet(format!(
276
- "provider change is not allowed for agent {agent_id}: {} -> {}",
277
- crate::provider::wire::provider_wire(old_provider),
278
- crate::provider::wire::provider_wire(provider),
279
- )));
280
- }
281
- let snapshot = RoleSnapshot::capture(selected, &role_path)?;
282
- let result = (|| {
283
- if snapshot.role.bytes.as_deref() != Some(next.as_bytes()) {
284
- atomic_write(&role_path, next.as_bytes())?;
285
- }
286
- let team_meta = crate::compiler::read_front_matter(&selected.team_dir.join("TEAM.md"))
287
- .map_err(|error| LifecycleError::Compile(error.to_string()))?
288
- .0;
289
- let compiled = crate::compiler::compile_role_agent(
290
- &role_path,
291
- &team_meta,
292
- &selected.run_workspace.to_string_lossy(),
293
- )
294
- .map_err(|error| LifecycleError::Compile(error.to_string()))?;
295
- if compiled.id != agent_id.as_str() {
296
- return Err(LifecycleError::Compile(
297
- "role identity does not match agent id".into(),
298
- ));
299
- }
300
- if compiled
301
- .agent
302
- .get("provider")
303
- .and_then(Value::as_str)
304
- .and_then(crate::provider::wire::parse_provider)
305
- != Some(old_provider)
306
- {
307
- return Err(LifecycleError::RequirementUnmet(
308
- "compiled provider does not match established provider".into(),
309
- ));
310
- }
311
- let mut spec = read_spec(selected)?;
312
- replace_spec_agent(&mut spec, agent_id, &compiled.agent)?;
313
- crate::model::spec::validate_spec(&spec, &selected.run_workspace)
314
- .map_err(|error| LifecycleError::Compile(error.to_string()))?;
315
- atomic_write(&snapshot.spec.path, yaml::dumps(&spec).as_bytes())?;
316
- update_agent_config(selected, agent_id, &compiled.agent, &role_path)?;
317
- let outcome = super::restart::start_agent_at_paths(
318
- &selected.run_workspace,
319
- snapshot.spec.path.parent().unwrap_or(&selected.team_dir),
320
- agent_id,
321
- false,
322
- true,
323
- allow_fresh,
324
- Some(&selected.team_key),
325
- transport,
326
- )?;
327
- match outcome {
328
- StartAgentOutcome::Running { .. } => Ok(outcome),
329
- StartAgentOutcome::Noop { .. } => Err(LifecycleError::RequirementUnmet(format!(
330
- "agent {agent_id} is already running; use stop-agent first"
331
- ))),
332
- StartAgentOutcome::Paused { .. } => Err(LifecycleError::RequirementUnmet(format!(
333
- "agent {agent_id} is paused"
334
- ))),
335
- }
336
- })();
337
- match result {
338
- Ok(outcome) => Ok(outcome),
339
- Err(error) => Err(snapshot.rollback(selected, agent_id, error)),
340
- }
341
- }
342
-
343
- // A file is an explicit configuration source, not a template to silently override.
344
- // Equal supplied values are omitted from the patch to preserve existing file bytes.
345
- fn reconcile_creation(
346
- text: &str,
347
- patch: &RoleConfigPatch,
348
- ) -> Result<RoleConfigPatch, LifecycleError> {
349
- let (meta, body) = role_parts(text)?;
350
- for field in ["provider", "model", "effort", "profile"] {
351
- if meta
352
- .get(field)
353
- .is_some_and(|value| !matches!(value, Value::Null | Value::Str(_)))
354
- {
355
- return Err(LifecycleError::Compile(format!(
356
- "role {field} must be a string"
357
- )));
358
- }
359
- }
360
- let mut next = patch.clone();
361
- let conflict = |field: &str| {
362
- LifecycleError::RequirementUnmet(format!(
363
- "conflicting {field}: --role-file and CLI values differ"
364
- ))
365
- };
366
- let file_provider = meta
367
- .get("provider")
368
- .and_then(Value::as_str)
369
- .filter(|value| !value.trim().is_empty());
370
- if let Some(file_provider) = file_provider {
371
- let file_provider = declared_provider(file_provider)
372
- .ok_or_else(|| LifecycleError::Compile("unknown role provider".into()))?;
373
- if let Some(provider) = &next.provider {
374
- if declared_provider(provider) != Some(file_provider) {
375
- return Err(conflict("provider"));
376
- }
377
- next.provider = None;
378
- }
379
- } else if next.provider.is_none() {
380
- return Err(LifecycleError::RequirementUnmet(
381
- "add-agent requires provider in --role-file or --provider <name>".into(),
382
- ));
383
- }
384
- match meta.get("dangerously_skip_permissions") {
385
- Some(Value::Bool(value)) => {
386
- if let Some(bypass) = next.bypass {
387
- if bypass != *value {
388
- return Err(conflict("bypass"));
389
- }
390
- next.bypass = None;
391
- }
392
- }
393
- Some(_) => {
394
- return Err(LifecycleError::Compile(
395
- "role dangerously_skip_permissions must be a boolean".into(),
396
- ))
397
- }
398
- None if next.bypass.is_none() => {
399
- return Err(LifecycleError::RequirementUnmet(
400
- "add-agent requires bypass in --role-file or --bypass <true|false>".into(),
401
- ))
402
- }
403
- None => {}
404
- }
405
- for (field, supplied) in [
406
- ("model", &mut next.model),
407
- ("effort", &mut next.effort),
408
- ("profile", &mut next.profile),
409
- ] {
410
- if let Some(file_value) = meta
411
- .get(field)
412
- .and_then(Value::as_str)
413
- .filter(|value| !value.trim().is_empty())
414
- {
415
- if let Some(value) = supplied.as_ref() {
416
- let equal = if field == "effort" {
417
- crate::model::enums::ProviderEffort::parse(file_value)
418
- == crate::model::enums::ProviderEffort::parse(value)
419
- } else {
420
- value == file_value
421
- };
422
- if !equal {
423
- return Err(conflict(field));
424
- }
425
- *supplied = None;
426
- }
427
- }
428
- }
429
- if let Some(prompt) = &next.prompt {
430
- if !body.trim().is_empty() {
431
- if prompt.trim() != body.trim() {
432
- return Err(conflict("prompt"));
433
- }
434
- next.prompt = None;
435
- }
436
- }
437
- Ok(next)
438
- }
439
-
440
- /// One identity per declared provider: its contract leaf id, else its legacy
441
- /// wire form, so `agent` in a role file and `--provider cursor` agree.
442
- fn declared_provider(value: &str) -> Option<&'static str> {
443
- #[cfg(unix)]
444
- if let Some(descriptor) = crate::contract_runtime::registry::declared(value) {
445
- return Some(descriptor.identity.id);
446
- }
447
- crate::provider::wire::parse_provider(value).map(crate::provider::wire::provider_wire)
448
- }
449
-
450
- fn role_path(selected: &SelectedTeam, agent_id: &AgentId) -> PathBuf {
451
- selected
452
- .team_dir
453
- .join("agents")
454
- .join(format!("{agent_id}.md"))
455
- }
456
-
457
- fn validate_id(agent_id: &AgentId) -> Result<(), LifecycleError> {
458
- let id = agent_id.as_str();
459
- if id.is_empty() || id == "." || id == ".." || id.contains(['/', '\\', '\0']) {
460
- return Err(LifecycleError::RequirementUnmet("invalid agent id".into()));
461
- }
462
- Ok(())
463
- }
464
-
465
- fn check_destination(path: &Path, source: Option<&Path>) -> Result<(), LifecycleError> {
466
- if fs::symlink_metadata(path).is_ok() {
467
- if !source.is_some_and(|source| {
468
- fs::canonicalize(source)
469
- .ok()
470
- .zip(fs::canonicalize(path).ok())
471
- .is_some_and(|(source, target)| source == target)
472
- }) {
473
- return Err(LifecycleError::RequirementUnmet(format!(
474
- "role file already exists: {}{}",
475
- path.display(),
476
- if source.is_none() {
477
- "; reuse it with --role-file or re-adopt it with --force"
478
- } else {
479
- ""
480
- }
481
- )));
482
- }
483
- }
484
- Ok(())
485
- }
486
-
487
- fn read_text(path: &Path) -> Result<String, LifecycleError> {
488
- fs::read_to_string(path)
489
- .map_err(|error| LifecycleError::Compile(format!("{}: {error}", path.display())))
490
- }
491
-
492
- // Unlike read_front_matter, this returns the unnormalised body slice, including blank lines.
493
- fn role_parts(text: &str) -> Result<(Value, &str), LifecycleError> {
494
- let (end, opening) = raw_line_end(text, 0);
495
- if &text[..end] != "---" || end == opening {
496
- return Ok((Value::Map(Vec::new()), text));
497
- }
498
- let mut offset = opening;
499
- while offset < text.len() {
500
- let (end, next) = raw_line_end(text, offset);
501
- if &text[offset..end] == "---" {
502
- let header = text[opening..offset]
503
- .replace("\r\n", "\n")
504
- .replace('\r', "\n");
505
- let meta = if header.trim().is_empty() {
506
- Value::Map(Vec::new())
507
- } else {
508
- yaml::loads(&header).map_err(|error| LifecycleError::Compile(error.to_string()))?
509
- };
510
- if !meta.is_map() {
511
- return Err(LifecycleError::Compile(
512
- "role frontmatter must be a map".into(),
513
- ));
514
- }
515
- return Ok((meta, &text[next..]));
516
- }
517
- offset = next;
518
- }
519
- Err(LifecycleError::Compile(
520
- "unterminated role frontmatter".into(),
521
- ))
522
- }
523
-
524
- fn raw_line_end(text: &str, start: usize) -> (usize, usize) {
525
- match text[start..].find(['\r', '\n']) {
526
- Some(relative) => {
527
- let end = start + relative;
528
- (
529
- end,
530
- end + if text[end..].starts_with("\r\n") {
531
- 2
532
- } else {
533
- 1
534
- },
535
- )
536
- }
537
- None => (text.len(), text.len()),
538
- }
539
- }
540
-
541
- pub(crate) fn patched_role(
542
- text: &str,
543
- agent_id: &AgentId,
544
- patch: &RoleConfigPatch,
545
- generated: bool,
546
- ) -> Result<String, LifecycleError> {
547
- if !generated && patch == &RoleConfigPatch::default() {
548
- return Ok(text.to_string());
549
- }
550
- let (mut meta, body) = role_parts(text)?;
551
- for key in ["agent_id", "name"] {
552
- if meta
553
- .get(key)
554
- .and_then(Value::as_str)
555
- .is_some_and(|id| id != agent_id.as_str())
556
- {
557
- return Err(LifecycleError::Compile(format!(
558
- "role {key} does not match {agent_id}"
559
- )));
560
- }
561
- }
562
- let before = meta.clone();
563
- if generated {
564
- set(&mut meta, "name", Value::Str(agent_id.as_str().into()))?;
565
- }
566
- for (key, value) in [
567
- ("provider", patch.provider.as_ref()),
568
- ("model", patch.model.as_ref()),
569
- ("effort", patch.effort.as_ref()),
570
- ("profile", patch.profile.as_ref()),
571
- ] {
572
- if let Some(value) = value {
573
- set(&mut meta, key, Value::Str(value.clone()))?;
574
- }
575
- }
576
- if let Some(value) = patch.bypass {
577
- set(
578
- &mut meta,
579
- "dangerously_skip_permissions",
580
- Value::Bool(value),
581
- )?;
582
- }
583
- if before == meta && patch.prompt.is_none() && !generated {
584
- return Ok(text.to_string());
585
- }
586
- let body = patch.prompt.as_deref().unwrap_or(body);
587
- Ok(format!(
588
- "---\n{}---\n{}",
589
- super::launch::role_source::dump_role_frontmatter(&meta),
590
- body
591
- ))
592
- }
593
-
594
- fn set(map: &mut Value, key: &str, value: Value) -> Result<(), LifecycleError> {
595
- let Value::Map(pairs) = map else {
596
- return Err(LifecycleError::Compile("expected map".into()));
597
- };
598
- if pairs.iter().filter(|(name, _)| name == key).count() > 1 {
599
- return Err(LifecycleError::Compile(format!(
600
- "duplicate role field: {key}"
601
- )));
602
- }
603
- if let Some((_, old)) = pairs.iter_mut().find(|(name, _)| name == key) {
604
- *old = value;
605
- } else {
606
- pairs.push((key.into(), value));
607
- }
608
- Ok(())
609
- }
610
-
611
- fn read_spec(selected: &SelectedTeam) -> Result<Value, LifecycleError> {
612
- let path = selected
613
- .spec_path
614
- .as_ref()
615
- .ok_or_else(|| LifecycleError::Compile("missing spec".into()))?;
616
- yaml::loads(&read_text(path)?).map_err(|error| LifecycleError::Compile(error.to_string()))
617
- }
618
-
619
- fn spec_has_agent(spec: &Value, agent_id: &AgentId) -> bool {
620
- spec.get("agents")
621
- .and_then(Value::as_list)
622
- .is_some_and(|agents| {
623
- agents
624
- .iter()
625
- .any(|agent| agent.get("id").and_then(Value::as_str) == Some(agent_id.as_str()))
626
- })
627
- }
628
-
629
- fn replace_spec_agent(
630
- spec: &mut Value,
631
- agent_id: &AgentId,
632
- next: &Value,
633
- ) -> Result<(), LifecycleError> {
634
- let Value::Map(fields) = spec else {
635
- return Err(LifecycleError::Compile("invalid spec".into()));
636
- };
637
- let Some((_, Value::List(agents))) = fields.iter_mut().find(|(key, _)| key == "agents") else {
638
- return Err(LifecycleError::Compile("spec agents missing".into()));
639
- };
640
- let target = agents
641
- .iter_mut()
642
- .find(|agent| agent.get("id").and_then(Value::as_str) == Some(agent_id.as_str()))
643
- .ok_or_else(|| LifecycleError::Compile("agent missing from spec".into()))?;
644
- *target = next.clone();
645
- Ok(())
646
- }
647
-
648
- fn to_json(value: &Value) -> serde_json::Value {
649
- match value {
650
- Value::Null => serde_json::Value::Null,
651
- Value::Bool(value) => (*value).into(),
652
- Value::Int(value) => (*value).into(),
653
- Value::Float(value) => serde_json::json!(value),
654
- Value::Str(value) => value.clone().into(),
655
- Value::List(values) => values.iter().map(to_json).collect(),
656
- Value::Map(values) => values
657
- .iter()
658
- .map(|(key, value)| (key.clone(), to_json(value)))
659
- .collect(),
660
- }
661
- }
662
-
663
- fn update_agent_config(
664
- selected: &SelectedTeam,
665
- agent_id: &AgentId,
666
- compiled: &Value,
667
- role: &Path,
668
- ) -> Result<(), LifecycleError> {
669
- StateRepository::new(&selected.run_workspace)
670
- .commit(
671
- StateWriteIntent::StartAgent {
672
- team_key: &selected.team_key,
673
- agent_id: agent_id.as_str(),
674
- },
675
- |state| {
676
- if let Some(agent) = state
677
- .get_mut("agents")
678
- .and_then(|agents| agents.get_mut(agent_id.as_str()))
679
- .and_then(serde_json::Value::as_object_mut)
680
- {
681
- for field in [
682
- "provider",
683
- "model",
684
- "role",
685
- "system_prompt",
686
- "output_contract",
687
- "auth_mode",
688
- "effort",
689
- "profile",
690
- "dangerously_skip_permissions",
691
- "communication_mode",
692
- ] {
693
- if let Some(value) = compiled.get(field) {
694
- agent.insert(field.into(), to_json(value));
695
- } else {
696
- agent.remove(field);
697
- }
698
- }
699
- agent.insert(
700
- "dynamic_role_file".into(),
701
- role.to_string_lossy().to_string().into(),
702
- );
703
- agent.insert("role_source_ownership".into(), "external".into());
704
- agent.insert(
705
- "model_source".into(),
706
- if compiled.get("model").and_then(Value::as_str).is_some() {
707
- "role".into()
708
- } else {
709
- "default".into()
710
- },
711
- );
712
- // Imported roles are self-contained in this Team, including
713
- // profile lookup; never retain an external credential root.
714
- if compiled.get("profile").and_then(Value::as_str).is_some() {
715
- agent.insert(
716
- "_profile_dir".into(),
717
- selected
718
- .team_dir
719
- .join("profiles")
720
- .to_string_lossy()
721
- .to_string()
722
- .into(),
723
- );
724
- } else {
725
- agent.remove("_profile_dir");
726
- }
727
- }
728
- },
729
- )
730
- .map(|_| ())
731
- .map_err(|error| LifecycleError::StatePersist(error.to_string()))
732
- }
733
-
734
- struct FileSnapshot {
735
- path: PathBuf,
736
- bytes: Option<Vec<u8>>,
737
- }
738
- impl FileSnapshot {
739
- fn capture(path: PathBuf) -> Result<Self, LifecycleError> {
740
- let bytes = match fs::read(&path) {
741
- Ok(bytes) => Some(bytes),
742
- Err(error) if error.kind() == std::io::ErrorKind::NotFound => None,
743
- Err(error) => return Err(LifecycleError::StatePersist(error.to_string())),
744
- };
745
- Ok(Self { path, bytes })
746
- }
747
- fn restore(&self) -> Result<(), LifecycleError> {
748
- if self.bytes.is_some() && fs::read(&self.path).ok().as_ref() == self.bytes.as_ref() {
749
- return Ok(());
750
- }
751
- match self.bytes.as_ref() {
752
- Some(bytes) => atomic_write(&self.path, bytes),
753
- None => fs::remove_file(&self.path)
754
- .or_else(|error| {
755
- if error.kind() == std::io::ErrorKind::NotFound {
756
- Ok(())
757
- } else {
758
- Err(error)
759
- }
760
- })
761
- .map_err(|error| LifecycleError::StatePersist(error.to_string())),
762
- }
763
- }
764
- }
765
-
766
- struct RoleSnapshot {
767
- role: FileSnapshot,
768
- spec: FileSnapshot,
769
- }
770
- impl RoleSnapshot {
771
- fn capture(selected: &SelectedTeam, role: &Path) -> Result<Self, LifecycleError> {
772
- Ok(Self {
773
- role: FileSnapshot::capture(role.to_path_buf())?,
774
- spec: FileSnapshot::capture(crate::model::paths::runtime_spec_path(
775
- &selected.run_workspace,
776
- &selected.team_key,
777
- ))?,
778
- })
779
- }
780
- fn rollback(
781
- &self,
782
- selected: &SelectedTeam,
783
- agent_id: &AgentId,
784
- error: LifecycleError,
785
- ) -> LifecycleError {
786
- let mut failures = Vec::new();
787
- for snapshot in [&self.role, &self.spec] {
788
- if let Err(error) = snapshot.restore() {
789
- failures.push(error.to_string());
790
- }
791
- }
792
- let restored = StateRepository::new(&selected.run_workspace).commit(
793
- StateWriteIntent::StartAgent {
794
- team_key: &selected.team_key,
795
- agent_id: agent_id.as_str(),
796
- },
797
- |state| {
798
- for field in ["agents", "agent_lifecycle"] {
799
- if let Some(rows) = state
800
- .get_mut(field)
801
- .and_then(serde_json::Value::as_object_mut)
802
- {
803
- if let Some(old) = selected
804
- .state
805
- .get(field)
806
- .and_then(|rows| rows.get(agent_id.as_str()))
807
- {
808
- rows.insert(agent_id.as_str().into(), old.clone());
809
- } else {
810
- rows.remove(agent_id.as_str());
811
- }
812
- }
813
- }
814
- },
815
- );
816
- if let Err(error) = restored {
817
- failures.push(error.to_string());
818
- }
819
- if failures.is_empty() {
820
- error
821
- } else {
822
- LifecycleError::StatePersist(format!(
823
- "{error}; rollback failed: {}",
824
- failures.join("; ")
825
- ))
826
- }
827
- }
828
- }
829
-
830
- /// Compare-and-replace a role only after its complete native plan is admitted.
831
- /// No rollback of a live/uncertain native generation is implied by this write.
832
- pub(crate) fn commit_contract_role(
833
- path: &Path,
834
- expected: &str,
835
- replacement: &str,
836
- ) -> Result<(), LifecycleError> {
837
- if read_text(path)? != expected {
838
- return Err(LifecycleError::RequirementUnmet("role source changed during admission".into()));
839
- }
840
- if expected != replacement {
841
- atomic_write(path, replacement.as_bytes())?;
842
- }
843
- Ok(())
844
- }
845
-
846
- fn atomic_write(path: &Path, bytes: &[u8]) -> Result<(), LifecycleError> {
847
- static NEXT: AtomicU64 = AtomicU64::new(0);
848
- if let Some(parent) = path.parent() {
849
- fs::create_dir_all(parent).map_err(persist_error)?;
850
- }
851
- if fs::symlink_metadata(path).is_ok_and(|meta| meta.file_type().is_symlink()) {
852
- return Err(LifecycleError::StatePersist(format!(
853
- "refusing to replace symlink: {}",
854
- path.display()
855
- )));
856
- }
857
- let temp = path.with_extension(format!(
858
- "tmp-{}-{}",
859
- std::process::id(),
860
- NEXT.fetch_add(1, Ordering::Relaxed)
861
- ));
862
- let mut created = false;
863
- let result = (|| {
864
- let mut file = OpenOptions::new()
865
- .write(true)
866
- .create_new(true)
867
- .open(&temp)?;
868
- created = true;
869
- if let Ok(meta) = fs::metadata(path) {
870
- file.set_permissions(meta.permissions())?;
871
- }
872
- file.write_all(bytes)?;
873
- file.sync_all()?;
874
- drop(file);
875
- fs::rename(&temp, path)
876
- })();
877
- if result.is_err() && created {
878
- let _ = fs::remove_file(&temp);
879
- }
880
- result.map_err(persist_error)
881
- }
882
-
883
- fn persist_error(error: std::io::Error) -> LifecycleError {
884
- LifecycleError::StatePersist(error.to_string())
885
- }
886
-
887
- #[cfg(test)]
888
- mod tests {
889
- #![allow(clippy::unwrap_used, clippy::expect_used)]
890
- use super::*;
891
-
892
- fn pane(id: &str, session: &str, window: &str) -> crate::transport::PaneInfo {
893
- crate::transport::PaneInfo {
894
- pane_id: crate::transport::PaneId::new(id),
895
- session: crate::transport::SessionName::new(session),
896
- window_name: Some(crate::transport::WindowName::new(window)),
897
- window_index: None,
898
- pane_index: None,
899
- tty: None,
900
- current_command: None,
901
- current_path: None,
902
- active: true,
903
- pane_pid: None,
904
- leader_env: Default::default(),
905
- }
906
- }
907
-
908
- #[test]
909
- fn live_guard_does_not_borrow_a_peer_pane_or_ignore_own_live_cohort() {
910
- use crate::transport::test_support::OfflineTransport;
911
- let state = serde_json::json!({"session_name": "s", "agents": {
912
- "a": {"pane_id": "%b", "window": "a", "status": "running"},
913
- "b": {"pane_id": "%b", "window": "b", "status": "running"}
914
- }});
915
- let id = AgentId::new("a");
916
- let transport = OfflineTransport::new()
917
- .with_targets(vec![pane("%b", "s", "b")])
918
- .with_pane_presence("%b", true);
919
- super::super::restart::ensure_agent_not_running(&state, &id, &transport).unwrap();
920
- let transport = transport
921
- .with_targets(vec![pane("%a", "s", "a"), pane("%b", "s", "b")])
922
- .with_pane_presence("%a", true);
923
- let error = super::super::restart::ensure_agent_not_running(&state, &id, &transport)
924
- .unwrap_err()
925
- .to_string();
926
- assert!(error.contains("already running") && error.contains("cohort proof"));
927
- assert!(error.contains("window=a pane=%a"));
928
- assert!(!error.contains("pane=%b"));
929
- let transport = transport.with_pane_presence("%a", false);
930
- super::super::restart::ensure_agent_not_running(&state, &id, &transport).unwrap();
931
- }
932
-
933
- #[test]
934
- fn force_admission_requires_positive_death_and_never_replaces_a_live_seat() {
935
- use crate::transport::test_support::OfflineTransport;
936
- let state = serde_json::json!({"session_name": "s", "agents": {
937
- "a": {"pane_id": "%a", "window": "a", "status": "running"}
938
- }});
939
- let id = AgentId::new("a");
940
- let dead = OfflineTransport::new()
941
- .with_targets(vec![pane("%other", "other-team", "a")])
942
- .with_pane_presence("%a", false)
943
- .with_pane_presence("%other", true);
944
- assert!(admit_add(&state, &id, false, &dead)
945
- .unwrap_err()
946
- .to_string()
947
- .contains("agent id already exists"));
948
- assert!(admit_add(&state, &id, true, &dead).unwrap());
949
- let live = OfflineTransport::new()
950
- .with_targets(vec![pane("%a", "s", "a")])
951
- .with_pane_presence("%a", true);
952
- assert!(admit_add(&state, &id, true, &live)
953
- .unwrap_err()
954
- .to_string()
955
- .contains("already running"));
956
- assert!(admit_add(&state, &id, true, &OfflineTransport::new()).is_err());
957
- let absent_binding = serde_json::json!({"agents": {"a": {"status": "stopped"}}});
958
- assert!(admit_add(&absent_binding, &id, true, &dead).is_err());
959
- assert!(!admit_add(&serde_json::json!({"agents": {}}), &id, true, &dead).unwrap());
960
- assert!(admit_add(
961
- &state,
962
- &id,
963
- true,
964
- &dead.with_list_targets_error("unknown topology")
965
- )
966
- .is_err());
967
- }
968
-
969
- #[test]
970
- fn file_decisions_need_no_repeated_flags_and_conflicts_fail_closed() {
971
- let text = "---\nprovider: codex\ndangerously_skip_permissions: false\nmodel: native-model\neffort: ultra\nprofile: local\n---\n\nrole body\n";
972
- assert_eq!(
973
- reconcile_creation(text, &RoleConfigPatch::default()).unwrap(),
974
- RoleConfigPatch::default()
975
- );
976
- let same = RoleConfigPatch {
977
- provider: Some("codex".into()),
978
- bypass: Some(false),
979
- model: Some("native-model".into()),
980
- effort: Some("ultra".into()),
981
- profile: Some("local".into()),
982
- prompt: Some("role body".into()),
983
- };
984
- assert_eq!(
985
- reconcile_creation(text, &same).unwrap(),
986
- RoleConfigPatch::default()
987
- );
988
- for different in [
989
- RoleConfigPatch {
990
- provider: Some("pi".into()),
991
- ..Default::default()
992
- },
993
- RoleConfigPatch {
994
- bypass: Some(true),
995
- ..Default::default()
996
- },
997
- RoleConfigPatch {
998
- model: Some("other".into()),
999
- ..Default::default()
1000
- },
1001
- RoleConfigPatch {
1002
- effort: Some("high".into()),
1003
- ..Default::default()
1004
- },
1005
- RoleConfigPatch {
1006
- prompt: Some("other".into()),
1007
- ..Default::default()
1008
- },
1009
- RoleConfigPatch {
1010
- profile: Some("other".into()),
1011
- ..Default::default()
1012
- },
1013
- ] {
1014
- assert!(reconcile_creation(text, &different).is_err());
1015
- }
1016
- let supplied = RoleConfigPatch {
1017
- provider: Some("pi".into()),
1018
- bypass: Some(false),
1019
- ..Default::default()
1020
- };
1021
- assert_eq!(reconcile_creation("body", &supplied).unwrap(), supplied);
1022
- assert!(reconcile_creation("body", &RoleConfigPatch::default()).is_err());
1023
- // An invalid existing declaration is not a missing field to overwrite.
1024
- for field in ["provider", "model", "effort", "profile"] {
1025
- let text =
1026
- format!("---\n{field}: true\ndangerously_skip_permissions: false\n---\nbody");
1027
- assert!(reconcile_creation(&text, &supplied).is_err());
1028
- }
1029
- }
1030
-
1031
- #[cfg(unix)]
1032
- #[test]
1033
- fn cursor_names_in_a_role_file_and_cli_are_one_provider() {
1034
- for file in ["cursor", "cursor-agent", "agent", "cursor_agent"] {
1035
- let text = format!("---\nprovider: {file}\ndangerously_skip_permissions: true\n---\nbody");
1036
- for cli in ["cursor", "agent"] {
1037
- let patch = RoleConfigPatch { provider: Some(cli.into()), ..Default::default() };
1038
- assert_eq!(reconcile_creation(&text, &patch).unwrap(), RoleConfigPatch::default(), "{file}/{cli}");
1039
- }
1040
- let pi = RoleConfigPatch { provider: Some("pi".into()), ..Default::default() };
1041
- assert!(reconcile_creation(&text, &pi).is_err(), "{file}");
1042
- }
1043
- }
1044
-
1045
- #[test]
1046
- fn legacy_cr_only_header_does_not_change_engine_when_patched() {
1047
- let text = "---\rprovider: codex\ndangerously_skip_permissions: false\r---\r\rbody\r";
1048
- let patch = RoleConfigPatch {
1049
- effort: Some("ultra".into()),
1050
- ..Default::default()
1051
- };
1052
- let next = patched_role(text, &AgentId::new("w"), &patch, false).unwrap();
1053
- let (meta, body) = role_parts(&next).unwrap();
1054
- assert_eq!(meta.get("provider"), Some(&Value::Str("codex".into())));
1055
- assert_eq!(body, "\rbody\r");
1056
- assert!(reconcile_creation(text, &RoleConfigPatch::default()).is_ok());
1057
- }
1058
-
1059
- #[test]
1060
- fn partial_patch_preserves_body_and_false_presence() {
1061
- let body = "\r\n\r\n中文\r\n```yaml\r\nx: y\r\n```\r\n";
1062
- let original = format!("---\r\nname: worker\r\nprovider: pi\r\ncustom: kept\r\neffort: max\r\ndangerously_skip_permissions: true\r\n---\r\n{body}");
1063
- let patch = RoleConfigPatch {
1064
- effort: Some("high".into()),
1065
- bypass: Some(false),
1066
- ..Default::default()
1067
- };
1068
- let next = patched_role(&original, &AgentId::new("worker"), &patch, false).unwrap();
1069
- let (meta, actual_body) = role_parts(&next).unwrap();
1070
- assert_eq!(actual_body, body);
1071
- assert_eq!(meta.get("custom"), Some(&Value::Str("kept".into())));
1072
- assert_eq!(meta.get("effort"), Some(&Value::Str("high".into())));
1073
- assert_eq!(
1074
- meta.get("dangerously_skip_permissions"),
1075
- Some(&Value::Bool(false))
1076
- );
1077
- }
1078
-
1079
- #[test]
1080
- fn equal_patch_is_byte_identical() {
1081
- let text = "---\n# keep formatting\nprovider: pi\ndangerously_skip_permissions: false\n---\n\nbody\n";
1082
- let patch = RoleConfigPatch {
1083
- provider: Some("pi".into()),
1084
- bypass: Some(false),
1085
- ..Default::default()
1086
- };
1087
- assert_eq!(
1088
- patched_role(text, &AgentId::new("w"), &patch, false).unwrap(),
1089
- text
1090
- );
1091
- }
1092
-
1093
- #[test]
1094
- fn prompt_replaces_instead_of_appending() {
1095
- let patch = RoleConfigPatch {
1096
- prompt: Some("new\n---\nbody".into()),
1097
- ..Default::default()
1098
- };
1099
- let text = "---\nprovider: codex\n---\nold body";
1100
- let next = patched_role(text, &AgentId::new("w"), &patch, false).unwrap();
1101
- assert_eq!(role_parts(&next).unwrap().1, "new\n---\nbody");
1102
- assert!(!next.contains("old body"));
1103
- }
1104
-
1105
- #[test]
1106
- fn traditional_plain_and_empty_header_remain_supported() {
1107
- for text in ["plain\r\nrole", "---\n---\n\nbody"] {
1108
- assert_eq!(
1109
- patched_role(text, &AgentId::new("w"), &RoleConfigPatch::default(), false).unwrap(),
1110
- text
1111
- );
1112
- }
1113
- }
1114
-
1115
- #[test]
1116
- fn imported_profile_is_team_local_and_failed_update_restores_target_only() {
1117
- let nonce = std::time::SystemTime::now()
1118
- .duration_since(std::time::UNIX_EPOCH)
1119
- .unwrap()
1120
- .as_nanos();
1121
- let root =
1122
- std::env::temp_dir().join(format!("role-projection-{}-{nonce}", std::process::id()));
1123
- let team_dir = root.join("roles");
1124
- let old = serde_json::json!({
1125
- "provider": "codex", "model": "old", "effort": "high",
1126
- "profile": "local", "_profile_dir": "/external/profiles",
1127
- "session_id": "preserved-session", "status": "stopped"
1128
- });
1129
- let sibling = serde_json::json!({"provider": "pi", "model": "keep"});
1130
- let selected_state = serde_json::json!({"agents": {"w": old.clone()}});
1131
- crate::state::persist::save_runtime_state(
1132
- &root,
1133
- &serde_json::json!({
1134
- "teams": {
1135
- "t1": selected_state.clone(),
1136
- "t2": {"agents": {"w": sibling.clone()}}
1137
- }
1138
- }),
1139
- )
1140
- .unwrap();
1141
- let spec_path = crate::model::paths::runtime_spec_path(&root, "t1");
1142
- atomic_write(&spec_path, b"old spec\r\n").unwrap();
1143
- let selected = SelectedTeam {
1144
- run_workspace: root.clone(),
1145
- team_key: "t1".into(),
1146
- state: selected_state,
1147
- team_dir: team_dir.clone(),
1148
- spec_workspace: spec_path.parent().map(Path::to_path_buf),
1149
- spec_path: Some(spec_path.clone()),
1150
- };
1151
- let id = AgentId::new("w");
1152
- let role = role_path(&selected, &id);
1153
- let snapshot = RoleSnapshot::capture(&selected, &role).unwrap();
1154
- let compiled = yaml::loads("provider: codex\nmodel: new\neffort: ultra\nprofile: local\ndangerously_skip_permissions: false\nsystem_prompt: new instructions\n").unwrap();
1155
- atomic_write(&role, b"new role").unwrap();
1156
- atomic_write(&spec_path, b"new spec").unwrap();
1157
- update_agent_config(&selected, &id, &compiled, &role).unwrap();
1158
- let updated = crate::state::persist::load_runtime_state(&root).unwrap();
1159
- let target = &updated["teams"]["t1"]["agents"]["w"];
1160
- assert_eq!(target["model"], "new");
1161
- assert_eq!(target["effort"], "ultra");
1162
- assert_eq!(target["system_prompt"], "new instructions");
1163
- assert_eq!(target["dangerously_skip_permissions"], false);
1164
- assert_eq!(target["session_id"], "preserved-session");
1165
- assert_eq!(
1166
- target["_profile_dir"],
1167
- team_dir.join("profiles").to_string_lossy().as_ref()
1168
- );
1169
- assert_eq!(updated["teams"]["t2"]["agents"]["w"], sibling);
1170
- let error = snapshot.rollback(
1171
- &selected,
1172
- &id,
1173
- LifecycleError::RequirementUnmet("injected failure".into()),
1174
- );
1175
- assert!(error.to_string().contains("injected failure"));
1176
- assert!(!error.to_string().contains("rollback failed"));
1177
- assert!(!role.exists());
1178
- assert_eq!(fs::read(&spec_path).unwrap(), b"old spec\r\n");
1179
- let restored = crate::state::persist::load_runtime_state(&root).unwrap();
1180
- assert_eq!(restored["teams"]["t1"]["agents"]["w"], old);
1181
- assert_eq!(restored["teams"]["t2"]["agents"]["w"], sibling);
1182
- fs::remove_dir_all(root).unwrap();
1183
- }
1184
-
1185
- #[test]
1186
- fn file_snapshot_restores_original_bytes_and_absence() {
1187
- let nonce = std::time::SystemTime::now()
1188
- .duration_since(std::time::UNIX_EPOCH)
1189
- .unwrap()
1190
- .as_nanos();
1191
- let root = std::env::temp_dir().join(format!("role-config-{}-{nonce}", std::process::id()));
1192
- fs::create_dir_all(&root).unwrap();
1193
- let path = root.join("role.md");
1194
- let absent = FileSnapshot::capture(path.clone()).unwrap();
1195
- atomic_write(&path, b"old\r\n\n").unwrap();
1196
- let existing = FileSnapshot::capture(path.clone()).unwrap();
1197
- atomic_write(&path, b"new").unwrap();
1198
- existing.restore().unwrap();
1199
- assert_eq!(fs::read(&path).unwrap(), b"old\r\n\n");
1200
- absent.restore().unwrap();
1201
- assert!(!path.exists());
1202
- assert_eq!(fs::read_dir(&root).unwrap().count(), 0);
1203
- fs::remove_dir(&root).unwrap();
1204
- }
1205
- }