@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,883 +0,0 @@
1
- //!
2
- //! Phase-DX E7 (0.5.9 host-leader-registry-design): file-per-leader discovery index.
3
- //!
4
- //! Location: `~/.team-agent/leaders/<workspace_hash>__<team_key>.json`.
5
- //!
6
- //! Registry entries are a **derived** discovery index — never a source of
7
- //! authority. Read paths must always canonical-validate a resolved entry
8
- //! against the target workspace's runtime state (leader_receiver,
9
- //! owner_epoch, transport_kind) before delivering. Ambiguous short names
10
- //! are refused (`name_ambiguous`) rather than resolved by any priority
11
- //! heuristic.
12
- //!
13
- //! Design constraints (host-leader-registry-design.md §§3, 9):
14
- //! - No shared/global relational store (file-per-team on the host filesystem).
15
- //! - No identity writes on read/send paths — write only happens from
16
- //! `claim-leader`/`attach-leader`/`takeover`/`attach-app-server-leader`
17
- //! canonical success hooks and shutdown/unregister on canonical success.
18
- //! - Registry write failure never fails the underlying binding command;
19
- //! it is discoverability degradation, not binding failure.
20
-
21
- #![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic)]
22
-
23
- use std::fmt;
24
- use std::path::{Path, PathBuf};
25
- use std::time::{Duration, Instant};
26
-
27
- use serde::{Deserialize, Serialize};
28
- use serde_json::{json, Value};
29
- use sha2::{Digest, Sha256};
30
-
31
- use crate::transport::Transport;
32
-
33
- /// Current file schema_version for `~/.team-agent/leaders/*.json`.
34
- pub const REGISTRY_SCHEMA_VERSION: u32 = 1;
35
-
36
- /// Event name emitted after a successful atomic temp+rename write.
37
- pub const EVENT_REGISTERED: &str = "leader_registry.registered";
38
-
39
- /// Event name emitted when the write step (fs::write / rename / dir create)
40
- /// failed — the binding command still returns ok, but discoverability
41
- /// degrades until the next successful canonical hook.
42
- pub const EVENT_WRITE_FAILED: &str = "leader_registry.write_failed";
43
-
44
- /// Event name emitted when a canonical shutdown/unbind removed the entry.
45
- pub const EVENT_UNREGISTERED: &str = "leader_registry.unregistered";
46
-
47
- /// v1 file schema. Fields are copied verbatim from `LeaderRegistryEntry`
48
- /// during atomic writes; readers deserialize the same shape then validate
49
- /// against canonical state before using any field for routing.
50
- #[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
51
- pub struct LeaderRegistryEntry {
52
- pub schema_version: u32,
53
- /// Default delivery name — the runtime `team_key`. Never the display or
54
- /// spec name (host-leader-registry-design.md §5.1).
55
- pub delivery_name: String,
56
- /// Human-friendly qualified name: `<workspace_short>/<team_key>`.
57
- pub qualified_name: String,
58
- /// Stable qualified name: `<workspace_hash>/<team_key>`. Always accepted
59
- /// by resolvers even when the human form is ambiguous.
60
- pub stable_qualified_name: String,
61
- /// Explicit aliases attached at binding time. Alias collisions follow
62
- /// the same `name_ambiguous` rule as delivery_name collisions.
63
- pub aliases: Vec<String>,
64
- pub workspace: PathBuf,
65
- /// sha256-prefix of canonical realpath workspace; used only as a file
66
- /// name component and stable identifier — never an authority key.
67
- pub workspace_hash: String,
68
- pub workspace_short: String,
69
- pub team_key: String,
70
- pub transport_kind: String,
71
- pub channel: serde_json::Value,
72
- pub owner_epoch: u64,
73
- pub attached_at: String,
74
- pub updated_at: String,
75
- pub source: String,
76
- pub status: String,
77
- }
78
-
79
- impl LeaderRegistryEntry {
80
- /// Ambiguous-name refusal semantics (host-leader-registry-design.md §5.2):
81
- /// two entries with the same `delivery_name` remain visible in
82
- /// `leaders` under `ambiguous_names`, and `send --to-leader <short>`
83
- /// refuses `name_ambiguous`. Callers surface the ambiguity via
84
- /// candidates rather than choosing a winner. The design explicitly
85
- /// forbids priority heuristics based on write order, modification
86
- /// time, or ambient state — see host-leader-registry-design.md §5.2
87
- /// for the enumerated exclusions.
88
- #[must_use]
89
- pub fn short_name_collision_refuses() -> &'static str {
90
- "ambiguous"
91
- }
92
- }
93
-
94
- /// Payload key for the list of short-name collisions surfaced by
95
- /// `team-agent leaders` — kept as a top-level constant so both `leaders`
96
- /// and `send --to-leader` refer to the same wire name.
97
- pub const AMBIGUOUS_NAMES_FIELD: &str = "ambiguous_names";
98
-
99
- /// Wire reason used by both `leaders` and `send --to-leader` when a name
100
- /// resolves to multiple canonical-live entries. Candidates carry
101
- /// `workspace_hash` + `stable_qualified_name` so the caller can retry with
102
- /// a fully-qualified form.
103
- pub const REASON_AMBIGUOUS: &str = "name_ambiguous";
104
-
105
- #[derive(Debug, Clone, Copy, PartialEq, Eq)]
106
- pub enum RegistryRollback {
107
- Restored,
108
- Superseded,
109
- }
110
-
111
- #[derive(Debug, Clone, Copy, PartialEq, Eq)]
112
- pub enum RegistryRollbackError {
113
- InvalidReceipt,
114
- LockUnavailable,
115
- ReadFailed,
116
- RestoreFailed,
117
- DeleteFailed,
118
- }
119
-
120
- impl fmt::Display for RegistryRollbackError {
121
- fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
122
- let reason = match self {
123
- Self::InvalidReceipt => "invalid receipt",
124
- Self::LockUnavailable => "registry lock unavailable",
125
- Self::ReadFailed => "registry receipt read failed",
126
- Self::RestoreFailed => "registry restore failed",
127
- Self::DeleteFailed => "registry delete failed",
128
- };
129
- f.write_str(reason)
130
- }
131
- }
132
-
133
- #[derive(Debug, Clone, PartialEq, Eq)]
134
- pub struct RegistryWriteReceipt {
135
- pub path: PathBuf,
136
- pub bytes: Vec<u8>,
137
- }
138
-
139
- #[derive(Debug, Clone, PartialEq, Eq)]
140
- pub struct RegistryWriteOutcome {
141
- pub status: &'static str,
142
- pub path: Option<PathBuf>,
143
- pub receipt: Option<RegistryWriteReceipt>,
144
- pub team_key: String,
145
- pub workspace_hash: String,
146
- pub source: String,
147
- pub owner_epoch: u64,
148
- }
149
-
150
- impl RegistryWriteOutcome {
151
- #[must_use]
152
- pub fn response_json(&self) -> Value {
153
- match &self.path {
154
- Some(path) => json!({"status": self.status, "path": path.display().to_string()}),
155
- None => json!({"status": self.status}),
156
- }
157
- }
158
- }
159
-
160
- /// Absolute path to `~/.team-agent/leaders`. Returns `None` when the
161
- /// caller has no HOME (e.g. some CI shells) — callers should treat this as
162
- /// "no registry" rather than an error, since registry is derived.
163
- #[must_use]
164
- pub fn registry_dir() -> Option<PathBuf> {
165
- let home = std::env::var_os("HOME").map(PathBuf::from)?;
166
- Some(home.join(".team-agent").join("leaders"))
167
- }
168
-
169
- /// Compute the sha256 hex prefix (12 chars) of a canonical workspace path.
170
- /// The canonical form is used so equivalent paths (symlinks, `.`, etc.)
171
- /// map to the same discovery id. If canonicalize fails the input path is
172
- /// used verbatim — the hash is a stable label, not an authority key.
173
- #[must_use]
174
- pub fn workspace_hash(workspace: &Path) -> String {
175
- let canonical = std::fs::canonicalize(workspace).unwrap_or_else(|_| workspace.to_path_buf());
176
- let mut hasher = Sha256::new();
177
- hasher.update(canonical.to_string_lossy().as_bytes());
178
- let digest = hasher.finalize();
179
- digest[..6]
180
- .iter()
181
- .map(|byte| format!("{byte:02x}"))
182
- .collect()
183
- }
184
-
185
- /// Compute the discovery-visible short label for a workspace path —
186
- /// basename by default, "workspace" when the path has no name component.
187
- #[must_use]
188
- fn workspace_short(workspace: &Path) -> String {
189
- workspace
190
- .file_name()
191
- .and_then(|s| s.to_str())
192
- .map(str::to_string)
193
- .unwrap_or_else(|| "workspace".to_string())
194
- }
195
-
196
- /// Assemble a schema-v1 entry from canonical binding fields. Callers use
197
- /// this from the binding-command success hooks; the entry then flows into
198
- /// [`write_entry_best_effort`].
199
- #[must_use]
200
- #[allow(clippy::too_many_arguments)]
201
- pub fn build_entry(
202
- workspace: &Path,
203
- team_key: &str,
204
- transport_kind: &str,
205
- channel: serde_json::Value,
206
- owner_epoch: u64,
207
- source: &str,
208
- now_rfc3339: String,
209
- ) -> LeaderRegistryEntry {
210
- let hash = workspace_hash(workspace);
211
- let short = workspace_short(workspace);
212
- LeaderRegistryEntry {
213
- schema_version: REGISTRY_SCHEMA_VERSION,
214
- delivery_name: team_key.to_string(),
215
- qualified_name: format!("{short}/{team_key}"),
216
- stable_qualified_name: format!("{hash}/{team_key}"),
217
- aliases: Vec::new(),
218
- workspace: std::fs::canonicalize(workspace).unwrap_or_else(|_| workspace.to_path_buf()),
219
- workspace_hash: hash,
220
- workspace_short: short,
221
- team_key: team_key.to_string(),
222
- transport_kind: transport_kind.to_string(),
223
- channel,
224
- owner_epoch,
225
- attached_at: now_rfc3339.clone(),
226
- updated_at: now_rfc3339,
227
- source: source.to_string(),
228
- status: "attached".to_string(),
229
- }
230
- }
231
-
232
- /// After a canonical leader binding succeeds, write the derived host
233
- /// discovery entry from the just-persisted state. Registry failure is
234
- /// non-fatal: callers get a write_failed outcome and the binding remains
235
- /// successful.
236
- pub fn register_binding_from_state_best_effort(
237
- workspace: &Path,
238
- team: Option<&str>,
239
- source: &str,
240
- ) -> Option<RegistryWriteOutcome> {
241
- let Ok(state) = crate::state::persist::load_runtime_state_without_migrations(workspace) else {
242
- return None;
243
- };
244
- let team_key = match team.filter(|team| !team.is_empty()) {
245
- Some(team) => team.to_string(),
246
- None => crate::state::projection::team_state_key(&state),
247
- };
248
- let receiver = state
249
- .get("teams")
250
- .and_then(|value| value.as_object())
251
- .and_then(|teams| teams.get(&team_key))
252
- .and_then(|team| team.get("leader_receiver"))
253
- .or_else(|| state.get("leader_receiver"))
254
- .cloned()?;
255
- let transport_kind = receiver
256
- .get("transport_kind")
257
- .and_then(Value::as_str)
258
- .unwrap_or("direct_tmux")
259
- .to_string();
260
- let owner_epoch = receiver
261
- .get("owner_epoch")
262
- .and_then(Value::as_u64)
263
- .or_else(|| {
264
- state
265
- .get("teams")
266
- .and_then(|value| value.as_object())
267
- .and_then(|teams| teams.get(&team_key))
268
- .and_then(|team| team.get("owner_epoch"))
269
- .and_then(Value::as_u64)
270
- })
271
- .unwrap_or(0);
272
- let entry = build_entry(
273
- workspace,
274
- &team_key,
275
- &transport_kind,
276
- receiver,
277
- owner_epoch,
278
- source,
279
- chrono::Utc::now().to_rfc3339(),
280
- );
281
- let event_log = crate::event_log::EventLog::new(workspace);
282
- let write_receipt = write_entry_best_effort_with_receipt(&entry);
283
- let write_result = write_receipt.as_ref().map(|receipt| receipt.path.clone());
284
- let status = if let Some(path) = &write_result {
285
- let _ = event_log.write(
286
- EVENT_REGISTERED,
287
- json!({
288
- "path": path.display().to_string(),
289
- "team_key": team_key,
290
- "workspace_hash": entry.workspace_hash,
291
- "source": source,
292
- }),
293
- );
294
- "registered"
295
- } else {
296
- let _ = event_log.write(
297
- EVENT_WRITE_FAILED,
298
- json!({
299
- "team_key": team_key,
300
- "workspace_hash": entry.workspace_hash,
301
- "source": source,
302
- }),
303
- );
304
- "write_failed"
305
- };
306
- Some(RegistryWriteOutcome {
307
- status,
308
- path: write_result,
309
- receipt: write_receipt,
310
- team_key,
311
- workspace_hash: entry.workspace_hash,
312
- source: source.to_string(),
313
- owner_epoch: entry.owner_epoch,
314
- })
315
- }
316
-
317
- fn entry_filename(entry: &LeaderRegistryEntry) -> String {
318
- format!("{}__{}.json", entry.workspace_hash, entry.team_key)
319
- }
320
-
321
- /// Registry write is best-effort. Writes to `.<file>.tmp-<pid>-<counter>`
322
- /// first, then atomically renames to `<workspace_hash>__<team_key>.json`.
323
- /// Every writer uses the registry lock so conditional rollback can compare
324
- /// and restore without racing a concurrent publish.
325
- pub fn write_entry_best_effort(entry: &LeaderRegistryEntry) -> Option<PathBuf> {
326
- write_entry_best_effort_with_receipt(entry).map(|receipt| receipt.path)
327
- }
328
-
329
- fn write_entry_best_effort_with_receipt(
330
- entry: &LeaderRegistryEntry,
331
- ) -> Option<RegistryWriteReceipt> {
332
- with_registry_lock(|dir| write_entry_locked(dir, entry)).flatten()
333
- }
334
-
335
- fn write_entry_locked(dir: &Path, entry: &LeaderRegistryEntry) -> Option<RegistryWriteReceipt> {
336
- let final_path = dir.join(entry_filename(entry));
337
- let tmp_name = format!(
338
- ".{}.tmp-{}-{}",
339
- entry_filename(entry),
340
- std::process::id(),
341
- rand_suffix()
342
- );
343
- let tmp_path = dir.join(tmp_name);
344
- let serialized = serde_json::to_vec_pretty(entry).ok()?;
345
- if std::fs::write(&tmp_path, &serialized).is_err() {
346
- let _ = std::fs::remove_file(&tmp_path);
347
- return None;
348
- }
349
- if std::fs::rename(&tmp_path, &final_path).is_err() {
350
- let _ = std::fs::remove_file(&tmp_path);
351
- return None;
352
- }
353
- Some(RegistryWriteReceipt {
354
- path: final_path,
355
- bytes: serialized,
356
- })
357
- }
358
-
359
- /// Restore one registry path only when it still contains this writer's exact
360
- /// receipt. The comparison and restore/delete are held under the same lock as
361
- /// ordinary registry writers, so a winner cannot slip in between them.
362
- pub fn restore_entry_if_current_matches(
363
- receipt: &RegistryWriteReceipt,
364
- previous: Option<&[u8]>,
365
- ) -> Result<RegistryRollback, RegistryRollbackError> {
366
- with_registry_lock_result(|dir| {
367
- if receipt.path.parent() != Some(dir) {
368
- return Err(RegistryRollbackError::InvalidReceipt);
369
- }
370
- let current =
371
- std::fs::read(&receipt.path).map_err(|_| RegistryRollbackError::ReadFailed)?;
372
- if current != receipt.bytes {
373
- return Ok(RegistryRollback::Superseded);
374
- }
375
- match previous {
376
- Some(bytes) if replace_entry_bytes_locked(&receipt.path, bytes) => {
377
- Ok(RegistryRollback::Restored)
378
- }
379
- Some(_) => Err(RegistryRollbackError::RestoreFailed),
380
- None if std::fs::remove_file(&receipt.path).is_ok() => Ok(RegistryRollback::Restored),
381
- None => Err(RegistryRollbackError::DeleteFailed),
382
- }
383
- })
384
- }
385
-
386
- #[derive(Debug, Clone, PartialEq, Eq)]
387
- pub struct RegistryPruneItem {
388
- pub workspace: PathBuf,
389
- pub team_key: String,
390
- pub reason: String,
391
- }
392
-
393
- #[derive(Debug, Clone, PartialEq, Eq)]
394
- pub struct RegistryPruneReport {
395
- pub dry_run: bool,
396
- pub candidates: Vec<RegistryPruneItem>,
397
- pub removed: Vec<RegistryPruneItem>,
398
- pub kept: Vec<RegistryPruneItem>,
399
- pub skipped: Vec<RegistryPruneItem>,
400
- pub errors: Vec<RegistryPruneItem>,
401
- }
402
-
403
- #[derive(Debug, Clone, Copy, PartialEq, Eq)]
404
- enum PruneAttempt {
405
- Removed,
406
- Skipped(&'static str),
407
- Error,
408
- }
409
-
410
- fn prune_item(entry: &LeaderRegistryEntry, reason: impl Into<String>) -> RegistryPruneItem {
411
- RegistryPruneItem {
412
- workspace: entry.workspace.clone(),
413
- team_key: entry.team_key.clone(),
414
- reason: reason.into(),
415
- }
416
- }
417
-
418
- fn prune_reason_from_state(entry: &LeaderRegistryEntry, state: &Value) -> Option<&'static str> {
419
- let (status, reason) = classify_loaded(entry, state);
420
- if status != "STALE" {
421
- return None;
422
- }
423
- match reason.as_deref() {
424
- Some("team_key_not_found") => Some("team_key_not_found"),
425
- Some("team_not_alive") => Some("team_not_alive"),
426
- _ => None,
427
- }
428
- }
429
-
430
- fn prune_reason(
431
- entry: &LeaderRegistryEntry,
432
- status: &str,
433
- reason: Option<&str>,
434
- ) -> Option<&'static str> {
435
- if status != "STALE" || !crate::state::persist::runtime_state_path(&entry.workspace).is_file() {
436
- // A missing state file is unavailable rather than proof that the
437
- // workspace was retired. Only an existing, readable canonical state
438
- // can authorize the team-key cleanup below.
439
- return None;
440
- }
441
- match reason {
442
- Some("team_key_not_found") => Some("team_key_not_found"),
443
- Some("team_not_alive") => Some("team_not_alive"),
444
- _ => None,
445
- }
446
- }
447
-
448
- fn prune_entry_if_current(
449
- path: &Path,
450
- expected: &LeaderRegistryEntry,
451
- snapshot: &[u8],
452
- ) -> PruneAttempt {
453
- let result = crate::state::persist::with_runtime_state_lock_without_migrations(
454
- &expected.workspace,
455
- |state| {
456
- let Some(state) = state.as_ref() else {
457
- return PruneAttempt::Skipped("canonical_state_unavailable");
458
- };
459
- // Lock ordering is canonical state -> host registry. The state
460
- // remains unchanged while the exact registry bytes are checked.
461
- with_registry_lock(|_| {
462
- let Ok(bytes) = std::fs::read(path) else {
463
- return PruneAttempt::Skipped("registry_entry_missing");
464
- };
465
- if bytes != snapshot {
466
- return PruneAttempt::Skipped("registry_entry_changed");
467
- }
468
- let Ok(current) = serde_json::from_slice::<LeaderRegistryEntry>(&bytes) else {
469
- return PruneAttempt::Skipped("registry_entry_changed");
470
- };
471
- if current != *expected {
472
- return PruneAttempt::Skipped("registry_entry_changed");
473
- }
474
- if prune_reason_from_state(&current, state).is_none() {
475
- return PruneAttempt::Skipped("canonical_state_changed");
476
- }
477
- if std::fs::remove_file(path).is_ok() {
478
- PruneAttempt::Removed
479
- } else {
480
- PruneAttempt::Error
481
- }
482
- })
483
- .unwrap_or(PruneAttempt::Skipped("registry_lock_unavailable"))
484
- },
485
- );
486
- result.unwrap_or(PruneAttempt::Skipped("canonical_state_unavailable"))
487
- }
488
-
489
- fn replace_entry_bytes_locked(path: &Path, bytes: &[u8]) -> bool {
490
- let tmp = path.with_extension(format!("rollback-{}-{}", std::process::id(), rand_suffix()));
491
- if std::fs::write(&tmp, bytes).is_err() {
492
- let _ = std::fs::remove_file(&tmp);
493
- return false;
494
- }
495
- if std::fs::rename(&tmp, path).is_err() {
496
- let _ = std::fs::remove_file(&tmp);
497
- return false;
498
- }
499
- true
500
- }
501
-
502
- struct RegistryLock {
503
- file: std::fs::File,
504
- }
505
-
506
- impl RegistryLock {
507
- fn acquire(dir: &Path) -> Option<Self> {
508
- let path = dir.join(".registry.lock");
509
- let file = std::fs::OpenOptions::new()
510
- .create(true)
511
- .read(true)
512
- .write(true)
513
- .truncate(false)
514
- .open(path)
515
- .ok()?;
516
- let started = Instant::now();
517
- loop {
518
- match crate::platform::file_lock::try_lock_once_nonblocking(&file) {
519
- Ok(true) => return Some(Self { file }),
520
- Ok(false) if started.elapsed() < Duration::from_secs(2) => {
521
- std::thread::sleep(Duration::from_millis(10));
522
- }
523
- Ok(false) | Err(_) => return None,
524
- }
525
- }
526
- }
527
- }
528
-
529
- impl Drop for RegistryLock {
530
- fn drop(&mut self) {
531
- let _ = crate::platform::file_lock::unlock(&self.file);
532
- }
533
- }
534
-
535
- fn with_registry_lock<T>(f: impl FnOnce(&Path) -> T) -> Option<T> {
536
- let dir = registry_dir()?;
537
- std::fs::create_dir_all(&dir).ok()?;
538
- let _lock = RegistryLock::acquire(&dir)?;
539
- Some(f(&dir))
540
- }
541
-
542
- fn with_registry_lock_result<T>(
543
- f: impl FnOnce(&Path) -> Result<T, RegistryRollbackError>,
544
- ) -> Result<T, RegistryRollbackError> {
545
- let dir = registry_dir().ok_or(RegistryRollbackError::LockUnavailable)?;
546
- std::fs::create_dir_all(&dir).map_err(|_| RegistryRollbackError::LockUnavailable)?;
547
- let _lock = RegistryLock::acquire(&dir).ok_or(RegistryRollbackError::LockUnavailable)?;
548
- f(&dir)
549
- }
550
-
551
- fn rand_suffix() -> String {
552
- // Non-cryptographic uniqueness: a process-local monotonic counter is
553
- // enough because the tmp name only needs to be unique within one
554
- // process lifetime (we also embed the pid).
555
- use std::sync::atomic::{AtomicU64, Ordering};
556
- static COUNTER: AtomicU64 = AtomicU64::new(0);
557
- let n = COUNTER.fetch_add(1, Ordering::Relaxed);
558
- format!("{n:x}")
559
- }
560
-
561
- /// Remove the registry entry for `(workspace, team_key)` if it exists.
562
- /// Called from shutdown/unbind success hooks after canonical state
563
- /// reflects the leader as no longer bound. Missing files are treated as
564
- /// success — the invariant is "no registry entry after unbind", not
565
- /// "must have found something to delete".
566
- pub fn unregister_entry(workspace: &Path, team_key: &str) -> Option<PathBuf> {
567
- with_registry_lock(|dir| {
568
- let hash = workspace_hash(workspace);
569
- let path = dir.join(format!("{hash}__{team_key}.json"));
570
- if path.exists() {
571
- std::fs::remove_file(&path).ok()?;
572
- return Some(path);
573
- }
574
- None
575
- })
576
- .flatten()
577
- }
578
-
579
- /// Read and deserialize every `*.json` file under the registry directory.
580
- /// Skips unreadable / malformed files silently (returns fewer entries) —
581
- /// registry is a derived discovery index and unreadable files are the
582
- /// "STALE / UNREADABLE" dirty class rather than an error.
583
- #[must_use]
584
- fn read_all_entries_with_bytes() -> Vec<(PathBuf, LeaderRegistryEntry, Vec<u8>)> {
585
- let Some(dir) = registry_dir() else {
586
- return Vec::new();
587
- };
588
- let Ok(read_dir) = std::fs::read_dir(&dir) else {
589
- return Vec::new();
590
- };
591
- let mut out = Vec::new();
592
- for entry in read_dir.flatten() {
593
- let path = entry.path();
594
- if path
595
- .file_name()
596
- .and_then(|s| s.to_str())
597
- .is_some_and(|name| name.starts_with('.'))
598
- {
599
- continue;
600
- }
601
- if path.extension().and_then(|s| s.to_str()) != Some("json") {
602
- continue;
603
- }
604
- let Ok(bytes) = std::fs::read(&path) else {
605
- continue;
606
- };
607
- let Ok(entry) = serde_json::from_slice::<LeaderRegistryEntry>(&bytes) else {
608
- continue;
609
- };
610
- out.push((path, entry, bytes));
611
- }
612
- out.sort_by(|a, b| a.0.cmp(&b.0));
613
- out
614
- }
615
-
616
- #[must_use]
617
- fn read_all_entries() -> Vec<(PathBuf, LeaderRegistryEntry)> {
618
- read_all_entries_with_bytes()
619
- .into_iter()
620
- .map(|(path, entry, _bytes)| (path, entry))
621
- .collect()
622
- }
623
-
624
- /// Validate a registry entry against its canonical workspace state.
625
- /// Returns the wire status label — LIVE when canonical `leader_receiver`
626
- /// matches, STALE otherwise. This is the pruning gate for GC and the
627
- /// per-entry classification the `leaders` CLI reports.
628
- #[must_use]
629
- pub fn classify(entry: &LeaderRegistryEntry) -> (&'static str, Option<String>) {
630
- let Ok(state) = crate::state::persist::load_runtime_state(&entry.workspace) else {
631
- return ("STALE", Some("workspace_no_state".to_string()));
632
- };
633
- classify_loaded(entry, &state)
634
- }
635
-
636
- fn classify_loaded(entry: &LeaderRegistryEntry, state: &Value) -> (&'static str, Option<String>) {
637
- // A missing or malformed `teams` root is an unavailable/legacy state,
638
- // not proof that this registry entry's team was retired. Keep it out of
639
- // the prune candidate set unless a valid map was independently found.
640
- let Some(teams) = state.get("teams").and_then(Value::as_object) else {
641
- return ("STALE", Some("teams_invalid".to_string()));
642
- };
643
- let team = match teams.get(&entry.team_key) {
644
- Some(t) => t,
645
- None => return ("STALE", Some("team_key_not_found".to_string())),
646
- };
647
- // Team status must indicate liveness. Empty / missing counts as
648
- // alive for pre-status states; explicit down/stopped/archived is
649
- // terminal STALE.
650
- let team_status = team
651
- .get("status")
652
- .and_then(|v| v.as_str())
653
- .unwrap_or("alive");
654
- if team_status == "down" || team_status == "stopped" || team_status == "archived" {
655
- return ("STALE", Some("team_not_alive".to_string()));
656
- }
657
- let receiver = team.get("leader_receiver");
658
- let receiver = match receiver {
659
- Some(r) if !r.is_null() => r,
660
- _ => return ("STALE", Some("leader_not_attached".to_string())),
661
- };
662
- let canonical_epoch = receiver
663
- .get("owner_epoch")
664
- .and_then(|v| v.as_u64())
665
- .or_else(|| team.get("owner_epoch").and_then(|v| v.as_u64()))
666
- .unwrap_or(0);
667
- if canonical_epoch != entry.owner_epoch {
668
- return (
669
- "STALE",
670
- Some(format!(
671
- "owner_epoch_mismatch:registry={},canonical={}",
672
- entry.owner_epoch, canonical_epoch
673
- )),
674
- );
675
- }
676
- // Verify the recorded leader pane is actually live on its recorded
677
- // tmux endpoint. A killed tmux session (test's kill_session) means
678
- // the receiver is stale even though the state hasn't been updated.
679
- let pane_id = receiver.get("pane_id").and_then(|v| v.as_str());
680
- let socket = receiver.get("tmux_socket").and_then(|v| v.as_str());
681
- if let (Some(pane), Some(sock)) = (pane_id, socket) {
682
- if !tmux_pane_live(sock, pane) {
683
- return ("STALE", Some("leader_pane_dead".to_string()));
684
- }
685
- }
686
- ("LIVE", None)
687
- }
688
-
689
- fn tmux_pane_live(socket: &str, pane_id: &str) -> bool {
690
- // Best-effort tmux liveness check via the same transport factory the
691
- // main runtime uses. On error we treat the pane as NOT live so a
692
- // send never routes through a socket we could not verify.
693
- let backend = crate::transport_factory::tmux_endpoint_transport(socket);
694
- match backend.list_targets() {
695
- Ok(targets) => targets.iter().any(|t| t.pane_id.as_str() == pane_id),
696
- Err(_) => false,
697
- }
698
- }
699
-
700
- /// Explicitly prune only entries whose readable canonical state proves that
701
- /// the team is gone or terminal. Dead panes, old epochs, unattached leaders,
702
- /// missing/unreadable state, and probe failures remain untouched.
703
- #[must_use]
704
- pub fn prune_registry(dry_run: bool) -> RegistryPruneReport {
705
- let mut report = RegistryPruneReport {
706
- dry_run,
707
- candidates: Vec::new(),
708
- removed: Vec::new(),
709
- kept: Vec::new(),
710
- skipped: Vec::new(),
711
- errors: Vec::new(),
712
- };
713
- let mut registry_lock_unavailable = false;
714
- for (path, entry, snapshot) in read_all_entries_with_bytes() {
715
- let (status, classified_reason) = classify(&entry);
716
- let reason = prune_reason(&entry, status, classified_reason.as_deref());
717
- let kept_reason = classified_reason.as_deref().unwrap_or("LIVE");
718
- let Some(reason) = reason else {
719
- report.kept.push(prune_item(&entry, kept_reason));
720
- continue;
721
- };
722
- let candidate = prune_item(&entry, reason);
723
- report.candidates.push(candidate.clone());
724
- if dry_run {
725
- continue;
726
- }
727
- if registry_lock_unavailable {
728
- report
729
- .skipped
730
- .push(prune_item(&entry, "registry_lock_unavailable"));
731
- continue;
732
- }
733
- match prune_entry_if_current(&path, &entry, &snapshot) {
734
- PruneAttempt::Removed => report.removed.push(candidate),
735
- PruneAttempt::Skipped(reason) => {
736
- if reason == "registry_lock_unavailable" {
737
- registry_lock_unavailable = true;
738
- }
739
- report.skipped.push(prune_item(&entry, reason));
740
- }
741
- PruneAttempt::Error => report.errors.push(prune_item(&entry, "remove_failed")),
742
- }
743
- }
744
- report
745
- }
746
-
747
- /// Read all entries and classify each without mutating the derived index.
748
- /// `send --to-leader` needs to see stale entries so it can refuse with
749
- /// `registry_stale` (and never silently fall through to
750
- /// `leader_name_not_found` just because a listing ran first).
751
- #[must_use]
752
- pub fn list_validated_no_gc() -> Vec<(LeaderRegistryEntry, &'static str, Option<String>)> {
753
- read_all_entries()
754
- .into_iter()
755
- .map(|(_path, entry)| {
756
- let (status, reason) = classify(&entry);
757
- (entry, status, reason)
758
- })
759
- .collect()
760
- }
761
-
762
- #[cfg(test)]
763
- mod tests {
764
- #![allow(clippy::expect_used, clippy::panic, clippy::unwrap_used)]
765
-
766
- use super::*;
767
- use serde_json::json;
768
- use serial_test::serial;
769
- use std::sync::atomic::{AtomicU64, Ordering};
770
-
771
- static TEST_SEQ: AtomicU64 = AtomicU64::new(0);
772
-
773
- struct HomeGuard(Option<std::ffi::OsString>);
774
-
775
- impl Drop for HomeGuard {
776
- fn drop(&mut self) {
777
- if let Some(home) = self.0.take() {
778
- std::env::set_var("HOME", home);
779
- } else {
780
- std::env::remove_var("HOME");
781
- }
782
- }
783
- }
784
-
785
- fn test_home(label: &str) -> (HomeGuard, PathBuf, PathBuf) {
786
- let n = TEST_SEQ.fetch_add(1, Ordering::Relaxed);
787
- let home =
788
- std::env::temp_dir().join(format!("ta-registry-{label}-{}-{n}", std::process::id()));
789
- let workspace = home.join("workspace");
790
- std::fs::create_dir_all(&workspace).expect("create workspace");
791
- let previous = std::env::var_os("HOME");
792
- std::env::set_var("HOME", &home);
793
- (HomeGuard(previous), home, workspace)
794
- }
795
-
796
- fn write_state(workspace: &Path, state: Value) {
797
- let path = crate::state::persist::runtime_state_path(workspace);
798
- std::fs::create_dir_all(path.parent().expect("runtime parent")).expect("create runtime");
799
- std::fs::write(
800
- path,
801
- serde_json::to_vec_pretty(&state).expect("serialize state"),
802
- )
803
- .expect("write state");
804
- }
805
-
806
- fn entry(workspace: &Path, team_key: &str) -> LeaderRegistryEntry {
807
- build_entry(
808
- workspace,
809
- team_key,
810
- "direct_tmux",
811
- json!({"pane_id": "%fixture"}),
812
- 1,
813
- "test",
814
- "2026-09-16T00:00:00Z".to_string(),
815
- )
816
- }
817
-
818
- #[test]
819
- #[serial(env)]
820
- fn prune_removes_team_proven_terminal() {
821
- let (_home_guard, _home, workspace) = test_home("terminal");
822
- write_state(
823
- &workspace,
824
- json!({"teams": {"retired": {"status": "stopped"}}}),
825
- );
826
- let registered = entry(&workspace, "retired");
827
- let path = write_entry_best_effort(&registered).expect("register terminal team");
828
-
829
- let report = prune_registry(false);
830
- assert_eq!(report.candidates.len(), 1);
831
- assert_eq!(report.removed.len(), 1);
832
- assert!(report.skipped.is_empty());
833
- assert!(!path.exists(), "terminal registry entry must be removed");
834
- }
835
-
836
- #[test]
837
- #[serial(env)]
838
- fn prune_removes_missing_key_only_from_valid_teams_map() {
839
- let (_home_guard, _home, workspace) = test_home("missing-key");
840
- write_state(&workspace, json!({"teams": {"other": {"status": "alive"}}}));
841
- let registered = entry(&workspace, "gone");
842
- let path = write_entry_best_effort(&registered).expect("register missing team");
843
-
844
- let report = prune_registry(false);
845
- assert_eq!(report.removed.len(), 1);
846
- assert!(
847
- !path.exists(),
848
- "missing key in valid teams map is removable"
849
- );
850
- }
851
-
852
- #[test]
853
- #[serial(env)]
854
- fn prune_keeps_null_nonobject_and_legacy_teams_shapes() {
855
- for (label, state) in [
856
- ("null", json!({"teams": null})),
857
- ("array", json!({"teams": []})),
858
- ("string", json!({"teams": "legacy"})),
859
- (
860
- "single",
861
- json!({"team_key": "retired", "status": "stopped"}),
862
- ),
863
- ] {
864
- let (_home_guard, _home, workspace) = test_home(label);
865
- write_state(&workspace, state);
866
- let registered = entry(&workspace, "retired");
867
- let path = write_entry_best_effort(&registered).expect("register malformed state");
868
-
869
- let report = prune_registry(false);
870
- assert!(
871
- report.candidates.is_empty(),
872
- "malformed shape became candidate: {label}"
873
- );
874
- assert_eq!(
875
- report.kept.len(),
876
- 1,
877
- "malformed shape must be retained: {label}"
878
- );
879
- assert_eq!(report.kept[0].reason, "teams_invalid");
880
- assert!(path.exists(), "malformed state entry must remain: {label}");
881
- }
882
- }
883
- }