@team-agent/installer 0.5.128 → 0.6.1
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,2199 +0,0 @@
|
|
|
1
|
-
//! ---
|
|
2
|
-
//! purpose: 单席位的起、停与重置,全部在 workspace 级生命周期锁下串行
|
|
3
|
-
//! contract:
|
|
4
|
-
//! provides:
|
|
5
|
-
//! - name: start_agent
|
|
6
|
-
//! what: 起或复活一席,按四象限定出 resume 或 fresh
|
|
7
|
-
//! - name: stop_agent
|
|
8
|
-
//! what: 停一席,按 pane、同角色残留 pane、窗口三级收敛并取排空证据
|
|
9
|
-
//! - name: reset_agent
|
|
10
|
-
//! what: 必须显式丢弃会话,停席后换血再起
|
|
11
|
-
//! - name: start_agent_at_paths
|
|
12
|
-
//! what: 起席的实体实现,owner 门、暂停判定、启动模式决策与落 state
|
|
13
|
-
//! - name: drain_old_pane_and_pid
|
|
14
|
-
//! what: 有界轮询等旧 pane 与旧进程消失,产出可核的证据载荷
|
|
15
|
-
//! depends:
|
|
16
|
-
//! - crate::lifecycle::lock
|
|
17
|
-
//! - crate::lifecycle::restart::selection
|
|
18
|
-
//! - crate::lifecycle::restart::remove
|
|
19
|
-
//! - crate::lifecycle::restart::team_state
|
|
20
|
-
//! - crate::lifecycle::launch
|
|
21
|
-
//! - crate::state::projection
|
|
22
|
-
//! - crate::state::persist
|
|
23
|
-
//! - crate::platform::process
|
|
24
|
-
//! - crate::lifecycle::profile_launch
|
|
25
|
-
//! - crate::lifecycle::worker_command_context
|
|
26
|
-
//! - crate::event_log::EventLog
|
|
27
|
-
//! boundary:
|
|
28
|
-
//! - 每个对外入口先取 workspace 级生命周期锁,本文件不做无锁写
|
|
29
|
-
//! - owner 门先于未知席位报错,权限判定不被存在性判定绕过
|
|
30
|
-
//! - reset 未显式丢弃会话时直接拒绝,不擅自换血
|
|
31
|
-
//! - 排空是有界等待,等不到只如实记录,不无限阻塞
|
|
32
|
-
//! maturity: wired
|
|
33
|
-
//! ---
|
|
34
|
-
use super::common::*;
|
|
35
|
-
use super::selection::decide_start_mode;
|
|
36
|
-
use super::team_state::write_team_state;
|
|
37
|
-
use super::*;
|
|
38
|
-
use crate::lifecycle::lock::{acquire_agent_lifecycle_lock, LifecycleLockRequest};
|
|
39
|
-
|
|
40
|
-
/// CLI start is orthogonal to stop: refuse any live target before editing its role.
|
|
41
|
-
/// Transport uncertainty is a refusal, not permission to spawn a duplicate.
|
|
42
|
-
pub(crate) fn ensure_agent_not_running(
|
|
43
|
-
state: &serde_json::Value,
|
|
44
|
-
agent_id: &AgentId,
|
|
45
|
-
transport: &dyn crate::transport::Transport,
|
|
46
|
-
) -> Result<(), LifecycleError> {
|
|
47
|
-
let agent = state.get("agents").and_then(|agents| agents.get(agent_id.as_str()))
|
|
48
|
-
.ok_or_else(|| LifecycleError::RequirementUnmet(format!("agent {agent_id} not found")))?;
|
|
49
|
-
let session = state_session_name(state);
|
|
50
|
-
let window = agent_window(agent, agent_id);
|
|
51
|
-
let pane = agent.get("pane_id").and_then(serde_json::Value::as_str);
|
|
52
|
-
let targets = transport.list_targets()
|
|
53
|
-
.map_err(|error| LifecycleError::Transport(error.to_string()))?;
|
|
54
|
-
let live = targets.iter().find(|target| {
|
|
55
|
-
target.session == session
|
|
56
|
-
&& target.window_name.as_ref().is_some_and(|name| name.as_str() == window)
|
|
57
|
-
&& (pane == Some(target.pane_id.as_str())
|
|
58
|
-
|| !crate::lifecycle::launch::state_uses_adaptive_layout(state))
|
|
59
|
-
&& agent_pane_live_by_id(transport, &target.pane_id)
|
|
60
|
-
});
|
|
61
|
-
if let Some(target) = live {
|
|
62
|
-
Err(LifecycleError::RequirementUnmet(format!(
|
|
63
|
-
"agent {agent_id} is already running (duplicate / active cohort); use stop-agent first; cohort proof: session={} window={window} pane={}",
|
|
64
|
-
session.as_str(), target.pane_id.as_str(),
|
|
65
|
-
)))
|
|
66
|
-
} else {
|
|
67
|
-
Ok(())
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
/// ---
|
|
72
|
-
/// purpose: 起或复活一席的对外入口
|
|
73
|
-
/// params:
|
|
74
|
-
/// force: 窗口已存在时也重起
|
|
75
|
-
/// open_display: 是否开显示
|
|
76
|
-
/// allow_fresh: resume 依据缺失时允许改跑全新会话
|
|
77
|
-
/// returns: Running、Noop 或 Paused
|
|
78
|
-
/// errors: 选不到 team 返回 TeamSelect,owner 门不过返回 OwnerRefused,席位不存在返回 RequirementUnmet
|
|
79
|
-
/// contract_id: lifecycle.start_agent.entry
|
|
80
|
-
/// ---
|
|
81
|
-
/// `start_agent(workspace, agent_id, force, open_display, allow_fresh, team)`
|
|
82
|
-
/// (`lifecycle/start.py:72`)。`_runtime_lock("start-agent")` 下串行:resume-or-fresh
|
|
83
|
-
/// 决策、resume 窗口退出回退 fresh、起后投递 pending message、起 coordinator。
|
|
84
|
-
/// bug-085:`(session_id, rollout_path)` 四象限穷尽 match,缺 rollout 的 codex 仅在
|
|
85
|
-
/// allow_fresh 时回退 fresh。
|
|
86
|
-
pub fn start_agent(
|
|
87
|
-
workspace: &Path,
|
|
88
|
-
agent_id: &AgentId,
|
|
89
|
-
force: bool,
|
|
90
|
-
open_display: bool,
|
|
91
|
-
allow_fresh: bool,
|
|
92
|
-
team: Option<&str>,
|
|
93
|
-
) -> Result<StartAgentOutcome, LifecycleError> {
|
|
94
|
-
let paths = lifecycle_paths(workspace, team)?;
|
|
95
|
-
let canonical_team = paths.canonical_team(team).map(str::to_string);
|
|
96
|
-
let team = canonical_team.as_deref();
|
|
97
|
-
let _lock = acquire_agent_lifecycle_lock(LifecycleLockRequest {
|
|
98
|
-
workspace: &paths.run_workspace,
|
|
99
|
-
operation: "start-agent",
|
|
100
|
-
team,
|
|
101
|
-
agent_id: Some(agent_id),
|
|
102
|
-
})?;
|
|
103
|
-
let transport = paths.tmux_backend()?;
|
|
104
|
-
start_agent_at_paths(
|
|
105
|
-
&paths.run_workspace,
|
|
106
|
-
&paths.spec_workspace,
|
|
107
|
-
agent_id,
|
|
108
|
-
force,
|
|
109
|
-
open_display,
|
|
110
|
-
allow_fresh,
|
|
111
|
-
team,
|
|
112
|
-
&transport,
|
|
113
|
-
)
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
/// ---
|
|
117
|
-
/// purpose: 带注入 transport 的起席入口;未指定 team 且解析失败时退回把入参 workspace 当作两个路径
|
|
118
|
-
/// returns: Running、Noop 或 Paused
|
|
119
|
-
/// errors: 指定了 team 而解析失败时透传该错误,其余同 start_agent
|
|
120
|
-
/// contract_id: lifecycle.start_agent.entry
|
|
121
|
-
/// ---
|
|
122
|
-
/// `start_agent` with an injected transport — wires the single-worker resume/fresh spawn +
|
|
123
|
-
/// start_coordinator (rt-host-a sweep: was a stub returning RequirementUnmet at the spawn boundary).
|
|
124
|
-
#[allow(clippy::too_many_arguments)]
|
|
125
|
-
pub(crate) fn start_agent_with_transport(
|
|
126
|
-
workspace: &Path,
|
|
127
|
-
agent_id: &AgentId,
|
|
128
|
-
force: bool,
|
|
129
|
-
open_display: bool,
|
|
130
|
-
allow_fresh: bool,
|
|
131
|
-
team: Option<&str>,
|
|
132
|
-
transport: &dyn crate::transport::Transport,
|
|
133
|
-
) -> Result<StartAgentOutcome, LifecycleError> {
|
|
134
|
-
let paths = match lifecycle_paths(workspace, team) {
|
|
135
|
-
Ok(paths) => paths,
|
|
136
|
-
Err(_) if team.is_none() => LifecyclePaths {
|
|
137
|
-
run_workspace: workspace.to_path_buf(),
|
|
138
|
-
spec_workspace: workspace.to_path_buf(),
|
|
139
|
-
selected: None,
|
|
140
|
-
},
|
|
141
|
-
Err(error) => return Err(error),
|
|
142
|
-
};
|
|
143
|
-
let canonical_team = paths.canonical_team(team).map(str::to_string);
|
|
144
|
-
let team = canonical_team.as_deref();
|
|
145
|
-
let _lock = acquire_agent_lifecycle_lock(LifecycleLockRequest {
|
|
146
|
-
workspace: &paths.run_workspace,
|
|
147
|
-
operation: "start-agent",
|
|
148
|
-
team,
|
|
149
|
-
agent_id: Some(agent_id),
|
|
150
|
-
})?;
|
|
151
|
-
start_agent_at_paths(
|
|
152
|
-
&paths.run_workspace,
|
|
153
|
-
&paths.spec_workspace,
|
|
154
|
-
agent_id,
|
|
155
|
-
force,
|
|
156
|
-
open_display,
|
|
157
|
-
allow_fresh,
|
|
158
|
-
team,
|
|
159
|
-
transport,
|
|
160
|
-
)
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
/// ---
|
|
164
|
-
/// purpose: 起席的实体实现,读团队作用域 state、过 owner 门、定启动模式、spawn 并标记已起
|
|
165
|
-
/// params:
|
|
166
|
-
/// spec_workspace: spec 所在目录,用于补回席位的命令上下文
|
|
167
|
-
/// open_display: 当前实现未直接使用,仅参与自适应布局判定
|
|
168
|
-
/// returns: 席位暂停时直接返回 Paused,其余按启动模式返回 Running 或 Noop
|
|
169
|
-
/// errors: 读 state 失败返回 StatePersist,owner 门不过返回 OwnerRefused,席位不存在返回 RequirementUnmet
|
|
170
|
-
/// ---
|
|
171
|
-
#[allow(clippy::too_many_arguments)]
|
|
172
|
-
pub(crate) fn start_agent_at_paths(
|
|
173
|
-
workspace: &Path,
|
|
174
|
-
spec_workspace: &Path,
|
|
175
|
-
agent_id: &AgentId,
|
|
176
|
-
force: bool,
|
|
177
|
-
open_display: bool,
|
|
178
|
-
allow_fresh: bool,
|
|
179
|
-
team: Option<&str>,
|
|
180
|
-
transport: &dyn crate::transport::Transport,
|
|
181
|
-
) -> Result<StartAgentOutcome, LifecycleError> {
|
|
182
|
-
let _ = open_display;
|
|
183
|
-
let mut state = if team.is_some() {
|
|
184
|
-
resolve_team_scoped_state_or_refuse(workspace, team)?
|
|
185
|
-
} else {
|
|
186
|
-
crate::state::persist::load_runtime_state(workspace)
|
|
187
|
-
.map_err(|e| LifecycleError::StatePersist(e.to_string()))?
|
|
188
|
-
};
|
|
189
|
-
crate::lifecycle::launch::ensure_owner_allowed_for_state(&state, Some(agent_id))?;
|
|
190
|
-
let raw_agent = state
|
|
191
|
-
.get("agents")
|
|
192
|
-
.and_then(|v| v.get(agent_id.as_str()))
|
|
193
|
-
.ok_or_else(|| LifecycleError::RequirementUnmet(format!("agent {agent_id} not found")))?
|
|
194
|
-
.clone();
|
|
195
|
-
let agent = rehydrate_agent_command_context_from_spec(spec_workspace, agent_id, &raw_agent);
|
|
196
|
-
if raw_agent
|
|
197
|
-
.get("paused")
|
|
198
|
-
.and_then(serde_json::Value::as_bool)
|
|
199
|
-
.unwrap_or(false)
|
|
200
|
-
{
|
|
201
|
-
return Ok(StartAgentOutcome::Paused {
|
|
202
|
-
agent_id: agent_id.clone(),
|
|
203
|
-
});
|
|
204
|
-
}
|
|
205
|
-
let session_name = state_session_name(&state);
|
|
206
|
-
let window = agent_window(&agent, agent_id);
|
|
207
|
-
let adaptive_layout =
|
|
208
|
-
open_display && crate::lifecycle::launch::state_uses_adaptive_layout(&state);
|
|
209
|
-
if force && is_per_agent_window(&window, agent_id) {
|
|
210
|
-
let expected_pane_id = raw_agent
|
|
211
|
-
.get("pane_id")
|
|
212
|
-
.and_then(serde_json::Value::as_str)
|
|
213
|
-
.filter(|value| !value.is_empty());
|
|
214
|
-
let target =
|
|
215
|
-
SameRoleCohortTarget::new(agent_id, &window).with_expected_pane_id(expected_pane_id);
|
|
216
|
-
if let Some(error) =
|
|
217
|
-
same_role_cohort_pre_spawn_error(transport, &session_name, "start-agent", &[target])
|
|
218
|
-
{
|
|
219
|
-
return Err(LifecycleError::RequirementUnmet(error));
|
|
220
|
-
}
|
|
221
|
-
}
|
|
222
|
-
// A persisted window name is only a topology hint. Once a pane binding is
|
|
223
|
-
// recorded, the pane must be both physically live and owned by the
|
|
224
|
-
// intended session/window; a foreign pane must never make start-agent
|
|
225
|
-
// return Noop. A coordinator-provided pane_dead reason is also
|
|
226
|
-
// authoritative when a backend cannot answer the exact-pane probe.
|
|
227
|
-
let pane_marked_dead = raw_agent
|
|
228
|
-
.get("stale_reason")
|
|
229
|
-
.and_then(serde_json::Value::as_str)
|
|
230
|
-
.is_some_and(|reason| matches!(reason, "pane_dead" | "both"));
|
|
231
|
-
// A stale cached pane may still be recoverable without a spawn when the
|
|
232
|
-
// intended per-agent window has exactly one live pane. The pane found by
|
|
233
|
-
// this session/window lookup, not the cached id, is the binding that the
|
|
234
|
-
// Noop path refreshes. A foreign pane with no intended window still falls
|
|
235
|
-
// through to a real spawn.
|
|
236
|
-
let noop_pane = if adaptive_layout {
|
|
237
|
-
None
|
|
238
|
-
} else {
|
|
239
|
-
single_live_pane_for_window(transport, &session_name, &window)
|
|
240
|
-
};
|
|
241
|
-
let agent_live = if pane_marked_dead {
|
|
242
|
-
false
|
|
243
|
-
} else if adaptive_layout
|
|
244
|
-
|| raw_agent
|
|
245
|
-
.get("pane_id")
|
|
246
|
-
.and_then(serde_json::Value::as_str)
|
|
247
|
-
.is_some_and(|pane| !pane.is_empty())
|
|
248
|
-
{
|
|
249
|
-
agent_pane_owned_and_live(transport, &raw_agent, &session_name, &window)
|
|
250
|
-
|| noop_pane.is_some()
|
|
251
|
-
} else {
|
|
252
|
-
window_exists(transport, &session_name, &window)
|
|
253
|
-
};
|
|
254
|
-
// 0.3.28 Step 9: E51 self-heal converted to topology assertion. After
|
|
255
|
-
// Step 2 the leader lives in its own session (`team-agent-leader-*`),
|
|
256
|
-
// so `agent.pane_id == leader_receiver.pane_id` is STRUCTURALLY
|
|
257
|
-
// impossible. We keep the check as a runtime guard: if it ever fires,
|
|
258
|
-
// emit a topology_invariant_violation event AND still force a fresh
|
|
259
|
-
// spawn (defensive). The check itself remains a no-op on healthy
|
|
260
|
-
// state — assert_topology_invariants from Step 1 catches the
|
|
261
|
-
// upstream corruption.
|
|
262
|
-
let noop_pane_conflict = noop_pane.as_ref().is_some_and(|pane| {
|
|
263
|
-
pane_conflicts_with_pane_id(&state, agent_id, &raw_agent, pane.pane_id.as_str())
|
|
264
|
-
});
|
|
265
|
-
let has_collision = pane_conflicts_with_leader_or_other(&state, agent_id, &raw_agent)
|
|
266
|
-
|| noop_pane_conflict;
|
|
267
|
-
if has_collision {
|
|
268
|
-
eprintln!(
|
|
269
|
-
"team_agent::layout pane_collision agent_id=`{agent_id}` \
|
|
270
|
-
action=forcing_fresh_spawn \
|
|
271
|
-
(the persisted or discovered pane is owned by another live seat)"
|
|
272
|
-
);
|
|
273
|
-
}
|
|
274
|
-
let agent_live = agent_live && !has_collision;
|
|
275
|
-
// 0.5.66 bypass 单源 §2.6 — dangerously_skip_permissions drift 挡板:
|
|
276
|
-
// Noop 路径不 spawn,若声明面(角色 md / spec)与运行面(as-launched)不一致,
|
|
277
|
-
// 拒 Noop 报明,防"改了 md 却以为生效、进程仍带旧 bypass 跑着"的暗门。
|
|
278
|
-
let file_bypass = agent
|
|
279
|
-
.get("dangerously_skip_permissions")
|
|
280
|
-
.and_then(serde_json::Value::as_bool);
|
|
281
|
-
let launched_bypass = raw_agent
|
|
282
|
-
.get("as_launched_dangerously_skip_permissions")
|
|
283
|
-
.and_then(serde_json::Value::as_bool);
|
|
284
|
-
if !force && agent_live && check_bypass_drift(file_bypass, launched_bypass) {
|
|
285
|
-
// 0.5.66 bypass 单源 §2.7:Noop drift 拒也写审计事件。
|
|
286
|
-
let _ = crate::event_log::EventLog::new(workspace).write(
|
|
287
|
-
"worker.spawn_dangerously_skip_permissions_drift_denied",
|
|
288
|
-
serde_json::json!({
|
|
289
|
-
"agent_id": agent_id.as_str(),
|
|
290
|
-
"file_value": file_bypass,
|
|
291
|
-
"launched_value": launched_bypass,
|
|
292
|
-
"action": "noop_denied_require_remove_add_or_force",
|
|
293
|
-
}),
|
|
294
|
-
);
|
|
295
|
-
return Err(LifecycleError::RequirementUnmet(format!(
|
|
296
|
-
"dangerously_skip_permissions drift for agent {agent_id}: file={file_bypass:?} but running process launched with {launched_bypass:?}. \
|
|
297
|
-
Run `remove-agent {agent_id}` then `add-agent {agent_id}` to apply, or `restart --force {agent_id}` to accept fresh spawn."
|
|
298
|
-
)));
|
|
299
|
-
}
|
|
300
|
-
if !force && agent_live {
|
|
301
|
-
let old_binding = pane_binding_snapshot(&raw_agent);
|
|
302
|
-
let refreshed_binding = noop_pane.as_ref().map(pane_binding_from_live);
|
|
303
|
-
mark_agent_running_noop(
|
|
304
|
-
&mut state,
|
|
305
|
-
agent_id,
|
|
306
|
-
&session_name,
|
|
307
|
-
&window,
|
|
308
|
-
noop_pane.as_ref(),
|
|
309
|
-
)?;
|
|
310
|
-
state["agents"][agent_id.as_str()]["explicit_stop"] = serde_json::Value::Bool(false);
|
|
311
|
-
let team_key = restart_projection_team_key(&state, team);
|
|
312
|
-
save_restart_projected_state(workspace, &mut state, &team_key, &[agent_id.as_str()])?;
|
|
313
|
-
if let Ok(spec) = load_team_spec(spec_workspace) {
|
|
314
|
-
write_team_state(spec_workspace, &spec, &state)?;
|
|
315
|
-
}
|
|
316
|
-
replay_worker_target_missing_messages(workspace, agent_id, &team_key, &state, transport)?;
|
|
317
|
-
let coordinator = start_coordinator_for_workspace(workspace, Some(&team_key))?;
|
|
318
|
-
let coordinator_started = coordinator.ok;
|
|
319
|
-
let target = format!("{}:{window}", session_name.as_str());
|
|
320
|
-
if let Some(new_binding) = refreshed_binding.as_ref() {
|
|
321
|
-
if old_binding.as_ref() != Some(new_binding) {
|
|
322
|
-
write_agent_pane_binding_refreshed_event(
|
|
323
|
-
workspace,
|
|
324
|
-
agent_id,
|
|
325
|
-
&session_name,
|
|
326
|
-
&window,
|
|
327
|
-
old_binding.as_ref(),
|
|
328
|
-
new_binding,
|
|
329
|
-
)?;
|
|
330
|
-
}
|
|
331
|
-
}
|
|
332
|
-
write_start_agent_noop_event(workspace, agent_id, &target, coordinator_started)?;
|
|
333
|
-
return Ok(StartAgentOutcome::Noop {
|
|
334
|
-
env: AgentActionEnvelope {
|
|
335
|
-
agent_id: agent_id.clone(),
|
|
336
|
-
state_file: crate::state::persist::runtime_state_path(workspace),
|
|
337
|
-
coordinator_started,
|
|
338
|
-
},
|
|
339
|
-
target,
|
|
340
|
-
});
|
|
341
|
-
}
|
|
342
|
-
let provider = agent_provider(&agent);
|
|
343
|
-
let session_id = agent_session_id(&agent);
|
|
344
|
-
let rollout_path = agent_rollout_path(&agent);
|
|
345
|
-
let resume_backing_exists = session_id
|
|
346
|
-
.as_ref()
|
|
347
|
-
.map(|session| {
|
|
348
|
-
resume_backing_exists_for_agent(
|
|
349
|
-
workspace,
|
|
350
|
-
agent_id,
|
|
351
|
-
&agent,
|
|
352
|
-
provider,
|
|
353
|
-
session,
|
|
354
|
-
rollout_path.as_ref(),
|
|
355
|
-
)
|
|
356
|
-
})
|
|
357
|
-
.unwrap_or(false);
|
|
358
|
-
let start_mode = decide_start_mode(
|
|
359
|
-
provider_wire(provider),
|
|
360
|
-
session_id.as_ref(),
|
|
361
|
-
rollout_path.as_ref(),
|
|
362
|
-
resume_backing_exists,
|
|
363
|
-
allow_fresh,
|
|
364
|
-
);
|
|
365
|
-
if matches!(start_mode, StartMode::Noop) {
|
|
366
|
-
return Err(LifecycleError::RequirementUnmet(format!(
|
|
367
|
-
"resume_not_ready: session backing store missing for agent {agent_id}; rerun with --allow-fresh to start fresh"
|
|
368
|
-
)));
|
|
369
|
-
}
|
|
370
|
-
let spawn_session_id = if matches!(start_mode, StartMode::Resumed) {
|
|
371
|
-
session_id.clone()
|
|
372
|
-
} else {
|
|
373
|
-
None
|
|
374
|
-
};
|
|
375
|
-
let into_existing_session =
|
|
376
|
-
session_live_or_default(transport, &session_name, session_name_present(&state));
|
|
377
|
-
let inherited_generation =
|
|
378
|
-
crate::state::projection::session_generation(&state).map(str::to_string);
|
|
379
|
-
let safety = crate::lifecycle::launch::effective_runtime_config_for_worker_spawn_json(
|
|
380
|
-
&agent,
|
|
381
|
-
provider,
|
|
382
|
-
)?;
|
|
383
|
-
let layout_placement = if adaptive_layout {
|
|
384
|
-
crate::lifecycle::launch::adaptive_existing_placement_for_agent(
|
|
385
|
-
&state,
|
|
386
|
-
transport,
|
|
387
|
-
&session_name,
|
|
388
|
-
agent_id,
|
|
389
|
-
)
|
|
390
|
-
.or_else(|| {
|
|
391
|
-
crate::lifecycle::launch::adaptive_placement_for_agent(
|
|
392
|
-
&state,
|
|
393
|
-
transport,
|
|
394
|
-
&session_name,
|
|
395
|
-
agent_id,
|
|
396
|
-
)
|
|
397
|
-
})
|
|
398
|
-
} else {
|
|
399
|
-
None
|
|
400
|
-
};
|
|
401
|
-
let spawn_window = layout_placement
|
|
402
|
-
.as_ref()
|
|
403
|
-
.map(|placement| placement.layout_window.as_str().to_string())
|
|
404
|
-
.unwrap_or_else(|| window.clone());
|
|
405
|
-
// Issue 2 (Round 3b gate review §6): pass the explicit team_key so the
|
|
406
|
-
// worker MCP env carries it through restart-agent path too. The
|
|
407
|
-
// `restart_projection_team_key` helper consolidates the same resolution
|
|
408
|
-
// used for save_restart_projected_state below.
|
|
409
|
-
let resolved_team_key = restart_projection_team_key(&state, team);
|
|
410
|
-
let spawn = spawn_agent_window(
|
|
411
|
-
workspace,
|
|
412
|
-
&session_name,
|
|
413
|
-
agent_id,
|
|
414
|
-
&agent,
|
|
415
|
-
spawn_session_id.as_ref(),
|
|
416
|
-
into_existing_session,
|
|
417
|
-
transport,
|
|
418
|
-
Some(&safety),
|
|
419
|
-
layout_placement.as_ref(),
|
|
420
|
-
None,
|
|
421
|
-
None,
|
|
422
|
-
Some(resolved_team_key.as_str()),
|
|
423
|
-
)?;
|
|
424
|
-
if let Err(error) = verify_spawned_pane_matches_target(
|
|
425
|
-
transport,
|
|
426
|
-
&spawn.spawn.pane_id,
|
|
427
|
-
&session_name,
|
|
428
|
-
&spawn.spawn.window,
|
|
429
|
-
) {
|
|
430
|
-
return Err(rollback_spawned_pane_keep_error(
|
|
431
|
-
transport,
|
|
432
|
-
&spawn.spawn.pane_id,
|
|
433
|
-
error,
|
|
434
|
-
));
|
|
435
|
-
}
|
|
436
|
-
let actual_spawn_window = spawn.spawn.window.as_str().to_string();
|
|
437
|
-
let spawned = (|| {
|
|
438
|
-
mark_agent_started(
|
|
439
|
-
&mut state,
|
|
440
|
-
agent_id,
|
|
441
|
-
&actual_spawn_window,
|
|
442
|
-
&spawn,
|
|
443
|
-
transport,
|
|
444
|
-
&safety,
|
|
445
|
-
start_mode,
|
|
446
|
-
)?;
|
|
447
|
-
let generation = if into_existing_session {
|
|
448
|
-
inherited_generation.as_deref()
|
|
449
|
-
} else {
|
|
450
|
-
Some(spawn.spawned_at.as_str())
|
|
451
|
-
};
|
|
452
|
-
if let Some(generation) = generation {
|
|
453
|
-
state["generation"] = serde_json::json!(generation);
|
|
454
|
-
if !into_existing_session {
|
|
455
|
-
transport
|
|
456
|
-
.set_session_owner_with_generation(
|
|
457
|
-
&session_name,
|
|
458
|
-
workspace,
|
|
459
|
-
&resolved_team_key,
|
|
460
|
-
generation,
|
|
461
|
-
)
|
|
462
|
-
.map_err(|error| LifecycleError::Transport(error.to_string()))?;
|
|
463
|
-
}
|
|
464
|
-
}
|
|
465
|
-
// 0.5.66 bypass 单源 §2.7:启动即带 bypass → 审计留痕。
|
|
466
|
-
if safety.enabled {
|
|
467
|
-
let state_agent = state
|
|
468
|
-
.get("agents")
|
|
469
|
-
.and_then(|v| v.get(agent_id.as_str()));
|
|
470
|
-
let _ = crate::event_log::EventLog::new(workspace).write(
|
|
471
|
-
"worker.spawn_dangerously_skip_permissions",
|
|
472
|
-
serde_json::json!({
|
|
473
|
-
"agent_id": agent_id.as_str(),
|
|
474
|
-
"role": state_agent
|
|
475
|
-
.and_then(|a| a.get("role"))
|
|
476
|
-
.and_then(serde_json::Value::as_str)
|
|
477
|
-
.unwrap_or_default(),
|
|
478
|
-
"provider": state_agent
|
|
479
|
-
.and_then(|a| a.get("provider"))
|
|
480
|
-
.and_then(serde_json::Value::as_str)
|
|
481
|
-
.unwrap_or_default(),
|
|
482
|
-
"trigger": "start-agent",
|
|
483
|
-
"spec_source_path": crate::lifecycle::launch::agent_id_spec_source_path(
|
|
484
|
-
workspace, agent_id.as_str(),
|
|
485
|
-
),
|
|
486
|
-
}),
|
|
487
|
-
);
|
|
488
|
-
}
|
|
489
|
-
// **0.3.24 add-agent socket drift fix**: keep `state.tmux_endpoint` /
|
|
490
|
-
// `state.tmux_socket` synchronized with the transport actually used for the
|
|
491
|
-
// spawn. Without this, add-agent / fork-agent could spawn to a socket that
|
|
492
|
-
// never gets persisted, and the next coordinator tick would re-resolve to
|
|
493
|
-
// the workspace-hash socket and lose the new pane. Annotation runs inside
|
|
494
|
-
// the same `save_restart_projected_state` window — no parallel "annotate
|
|
495
|
-
// after spawn" race with coordinator and no double source of truth.
|
|
496
|
-
crate::lifecycle::launch::annotate_runtime_tmux_endpoint(&mut state, transport, workspace);
|
|
497
|
-
let team_key = restart_projection_team_key(&state, team);
|
|
498
|
-
// 0.5.32 (`.team/artifacts/restart-resumed-stale-activity-locate.md` §5):
|
|
499
|
-
// clear the matching `agent_health` observation on the new spawn cohort
|
|
500
|
-
// so five-line status summary and status --json do not surface a stale
|
|
501
|
-
// WORKING row from the pre-shutdown process. Best-effort: DB failure
|
|
502
|
-
// must not fail the start-agent path.
|
|
503
|
-
let _ = crate::db::agent_health_capture::clear_agent_health_observation(
|
|
504
|
-
workspace, &team_key, agent_id,
|
|
505
|
-
);
|
|
506
|
-
let skip_capture_backfill = if matches!(
|
|
507
|
-
start_mode,
|
|
508
|
-
StartMode::Fresh | StartMode::FreshAfterMissingRollout
|
|
509
|
-
) {
|
|
510
|
-
vec![agent_id.as_str()]
|
|
511
|
-
} else {
|
|
512
|
-
Vec::new()
|
|
513
|
-
};
|
|
514
|
-
persist_selected_receiver_session_name(&mut state, &session_name);
|
|
515
|
-
save_restart_projected_state_with_capture_backfill_skip(
|
|
516
|
-
workspace,
|
|
517
|
-
&mut state,
|
|
518
|
-
&team_key,
|
|
519
|
-
&skip_capture_backfill,
|
|
520
|
-
&[agent_id.as_str()],
|
|
521
|
-
)?;
|
|
522
|
-
write_start_agent_start_event(
|
|
523
|
-
workspace,
|
|
524
|
-
agent_id,
|
|
525
|
-
&agent,
|
|
526
|
-
&spawn.plan,
|
|
527
|
-
provider,
|
|
528
|
-
start_mode,
|
|
529
|
-
&session_name,
|
|
530
|
-
&actual_spawn_window,
|
|
531
|
-
spawn_session_id.as_ref(),
|
|
532
|
-
tmux_start_mode_for_spawn(&spawn, into_existing_session),
|
|
533
|
-
)?;
|
|
534
|
-
replay_worker_target_missing_messages(workspace, agent_id, &team_key, &state, transport)?;
|
|
535
|
-
let coordinator = start_coordinator_for_workspace(workspace, Some(&team_key))?;
|
|
536
|
-
let coordinator_started = coordinator.ok;
|
|
537
|
-
Ok(StartAgentOutcome::Running {
|
|
538
|
-
env: AgentActionEnvelope {
|
|
539
|
-
agent_id: agent_id.clone(),
|
|
540
|
-
state_file: crate::state::persist::runtime_state_path(workspace),
|
|
541
|
-
coordinator_started,
|
|
542
|
-
},
|
|
543
|
-
start_mode,
|
|
544
|
-
target: spawn.spawn.pane_id.as_str().to_string(),
|
|
545
|
-
session_id,
|
|
546
|
-
new_session_id: spawn.plan.expected_session_id.clone(),
|
|
547
|
-
rollout_path,
|
|
548
|
-
})
|
|
549
|
-
})();
|
|
550
|
-
match spawned {
|
|
551
|
-
Ok(outcome) => Ok(outcome),
|
|
552
|
-
Err(error) => Err(rollback_spawned_pane_keep_error(
|
|
553
|
-
transport,
|
|
554
|
-
&spawn.spawn.pane_id,
|
|
555
|
-
error,
|
|
556
|
-
)),
|
|
557
|
-
}
|
|
558
|
-
}
|
|
559
|
-
|
|
560
|
-
fn rollback_spawned_pane_keep_error(
|
|
561
|
-
transport: &dyn crate::transport::Transport,
|
|
562
|
-
pane: &crate::transport::PaneId,
|
|
563
|
-
error: LifecycleError,
|
|
564
|
-
) -> LifecycleError {
|
|
565
|
-
match transport.kill_pane(pane) {
|
|
566
|
-
Ok(()) => error,
|
|
567
|
-
Err(rollback_error) => LifecycleError::Transport(format!(
|
|
568
|
-
"{error}; failed to roll back spawned pane {}: {rollback_error}",
|
|
569
|
-
pane.as_str()
|
|
570
|
-
)),
|
|
571
|
-
}
|
|
572
|
-
}
|
|
573
|
-
|
|
574
|
-
fn replay_worker_target_missing_messages(
|
|
575
|
-
workspace: &Path,
|
|
576
|
-
agent_id: &AgentId,
|
|
577
|
-
team_key: &str,
|
|
578
|
-
state: &serde_json::Value,
|
|
579
|
-
transport: &dyn crate::transport::Transport,
|
|
580
|
-
) -> Result<(), LifecycleError> {
|
|
581
|
-
let store = crate::message_store::MessageStore::open(workspace)
|
|
582
|
-
.map_err(|e| LifecycleError::StatePersist(e.to_string()))?;
|
|
583
|
-
let event_log = crate::event_log::EventLog::new(workspace);
|
|
584
|
-
let ids = crate::messaging::delivery::requeue_worker_target_missing_messages(
|
|
585
|
-
workspace,
|
|
586
|
-
&store,
|
|
587
|
-
&event_log,
|
|
588
|
-
agent_id.as_str(),
|
|
589
|
-
Some(team_key),
|
|
590
|
-
)
|
|
591
|
-
.map_err(|e| LifecycleError::StatePersist(e.to_string()))?;
|
|
592
|
-
if !ids.is_empty() {
|
|
593
|
-
crate::messaging::delivery::deliver_pending_messages(
|
|
594
|
-
workspace, state, transport, &event_log,
|
|
595
|
-
)
|
|
596
|
-
.map_err(|e| LifecycleError::StatePersist(e.to_string()))?;
|
|
597
|
-
}
|
|
598
|
-
Ok(())
|
|
599
|
-
}
|
|
600
|
-
|
|
601
|
-
pub(super) fn verify_spawned_pane_matches_target(
|
|
602
|
-
transport: &dyn crate::transport::Transport,
|
|
603
|
-
pane: &crate::transport::PaneId,
|
|
604
|
-
session: &crate::transport::SessionName,
|
|
605
|
-
window: &crate::transport::WindowName,
|
|
606
|
-
) -> Result<(), LifecycleError> {
|
|
607
|
-
let targets = transport
|
|
608
|
-
.list_targets()
|
|
609
|
-
.map_err(|e| LifecycleError::Transport(e.to_string()))?;
|
|
610
|
-
let observed = targets.iter().find(|target| target.pane_id == *pane);
|
|
611
|
-
let Some(observed) = observed else {
|
|
612
|
-
return Err(LifecycleError::RequirementUnmet(format!(
|
|
613
|
-
"start refused: spawned pane not addressable on transport socket (possible socket drift); window disappeared or spawned pane not owned by requested agent window; requested={}:{} pane={} observed=<missing>",
|
|
614
|
-
session.as_str(),
|
|
615
|
-
window.as_str(),
|
|
616
|
-
pane.as_str()
|
|
617
|
-
)));
|
|
618
|
-
};
|
|
619
|
-
let observed_window = observed
|
|
620
|
-
.window_name
|
|
621
|
-
.as_ref()
|
|
622
|
-
.map(crate::transport::WindowName::as_str)
|
|
623
|
-
.unwrap_or("<unknown>");
|
|
624
|
-
if observed.session.as_str() != session.as_str() || observed_window != window.as_str() {
|
|
625
|
-
return Err(LifecycleError::RequirementUnmet(format!(
|
|
626
|
-
"start refused: spawned pane not owned by requested agent window; requested={}:{} pane={} observed={}:{}",
|
|
627
|
-
session.as_str(),
|
|
628
|
-
window.as_str(),
|
|
629
|
-
pane.as_str(),
|
|
630
|
-
observed.session.as_str(),
|
|
631
|
-
observed_window
|
|
632
|
-
)));
|
|
633
|
-
}
|
|
634
|
-
if matches!(
|
|
635
|
-
transport.liveness(pane),
|
|
636
|
-
Ok(crate::model::enums::PaneLiveness::Dead)
|
|
637
|
-
) {
|
|
638
|
-
return Err(LifecycleError::RequirementUnmet(format!(
|
|
639
|
-
"start refused: spawned pane not addressable on transport socket; pane is dead after spawn; requested={}:{} pane={}",
|
|
640
|
-
session.as_str(),
|
|
641
|
-
window.as_str(),
|
|
642
|
-
pane.as_str()
|
|
643
|
-
)));
|
|
644
|
-
}
|
|
645
|
-
Ok(())
|
|
646
|
-
}
|
|
647
|
-
|
|
648
|
-
#[derive(Debug, Clone, PartialEq, Eq)]
|
|
649
|
-
struct PaneBindingSnapshot {
|
|
650
|
-
pane_id: String,
|
|
651
|
-
pane_pid: Option<u32>,
|
|
652
|
-
}
|
|
653
|
-
|
|
654
|
-
fn pane_binding_snapshot(agent: &serde_json::Value) -> Option<PaneBindingSnapshot> {
|
|
655
|
-
let pane_id = agent
|
|
656
|
-
.get("pane_id")
|
|
657
|
-
.and_then(serde_json::Value::as_str)
|
|
658
|
-
.filter(|pane| !pane.is_empty())?;
|
|
659
|
-
Some(PaneBindingSnapshot {
|
|
660
|
-
pane_id: pane_id.to_string(),
|
|
661
|
-
pane_pid: agent
|
|
662
|
-
.get("pane_pid")
|
|
663
|
-
.and_then(serde_json::Value::as_u64)
|
|
664
|
-
.map(|pid| pid as u32),
|
|
665
|
-
})
|
|
666
|
-
}
|
|
667
|
-
|
|
668
|
-
fn pane_binding_from_live(pane: &crate::transport::PaneInfo) -> PaneBindingSnapshot {
|
|
669
|
-
PaneBindingSnapshot {
|
|
670
|
-
pane_id: pane.pane_id.as_str().to_string(),
|
|
671
|
-
pane_pid: pane.pane_pid,
|
|
672
|
-
}
|
|
673
|
-
}
|
|
674
|
-
|
|
675
|
-
fn single_live_pane_for_window(
|
|
676
|
-
transport: &dyn crate::transport::Transport,
|
|
677
|
-
session: &crate::transport::SessionName,
|
|
678
|
-
window: &str,
|
|
679
|
-
) -> Option<crate::transport::PaneInfo> {
|
|
680
|
-
let targets = transport.list_targets().ok()?;
|
|
681
|
-
let mut matches = targets.into_iter().filter(|target| {
|
|
682
|
-
target.session.as_str() == session.as_str()
|
|
683
|
-
&& target
|
|
684
|
-
.window_name
|
|
685
|
-
.as_ref()
|
|
686
|
-
.is_some_and(|name| name.as_str() == window)
|
|
687
|
-
});
|
|
688
|
-
let pane = matches.next()?;
|
|
689
|
-
if matches.next().is_some() {
|
|
690
|
-
return None;
|
|
691
|
-
}
|
|
692
|
-
Some(pane)
|
|
693
|
-
}
|
|
694
|
-
|
|
695
|
-
fn write_agent_pane_binding_refreshed_event(
|
|
696
|
-
workspace: &Path,
|
|
697
|
-
agent_id: &AgentId,
|
|
698
|
-
session: &crate::transport::SessionName,
|
|
699
|
-
window: &str,
|
|
700
|
-
old: Option<&PaneBindingSnapshot>,
|
|
701
|
-
new: &PaneBindingSnapshot,
|
|
702
|
-
) -> Result<(), LifecycleError> {
|
|
703
|
-
crate::event_log::EventLog::new(workspace)
|
|
704
|
-
.write(
|
|
705
|
-
"agent_pane_binding_refreshed",
|
|
706
|
-
serde_json::json!({
|
|
707
|
-
"agent_id": agent_id.as_str(),
|
|
708
|
-
"session": session.as_str(),
|
|
709
|
-
"window": window,
|
|
710
|
-
"old_pane_id": old.map(|binding| binding.pane_id.as_str()),
|
|
711
|
-
"old_pane_pid": old.and_then(|binding| binding.pane_pid),
|
|
712
|
-
"pane_id": new.pane_id.as_str(),
|
|
713
|
-
"pane_pid": new.pane_pid,
|
|
714
|
-
}),
|
|
715
|
-
)
|
|
716
|
-
.map_err(|e| LifecycleError::StatePersist(e.to_string()))?;
|
|
717
|
-
Ok(())
|
|
718
|
-
}
|
|
719
|
-
|
|
720
|
-
/// E51 (0.3.26 P0, restart self-heal): returns `true` when the agent's pane_id
|
|
721
|
-
/// is the same as the leader_receiver/team_owner pane_id OR is owned by a
|
|
722
|
-
/// different agent in the state. In both cases `start_agent` must NOT treat the
|
|
723
|
-
/// pane as "this agent's live pane" (it should spawn fresh).
|
|
724
|
-
fn pane_conflicts_with_leader_or_other(
|
|
725
|
-
state: &serde_json::Value,
|
|
726
|
-
agent_id: &crate::model::ids::AgentId,
|
|
727
|
-
agent: &serde_json::Value,
|
|
728
|
-
) -> bool {
|
|
729
|
-
let Some(pane_id) = agent
|
|
730
|
-
.get("pane_id")
|
|
731
|
-
.and_then(serde_json::Value::as_str)
|
|
732
|
-
.filter(|s| !s.is_empty())
|
|
733
|
-
else {
|
|
734
|
-
return false;
|
|
735
|
-
};
|
|
736
|
-
pane_conflicts_with_pane_id(state, agent_id, agent, pane_id)
|
|
737
|
-
}
|
|
738
|
-
|
|
739
|
-
fn pane_conflicts_with_pane_id(
|
|
740
|
-
state: &serde_json::Value,
|
|
741
|
-
agent_id: &crate::model::ids::AgentId,
|
|
742
|
-
agent: &serde_json::Value,
|
|
743
|
-
pane_id: &str,
|
|
744
|
-
) -> bool {
|
|
745
|
-
let state_socket = runtime_tmux_socket(state);
|
|
746
|
-
let agent_socket = tmux_socket_field(agent).or(state_socket);
|
|
747
|
-
// Check leader anchor.
|
|
748
|
-
for key in ["leader_receiver", "team_owner"] {
|
|
749
|
-
if state
|
|
750
|
-
.get(key)
|
|
751
|
-
.and_then(pane_socket_binding)
|
|
752
|
-
.is_some_and(|leader| pane_conflicts_on_same_socket(pane_id, agent_socket, leader))
|
|
753
|
-
{
|
|
754
|
-
return true;
|
|
755
|
-
}
|
|
756
|
-
}
|
|
757
|
-
// Check other agents.
|
|
758
|
-
if let Some(agents) = state.get("agents").and_then(serde_json::Value::as_object) {
|
|
759
|
-
for (id, other) in agents {
|
|
760
|
-
if id == agent_id.as_str()
|
|
761
|
-
|| matches!(
|
|
762
|
-
other.get("status").and_then(serde_json::Value::as_str),
|
|
763
|
-
Some("stopped" | "paused" | "failed" | "removed")
|
|
764
|
-
)
|
|
765
|
-
{
|
|
766
|
-
continue;
|
|
767
|
-
}
|
|
768
|
-
let other_socket = tmux_socket_field(other).or(state_socket);
|
|
769
|
-
if other
|
|
770
|
-
.get("pane_id")
|
|
771
|
-
.and_then(serde_json::Value::as_str)
|
|
772
|
-
.is_some_and(|op| {
|
|
773
|
-
op == pane_id && !tmux_sockets_known_different(agent_socket, other_socket)
|
|
774
|
-
})
|
|
775
|
-
{
|
|
776
|
-
return true;
|
|
777
|
-
}
|
|
778
|
-
}
|
|
779
|
-
}
|
|
780
|
-
false
|
|
781
|
-
}
|
|
782
|
-
|
|
783
|
-
#[derive(Clone, Copy)]
|
|
784
|
-
struct PaneSocketBinding<'a> {
|
|
785
|
-
pane_id: &'a str,
|
|
786
|
-
tmux_socket: Option<&'a str>,
|
|
787
|
-
}
|
|
788
|
-
|
|
789
|
-
fn pane_conflicts_on_same_socket(
|
|
790
|
-
pane_id: &str,
|
|
791
|
-
agent_socket: Option<&str>,
|
|
792
|
-
other: PaneSocketBinding<'_>,
|
|
793
|
-
) -> bool {
|
|
794
|
-
other.pane_id == pane_id && !tmux_sockets_known_different(agent_socket, other.tmux_socket)
|
|
795
|
-
}
|
|
796
|
-
|
|
797
|
-
fn tmux_sockets_known_different(left: Option<&str>, right: Option<&str>) -> bool {
|
|
798
|
-
let (Some(left), Some(right)) = (left, right) else {
|
|
799
|
-
return false;
|
|
800
|
-
};
|
|
801
|
-
if left == right {
|
|
802
|
-
return false;
|
|
803
|
-
}
|
|
804
|
-
std::path::Path::new(left).is_absolute() && std::path::Path::new(right).is_absolute()
|
|
805
|
-
}
|
|
806
|
-
|
|
807
|
-
fn runtime_tmux_socket(state: &serde_json::Value) -> Option<&str> {
|
|
808
|
-
tmux_socket_field(state)
|
|
809
|
-
}
|
|
810
|
-
|
|
811
|
-
fn tmux_socket_field(value: &serde_json::Value) -> Option<&str> {
|
|
812
|
-
value
|
|
813
|
-
.get("tmux_endpoint")
|
|
814
|
-
.or_else(|| value.get("tmux_socket"))
|
|
815
|
-
.and_then(serde_json::Value::as_str)
|
|
816
|
-
.filter(|value| !value.is_empty())
|
|
817
|
-
}
|
|
818
|
-
|
|
819
|
-
fn pane_socket_binding(value: &serde_json::Value) -> Option<PaneSocketBinding<'_>> {
|
|
820
|
-
let pane_id = value
|
|
821
|
-
.get("pane_id")
|
|
822
|
-
.and_then(serde_json::Value::as_str)
|
|
823
|
-
.filter(|s| !s.is_empty() && *s != "__team_agent_unbound__")?;
|
|
824
|
-
Some(PaneSocketBinding {
|
|
825
|
-
pane_id,
|
|
826
|
-
tmux_socket: tmux_socket_field(value),
|
|
827
|
-
})
|
|
828
|
-
}
|
|
829
|
-
|
|
830
|
-
fn agent_pane_owned_and_live(
|
|
831
|
-
transport: &dyn crate::transport::Transport,
|
|
832
|
-
agent: &serde_json::Value,
|
|
833
|
-
expected_session: &crate::transport::SessionName,
|
|
834
|
-
expected_window: &str,
|
|
835
|
-
) -> bool {
|
|
836
|
-
let Some(pane) = agent
|
|
837
|
-
.get("pane_id")
|
|
838
|
-
.and_then(serde_json::Value::as_str)
|
|
839
|
-
.filter(|pane| !pane.is_empty())
|
|
840
|
-
.map(crate::transport::PaneId::new)
|
|
841
|
-
else {
|
|
842
|
-
return false;
|
|
843
|
-
};
|
|
844
|
-
let Ok(targets) = transport.list_targets() else {
|
|
845
|
-
// Ownership is unknown when the topology snapshot fails. Preserve the
|
|
846
|
-
// base liveness direction: only positive death evidence may authorize
|
|
847
|
-
// a destructive respawn; otherwise fall back to the pane probe and
|
|
848
|
-
// leave retry authority to the operator.
|
|
849
|
-
return agent_pane_live_by_id(transport, &pane);
|
|
850
|
-
};
|
|
851
|
-
let owned = targets.iter().any(|target| {
|
|
852
|
-
target.pane_id == pane
|
|
853
|
-
&& target.session.as_str() == expected_session.as_str()
|
|
854
|
-
&& target
|
|
855
|
-
.window_name
|
|
856
|
-
.as_ref()
|
|
857
|
-
.is_some_and(|window| window.as_str() == expected_window)
|
|
858
|
-
});
|
|
859
|
-
owned && agent_pane_live_by_id(transport, &pane)
|
|
860
|
-
}
|
|
861
|
-
|
|
862
|
-
fn agent_pane_live_by_id(
|
|
863
|
-
transport: &dyn crate::transport::Transport,
|
|
864
|
-
pane: &crate::transport::PaneId,
|
|
865
|
-
) -> bool {
|
|
866
|
-
match transport.has_pane(&pane) {
|
|
867
|
-
Ok(Some(live)) => live,
|
|
868
|
-
Ok(None) | Err(_) => !matches!(
|
|
869
|
-
transport.liveness(&pane),
|
|
870
|
-
Ok(crate::model::enums::PaneLiveness::Dead)
|
|
871
|
-
),
|
|
872
|
-
}
|
|
873
|
-
}
|
|
874
|
-
|
|
875
|
-
/// 0.4.10+ reset duplicate-window fix (CR-approved, plan §1).
|
|
876
|
-
///
|
|
877
|
-
/// Enumerate live panes whose `(session, window_name)` match the given pair.
|
|
878
|
-
/// Used by `stop_agent_at_paths` (when the stored pane_id is stale/dead but a
|
|
879
|
-
/// same-role window survives) and by the reset hard gate (to prove no
|
|
880
|
-
/// duplicate window residue remains before `start_agent_at_paths`).
|
|
881
|
-
///
|
|
882
|
-
/// `list_targets()` is a point-in-time tmux snapshot — duplicates ARE
|
|
883
|
-
/// preserved in the result so callers can see the full set.
|
|
884
|
-
///
|
|
885
|
-
/// Caller MUST also check `is_per_agent_window(window, agent_id)` before
|
|
886
|
-
/// using this list to kill panes (plan §2 safety constraint): a shared
|
|
887
|
-
/// layout window may host co-tenants.
|
|
888
|
-
fn list_same_role_panes(
|
|
889
|
-
transport: &dyn crate::transport::Transport,
|
|
890
|
-
session: &crate::transport::SessionName,
|
|
891
|
-
window: &str,
|
|
892
|
-
) -> Vec<crate::transport::PaneInfo> {
|
|
893
|
-
transport
|
|
894
|
-
.list_targets()
|
|
895
|
-
.unwrap_or_default()
|
|
896
|
-
.into_iter()
|
|
897
|
-
.filter(|pane| {
|
|
898
|
-
pane.session.as_str() == session.as_str()
|
|
899
|
-
&& pane.window_name.as_ref().map(WindowName::as_str) == Some(window)
|
|
900
|
-
})
|
|
901
|
-
.collect()
|
|
902
|
-
}
|
|
903
|
-
|
|
904
|
-
/// 0.4.10+ reset duplicate-window fix (plan §2 safety constraint).
|
|
905
|
-
///
|
|
906
|
-
/// Returns true only when `window == agent_id`, i.e. the canonical
|
|
907
|
-
/// per-agent window. Adaptive/shared layout windows (`workers`, `team`,
|
|
908
|
-
/// or any layout window name produced by `is_adaptive_layout_window_pub`)
|
|
909
|
-
/// MUST NOT be broad-killed by name — they may host co-tenants. The
|
|
910
|
-
/// caller falls back to safer behavior (refuse stop, surface
|
|
911
|
-
/// RequirementUnmet/transport error) when this returns false.
|
|
912
|
-
fn is_per_agent_window(window: &str, agent_id: &AgentId) -> bool {
|
|
913
|
-
is_per_agent_cohort_window(window, agent_id)
|
|
914
|
-
}
|
|
915
|
-
|
|
916
|
-
fn tmux_start_mode_for_spawn(
|
|
917
|
-
spawn: &SpawnedAgentWindow,
|
|
918
|
-
into_existing_session: bool,
|
|
919
|
-
) -> &'static str {
|
|
920
|
-
if let Some(placement) = spawn.layout_placement.as_ref() {
|
|
921
|
-
if placement.starts_window {
|
|
922
|
-
if into_existing_session {
|
|
923
|
-
"new-window"
|
|
924
|
-
} else {
|
|
925
|
-
"new-session"
|
|
926
|
-
}
|
|
927
|
-
} else {
|
|
928
|
-
"split-window"
|
|
929
|
-
}
|
|
930
|
-
} else if into_existing_session {
|
|
931
|
-
"new-window"
|
|
932
|
-
} else {
|
|
933
|
-
"new-session"
|
|
934
|
-
}
|
|
935
|
-
}
|
|
936
|
-
|
|
937
|
-
// ═══════════════════════════════════════════════════════════════════════
|
|
938
|
-
// 0.4.6 Stage 1: reset/start clean-boundary helpers
|
|
939
|
-
// ═══════════════════════════════════════════════════════════════════════
|
|
940
|
-
|
|
941
|
-
/// ---
|
|
942
|
-
/// purpose: 有界轮询等待旧 pane 不可达且旧进程退出
|
|
943
|
-
/// params:
|
|
944
|
-
/// old_pane: 旧 pane,None 视作已死
|
|
945
|
-
/// old_pid: 旧进程号,None 视作已退出
|
|
946
|
-
/// returns: 证据载荷,含两者是否确认消失与实际等待毫秒数;到上限仍未消失也照实返回
|
|
947
|
-
/// ---
|
|
948
|
-
/// Bounded polling: wait for the old pane to become unreachable AND, when
|
|
949
|
-
/// possible, for the old pane_pid to exit. Returns a JSON evidence blob
|
|
950
|
-
/// recording the observed transition for events.
|
|
951
|
-
pub(super) fn drain_old_pane_and_pid(
|
|
952
|
-
transport: &dyn crate::transport::Transport,
|
|
953
|
-
old_pane: Option<&crate::transport::PaneId>,
|
|
954
|
-
old_pid: Option<u32>,
|
|
955
|
-
) -> serde_json::Value {
|
|
956
|
-
const DRAIN_MAX_MS: u64 = 1500;
|
|
957
|
-
const DRAIN_POLL_MS: u64 = 50;
|
|
958
|
-
let start = std::time::Instant::now();
|
|
959
|
-
let mut pane_dead = old_pane.is_none();
|
|
960
|
-
let mut pid_dead = old_pid.is_none();
|
|
961
|
-
while start.elapsed().as_millis() < DRAIN_MAX_MS as u128 {
|
|
962
|
-
if !pane_dead {
|
|
963
|
-
if let Some(pane) = old_pane {
|
|
964
|
-
if !agent_pane_live_by_id(transport, pane) {
|
|
965
|
-
pane_dead = true;
|
|
966
|
-
}
|
|
967
|
-
}
|
|
968
|
-
}
|
|
969
|
-
if !pid_dead {
|
|
970
|
-
if let Some(pid) = old_pid {
|
|
971
|
-
if !pid_is_alive(pid) {
|
|
972
|
-
pid_dead = true;
|
|
973
|
-
}
|
|
974
|
-
}
|
|
975
|
-
}
|
|
976
|
-
if pane_dead && pid_dead {
|
|
977
|
-
break;
|
|
978
|
-
}
|
|
979
|
-
std::thread::sleep(std::time::Duration::from_millis(DRAIN_POLL_MS));
|
|
980
|
-
}
|
|
981
|
-
serde_json::json!({
|
|
982
|
-
"old_pane_id": old_pane.map(|p| p.as_str()),
|
|
983
|
-
"old_pane_pid": old_pid,
|
|
984
|
-
"old_pane_dead": pane_dead,
|
|
985
|
-
"old_pid_dead": pid_dead,
|
|
986
|
-
"drain_elapsed_ms": start.elapsed().as_millis() as u64,
|
|
987
|
-
})
|
|
988
|
-
}
|
|
989
|
-
|
|
990
|
-
/// ---
|
|
991
|
-
/// purpose: 尽力探测某进程号是否还活着
|
|
992
|
-
/// returns: 由平台层给出的判定
|
|
993
|
-
/// ---
|
|
994
|
-
/// Best-effort liveness probe for a pid.
|
|
995
|
-
///
|
|
996
|
-
/// 0.5.x Windows portability Batch 3: routes through
|
|
997
|
-
/// `crate::platform::process::pid_is_alive`. Unix uses `kill(pid, 0)`
|
|
998
|
-
/// with the `EPERM = Live` branch preserved byte-for-byte; Windows
|
|
999
|
-
/// uses `OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION)` +
|
|
1000
|
-
/// `GetExitCodeProcess` (STILL_ACTIVE = Live). The legacy non-Unix
|
|
1001
|
-
/// `true` fallback (which broke drain by reporting every pid alive)
|
|
1002
|
-
/// is gone.
|
|
1003
|
-
pub(super) fn pid_is_alive(pid: u32) -> bool {
|
|
1004
|
-
crate::platform::process::pid_is_alive(pid)
|
|
1005
|
-
}
|
|
1006
|
-
|
|
1007
|
-
/// ---
|
|
1008
|
-
/// purpose: 从 state 里读该席位的 pane 进程号
|
|
1009
|
-
/// returns: 存在且是无符号整数时返回它,否则 None
|
|
1010
|
-
/// ---
|
|
1011
|
-
/// Read state.agents[agent_id].pane_pid (u32) from the runtime state.
|
|
1012
|
-
pub(super) fn state_pane_pid(state: &serde_json::Value, agent_id: &AgentId) -> Option<u32> {
|
|
1013
|
-
state
|
|
1014
|
-
.get("agents")
|
|
1015
|
-
.and_then(|v| v.get(agent_id.as_str()))
|
|
1016
|
-
.and_then(|v| v.get("pane_pid"))
|
|
1017
|
-
.and_then(serde_json::Value::as_u64)
|
|
1018
|
-
.map(|p| p as u32)
|
|
1019
|
-
}
|
|
1020
|
-
|
|
1021
|
-
/// ---
|
|
1022
|
-
/// purpose: 算出该席位下一次的 spawn 世代号
|
|
1023
|
-
/// returns: 当前值加一,缺失时按 0 起算;世代号用于把捕获与事件归因到本次进程
|
|
1024
|
-
/// ---
|
|
1025
|
-
/// Read + increment state.agents[agent_id].spawn_epoch. The epoch is a
|
|
1026
|
-
/// monotonic counter persisted with the agent row that uniquely tags each
|
|
1027
|
-
/// fresh-start / reset / restart cycle. Subsequent capture / event /
|
|
1028
|
-
/// status logic dispatches on `(team_key, agent_id, spawn_epoch,
|
|
1029
|
-
/// pane_pid, expected_session_id)` to avoid attributing a stale prior
|
|
1030
|
-
/// fresh attempt to the current process.
|
|
1031
|
-
pub(super) fn next_spawn_epoch(state: &serde_json::Value, agent_id: &AgentId) -> u64 {
|
|
1032
|
-
let current = state
|
|
1033
|
-
.get("agents")
|
|
1034
|
-
.and_then(|v| v.get(agent_id.as_str()))
|
|
1035
|
-
.and_then(|v| v.get("spawn_epoch"))
|
|
1036
|
-
.and_then(serde_json::Value::as_u64)
|
|
1037
|
-
.unwrap_or(0);
|
|
1038
|
-
current.saturating_add(1)
|
|
1039
|
-
}
|
|
1040
|
-
|
|
1041
|
-
/// ---
|
|
1042
|
-
/// purpose: 停一席的对外入口
|
|
1043
|
-
/// returns: 停止报告
|
|
1044
|
-
/// errors: 选不到 team 返回 TeamSelect,席位不存在返回 RequirementUnmet
|
|
1045
|
-
/// contract_id: lifecycle.stop_agent.entry
|
|
1046
|
-
/// ---
|
|
1047
|
-
/// `stop_agent(workspace, agent_id, team)`(`lifecycle/operations.py:62`)。
|
|
1048
|
-
/// owner-gate → kill window → **同时关显示** → 写 state。
|
|
1049
|
-
pub fn stop_agent(
|
|
1050
|
-
workspace: &Path,
|
|
1051
|
-
agent_id: &AgentId,
|
|
1052
|
-
team: Option<&str>,
|
|
1053
|
-
) -> Result<StopAgentReport, LifecycleError> {
|
|
1054
|
-
let paths = lifecycle_paths(workspace, team)?;
|
|
1055
|
-
let canonical_team = paths.canonical_team(team).map(str::to_string);
|
|
1056
|
-
let team = canonical_team.as_deref();
|
|
1057
|
-
let _lock = acquire_agent_lifecycle_lock(LifecycleLockRequest {
|
|
1058
|
-
workspace: &paths.run_workspace,
|
|
1059
|
-
operation: "stop-agent",
|
|
1060
|
-
team,
|
|
1061
|
-
agent_id: Some(agent_id),
|
|
1062
|
-
})?;
|
|
1063
|
-
let transport = paths.tmux_backend()?;
|
|
1064
|
-
stop_agent_at_paths(
|
|
1065
|
-
&paths.run_workspace,
|
|
1066
|
-
&paths.spec_workspace,
|
|
1067
|
-
agent_id,
|
|
1068
|
-
team,
|
|
1069
|
-
&transport,
|
|
1070
|
-
)
|
|
1071
|
-
}
|
|
1072
|
-
|
|
1073
|
-
/// ---
|
|
1074
|
-
/// purpose: 带注入 transport 的停席入口
|
|
1075
|
-
/// returns: 停止报告
|
|
1076
|
-
/// errors: 同 stop_agent
|
|
1077
|
-
/// contract_id: lifecycle.stop_agent.entry
|
|
1078
|
-
/// ---
|
|
1079
|
-
pub(crate) fn stop_agent_with_transport(
|
|
1080
|
-
workspace: &Path,
|
|
1081
|
-
agent_id: &AgentId,
|
|
1082
|
-
team: Option<&str>,
|
|
1083
|
-
transport: &dyn crate::transport::Transport,
|
|
1084
|
-
) -> Result<StopAgentReport, LifecycleError> {
|
|
1085
|
-
let paths = lifecycle_paths(workspace, team)?;
|
|
1086
|
-
let canonical_team = paths.canonical_team(team).map(str::to_string);
|
|
1087
|
-
let team = canonical_team.as_deref();
|
|
1088
|
-
let _lock = acquire_agent_lifecycle_lock(LifecycleLockRequest {
|
|
1089
|
-
workspace: &paths.run_workspace,
|
|
1090
|
-
operation: "stop-agent",
|
|
1091
|
-
team,
|
|
1092
|
-
agent_id: Some(agent_id),
|
|
1093
|
-
})?;
|
|
1094
|
-
stop_agent_at_paths(
|
|
1095
|
-
&paths.run_workspace,
|
|
1096
|
-
&paths.spec_workspace,
|
|
1097
|
-
agent_id,
|
|
1098
|
-
team,
|
|
1099
|
-
transport,
|
|
1100
|
-
)
|
|
1101
|
-
}
|
|
1102
|
-
|
|
1103
|
-
/// ---
|
|
1104
|
-
/// purpose: 停席的实体实现,解析席位、收敛杀 pane 与窗口、取排空证据后落 state
|
|
1105
|
-
/// returns: 停止报告;注意报告里的显示已关闭字段恒为真,不反映实际关显示结果
|
|
1106
|
-
/// errors: 席位解析不到实体返回 RequirementUnmet,写 state 失败返回 StatePersist
|
|
1107
|
-
/// ---
|
|
1108
|
-
pub(super) fn stop_agent_at_paths(
|
|
1109
|
-
workspace: &Path,
|
|
1110
|
-
spec_workspace: &Path,
|
|
1111
|
-
agent_id: &AgentId,
|
|
1112
|
-
team: Option<&str>,
|
|
1113
|
-
transport: &dyn crate::transport::Transport,
|
|
1114
|
-
) -> Result<StopAgentReport, LifecycleError> {
|
|
1115
|
-
// golden operations.py:64-66: resolve_team_scoped_state -> owner gate, BEFORE the unknown-worker raise.
|
|
1116
|
-
let seat = super::remove::resolve_seat(workspace, spec_workspace, agent_id, team, transport)?;
|
|
1117
|
-
if seat.consistency == super::remove::SeatConsistency::Absent {
|
|
1118
|
-
return Err(unknown_worker(agent_id));
|
|
1119
|
-
}
|
|
1120
|
-
let mut state = seat.state;
|
|
1121
|
-
let spec = seat.spec;
|
|
1122
|
-
let state_agent = state.get("agents").and_then(|v| v.get(agent_id.as_str()));
|
|
1123
|
-
// Persisted-only seats must remain stoppable. Build the minimal provider
|
|
1124
|
-
// projection consumed by mark_agent_stopped instead of requiring a spec
|
|
1125
|
-
// row that is precisely what recovery is repairing.
|
|
1126
|
-
let fallback_agent = YamlValue::Map(vec![(
|
|
1127
|
-
"provider".to_string(),
|
|
1128
|
-
YamlValue::Str(
|
|
1129
|
-
state_agent
|
|
1130
|
-
.and_then(|agent| agent.get("provider"))
|
|
1131
|
-
.and_then(|value| value.as_str())
|
|
1132
|
-
.unwrap_or("codex")
|
|
1133
|
-
.to_string(),
|
|
1134
|
-
),
|
|
1135
|
-
)]);
|
|
1136
|
-
let agent = find_spec_agent(&spec, agent_id).unwrap_or(&fallback_agent);
|
|
1137
|
-
let session_name = seat.session;
|
|
1138
|
-
let window = seat.window;
|
|
1139
|
-
let pane_id = seat.physical.map(|pane| pane.pane_id).or_else(|| {
|
|
1140
|
-
state
|
|
1141
|
-
.get("agents")
|
|
1142
|
-
.and_then(|agents| agents.get(agent_id.as_str()))
|
|
1143
|
-
.and_then(|agent| agent.get("pane_id"))
|
|
1144
|
-
.and_then(serde_json::Value::as_str)
|
|
1145
|
-
.filter(|pane| !pane.is_empty())
|
|
1146
|
-
.map(crate::transport::PaneId::new)
|
|
1147
|
-
});
|
|
1148
|
-
let target_str = pane_id
|
|
1149
|
-
.as_ref()
|
|
1150
|
-
.map(|pane| pane.as_str().to_string())
|
|
1151
|
-
.unwrap_or_else(|| format!("{}:{window}", session_name.as_str()));
|
|
1152
|
-
// 0.4.10+ reset duplicate-window fix (plan §2): stop must resolve a
|
|
1153
|
-
// STALE stored pane_id to live same-role panes by `(session, window)`
|
|
1154
|
-
// enumeration BEFORE concluding the worker is absent. Pre-fix logic
|
|
1155
|
-
// returned stopped=false when pane_id was dead even if a residual
|
|
1156
|
-
// duplicate window survived — that residue then collided with
|
|
1157
|
-
// reset's unconditional start, producing the observed
|
|
1158
|
-
// `stopped=false, started=true` duplicate-window pattern.
|
|
1159
|
-
//
|
|
1160
|
-
// Only the STALE-pane-id branch enumerates same-role panes. When
|
|
1161
|
-
// pane_id was never set in state (legacy state shape, never observed
|
|
1162
|
-
// a real spawn), the existing window-based fallback is preserved —
|
|
1163
|
-
// the duplicate-window bug requires a stored-but-stale pane_id as
|
|
1164
|
-
// the trigger.
|
|
1165
|
-
let stored_pane_live = pane_id
|
|
1166
|
-
.as_ref()
|
|
1167
|
-
.map(|pane| agent_pane_live_by_id(transport, pane))
|
|
1168
|
-
.unwrap_or(false);
|
|
1169
|
-
let stored_pane_stale = pane_id.is_some() && !stored_pane_live;
|
|
1170
|
-
let same_role_panes: Vec<crate::transport::PaneInfo> =
|
|
1171
|
-
if stored_pane_stale && is_per_agent_window(&window, agent_id) {
|
|
1172
|
-
list_same_role_panes(transport, &session_name, &window)
|
|
1173
|
-
} else {
|
|
1174
|
-
Vec::new()
|
|
1175
|
-
};
|
|
1176
|
-
let stopped = stored_pane_live
|
|
1177
|
-
|| !same_role_panes.is_empty()
|
|
1178
|
-
|| (pane_id.is_none() && window_exists(transport, &session_name, &window));
|
|
1179
|
-
if stopped {
|
|
1180
|
-
let stop_targets =
|
|
1181
|
-
if let Some(pane) = pane_id.as_ref().filter(|_| stored_pane_live) {
|
|
1182
|
-
vec![Target::Pane(pane.clone())]
|
|
1183
|
-
} else if !same_role_panes.is_empty() {
|
|
1184
|
-
same_role_panes
|
|
1185
|
-
.iter()
|
|
1186
|
-
.map(|pane| Target::Pane(pane.pane_id.clone()))
|
|
1187
|
-
.collect()
|
|
1188
|
-
} else {
|
|
1189
|
-
vec![Target::SessionWindow {
|
|
1190
|
-
session: session_name.clone(),
|
|
1191
|
-
window: WindowName::new(&window),
|
|
1192
|
-
}]
|
|
1193
|
-
};
|
|
1194
|
-
// Explicit kill-pane/kill-window ignores remain-on-exit on the worker.
|
|
1195
|
-
// Preserve the last window BEFORE killing it, or the coordinator sees
|
|
1196
|
-
// a missing session and stops while the seat is merely being stopped.
|
|
1197
|
-
transport
|
|
1198
|
-
.preserve_session_before_stop(&session_name, &stop_targets)
|
|
1199
|
-
.map_err(|error| {
|
|
1200
|
-
LifecycleError::Transport(format!(
|
|
1201
|
-
"failed to preserve session before stopping agent {agent_id}: {error}"
|
|
1202
|
-
))
|
|
1203
|
-
})?;
|
|
1204
|
-
// golden operations.py:84-86: a non-zero kill-window raises
|
|
1205
|
-
// RuntimeError(f"failed to stop agent {agent_id}: {proc.stderr.strip()}").
|
|
1206
|
-
//
|
|
1207
|
-
// 0.4.10+ kill resolution order (plan §2):
|
|
1208
|
-
// 1. stored pane_id is live → kill it by pane_id.
|
|
1209
|
-
// 2. stored pane_id is stale BUT same-role panes survive →
|
|
1210
|
-
// kill each by pane_id (duplicate window names make
|
|
1211
|
-
// kill-window -t session:window ambiguous).
|
|
1212
|
-
// 3. no pane_id at all but window exists → kill_window as
|
|
1213
|
-
// before (legacy compat for state without pane_id field).
|
|
1214
|
-
let kill_result: Result<(), crate::transport::TransportError> =
|
|
1215
|
-
if let Some(pane) = pane_id.as_ref().filter(|_| stored_pane_live) {
|
|
1216
|
-
transport.kill_pane(pane)
|
|
1217
|
-
} else if !same_role_panes.is_empty() {
|
|
1218
|
-
let mut last_err: Option<crate::transport::TransportError> = None;
|
|
1219
|
-
for residual in &same_role_panes {
|
|
1220
|
-
if let Err(e) = transport.kill_pane(&residual.pane_id) {
|
|
1221
|
-
last_err = Some(e);
|
|
1222
|
-
}
|
|
1223
|
-
}
|
|
1224
|
-
last_err.map(Err).unwrap_or(Ok(()))
|
|
1225
|
-
} else {
|
|
1226
|
-
let target = Target::SessionWindow {
|
|
1227
|
-
session: session_name.clone(),
|
|
1228
|
-
window: WindowName::new(&window),
|
|
1229
|
-
};
|
|
1230
|
-
transport.kill_window(&target)
|
|
1231
|
-
};
|
|
1232
|
-
if let Err(e) = kill_result {
|
|
1233
|
-
let stderr = match &e {
|
|
1234
|
-
crate::transport::TransportError::Subprocess { stderr, .. } => {
|
|
1235
|
-
stderr.trim().to_string()
|
|
1236
|
-
}
|
|
1237
|
-
other => other.to_string(),
|
|
1238
|
-
};
|
|
1239
|
-
let _ = write_stop_window_failed_event(workspace, agent_id, &target_str, &stderr);
|
|
1240
|
-
return Err(LifecycleError::Transport(format!(
|
|
1241
|
-
"failed to stop agent {agent_id}: {stderr}"
|
|
1242
|
-
)));
|
|
1243
|
-
}
|
|
1244
|
-
// 0.4.6 Stage 1: drain-and-prove. The pre-fix path trusted `kill_pane`'s
|
|
1245
|
-
// success return without waiting for the pane / pid to actually exit.
|
|
1246
|
-
// This created a window where reset/start spawned a new worker while
|
|
1247
|
-
// the old Claude process + tty + provider state were still alive,
|
|
1248
|
-
// leading to the macmini "running but not capturable" failure mode.
|
|
1249
|
-
//
|
|
1250
|
-
// Poll bounded time for old pane to become unreachable. If it stays
|
|
1251
|
-
// reachable after the budget, emit an event but do NOT block — the
|
|
1252
|
-
// subsequent spawn boundary check (new pane_id != old pane_id) is
|
|
1253
|
-
// the final safety net.
|
|
1254
|
-
let drain_evidence = drain_old_pane_and_pid(
|
|
1255
|
-
transport,
|
|
1256
|
-
pane_id.as_ref(),
|
|
1257
|
-
state_pane_pid(&state, agent_id),
|
|
1258
|
-
);
|
|
1259
|
-
let _ = write_stop_drain_event(workspace, agent_id, &target_str, &drain_evidence);
|
|
1260
|
-
}
|
|
1261
|
-
mark_agent_stopped(&mut state, agent_id, agent, &window)?;
|
|
1262
|
-
state["agents"][agent_id.as_str()]["explicit_stop"] = serde_json::Value::Bool(true);
|
|
1263
|
-
let team_key = restart_projection_team_key(&state, team);
|
|
1264
|
-
// golden operations.py:95: save_team_scoped_state (team projection) — NOT a raw save, so a
|
|
1265
|
-
// multi-team workspace keeps the other teams' persisted runtime state instead of being clobbered.
|
|
1266
|
-
crate::state::repository::StateRepository::new(workspace).save(
|
|
1267
|
-
crate::state::repository::StateWriteIntent::StopAgent {
|
|
1268
|
-
team_key: &team_key,
|
|
1269
|
-
agent_id: agent_id.as_str(),
|
|
1270
|
-
},
|
|
1271
|
-
&state,
|
|
1272
|
-
)
|
|
1273
|
-
.map_err(|e| LifecycleError::StatePersist(e.to_string()))?;
|
|
1274
|
-
// golden operations.py:96-99: snapshot (side-effect), then state_file = write_team_state path.
|
|
1275
|
-
// Foundation-0 F0-2: legacy per-session snapshot dual-write retired
|
|
1276
|
-
// (`.team/artifacts/foundation-0-slice-design.md` §§4-5). Root state
|
|
1277
|
-
// remains the only save target on the stop path.
|
|
1278
|
-
let state_file = write_team_state(spec_workspace, &spec, &state)?;
|
|
1279
|
-
write_stop_complete_event(workspace, agent_id, &target_str, stopped)?;
|
|
1280
|
-
Ok(StopAgentReport {
|
|
1281
|
-
agent_id: agent_id.clone(),
|
|
1282
|
-
target: target_str,
|
|
1283
|
-
stopped,
|
|
1284
|
-
display_closed: true,
|
|
1285
|
-
state_file,
|
|
1286
|
-
})
|
|
1287
|
-
}
|
|
1288
|
-
|
|
1289
|
-
/// ---
|
|
1290
|
-
/// purpose: 取团队作用域的投影 state,把选择拒绝转成带原因的错误
|
|
1291
|
-
/// params:
|
|
1292
|
-
/// team: 目标团队,None 时按唯一性选
|
|
1293
|
-
/// returns: 投影后的 state
|
|
1294
|
-
/// errors: 目标歧义或选不出时返回 TeamSelect 并在消息里带原因与细节,读取失败返回 StatePersist
|
|
1295
|
-
/// ---
|
|
1296
|
-
/// golden `resolve_team_scoped_state` (state.py:243): returns the team-scoped projected state, or
|
|
1297
|
-
/// surfaces the refusal dict (`team_target_ambiguous` / `team_target_unresolved`) as a typed error
|
|
1298
|
-
/// BEFORE the owner gate / unknown-worker raise (operations.py:64-66). The lifecycle return types are
|
|
1299
|
-
/// typed structs with no refusal-Value variant, so the observable refusal is carried in
|
|
1300
|
-
/// `LifecycleError::TeamSelect`'s message (reason + error), which is the closest byte-faithful surface.
|
|
1301
|
-
pub(super) fn resolve_team_scoped_state_or_refuse(
|
|
1302
|
-
workspace: &Path,
|
|
1303
|
-
team: Option<&str>,
|
|
1304
|
-
) -> Result<serde_json::Value, LifecycleError> {
|
|
1305
|
-
let (state, refusal) = crate::state::projection::resolve_team_scoped_state(workspace, team)
|
|
1306
|
-
.map_err(|e| LifecycleError::StatePersist(e.to_string()))?;
|
|
1307
|
-
if let Some(refusal) = refusal {
|
|
1308
|
-
let reason = refusal
|
|
1309
|
-
.get("reason")
|
|
1310
|
-
.and_then(|v| v.as_str())
|
|
1311
|
-
.unwrap_or("team_target_unresolved");
|
|
1312
|
-
let detail = refusal
|
|
1313
|
-
.get("error")
|
|
1314
|
-
.or_else(|| refusal.get("message"))
|
|
1315
|
-
.and_then(|v| v.as_str())
|
|
1316
|
-
.unwrap_or("");
|
|
1317
|
-
return Err(LifecycleError::TeamSelect(format!("{reason}: {detail}")));
|
|
1318
|
-
}
|
|
1319
|
-
state.ok_or_else(|| {
|
|
1320
|
-
LifecycleError::StatePersist("resolve_team_scoped_state returned no state".to_string())
|
|
1321
|
-
})
|
|
1322
|
-
}
|
|
1323
|
-
|
|
1324
|
-
pub(super) fn mark_agent_started(
|
|
1325
|
-
state: &mut serde_json::Value,
|
|
1326
|
-
agent_id: &AgentId,
|
|
1327
|
-
window: &str,
|
|
1328
|
-
spawn: &SpawnedAgentWindow,
|
|
1329
|
-
transport: &dyn crate::transport::Transport,
|
|
1330
|
-
safety: &DangerousApproval,
|
|
1331
|
-
start_mode: StartMode,
|
|
1332
|
-
) -> Result<(), LifecycleError> {
|
|
1333
|
-
let Some(agent) = state
|
|
1334
|
-
.get_mut("agents")
|
|
1335
|
-
.and_then(serde_json::Value::as_object_mut)
|
|
1336
|
-
.and_then(|agents| agents.get_mut(agent_id.as_str()))
|
|
1337
|
-
.and_then(serde_json::Value::as_object_mut)
|
|
1338
|
-
else {
|
|
1339
|
-
return Err(LifecycleError::StatePersist(format!(
|
|
1340
|
-
"agent {} state is not an object",
|
|
1341
|
-
agent_id
|
|
1342
|
-
)));
|
|
1343
|
-
};
|
|
1344
|
-
// 0.5.32 (`.team/artifacts/restart-resumed-stale-activity-locate.md` §5):
|
|
1345
|
-
// a successful new process cohort invalidates the per-agent
|
|
1346
|
-
// turn/activity observation set. Do this before overwriting the
|
|
1347
|
-
// lifecycle/topology fields so absence == UNKNOWN until the next
|
|
1348
|
-
// coordinator tick or pane fallback produces a post-spawn observation.
|
|
1349
|
-
clear_agent_runtime_activity_observation(agent);
|
|
1350
|
-
// 0.5.66 bypass 单源 §2.6:fresh spawn 那一刻记录运行面 bypass 值,
|
|
1351
|
-
// 供下一次 restart 的 Noop drift 挡板比较。
|
|
1352
|
-
agent.insert(
|
|
1353
|
-
"as_launched_dangerously_skip_permissions".to_string(),
|
|
1354
|
-
serde_json::json!(safety.enabled),
|
|
1355
|
-
);
|
|
1356
|
-
// S1-CAPTURE-001 (0.4.8, CR M3 provider-agnostic): on a Fresh /
|
|
1357
|
-
// FreshAfterMissingRollout start, the prior session's authoritative
|
|
1358
|
-
// capture tuple MUST be cleared before persist_command_plan_state
|
|
1359
|
-
// writes the new _pending_session_id. Otherwise old session_id +
|
|
1360
|
-
// rollout_path coexist with new _pending_session_id and
|
|
1361
|
-
// agent_session_complete returns true on the stale tuple — capture
|
|
1362
|
-
// never re-binds to the new process, and any delivered token lands
|
|
1363
|
-
// in the old transcript (the leader/unassigned mis-attribution seen
|
|
1364
|
-
// in the gate evidence). This applies to all providers that resume:
|
|
1365
|
-
// codex, claude, copilot. Reset_agent --discard-session already does
|
|
1366
|
-
// this at common.rs:1144-1188; here we mirror it for start-agent /
|
|
1367
|
-
// restart-agent fresh paths so the fresh-tuple invariant is global.
|
|
1368
|
-
if matches!(
|
|
1369
|
-
start_mode,
|
|
1370
|
-
StartMode::Fresh | StartMode::FreshAfterMissingRollout
|
|
1371
|
-
) {
|
|
1372
|
-
for field in [
|
|
1373
|
-
"session_id",
|
|
1374
|
-
"rollout_path",
|
|
1375
|
-
"captured_at",
|
|
1376
|
-
"captured_via",
|
|
1377
|
-
"attribution_confidence",
|
|
1378
|
-
"capture_state",
|
|
1379
|
-
"attribution_ambiguous",
|
|
1380
|
-
] {
|
|
1381
|
-
agent.remove(field);
|
|
1382
|
-
}
|
|
1383
|
-
}
|
|
1384
|
-
agent.insert("status".to_string(), serde_json::json!("running"));
|
|
1385
|
-
agent.insert("explicit_stop".to_string(), serde_json::Value::Bool(false));
|
|
1386
|
-
agent.insert("agent_id".to_string(), serde_json::json!(agent_id.as_str()));
|
|
1387
|
-
agent.insert("window".to_string(), serde_json::json!(window));
|
|
1388
|
-
agent.insert(
|
|
1389
|
-
"pane_id".to_string(),
|
|
1390
|
-
serde_json::json!(spawn.spawn.pane_id.as_str()),
|
|
1391
|
-
);
|
|
1392
|
-
let pane_pid = spawn.spawn.child_pid.or_else(|| {
|
|
1393
|
-
transport
|
|
1394
|
-
.list_targets()
|
|
1395
|
-
.unwrap_or_default()
|
|
1396
|
-
.into_iter()
|
|
1397
|
-
.find(|pane| pane.pane_id == spawn.spawn.pane_id)
|
|
1398
|
-
.and_then(|pane| pane.pane_pid)
|
|
1399
|
-
});
|
|
1400
|
-
if let Some(pane_pid) = pane_pid {
|
|
1401
|
-
agent.insert("pane_pid".to_string(), serde_json::json!(pane_pid));
|
|
1402
|
-
} else {
|
|
1403
|
-
agent.remove("pane_pid");
|
|
1404
|
-
}
|
|
1405
|
-
agent.insert(
|
|
1406
|
-
"spawned_at".to_string(),
|
|
1407
|
-
serde_json::json!(spawn.spawned_at.as_str()),
|
|
1408
|
-
);
|
|
1409
|
-
agent.insert(
|
|
1410
|
-
"spawn_cwd".to_string(),
|
|
1411
|
-
serde_json::json!(spawn.spawn_cwd.to_string_lossy().to_string()),
|
|
1412
|
-
);
|
|
1413
|
-
if let Some(path) = &spawn.mcp_config_path {
|
|
1414
|
-
agent.insert("mcp_config".to_string(), serde_json::json!(path));
|
|
1415
|
-
}
|
|
1416
|
-
// 0.4.6 Stage 1+2: ensure spawn_epoch is present after every start.
|
|
1417
|
-
// reset_agent_at_paths bumps it before start; non-reset starts
|
|
1418
|
-
// initialise to 1 (or preserve existing) so capture/event paths can
|
|
1419
|
-
// always dispatch on (team_key, agent_id, spawn_epoch, ...).
|
|
1420
|
-
let preserved_epoch = agent
|
|
1421
|
-
.get("spawn_epoch")
|
|
1422
|
-
.and_then(serde_json::Value::as_u64)
|
|
1423
|
-
.filter(|n| *n > 0);
|
|
1424
|
-
agent.insert(
|
|
1425
|
-
"spawn_epoch".to_string(),
|
|
1426
|
-
serde_json::json!(preserved_epoch.unwrap_or(1)),
|
|
1427
|
-
);
|
|
1428
|
-
// Issue 2 (Round 3b gate review §6): persist the resolved owner_team_id
|
|
1429
|
-
// so future restart/start cycles read it directly from the agent row.
|
|
1430
|
-
if let Some(ref team_id) = spawn.owner_team_id {
|
|
1431
|
-
if !team_id.is_empty() {
|
|
1432
|
-
agent.insert("owner_team_id".to_string(), serde_json::json!(team_id));
|
|
1433
|
-
}
|
|
1434
|
-
}
|
|
1435
|
-
crate::lifecycle::launch::persist_command_plan_state(agent, &spawn.plan, &spawn.profile_launch);
|
|
1436
|
-
crate::lifecycle::launch::persist_effective_approval_policy(agent, safety);
|
|
1437
|
-
if let Some(placement) = spawn.layout_placement.as_ref() {
|
|
1438
|
-
agent.insert(
|
|
1439
|
-
"layout_window".to_string(),
|
|
1440
|
-
serde_json::json!(placement.layout_window.as_str()),
|
|
1441
|
-
);
|
|
1442
|
-
agent.insert(
|
|
1443
|
-
"layout_index".to_string(),
|
|
1444
|
-
serde_json::json!(placement.layout_index),
|
|
1445
|
-
);
|
|
1446
|
-
agent.insert(
|
|
1447
|
-
"pane_index".to_string(),
|
|
1448
|
-
serde_json::json!(placement.pane_index),
|
|
1449
|
-
);
|
|
1450
|
-
agent.insert(
|
|
1451
|
-
"display".to_string(),
|
|
1452
|
-
serde_json::json!({
|
|
1453
|
-
"backend": "adaptive",
|
|
1454
|
-
"status": "opened",
|
|
1455
|
-
"window": placement.layout_window.as_str(),
|
|
1456
|
-
"workspace_window": null,
|
|
1457
|
-
"pane_id": spawn.spawn.pane_id.as_str(),
|
|
1458
|
-
"pane_title": agent_id.as_str(),
|
|
1459
|
-
"target": spawn.spawn.pane_id.as_str(),
|
|
1460
|
-
"target_worker_session": spawn.spawn.session.as_str(),
|
|
1461
|
-
"linked_session": null,
|
|
1462
|
-
"leader_session": spawn.spawn.session.as_str(),
|
|
1463
|
-
"display_session": null,
|
|
1464
|
-
"fallback": null,
|
|
1465
|
-
}),
|
|
1466
|
-
);
|
|
1467
|
-
}
|
|
1468
|
-
agent.remove("startup_prompts");
|
|
1469
|
-
agent.remove("startup_prompt_status");
|
|
1470
|
-
agent.remove("startup_prompt_probe_epoch");
|
|
1471
|
-
agent.remove(crate::lifecycle::launch::add_agent_state::FORK_PARENT_SESSION_ID);
|
|
1472
|
-
agent.remove("startup_prompt_probe_disabled_at");
|
|
1473
|
-
Ok(())
|
|
1474
|
-
}
|
|
1475
|
-
|
|
1476
|
-
/// ---
|
|
1477
|
-
/// purpose: 重置一席的对外入口
|
|
1478
|
-
/// params:
|
|
1479
|
-
/// discard_session: 必须为真,否则直接返回拒绝
|
|
1480
|
-
/// returns: 重置结果,未显式丢弃会话时返回 Refused
|
|
1481
|
-
/// errors: 选不到 team 返回 TeamSelect,其余透传实体实现
|
|
1482
|
-
/// contract_id: lifecycle.reset_agent.entry
|
|
1483
|
-
/// ---
|
|
1484
|
-
/// `reset_agent(workspace, agent_id, discard_session, open_display, team)`
|
|
1485
|
-
/// (`lifecycle/operations.py:102`)。discard + 重起;**未传 discard_session → 拒绝**。
|
|
1486
|
-
pub fn reset_agent(
|
|
1487
|
-
workspace: &Path,
|
|
1488
|
-
agent_id: &AgentId,
|
|
1489
|
-
discard_session: bool,
|
|
1490
|
-
open_display: bool,
|
|
1491
|
-
team: Option<&str>,
|
|
1492
|
-
) -> Result<ResetAgentOutcome, LifecycleError> {
|
|
1493
|
-
if !discard_session {
|
|
1494
|
-
return Ok(ResetAgentOutcome::Refused {
|
|
1495
|
-
reason: ResetRefusal::DiscardSessionRequired,
|
|
1496
|
-
});
|
|
1497
|
-
}
|
|
1498
|
-
let paths = lifecycle_paths(workspace, team)?;
|
|
1499
|
-
let canonical_team = paths.canonical_team(team).map(str::to_string);
|
|
1500
|
-
let team = canonical_team.as_deref();
|
|
1501
|
-
let _lock = acquire_agent_lifecycle_lock(LifecycleLockRequest {
|
|
1502
|
-
workspace: &paths.run_workspace,
|
|
1503
|
-
operation: "reset-agent",
|
|
1504
|
-
team,
|
|
1505
|
-
agent_id: Some(agent_id),
|
|
1506
|
-
})?;
|
|
1507
|
-
let transport = paths.tmux_backend()?;
|
|
1508
|
-
reset_agent_at_paths(
|
|
1509
|
-
&paths.run_workspace,
|
|
1510
|
-
&paths.spec_workspace,
|
|
1511
|
-
agent_id,
|
|
1512
|
-
discard_session,
|
|
1513
|
-
open_display,
|
|
1514
|
-
team,
|
|
1515
|
-
&transport,
|
|
1516
|
-
)
|
|
1517
|
-
}
|
|
1518
|
-
|
|
1519
|
-
/// ---
|
|
1520
|
-
/// purpose: 带注入 transport 的重置入口
|
|
1521
|
-
/// params:
|
|
1522
|
-
/// discard_session: 必须为真,否则直接返回拒绝
|
|
1523
|
-
/// returns: 重置结果
|
|
1524
|
-
/// errors: 同 reset_agent
|
|
1525
|
-
/// contract_id: lifecycle.reset_agent.entry
|
|
1526
|
-
/// ---
|
|
1527
|
-
pub(crate) fn reset_agent_with_transport(
|
|
1528
|
-
workspace: &Path,
|
|
1529
|
-
agent_id: &AgentId,
|
|
1530
|
-
discard_session: bool,
|
|
1531
|
-
open_display: bool,
|
|
1532
|
-
team: Option<&str>,
|
|
1533
|
-
transport: &dyn crate::transport::Transport,
|
|
1534
|
-
) -> Result<ResetAgentOutcome, LifecycleError> {
|
|
1535
|
-
if !discard_session {
|
|
1536
|
-
return Ok(ResetAgentOutcome::Refused {
|
|
1537
|
-
reason: ResetRefusal::DiscardSessionRequired,
|
|
1538
|
-
});
|
|
1539
|
-
}
|
|
1540
|
-
let paths = lifecycle_paths(workspace, team)?;
|
|
1541
|
-
let canonical_team = paths.canonical_team(team).map(str::to_string);
|
|
1542
|
-
let team = canonical_team.as_deref();
|
|
1543
|
-
let _lock = acquire_agent_lifecycle_lock(LifecycleLockRequest {
|
|
1544
|
-
workspace: &paths.run_workspace,
|
|
1545
|
-
operation: "reset-agent",
|
|
1546
|
-
team,
|
|
1547
|
-
agent_id: Some(agent_id),
|
|
1548
|
-
})?;
|
|
1549
|
-
reset_agent_at_paths(
|
|
1550
|
-
&paths.run_workspace,
|
|
1551
|
-
&paths.spec_workspace,
|
|
1552
|
-
agent_id,
|
|
1553
|
-
discard_session,
|
|
1554
|
-
open_display,
|
|
1555
|
-
team,
|
|
1556
|
-
transport,
|
|
1557
|
-
)
|
|
1558
|
-
}
|
|
1559
|
-
|
|
1560
|
-
fn reset_agent_at_paths(
|
|
1561
|
-
workspace: &Path,
|
|
1562
|
-
spec_workspace: &Path,
|
|
1563
|
-
agent_id: &AgentId,
|
|
1564
|
-
discard_session: bool,
|
|
1565
|
-
open_display: bool,
|
|
1566
|
-
team: Option<&str>,
|
|
1567
|
-
transport: &dyn crate::transport::Transport,
|
|
1568
|
-
) -> Result<ResetAgentOutcome, LifecycleError> {
|
|
1569
|
-
if !discard_session {
|
|
1570
|
-
return Ok(ResetAgentOutcome::Refused {
|
|
1571
|
-
reason: ResetRefusal::DiscardSessionRequired,
|
|
1572
|
-
});
|
|
1573
|
-
}
|
|
1574
|
-
// golden operations.py:105-110: team-scope resolve + owner gate BEFORE the nested stop.
|
|
1575
|
-
let state_before_stop = resolve_team_scoped_state_or_refuse(workspace, team)?;
|
|
1576
|
-
let discarded_session_id = state_before_stop
|
|
1577
|
-
.get("agents")
|
|
1578
|
-
.and_then(|v| v.get(agent_id.as_str()))
|
|
1579
|
-
.and_then(|v| v.get("session_id"))
|
|
1580
|
-
.and_then(|v| v.as_str())
|
|
1581
|
-
.filter(|session| !session.is_empty())
|
|
1582
|
-
.map(SessionId::new);
|
|
1583
|
-
crate::lifecycle::launch::ensure_owner_allowed_for_state(&state_before_stop, Some(agent_id))?;
|
|
1584
|
-
// Capture old pane_id / pane_pid / window BEFORE stop, so the hard gate
|
|
1585
|
-
// below can prove the same prior instance is gone (or refuse start).
|
|
1586
|
-
let old_pane_id_before = state_before_stop
|
|
1587
|
-
.get("agents")
|
|
1588
|
-
.and_then(|v| v.get(agent_id.as_str()))
|
|
1589
|
-
.and_then(|v| v.get("pane_id"))
|
|
1590
|
-
.and_then(|v| v.as_str())
|
|
1591
|
-
.filter(|p| !p.is_empty())
|
|
1592
|
-
.map(crate::transport::PaneId::new);
|
|
1593
|
-
let old_pane_pid_before = state_pane_pid(&state_before_stop, agent_id);
|
|
1594
|
-
let old_pane_live_before = old_pane_id_before
|
|
1595
|
-
.as_ref()
|
|
1596
|
-
.map(|pane| agent_pane_live_by_id(transport, pane))
|
|
1597
|
-
.unwrap_or(false);
|
|
1598
|
-
// CR C-2: take ONE pre-stop snapshot of the team session's panes so
|
|
1599
|
-
// the gate below can compute "what survived stop" by set difference,
|
|
1600
|
-
// not "what panes exist at all" (which would refuse legitimate
|
|
1601
|
-
// reset flows where stop killed the pane and the post-stop snapshot
|
|
1602
|
-
// includes that same pane id in a transport mock that does not
|
|
1603
|
-
// reflect kill removal).
|
|
1604
|
-
let pre_stop_window = state_before_stop
|
|
1605
|
-
.get("agents")
|
|
1606
|
-
.and_then(|v| v.get(agent_id.as_str()))
|
|
1607
|
-
.and_then(|v| v.get("window"))
|
|
1608
|
-
.and_then(|v| v.as_str())
|
|
1609
|
-
.filter(|s| !s.is_empty())
|
|
1610
|
-
.unwrap_or_else(|| agent_id.as_str())
|
|
1611
|
-
.to_string();
|
|
1612
|
-
let pre_stop_spec = load_team_spec(spec_workspace).ok();
|
|
1613
|
-
let pre_stop_session = pre_stop_spec
|
|
1614
|
-
.as_ref()
|
|
1615
|
-
.map(|spec| state_session_name_from_spec(&state_before_stop, spec));
|
|
1616
|
-
let pre_stop_pane_ids: std::collections::BTreeSet<String> =
|
|
1617
|
-
if let Some(session) = pre_stop_session.as_ref() {
|
|
1618
|
-
if is_per_agent_window(&pre_stop_window, agent_id) {
|
|
1619
|
-
list_same_role_panes(transport, session, &pre_stop_window)
|
|
1620
|
-
.into_iter()
|
|
1621
|
-
.filter(|pane| {
|
|
1622
|
-
if !old_pane_live_before {
|
|
1623
|
-
return true;
|
|
1624
|
-
}
|
|
1625
|
-
let same_old_pane = old_pane_id_before
|
|
1626
|
-
.as_ref()
|
|
1627
|
-
.is_some_and(|old| pane.pane_id.as_str() == old.as_str());
|
|
1628
|
-
let same_old_pid = old_pane_pid_before
|
|
1629
|
-
.is_some_and(|old_pid| pane.pane_pid == Some(old_pid));
|
|
1630
|
-
same_old_pane || same_old_pid
|
|
1631
|
-
})
|
|
1632
|
-
.map(|p| p.pane_id.as_str().to_string())
|
|
1633
|
-
.collect()
|
|
1634
|
-
} else {
|
|
1635
|
-
std::collections::BTreeSet::new()
|
|
1636
|
-
}
|
|
1637
|
-
} else {
|
|
1638
|
-
std::collections::BTreeSet::new()
|
|
1639
|
-
};
|
|
1640
|
-
let stop = stop_agent_at_paths(workspace, spec_workspace, agent_id, team, transport)?;
|
|
1641
|
-
|
|
1642
|
-
// 0.4.10+ paused agent skip: a paused agent's start path returns
|
|
1643
|
-
// StartAgentOutcome::Paused (no spawn). There is no duplicate-window
|
|
1644
|
-
// hazard, so the gate is a no-op for paused agents.
|
|
1645
|
-
let agent_is_paused = state_before_stop
|
|
1646
|
-
.get("agents")
|
|
1647
|
-
.and_then(|v| v.get(agent_id.as_str()))
|
|
1648
|
-
.and_then(|v| v.get("paused"))
|
|
1649
|
-
.and_then(|v| v.as_bool())
|
|
1650
|
-
.unwrap_or(false);
|
|
1651
|
-
// 0.4.10+ reset duplicate-window fix (plan §3): HARD GATE before start.
|
|
1652
|
-
// After stop_agent_at_paths returns, prove the old instance is gone OR
|
|
1653
|
-
// refuse to spawn. The pre-fix path unconditionally proceeded to
|
|
1654
|
-
// discard/save/start even when stop returned stopped=false (stop's
|
|
1655
|
-
// pane_id was stale), creating the observed duplicate-window pattern.
|
|
1656
|
-
//
|
|
1657
|
-
// Residue definition (correct: differential, not absolute):
|
|
1658
|
-
// A pane is RESIDUE iff it appears in BOTH the pre-stop snapshot
|
|
1659
|
-
// AND the post-stop snapshot. The pre-fix attempt used "any
|
|
1660
|
-
// matching pane exists post-stop" which broke legitimate flows
|
|
1661
|
-
// where the transport mock does not model kill_pane removal.
|
|
1662
|
-
//
|
|
1663
|
-
// Old pane id / pid checks:
|
|
1664
|
-
// The OLD stored pane_id / pane_pid must be gone (not just
|
|
1665
|
-
// reachable but actually killed). For real tmux this is the
|
|
1666
|
-
// structural truth source; for mocks the differential approach
|
|
1667
|
-
// above covers the post-stop visibility.
|
|
1668
|
-
//
|
|
1669
|
-
// CR C-5: gate is reset-specific; standalone stop-agent path keeps
|
|
1670
|
-
// existing "already absent is ok" behavior.
|
|
1671
|
-
//
|
|
1672
|
-
// P0 cohort proof: every non-paused reset takes a post-stop
|
|
1673
|
-
// same-role snapshot. Refuse only on tmux-visible residue; a
|
|
1674
|
-
// standalone old-pane liveness probe can be stale in mocks and
|
|
1675
|
-
// must not mask the later spawn ownership/window-disappeared
|
|
1676
|
-
// verifier.
|
|
1677
|
-
if !agent_is_paused {
|
|
1678
|
-
let spec_for_gate = load_team_spec(spec_workspace)?;
|
|
1679
|
-
let gate_state = resolve_team_scoped_state_or_refuse(workspace, team)?;
|
|
1680
|
-
let session_name_gate = state_session_name_from_spec(&gate_state, &spec_for_gate);
|
|
1681
|
-
let gate_window = gate_state
|
|
1682
|
-
.get("agents")
|
|
1683
|
-
.and_then(|v| v.get(agent_id.as_str()))
|
|
1684
|
-
.and_then(|v| v.get("window"))
|
|
1685
|
-
.and_then(|v| v.as_str())
|
|
1686
|
-
.filter(|s| !s.is_empty())
|
|
1687
|
-
.unwrap_or_else(|| agent_id.as_str())
|
|
1688
|
-
.to_string();
|
|
1689
|
-
// Take a SECOND snapshot post-stop and compute the differential.
|
|
1690
|
-
// Only panes present in BOTH snapshots are residue (stop did not
|
|
1691
|
-
// remove them).
|
|
1692
|
-
let post_stop_panes: Vec<crate::transport::PaneInfo> =
|
|
1693
|
-
if is_per_agent_window(&gate_window, agent_id) {
|
|
1694
|
-
list_same_role_panes(transport, &session_name_gate, &gate_window)
|
|
1695
|
-
} else {
|
|
1696
|
-
Vec::new()
|
|
1697
|
-
};
|
|
1698
|
-
let remaining_panes: Vec<crate::transport::PaneInfo> = post_stop_panes
|
|
1699
|
-
.into_iter()
|
|
1700
|
-
.filter(|p| pre_stop_pane_ids.contains(p.pane_id.as_str()))
|
|
1701
|
-
.collect();
|
|
1702
|
-
// Pid-alone aliveness is secondary evidence and noisy under
|
|
1703
|
-
// fixtures. Block only on tmux-visible same-role residue; record
|
|
1704
|
-
// the old pid in the event for diagnostics.
|
|
1705
|
-
if !remaining_panes.is_empty() {
|
|
1706
|
-
let remaining_pane_ids: Vec<String> = remaining_panes
|
|
1707
|
-
.iter()
|
|
1708
|
-
.map(|p| p.pane_id.as_str().to_string())
|
|
1709
|
-
.collect();
|
|
1710
|
-
let old_pane_str = old_pane_id_before
|
|
1711
|
-
.as_ref()
|
|
1712
|
-
.map(|p| p.as_str().to_string())
|
|
1713
|
-
.unwrap_or_default();
|
|
1714
|
-
let old_pid_val = old_pane_pid_before.unwrap_or(0);
|
|
1715
|
-
let _ = write_reset_stop_not_proven_event(
|
|
1716
|
-
workspace,
|
|
1717
|
-
agent_id,
|
|
1718
|
-
&old_pane_str,
|
|
1719
|
-
old_pid_val,
|
|
1720
|
-
&remaining_pane_ids,
|
|
1721
|
-
);
|
|
1722
|
-
// CR C-1 N38 three-line error: error / action / log_hint.
|
|
1723
|
-
let _ = stop; // silence unused on the refusal path
|
|
1724
|
-
return Err(LifecycleError::RequirementUnmet(format!(
|
|
1725
|
-
"reset refused: old agent instance still live for {agent_id}\n\
|
|
1726
|
-
action: stop the worker manually with `team-agent stop-agent {agent_id} --team <team>` then retry reset, or kill the residual tmux panes [{ids}]\n\
|
|
1727
|
-
log_hint: see reset_agent.stop_not_proven event (old_pane_id={old}, old_pane_pid={pid}, remaining_panes=[{ids}])",
|
|
1728
|
-
agent_id = agent_id.as_str(),
|
|
1729
|
-
ids = remaining_pane_ids.join(", "),
|
|
1730
|
-
old = old_pane_str,
|
|
1731
|
-
pid = old_pid_val,
|
|
1732
|
-
)));
|
|
1733
|
-
}
|
|
1734
|
-
}
|
|
1735
|
-
|
|
1736
|
-
let mut state = resolve_team_scoped_state_or_refuse(workspace, team)?;
|
|
1737
|
-
let spec = load_team_spec(spec_workspace)?;
|
|
1738
|
-
discard_agent_session_fields(&mut state, agent_id)?;
|
|
1739
|
-
// 0.4.6 Stage 1: bump spawn_epoch on every reset. The new epoch is
|
|
1740
|
-
// visible to subsequent capture/event/status paths so they can
|
|
1741
|
-
// attribute observations to the current process cohort (not a stale
|
|
1742
|
-
// prior fresh attempt).
|
|
1743
|
-
let new_epoch = next_spawn_epoch(&state, agent_id);
|
|
1744
|
-
if let Some(obj) = state
|
|
1745
|
-
.get_mut("agents")
|
|
1746
|
-
.and_then(serde_json::Value::as_object_mut)
|
|
1747
|
-
.and_then(|agents| agents.get_mut(agent_id.as_str()))
|
|
1748
|
-
.and_then(serde_json::Value::as_object_mut)
|
|
1749
|
-
{
|
|
1750
|
-
obj.insert("spawn_epoch".to_string(), serde_json::json!(new_epoch));
|
|
1751
|
-
obj.insert("explicit_stop".to_string(), serde_json::Value::Bool(false));
|
|
1752
|
-
}
|
|
1753
|
-
let team_key = restart_projection_team_key(&state, team);
|
|
1754
|
-
sync_restart_team_projections(&mut state, &team_key);
|
|
1755
|
-
// golden operations.py (reset): save_team_scoped_state on the team projection — same multi-team
|
|
1756
|
-
// preservation as stop, not a raw save_runtime_state.
|
|
1757
|
-
crate::state::repository::StateRepository::new(workspace).save(
|
|
1758
|
-
crate::state::repository::StateWriteIntent::ResetAgent {
|
|
1759
|
-
team_key: &team_key,
|
|
1760
|
-
agent_id: agent_id.as_str(),
|
|
1761
|
-
discard_session,
|
|
1762
|
-
},
|
|
1763
|
-
&state,
|
|
1764
|
-
)
|
|
1765
|
-
.map_err(|e| LifecycleError::StatePersist(e.to_string()))?;
|
|
1766
|
-
// golden operations.py:125: write_team_state after the discard-save (the intermediate stopped snapshot).
|
|
1767
|
-
write_team_state(spec_workspace, &spec, &state)?;
|
|
1768
|
-
write_reset_tombstone_event(
|
|
1769
|
-
workspace,
|
|
1770
|
-
agent_id,
|
|
1771
|
-
discarded_session_id
|
|
1772
|
-
.as_ref()
|
|
1773
|
-
.map(SessionId::as_str)
|
|
1774
|
-
.unwrap_or(""),
|
|
1775
|
-
)?;
|
|
1776
|
-
let start = start_agent_at_paths(
|
|
1777
|
-
workspace,
|
|
1778
|
-
spec_workspace,
|
|
1779
|
-
agent_id,
|
|
1780
|
-
true,
|
|
1781
|
-
open_display,
|
|
1782
|
-
true,
|
|
1783
|
-
team,
|
|
1784
|
-
transport,
|
|
1785
|
-
)?;
|
|
1786
|
-
let started = matches!(start, StartAgentOutcome::Running { .. });
|
|
1787
|
-
write_reset_complete_event(workspace, agent_id, stop.stopped, started)?;
|
|
1788
|
-
let (capture_state, reset_proof, weak_reset_warning) =
|
|
1789
|
-
reset_capture_proof(workspace, agent_id, discarded_session_id.as_ref());
|
|
1790
|
-
match start {
|
|
1791
|
-
StartAgentOutcome::Running {
|
|
1792
|
-
env,
|
|
1793
|
-
start_mode,
|
|
1794
|
-
session_id,
|
|
1795
|
-
new_session_id,
|
|
1796
|
-
..
|
|
1797
|
-
} => {
|
|
1798
|
-
let output_session_id = if matches!(
|
|
1799
|
-
start_mode,
|
|
1800
|
-
StartMode::Fresh | StartMode::FreshAfterMissingRollout
|
|
1801
|
-
) {
|
|
1802
|
-
new_session_id.clone().or(session_id)
|
|
1803
|
-
} else {
|
|
1804
|
-
session_id
|
|
1805
|
-
};
|
|
1806
|
-
Ok(ResetAgentOutcome::Reset {
|
|
1807
|
-
env,
|
|
1808
|
-
start_mode,
|
|
1809
|
-
discarded_session_id,
|
|
1810
|
-
session_id: output_session_id,
|
|
1811
|
-
new_session_id,
|
|
1812
|
-
capture_state,
|
|
1813
|
-
reset_proof,
|
|
1814
|
-
weak_reset_warning,
|
|
1815
|
-
})
|
|
1816
|
-
}
|
|
1817
|
-
StartAgentOutcome::Noop { env, .. } => Ok(ResetAgentOutcome::Reset {
|
|
1818
|
-
env,
|
|
1819
|
-
start_mode: StartMode::Noop,
|
|
1820
|
-
discarded_session_id,
|
|
1821
|
-
session_id: None,
|
|
1822
|
-
new_session_id: None,
|
|
1823
|
-
capture_state,
|
|
1824
|
-
reset_proof,
|
|
1825
|
-
weak_reset_warning,
|
|
1826
|
-
}),
|
|
1827
|
-
StartAgentOutcome::Paused { .. } => Ok(ResetAgentOutcome::Reset {
|
|
1828
|
-
env: AgentActionEnvelope {
|
|
1829
|
-
agent_id: agent_id.clone(),
|
|
1830
|
-
state_file: crate::state::persist::runtime_state_path(workspace),
|
|
1831
|
-
coordinator_started: false,
|
|
1832
|
-
},
|
|
1833
|
-
start_mode: StartMode::Noop,
|
|
1834
|
-
discarded_session_id,
|
|
1835
|
-
session_id: None,
|
|
1836
|
-
new_session_id: None,
|
|
1837
|
-
capture_state,
|
|
1838
|
-
reset_proof,
|
|
1839
|
-
weak_reset_warning,
|
|
1840
|
-
}),
|
|
1841
|
-
}
|
|
1842
|
-
}
|
|
1843
|
-
|
|
1844
|
-
fn reset_capture_proof(
|
|
1845
|
-
workspace: &Path,
|
|
1846
|
-
agent_id: &AgentId,
|
|
1847
|
-
discarded_session_id: Option<&SessionId>,
|
|
1848
|
-
) -> (String, String, Option<String>) {
|
|
1849
|
-
let state = crate::state::persist::load_runtime_state(workspace)
|
|
1850
|
-
.unwrap_or_else(|_| serde_json::json!({}));
|
|
1851
|
-
let agent = state
|
|
1852
|
-
.get("agents")
|
|
1853
|
-
.and_then(|agents| agents.get(agent_id.as_str()));
|
|
1854
|
-
let capture_state = agent
|
|
1855
|
-
.and_then(|agent| agent.get("capture_state"))
|
|
1856
|
-
.and_then(serde_json::Value::as_str)
|
|
1857
|
-
.filter(|value| !value.is_empty())
|
|
1858
|
-
.or_else(|| {
|
|
1859
|
-
agent
|
|
1860
|
-
.and_then(|agent| agent.get("attribution_ambiguous"))
|
|
1861
|
-
.and_then(serde_json::Value::as_bool)
|
|
1862
|
-
.filter(|ambiguous| *ambiguous)
|
|
1863
|
-
.map(|_| "attribution_ambiguous")
|
|
1864
|
-
})
|
|
1865
|
-
.or_else(|| {
|
|
1866
|
-
let has_session = agent
|
|
1867
|
-
.and_then(|agent| agent.get("session_id"))
|
|
1868
|
-
.and_then(serde_json::Value::as_str)
|
|
1869
|
-
.is_some_and(|value| !value.is_empty());
|
|
1870
|
-
let has_rollout = agent
|
|
1871
|
-
.and_then(|agent| agent.get("rollout_path"))
|
|
1872
|
-
.and_then(serde_json::Value::as_str)
|
|
1873
|
-
.is_some_and(|value| !value.is_empty());
|
|
1874
|
-
(has_session && has_rollout).then_some("captured")
|
|
1875
|
-
})
|
|
1876
|
-
.unwrap_or("transcript_missing")
|
|
1877
|
-
.to_string();
|
|
1878
|
-
let weak = discarded_session_id.is_none()
|
|
1879
|
-
|| matches!(
|
|
1880
|
-
capture_state.as_str(),
|
|
1881
|
-
"transcript_missing" | "attribution_ambiguous"
|
|
1882
|
-
);
|
|
1883
|
-
let reset_proof = if weak { "weak" } else { "strong" }.to_string();
|
|
1884
|
-
let weak_reset_warning = weak.then(|| {
|
|
1885
|
-
format!(
|
|
1886
|
-
"weak reset proof: capture_state={capture_state}; lifecycle restarted but attribution did not prove a fresh transcript"
|
|
1887
|
-
)
|
|
1888
|
-
});
|
|
1889
|
-
(capture_state, reset_proof, weak_reset_warning)
|
|
1890
|
-
}
|
|
1891
|
-
|
|
1892
|
-
#[allow(clippy::too_many_arguments)]
|
|
1893
|
-
fn write_start_agent_start_event(
|
|
1894
|
-
workspace: &Path,
|
|
1895
|
-
agent_id: &AgentId,
|
|
1896
|
-
agent: &serde_json::Value,
|
|
1897
|
-
spawn_plan: &crate::provider::CommandPlan,
|
|
1898
|
-
provider: crate::provider::Provider,
|
|
1899
|
-
start_mode: StartMode,
|
|
1900
|
-
session_name: &SessionName,
|
|
1901
|
-
window: &str,
|
|
1902
|
-
session_id: Option<&SessionId>,
|
|
1903
|
-
tmux_start_mode: &'static str,
|
|
1904
|
-
) -> Result<(), LifecycleError> {
|
|
1905
|
-
crate::event_log::EventLog::new(workspace)
|
|
1906
|
-
.write(
|
|
1907
|
-
"start_agent.agent_start",
|
|
1908
|
-
serde_json::json!({
|
|
1909
|
-
"agent_id": agent_id.as_str(),
|
|
1910
|
-
"provider": provider_wire(provider),
|
|
1911
|
-
"start_mode": start_mode,
|
|
1912
|
-
"session_id": session_id.map(|s| s.as_str()),
|
|
1913
|
-
"session": session_name.as_str(),
|
|
1914
|
-
"window": window,
|
|
1915
|
-
"tmux_start_mode": tmux_start_mode,
|
|
1916
|
-
"command": spawn_plan.argv,
|
|
1917
|
-
"mcp_config": agent.get("mcp_config").cloned().unwrap_or(serde_json::Value::Null),
|
|
1918
|
-
}),
|
|
1919
|
-
)
|
|
1920
|
-
.map_err(|e| LifecycleError::StatePersist(e.to_string()))?;
|
|
1921
|
-
Ok(())
|
|
1922
|
-
}
|
|
1923
|
-
|
|
1924
|
-
fn write_stop_complete_event(
|
|
1925
|
-
workspace: &Path,
|
|
1926
|
-
agent_id: &AgentId,
|
|
1927
|
-
target: &str,
|
|
1928
|
-
stopped: bool,
|
|
1929
|
-
) -> Result<(), LifecycleError> {
|
|
1930
|
-
crate::event_log::EventLog::new(workspace)
|
|
1931
|
-
.write(
|
|
1932
|
-
"stop_agent.complete",
|
|
1933
|
-
serde_json::json!({
|
|
1934
|
-
"agent_id": agent_id.as_str(),
|
|
1935
|
-
"target": target,
|
|
1936
|
-
"stopped": stopped,
|
|
1937
|
-
}),
|
|
1938
|
-
)
|
|
1939
|
-
.map_err(|e| LifecycleError::StatePersist(e.to_string()))?;
|
|
1940
|
-
Ok(())
|
|
1941
|
-
}
|
|
1942
|
-
|
|
1943
|
-
/// ---
|
|
1944
|
-
/// purpose: 写一条停席排空证据事件
|
|
1945
|
-
/// params:
|
|
1946
|
-
/// target: 被停的目标描述
|
|
1947
|
-
/// drain: 排空证据载荷,其字段被并进事件顶层
|
|
1948
|
-
/// returns: 成功返回空值
|
|
1949
|
-
/// errors: 事件写入失败时返回 StatePersist
|
|
1950
|
-
/// ---
|
|
1951
|
-
/// 0.4.6 Stage 1: drain evidence for the old pane+pid after stop. Fires
|
|
1952
|
-
/// during reset/stop so operators can see whether the prior worker really
|
|
1953
|
-
/// went away or stuck around inside the resp budget.
|
|
1954
|
-
pub(super) fn write_stop_drain_event(
|
|
1955
|
-
workspace: &Path,
|
|
1956
|
-
agent_id: &AgentId,
|
|
1957
|
-
target: &str,
|
|
1958
|
-
drain: &serde_json::Value,
|
|
1959
|
-
) -> Result<(), LifecycleError> {
|
|
1960
|
-
let mut payload = serde_json::Map::new();
|
|
1961
|
-
payload.insert("agent_id".to_string(), serde_json::json!(agent_id.as_str()));
|
|
1962
|
-
payload.insert("target".to_string(), serde_json::json!(target));
|
|
1963
|
-
if let Some(obj) = drain.as_object() {
|
|
1964
|
-
for (k, v) in obj {
|
|
1965
|
-
payload.insert(k.clone(), v.clone());
|
|
1966
|
-
}
|
|
1967
|
-
}
|
|
1968
|
-
crate::event_log::EventLog::new(workspace)
|
|
1969
|
-
.write("stop_agent.drain", serde_json::Value::Object(payload))
|
|
1970
|
-
.map_err(|e| LifecycleError::StatePersist(e.to_string()))?;
|
|
1971
|
-
Ok(())
|
|
1972
|
-
}
|
|
1973
|
-
|
|
1974
|
-
fn write_stop_window_failed_event(
|
|
1975
|
-
workspace: &Path,
|
|
1976
|
-
agent_id: &AgentId,
|
|
1977
|
-
target: &str,
|
|
1978
|
-
stderr: &str,
|
|
1979
|
-
) -> Result<(), LifecycleError> {
|
|
1980
|
-
crate::event_log::EventLog::new(workspace)
|
|
1981
|
-
.write(
|
|
1982
|
-
"stop_agent.window_stop_failed",
|
|
1983
|
-
serde_json::json!({
|
|
1984
|
-
"agent_id": agent_id.as_str(),
|
|
1985
|
-
"target": target,
|
|
1986
|
-
"stderr": stderr,
|
|
1987
|
-
}),
|
|
1988
|
-
)
|
|
1989
|
-
.map_err(|e| LifecycleError::StatePersist(e.to_string()))?;
|
|
1990
|
-
Ok(())
|
|
1991
|
-
}
|
|
1992
|
-
|
|
1993
|
-
fn write_reset_tombstone_event(
|
|
1994
|
-
workspace: &Path,
|
|
1995
|
-
agent_id: &AgentId,
|
|
1996
|
-
discarded_session_id: &str,
|
|
1997
|
-
) -> Result<(), LifecycleError> {
|
|
1998
|
-
crate::event_log::EventLog::new(workspace)
|
|
1999
|
-
.write(
|
|
2000
|
-
"discard.session_tombstone",
|
|
2001
|
-
serde_json::json!({
|
|
2002
|
-
"agent_id": agent_id.as_str(),
|
|
2003
|
-
"discarded_session_id": discarded_session_id,
|
|
2004
|
-
}),
|
|
2005
|
-
)
|
|
2006
|
-
.map_err(|e| LifecycleError::StatePersist(e.to_string()))?;
|
|
2007
|
-
Ok(())
|
|
2008
|
-
}
|
|
2009
|
-
|
|
2010
|
-
/// 0.4.10+ reset duplicate-window fix (plan §3): emit a structured event
|
|
2011
|
-
/// when the reset hard gate refuses to start because the old instance is
|
|
2012
|
-
/// proven still live (old pane id reachable, old pane pid alive, or
|
|
2013
|
-
/// same-role panes remain in the team session).
|
|
2014
|
-
fn write_reset_stop_not_proven_event(
|
|
2015
|
-
workspace: &Path,
|
|
2016
|
-
agent_id: &AgentId,
|
|
2017
|
-
old_pane_id: &str,
|
|
2018
|
-
old_pane_pid: u32,
|
|
2019
|
-
remaining_panes: &[String],
|
|
2020
|
-
) -> Result<(), LifecycleError> {
|
|
2021
|
-
crate::event_log::EventLog::new(workspace)
|
|
2022
|
-
.write(
|
|
2023
|
-
"reset_agent.stop_not_proven",
|
|
2024
|
-
serde_json::json!({
|
|
2025
|
-
"agent_id": agent_id.as_str(),
|
|
2026
|
-
"old_pane_id": old_pane_id,
|
|
2027
|
-
"old_pane_pid": old_pane_pid,
|
|
2028
|
-
"remaining_panes": remaining_panes,
|
|
2029
|
-
"action": "stop the worker manually then retry reset, or kill the residual tmux panes",
|
|
2030
|
-
}),
|
|
2031
|
-
)
|
|
2032
|
-
.map_err(|e| LifecycleError::StatePersist(e.to_string()))?;
|
|
2033
|
-
Ok(())
|
|
2034
|
-
}
|
|
2035
|
-
|
|
2036
|
-
fn write_reset_complete_event(
|
|
2037
|
-
workspace: &Path,
|
|
2038
|
-
agent_id: &AgentId,
|
|
2039
|
-
stopped: bool,
|
|
2040
|
-
started: bool,
|
|
2041
|
-
) -> Result<(), LifecycleError> {
|
|
2042
|
-
crate::event_log::EventLog::new(workspace)
|
|
2043
|
-
.write(
|
|
2044
|
-
"reset_agent.complete",
|
|
2045
|
-
serde_json::json!({
|
|
2046
|
-
"agent_id": agent_id.as_str(),
|
|
2047
|
-
"stopped": stopped,
|
|
2048
|
-
"started": started,
|
|
2049
|
-
}),
|
|
2050
|
-
)
|
|
2051
|
-
.map_err(|e| LifecycleError::StatePersist(e.to_string()))?;
|
|
2052
|
-
Ok(())
|
|
2053
|
-
}
|
|
2054
|
-
|
|
2055
|
-
/// 0.5.66 bypass 单源 §2.6:声明面(file)与运行面(as-launched)是否 drift。
|
|
2056
|
-
/// 缺 as-launched(从未 spawn)= 视为未启动,不 drift(首次 Noop 无进程可比)。
|
|
2057
|
-
fn check_bypass_drift(file: Option<bool>, launched: Option<bool>) -> bool {
|
|
2058
|
-
match (file, launched) {
|
|
2059
|
-
(Some(file), Some(launched)) => file != launched,
|
|
2060
|
-
_ => false,
|
|
2061
|
-
}
|
|
2062
|
-
}
|
|
2063
|
-
|
|
2064
|
-
#[cfg(test)]
|
|
2065
|
-
|
|
2066
|
-
#[cfg(test)]
|
|
2067
|
-
mod tests {
|
|
2068
|
-
use super::*;
|
|
2069
|
-
|
|
2070
|
-
#[test]
|
|
2071
|
-
fn e51_restart_allows_leader_same_pane_id_on_different_tmux_sockets() {
|
|
2072
|
-
let state = serde_json::json!({
|
|
2073
|
-
"tmux_endpoint": "/private/tmp/tmux-501/ta-worker",
|
|
2074
|
-
"leader_receiver": {
|
|
2075
|
-
"pane_id": "%0",
|
|
2076
|
-
"tmux_socket": "/private/tmp/tmux-501/default"
|
|
2077
|
-
},
|
|
2078
|
-
"agents": {
|
|
2079
|
-
"architect": {
|
|
2080
|
-
"pane_id": "%0",
|
|
2081
|
-
"tmux_socket": "/private/tmp/tmux-501/ta-worker"
|
|
2082
|
-
}
|
|
2083
|
-
}
|
|
2084
|
-
});
|
|
2085
|
-
let agent_id = AgentId::new("architect");
|
|
2086
|
-
let agent = state["agents"]["architect"].clone();
|
|
2087
|
-
|
|
2088
|
-
assert!(
|
|
2089
|
-
!pane_conflicts_with_leader_or_other(&state, &agent_id, &agent),
|
|
2090
|
-
"same pane id on different tmux sockets must not force a fresh restart"
|
|
2091
|
-
);
|
|
2092
|
-
}
|
|
2093
|
-
|
|
2094
|
-
#[test]
|
|
2095
|
-
fn e51_restart_keeps_leader_conflict_on_same_tmux_socket() {
|
|
2096
|
-
let socket = "/private/tmp/tmux-501/default";
|
|
2097
|
-
let state = serde_json::json!({
|
|
2098
|
-
"tmux_endpoint": socket,
|
|
2099
|
-
"leader_receiver": {
|
|
2100
|
-
"pane_id": "%0",
|
|
2101
|
-
"tmux_socket": socket
|
|
2102
|
-
},
|
|
2103
|
-
"agents": {
|
|
2104
|
-
"architect": {
|
|
2105
|
-
"pane_id": "%0",
|
|
2106
|
-
"tmux_socket": socket
|
|
2107
|
-
}
|
|
2108
|
-
}
|
|
2109
|
-
});
|
|
2110
|
-
let agent_id = AgentId::new("architect");
|
|
2111
|
-
let agent = state["agents"]["architect"].clone();
|
|
2112
|
-
|
|
2113
|
-
assert!(
|
|
2114
|
-
pane_conflicts_with_leader_or_other(&state, &agent_id, &agent),
|
|
2115
|
-
"same pane id on the same tmux socket must keep the E51 guard"
|
|
2116
|
-
);
|
|
2117
|
-
}
|
|
2118
|
-
|
|
2119
|
-
#[test]
|
|
2120
|
-
fn e51_restart_allows_other_agent_same_pane_id_on_different_tmux_sockets() {
|
|
2121
|
-
let state = serde_json::json!({
|
|
2122
|
-
"tmux_endpoint": "/private/tmp/tmux-501/ta-worker",
|
|
2123
|
-
"agents": {
|
|
2124
|
-
"architect": {
|
|
2125
|
-
"pane_id": "%0",
|
|
2126
|
-
"tmux_socket": "/private/tmp/tmux-501/ta-worker"
|
|
2127
|
-
},
|
|
2128
|
-
"reviewer": {
|
|
2129
|
-
"pane_id": "%0",
|
|
2130
|
-
"tmux_socket": "/private/tmp/tmux-501/default"
|
|
2131
|
-
}
|
|
2132
|
-
}
|
|
2133
|
-
});
|
|
2134
|
-
let agent_id = AgentId::new("architect");
|
|
2135
|
-
let agent = state["agents"]["architect"].clone();
|
|
2136
|
-
|
|
2137
|
-
assert!(
|
|
2138
|
-
!pane_conflicts_with_leader_or_other(&state, &agent_id, &agent),
|
|
2139
|
-
"other-agent collision checks must also include tmux_socket"
|
|
2140
|
-
);
|
|
2141
|
-
}
|
|
2142
|
-
|
|
2143
|
-
#[test]
|
|
2144
|
-
fn e51_restart_rejects_discovered_peer_pane_for_stale_worker() {
|
|
2145
|
-
let socket = "/private/tmp/tmux-501/ta-worker";
|
|
2146
|
-
let state = serde_json::json!({
|
|
2147
|
-
"tmux_endpoint": socket,
|
|
2148
|
-
"agents": {
|
|
2149
|
-
"worker": {"pane_id": "%1", "status": "running"},
|
|
2150
|
-
"peer": {"pane_id": "%0", "status": "running"}
|
|
2151
|
-
}
|
|
2152
|
-
});
|
|
2153
|
-
let agent_id = AgentId::new("worker");
|
|
2154
|
-
let agent = state["agents"]["worker"].clone();
|
|
2155
|
-
|
|
2156
|
-
assert!(
|
|
2157
|
-
pane_conflicts_with_pane_id(&state, &agent_id, &agent, "%0"),
|
|
2158
|
-
"a discovered live pane owned by a running peer must not be rebound"
|
|
2159
|
-
);
|
|
2160
|
-
}
|
|
2161
|
-
|
|
2162
|
-
#[test]
|
|
2163
|
-
fn e51_restart_ignores_stopped_peer_pane_residue() {
|
|
2164
|
-
let socket = "/private/tmp/tmux-501/ta-worker";
|
|
2165
|
-
let state = serde_json::json!({
|
|
2166
|
-
"tmux_endpoint": socket,
|
|
2167
|
-
"agents": {
|
|
2168
|
-
"worker": {"pane_id": "%1", "status": "running"},
|
|
2169
|
-
"peer": {"pane_id": "%0", "status": "stopped"}
|
|
2170
|
-
}
|
|
2171
|
-
});
|
|
2172
|
-
let agent_id = AgentId::new("worker");
|
|
2173
|
-
let agent = state["agents"]["worker"].clone();
|
|
2174
|
-
|
|
2175
|
-
assert!(
|
|
2176
|
-
!pane_conflicts_with_pane_id(&state, &agent_id, &agent, "%0"),
|
|
2177
|
-
"stopped peer residue must not claim a pane for a live restart"
|
|
2178
|
-
);
|
|
2179
|
-
}
|
|
2180
|
-
|
|
2181
|
-
// 0.5.66 bypass 单源 §4.1:Noop drift 检测——声明面与运行面不一致 → drift。
|
|
2182
|
-
#[test]
|
|
2183
|
-
fn test_restart_noop_denied_on_drift() {
|
|
2184
|
-
// file=true, launched=false → drift(改了 md 升 bypass 但进程没带)。
|
|
2185
|
-
assert!(check_bypass_drift(Some(true), Some(false)));
|
|
2186
|
-
// file=false, launched=true → drift(改了 md 撤 bypass 但进程还带着)。
|
|
2187
|
-
assert!(check_bypass_drift(Some(false), Some(true)));
|
|
2188
|
-
}
|
|
2189
|
-
|
|
2190
|
-
// 0.5.66 bypass 单源 §4.1:无 drift 或从未 spawn(as-launched 缺)→ 不拒。
|
|
2191
|
-
#[test]
|
|
2192
|
-
fn test_restart_fresh_spawn_after_drift_uses_file_value() {
|
|
2193
|
-
assert!(!check_bypass_drift(Some(true), Some(true)));
|
|
2194
|
-
assert!(!check_bypass_drift(Some(false), Some(false)));
|
|
2195
|
-
// 从未 spawn:as-launched 缺失 → 不 drift(首次 Noop 无进程可比)。
|
|
2196
|
-
assert!(!check_bypass_drift(Some(true), None));
|
|
2197
|
-
assert!(!check_bypass_drift(None, None));
|
|
2198
|
-
}
|
|
2199
|
-
}
|