@team-agent/installer 0.5.128 → 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.
- package/README.md +4 -4
- package/README.zh.md +1 -1
- package/package.json +4 -8
- package/skills/team-agent/SKILL.md +37 -20
- package/skills/team-agent/command-coverage.json +169 -183
- package/skills/team-agent/docs/reference/team-agent-operator.md +64 -30
- package/skills/team-agent/references/recovery-runbook.md +1 -1
- package/skills/team-agent/references/team-agent-operator.md +64 -30
- package/templates/team_state.md +1 -1
- package/Cargo.lock +0 -1063
- package/Cargo.toml +0 -36
- package/crates/team-agent/Cargo.toml +0 -69
- package/crates/team-agent/src/app_server_test_support.rs +0 -258
- package/crates/team-agent/src/cli/adapters.rs +0 -1288
- package/crates/team-agent/src/cli/arguments.rs +0 -405
- package/crates/team-agent/src/cli/attach_app_server_leader.rs +0 -98
- package/crates/team-agent/src/cli/diagnose.rs +0 -1721
- package/crates/team-agent/src/cli/emit.rs +0 -2930
- package/crates/team-agent/src/cli/grok_slot.rs +0 -384
- package/crates/team-agent/src/cli/helpers.rs +0 -91
- package/crates/team-agent/src/cli/intent/runtime.rs +0 -1216
- package/crates/team-agent/src/cli/intent/runtime_tests.rs +0 -912
- package/crates/team-agent/src/cli/intent/tests.rs +0 -689
- package/crates/team-agent/src/cli/intent.rs +0 -664
- package/crates/team-agent/src/cli/leader.rs +0 -300
- package/crates/team-agent/src/cli/leader_prompt.rs +0 -261
- package/crates/team-agent/src/cli/leaders.rs +0 -418
- package/crates/team-agent/src/cli/mod.rs +0 -6545
- package/crates/team-agent/src/cli/models.rs +0 -1206
- package/crates/team-agent/src/cli/named_address.rs +0 -1651
- package/crates/team-agent/src/cli/profile.rs +0 -342
- package/crates/team-agent/src/cli/route.rs +0 -212
- package/crates/team-agent/src/cli/schedule.rs +0 -175
- package/crates/team-agent/src/cli/send/coordinator.rs +0 -164
- package/crates/team-agent/src/cli/send/mailbox.rs +0 -145
- package/crates/team-agent/src/cli/send/persist.rs +0 -156
- package/crates/team-agent/src/cli/send/presentation.rs +0 -370
- package/crates/team-agent/src/cli/send/resolve.rs +0 -573
- package/crates/team-agent/src/cli/send.rs +0 -199
- package/crates/team-agent/src/cli/shutdown_socket.rs +0 -288
- package/crates/team-agent/src/cli/spec.rs +0 -165
- package/crates/team-agent/src/cli/status.rs +0 -280
- package/crates/team-agent/src/cli/status_port/agents.rs +0 -358
- package/crates/team-agent/src/cli/status_port/approvals.rs +0 -79
- package/crates/team-agent/src/cli/status_port/brief.rs +0 -1981
- package/crates/team-agent/src/cli/status_port/compact.rs +0 -240
- package/crates/team-agent/src/cli/status_port/format.rs +0 -146
- package/crates/team-agent/src/cli/status_port/inbox.rs +0 -62
- package/crates/team-agent/src/cli/status_port/runtime.rs +0 -197
- package/crates/team-agent/src/cli/status_port/snapshot.rs +0 -208
- package/crates/team-agent/src/cli/status_port/store.rs +0 -416
- package/crates/team-agent/src/cli/status_port/tests.rs +0 -90
- package/crates/team-agent/src/cli/status_port.rs +0 -92
- package/crates/team-agent/src/cli/tests/base.rs +0 -885
- package/crates/team-agent/src/cli/tests/divergence.rs +0 -518
- package/crates/team-agent/src/cli/tests/lane_c.rs +0 -453
- package/crates/team-agent/src/cli/tests/leader_watch.rs +0 -322
- package/crates/team-agent/src/cli/tests/main_preserved.rs +0 -430
- package/crates/team-agent/src/cli/tests/missing_subcommands.rs +0 -336
- package/crates/team-agent/src/cli/tests/mod.rs +0 -137
- package/crates/team-agent/src/cli/tests/named_address.rs +0 -761
- package/crates/team-agent/src/cli/tests/parser_safety.rs +0 -383
- package/crates/team-agent/src/cli/tests/run_delegation.rs +0 -484
- package/crates/team-agent/src/cli/tests/shutdown_kill_plan.rs +0 -2179
- package/crates/team-agent/src/cli/tests/status_brief.rs +0 -1514
- package/crates/team-agent/src/cli/tests/status_send.rs +0 -888
- package/crates/team-agent/src/cli/tests/verb_install_skill.rs +0 -93
- package/crates/team-agent/src/cli/tests/verb_profile.rs +0 -392
- package/crates/team-agent/src/cli/triage.rs +0 -234
- package/crates/team-agent/src/cli/types.rs +0 -591
- package/crates/team-agent/src/codex_app_server.rs +0 -546
- package/crates/team-agent/src/communication_mode/mod.rs +0 -56
- package/crates/team-agent/src/compiler/tests.rs +0 -827
- package/crates/team-agent/src/compiler.rs +0 -890
- package/crates/team-agent/src/conpty/backend.rs +0 -829
- package/crates/team-agent/src/conpty/mod.rs +0 -33
- package/crates/team-agent/src/contract_runtime/backend.rs +0 -1744
- package/crates/team-agent/src/contract_runtime/commands.rs +0 -671
- package/crates/team-agent/src/contract_runtime/config.rs +0 -364
- package/crates/team-agent/src/contract_runtime/config_tests.rs +0 -76
- package/crates/team-agent/src/contract_runtime/discovery.rs +0 -449
- package/crates/team-agent/src/contract_runtime/fork.rs +0 -361
- package/crates/team-agent/src/contract_runtime/forward.rs +0 -288
- package/crates/team-agent/src/contract_runtime/framework.rs +0 -367
- package/crates/team-agent/src/contract_runtime/mcp.rs +0 -394
- package/crates/team-agent/src/contract_runtime/mod.rs +0 -17
- package/crates/team-agent/src/contract_runtime/prompt.rs +0 -216
- package/crates/team-agent/src/contract_runtime/pump.rs +0 -271
- package/crates/team-agent/src/contract_runtime/registry.rs +0 -553
- package/crates/team-agent/src/contract_runtime/workspace.rs +0 -228
- package/crates/team-agent/src/coordinator/backoff.rs +0 -741
- package/crates/team-agent/src/coordinator/conpty_shim.rs +0 -805
- package/crates/team-agent/src/coordinator/health.rs +0 -1185
- package/crates/team-agent/src/coordinator/mod.rs +0 -119
- package/crates/team-agent/src/coordinator/orphan.rs +0 -110
- package/crates/team-agent/src/coordinator/runtime_detectors.rs +0 -798
- package/crates/team-agent/src/coordinator/runtime_observation.rs +0 -84
- package/crates/team-agent/src/coordinator/steps/abnormal.rs +0 -2309
- package/crates/team-agent/src/coordinator/steps/delivery.rs +0 -15
- package/crates/team-agent/src/coordinator/steps/health_sync.rs +0 -15
- package/crates/team-agent/src/coordinator/steps/mod.rs +0 -126
- package/crates/team-agent/src/coordinator/steps/persist.rs +0 -15
- package/crates/team-agent/src/coordinator/steps/runtime_prompts.rs +0 -15
- package/crates/team-agent/src/coordinator/steps/session_gate.rs +0 -16
- package/crates/team-agent/src/coordinator/tests/a0_lostupdate.rs +0 -158
- package/crates/team-agent/src/coordinator/tests/abnormal.rs +0 -312
- package/crates/team-agent/src/coordinator/tests/api_error_recovery.rs +0 -300
- package/crates/team-agent/src/coordinator/tests/basics.rs +0 -211
- package/crates/team-agent/src/coordinator/tests/daemon.rs +0 -514
- package/crates/team-agent/src/coordinator/tests/energy.rs +0 -549
- package/crates/team-agent/src/coordinator/tests/health_sync.rs +0 -503
- package/crates/team-agent/src/coordinator/tests/main_preserved.rs +0 -186
- package/crates/team-agent/src/coordinator/tests/mod.rs +0 -295
- package/crates/team-agent/src/coordinator/tests/s3_bounded.rs +0 -207
- package/crates/team-agent/src/coordinator/tests/spine.rs +0 -308
- package/crates/team-agent/src/coordinator/tests/takeover.rs +0 -286
- package/crates/team-agent/src/coordinator/tests/tick_core.rs +0 -237
- package/crates/team-agent/src/coordinator/tick.rs +0 -3024
- package/crates/team-agent/src/coordinator/types.rs +0 -540
- package/crates/team-agent/src/db/agent_health_capture.rs +0 -126
- package/crates/team-agent/src/db/message_store.rs +0 -1527
- package/crates/team-agent/src/db/migration.rs +0 -984
- package/crates/team-agent/src/db/mod.rs +0 -26
- package/crates/team-agent/src/db/schema.rs +0 -663
- package/crates/team-agent/src/diagnose/comms.rs +0 -242
- package/crates/team-agent/src/diagnose/mod.rs +0 -44
- package/crates/team-agent/src/diagnose/orphans.rs +0 -889
- package/crates/team-agent/src/direct_delivery.rs +0 -368
- package/crates/team-agent/src/event_log.rs +0 -502
- package/crates/team-agent/src/fake_worker.rs +0 -303
- package/crates/team-agent/src/inbox_store.rs +0 -169
- package/crates/team-agent/src/kill_audit.rs +0 -106
- package/crates/team-agent/src/layout/manager.rs +0 -248
- package/crates/team-agent/src/layout/mod.rs +0 -36
- package/crates/team-agent/src/layout/overlay.rs +0 -134
- package/crates/team-agent/src/layout/placement.rs +0 -52
- package/crates/team-agent/src/layout/recovery.rs +0 -106
- package/crates/team-agent/src/layout/runtime_sessions.rs +0 -317
- package/crates/team-agent/src/layout/sessions.rs +0 -294
- package/crates/team-agent/src/layout/tmux_endpoint.rs +0 -165
- package/crates/team-agent/src/layout/worker_env.rs +0 -721
- package/crates/team-agent/src/layout/worker_window_helpers.rs +0 -37
- package/crates/team-agent/src/leader/helpers.rs +0 -178
- package/crates/team-agent/src/leader/incident.rs +0 -6
- package/crates/team-agent/src/leader/lease.rs +0 -3356
- package/crates/team-agent/src/leader/mod.rs +0 -104
- package/crates/team-agent/src/leader/owner_bind.rs +0 -400
- package/crates/team-agent/src/leader/prompt.rs +0 -905
- package/crates/team-agent/src/leader/provider_attribution.rs +0 -247
- package/crates/team-agent/src/leader/rediscover/tests.rs +0 -689
- package/crates/team-agent/src/leader/rediscover.rs +0 -1192
- package/crates/team-agent/src/leader/registry.rs +0 -888
- package/crates/team-agent/src/leader/start.rs +0 -4501
- package/crates/team-agent/src/leader/takeover.rs +0 -65
- package/crates/team-agent/src/leader/tests/basics.rs +0 -337
- package/crates/team-agent/src/leader/tests/byte_findings.rs +0 -355
- package/crates/team-agent/src/leader/tests/claim_leader_mailbox_flush_contract.rs +0 -544
- package/crates/team-agent/src/leader/tests/historical_inventory_upgrade_gate_red.rs +0 -624
- package/crates/team-agent/src/leader/tests/identity.rs +0 -465
- package/crates/team-agent/src/leader/tests/identity_session_names.rs +0 -42
- package/crates/team-agent/src/leader/tests/idle.rs +0 -113
- package/crates/team-agent/src/leader/tests/lease_api.rs +0 -520
- package/crates/team-agent/src/leader/tests/lease_claim.rs +0 -629
- package/crates/team-agent/src/leader/tests/mod.rs +0 -106
- package/crates/team-agent/src/leader/tests/prompt_contract.rs +0 -268
- package/crates/team-agent/src/leader/tests/rediscover.rs +0 -403
- package/crates/team-agent/src/leader/tests/rehomed_env.rs +0 -83
- package/crates/team-agent/src/leader/tests/team_in_team_state_scope_red.rs +0 -1273
- package/crates/team-agent/src/leader/tests/terminal_and_replay_vs_rerender_invariants_red.rs +0 -417
- package/crates/team-agent/src/leader/tests/wake_start_owner.rs +0 -168
- package/crates/team-agent/src/leader/types.rs +0 -534
- package/crates/team-agent/src/lib.rs +0 -147
- package/crates/team-agent/src/lifecycle/helpers.rs +0 -212
- package/crates/team-agent/src/lifecycle/launch/add_agent.rs +0 -881
- package/crates/team-agent/src/lifecycle/launch/add_agent_state.rs +0 -598
- package/crates/team-agent/src/lifecycle/launch/agent_state.rs +0 -332
- package/crates/team-agent/src/lifecycle/launch/clone_agent.rs +0 -204
- package/crates/team-agent/src/lifecycle/launch/fork_agent.rs +0 -107
- package/crates/team-agent/src/lifecycle/launch/fork_entry.rs +0 -144
- package/crates/team-agent/src/lifecycle/launch/identity.rs +0 -674
- package/crates/team-agent/src/lifecycle/launch/layout.rs +0 -350
- package/crates/team-agent/src/lifecycle/launch/leader_context.rs +0 -589
- package/crates/team-agent/src/lifecycle/launch/mcp_config.rs +0 -160
- package/crates/team-agent/src/lifecycle/launch/ownership.rs +0 -105
- package/crates/team-agent/src/lifecycle/launch/pi_mcp.rs +0 -899
- package/crates/team-agent/src/lifecycle/launch/pi_mcp_extension.js +0 -225
- package/crates/team-agent/src/lifecycle/launch/plan.rs +0 -269
- package/crates/team-agent/src/lifecycle/launch/quick_start.rs +0 -5200
- package/crates/team-agent/src/lifecycle/launch/quick_start_transport.rs +0 -368
- package/crates/team-agent/src/lifecycle/launch/readiness.rs +0 -519
- package/crates/team-agent/src/lifecycle/launch/role_source.rs +0 -287
- package/crates/team-agent/src/lifecycle/launch/spawn.rs +0 -473
- package/crates/team-agent/src/lifecycle/launch/spec_state.rs +0 -801
- package/crates/team-agent/src/lifecycle/launch/state_projection.rs +0 -624
- package/crates/team-agent/src/lifecycle/launch/worker_env.rs +0 -558
- package/crates/team-agent/src/lifecycle/launch.rs +0 -557
- package/crates/team-agent/src/lifecycle/lock.rs +0 -350
- package/crates/team-agent/src/lifecycle/mod.rs +0 -79
- package/crates/team-agent/src/lifecycle/pane_input_lock.rs +0 -239
- package/crates/team-agent/src/lifecycle/profile_launch.rs +0 -1067
- package/crates/team-agent/src/lifecycle/profile_smoke.rs +0 -557
- package/crates/team-agent/src/lifecycle/restart/agent.rs +0 -2199
- package/crates/team-agent/src/lifecycle/restart/avatar.rs +0 -1798
- package/crates/team-agent/src/lifecycle/restart/claude_fork.rs +0 -163
- package/crates/team-agent/src/lifecycle/restart/common.rs +0 -2362
- package/crates/team-agent/src/lifecycle/restart/orchestrator.rs +0 -65
- package/crates/team-agent/src/lifecycle/restart/preflight.rs +0 -113
- package/crates/team-agent/src/lifecycle/restart/rebuild.rs +0 -5135
- package/crates/team-agent/src/lifecycle/restart/remove.rs +0 -1504
- package/crates/team-agent/src/lifecycle/restart/selection.rs +0 -559
- package/crates/team-agent/src/lifecycle/restart/team_state.rs +0 -329
- package/crates/team-agent/src/lifecycle/restart.rs +0 -227
- package/crates/team-agent/src/lifecycle/role_config.rs +0 -1230
- package/crates/team-agent/src/lifecycle/tests/acceptance_b_batch_red.rs +0 -1021
- package/crates/team-agent/src/lifecycle/tests/agent_ops.rs +0 -1205
- package/crates/team-agent/src/lifecycle/tests/b0_legacy_snapshot_nonauthority_contract.rs +0 -265
- package/crates/team-agent/src/lifecycle/tests/b0_reader_hideone_audit_contract.rs +0 -490
- package/crates/team-agent/src/lifecycle/tests/behavioral_diff_264_red.rs +0 -656
- package/crates/team-agent/src/lifecycle/tests/claude_compatible_config_red.rs +0 -325
- package/crates/team-agent/src/lifecycle/tests/claude_profile_launch_red.rs +0 -447
- package/crates/team-agent/src/lifecycle/tests/cli_argv_routing_red.rs +0 -1638
- package/crates/team-agent/src/lifecycle/tests/clone_agent_preserves_source_tools.rs +0 -427
- package/crates/team-agent/src/lifecycle/tests/clone_fork_copilot_perms_red.rs +0 -770
- package/crates/team-agent/src/lifecycle/tests/communication_mode_runtime_contract_red.rs +0 -419
- package/crates/team-agent/src/lifecycle/tests/copilot_provider_red.rs +0 -2035
- package/crates/team-agent/src/lifecycle/tests/core.rs +0 -1327
- package/crates/team-agent/src/lifecycle/tests/f032_startup_prompt_best_effort_red.rs +0 -315
- package/crates/team-agent/src/lifecycle/tests/fixtures/pi_list_models_g0.stdout.txt +0 -222
- package/crates/team-agent/src/lifecycle/tests/fixtures/pi_list_models_spaced_ids.stdout.txt +0 -9
- package/crates/team-agent/src/lifecycle/tests/fixtures/pi_native_mcp_host.mjs +0 -72
- package/crates/team-agent/src/lifecycle/tests/gate_fixtures.rs +0 -562
- package/crates/team-agent/src/lifecycle/tests/grok_require_explicit_model_red.rs +0 -110
- package/crates/team-agent/src/lifecycle/tests/harvest2_a_batch_red.rs +0 -512
- package/crates/team-agent/src/lifecycle/tests/host_cotenant_death_p0_contract.rs +0 -921
- package/crates/team-agent/src/lifecycle/tests/issue238_codex_effort_contract.rs +0 -68
- package/crates/team-agent/src/lifecycle/tests/lane_ops.rs +0 -1851
- package/crates/team-agent/src/lifecycle/tests/launch_spawn.rs +0 -3983
- package/crates/team-agent/src/lifecycle/tests/lifecycle_lock.rs +0 -339
- package/crates/team-agent/src/lifecycle/tests/lifecycle_rollback_red.rs +0 -596
- package/crates/team-agent/src/lifecycle/tests/main_preserved.rs +0 -513
- package/crates/team-agent/src/lifecycle/tests/mcp_tool_name_format_red.rs +0 -69
- package/crates/team-agent/src/lifecycle/tests/phase_b_contracts.rs +0 -824
- package/crates/team-agent/src/lifecycle/tests/phase_golden.rs +0 -723
- package/crates/team-agent/src/lifecycle/tests/pi_catalog_red.rs +0 -164
- package/crates/team-agent/src/lifecycle/tests/pi_command_red.rs +0 -200
- package/crates/team-agent/src/lifecycle/tests/pi_compiler_red.rs +0 -210
- package/crates/team-agent/src/lifecycle/tests/pi_dual_entry_red.rs +0 -527
- package/crates/team-agent/src/lifecycle/tests/pi_executable_mcp_red.rs +0 -1062
- package/crates/team-agent/src/lifecycle/tests/pi_fork_session_inheritance_red.rs +0 -1419
- package/crates/team-agent/src/lifecycle/tests/pi_lifecycle_doctor_red.rs +0 -98
- package/crates/team-agent/src/lifecycle/tests/pi_native_mcp_capability_red.rs +0 -550
- package/crates/team-agent/src/lifecycle/tests/pi_session_delivery_red.rs +0 -226
- package/crates/team-agent/src/lifecycle/tests/quick_start_worker_readiness_red.rs +0 -332
- package/crates/team-agent/src/lifecycle/tests/realmachine_clusters_1_6_red.rs +0 -786
- package/crates/team-agent/src/lifecycle/tests/realmachine_residual_g1_g4_red.rs +0 -648
- package/crates/team-agent/src/lifecycle/tests/restart.rs +0 -185
- package/crates/team-agent/src/lifecycle/tests/restart_build_before_destroy_0540_contract.rs +0 -1053
- package/crates/team-agent/src/lifecycle/tests/restart_liveness_red.rs +0 -679
- package/crates/team-agent/src/lifecycle/tests/restart_rebind_hotfix_252_red.rs +0 -1126
- package/crates/team-agent/src/lifecycle/tests/restart_session_capture_red.rs +0 -153
- package/crates/team-agent/src/lifecycle/tests/resume_recover_red.rs +0 -427
- package/crates/team-agent/src/lifecycle/tests/stale_team_saveconflict_contract.rs +0 -431
- package/crates/team-agent/src/lifecycle/tests/startup_latency_contract.rs +0 -1066
- package/crates/team-agent/src/lifecycle/tests/status_credential_redaction_contract.rs +0 -876
- package/crates/team-agent/src/lifecycle/tests/subprep_codex_trust_roundtrip_red.rs +0 -1026
- package/crates/team-agent/src/lifecycle/tests/swallow_batch4_semantics_red.rs +0 -342
- package/crates/team-agent/src/lifecycle/tests/team_in_team_identity_scope_red.rs +0 -543
- package/crates/team-agent/src/lifecycle/tests/team_in_team_sibling_quick_start_red.rs +0 -1103
- package/crates/team-agent/src/lifecycle/tests/team_in_team_state_scope_red.rs +0 -1276
- package/crates/team-agent/src/lifecycle/tests/team_key_retirement_roster_red.rs +0 -145
- package/crates/team-agent/src/lifecycle/tests/team_key_retirement_txn_red.rs +0 -603
- package/crates/team-agent/src/lifecycle/tests/test_isolation_escape_contract.rs +0 -936
- package/crates/team-agent/src/lifecycle/tests/upgrade_compat_0211_red.rs +0 -970
- package/crates/team-agent/src/lifecycle/tests/verify_rs031_window_consistency_red.rs +0 -995
- package/crates/team-agent/src/lifecycle/tests/worker_spawn_env_red.rs +0 -836
- package/crates/team-agent/src/lifecycle/tests.rs +0 -105
- package/crates/team-agent/src/lifecycle/types.rs +0 -877
- package/crates/team-agent/src/lifecycle/worker_command_context.rs +0 -363
- package/crates/team-agent/src/main.rs +0 -52
- package/crates/team-agent/src/mcp_server/helpers.rs +0 -510
- package/crates/team-agent/src/mcp_server/lifecycle_tools/mod.rs +0 -6
- package/crates/team-agent/src/mcp_server/lifecycle_tools/state_status.rs +0 -43
- package/crates/team-agent/src/mcp_server/mod.rs +0 -119
- package/crates/team-agent/src/mcp_server/normalize.rs +0 -485
- package/crates/team-agent/src/mcp_server/readiness.rs +0 -527
- package/crates/team-agent/src/mcp_server/readiness_tests.rs +0 -348
- package/crates/team-agent/src/mcp_server/tests/golden.rs +0 -285
- package/crates/team-agent/src/mcp_server/tests/normalize.rs +0 -247
- package/crates/team-agent/src/mcp_server/tests/scoped.rs +0 -158
- package/crates/team-agent/src/mcp_server/tests/send.rs +0 -353
- package/crates/team-agent/src/mcp_server/tests/tools.rs +0 -341
- package/crates/team-agent/src/mcp_server/tests/wire.rs +0 -254
- package/crates/team-agent/src/mcp_server/tests.rs +0 -43
- package/crates/team-agent/src/mcp_server/tools.rs +0 -889
- package/crates/team-agent/src/mcp_server/types.rs +0 -396
- package/crates/team-agent/src/mcp_server/wire.rs +0 -867
- package/crates/team-agent/src/messaging/activity.rs +0 -461
- package/crates/team-agent/src/messaging/address.rs +0 -87
- package/crates/team-agent/src/messaging/delivery.rs +0 -3727
- package/crates/team-agent/src/messaging/delivery_consumption_tests.rs +0 -234
- package/crates/team-agent/src/messaging/enter_rescue.rs +0 -362
- package/crates/team-agent/src/messaging/enter_rescue_tests.rs +0 -633
- package/crates/team-agent/src/messaging/helpers.rs +0 -336
- package/crates/team-agent/src/messaging/leader_channel.rs +0 -287
- package/crates/team-agent/src/messaging/leader_receiver.rs +0 -786
- package/crates/team-agent/src/messaging/mod.rs +0 -167
- package/crates/team-agent/src/messaging/persist.rs +0 -377
- package/crates/team-agent/src/messaging/presentation.rs +0 -417
- package/crates/team-agent/src/messaging/reminders.rs +0 -316
- package/crates/team-agent/src/messaging/results/collection_tests.rs +0 -719
- package/crates/team-agent/src/messaging/results.rs +0 -1415
- package/crates/team-agent/src/messaging/scheduler.rs +0 -223
- package/crates/team-agent/src/messaging/selftest.rs +0 -315
- package/crates/team-agent/src/messaging/send.rs +0 -981
- package/crates/team-agent/src/messaging/tests/basic.rs +0 -346
- package/crates/team-agent/src/messaging/tests/dup_inject.rs +0 -657
- package/crates/team-agent/src/messaging/tests/e23.rs +0 -49
- package/crates/team-agent/src/messaging/tests/leader_channel.rs +0 -169
- package/crates/team-agent/src/messaging/tests/leader_inject_acceptance.rs +0 -817
- package/crates/team-agent/src/messaging/tests/main_preserved.rs +0 -85
- package/crates/team-agent/src/messaging/tests/mod.rs +0 -306
- package/crates/team-agent/src/messaging/tests/runtime.rs +0 -3549
- package/crates/team-agent/src/messaging/tests/spine.rs +0 -853
- package/crates/team-agent/src/messaging/tests/wave2.rs +0 -381
- package/crates/team-agent/src/messaging/trust.rs +0 -215
- package/crates/team-agent/src/messaging/types.rs +0 -472
- package/crates/team-agent/src/messaging/wait.rs +0 -366
- package/crates/team-agent/src/messaging/watchers.rs +0 -916
- package/crates/team-agent/src/model/enums.rs +0 -356
- package/crates/team-agent/src/model/errors.rs +0 -18
- package/crates/team-agent/src/model/ids.rs +0 -165
- package/crates/team-agent/src/model/mod.rs +0 -29
- package/crates/team-agent/src/model/name_similarity.rs +0 -252
- package/crates/team-agent/src/model/pane_authority_refusal.rs +0 -359
- package/crates/team-agent/src/model/paths.rs +0 -257
- package/crates/team-agent/src/model/routing.rs +0 -387
- package/crates/team-agent/src/model/spec.rs +0 -1184
- package/crates/team-agent/src/model/task_graph.rs +0 -321
- package/crates/team-agent/src/model/testdata/fuzz.golden.yaml +0 -43
- package/crates/team-agent/src/model/testdata/fuzz.yaml +0 -43
- package/crates/team-agent/src/model/testdata/spec_invalid_a.yaml +0 -209
- package/crates/team-agent/src/model/testdata/team.spec.golden.yaml +0 -208
- package/crates/team-agent/src/model/testdata/team.spec.yaml +0 -208
- package/crates/team-agent/src/model/yaml/tests.rs +0 -188
- package/crates/team-agent/src/model/yaml.rs +0 -802
- package/crates/team-agent/src/os_probe.rs +0 -280
- package/crates/team-agent/src/packaging/install.rs +0 -219
- package/crates/team-agent/src/packaging/migrate.rs +0 -38
- package/crates/team-agent/src/packaging/mod.rs +0 -91
- package/crates/team-agent/src/packaging/repair.rs +0 -33
- package/crates/team-agent/src/packaging/tests.rs +0 -822
- package/crates/team-agent/src/packaging/types.rs +0 -417
- package/crates/team-agent/src/platform/argv.rs +0 -331
- package/crates/team-agent/src/platform/errors.rs +0 -104
- package/crates/team-agent/src/platform/file_lock.rs +0 -279
- package/crates/team-agent/src/platform/mod.rs +0 -5
- package/crates/team-agent/src/platform/process.rs +0 -545
- package/crates/team-agent/src/provider/adapter.rs +0 -1150
- package/crates/team-agent/src/provider/adapters/claude.rs +0 -78
- package/crates/team-agent/src/provider/adapters/claude_fork.rs +0 -131
- package/crates/team-agent/src/provider/adapters/copilot.rs +0 -75
- package/crates/team-agent/src/provider/adapters/copilot_fork.rs +0 -307
- package/crates/team-agent/src/provider/adapters/fake.rs +0 -18
- package/crates/team-agent/src/provider/adapters/mod.rs +0 -30
- package/crates/team-agent/src/provider/adapters/pi.rs +0 -106
- package/crates/team-agent/src/provider/approvals/mod.rs +0 -3
- package/crates/team-agent/src/provider/approvals/parsing.rs +0 -586
- package/crates/team-agent/src/provider/approvals/runtime_prompts.rs +0 -205
- package/crates/team-agent/src/provider/argv_route.rs +0 -405
- package/crates/team-agent/src/provider/bypass_flags.rs +0 -116
- package/crates/team-agent/src/provider/classify.rs +0 -708
- package/crates/team-agent/src/provider/command_helpers.rs +0 -45
- package/crates/team-agent/src/provider/faults.rs +0 -165
- package/crates/team-agent/src/provider/helpers.rs +0 -52
- package/crates/team-agent/src/provider/mod.rs +0 -78
- package/crates/team-agent/src/provider/model_catalog.rs +0 -910
- package/crates/team-agent/src/provider/session/capture.rs +0 -2731
- package/crates/team-agent/src/provider/session/context_fork/claude.rs +0 -123
- package/crates/team-agent/src/provider/session/context_fork/outcome.rs +0 -172
- package/crates/team-agent/src/provider/session/context_fork.rs +0 -396
- package/crates/team-agent/src/provider/session/mod.rs +0 -36
- package/crates/team-agent/src/provider/session/resume.rs +0 -269
- package/crates/team-agent/src/provider/session_scan/claude.rs +0 -523
- package/crates/team-agent/src/provider/session_scan/common/tests.rs +0 -316
- package/crates/team-agent/src/provider/session_scan/common.rs +0 -654
- package/crates/team-agent/src/provider/session_scan/copilot.rs +0 -280
- package/crates/team-agent/src/provider/session_scan/pi.rs +0 -185
- package/crates/team-agent/src/provider/session_scan.rs +0 -71
- package/crates/team-agent/src/provider/startup_prompt.rs +0 -757
- package/crates/team-agent/src/provider/submit_now.rs +0 -71
- package/crates/team-agent/src/provider/testdata/copilot-ready-marker.txt +0 -5
- package/crates/team-agent/src/provider/testdata/copilot-trust-prompt.txt +0 -19
- package/crates/team-agent/src/provider/testdata/grok-models-authenticated.stdout +0 -9
- package/crates/team-agent/src/provider/testdata/grok-models-unauthenticated.stdout +0 -7
- package/crates/team-agent/src/provider/tests/adapter.rs +0 -441
- package/crates/team-agent/src/provider/tests/classify.rs +0 -148
- package/crates/team-agent/src/provider/tests/command_helpers.rs +0 -35
- package/crates/team-agent/src/provider/tests/copilot_fork.rs +0 -191
- package/crates/team-agent/src/provider/tests/faults.rs +0 -200
- package/crates/team-agent/src/provider/tests/idle.rs +0 -177
- package/crates/team-agent/src/provider/tests/wire.rs +0 -213
- package/crates/team-agent/src/provider/tests.rs +0 -32
- package/crates/team-agent/src/provider/types.rs +0 -529
- package/crates/team-agent/src/provider/wire.rs +0 -394
- package/crates/team-agent/src/redaction.rs +0 -259
- package/crates/team-agent/src/state/abnormal_watch/tests/mod.rs +0 -248
- package/crates/team-agent/src/state/abnormal_watch.rs +0 -144
- package/crates/team-agent/src/state/identity.rs +0 -914
- package/crates/team-agent/src/state/identity_keys.rs +0 -132
- package/crates/team-agent/src/state/mod.rs +0 -74
- package/crates/team-agent/src/state/owner_gate.rs +0 -482
- package/crates/team-agent/src/state/ownership.rs +0 -563
- package/crates/team-agent/src/state/paths.rs +0 -395
- package/crates/team-agent/src/state/persist.rs +0 -3811
- package/crates/team-agent/src/state/projection.rs +0 -1508
- package/crates/team-agent/src/state/repository/intent.rs +0 -14
- package/crates/team-agent/src/state/repository/tests.rs +0 -184
- package/crates/team-agent/src/state/repository.rs +0 -866
- package/crates/team-agent/src/state/selector.rs +0 -160
- package/crates/team-agent/src/state/testdata/state-rich.canonical.json +0 -133
- package/crates/team-agent/src/test_support/models_timeout_fixture.rs +0 -144
- package/crates/team-agent/src/tmux_backend/enter_rescue.rs +0 -325
- package/crates/team-agent/src/tmux_backend/enter_rescue_tests.rs +0 -432
- package/crates/team-agent/src/tmux_backend/native_input.rs +0 -513
- package/crates/team-agent/src/tmux_backend/native_input_tests.rs +0 -742
- package/crates/team-agent/src/tmux_backend/tests.rs +0 -4103
- package/crates/team-agent/src/tmux_backend.rs +0 -4492
- package/crates/team-agent/src/topology.rs +0 -537
- package/crates/team-agent/src/transport/enter_rescue.rs +0 -166
- package/crates/team-agent/src/transport/native_input.rs +0 -491
- package/crates/team-agent/src/transport/test_support.rs +0 -743
- package/crates/team-agent/src/transport/tests/behavior.rs +0 -326
- package/crates/team-agent/src/transport/tests/mod.rs +0 -194
- package/crates/team-agent/src/transport/tests/wire.rs +0 -574
- package/crates/team-agent/src/transport.rs +0 -1655
- package/crates/team-agent/src/transport_factory.rs +0 -819
|
@@ -1,1504 +0,0 @@
|
|
|
1
|
-
//! ---
|
|
2
|
-
//! purpose: 摘掉一席,含双确认门、六态一致性判定、原子摘除与字节级回滚快照
|
|
3
|
-
//! contract:
|
|
4
|
-
//! provides:
|
|
5
|
-
//! - name: remove_agent
|
|
6
|
-
//! what: 从 spec、state、team_state 与 role 文件原子摘除一席
|
|
7
|
-
//! - name: remove_agent_flag_requirements
|
|
8
|
-
//! what: 只做前置判定,告诉调用方这次需要哪些确认标志
|
|
9
|
-
//! - name: ForceRecreateSnapshot
|
|
10
|
-
//! what: 强制重建用的快照,可在失败后恢复席位并清掉本事务新起的 pane
|
|
11
|
-
//! - name: resolve_seat
|
|
12
|
-
//! what: 由期望、持久与物理三源定出席位的唯一身份与一致性态
|
|
13
|
-
//! - name: spec_without_agent
|
|
14
|
-
//! what: 生成摘掉该席位后的 spec,并清掉指向它的路由与启动项
|
|
15
|
-
//! depends:
|
|
16
|
-
//! - crate::lifecycle::lock
|
|
17
|
-
//! - crate::lifecycle::restart::agent
|
|
18
|
-
//! - crate::lifecycle::restart::team_state
|
|
19
|
-
//! - crate::state::projection
|
|
20
|
-
//! - crate::transport::Transport
|
|
21
|
-
//! boundary:
|
|
22
|
-
//! - 未给 from_spec 确认或运行中未给 force 时拒绝,不擅自摘除
|
|
23
|
-
//! - 物理身份必须收敛到唯一的 session 加 window 加 pane,全局同名窗口不算身份
|
|
24
|
-
//! - 只删托管目录下的角色副本,用户自带的 role 文件不删
|
|
25
|
-
//! maturity: wired
|
|
26
|
-
//! ---
|
|
27
|
-
use super::agent::{resolve_team_scoped_state_or_refuse, start_agent_at_paths};
|
|
28
|
-
use super::common::*;
|
|
29
|
-
use super::team_state::write_team_state;
|
|
30
|
-
use super::*;
|
|
31
|
-
use crate::lifecycle::lock::{acquire_agent_lifecycle_lock, LifecycleLockRequest};
|
|
32
|
-
|
|
33
|
-
/// ---
|
|
34
|
-
/// purpose: 摘掉一席的对外入口,取生命周期锁后走实体实现
|
|
35
|
-
/// params:
|
|
36
|
-
/// from_spec: 确认同时从 spec 里摘掉
|
|
37
|
-
/// force: 席位仍在运行时必须给出
|
|
38
|
-
/// returns: 摘除结果
|
|
39
|
-
/// errors: 选不到 team 返回 TeamSelect;确认标志不足或一致性态不允许时返回 RequirementUnmet;写盘失败返回 StatePersist
|
|
40
|
-
/// contract_id: lifecycle.remove_agent.entry
|
|
41
|
-
/// ---
|
|
42
|
-
/// `remove_agent(workspace, agent_id, from_spec, force, team)`(`lifecycle/agents.py:22`)。
|
|
43
|
-
/// 从 spec/state/team_state/agent_health 原子摘除。
|
|
44
|
-
/// 托管目录 `.team/dynamic-role-files/` 下的物化副本随席位清掉;托管目录之外的
|
|
45
|
-
/// `--role-file` 仍是用户资产,不删。A-28 对托管文件「默认保留」的承诺已由
|
|
46
|
-
/// `ledger.seat-supply-prereq` 推翻。
|
|
47
|
-
/// `_RemoveRollback` 字节级快照回滚全部运行时变更。未传 from_spec 确认 / 运行中未传 force → 拒绝。
|
|
48
|
-
pub fn remove_agent(
|
|
49
|
-
workspace: &Path,
|
|
50
|
-
agent_id: &AgentId,
|
|
51
|
-
from_spec: bool,
|
|
52
|
-
force: bool,
|
|
53
|
-
team: Option<&str>,
|
|
54
|
-
) -> Result<RemoveAgentOutcome, LifecycleError> {
|
|
55
|
-
let paths = lifecycle_paths(workspace, team)?;
|
|
56
|
-
let canonical_team = paths.canonical_team(team).map(str::to_string);
|
|
57
|
-
let team = canonical_team.as_deref();
|
|
58
|
-
let _lock = acquire_agent_lifecycle_lock(LifecycleLockRequest {
|
|
59
|
-
workspace: &paths.run_workspace,
|
|
60
|
-
operation: "remove-agent",
|
|
61
|
-
team,
|
|
62
|
-
agent_id: Some(agent_id),
|
|
63
|
-
})?;
|
|
64
|
-
let transport = paths.tmux_backend()?;
|
|
65
|
-
remove_agent_at_paths(
|
|
66
|
-
&paths.run_workspace,
|
|
67
|
-
&paths.spec_workspace,
|
|
68
|
-
agent_id,
|
|
69
|
-
from_spec,
|
|
70
|
-
force,
|
|
71
|
-
team,
|
|
72
|
-
&transport,
|
|
73
|
-
)
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
/// ---
|
|
77
|
-
/// purpose: 带注入 transport 的摘席入口,自行取锁
|
|
78
|
-
/// returns: 摘除结果
|
|
79
|
-
/// errors: 同 remove_agent
|
|
80
|
-
/// contract_id: lifecycle.remove_agent.entry
|
|
81
|
-
/// ---
|
|
82
|
-
pub(crate) fn remove_agent_with_transport(
|
|
83
|
-
workspace: &Path,
|
|
84
|
-
agent_id: &AgentId,
|
|
85
|
-
from_spec: bool,
|
|
86
|
-
force: bool,
|
|
87
|
-
team: Option<&str>,
|
|
88
|
-
transport: &dyn crate::transport::Transport,
|
|
89
|
-
) -> Result<RemoveAgentOutcome, LifecycleError> {
|
|
90
|
-
let paths = lifecycle_paths(workspace, team)?;
|
|
91
|
-
let canonical_team = paths.canonical_team(team).map(str::to_string);
|
|
92
|
-
let team = canonical_team.as_deref();
|
|
93
|
-
let _lock = acquire_agent_lifecycle_lock(LifecycleLockRequest {
|
|
94
|
-
workspace: &paths.run_workspace,
|
|
95
|
-
operation: "remove-agent",
|
|
96
|
-
team,
|
|
97
|
-
agent_id: Some(agent_id),
|
|
98
|
-
})?;
|
|
99
|
-
remove_agent_at_paths(
|
|
100
|
-
&paths.run_workspace,
|
|
101
|
-
&paths.spec_workspace,
|
|
102
|
-
agent_id,
|
|
103
|
-
from_spec,
|
|
104
|
-
force,
|
|
105
|
-
team,
|
|
106
|
-
transport,
|
|
107
|
-
)
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
/// ---
|
|
111
|
-
/// purpose: 调用方已持有生命周期锁时的摘席入口,本函数不再取锁
|
|
112
|
-
/// returns: 摘除结果
|
|
113
|
-
/// errors: 同 remove_agent
|
|
114
|
-
/// contract_id: lifecycle.remove_agent.entry
|
|
115
|
-
/// ---
|
|
116
|
-
pub(crate) fn remove_agent_with_transport_locked(
|
|
117
|
-
workspace: &Path,
|
|
118
|
-
agent_id: &AgentId,
|
|
119
|
-
from_spec: bool,
|
|
120
|
-
force: bool,
|
|
121
|
-
team: Option<&str>,
|
|
122
|
-
transport: &dyn crate::transport::Transport,
|
|
123
|
-
) -> Result<RemoveAgentOutcome, LifecycleError> {
|
|
124
|
-
let paths = lifecycle_paths(workspace, team)?;
|
|
125
|
-
let canonical_team = paths.canonical_team(team).map(str::to_string);
|
|
126
|
-
let team = canonical_team.as_deref();
|
|
127
|
-
remove_agent_at_paths(
|
|
128
|
-
&paths.run_workspace,
|
|
129
|
-
&paths.spec_workspace,
|
|
130
|
-
agent_id,
|
|
131
|
-
from_spec,
|
|
132
|
-
force,
|
|
133
|
-
team,
|
|
134
|
-
transport,
|
|
135
|
-
)
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
pub(crate) struct ForceRecreateSnapshot {
|
|
139
|
-
rollback: RemoveRollback,
|
|
140
|
-
run_workspace: std::path::PathBuf,
|
|
141
|
-
spec_workspace: std::path::PathBuf,
|
|
142
|
-
before_physical: Option<crate::transport::PaneInfo>,
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
impl ForceRecreateSnapshot {
|
|
146
|
-
/// ---
|
|
147
|
-
/// purpose: 为强制重建拍一份可回滚的快照
|
|
148
|
-
/// returns: 含逻辑回滚数据与摘除前物理 pane 身份的快照
|
|
149
|
-
/// errors: 选不到 team 或席位解析失败时返回 LifecycleError
|
|
150
|
-
/// ---
|
|
151
|
-
pub(crate) fn capture(
|
|
152
|
-
workspace: &Path,
|
|
153
|
-
agent_id: &AgentId,
|
|
154
|
-
team: Option<&str>,
|
|
155
|
-
transport: &dyn crate::transport::Transport,
|
|
156
|
-
) -> Result<Self, LifecycleError> {
|
|
157
|
-
let paths = lifecycle_paths(workspace, team)?;
|
|
158
|
-
let canonical_team = paths.canonical_team(team).map(str::to_string);
|
|
159
|
-
let team = canonical_team.as_deref();
|
|
160
|
-
let seat = resolve_seat(
|
|
161
|
-
&paths.run_workspace,
|
|
162
|
-
&paths.spec_workspace,
|
|
163
|
-
agent_id,
|
|
164
|
-
team,
|
|
165
|
-
transport,
|
|
166
|
-
)?;
|
|
167
|
-
let mut rollback = RemoveRollback::capture(
|
|
168
|
-
&paths.run_workspace,
|
|
169
|
-
&paths.spec_workspace,
|
|
170
|
-
&seat.spec,
|
|
171
|
-
&seat.state,
|
|
172
|
-
&seat.team_key,
|
|
173
|
-
agent_id,
|
|
174
|
-
)?;
|
|
175
|
-
rollback.restore_running = seat.physical.is_some();
|
|
176
|
-
Ok(Self {
|
|
177
|
-
rollback,
|
|
178
|
-
run_workspace: paths.run_workspace,
|
|
179
|
-
spec_workspace: paths.spec_workspace,
|
|
180
|
-
before_physical: seat.physical,
|
|
181
|
-
})
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
/// ---
|
|
185
|
-
/// purpose: 按快照恢复席位的逻辑状态
|
|
186
|
-
/// returns: 恢复过程中的错误描述列表,空表示恢复干净
|
|
187
|
-
/// ---
|
|
188
|
-
pub(crate) fn restore(
|
|
189
|
-
&self,
|
|
190
|
-
team: Option<&str>,
|
|
191
|
-
transport: &dyn crate::transport::Transport,
|
|
192
|
-
) -> Vec<String> {
|
|
193
|
-
self.rollback
|
|
194
|
-
.restore(&self.run_workspace, &self.spec_workspace, team, transport)
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
/// ---
|
|
198
|
-
/// purpose: 旧 pane 已被消费后的恢复,先杀掉本次事务新起的 pane 再恢复逻辑快照
|
|
199
|
-
/// returns: 错误描述列表;恢复干净时还会校验物理身份是否回到摘除前的 session 与窗口
|
|
200
|
-
/// ---
|
|
201
|
-
/// The old pane has already been consumed. Any exact pane now resolved for
|
|
202
|
-
/// this seat belongs to this force-recreate transaction and must be removed
|
|
203
|
-
/// before the logical snapshot is restored, otherwise rollback can leave a
|
|
204
|
-
/// duplicate worker behind.
|
|
205
|
-
pub(crate) fn restore_after_consumption(
|
|
206
|
-
&self,
|
|
207
|
-
transport: &dyn crate::transport::Transport,
|
|
208
|
-
) -> Vec<String> {
|
|
209
|
-
let mut errors = Vec::new();
|
|
210
|
-
match resolve_seat(
|
|
211
|
-
&self.run_workspace,
|
|
212
|
-
&self.spec_workspace,
|
|
213
|
-
&self.rollback.agent_id,
|
|
214
|
-
Some(self.rollback.team_key.as_str()),
|
|
215
|
-
transport,
|
|
216
|
-
) {
|
|
217
|
-
Ok(after) => {
|
|
218
|
-
if let Some(pane) = after.physical {
|
|
219
|
-
if let Err(error) = transport.kill_pane(&pane.pane_id) {
|
|
220
|
-
errors.push(format!(
|
|
221
|
-
"transaction_pane:{}:{error}",
|
|
222
|
-
pane.pane_id.as_str()
|
|
223
|
-
));
|
|
224
|
-
}
|
|
225
|
-
}
|
|
226
|
-
}
|
|
227
|
-
Err(error) => errors.push(format!("transaction_resolve:{error}")),
|
|
228
|
-
}
|
|
229
|
-
errors.extend(self.rollback.restore(
|
|
230
|
-
&self.run_workspace,
|
|
231
|
-
&self.spec_workspace,
|
|
232
|
-
Some(self.rollback.team_key.as_str()),
|
|
233
|
-
transport,
|
|
234
|
-
));
|
|
235
|
-
if errors.is_empty() {
|
|
236
|
-
if let Some(before) = &self.before_physical {
|
|
237
|
-
match resolve_seat(
|
|
238
|
-
&self.run_workspace,
|
|
239
|
-
&self.spec_workspace,
|
|
240
|
-
&self.rollback.agent_id,
|
|
241
|
-
Some(self.rollback.team_key.as_str()),
|
|
242
|
-
transport,
|
|
243
|
-
) {
|
|
244
|
-
Ok(after)
|
|
245
|
-
if after.physical.as_ref().is_some_and(|pane| {
|
|
246
|
-
pane.session == before.session && pane.window_name == before.window_name
|
|
247
|
-
}) => {}
|
|
248
|
-
Ok(after) => errors.push(format!(
|
|
249
|
-
"worker_restore:before physical tuple not restored: {:?}",
|
|
250
|
-
after.consistency
|
|
251
|
-
)),
|
|
252
|
-
Err(error) => errors.push(format!("worker_restore_resolve:{error}")),
|
|
253
|
-
}
|
|
254
|
-
}
|
|
255
|
-
}
|
|
256
|
-
errors
|
|
257
|
-
}
|
|
258
|
-
|
|
259
|
-
/// ---
|
|
260
|
-
/// purpose: 强制重建之后要求席位处于一致态
|
|
261
|
-
/// returns: 一致时返回空值
|
|
262
|
-
/// errors: 解析失败透传;解析出的一致性态不是 Coherent 时返回 StatePersist
|
|
263
|
-
/// ---
|
|
264
|
-
pub(crate) fn require_coherent(
|
|
265
|
-
&self,
|
|
266
|
-
agent_id: &AgentId,
|
|
267
|
-
team: Option<&str>,
|
|
268
|
-
transport: &dyn crate::transport::Transport,
|
|
269
|
-
) -> Result<(), LifecycleError> {
|
|
270
|
-
let after = resolve_seat(
|
|
271
|
-
&self.run_workspace,
|
|
272
|
-
&self.spec_workspace,
|
|
273
|
-
agent_id,
|
|
274
|
-
team,
|
|
275
|
-
transport,
|
|
276
|
-
)?;
|
|
277
|
-
if after.consistency == SeatConsistency::Coherent {
|
|
278
|
-
Ok(())
|
|
279
|
-
} else {
|
|
280
|
-
Err(LifecycleError::StatePersist(format!(
|
|
281
|
-
"force-recreate post-resolve for {agent_id} is {:?}",
|
|
282
|
-
after.consistency
|
|
283
|
-
)))
|
|
284
|
-
}
|
|
285
|
-
}
|
|
286
|
-
}
|
|
287
|
-
|
|
288
|
-
/// ---
|
|
289
|
-
/// purpose: 只做前置判定,给出这次摘席需要哪些确认标志
|
|
290
|
-
/// returns: 标志要求;不做任何摘除动作
|
|
291
|
-
/// errors: 选不到 team 或席位解析失败时返回 LifecycleError
|
|
292
|
-
/// ---
|
|
293
|
-
pub fn remove_agent_flag_requirements(
|
|
294
|
-
workspace: &Path,
|
|
295
|
-
agent_id: &AgentId,
|
|
296
|
-
team: Option<&str>,
|
|
297
|
-
) -> Result<RemoveAgentFlagRequirements, LifecycleError> {
|
|
298
|
-
let paths = lifecycle_paths(workspace, team)?;
|
|
299
|
-
let canonical_team = paths.canonical_team(team).map(str::to_string);
|
|
300
|
-
let team = canonical_team.as_deref();
|
|
301
|
-
let transport = paths.tmux_backend()?;
|
|
302
|
-
Ok(remove_agent_preflight(
|
|
303
|
-
&paths.run_workspace,
|
|
304
|
-
&paths.spec_workspace,
|
|
305
|
-
agent_id,
|
|
306
|
-
team,
|
|
307
|
-
&transport,
|
|
308
|
-
)?
|
|
309
|
-
.requirements)
|
|
310
|
-
}
|
|
311
|
-
|
|
312
|
-
struct RemoveAgentPreflight {
|
|
313
|
-
seat: ResolvedSeat,
|
|
314
|
-
requirements: RemoveAgentFlagRequirements,
|
|
315
|
-
}
|
|
316
|
-
|
|
317
|
-
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
|
318
|
-
pub(super) enum SeatConsistency {
|
|
319
|
-
Absent,
|
|
320
|
-
Coherent,
|
|
321
|
-
StateOnly,
|
|
322
|
-
SpecOnly,
|
|
323
|
-
PhysicalOnly,
|
|
324
|
-
Mixed,
|
|
325
|
-
}
|
|
326
|
-
|
|
327
|
-
pub(super) struct ResolvedSeat {
|
|
328
|
-
pub(super) state: serde_json::Value,
|
|
329
|
-
pub(super) spec: YamlValue,
|
|
330
|
-
pub(super) team_key: String,
|
|
331
|
-
pub(super) session: crate::transport::SessionName,
|
|
332
|
-
pub(super) window: String,
|
|
333
|
-
pub(super) physical: Option<crate::transport::PaneInfo>,
|
|
334
|
-
pub(super) state_present: bool,
|
|
335
|
-
pub(super) spec_present: bool,
|
|
336
|
-
pub(super) consistency: SeatConsistency,
|
|
337
|
-
}
|
|
338
|
-
|
|
339
|
-
/// ---
|
|
340
|
-
/// purpose: 由 spec、runtime state 与物理 pane 三源定出席位身份与一致性态
|
|
341
|
-
/// params:
|
|
342
|
-
/// transport: 已绑定该 team endpoint 的 transport
|
|
343
|
-
/// returns: 席位解析结果,含所在 session、窗口、物理 pane 与六态之一的一致性判定
|
|
344
|
-
/// errors: 团队作用域 state 取不到或 owner 门不过时返回 LifecycleError
|
|
345
|
-
/// ---
|
|
346
|
-
/// Resolve one seat from the selected team's desired, persisted and physical
|
|
347
|
-
/// sources. The transport is already bound to the selected team's endpoint;
|
|
348
|
-
/// physical identity is then narrowed to exactly one `(session, window, pane)`
|
|
349
|
-
/// tuple. A global window-name match is never accepted as identity.
|
|
350
|
-
pub(super) fn resolve_seat(
|
|
351
|
-
workspace: &Path,
|
|
352
|
-
spec_workspace: &Path,
|
|
353
|
-
agent_id: &AgentId,
|
|
354
|
-
team: Option<&str>,
|
|
355
|
-
transport: &dyn crate::transport::Transport,
|
|
356
|
-
) -> Result<ResolvedSeat, LifecycleError> {
|
|
357
|
-
let state = resolve_team_scoped_state_or_refuse(workspace, team)?;
|
|
358
|
-
crate::lifecycle::launch::ensure_owner_allowed_for_state(&state, Some(agent_id))?;
|
|
359
|
-
let spec = load_team_spec(spec_workspace)?;
|
|
360
|
-
let team_key = state
|
|
361
|
-
.get("active_team_key")
|
|
362
|
-
.and_then(serde_json::Value::as_str)
|
|
363
|
-
.filter(|key| !key.is_empty())
|
|
364
|
-
.map(str::to_string)
|
|
365
|
-
.unwrap_or_else(|| crate::state::projection::team_state_key(&state));
|
|
366
|
-
let state_present = state
|
|
367
|
-
.get("agents")
|
|
368
|
-
.and_then(|agents| agents.get(agent_id.as_str()))
|
|
369
|
-
.is_some();
|
|
370
|
-
let spec_present = find_spec_agent(&spec, agent_id).is_some();
|
|
371
|
-
let session = state_session_name_from_spec(&state, &spec);
|
|
372
|
-
let window = state
|
|
373
|
-
.get("agents")
|
|
374
|
-
.and_then(|agents| agents.get(agent_id.as_str()))
|
|
375
|
-
.and_then(|agent| agent.get("window"))
|
|
376
|
-
.and_then(serde_json::Value::as_str)
|
|
377
|
-
.filter(|window| !window.is_empty())
|
|
378
|
-
.unwrap_or_else(|| agent_id.as_str())
|
|
379
|
-
.to_string();
|
|
380
|
-
let stored_pane_id = state
|
|
381
|
-
.get("agents")
|
|
382
|
-
.and_then(|agents| agents.get(agent_id.as_str()))
|
|
383
|
-
.and_then(|agent| agent.get("pane_id"))
|
|
384
|
-
.and_then(serde_json::Value::as_str)
|
|
385
|
-
.filter(|pane| !pane.is_empty())
|
|
386
|
-
.map(crate::transport::PaneId::new);
|
|
387
|
-
let mut physical = transport
|
|
388
|
-
.list_targets()
|
|
389
|
-
.map_err(|error| LifecycleError::Transport(format!("resolve seat targets: {error}")))?
|
|
390
|
-
.into_iter()
|
|
391
|
-
.filter(|pane| {
|
|
392
|
-
pane.session == session
|
|
393
|
-
&& pane
|
|
394
|
-
.window_name
|
|
395
|
-
.as_ref()
|
|
396
|
-
.is_some_and(|name| name.as_str() == window)
|
|
397
|
-
&& stored_pane_id
|
|
398
|
-
.as_ref()
|
|
399
|
-
.is_none_or(|stored| pane.pane_id == *stored)
|
|
400
|
-
});
|
|
401
|
-
let mut first = physical.next();
|
|
402
|
-
if physical.next().is_some() {
|
|
403
|
-
return Err(LifecycleError::RequirementUnmet(format!(
|
|
404
|
-
"seat identity ambiguous for {agent_id}: session={} window={window}",
|
|
405
|
-
session.as_str()
|
|
406
|
-
)));
|
|
407
|
-
}
|
|
408
|
-
// Some backends can positively probe an exact pane id even when their
|
|
409
|
-
// global target snapshot is temporarily empty (for example an attached
|
|
410
|
-
// explicit tmux socket during reset). The persisted tuple is scoped by the
|
|
411
|
-
// selected transport endpoint and session; a positive exact-pane probe is
|
|
412
|
-
// therefore safe, unlike a reverse window-name scan.
|
|
413
|
-
if first.is_none() {
|
|
414
|
-
if let Some(pane_id) = stored_pane_id
|
|
415
|
-
.as_ref()
|
|
416
|
-
.filter(|pane| transport.has_pane(pane).ok().flatten() == Some(true))
|
|
417
|
-
{
|
|
418
|
-
first = Some(crate::transport::PaneInfo {
|
|
419
|
-
pane_id: pane_id.clone(),
|
|
420
|
-
session: session.clone(),
|
|
421
|
-
window_index: None,
|
|
422
|
-
window_name: Some(crate::transport::WindowName::new(&window)),
|
|
423
|
-
pane_index: None,
|
|
424
|
-
tty: None,
|
|
425
|
-
current_command: None,
|
|
426
|
-
current_path: None,
|
|
427
|
-
active: true,
|
|
428
|
-
pane_pid: state
|
|
429
|
-
.get("agents")
|
|
430
|
-
.and_then(|agents| agents.get(agent_id.as_str()))
|
|
431
|
-
.and_then(|agent| agent.get("pane_pid"))
|
|
432
|
-
.and_then(serde_json::Value::as_u64)
|
|
433
|
-
.and_then(|pid| u32::try_from(pid).ok()),
|
|
434
|
-
leader_env: std::collections::BTreeMap::new(),
|
|
435
|
-
});
|
|
436
|
-
}
|
|
437
|
-
}
|
|
438
|
-
let physical_present = first.is_some();
|
|
439
|
-
let consistency = match (spec_present, state_present, physical_present) {
|
|
440
|
-
(false, false, false) => SeatConsistency::Absent,
|
|
441
|
-
(true, true, true) => SeatConsistency::Coherent,
|
|
442
|
-
(false, true, false) => SeatConsistency::StateOnly,
|
|
443
|
-
(true, false, false) => SeatConsistency::SpecOnly,
|
|
444
|
-
(false, false, true) => SeatConsistency::PhysicalOnly,
|
|
445
|
-
_ => SeatConsistency::Mixed,
|
|
446
|
-
};
|
|
447
|
-
Ok(ResolvedSeat {
|
|
448
|
-
state,
|
|
449
|
-
spec,
|
|
450
|
-
team_key,
|
|
451
|
-
session,
|
|
452
|
-
window,
|
|
453
|
-
physical: first,
|
|
454
|
-
state_present,
|
|
455
|
-
spec_present,
|
|
456
|
-
consistency,
|
|
457
|
-
})
|
|
458
|
-
}
|
|
459
|
-
|
|
460
|
-
fn remove_agent_preflight(
|
|
461
|
-
workspace: &Path,
|
|
462
|
-
spec_workspace: &Path,
|
|
463
|
-
agent_id: &AgentId,
|
|
464
|
-
team: Option<&str>,
|
|
465
|
-
transport: &dyn crate::transport::Transport,
|
|
466
|
-
) -> Result<RemoveAgentPreflight, LifecycleError> {
|
|
467
|
-
// golden agents.py:34-41: resolve_team_scoped_state FIRST (surfaces the team_target_ambiguous /
|
|
468
|
-
// team_target_unresolved refusal before the owner gate), THEN the owner gate, THEN load_spec +
|
|
469
|
-
// _find_worker (unknown-worker raise). Mirror the stop/reset wiring so remove is byte-symmetric:
|
|
470
|
-
// the team-scoped projection (not a raw load) drives the dynamic/running/from_spec decisions.
|
|
471
|
-
let seat = resolve_seat(workspace, spec_workspace, agent_id, team, transport)?;
|
|
472
|
-
let spec_agent = find_spec_agent(&seat.spec, agent_id);
|
|
473
|
-
// A persisted-only seat is still known and force-removable.
|
|
474
|
-
let dynamic_agent =
|
|
475
|
-
spec_agent.is_none_or(|agent| is_dynamic_agent(&seat.state, agent, agent_id));
|
|
476
|
-
let force_required =
|
|
477
|
-
seat.physical.is_some() || agent_is_running(&seat.state, agent_id, transport);
|
|
478
|
-
let has_session = seat
|
|
479
|
-
.state
|
|
480
|
-
.get("session_name")
|
|
481
|
-
.and_then(|v| v.as_str())
|
|
482
|
-
.is_some_and(|s| !s.is_empty())
|
|
483
|
-
|| seat
|
|
484
|
-
.state
|
|
485
|
-
.get("agents")
|
|
486
|
-
.and_then(|v| v.get(agent_id.as_str()))
|
|
487
|
-
.is_some_and(agent_has_session);
|
|
488
|
-
Ok(RemoveAgentPreflight {
|
|
489
|
-
seat,
|
|
490
|
-
requirements: RemoveAgentFlagRequirements {
|
|
491
|
-
agent_id: agent_id.clone(),
|
|
492
|
-
from_spec_required: !dynamic_agent,
|
|
493
|
-
force_required,
|
|
494
|
-
has_session,
|
|
495
|
-
},
|
|
496
|
-
})
|
|
497
|
-
}
|
|
498
|
-
|
|
499
|
-
fn agent_has_session(agent: &serde_json::Value) -> bool {
|
|
500
|
-
["session_id", "_pending_session_id", "rollout_path"]
|
|
501
|
-
.iter()
|
|
502
|
-
.any(|key| {
|
|
503
|
-
agent
|
|
504
|
-
.get(key)
|
|
505
|
-
.and_then(|v| v.as_str())
|
|
506
|
-
.is_some_and(|s| !s.is_empty())
|
|
507
|
-
})
|
|
508
|
-
}
|
|
509
|
-
|
|
510
|
-
fn remove_agent_at_paths(
|
|
511
|
-
workspace: &Path,
|
|
512
|
-
spec_workspace: &Path,
|
|
513
|
-
agent_id: &AgentId,
|
|
514
|
-
from_spec: bool,
|
|
515
|
-
force: bool,
|
|
516
|
-
team: Option<&str>,
|
|
517
|
-
transport: &dyn crate::transport::Transport,
|
|
518
|
-
) -> Result<RemoveAgentOutcome, LifecycleError> {
|
|
519
|
-
let preflight = remove_agent_preflight(workspace, spec_workspace, agent_id, team, transport)?;
|
|
520
|
-
if preflight.seat.consistency == SeatConsistency::Absent && !force {
|
|
521
|
-
return Err(unknown_worker(agent_id));
|
|
522
|
-
}
|
|
523
|
-
let missing_from_spec = preflight.requirements.from_spec_required && !from_spec;
|
|
524
|
-
let missing_force = preflight.requirements.force_required && !force;
|
|
525
|
-
if missing_from_spec || missing_force {
|
|
526
|
-
return Ok(if missing_from_spec && missing_force {
|
|
527
|
-
RemoveAgentOutcome::RefusedRequiredFlags {
|
|
528
|
-
agent_id: agent_id.clone(),
|
|
529
|
-
from_spec_required: true,
|
|
530
|
-
force_required: true,
|
|
531
|
-
}
|
|
532
|
-
} else if missing_from_spec {
|
|
533
|
-
RemoveAgentOutcome::RefusedFromSpecConfirm {
|
|
534
|
-
agent_id: agent_id.clone(),
|
|
535
|
-
}
|
|
536
|
-
} else {
|
|
537
|
-
RemoveAgentOutcome::RefusedForceRequired {
|
|
538
|
-
agent_id: agent_id.clone(),
|
|
539
|
-
}
|
|
540
|
-
});
|
|
541
|
-
}
|
|
542
|
-
let pi_cleanup_paths = preflight
|
|
543
|
-
.seat
|
|
544
|
-
.state
|
|
545
|
-
.get("agents")
|
|
546
|
-
.and_then(|agents| agents.get(agent_id.as_str()))
|
|
547
|
-
.and_then(|agent| agent.get("provider"))
|
|
548
|
-
.and_then(serde_json::Value::as_str)
|
|
549
|
-
.and_then(crate::provider::wire::parse_provider)
|
|
550
|
-
.filter(|provider| *provider == Provider::Pi)
|
|
551
|
-
.map(|_| {
|
|
552
|
-
crate::lifecycle::launch::pi_mcp::pi_seat_paths(
|
|
553
|
-
workspace,
|
|
554
|
-
&preflight.seat.team_key,
|
|
555
|
-
agent_id.as_str(),
|
|
556
|
-
)
|
|
557
|
-
});
|
|
558
|
-
let paths = LifecyclePathRefs {
|
|
559
|
-
run_workspace: workspace,
|
|
560
|
-
spec_workspace,
|
|
561
|
-
};
|
|
562
|
-
let mut rollback = RemoveRollback::capture(
|
|
563
|
-
paths.run_workspace,
|
|
564
|
-
paths.spec_workspace,
|
|
565
|
-
&preflight.seat.spec,
|
|
566
|
-
&preflight.seat.state,
|
|
567
|
-
&preflight.seat.team_key,
|
|
568
|
-
agent_id,
|
|
569
|
-
)?;
|
|
570
|
-
rollback.restore_running = force && preflight.seat.physical.is_some();
|
|
571
|
-
let result = remove_agent_inner(
|
|
572
|
-
&paths,
|
|
573
|
-
agent_id,
|
|
574
|
-
&preflight.seat.spec,
|
|
575
|
-
preflight.seat.state,
|
|
576
|
-
preflight.seat.physical,
|
|
577
|
-
&preflight.seat.team_key,
|
|
578
|
-
force,
|
|
579
|
-
team,
|
|
580
|
-
transport,
|
|
581
|
-
)
|
|
582
|
-
.and_then(|success| {
|
|
583
|
-
let after = resolve_seat(workspace, spec_workspace, agent_id, team, transport)?;
|
|
584
|
-
if after.consistency != SeatConsistency::Absent {
|
|
585
|
-
return Err(LifecycleError::StatePersist(format!(
|
|
586
|
-
"remove-agent post-resolve for {agent_id} is {:?}",
|
|
587
|
-
after.consistency
|
|
588
|
-
)));
|
|
589
|
-
}
|
|
590
|
-
Ok(success)
|
|
591
|
-
});
|
|
592
|
-
match result {
|
|
593
|
-
Ok(mut success) => {
|
|
594
|
-
if let Some(pi_paths) = pi_cleanup_paths.as_ref() {
|
|
595
|
-
let cleanup = crate::lifecycle::launch::pi_mcp::pi_cleanup_plan(
|
|
596
|
-
crate::lifecycle::launch::pi_mcp::PiCleanupAction::Remove,
|
|
597
|
-
pi_paths,
|
|
598
|
-
);
|
|
599
|
-
for path in cleanup.delete_paths {
|
|
600
|
-
match std::fs::remove_file(&path) {
|
|
601
|
-
Ok(()) => success
|
|
602
|
-
.cleared_locations
|
|
603
|
-
.push(serde_json::json!(path.to_string_lossy().to_string())),
|
|
604
|
-
Err(error) if error.kind() == std::io::ErrorKind::NotFound => {}
|
|
605
|
-
Err(error) => {
|
|
606
|
-
return Err(LifecycleError::StatePersist(format!(
|
|
607
|
-
"remove-agent completed logical removal for {agent_id}, but exact Pi wrapper cleanup failed at {}: {error}",
|
|
608
|
-
path.display()
|
|
609
|
-
)));
|
|
610
|
-
}
|
|
611
|
-
}
|
|
612
|
-
}
|
|
613
|
-
}
|
|
614
|
-
// Foundation-0 F0-2: the historical dual-write to the legacy
|
|
615
|
-
// per-session snapshot has been retired
|
|
616
|
-
// (`.team/artifacts/foundation-0-slice-design.md` §§4-5).
|
|
617
|
-
// Root/projection is the sole runtime authority; the
|
|
618
|
-
// snapshot writer stayed in `lifecycle::helpers` only for
|
|
619
|
-
// diagnostic/migration/test callers.
|
|
620
|
-
write_remove_complete_event(
|
|
621
|
-
paths.run_workspace,
|
|
622
|
-
agent_id,
|
|
623
|
-
from_spec,
|
|
624
|
-
force,
|
|
625
|
-
success.stopped,
|
|
626
|
-
success.role_file_removed,
|
|
627
|
-
success.cleared_locations,
|
|
628
|
-
)?;
|
|
629
|
-
Ok(success.outcome)
|
|
630
|
-
}
|
|
631
|
-
Err(error) => {
|
|
632
|
-
// golden agents.py:110-133: restore is best-effort (collects per-artifact errors, restores ALL),
|
|
633
|
-
// and the ORIGINAL operation error is ALWAYS re-raised, annotated with rollback_ok — a
|
|
634
|
-
// restore-step failure only flips rollback_ok, it never replaces the surfaced cause.
|
|
635
|
-
let restore_errors =
|
|
636
|
-
rollback.restore(paths.run_workspace, paths.spec_workspace, team, transport);
|
|
637
|
-
let rollback_ok = restore_errors.is_empty();
|
|
638
|
-
let rollback_event = RemoveRollbackEvent {
|
|
639
|
-
agent_id,
|
|
640
|
-
workspace: paths.run_workspace,
|
|
641
|
-
from_spec,
|
|
642
|
-
force,
|
|
643
|
-
stopped: rollback.restore_running,
|
|
644
|
-
error: &error,
|
|
645
|
-
rollback_ok,
|
|
646
|
-
restore_errors: &restore_errors,
|
|
647
|
-
};
|
|
648
|
-
let _ = write_remove_rollback_events(rollback_event);
|
|
649
|
-
Err(LifecycleError::StatePersist(format!(
|
|
650
|
-
"remove-agent failed for {agent_id}: {error}; rollback_ok={rollback_ok}"
|
|
651
|
-
)))
|
|
652
|
-
}
|
|
653
|
-
}
|
|
654
|
-
}
|
|
655
|
-
|
|
656
|
-
struct ContractRemovalInputs {
|
|
657
|
-
paths: LifecyclePaths,
|
|
658
|
-
team: Option<String>,
|
|
659
|
-
transport: crate::tmux_backend::TmuxBackend,
|
|
660
|
-
state: serde_json::Value,
|
|
661
|
-
spec: YamlValue,
|
|
662
|
-
team_key: String,
|
|
663
|
-
}
|
|
664
|
-
|
|
665
|
-
impl ContractRemovalInputs {
|
|
666
|
-
fn resolve(workspace: &Path, agent_id: &AgentId, team: Option<&str>) -> Result<Self, LifecycleError> {
|
|
667
|
-
let paths = lifecycle_paths(workspace, team)?;
|
|
668
|
-
let team = paths.canonical_team(team).map(str::to_string);
|
|
669
|
-
let transport = paths.tmux_backend()?;
|
|
670
|
-
let state = resolve_team_scoped_state_or_refuse(&paths.run_workspace, team.as_deref())?;
|
|
671
|
-
crate::lifecycle::launch::ensure_owner_allowed_for_state(&state, Some(agent_id))?;
|
|
672
|
-
let spec = load_team_spec(&paths.spec_workspace)?;
|
|
673
|
-
let team_key = state
|
|
674
|
-
.get("active_team_key")
|
|
675
|
-
.and_then(serde_json::Value::as_str)
|
|
676
|
-
.filter(|key| !key.is_empty())
|
|
677
|
-
.map(str::to_string)
|
|
678
|
-
.unwrap_or_else(|| crate::state::projection::team_state_key(&state));
|
|
679
|
-
Ok(Self { paths, team, transport, state, spec, team_key })
|
|
680
|
-
}
|
|
681
|
-
|
|
682
|
-
fn refs(&self) -> LifecyclePathRefs<'_> {
|
|
683
|
-
LifecyclePathRefs {
|
|
684
|
-
run_workspace: &self.paths.run_workspace,
|
|
685
|
-
spec_workspace: &self.paths.spec_workspace,
|
|
686
|
-
}
|
|
687
|
-
}
|
|
688
|
-
|
|
689
|
-
fn rollback(&self, agent_id: &AgentId) -> Result<RemoveRollback, LifecycleError> {
|
|
690
|
-
RemoveRollback::capture(
|
|
691
|
-
&self.paths.run_workspace,
|
|
692
|
-
&self.paths.spec_workspace,
|
|
693
|
-
&self.spec,
|
|
694
|
-
&self.state,
|
|
695
|
-
&self.team_key,
|
|
696
|
-
agent_id,
|
|
697
|
-
)
|
|
698
|
-
}
|
|
699
|
-
}
|
|
700
|
-
|
|
701
|
-
/// ---
|
|
702
|
-
/// purpose: 契约席位摘除前的只读预检;在原生席位停机或退役之前,确认逻辑摘除所需输入都能解析
|
|
703
|
-
/// errors: 选不到 team、owner 门不过、spec 缺失或摘除后 spec 校验不过时返回 LifecycleError
|
|
704
|
-
/// ---
|
|
705
|
-
/// Deterministic failures surface while the native seat is still intact.
|
|
706
|
-
pub(crate) fn preflight_contract_removal(
|
|
707
|
-
workspace: &Path,
|
|
708
|
-
agent_id: &AgentId,
|
|
709
|
-
team: Option<&str>,
|
|
710
|
-
) -> Result<(), LifecycleError> {
|
|
711
|
-
let inputs = ContractRemovalInputs::resolve(workspace, agent_id, team)?;
|
|
712
|
-
inputs.rollback(agent_id)?;
|
|
713
|
-
let removed = spec_without_agent(&inputs.spec, agent_id);
|
|
714
|
-
if should_validate_removed_spec(&removed, &inputs.refs()) {
|
|
715
|
-
crate::model::spec::validate_spec(&removed, &inputs.paths.run_workspace)
|
|
716
|
-
.map_err(|e| LifecycleError::Compile(e.to_string()))?;
|
|
717
|
-
}
|
|
718
|
-
Ok(())
|
|
719
|
-
}
|
|
720
|
-
|
|
721
|
-
/// ---
|
|
722
|
-
/// purpose: 契约席位的逻辑摘除;原生 store 的停机与退役已由调用方在同一把生命周期锁下完成
|
|
723
|
-
/// returns: 摘除结果
|
|
724
|
-
/// errors: 选不到 team、owner 门不过或写盘失败时返回 LifecycleError,并按快照回滚
|
|
725
|
-
/// ---
|
|
726
|
-
/// A contract seat has no workspace pane to resolve, so no legacy transport
|
|
727
|
-
/// identity is consulted. Only the shared logical records are removed.
|
|
728
|
-
pub(crate) fn remove_contract_records_locked(
|
|
729
|
-
workspace: &Path,
|
|
730
|
-
agent_id: &AgentId,
|
|
731
|
-
from_spec: bool,
|
|
732
|
-
force: bool,
|
|
733
|
-
stopped: bool,
|
|
734
|
-
team: Option<&str>,
|
|
735
|
-
) -> Result<RemoveAgentOutcome, LifecycleError> {
|
|
736
|
-
let inputs = ContractRemovalInputs::resolve(workspace, agent_id, team)?;
|
|
737
|
-
let rollback = inputs.rollback(agent_id)?;
|
|
738
|
-
let refs = inputs.refs();
|
|
739
|
-
let team = inputs.team.as_deref();
|
|
740
|
-
let result = remove_agent_inner(
|
|
741
|
-
&refs,
|
|
742
|
-
agent_id,
|
|
743
|
-
&inputs.spec,
|
|
744
|
-
inputs.state.clone(),
|
|
745
|
-
None,
|
|
746
|
-
&inputs.team_key,
|
|
747
|
-
false,
|
|
748
|
-
team,
|
|
749
|
-
&inputs.transport,
|
|
750
|
-
);
|
|
751
|
-
match result {
|
|
752
|
-
Ok(success) => {
|
|
753
|
-
write_remove_complete_event(
|
|
754
|
-
refs.run_workspace,
|
|
755
|
-
agent_id,
|
|
756
|
-
from_spec,
|
|
757
|
-
force,
|
|
758
|
-
stopped,
|
|
759
|
-
success.role_file_removed,
|
|
760
|
-
success.cleared_locations,
|
|
761
|
-
)?;
|
|
762
|
-
Ok(success.outcome)
|
|
763
|
-
}
|
|
764
|
-
Err(error) => {
|
|
765
|
-
let restore_errors =
|
|
766
|
-
rollback.restore(refs.run_workspace, refs.spec_workspace, team, &inputs.transport);
|
|
767
|
-
let rollback_ok = restore_errors.is_empty();
|
|
768
|
-
let _ = write_remove_rollback_events(RemoveRollbackEvent {
|
|
769
|
-
agent_id,
|
|
770
|
-
workspace: refs.run_workspace,
|
|
771
|
-
from_spec,
|
|
772
|
-
force,
|
|
773
|
-
stopped,
|
|
774
|
-
error: &error,
|
|
775
|
-
rollback_ok,
|
|
776
|
-
restore_errors: &restore_errors,
|
|
777
|
-
});
|
|
778
|
-
Err(LifecycleError::StatePersist(format!(
|
|
779
|
-
"remove-agent failed for {agent_id}: {error}; rollback_ok={rollback_ok}"
|
|
780
|
-
)))
|
|
781
|
-
}
|
|
782
|
-
}
|
|
783
|
-
}
|
|
784
|
-
|
|
785
|
-
fn remove_agent_inner(
|
|
786
|
-
paths: &LifecyclePathRefs<'_>,
|
|
787
|
-
agent_id: &AgentId,
|
|
788
|
-
spec: &YamlValue,
|
|
789
|
-
state: serde_json::Value,
|
|
790
|
-
physical: Option<crate::transport::PaneInfo>,
|
|
791
|
-
team_key: &str,
|
|
792
|
-
force: bool,
|
|
793
|
-
_team: Option<&str>,
|
|
794
|
-
transport: &dyn crate::transport::Transport,
|
|
795
|
-
) -> Result<RemoveSuccess, LifecycleError> {
|
|
796
|
-
// golden agents.py:75-79: when force-stopping a running worker, RE-RESOLVE the team-scoped state
|
|
797
|
-
// after the stop (stop_agent persisted it); otherwise the originally-resolved projection drives the
|
|
798
|
-
// removal. Either way we operate on the PROJECTION, never a raw load_runtime_state.
|
|
799
|
-
let working_state = state;
|
|
800
|
-
let recorded_role_file = recorded_dynamic_role_file(&working_state, agent_id);
|
|
801
|
-
let mut stopped = false;
|
|
802
|
-
let mut cleared_locations = Vec::new();
|
|
803
|
-
if force {
|
|
804
|
-
if let Some(pane) = physical {
|
|
805
|
-
transport.kill_pane(&pane.pane_id).map_err(|error| {
|
|
806
|
-
LifecycleError::Transport(format!(
|
|
807
|
-
"failed to stop exact seat pane {} for {agent_id}: {error}",
|
|
808
|
-
pane.pane_id.as_str()
|
|
809
|
-
))
|
|
810
|
-
})?;
|
|
811
|
-
stopped = true;
|
|
812
|
-
let target = pane.pane_id.as_str().to_string();
|
|
813
|
-
write_remove_step_event(paths.run_workspace, agent_id, "stop", &target, Some(true))?;
|
|
814
|
-
}
|
|
815
|
-
}
|
|
816
|
-
// golden agents.py:81-83: removed_state = deepcopy(state); pop the agent; save_team_scoped_state
|
|
817
|
-
// (team projection) — NOT a raw save, so other teams in a multi-team workspace are preserved.
|
|
818
|
-
let mut removed_state = working_state;
|
|
819
|
-
remove_agent_from_state(&mut removed_state, agent_id)?;
|
|
820
|
-
mark_agent_retired_in_state(&mut removed_state, agent_id)?;
|
|
821
|
-
crate::state::repository::StateRepository::new(paths.run_workspace)
|
|
822
|
-
.save(
|
|
823
|
-
crate::state::repository::StateWriteIntent::RemoveAgent {
|
|
824
|
-
team_key,
|
|
825
|
-
agent_id: agent_id.as_str(),
|
|
826
|
-
},
|
|
827
|
-
&removed_state,
|
|
828
|
-
)
|
|
829
|
-
.map_err(|e| LifecycleError::StatePersist(e.to_string()))?;
|
|
830
|
-
cleared_locations.push(serde_json::json!("state.json:agents"));
|
|
831
|
-
write_remove_step_event(
|
|
832
|
-
paths.run_workspace,
|
|
833
|
-
agent_id,
|
|
834
|
-
"workspace_state",
|
|
835
|
-
"state.json:agents",
|
|
836
|
-
None,
|
|
837
|
-
)?;
|
|
838
|
-
|
|
839
|
-
let removed_spec = spec_without_agent(spec, agent_id);
|
|
840
|
-
if should_validate_removed_spec(&removed_spec, paths) {
|
|
841
|
-
crate::model::spec::validate_spec(&removed_spec, paths.run_workspace)
|
|
842
|
-
.map_err(|e| LifecycleError::Compile(e.to_string()))?;
|
|
843
|
-
}
|
|
844
|
-
// golden agents.py:96-100,157: state_file = the team_state.md path written from removed_spec/state.
|
|
845
|
-
let team_state_path = write_team_state(paths.spec_workspace, &removed_spec, &removed_state)?;
|
|
846
|
-
cleared_locations.push(serde_json::json!(team_state_path
|
|
847
|
-
.to_string_lossy()
|
|
848
|
-
.to_string()));
|
|
849
|
-
write_remove_step_event(
|
|
850
|
-
paths.run_workspace,
|
|
851
|
-
agent_id,
|
|
852
|
-
"team_state",
|
|
853
|
-
&team_state_path.to_string_lossy(),
|
|
854
|
-
None,
|
|
855
|
-
)?;
|
|
856
|
-
std::fs::write(
|
|
857
|
-
paths.spec_workspace.join("team.spec.yaml"),
|
|
858
|
-
yaml::dumps(&removed_spec),
|
|
859
|
-
)
|
|
860
|
-
.map_err(|e| LifecycleError::StatePersist(format!("write spec: {e}")))?;
|
|
861
|
-
cleared_locations.push(serde_json::json!("team.spec.yaml"));
|
|
862
|
-
write_remove_step_event(
|
|
863
|
-
paths.run_workspace,
|
|
864
|
-
agent_id,
|
|
865
|
-
"spec",
|
|
866
|
-
"team.spec.yaml",
|
|
867
|
-
None,
|
|
868
|
-
)?;
|
|
869
|
-
// Managed copies under `.team/dynamic-role-files/` are framework residue
|
|
870
|
-
// and must not block the next same-id clone. External --role-file paths
|
|
871
|
-
// stay user-owned. Classify by the path's directory (do not follow a
|
|
872
|
-
// last-component symlink); unlink with remove_file so only the link dies.
|
|
873
|
-
let role_file_removed = clear_managed_role_residue(
|
|
874
|
-
paths.run_workspace,
|
|
875
|
-
agent_id,
|
|
876
|
-
recorded_role_file.as_deref(),
|
|
877
|
-
&mut cleared_locations,
|
|
878
|
-
)?;
|
|
879
|
-
let agent_health_deleted = delete_agent_health(paths.run_workspace, team_key, agent_id)?;
|
|
880
|
-
cleared_locations.push(serde_json::json!("agent_health"));
|
|
881
|
-
write_remove_step_event(
|
|
882
|
-
paths.run_workspace,
|
|
883
|
-
agent_id,
|
|
884
|
-
"agent_health",
|
|
885
|
-
"agent_health",
|
|
886
|
-
None,
|
|
887
|
-
)?;
|
|
888
|
-
maybe_fail_remove_after_agent_health_delete()?;
|
|
889
|
-
Ok(RemoveSuccess {
|
|
890
|
-
outcome: RemoveAgentOutcome::Removed {
|
|
891
|
-
agent_id: agent_id.clone(),
|
|
892
|
-
state_file: team_state_path,
|
|
893
|
-
agent_health_deleted: agent_health_deleted || role_file_removed,
|
|
894
|
-
},
|
|
895
|
-
removed_state,
|
|
896
|
-
stopped,
|
|
897
|
-
role_file_removed,
|
|
898
|
-
cleared_locations,
|
|
899
|
-
})
|
|
900
|
-
}
|
|
901
|
-
|
|
902
|
-
fn should_validate_removed_spec(spec: &YamlValue, paths: &LifecyclePathRefs<'_>) -> bool {
|
|
903
|
-
let agents_empty = spec
|
|
904
|
-
.get("agents")
|
|
905
|
-
.and_then(YamlValue::as_list)
|
|
906
|
-
.is_none_or(|agents| agents.is_empty());
|
|
907
|
-
!(agents_empty && paths.spec_workspace != paths.run_workspace)
|
|
908
|
-
}
|
|
909
|
-
|
|
910
|
-
struct RemoveSuccess {
|
|
911
|
-
outcome: RemoveAgentOutcome,
|
|
912
|
-
removed_state: serde_json::Value,
|
|
913
|
-
stopped: bool,
|
|
914
|
-
role_file_removed: bool,
|
|
915
|
-
cleared_locations: Vec<serde_json::Value>,
|
|
916
|
-
}
|
|
917
|
-
|
|
918
|
-
fn recorded_dynamic_role_file(
|
|
919
|
-
state: &serde_json::Value,
|
|
920
|
-
agent_id: &AgentId,
|
|
921
|
-
) -> Option<std::path::PathBuf> {
|
|
922
|
-
state
|
|
923
|
-
.get("agents")
|
|
924
|
-
.and_then(|v| v.get(agent_id.as_str()))
|
|
925
|
-
.and_then(|v| v.get("dynamic_role_file"))
|
|
926
|
-
.and_then(|v| v.as_str())
|
|
927
|
-
.map(str::trim)
|
|
928
|
-
.filter(|s| !s.is_empty())
|
|
929
|
-
.map(std::path::PathBuf::from)
|
|
930
|
-
}
|
|
931
|
-
|
|
932
|
-
fn resolve_role_path(workspace: &Path, role_file: &Path) -> std::path::PathBuf {
|
|
933
|
-
if role_file.is_absolute() {
|
|
934
|
-
role_file.to_path_buf()
|
|
935
|
-
} else {
|
|
936
|
-
workspace.join(role_file)
|
|
937
|
-
}
|
|
938
|
-
}
|
|
939
|
-
|
|
940
|
-
fn default_managed_role_file(workspace: &Path, agent_id: &AgentId) -> std::path::PathBuf {
|
|
941
|
-
workspace
|
|
942
|
-
.join(".team")
|
|
943
|
-
.join("dynamic-role-files")
|
|
944
|
-
.join(format!("{}.md", agent_id.as_str()))
|
|
945
|
-
}
|
|
946
|
-
|
|
947
|
-
/// Same prefix rule as `role_source_ownership`, but canonicalize the parent
|
|
948
|
-
/// only. Following the last component would classify a managed symlink whose
|
|
949
|
-
/// target lives outside the managed dir as external, and leave residue.
|
|
950
|
-
fn role_path_is_managed(workspace: &Path, role_file: &Path) -> bool {
|
|
951
|
-
let managed_root = workspace.join(".team").join("dynamic-role-files");
|
|
952
|
-
let Ok(root) = std::fs::canonicalize(&managed_root) else {
|
|
953
|
-
return false;
|
|
954
|
-
};
|
|
955
|
-
let abs = resolve_role_path(workspace, role_file);
|
|
956
|
-
let Some(parent) = abs.parent() else {
|
|
957
|
-
return false;
|
|
958
|
-
};
|
|
959
|
-
match std::fs::canonicalize(parent) {
|
|
960
|
-
Ok(parent_canon) => parent_canon.starts_with(&root),
|
|
961
|
-
Err(_) => false,
|
|
962
|
-
}
|
|
963
|
-
}
|
|
964
|
-
|
|
965
|
-
fn unlink_role_path(path: &Path) -> Result<bool, LifecycleError> {
|
|
966
|
-
match std::fs::symlink_metadata(path) {
|
|
967
|
-
Err(err) if err.kind() == std::io::ErrorKind::NotFound => Ok(false),
|
|
968
|
-
Err(err) => Err(LifecycleError::StatePersist(format!(
|
|
969
|
-
"inspect managed role file {}: {err}",
|
|
970
|
-
path.display()
|
|
971
|
-
))),
|
|
972
|
-
Ok(_) => std::fs::remove_file(path).map(|_| true).map_err(|err| {
|
|
973
|
-
LifecycleError::StatePersist(format!(
|
|
974
|
-
"remove managed role file {}: {err}",
|
|
975
|
-
path.display()
|
|
976
|
-
))
|
|
977
|
-
}),
|
|
978
|
-
}
|
|
979
|
-
}
|
|
980
|
-
|
|
981
|
-
fn clear_managed_role_residue(
|
|
982
|
-
workspace: &Path,
|
|
983
|
-
agent_id: &AgentId,
|
|
984
|
-
recorded: Option<&Path>,
|
|
985
|
-
cleared_locations: &mut Vec<serde_json::Value>,
|
|
986
|
-
) -> Result<bool, LifecycleError> {
|
|
987
|
-
let target = match recorded {
|
|
988
|
-
Some(recorded) => {
|
|
989
|
-
let abs = resolve_role_path(workspace, recorded);
|
|
990
|
-
if !role_path_is_managed(workspace, &abs) {
|
|
991
|
-
return Ok(false);
|
|
992
|
-
}
|
|
993
|
-
abs
|
|
994
|
-
}
|
|
995
|
-
None => default_managed_role_file(workspace, agent_id),
|
|
996
|
-
};
|
|
997
|
-
if !unlink_role_path(&target)? {
|
|
998
|
-
return Ok(false);
|
|
999
|
-
}
|
|
1000
|
-
let resource = target.to_string_lossy().into_owned();
|
|
1001
|
-
write_remove_step_event(workspace, agent_id, "role_file", &resource, None)?;
|
|
1002
|
-
cleared_locations.push(serde_json::json!(resource));
|
|
1003
|
-
Ok(true)
|
|
1004
|
-
}
|
|
1005
|
-
|
|
1006
|
-
fn write_remove_step_event(
|
|
1007
|
-
workspace: &Path,
|
|
1008
|
-
agent_id: &AgentId,
|
|
1009
|
-
step: &str,
|
|
1010
|
-
resource: &str,
|
|
1011
|
-
stopped: Option<bool>,
|
|
1012
|
-
) -> Result<(), LifecycleError> {
|
|
1013
|
-
let mut payload = serde_json::Map::new();
|
|
1014
|
-
payload.insert("agent_id".to_string(), serde_json::json!(agent_id.as_str()));
|
|
1015
|
-
payload.insert("step".to_string(), serde_json::json!(step));
|
|
1016
|
-
payload.insert("resource".to_string(), serde_json::json!(resource));
|
|
1017
|
-
if let Some(stopped) = stopped {
|
|
1018
|
-
payload.insert("stopped".to_string(), serde_json::json!(stopped));
|
|
1019
|
-
}
|
|
1020
|
-
crate::event_log::EventLog::new(workspace)
|
|
1021
|
-
.write(
|
|
1022
|
-
crate::lifecycle::types::event_names::REMOVE_STEP_COMPLETED,
|
|
1023
|
-
serde_json::Value::Object(payload),
|
|
1024
|
-
)
|
|
1025
|
-
.map_err(|e| LifecycleError::StatePersist(e.to_string()))?;
|
|
1026
|
-
Ok(())
|
|
1027
|
-
}
|
|
1028
|
-
|
|
1029
|
-
fn write_remove_complete_event(
|
|
1030
|
-
workspace: &Path,
|
|
1031
|
-
agent_id: &AgentId,
|
|
1032
|
-
from_spec: bool,
|
|
1033
|
-
force: bool,
|
|
1034
|
-
stopped: bool,
|
|
1035
|
-
role_file_removed: bool,
|
|
1036
|
-
cleared_locations: Vec<serde_json::Value>,
|
|
1037
|
-
) -> Result<(), LifecycleError> {
|
|
1038
|
-
crate::event_log::EventLog::new(workspace)
|
|
1039
|
-
.write(
|
|
1040
|
-
"remove_agent.complete",
|
|
1041
|
-
serde_json::json!({
|
|
1042
|
-
"agent_id": agent_id.as_str(),
|
|
1043
|
-
"from_spec": from_spec,
|
|
1044
|
-
"force": force,
|
|
1045
|
-
"stopped": stopped,
|
|
1046
|
-
"role_file_removed": role_file_removed,
|
|
1047
|
-
"cleared_locations": cleared_locations,
|
|
1048
|
-
}),
|
|
1049
|
-
)
|
|
1050
|
-
.map_err(|e| LifecycleError::StatePersist(e.to_string()))?;
|
|
1051
|
-
Ok(())
|
|
1052
|
-
}
|
|
1053
|
-
|
|
1054
|
-
struct RemoveRollbackEvent<'a> {
|
|
1055
|
-
workspace: &'a Path,
|
|
1056
|
-
agent_id: &'a AgentId,
|
|
1057
|
-
from_spec: bool,
|
|
1058
|
-
force: bool,
|
|
1059
|
-
stopped: bool,
|
|
1060
|
-
error: &'a LifecycleError,
|
|
1061
|
-
rollback_ok: bool,
|
|
1062
|
-
restore_errors: &'a [String],
|
|
1063
|
-
}
|
|
1064
|
-
|
|
1065
|
-
fn write_remove_rollback_events(event: RemoveRollbackEvent<'_>) -> Result<(), LifecycleError> {
|
|
1066
|
-
let log = crate::event_log::EventLog::new(event.workspace);
|
|
1067
|
-
let errors = event
|
|
1068
|
-
.restore_errors
|
|
1069
|
-
.iter()
|
|
1070
|
-
.map(|e| serde_json::json!(e))
|
|
1071
|
-
.collect::<Vec<_>>();
|
|
1072
|
-
log.write(
|
|
1073
|
-
"remove_agent.rollback",
|
|
1074
|
-
serde_json::json!({
|
|
1075
|
-
"agent_id": event.agent_id.as_str(),
|
|
1076
|
-
"from_spec": event.from_spec,
|
|
1077
|
-
"force": event.force,
|
|
1078
|
-
"stopped": event.stopped,
|
|
1079
|
-
"error": event.error.to_string(),
|
|
1080
|
-
"rollback_ok": event.rollback_ok,
|
|
1081
|
-
"errors": errors,
|
|
1082
|
-
}),
|
|
1083
|
-
)
|
|
1084
|
-
.map_err(|e| LifecycleError::StatePersist(e.to_string()))?;
|
|
1085
|
-
log.write(
|
|
1086
|
-
crate::lifecycle::types::event_names::REMOVE_ROLLED_BACK,
|
|
1087
|
-
serde_json::json!({
|
|
1088
|
-
"agent_id": event.agent_id.as_str(),
|
|
1089
|
-
"step": "rollback",
|
|
1090
|
-
"resource": "workspace",
|
|
1091
|
-
"rollback_ok": event.rollback_ok,
|
|
1092
|
-
"errors": errors,
|
|
1093
|
-
}),
|
|
1094
|
-
)
|
|
1095
|
-
.map_err(|e| LifecycleError::StatePersist(e.to_string()))?;
|
|
1096
|
-
if !event.restore_errors.is_empty() {
|
|
1097
|
-
log.write(
|
|
1098
|
-
"remove_agent.rollback_failed",
|
|
1099
|
-
serde_json::json!({
|
|
1100
|
-
"agent_id": event.agent_id.as_str(),
|
|
1101
|
-
"errors": errors,
|
|
1102
|
-
}),
|
|
1103
|
-
)
|
|
1104
|
-
.map_err(|e| LifecycleError::StatePersist(e.to_string()))?;
|
|
1105
|
-
}
|
|
1106
|
-
Ok(())
|
|
1107
|
-
}
|
|
1108
|
-
|
|
1109
|
-
fn remove_agent_from_state(
|
|
1110
|
-
state: &mut serde_json::Value,
|
|
1111
|
-
agent_id: &AgentId,
|
|
1112
|
-
) -> Result<(), LifecycleError> {
|
|
1113
|
-
if let Some(agents) = state.get_mut("agents").and_then(|v| v.as_object_mut()) {
|
|
1114
|
-
agents.remove(agent_id.as_str());
|
|
1115
|
-
Ok(())
|
|
1116
|
-
} else {
|
|
1117
|
-
Err(LifecycleError::StatePersist(
|
|
1118
|
-
"runtime state agents is not an object".to_string(),
|
|
1119
|
-
))
|
|
1120
|
-
}
|
|
1121
|
-
}
|
|
1122
|
-
|
|
1123
|
-
/// ---
|
|
1124
|
-
/// purpose: 在 state 里给该席位打上退役标记
|
|
1125
|
-
/// params:
|
|
1126
|
-
/// state: 就地写 agent_lifecycle 下该席位的状态、时间与原因
|
|
1127
|
-
/// returns: 已是退役态时幂等返回
|
|
1128
|
-
/// errors: state 根、agent_lifecycle 或该条目不是对象时返回 StatePersist
|
|
1129
|
-
/// ---
|
|
1130
|
-
pub(crate) fn mark_agent_retired_in_state(
|
|
1131
|
-
state: &mut serde_json::Value,
|
|
1132
|
-
agent_id: &AgentId,
|
|
1133
|
-
) -> Result<(), LifecycleError> {
|
|
1134
|
-
let root = state.as_object_mut().ok_or_else(|| {
|
|
1135
|
-
LifecycleError::StatePersist("runtime state root is not an object".to_string())
|
|
1136
|
-
})?;
|
|
1137
|
-
let lifecycle = root
|
|
1138
|
-
.entry("agent_lifecycle".to_string())
|
|
1139
|
-
.or_insert_with(|| serde_json::json!({}));
|
|
1140
|
-
let lifecycle = lifecycle.as_object_mut().ok_or_else(|| {
|
|
1141
|
-
LifecycleError::StatePersist("runtime state agent_lifecycle is not an object".to_string())
|
|
1142
|
-
})?;
|
|
1143
|
-
let entry = lifecycle
|
|
1144
|
-
.entry(agent_id.as_str().to_string())
|
|
1145
|
-
.or_insert_with(|| serde_json::json!({}));
|
|
1146
|
-
if entry.get("state").and_then(serde_json::Value::as_str) == Some("retired") {
|
|
1147
|
-
return Ok(());
|
|
1148
|
-
}
|
|
1149
|
-
let entry = entry.as_object_mut().ok_or_else(|| {
|
|
1150
|
-
LifecycleError::StatePersist(format!(
|
|
1151
|
-
"runtime state agent_lifecycle.{} is not an object",
|
|
1152
|
-
agent_id.as_str()
|
|
1153
|
-
))
|
|
1154
|
-
})?;
|
|
1155
|
-
entry.insert("state".to_string(), serde_json::json!("retired"));
|
|
1156
|
-
entry.insert(
|
|
1157
|
-
"changed_at".to_string(),
|
|
1158
|
-
serde_json::json!(chrono::Utc::now().to_rfc3339()),
|
|
1159
|
-
);
|
|
1160
|
-
entry.insert("reason".to_string(), serde_json::json!("remove-agent"));
|
|
1161
|
-
Ok(())
|
|
1162
|
-
}
|
|
1163
|
-
|
|
1164
|
-
/// ---
|
|
1165
|
-
/// purpose: 清掉该席位的退役标记
|
|
1166
|
-
/// params:
|
|
1167
|
-
/// state: 就地删除;只有当前确为退役态才删
|
|
1168
|
-
/// ---
|
|
1169
|
-
pub(crate) fn clear_agent_retirement_in_state(state: &mut serde_json::Value, agent_id: &AgentId) {
|
|
1170
|
-
let Some(lifecycle) = state
|
|
1171
|
-
.get_mut("agent_lifecycle")
|
|
1172
|
-
.and_then(serde_json::Value::as_object_mut)
|
|
1173
|
-
else {
|
|
1174
|
-
return;
|
|
1175
|
-
};
|
|
1176
|
-
let is_retired = lifecycle
|
|
1177
|
-
.get(agent_id.as_str())
|
|
1178
|
-
.and_then(|entry| entry.get("state"))
|
|
1179
|
-
.and_then(serde_json::Value::as_str)
|
|
1180
|
-
== Some("retired");
|
|
1181
|
-
if is_retired {
|
|
1182
|
-
lifecycle.remove(agent_id.as_str());
|
|
1183
|
-
}
|
|
1184
|
-
}
|
|
1185
|
-
|
|
1186
|
-
/// ---
|
|
1187
|
-
/// purpose: 生成摘掉该席位后的 spec
|
|
1188
|
-
/// returns: 去掉该 agent、去掉它的启动项、并清掉指向它的路由引用后的 spec;spec 不是 map 时原样返回
|
|
1189
|
-
/// ---
|
|
1190
|
-
/// Build the persisted spec after removing one worker. Besides deleting the worker and startup entry,
|
|
1191
|
-
/// prune routing references that would otherwise point at the removed worker.
|
|
1192
|
-
pub(crate) fn spec_without_agent(spec: &YamlValue, agent_id: &AgentId) -> YamlValue {
|
|
1193
|
-
let YamlValue::Map(pairs) = spec else {
|
|
1194
|
-
return spec.clone();
|
|
1195
|
-
};
|
|
1196
|
-
let mut out = Vec::new();
|
|
1197
|
-
for (key, value) in pairs {
|
|
1198
|
-
if key == "agents" {
|
|
1199
|
-
let agents = value
|
|
1200
|
-
.as_list()
|
|
1201
|
-
.map(|items| {
|
|
1202
|
-
items
|
|
1203
|
-
.iter()
|
|
1204
|
-
.filter(|agent| {
|
|
1205
|
-
agent
|
|
1206
|
-
.get("id")
|
|
1207
|
-
.and_then(YamlValue::as_str)
|
|
1208
|
-
.map(|id| id != agent_id.as_str())
|
|
1209
|
-
.unwrap_or(true)
|
|
1210
|
-
})
|
|
1211
|
-
.cloned()
|
|
1212
|
-
.collect::<Vec<_>>()
|
|
1213
|
-
})
|
|
1214
|
-
.unwrap_or_default();
|
|
1215
|
-
out.push((key.clone(), YamlValue::List(agents)));
|
|
1216
|
-
} else if key == "runtime" {
|
|
1217
|
-
out.push((key.clone(), runtime_without_startup_agent(value, agent_id)));
|
|
1218
|
-
} else if key == "routing" {
|
|
1219
|
-
out.push((key.clone(), routing_without_agent(value, agent_id)));
|
|
1220
|
-
} else if key == "tasks" {
|
|
1221
|
-
out.push((key.clone(), tasks_without_agent_assignee(value, agent_id)));
|
|
1222
|
-
} else {
|
|
1223
|
-
out.push((key.clone(), value.clone()));
|
|
1224
|
-
}
|
|
1225
|
-
}
|
|
1226
|
-
YamlValue::Map(out)
|
|
1227
|
-
}
|
|
1228
|
-
|
|
1229
|
-
fn runtime_without_startup_agent(runtime: &YamlValue, agent_id: &AgentId) -> YamlValue {
|
|
1230
|
-
let YamlValue::Map(pairs) = runtime else {
|
|
1231
|
-
return runtime.clone();
|
|
1232
|
-
};
|
|
1233
|
-
let mut out = Vec::new();
|
|
1234
|
-
for (key, value) in pairs {
|
|
1235
|
-
if key == "startup_order" {
|
|
1236
|
-
let order = value
|
|
1237
|
-
.as_list()
|
|
1238
|
-
.map(|items| {
|
|
1239
|
-
items
|
|
1240
|
-
.iter()
|
|
1241
|
-
.filter(|item| {
|
|
1242
|
-
item.as_str()
|
|
1243
|
-
.map(|id| id != agent_id.as_str())
|
|
1244
|
-
.unwrap_or(true)
|
|
1245
|
-
})
|
|
1246
|
-
.cloned()
|
|
1247
|
-
.collect::<Vec<_>>()
|
|
1248
|
-
})
|
|
1249
|
-
.unwrap_or_default();
|
|
1250
|
-
out.push((key.clone(), YamlValue::List(order)));
|
|
1251
|
-
} else {
|
|
1252
|
-
out.push((key.clone(), value.clone()));
|
|
1253
|
-
}
|
|
1254
|
-
}
|
|
1255
|
-
YamlValue::Map(out)
|
|
1256
|
-
}
|
|
1257
|
-
|
|
1258
|
-
fn routing_without_agent(routing: &YamlValue, agent_id: &AgentId) -> YamlValue {
|
|
1259
|
-
let YamlValue::Map(pairs) = routing else {
|
|
1260
|
-
return routing.clone();
|
|
1261
|
-
};
|
|
1262
|
-
let mut out = Vec::new();
|
|
1263
|
-
for (key, value) in pairs {
|
|
1264
|
-
if key == "default_assignee" && value.as_str().is_some_and(|id| id == agent_id.as_str()) {
|
|
1265
|
-
out.push((key.clone(), YamlValue::Str(String::new())));
|
|
1266
|
-
} else if key == "rules" {
|
|
1267
|
-
let rules = value
|
|
1268
|
-
.as_list()
|
|
1269
|
-
.map(|items| {
|
|
1270
|
-
items
|
|
1271
|
-
.iter()
|
|
1272
|
-
.filter(|rule| {
|
|
1273
|
-
rule.get("assign_to")
|
|
1274
|
-
.and_then(YamlValue::as_str)
|
|
1275
|
-
.map(|id| id != agent_id.as_str())
|
|
1276
|
-
.unwrap_or(true)
|
|
1277
|
-
})
|
|
1278
|
-
.cloned()
|
|
1279
|
-
.collect::<Vec<_>>()
|
|
1280
|
-
})
|
|
1281
|
-
.unwrap_or_default();
|
|
1282
|
-
out.push((key.clone(), YamlValue::List(rules)));
|
|
1283
|
-
} else {
|
|
1284
|
-
out.push((key.clone(), value.clone()));
|
|
1285
|
-
}
|
|
1286
|
-
}
|
|
1287
|
-
YamlValue::Map(out)
|
|
1288
|
-
}
|
|
1289
|
-
|
|
1290
|
-
fn tasks_without_agent_assignee(tasks: &YamlValue, agent_id: &AgentId) -> YamlValue {
|
|
1291
|
-
let YamlValue::List(items) = tasks else {
|
|
1292
|
-
return tasks.clone();
|
|
1293
|
-
};
|
|
1294
|
-
YamlValue::List(
|
|
1295
|
-
items
|
|
1296
|
-
.iter()
|
|
1297
|
-
.map(|task| task_without_agent_assignee(task, agent_id))
|
|
1298
|
-
.collect(),
|
|
1299
|
-
)
|
|
1300
|
-
}
|
|
1301
|
-
|
|
1302
|
-
fn task_without_agent_assignee(task: &YamlValue, agent_id: &AgentId) -> YamlValue {
|
|
1303
|
-
let YamlValue::Map(pairs) = task else {
|
|
1304
|
-
return task.clone();
|
|
1305
|
-
};
|
|
1306
|
-
YamlValue::Map(
|
|
1307
|
-
pairs
|
|
1308
|
-
.iter()
|
|
1309
|
-
.map(|(key, value)| {
|
|
1310
|
-
if key == "assignee" && value.as_str().is_some_and(|id| id == agent_id.as_str()) {
|
|
1311
|
-
(key.clone(), YamlValue::Str(String::new()))
|
|
1312
|
-
} else {
|
|
1313
|
-
(key.clone(), value.clone())
|
|
1314
|
-
}
|
|
1315
|
-
})
|
|
1316
|
-
.collect(),
|
|
1317
|
-
)
|
|
1318
|
-
}
|
|
1319
|
-
|
|
1320
|
-
fn delete_agent_health(
|
|
1321
|
-
workspace: &Path,
|
|
1322
|
-
owner_team_id: &str,
|
|
1323
|
-
agent_id: &AgentId,
|
|
1324
|
-
) -> Result<bool, LifecycleError> {
|
|
1325
|
-
let store = crate::message_store::MessageStore::open(workspace)
|
|
1326
|
-
.map_err(|e| LifecycleError::StatePersist(e.to_string()))?;
|
|
1327
|
-
let conn = crate::db::schema::open_db(store.db_path())
|
|
1328
|
-
.map_err(|e| LifecycleError::StatePersist(e.to_string()))?;
|
|
1329
|
-
let changed = conn
|
|
1330
|
-
.execute(
|
|
1331
|
-
"delete from agent_health where owner_team_id = ?1 and agent_id = ?2",
|
|
1332
|
-
rusqlite::params![owner_team_id, agent_id.as_str()],
|
|
1333
|
-
)
|
|
1334
|
-
.map_err(|e| LifecycleError::StatePersist(e.to_string()))?;
|
|
1335
|
-
Ok(changed > 0)
|
|
1336
|
-
}
|
|
1337
|
-
|
|
1338
|
-
// Phase-DX E2: `select_agent_health` / `restore_agent_health` / `CapturedHealth` moved to
|
|
1339
|
-
// `db::agent_health_capture` so the SQL column references (agent_health backup columns)
|
|
1340
|
-
// live in the persistence layer (whitelisted by the E2 grep guard) rather than lifecycle
|
|
1341
|
-
// policy code. The wrappers below preserve the existing `LifecycleError` surface.
|
|
1342
|
-
use crate::db::agent_health_capture::{
|
|
1343
|
-
restore_agent_health as capture_restore_agent_health,
|
|
1344
|
-
select_agent_health as capture_select_agent_health, CapturedHealth,
|
|
1345
|
-
};
|
|
1346
|
-
|
|
1347
|
-
fn select_agent_health(
|
|
1348
|
-
workspace: &Path,
|
|
1349
|
-
owner_team_id: &str,
|
|
1350
|
-
agent_id: &AgentId,
|
|
1351
|
-
) -> Result<Option<CapturedHealth>, LifecycleError> {
|
|
1352
|
-
capture_select_agent_health(workspace, owner_team_id, agent_id)
|
|
1353
|
-
.map_err(|e| LifecycleError::StatePersist(e.to_string()))
|
|
1354
|
-
}
|
|
1355
|
-
|
|
1356
|
-
fn restore_agent_health(
|
|
1357
|
-
workspace: &Path,
|
|
1358
|
-
owner_team_id: &str,
|
|
1359
|
-
agent_id: &AgentId,
|
|
1360
|
-
row: &Option<CapturedHealth>,
|
|
1361
|
-
) -> Result<(), LifecycleError> {
|
|
1362
|
-
capture_restore_agent_health(workspace, owner_team_id, agent_id, row)
|
|
1363
|
-
.map_err(|e| LifecycleError::StatePersist(e.to_string()))
|
|
1364
|
-
}
|
|
1365
|
-
|
|
1366
|
-
fn maybe_fail_remove_after_agent_health_delete() -> Result<(), LifecycleError> {
|
|
1367
|
-
let Ok(reason) = std::env::var("TEAM_AGENT_TEST_FAIL_REMOVE_AFTER_AGENT_HEALTH_DELETE") else {
|
|
1368
|
-
return Ok(());
|
|
1369
|
-
};
|
|
1370
|
-
if reason.is_empty() {
|
|
1371
|
-
return Ok(());
|
|
1372
|
-
}
|
|
1373
|
-
Err(LifecycleError::StatePersist(format!(
|
|
1374
|
-
"injected remove failure after agent_health delete: {reason}"
|
|
1375
|
-
)))
|
|
1376
|
-
}
|
|
1377
|
-
|
|
1378
|
-
struct RemoveRollback {
|
|
1379
|
-
agent_id: AgentId,
|
|
1380
|
-
team_key: String,
|
|
1381
|
-
spec_text: Option<String>,
|
|
1382
|
-
state: serde_json::Value,
|
|
1383
|
-
team_state_text: Option<String>,
|
|
1384
|
-
team_state_path: std::path::PathBuf,
|
|
1385
|
-
/// golden agents.py:185: the agent_health row captured BEFORE delete, re-upserted on rollback.
|
|
1386
|
-
health: Option<CapturedHealth>,
|
|
1387
|
-
restore_running: bool,
|
|
1388
|
-
}
|
|
1389
|
-
|
|
1390
|
-
impl RemoveRollback {
|
|
1391
|
-
fn capture(
|
|
1392
|
-
workspace: &Path,
|
|
1393
|
-
spec_workspace: &Path,
|
|
1394
|
-
spec: &YamlValue,
|
|
1395
|
-
state: &serde_json::Value,
|
|
1396
|
-
team_key: &str,
|
|
1397
|
-
agent_id: &AgentId,
|
|
1398
|
-
) -> Result<Self, LifecycleError> {
|
|
1399
|
-
let spec_path = spec_workspace.join("team.spec.yaml");
|
|
1400
|
-
let spec_text = match std::fs::read_to_string(&spec_path) {
|
|
1401
|
-
Ok(text) => Some(text),
|
|
1402
|
-
Err(e) if e.kind() == std::io::ErrorKind::NotFound => None,
|
|
1403
|
-
Err(e) => return Err(LifecycleError::StatePersist(format!("read spec: {e}"))),
|
|
1404
|
-
};
|
|
1405
|
-
let team_state_path = spec_workspace.join(
|
|
1406
|
-
spec.get("context")
|
|
1407
|
-
.and_then(|v| v.get("state_file"))
|
|
1408
|
-
.and_then(YamlValue::as_str)
|
|
1409
|
-
.unwrap_or("team_state.md"),
|
|
1410
|
-
);
|
|
1411
|
-
let team_state_text = match std::fs::read_to_string(&team_state_path) {
|
|
1412
|
-
Ok(text) => Some(text),
|
|
1413
|
-
Err(e) if e.kind() == std::io::ErrorKind::NotFound => None,
|
|
1414
|
-
Err(e) => {
|
|
1415
|
-
return Err(LifecycleError::StatePersist(format!(
|
|
1416
|
-
"read team_state: {e}"
|
|
1417
|
-
)))
|
|
1418
|
-
}
|
|
1419
|
-
};
|
|
1420
|
-
let health = select_agent_health(workspace, team_key, agent_id)?;
|
|
1421
|
-
Ok(Self {
|
|
1422
|
-
agent_id: agent_id.clone(),
|
|
1423
|
-
team_key: team_key.to_string(),
|
|
1424
|
-
spec_text,
|
|
1425
|
-
state: state.clone(),
|
|
1426
|
-
team_state_text,
|
|
1427
|
-
team_state_path,
|
|
1428
|
-
health,
|
|
1429
|
-
restore_running: false,
|
|
1430
|
-
})
|
|
1431
|
-
}
|
|
1432
|
-
|
|
1433
|
-
/// golden agents.py:189-227 `_RemoveRollback.restore`: BEST-EFFORT — wrap EACH artifact restore
|
|
1434
|
-
/// (spec → workspace_state → team_state → agent_health) in its own try/except, append
|
|
1435
|
-
/// per-artifact failures to `errors`, and NEVER short-circuit on the first failure. The worker is
|
|
1436
|
-
/// only re-started when restore_running AND no errors. Returns the collected error strings (empty
|
|
1437
|
-
/// == ok); the caller re-raises the ORIGINAL operation error annotated with rollback_ok.
|
|
1438
|
-
fn restore(
|
|
1439
|
-
&self,
|
|
1440
|
-
workspace: &Path,
|
|
1441
|
-
spec_workspace: &Path,
|
|
1442
|
-
team: Option<&str>,
|
|
1443
|
-
transport: &dyn crate::transport::Transport,
|
|
1444
|
-
) -> Vec<String> {
|
|
1445
|
-
let mut errors: Vec<String> = Vec::new();
|
|
1446
|
-
|
|
1447
|
-
// spec
|
|
1448
|
-
let spec_path = spec_workspace.join("team.spec.yaml");
|
|
1449
|
-
if let Some(text) = &self.spec_text {
|
|
1450
|
-
if let Err(e) = std::fs::write(&spec_path, text) {
|
|
1451
|
-
errors.push(format!("spec:{e}"));
|
|
1452
|
-
}
|
|
1453
|
-
}
|
|
1454
|
-
// workspace_state
|
|
1455
|
-
if let Err(e) = crate::state::repository::StateRepository::new(workspace).save(
|
|
1456
|
-
crate::state::repository::StateWriteIntent::ForceRecreateRollback {
|
|
1457
|
-
team_key: &self.team_key,
|
|
1458
|
-
agent_id: self.agent_id.as_str(),
|
|
1459
|
-
},
|
|
1460
|
-
&self.state,
|
|
1461
|
-
) {
|
|
1462
|
-
errors.push(format!("workspace_state:{e}"));
|
|
1463
|
-
}
|
|
1464
|
-
// team_state
|
|
1465
|
-
let team_state_result = match &self.team_state_text {
|
|
1466
|
-
Some(text) => {
|
|
1467
|
-
if let Some(parent) = self.team_state_path.parent() {
|
|
1468
|
-
let _ = std::fs::create_dir_all(parent);
|
|
1469
|
-
}
|
|
1470
|
-
std::fs::write(&self.team_state_path, text)
|
|
1471
|
-
}
|
|
1472
|
-
None => match std::fs::remove_file(&self.team_state_path) {
|
|
1473
|
-
Ok(()) => Ok(()),
|
|
1474
|
-
Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(()),
|
|
1475
|
-
Err(e) => Err(e),
|
|
1476
|
-
},
|
|
1477
|
-
};
|
|
1478
|
-
if let Err(e) = team_state_result {
|
|
1479
|
-
errors.push(format!("team_state:{e}"));
|
|
1480
|
-
}
|
|
1481
|
-
if self.restore_running && errors.is_empty() {
|
|
1482
|
-
if let Err(e) = start_agent_at_paths(
|
|
1483
|
-
workspace,
|
|
1484
|
-
spec_workspace,
|
|
1485
|
-
&self.agent_id,
|
|
1486
|
-
true,
|
|
1487
|
-
false,
|
|
1488
|
-
true,
|
|
1489
|
-
team,
|
|
1490
|
-
transport,
|
|
1491
|
-
) {
|
|
1492
|
-
errors.push(format!("worker_restore:{e}"));
|
|
1493
|
-
}
|
|
1494
|
-
}
|
|
1495
|
-
// Starting a replacement cohort intentionally clears stale health, so
|
|
1496
|
-
// rollback must restore the captured row after the old seat is back.
|
|
1497
|
-
if let Err(e) =
|
|
1498
|
-
restore_agent_health(workspace, &self.team_key, &self.agent_id, &self.health)
|
|
1499
|
-
{
|
|
1500
|
-
errors.push(format!("agent_health:{e}"));
|
|
1501
|
-
}
|
|
1502
|
-
errors
|
|
1503
|
-
}
|
|
1504
|
-
}
|