@team-agent/installer 0.5.127 → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +15 -4
- package/README.zh.md +1 -1
- package/package.json +4 -8
- package/skills/team-agent/SKILL.md +41 -26
- package/skills/team-agent/command-coverage.json +170 -184
- package/skills/team-agent/docs/reference/team-agent-operator.md +66 -32
- package/skills/team-agent/references/recovery-runbook.md +1 -1
- package/skills/team-agent/references/team-agent-operator.md +66 -32
- 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 -1298
- package/crates/team-agent/src/cli/arguments.rs +0 -401
- package/crates/team-agent/src/cli/attach_app_server_leader.rs +0 -98
- package/crates/team-agent/src/cli/diagnose.rs +0 -1675
- package/crates/team-agent/src/cli/emit.rs +0 -2898
- 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 -1189
- package/crates/team-agent/src/cli/intent/runtime_tests.rs +0 -821
- package/crates/team-agent/src/cli/intent/tests.rs +0 -691
- 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 -6528
- package/crates/team-agent/src/cli/models.rs +0 -449
- 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/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 -192
- 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 -201
- package/crates/team-agent/src/cli/shutdown_socket.rs +0 -288
- package/crates/team-agent/src/cli/spec.rs +0 -159
- 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 -2043
- 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 -372
- 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 -760
- package/crates/team-agent/src/cli/tests/parser_safety.rs +0 -383
- package/crates/team-agent/src/cli/tests/run_delegation.rs +0 -481
- 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 -1091
- 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 -842
- package/crates/team-agent/src/compiler.rs +0 -691
- 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 -1610
- package/crates/team-agent/src/contract_runtime/commands.rs +0 -671
- package/crates/team-agent/src/contract_runtime/config.rs +0 -354
- package/crates/team-agent/src/contract_runtime/config_tests.rs +0 -56
- package/crates/team-agent/src/contract_runtime/discovery.rs +0 -438
- package/crates/team-agent/src/contract_runtime/fork.rs +0 -368
- package/crates/team-agent/src/contract_runtime/forward.rs +0 -288
- package/crates/team-agent/src/contract_runtime/framework.rs +0 -350
- package/crates/team-agent/src/contract_runtime/mcp.rs +0 -399
- 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 -589
- 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 -338
- 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 -461
- 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 -3112
- 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 -1585
- 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/event_log.rs +0 -502
- package/crates/team-agent/src/fake_worker.rs +0 -303
- 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 -883
- package/crates/team-agent/src/leader/start.rs +0 -4500
- 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 -267
- 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 -145
- package/crates/team-agent/src/lifecycle/helpers.rs +0 -212
- package/crates/team-agent/src/lifecycle/launch/add_agent.rs +0 -848
- package/crates/team-agent/src/lifecycle/launch/add_agent_state.rs +0 -393
- package/crates/team-agent/src/lifecycle/launch/agent_state.rs +0 -332
- package/crates/team-agent/src/lifecycle/launch/clone_agent.rs +0 -178
- package/crates/team-agent/src/lifecycle/launch/fork_agent.rs +0 -82
- package/crates/team-agent/src/lifecycle/launch/fork_entry.rs +0 -63
- 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 -152
- package/crates/team-agent/src/lifecycle/launch/ownership.rs +0 -105
- package/crates/team-agent/src/lifecycle/launch/pi_mcp.rs +0 -629
- package/crates/team-agent/src/lifecycle/launch/pi_mcp_extension.js +0 -226
- package/crates/team-agent/src/lifecycle/launch/plan.rs +0 -269
- package/crates/team-agent/src/lifecycle/launch/quick_start.rs +0 -5176
- 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 -450
- 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 -534
- 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 -757
- package/crates/team-agent/src/lifecycle/profile_smoke.rs +0 -557
- package/crates/team-agent/src/lifecycle/restart/agent.rs +0 -2198
- package/crates/team-agent/src/lifecycle/restart/common.rs +0 -2052
- 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 -223
- package/crates/team-agent/src/lifecycle/role_config.rs +0 -1205
- 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_profile_launch_red.rs +0 -135
- package/crates/team-agent/src/lifecycle/tests/cli_argv_routing_red.rs +0 -1415
- 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 -396
- package/crates/team-agent/src/lifecycle/tests/communication_mode_runtime_contract_red.rs +0 -418
- package/crates/team-agent/src/lifecycle/tests/copilot_provider_red.rs +0 -2039
- package/crates/team-agent/src/lifecycle/tests/core.rs +0 -1298
- 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 -71
- 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 -472
- 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 -3981
- 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 -152
- package/crates/team-agent/src/lifecycle/tests/pi_command_red.rs +0 -182
- package/crates/team-agent/src/lifecycle/tests/pi_dual_entry_red.rs +0 -374
- package/crates/team-agent/src/lifecycle/tests/pi_executable_mcp_red.rs +0 -546
- package/crates/team-agent/src/lifecycle/tests/pi_lifecycle_doctor_red.rs +0 -31
- package/crates/team-agent/src/lifecycle/tests/pi_native_mcp_capability_red.rs +0 -528
- package/crates/team-agent/src/lifecycle/tests/pi_session_delivery_red.rs +0 -134
- package/crates/team-agent/src/lifecycle/tests/quick_start_worker_readiness_red.rs +0 -333
- 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 -90
- 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 -996
- package/crates/team-agent/src/lifecycle/tests/worker_spawn_env_red.rs +0 -827
- package/crates/team-agent/src/lifecycle/tests.rs +0 -102
- package/crates/team-agent/src/lifecycle/types.rs +0 -861
- package/crates/team-agent/src/lifecycle/worker_command_context.rs +0 -370
- 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 -546
- package/crates/team-agent/src/mcp_server/readiness_tests.rs +0 -369
- 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 -168
- package/crates/team-agent/src/mcp_server/tests/send.rs +0 -388
- package/crates/team-agent/src/mcp_server/tests/tools.rs +0 -343
- 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 -1015
- 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 -3785
- 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 -354
- 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 -166
- 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/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 -217
- 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 -551
- 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 -3655
- package/crates/team-agent/src/messaging/tests/spine.rs +0 -837
- 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 -471
- 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 -898
- 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 -28
- 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 -773
- 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 -722
- package/crates/team-agent/src/provider/session/capture.rs +0 -2631
- package/crates/team-agent/src/provider/session/context_fork/outcome.rs +0 -168
- package/crates/team-agent/src/provider/session/context_fork.rs +0 -264
- 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/common/tests.rs +0 -316
- package/crates/team-agent/src/provider/session_scan/common.rs +0 -618
- package/crates/team-agent/src/provider/session_scan/copilot.rs +0 -280
- package/crates/team-agent/src/provider/session_scan/pi.rs +0 -152
- package/crates/team-agent/src/provider/session_scan.rs +0 -61
- 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 -354
- package/crates/team-agent/src/provider/tests/classify.rs +0 -201
- package/crates/team-agent/src/provider/tests/command_helpers.rs +0 -26
- 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 -140
- 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 -406
- 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/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 -4254
- package/crates/team-agent/src/tmux_backend.rs +0 -4527
- 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,2052 +0,0 @@
|
|
|
1
|
-
//! ---
|
|
2
|
-
//! purpose: restart 与 start 共用的底座,含 spawn 执行、transport 解析、state 标记与各类读取判定
|
|
3
|
-
//! contract:
|
|
4
|
-
//! provides:
|
|
5
|
-
//! - name: spawn_agent_window
|
|
6
|
-
//! what: 为一个席位拼命令并在目标 session 里开窗或分格起进程
|
|
7
|
-
//! - name: lifecycle_worker_tmux_backend_for_selected_state
|
|
8
|
-
//! what: 按团队已持久化的 endpoint 解析 tmux 后端,非 tmux 状态直接拒绝
|
|
9
|
-
//! - name: save_restart_projected_state
|
|
10
|
-
//! what: 同步团队投影后经 repository 带写意图落盘
|
|
11
|
-
//! - name: resume_backing_probe_for_agent
|
|
12
|
-
//! what: 探测该席位的 resume 依据是否在盘上,并记下所有查过的路径
|
|
13
|
-
//! - name: converge_missing_provider_sessions
|
|
14
|
-
//! what: 有界等待会话捕获收敛,并逐轮写进度事件
|
|
15
|
-
//! - name: mark_agent_stopped
|
|
16
|
-
//! what: 把席位标记为已停并清掉 window 与 pane_id 字段
|
|
17
|
-
//! depends:
|
|
18
|
-
//! - crate::transport::Transport
|
|
19
|
-
//! - crate::tmux_backend
|
|
20
|
-
//! - crate::transport_factory
|
|
21
|
-
//! - crate::provider
|
|
22
|
-
//! - crate::session_capture
|
|
23
|
-
//! - crate::state::projection
|
|
24
|
-
//! - crate::state::repository
|
|
25
|
-
//! - crate::state::persist
|
|
26
|
-
//! - crate::coordinator::health
|
|
27
|
-
//! - crate::event_log::EventLog
|
|
28
|
-
//! - crate::lifecycle::launch
|
|
29
|
-
//! - crate::lifecycle::profile_launch
|
|
30
|
-
//! - crate::lifecycle::worker_command_context
|
|
31
|
-
//! - crate::lifecycle::restart::selection
|
|
32
|
-
//! boundary:
|
|
33
|
-
//! - 状态是 conpty 时 tmux 专用解析器直接报错,不静默降级到 tmux
|
|
34
|
-
//! - 探测类判定失败一律倒向保守值,不把不确定当成存活
|
|
35
|
-
//! - 清活动观测只删观测字段,不动生命周期与拓扑字段
|
|
36
|
-
//! - 收敛与排空都是有界等待,不无限阻塞
|
|
37
|
-
//! maturity: wired
|
|
38
|
-
//! ---
|
|
39
|
-
use super::*;
|
|
40
|
-
|
|
41
|
-
pub(super) struct SpawnedAgentWindow {
|
|
42
|
-
pub spawn: crate::transport::SpawnResult,
|
|
43
|
-
pub spawned_at: String,
|
|
44
|
-
pub plan: crate::provider::CommandPlan,
|
|
45
|
-
pub profile_launch: crate::provider::ProviderProfileLaunch,
|
|
46
|
-
pub layout_placement: Option<crate::lifecycle::launch::LayoutPlacement>,
|
|
47
|
-
pub spawn_cwd: std::path::PathBuf,
|
|
48
|
-
/// Actual configuration generated for this spawn, not inferred from old state.
|
|
49
|
-
pub mcp_config_path: Option<std::path::PathBuf>,
|
|
50
|
-
/// Issue 2 (Round 3b gate review §6): the resolved `owner_team_id` used
|
|
51
|
-
/// for this spawn's MCP env / command. Callers (`mark_agent_respawned`,
|
|
52
|
-
/// `mark_agent_started`) must persist this back into the agent row so
|
|
53
|
-
/// future restarts read it directly (priority #2 in the resolution
|
|
54
|
-
/// cascade) instead of relying on top-level `active_team_key`.
|
|
55
|
-
pub owner_team_id: Option<String>,
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
#[derive(Clone)]
|
|
59
|
-
pub(super) struct SameRoleCohortTarget {
|
|
60
|
-
pub agent_id: String,
|
|
61
|
-
pub window: String,
|
|
62
|
-
pub expected_pane_id: Option<String>,
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
impl SameRoleCohortTarget {
|
|
66
|
-
/// ---
|
|
67
|
-
/// purpose: 构造一个同角色同批目标
|
|
68
|
-
/// params:
|
|
69
|
-
/// window: 该席位的窗口名
|
|
70
|
-
/// returns: 未带期望 pane 的目标
|
|
71
|
-
/// ---
|
|
72
|
-
pub(super) fn new(agent_id: &AgentId, window: &str) -> Self {
|
|
73
|
-
Self {
|
|
74
|
-
agent_id: agent_id.as_str().to_string(),
|
|
75
|
-
window: window.to_string(),
|
|
76
|
-
expected_pane_id: None,
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
/// ---
|
|
81
|
-
/// purpose: 给目标补上期望的旧 pane
|
|
82
|
-
/// returns: 带期望 pane 的目标
|
|
83
|
-
/// ---
|
|
84
|
-
pub(super) fn with_expected_pane_id(mut self, pane_id: Option<&str>) -> Self {
|
|
85
|
-
self.expected_pane_id = pane_id.map(ToString::to_string);
|
|
86
|
-
self
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
/// ---
|
|
91
|
-
/// purpose: 判断该窗口是不是按席位命名的独立窗口
|
|
92
|
-
/// returns: 窗口名等于席位名且不是规范布局窗口时为 true
|
|
93
|
-
/// ---
|
|
94
|
-
pub(super) fn is_per_agent_cohort_window(window: &str, agent_id: &AgentId) -> bool {
|
|
95
|
-
window == agent_id.as_str() && !crate::lifecycle::launch::is_adaptive_layout_window_pub(window)
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
/// ---
|
|
99
|
-
/// purpose: spawn 之前检查同角色同批是否已有残留
|
|
100
|
-
/// returns: 期望基数为 0;不满足时给出可读的拒绝说明,满足则 None
|
|
101
|
-
/// ---
|
|
102
|
-
pub(super) fn same_role_cohort_pre_spawn_error(
|
|
103
|
-
transport: &dyn crate::transport::Transport,
|
|
104
|
-
session_name: &SessionName,
|
|
105
|
-
operation: &str,
|
|
106
|
-
targets: &[SameRoleCohortTarget],
|
|
107
|
-
) -> Option<String> {
|
|
108
|
-
same_role_cohort_error(transport, session_name, operation, targets, 0, true)
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
/// ---
|
|
112
|
-
/// purpose: spawn 之后检查同角色同批是否恰好只剩一个
|
|
113
|
-
/// returns: 期望基数为 1;不满足时给出可读的拒绝说明,满足则 None
|
|
114
|
-
/// ---
|
|
115
|
-
pub(super) fn same_role_cohort_exactly_one_error(
|
|
116
|
-
transport: &dyn crate::transport::Transport,
|
|
117
|
-
session_name: &SessionName,
|
|
118
|
-
operation: &str,
|
|
119
|
-
targets: &[SameRoleCohortTarget],
|
|
120
|
-
) -> Option<String> {
|
|
121
|
-
same_role_cohort_error(transport, session_name, operation, targets, 1, false)
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
/// ---
|
|
125
|
-
/// purpose: 杀掉这些目标记录的旧 pane
|
|
126
|
-
/// returns: 全部处理完返回空值;目标没有期望 pane 或该 pane 明确不存在时跳过
|
|
127
|
-
/// errors: 杀 pane 失败时返回带席位、窗口与 pane 的错误串
|
|
128
|
-
/// ---
|
|
129
|
-
pub(super) fn retire_expected_same_role_cohorts(
|
|
130
|
-
transport: &dyn crate::transport::Transport,
|
|
131
|
-
operation: &str,
|
|
132
|
-
targets: &[SameRoleCohortTarget],
|
|
133
|
-
) -> Result<(), String> {
|
|
134
|
-
for target in targets {
|
|
135
|
-
let Some(pane_id) = target.expected_pane_id.as_deref() else {
|
|
136
|
-
continue;
|
|
137
|
-
};
|
|
138
|
-
let pane = crate::transport::PaneId::new(pane_id);
|
|
139
|
-
if transport.has_pane(&pane).ok().flatten() == Some(false) {
|
|
140
|
-
continue;
|
|
141
|
-
}
|
|
142
|
-
transport.kill_pane(&pane).map_err(|error| {
|
|
143
|
-
format!(
|
|
144
|
-
"{operation} failed to retire old same-role cohort {}:window={}:pane={pane_id}: {error}",
|
|
145
|
-
target.agent_id, target.window
|
|
146
|
-
)
|
|
147
|
-
})?;
|
|
148
|
-
}
|
|
149
|
-
Ok(())
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
fn same_role_cohort_error(
|
|
153
|
-
transport: &dyn crate::transport::Transport,
|
|
154
|
-
session_name: &SessionName,
|
|
155
|
-
operation: &str,
|
|
156
|
-
targets: &[SameRoleCohortTarget],
|
|
157
|
-
expected_live: usize,
|
|
158
|
-
expected_old_only: bool,
|
|
159
|
-
) -> Option<String> {
|
|
160
|
-
let panes = match transport.list_targets() {
|
|
161
|
-
Ok(panes) => panes,
|
|
162
|
-
Err(error) => {
|
|
163
|
-
return Some(format!(
|
|
164
|
-
"{operation} refused: same-role cohort observation failed: {error}"
|
|
165
|
-
));
|
|
166
|
-
}
|
|
167
|
-
};
|
|
168
|
-
let mut cardinality_proofs = Vec::new();
|
|
169
|
-
let mut binding_proofs = Vec::new();
|
|
170
|
-
let mut observation_proofs = Vec::new();
|
|
171
|
-
for target in targets {
|
|
172
|
-
let candidate_panes = panes
|
|
173
|
-
.iter()
|
|
174
|
-
.filter(|pane| pane.session.as_str() == session_name.as_str())
|
|
175
|
-
.filter(|pane| {
|
|
176
|
-
pane.window_name
|
|
177
|
-
.as_ref()
|
|
178
|
-
.is_some_and(|window| window.as_str() == target.window)
|
|
179
|
-
})
|
|
180
|
-
.collect::<Vec<_>>();
|
|
181
|
-
let mut live_panes = Vec::new();
|
|
182
|
-
for pane in candidate_panes {
|
|
183
|
-
match transport.liveness(&pane.pane_id) {
|
|
184
|
-
Ok(crate::transport::PaneLiveness::Live) => {
|
|
185
|
-
live_panes.push(pane.pane_id.as_str().to_string());
|
|
186
|
-
}
|
|
187
|
-
Ok(crate::transport::PaneLiveness::Dead) => {}
|
|
188
|
-
Ok(crate::transport::PaneLiveness::Unknown) => {
|
|
189
|
-
observation_proofs.push(format!(
|
|
190
|
-
"{}:window={}:pane={}:liveness=unknown",
|
|
191
|
-
target.agent_id,
|
|
192
|
-
target.window,
|
|
193
|
-
pane.pane_id.as_str()
|
|
194
|
-
));
|
|
195
|
-
}
|
|
196
|
-
Err(error) => observation_proofs.push(format!(
|
|
197
|
-
"{}:window={}:pane={}:liveness_error={error}",
|
|
198
|
-
target.agent_id,
|
|
199
|
-
target.window,
|
|
200
|
-
pane.pane_id.as_str()
|
|
201
|
-
)),
|
|
202
|
-
}
|
|
203
|
-
}
|
|
204
|
-
if live_panes.len() != expected_live {
|
|
205
|
-
cardinality_proofs.push(format!(
|
|
206
|
-
"{}:window={}:live_panes=[{}]",
|
|
207
|
-
target.agent_id,
|
|
208
|
-
target.window,
|
|
209
|
-
live_panes.join(",")
|
|
210
|
-
));
|
|
211
|
-
} else if !expected_old_only {
|
|
212
|
-
let observed = live_panes.first().map(String::as_str);
|
|
213
|
-
if target.expected_pane_id.as_deref() != observed {
|
|
214
|
-
binding_proofs.push(format!(
|
|
215
|
-
"{}:window={}:expected_pane={}:observed_pane={}",
|
|
216
|
-
target.agent_id,
|
|
217
|
-
target.window,
|
|
218
|
-
target.expected_pane_id.as_deref().unwrap_or("<missing>"),
|
|
219
|
-
observed.unwrap_or("<missing>")
|
|
220
|
-
));
|
|
221
|
-
}
|
|
222
|
-
}
|
|
223
|
-
}
|
|
224
|
-
if !observation_proofs.is_empty() {
|
|
225
|
-
return Some(format!(
|
|
226
|
-
"{operation} refused: same-role cohort observation failed; {}",
|
|
227
|
-
observation_proofs.join("; ")
|
|
228
|
-
));
|
|
229
|
-
}
|
|
230
|
-
if !binding_proofs.is_empty() {
|
|
231
|
-
return Some(format!(
|
|
232
|
-
"{operation} refused: spawn identity/binding mismatch; {}",
|
|
233
|
-
binding_proofs.join("; ")
|
|
234
|
-
));
|
|
235
|
-
}
|
|
236
|
-
if !cardinality_proofs.is_empty() {
|
|
237
|
-
return Some(format!(
|
|
238
|
-
"{operation} refused: same-role cohort duplicate proof failed; {}",
|
|
239
|
-
cardinality_proofs.join("; ")
|
|
240
|
-
));
|
|
241
|
-
}
|
|
242
|
-
None
|
|
243
|
-
}
|
|
244
|
-
|
|
245
|
-
/// ---
|
|
246
|
-
/// purpose: 为一个席位拼出命令并在目标 session 里起进程
|
|
247
|
-
/// params:
|
|
248
|
-
/// resume_session_id: 给出且该 provider 支持 resume 时按 resume 起,否则丢弃它全新起
|
|
249
|
-
/// into_existing_session: 目标 session 已存在时开新窗口,否则新建 session
|
|
250
|
-
/// layout_placement: 有布局位置时按布局开窗或分格
|
|
251
|
-
/// spawn_cwd_override: 覆盖工作目录
|
|
252
|
-
/// owner_team_id_override: 显式指定写进 worker 环境的 owner team,缺省时退回席位行与顶层活跃键
|
|
253
|
-
/// returns: spawn 结果、时间戳、命令计划、profile 启动参数、布局位置与实际使用的 owner team
|
|
254
|
-
/// errors: 命令拼装、profile 准备或 transport spawn 失败时返回 LifecycleError
|
|
255
|
-
/// ---
|
|
256
|
-
#[allow(clippy::too_many_arguments)]
|
|
257
|
-
pub(super) fn spawn_agent_window(
|
|
258
|
-
workspace: &Path,
|
|
259
|
-
session_name: &SessionName,
|
|
260
|
-
agent_id: &AgentId,
|
|
261
|
-
agent: &serde_json::Value,
|
|
262
|
-
resume_session_id: Option<&SessionId>,
|
|
263
|
-
into_existing_session: bool,
|
|
264
|
-
transport: &dyn crate::transport::Transport,
|
|
265
|
-
safety: Option<&DangerousApproval>,
|
|
266
|
-
layout_placement: Option<&crate::lifecycle::launch::LayoutPlacement>,
|
|
267
|
-
spawn_cwd_override: Option<&Path>,
|
|
268
|
-
tmux_endpoint_source: Option<&str>,
|
|
269
|
-
// Issue 2 (Round 3b gate review §6): explicit owner_team_id override.
|
|
270
|
-
// When `Some`, callers (restart/rebuild.rs, restart/agent.rs) thread the
|
|
271
|
-
// resolved `selected.team_key` through here so the worker's MCP env /
|
|
272
|
-
// command argv carries `TEAM_AGENT_OWNER_TEAM_ID=<selected team>` —
|
|
273
|
-
// even when the persisted agent row OR the top-level `active_team_key`
|
|
274
|
-
// is stale. When `None`, falls back to the legacy resolution
|
|
275
|
-
// (agent row → active_team_key) for back-compat with non-restart callers.
|
|
276
|
-
owner_team_id_override: Option<&str>,
|
|
277
|
-
) -> Result<SpawnedAgentWindow, LifecycleError> {
|
|
278
|
-
// A pre-upgrade Claude Code, Codex, Grok or Pi worker row has no legacy
|
|
279
|
-
// runtime left; refuse before any MCP file, profile or pane side effect.
|
|
280
|
-
if let Some(retired) = agent
|
|
281
|
-
.get("provider")
|
|
282
|
-
.and_then(serde_json::Value::as_str)
|
|
283
|
-
.filter(|provider| crate::provider::wire::is_retired_provider(provider))
|
|
284
|
-
{
|
|
285
|
-
return Err(LifecycleError::Provider(
|
|
286
|
-
crate::provider::wire::retired_provider_error(retired).into(),
|
|
287
|
-
));
|
|
288
|
-
}
|
|
289
|
-
let provider = agent_provider(agent);
|
|
290
|
-
let auth_mode = agent_auth_mode(agent);
|
|
291
|
-
let model = agent.get("model").and_then(|v| v.as_str());
|
|
292
|
-
let adapter = crate::provider::get_adapter(provider);
|
|
293
|
-
let resume_session_id = if adapter.caps().resume {
|
|
294
|
-
resume_session_id
|
|
295
|
-
} else {
|
|
296
|
-
None
|
|
297
|
-
};
|
|
298
|
-
// Contract C / F6.4: thread compiled role/tools/MCP context through restart as well —
|
|
299
|
-
// a restarted worker must come back up with the SAME callable MCP capability + role
|
|
300
|
-
// prompt as a fresh launch, else `report_result` becomes unreachable after every restart.
|
|
301
|
-
let detected_safety;
|
|
302
|
-
let safety = if let Some(safety) = safety {
|
|
303
|
-
safety
|
|
304
|
-
} else {
|
|
305
|
-
detected_safety = crate::lifecycle::launch::effective_runtime_config_for_worker_spawn_json(
|
|
306
|
-
agent, provider,
|
|
307
|
-
)?;
|
|
308
|
-
&detected_safety
|
|
309
|
-
};
|
|
310
|
-
let command_agent = crate::lifecycle::worker_command_context::WorkerCommandAgent::from_json(
|
|
311
|
-
agent,
|
|
312
|
-
Some(agent_id.as_str()),
|
|
313
|
-
provider,
|
|
314
|
-
)?;
|
|
315
|
-
let system_prompt =
|
|
316
|
-
crate::lifecycle::worker_command_context::compile_worker_system_prompt(&command_agent)?;
|
|
317
|
-
// owner_team_id resolution priority (Issue 2 fix):
|
|
318
|
-
// 1. caller's explicit override (restart paths pass `selected.team_key`)
|
|
319
|
-
// 2. agent row's persisted `owner_team_id` (set by prior launch/restart)
|
|
320
|
-
// 3. top-level `active_team_key` (legacy fallback for add-agent etc.)
|
|
321
|
-
// The override breaks the dependency on top-level state mutation: even if
|
|
322
|
-
// top-level `active_team_key` is stale (e.g. `ta-probe-ws`), a restart that
|
|
323
|
-
// resolved `selected.team_key=prerelease-040-round3b` propagates THAT team
|
|
324
|
-
// into the worker's MCP env.
|
|
325
|
-
let state_for_team =
|
|
326
|
-
crate::state::persist::load_runtime_state(workspace).unwrap_or(serde_json::json!({}));
|
|
327
|
-
let team_id = owner_team_id_override
|
|
328
|
-
.filter(|s| !s.is_empty())
|
|
329
|
-
.map(str::to_string)
|
|
330
|
-
.or_else(|| {
|
|
331
|
-
agent
|
|
332
|
-
.get("owner_team_id")
|
|
333
|
-
.and_then(|v| v.as_str())
|
|
334
|
-
.filter(|s| !s.is_empty())
|
|
335
|
-
.map(str::to_string)
|
|
336
|
-
})
|
|
337
|
-
.or_else(|| {
|
|
338
|
-
let key =
|
|
339
|
-
crate::messaging::leader_receiver::active_team_key(workspace, &state_for_team);
|
|
340
|
-
(!key.is_empty()).then_some(key)
|
|
341
|
-
});
|
|
342
|
-
let mcp_config = adapter
|
|
343
|
-
.mcp_config(auth_mode)
|
|
344
|
-
.map_err(|e| LifecycleError::Provider(e.to_string()))?;
|
|
345
|
-
let mcp_config = crate::lifecycle::launch::resolve_mcp_config(
|
|
346
|
-
mcp_config,
|
|
347
|
-
workspace,
|
|
348
|
-
agent_id.as_str(),
|
|
349
|
-
team_id.as_deref().unwrap_or(""),
|
|
350
|
-
);
|
|
351
|
-
let mcp_config_path = crate::lifecycle::launch::write_worker_mcp_config_for_provider(
|
|
352
|
-
workspace,
|
|
353
|
-
agent_id.as_str(),
|
|
354
|
-
&mcp_config,
|
|
355
|
-
Some(provider),
|
|
356
|
-
)?;
|
|
357
|
-
let profile_launch =
|
|
358
|
-
crate::lifecycle::profile_launch::prepare_provider_profile_launch_from_json(
|
|
359
|
-
workspace,
|
|
360
|
-
agent_id.as_str(),
|
|
361
|
-
agent,
|
|
362
|
-
Some(&mcp_config),
|
|
363
|
-
)?;
|
|
364
|
-
let command_model = profile_launch.command_overrides.model.as_deref().or(model);
|
|
365
|
-
// 0.4.x provider effort MVP: restart/resume preserves effort from the
|
|
366
|
-
// persisted agent JSON (state's agent["effort"] field, set by launch).
|
|
367
|
-
let restart_effort = crate::lifecycle::launch::provider_effort_for_spawn_json(agent, provider)?;
|
|
368
|
-
if let Some(event_value) = crate::lifecycle::launch::provider_effort_event_if_dropped_json(
|
|
369
|
-
agent,
|
|
370
|
-
provider,
|
|
371
|
-
agent_id.as_str(),
|
|
372
|
-
) {
|
|
373
|
-
let _ = crate::event_log::EventLog::new(workspace)
|
|
374
|
-
.write("provider.effort_unsupported", event_value);
|
|
375
|
-
}
|
|
376
|
-
let context = crate::provider::ProviderCommandContext {
|
|
377
|
-
auth_mode,
|
|
378
|
-
mcp_config: Some(&mcp_config),
|
|
379
|
-
system_prompt: Some(system_prompt.as_str()),
|
|
380
|
-
model: command_model,
|
|
381
|
-
dangerously_skip_permissions: agent
|
|
382
|
-
.get("dangerously_skip_permissions")
|
|
383
|
-
.and_then(serde_json::Value::as_bool)
|
|
384
|
-
.unwrap_or(false),
|
|
385
|
-
profile_launch: Some(&profile_launch),
|
|
386
|
-
agent_id_hint: Some(agent_id.as_str()),
|
|
387
|
-
effort: restart_effort,
|
|
388
|
-
};
|
|
389
|
-
let spawn_cwd = spawn_cwd_override.unwrap_or(workspace);
|
|
390
|
-
let mut plan = match resume_session_id {
|
|
391
|
-
Some(session_id) => adapter.build_resume_command_plan(Some(session_id), context),
|
|
392
|
-
None => adapter.build_command_plan(context),
|
|
393
|
-
}
|
|
394
|
-
.map_err(|e| LifecycleError::Provider(e.to_string()))?;
|
|
395
|
-
if !plan.managed_mcp_config && !profile_launch.managed_mcp_config {
|
|
396
|
-
crate::lifecycle::launch::point_native_mcp_config_at_file(
|
|
397
|
-
&mut plan.argv,
|
|
398
|
-
provider,
|
|
399
|
-
&mcp_config_path,
|
|
400
|
-
);
|
|
401
|
-
}
|
|
402
|
-
crate::lifecycle::launch::fill_spawn_placeholders_full(
|
|
403
|
-
&mut plan.argv,
|
|
404
|
-
workspace,
|
|
405
|
-
agent_id.as_str(),
|
|
406
|
-
team_id.as_deref(),
|
|
407
|
-
);
|
|
408
|
-
let window = layout_placement
|
|
409
|
-
.map(|placement| placement.layout_window.clone())
|
|
410
|
-
.unwrap_or_else(|| WindowName::new(agent_id.as_str()));
|
|
411
|
-
let mut env = crate::lifecycle::launch::inherited_env_with_team_overrides(
|
|
412
|
-
workspace,
|
|
413
|
-
agent_id.as_str(),
|
|
414
|
-
team_id.as_deref(),
|
|
415
|
-
Some(crate::lifecycle::launch::auth_mode_env_value(auth_mode)),
|
|
416
|
-
);
|
|
417
|
-
crate::lifecycle::launch::apply_profile_launch_env(&mut env, &profile_launch);
|
|
418
|
-
crate::lifecycle::launch::apply_mcp_auto_approval_env(&mut env, safety);
|
|
419
|
-
if provider == crate::provider::Provider::Copilot {
|
|
420
|
-
crate::lifecycle::launch::apply_copilot_instructions_overlay(
|
|
421
|
-
workspace,
|
|
422
|
-
agent_id.as_str(),
|
|
423
|
-
&system_prompt,
|
|
424
|
-
&mut env,
|
|
425
|
-
)?;
|
|
426
|
-
}
|
|
427
|
-
// Spawn cwd is override-or-workspace (E56: ignore drifted state
|
|
428
|
-
// `.team/runtime/<team_key>/`).
|
|
429
|
-
let env_unset = crate::layout::worker_env::isolate_worker_spawn_env(
|
|
430
|
-
provider,
|
|
431
|
-
&mut env,
|
|
432
|
-
crate::lifecycle::launch::extend_worker_env_unset_for_effort(
|
|
433
|
-
profile_launch.env_unset.iter().cloned().collect(),
|
|
434
|
-
provider,
|
|
435
|
-
restart_effort.is_some(),
|
|
436
|
-
),
|
|
437
|
-
);
|
|
438
|
-
// Keep the launcher-known typed provider independent from inherited
|
|
439
|
-
// LEADER_* identity. The tmux wrapper binds pane/socket at invocation time.
|
|
440
|
-
crate::layout::worker_env::inject_current_caller_provider(&mut env, provider);
|
|
441
|
-
crate::provider::argv_route::apply(provider, &mut plan.argv)
|
|
442
|
-
.map_err(|error| LifecycleError::Provider(error.to_string()))?;
|
|
443
|
-
|
|
444
|
-
// 0.4.6 Stage 2: write actual spawn plan event BEFORE invoking the
|
|
445
|
-
// transport spawn. Mirrors `launch.rs:359-380` (the reference impl)
|
|
446
|
-
// so reset/start/restart fresh produces the same truth source as
|
|
447
|
-
// quick-start. Any "argv had --session-id but didn't take effect"
|
|
448
|
-
// failure can now be diagnosed from events.jsonl — the recorded
|
|
449
|
-
// expected_session_id == state._pending_session_id (after
|
|
450
|
-
// mark_agent_started persists the same plan tuple).
|
|
451
|
-
let spawned_at = crate::lifecycle::launch::spawn_timestamp();
|
|
452
|
-
{
|
|
453
|
-
let session_id_in_argv = plan
|
|
454
|
-
.argv
|
|
455
|
-
.iter()
|
|
456
|
-
.position(|a| a == "--session-id")
|
|
457
|
-
.and_then(|i| plan.argv.get(i + 1))
|
|
458
|
-
.cloned();
|
|
459
|
-
let env_overlay_keys: Vec<&String> = env.keys().collect();
|
|
460
|
-
let tmux_start_mode_pre_spawn =
|
|
461
|
-
predict_tmux_start_mode(layout_placement, into_existing_session);
|
|
462
|
-
let spawn_epoch = state_spawn_epoch_for_agent(workspace, agent_id);
|
|
463
|
-
let tmux_endpoint = transport.tmux_endpoint();
|
|
464
|
-
let event_log = crate::event_log::EventLog::new(workspace);
|
|
465
|
-
let _ = event_log.write(
|
|
466
|
-
crate::event_log::PROVIDER_WORKER_SPAWN_ARGV,
|
|
467
|
-
crate::event_log::provider_worker_spawn_argv_fields(serde_json::json!({
|
|
468
|
-
"agent_id": agent_id.as_str(),
|
|
469
|
-
"provider": provider,
|
|
470
|
-
"argv": plan.argv,
|
|
471
|
-
"session_id_in_argv": session_id_in_argv,
|
|
472
|
-
"expected_session_id": plan.expected_session_id.as_ref().map(|s| s.as_str()),
|
|
473
|
-
"spawn_cwd": spawn_cwd.to_string_lossy(),
|
|
474
|
-
"env_overlay_keys": env_overlay_keys,
|
|
475
|
-
"env_unset": env_unset,
|
|
476
|
-
"tmux_start_mode": tmux_start_mode_pre_spawn,
|
|
477
|
-
"spawn_epoch": spawn_epoch,
|
|
478
|
-
"spawned_at": spawned_at.as_str(),
|
|
479
|
-
"source": "restart",
|
|
480
|
-
"tmux_endpoint": tmux_endpoint,
|
|
481
|
-
"tmux_endpoint_source": tmux_endpoint_source.unwrap_or("transport"),
|
|
482
|
-
})),
|
|
483
|
-
);
|
|
484
|
-
}
|
|
485
|
-
|
|
486
|
-
// 0.5.39 Slice 2 (tmux-server-death-locate §7 Slice 2): route the
|
|
487
|
-
// primary worker spawn through the worker shell wrapper so provider
|
|
488
|
-
// exit leaves the pane at an inert sh tail with an explicit exit marker
|
|
489
|
-
// instead of collapsing the pane into `[exited]`. The inert tail does
|
|
490
|
-
// not read pane stdin. The split path stays
|
|
491
|
-
// on plain spawn_split — split panes are display overlays, not the
|
|
492
|
-
// primary worker process.
|
|
493
|
-
let provider_label = crate::provider::wire::command_name(provider);
|
|
494
|
-
let result = if let Some(placement) = layout_placement {
|
|
495
|
-
if placement.starts_window {
|
|
496
|
-
if into_existing_session {
|
|
497
|
-
transport.spawn_into_with_worker_shell_wrapper(
|
|
498
|
-
session_name,
|
|
499
|
-
&window,
|
|
500
|
-
&plan.argv,
|
|
501
|
-
spawn_cwd,
|
|
502
|
-
&env,
|
|
503
|
-
&env_unset,
|
|
504
|
-
provider_label,
|
|
505
|
-
)
|
|
506
|
-
} else {
|
|
507
|
-
transport.spawn_first_with_worker_shell_wrapper(
|
|
508
|
-
session_name,
|
|
509
|
-
&window,
|
|
510
|
-
&plan.argv,
|
|
511
|
-
spawn_cwd,
|
|
512
|
-
&env,
|
|
513
|
-
&env_unset,
|
|
514
|
-
provider_label,
|
|
515
|
-
)
|
|
516
|
-
}
|
|
517
|
-
} else if !window_present_in_live(transport, session_name, &window)
|
|
518
|
-
|| !crate::lifecycle::launch::is_adaptive_layout_window_pub(window.as_str())
|
|
519
|
-
{
|
|
520
|
-
// E43 Fix C + E45 (0.3.24 bug#3 → bug#4): never split into a
|
|
521
|
-
// window that either does not exist on live tmux OR is a
|
|
522
|
-
// per-agent window (`developer`, `architect`, ...) that the
|
|
523
|
-
// upstream placement guards should have refused. This is the
|
|
524
|
-
// defence-in-depth layer; the primary fix is in
|
|
525
|
-
// `adaptive_placement_for_agent` / `adaptive_existing_placement_for_agent`,
|
|
526
|
-
// but a placement built from stale `pane_index>0` state can
|
|
527
|
-
// still ask to split a per-agent window — and the macmini repro
|
|
528
|
-
// showed split-window -t :developer would otherwise succeed and
|
|
529
|
-
// hijack the developer worker's pane. Downgrade to spawn_into
|
|
530
|
-
// (new window named after agent_id) — canonical per-agent
|
|
531
|
-
// fallback the existing 7 workers use.
|
|
532
|
-
transport.spawn_into_with_worker_shell_wrapper(
|
|
533
|
-
session_name,
|
|
534
|
-
&WindowName::new(agent_id.as_str()),
|
|
535
|
-
&plan.argv,
|
|
536
|
-
spawn_cwd,
|
|
537
|
-
&env,
|
|
538
|
-
&env_unset,
|
|
539
|
-
provider_label,
|
|
540
|
-
)
|
|
541
|
-
} else {
|
|
542
|
-
// 0.3.28 Step 8: spawn_split must only fire from the display
|
|
543
|
-
// overlay path. Warn-only here; Step 9 promotes to hard fail.
|
|
544
|
-
crate::layout::overlay::assert_overlay_call_site(session_name, &window);
|
|
545
|
-
transport.spawn_split_with_env_unset(
|
|
546
|
-
session_name,
|
|
547
|
-
&window,
|
|
548
|
-
&plan.argv,
|
|
549
|
-
spawn_cwd,
|
|
550
|
-
&env,
|
|
551
|
-
&env_unset,
|
|
552
|
-
)
|
|
553
|
-
}
|
|
554
|
-
} else if into_existing_session {
|
|
555
|
-
transport.spawn_into_with_worker_shell_wrapper(
|
|
556
|
-
session_name,
|
|
557
|
-
&window,
|
|
558
|
-
&plan.argv,
|
|
559
|
-
spawn_cwd,
|
|
560
|
-
&env,
|
|
561
|
-
&env_unset,
|
|
562
|
-
provider_label,
|
|
563
|
-
)
|
|
564
|
-
} else {
|
|
565
|
-
transport.spawn_first_with_worker_shell_wrapper(
|
|
566
|
-
session_name,
|
|
567
|
-
&window,
|
|
568
|
-
&plan.argv,
|
|
569
|
-
spawn_cwd,
|
|
570
|
-
&env,
|
|
571
|
-
&env_unset,
|
|
572
|
-
provider_label,
|
|
573
|
-
)
|
|
574
|
-
};
|
|
575
|
-
let spawn = result.map_err(|e| LifecycleError::Transport(e.to_string()))?;
|
|
576
|
-
if layout_placement.is_some() {
|
|
577
|
-
crate::lifecycle::launch::configure_adaptive_pane_title(
|
|
578
|
-
workspace,
|
|
579
|
-
transport,
|
|
580
|
-
session_name,
|
|
581
|
-
&window,
|
|
582
|
-
&spawn.pane_id,
|
|
583
|
-
agent_id.as_str(),
|
|
584
|
-
);
|
|
585
|
-
}
|
|
586
|
-
let startup_prompt = adapter.handle_startup_prompts_outcome(
|
|
587
|
-
transport,
|
|
588
|
-
&crate::transport::Target::Pane(spawn.pane_id.clone()),
|
|
589
|
-
30,
|
|
590
|
-
0.5,
|
|
591
|
-
);
|
|
592
|
-
if let Some(error) = startup_prompt.capture_error.as_deref() {
|
|
593
|
-
if is_structural_startup_prompt_error(error) {
|
|
594
|
-
if let Err(rollback_error) = transport.kill_pane(&spawn.pane_id) {
|
|
595
|
-
return Err(LifecycleError::Transport(format!(
|
|
596
|
-
"startup prompt structural failure for {}:{} pane {}: {}; failed to roll back spawned pane: {}",
|
|
597
|
-
session_name.as_str(),
|
|
598
|
-
window.as_str(),
|
|
599
|
-
spawn.pane_id.as_str(),
|
|
600
|
-
error,
|
|
601
|
-
rollback_error
|
|
602
|
-
)));
|
|
603
|
-
}
|
|
604
|
-
return Err(LifecycleError::Transport(format!(
|
|
605
|
-
"startup prompt structural failure for {}:{} pane {}: {}",
|
|
606
|
-
session_name.as_str(),
|
|
607
|
-
window.as_str(),
|
|
608
|
-
spawn.pane_id.as_str(),
|
|
609
|
-
error
|
|
610
|
-
)));
|
|
611
|
-
}
|
|
612
|
-
}
|
|
613
|
-
Ok(SpawnedAgentWindow {
|
|
614
|
-
spawn,
|
|
615
|
-
spawned_at,
|
|
616
|
-
plan,
|
|
617
|
-
profile_launch,
|
|
618
|
-
layout_placement: layout_placement.cloned(),
|
|
619
|
-
spawn_cwd: spawn_cwd.to_path_buf(),
|
|
620
|
-
mcp_config_path: Some(mcp_config_path),
|
|
621
|
-
owner_team_id: team_id,
|
|
622
|
-
})
|
|
623
|
-
}
|
|
624
|
-
|
|
625
|
-
fn is_structural_startup_prompt_error(error: &str) -> bool {
|
|
626
|
-
let lower = error.to_ascii_lowercase();
|
|
627
|
-
[
|
|
628
|
-
"can't find window",
|
|
629
|
-
"can't find pane",
|
|
630
|
-
"cannot find window",
|
|
631
|
-
"cannot find pane",
|
|
632
|
-
"target not found",
|
|
633
|
-
"no such pane",
|
|
634
|
-
"window disappeared",
|
|
635
|
-
"pane not owned",
|
|
636
|
-
"not owned by requested",
|
|
637
|
-
]
|
|
638
|
-
.iter()
|
|
639
|
-
.any(|needle| lower.contains(needle))
|
|
640
|
-
}
|
|
641
|
-
|
|
642
|
-
/// ---
|
|
643
|
-
/// purpose: 用 spec 里的同名 agent 补回 state 席位行缺的命令上下文字段
|
|
644
|
-
/// returns: 合并后的席位行;spec 读不到或找不到该 agent 时原样返回
|
|
645
|
-
/// ---
|
|
646
|
-
pub(super) fn rehydrate_agent_command_context_from_spec(
|
|
647
|
-
spec_workspace: &Path,
|
|
648
|
-
agent_id: &AgentId,
|
|
649
|
-
agent: &serde_json::Value,
|
|
650
|
-
) -> serde_json::Value {
|
|
651
|
-
let Ok(spec) = load_team_spec(spec_workspace) else {
|
|
652
|
-
return agent.clone();
|
|
653
|
-
};
|
|
654
|
-
let Some(spec_agent) = find_spec_agent(&spec, agent_id) else {
|
|
655
|
-
return agent.clone();
|
|
656
|
-
};
|
|
657
|
-
merge_command_context_fields(agent, spec_agent)
|
|
658
|
-
}
|
|
659
|
-
|
|
660
|
-
fn merge_command_context_fields(
|
|
661
|
-
agent: &serde_json::Value,
|
|
662
|
-
spec_agent: &YamlValue,
|
|
663
|
-
) -> serde_json::Value {
|
|
664
|
-
let mut merged = agent.clone();
|
|
665
|
-
let Some(obj) = merged.as_object_mut() else {
|
|
666
|
-
return merged;
|
|
667
|
-
};
|
|
668
|
-
for field in [
|
|
669
|
-
"role",
|
|
670
|
-
"system_prompt",
|
|
671
|
-
"output_contract",
|
|
672
|
-
"provider",
|
|
673
|
-
"model",
|
|
674
|
-
"auth_mode",
|
|
675
|
-
"effort",
|
|
676
|
-
"profile",
|
|
677
|
-
// 0.5.66 bypass 单源:rehydrate 同步合入,restart 的 safety 构造读到新值。
|
|
678
|
-
"dangerously_skip_permissions",
|
|
679
|
-
] {
|
|
680
|
-
if let Some(value) = spec_agent.get(field).and_then(yaml_value_to_json) {
|
|
681
|
-
obj.insert(field.to_string(), value);
|
|
682
|
-
}
|
|
683
|
-
}
|
|
684
|
-
merged
|
|
685
|
-
}
|
|
686
|
-
|
|
687
|
-
fn yaml_value_to_json(value: &YamlValue) -> Option<serde_json::Value> {
|
|
688
|
-
match value {
|
|
689
|
-
YamlValue::Null => Some(serde_json::Value::Null),
|
|
690
|
-
YamlValue::Bool(value) => Some(serde_json::json!(value)),
|
|
691
|
-
YamlValue::Int(value) => Some(serde_json::json!(value)),
|
|
692
|
-
YamlValue::Float(value) => Some(serde_json::json!(value)),
|
|
693
|
-
YamlValue::Str(value) => Some(serde_json::json!(value)),
|
|
694
|
-
YamlValue::List(items) => Some(serde_json::Value::Array(
|
|
695
|
-
items.iter().filter_map(yaml_value_to_json).collect(),
|
|
696
|
-
)),
|
|
697
|
-
YamlValue::Map(items) => {
|
|
698
|
-
let mut map = serde_json::Map::new();
|
|
699
|
-
for (key, value) in items {
|
|
700
|
-
if let Some(value) = yaml_value_to_json(value) {
|
|
701
|
-
map.insert(key.clone(), value);
|
|
702
|
-
}
|
|
703
|
-
}
|
|
704
|
-
Some(serde_json::Value::Object(map))
|
|
705
|
-
}
|
|
706
|
-
}
|
|
707
|
-
}
|
|
708
|
-
|
|
709
|
-
/// E43 Fix C helper (0.3.24 bug#3): probe live tmux for a window's existence
|
|
710
|
-
/// before issuing `split-window -t :<window>`. Uses `list_windows` first
|
|
711
|
-
/// (cheaper, authoritative when present); falls back to `list_targets` so
|
|
712
|
-
/// transports that don't seed `windows` directly still surface real entries.
|
|
713
|
-
fn window_present_in_live(
|
|
714
|
-
transport: &dyn crate::transport::Transport,
|
|
715
|
-
session: &SessionName,
|
|
716
|
-
window: &WindowName,
|
|
717
|
-
) -> bool {
|
|
718
|
-
if let Ok(windows) = transport.list_windows(session) {
|
|
719
|
-
if windows.iter().any(|w| w.as_str() == window.as_str()) {
|
|
720
|
-
return true;
|
|
721
|
-
}
|
|
722
|
-
}
|
|
723
|
-
if let Ok(targets) = transport.list_targets() {
|
|
724
|
-
if targets.iter().any(|t| {
|
|
725
|
-
t.session.as_str() == session.as_str()
|
|
726
|
-
&& t.window_name
|
|
727
|
-
.as_ref()
|
|
728
|
-
.is_some_and(|n| n.as_str() == window.as_str())
|
|
729
|
-
}) {
|
|
730
|
-
return true;
|
|
731
|
-
}
|
|
732
|
-
}
|
|
733
|
-
false
|
|
734
|
-
}
|
|
735
|
-
|
|
736
|
-
/// ---
|
|
737
|
-
/// purpose: 起该 workspace 的 coordinator
|
|
738
|
-
/// params:
|
|
739
|
-
/// team_key: 传给 coordinator 的团队键
|
|
740
|
-
/// returns: 启动摘要
|
|
741
|
-
/// errors: 启动失败时返回 StatePersist
|
|
742
|
-
/// ---
|
|
743
|
-
pub(super) fn start_coordinator_for_workspace(
|
|
744
|
-
workspace: &Path,
|
|
745
|
-
team_key: Option<&str>,
|
|
746
|
-
) -> Result<crate::lifecycle::CoordinatorStartSummary, LifecycleError> {
|
|
747
|
-
let workspace = crate::coordinator::WorkspacePath::new(workspace.to_path_buf());
|
|
748
|
-
crate::coordinator::health::start_coordinator_with_team(&workspace, team_key)
|
|
749
|
-
.map(|report| crate::lifecycle::CoordinatorStartSummary::from_start_report(&report))
|
|
750
|
-
.map_err(|e| LifecycleError::StatePersist(e.to_string()))
|
|
751
|
-
}
|
|
752
|
-
|
|
753
|
-
/// ---
|
|
754
|
-
/// purpose: 按团队持久化的 tmux endpoint 解析出后端,让 restart、add 与 fork 落在同一 socket
|
|
755
|
-
/// params:
|
|
756
|
-
/// team: 目标团队,None 时按唯一性选
|
|
757
|
-
/// returns: 绑定到该 endpoint 的 tmux 后端;冷 workspace 无持久 endpoint 时退到按 workspace 派生
|
|
758
|
-
/// errors: 团队目标歧义或未解析返回 TeamSelect;state 声明后端是 conpty 时也返回 TeamSelect 拒绝,不降级
|
|
759
|
-
/// ---
|
|
760
|
-
/// State-aware tmux backend resolver. Reads the team's persisted
|
|
761
|
-
/// `tmux_endpoint` (set at `team-agent launch` time and shared across
|
|
762
|
-
/// restart/add-agent/fork-agent) and constructs a TmuxBackend on THAT socket,
|
|
763
|
-
/// so add-agent / fork-agent / restart all spawn into the SAME tmux socket
|
|
764
|
-
/// the live team already runs on.
|
|
765
|
-
///
|
|
766
|
-
/// First-agent / cold workspace (no persisted endpoint) safely falls back to
|
|
767
|
-
/// `TmuxBackend::for_workspace(run_workspace)` — the canonical workspace-hash
|
|
768
|
-
/// socket. No panic, no None.
|
|
769
|
-
///
|
|
770
|
-
/// **Exposed `pub(crate)` for `lifecycle::launch::add_agent` / `fork_agent`
|
|
771
|
-
/// (`0.3.24 add-agent socket drift fix`). Previously `pub(super)` and shared
|
|
772
|
-
/// only within `lifecycle::restart`. Sharing the resolver across the lifecycle
|
|
773
|
-
/// module is the correct ownership: restart/add/fork all need the SAME socket
|
|
774
|
-
/// the live team uses, and duplicating the lookup invited drift.**
|
|
775
|
-
/// 0.5.x Phase 1d Batch 1: this legacy resolver stays tmux-only. It
|
|
776
|
-
/// still returns a concrete `TmuxBackend` because the 6 caller sites
|
|
777
|
-
/// (rebuild/agent/remove/launch) currently call `TmuxBackend`-specific
|
|
778
|
-
/// methods (`.tmux_endpoint()`, `.for_workspace()`, etc.). But it now
|
|
779
|
-
/// routes through the factory so its **selection semantics** match
|
|
780
|
-
/// the new `lifecycle_worker_transport_for_selected_state` API:
|
|
781
|
-
///
|
|
782
|
-
/// - If `state.transport.kind` is `"conpty"`, this legacy tmux-typed
|
|
783
|
-
/// resolver **fails-closed** with `TransportBackendKindMismatch`
|
|
784
|
-
/// rather than silently returning a tmux backend that would
|
|
785
|
-
/// spawn/inject into the wrong pane universe. That satisfies CR C-1
|
|
786
|
-
/// fail-closed while the migration of caller sites to the generic
|
|
787
|
-
/// `_transport_for_selected_state` API completes.
|
|
788
|
-
/// - Otherwise (default/legacy tmux endpoint), behavior is preserved
|
|
789
|
-
/// byte-for-byte through
|
|
790
|
-
/// `tmux_backend_for_runtime_state_or_workspace`.
|
|
791
|
-
pub(crate) fn lifecycle_worker_tmux_backend_for_selected_state(
|
|
792
|
-
run_workspace: &Path,
|
|
793
|
-
team: Option<&str>,
|
|
794
|
-
) -> Result<crate::tmux_backend::TmuxBackend, LifecycleError> {
|
|
795
|
-
let (state, refusal) = crate::state::projection::resolve_team_scoped_state(run_workspace, team)
|
|
796
|
-
.map_err(|e| LifecycleError::StatePersist(e.to_string()))?;
|
|
797
|
-
if let Some(refusal) = refusal {
|
|
798
|
-
let reason = refusal
|
|
799
|
-
.get("reason")
|
|
800
|
-
.and_then(|v| v.as_str())
|
|
801
|
-
.unwrap_or("team_target_unresolved");
|
|
802
|
-
let detail = refusal
|
|
803
|
-
.get("error")
|
|
804
|
-
.or_else(|| refusal.get("message"))
|
|
805
|
-
.and_then(|v| v.as_str())
|
|
806
|
-
.unwrap_or("");
|
|
807
|
-
return Err(LifecycleError::TeamSelect(format!("{reason}: {detail}")));
|
|
808
|
-
}
|
|
809
|
-
// CR C-1 fail-closed: state with `transport.kind=conpty` must NOT
|
|
810
|
-
// silently downgrade to a tmux backend just because the caller
|
|
811
|
-
// asked for the tmux-typed variant. Callers that see this refusal
|
|
812
|
-
// are expected to migrate to `lifecycle_worker_transport_for_selected_state`
|
|
813
|
-
// during Batch 2/3.
|
|
814
|
-
if let Some(state_ref) = state.as_ref() {
|
|
815
|
-
if let Some(kind) = state_ref
|
|
816
|
-
.pointer("/transport/kind")
|
|
817
|
-
.and_then(|v| v.as_str())
|
|
818
|
-
{
|
|
819
|
-
if kind.eq_ignore_ascii_case("conpty") {
|
|
820
|
-
return Err(LifecycleError::TeamSelect(format!(
|
|
821
|
-
"backend_kind_mismatch: state.transport.kind={kind:?} but the legacy \
|
|
822
|
-
tmux-typed lifecycle resolver was invoked; caller must migrate to \
|
|
823
|
-
`lifecycle_worker_transport_for_selected_state` (Phase 1d Batch 2/3)"
|
|
824
|
-
)));
|
|
825
|
-
}
|
|
826
|
-
}
|
|
827
|
-
}
|
|
828
|
-
Ok(state
|
|
829
|
-
.as_ref()
|
|
830
|
-
.map(|state| lifecycle_worker_tmux_backend_for_state(run_workspace, state))
|
|
831
|
-
.unwrap_or_else(|| crate::tmux_backend::TmuxBackend::for_workspace(run_workspace)))
|
|
832
|
-
}
|
|
833
|
-
|
|
834
|
-
/// ---
|
|
835
|
-
/// purpose: 与上面同样的团队选择语义,但返回工厂解析出的通用 transport
|
|
836
|
-
/// returns: 已解析的 transport,含后端种类、来源与提示
|
|
837
|
-
/// errors: 团队选择失败或工厂拒绝时返回 TeamSelect,读 state 失败返回 StatePersist
|
|
838
|
-
/// ---
|
|
839
|
-
/// 0.5.x Phase 1d Batch 1: new generic-typed lifecycle resolver.
|
|
840
|
-
///
|
|
841
|
-
/// Same team-selection semantics as the legacy tmux-typed variant, but
|
|
842
|
-
/// returns a `ResolvedTransport` from the factory so callers that can
|
|
843
|
-
/// hold `Box<dyn Transport>` (or the resolved `BackendKind`) work
|
|
844
|
-
/// against both tmux AND conpty state. Caller migration to this API
|
|
845
|
-
/// happens across Batch 2-5.
|
|
846
|
-
///
|
|
847
|
-
/// Fail-closed rules follow the factory (C-1 ×2, C-2 CLI-vs-state,
|
|
848
|
-
/// C-3 N38 notices via `ResolvedTransport.notices`).
|
|
849
|
-
pub(crate) fn lifecycle_worker_transport_for_selected_state(
|
|
850
|
-
run_workspace: &Path,
|
|
851
|
-
team: Option<&str>,
|
|
852
|
-
) -> Result<crate::transport_factory::ResolvedTransport, LifecycleError> {
|
|
853
|
-
let (state, refusal) = crate::state::projection::resolve_team_scoped_state(run_workspace, team)
|
|
854
|
-
.map_err(|e| LifecycleError::StatePersist(e.to_string()))?;
|
|
855
|
-
if let Some(refusal) = refusal {
|
|
856
|
-
let reason = refusal
|
|
857
|
-
.get("reason")
|
|
858
|
-
.and_then(|v| v.as_str())
|
|
859
|
-
.unwrap_or("team_target_unresolved");
|
|
860
|
-
let detail = refusal
|
|
861
|
-
.get("error")
|
|
862
|
-
.or_else(|| refusal.get("message"))
|
|
863
|
-
.and_then(|v| v.as_str())
|
|
864
|
-
.unwrap_or("");
|
|
865
|
-
return Err(LifecycleError::TeamSelect(format!("{reason}: {detail}")));
|
|
866
|
-
}
|
|
867
|
-
let input = crate::transport_factory::TransportFactoryInput::new(
|
|
868
|
-
run_workspace,
|
|
869
|
-
crate::transport_factory::TransportPurpose::LifecycleWorker,
|
|
870
|
-
)
|
|
871
|
-
.with_team_key(team)
|
|
872
|
-
.with_state(state.as_ref());
|
|
873
|
-
crate::transport_factory::resolve_transport(input)
|
|
874
|
-
.map_err(|e| LifecycleError::TeamSelect(e.to_string()))
|
|
875
|
-
}
|
|
876
|
-
|
|
877
|
-
/// ---
|
|
878
|
-
/// purpose: 由已取到的 state 解析 tmux 后端与它的来源
|
|
879
|
-
/// returns: 后端与 endpoint 来源
|
|
880
|
-
/// errors: state 声明后端是 conpty 时返回 TeamSelect
|
|
881
|
-
/// ---
|
|
882
|
-
pub(crate) fn lifecycle_worker_tmux_backend_selection_for_state(
|
|
883
|
-
run_workspace: &Path,
|
|
884
|
-
state: &serde_json::Value,
|
|
885
|
-
) -> Result<crate::tmux_backend::RuntimeTmuxBackendSelection, LifecycleError> {
|
|
886
|
-
if let Some(kind) = state.pointer("/transport/kind").and_then(|v| v.as_str()) {
|
|
887
|
-
if kind.eq_ignore_ascii_case("conpty") {
|
|
888
|
-
return Err(LifecycleError::TeamSelect(format!(
|
|
889
|
-
"backend_kind_mismatch: state.transport.kind={kind:?} but the legacy \
|
|
890
|
-
tmux-typed lifecycle resolver was invoked; caller must migrate to \
|
|
891
|
-
`lifecycle_worker_transport_for_selected_state` (Phase 1d Batch 2/3)"
|
|
892
|
-
)));
|
|
893
|
-
}
|
|
894
|
-
}
|
|
895
|
-
Ok(
|
|
896
|
-
crate::tmux_backend::tmux_backend_for_runtime_state_or_workspace(
|
|
897
|
-
run_workspace,
|
|
898
|
-
Some(state),
|
|
899
|
-
),
|
|
900
|
-
)
|
|
901
|
-
}
|
|
902
|
-
|
|
903
|
-
/// ---
|
|
904
|
-
/// purpose: 由已取到的 state 直接给出 tmux 后端
|
|
905
|
-
/// returns: 绑定到该 state endpoint 的后端,缺失时按 workspace 派生
|
|
906
|
-
/// ---
|
|
907
|
-
pub(super) fn lifecycle_worker_tmux_backend_for_state(
|
|
908
|
-
run_workspace: &Path,
|
|
909
|
-
state: &serde_json::Value,
|
|
910
|
-
) -> crate::tmux_backend::TmuxBackend {
|
|
911
|
-
crate::tmux_backend::tmux_backend_for_runtime_state_or_workspace(run_workspace, Some(state))
|
|
912
|
-
.backend
|
|
913
|
-
}
|
|
914
|
-
|
|
915
|
-
/// ---
|
|
916
|
-
/// purpose: 同步团队投影后落盘 restart 结果
|
|
917
|
-
/// params:
|
|
918
|
-
/// topology_authority_agent_ids: 本次以内存值为拓扑权威的席位
|
|
919
|
-
/// returns: 成功返回空值
|
|
920
|
-
/// errors: 落盘失败时返回 StatePersist
|
|
921
|
-
/// contract_id: lifecycle.common.save_restart_projected_state
|
|
922
|
-
/// ---
|
|
923
|
-
pub(super) fn save_restart_projected_state(
|
|
924
|
-
workspace: &Path,
|
|
925
|
-
state: &mut serde_json::Value,
|
|
926
|
-
team_key: &str,
|
|
927
|
-
topology_authority_agent_ids: &[&str],
|
|
928
|
-
) -> Result<(), LifecycleError> {
|
|
929
|
-
save_restart_projected_state_with_capture_backfill_skip(
|
|
930
|
-
workspace,
|
|
931
|
-
state,
|
|
932
|
-
team_key,
|
|
933
|
-
&[],
|
|
934
|
-
topology_authority_agent_ids,
|
|
935
|
-
)
|
|
936
|
-
}
|
|
937
|
-
|
|
938
|
-
/// ---
|
|
939
|
-
/// purpose: 同上,并可指定哪些席位跳过会话捕获回填
|
|
940
|
-
/// params:
|
|
941
|
-
/// skip_capture_backfill_agent_ids: 跳过回填的席位
|
|
942
|
-
/// returns: 成功返回空值
|
|
943
|
-
/// errors: 落盘失败时返回 StatePersist
|
|
944
|
-
/// contract_id: lifecycle.common.save_restart_projected_state
|
|
945
|
-
/// ---
|
|
946
|
-
pub(super) fn save_restart_projected_state_with_capture_backfill_skip(
|
|
947
|
-
workspace: &Path,
|
|
948
|
-
state: &mut serde_json::Value,
|
|
949
|
-
team_key: &str,
|
|
950
|
-
skip_capture_backfill_agent_ids: &[&str],
|
|
951
|
-
topology_authority_agent_ids: &[&str],
|
|
952
|
-
) -> Result<(), LifecycleError> {
|
|
953
|
-
sync_restart_team_projections(state, team_key);
|
|
954
|
-
crate::state::repository::StateRepository::new(workspace)
|
|
955
|
-
.save(
|
|
956
|
-
crate::state::repository::StateWriteIntent::RestartTeam {
|
|
957
|
-
team_key,
|
|
958
|
-
topology_authority_agent_ids,
|
|
959
|
-
skip_capture_backfill_agent_ids,
|
|
960
|
-
},
|
|
961
|
-
state,
|
|
962
|
-
)
|
|
963
|
-
.map_err(|e| LifecycleError::StatePersist(e.to_string()))
|
|
964
|
-
}
|
|
965
|
-
|
|
966
|
-
/// ---
|
|
967
|
-
/// purpose: 定出本次投影使用的团队键
|
|
968
|
-
/// returns: 显式 team 优先,其次 state 里的活跃键,最后由 state 推算
|
|
969
|
-
/// ---
|
|
970
|
-
pub(super) fn restart_projection_team_key(state: &serde_json::Value, team: Option<&str>) -> String {
|
|
971
|
-
team.filter(|key| !key.is_empty())
|
|
972
|
-
.map(str::to_string)
|
|
973
|
-
.or_else(|| {
|
|
974
|
-
state
|
|
975
|
-
.get("active_team_key")
|
|
976
|
-
.and_then(serde_json::Value::as_str)
|
|
977
|
-
.filter(|key| !key.is_empty())
|
|
978
|
-
.map(str::to_string)
|
|
979
|
-
})
|
|
980
|
-
.unwrap_or_else(|| crate::state::projection::team_state_key(state))
|
|
981
|
-
}
|
|
982
|
-
|
|
983
|
-
/// ---
|
|
984
|
-
/// purpose: 把当前顶层状态压实后写回 teams 表
|
|
985
|
-
/// params:
|
|
986
|
-
/// state: 就地改写;显式团队键允许覆盖或新建,别名键只在盘上已有且身份不冲突时才写
|
|
987
|
-
/// returns: teams 表缺失或为空时不动
|
|
988
|
-
/// ---
|
|
989
|
-
pub(super) fn sync_restart_team_projections(state: &mut serde_json::Value, team_key: &str) {
|
|
990
|
-
let Some(teams) = state.get("teams").and_then(serde_json::Value::as_object) else {
|
|
991
|
-
return;
|
|
992
|
-
};
|
|
993
|
-
if teams.is_empty() {
|
|
994
|
-
return;
|
|
995
|
-
}
|
|
996
|
-
let compact = crate::state::projection::compact_team_state(state);
|
|
997
|
-
let active_key = state
|
|
998
|
-
.get("active_team_key")
|
|
999
|
-
.and_then(serde_json::Value::as_str)
|
|
1000
|
-
.filter(|key| !key.is_empty())
|
|
1001
|
-
.map(str::to_string);
|
|
1002
|
-
let derived_key = crate::state::projection::team_state_key(state);
|
|
1003
|
-
let Some(teams) = state
|
|
1004
|
-
.get_mut("teams")
|
|
1005
|
-
.and_then(serde_json::Value::as_object_mut)
|
|
1006
|
-
else {
|
|
1007
|
-
return;
|
|
1008
|
-
};
|
|
1009
|
-
// 0.5.28 (`.team/artifacts/0527-realfail-layer2-leader-locate.md` §3):
|
|
1010
|
-
// 显式 team_key = 操作目标,始终允许覆盖 / 新建(该 helper 的正当职责)。
|
|
1011
|
-
// active/derived/"current" 三种别名兜底 = alias-identity 家族第三例,只有
|
|
1012
|
-
// 「盘上已有该 alias 条目」且「其身份与 compact 不冲突」时才允许写,
|
|
1013
|
-
// 避免 0.5.26 起死 sibling 保留时被活队 compact 硬克隆。禁止 alias 新建条目。
|
|
1014
|
-
let mut keys = Vec::new();
|
|
1015
|
-
let mut explicit = false;
|
|
1016
|
-
if !team_key.is_empty() {
|
|
1017
|
-
keys.push(team_key.to_string());
|
|
1018
|
-
explicit = true;
|
|
1019
|
-
}
|
|
1020
|
-
if let Some(active_key) = active_key {
|
|
1021
|
-
keys.push(active_key);
|
|
1022
|
-
}
|
|
1023
|
-
if !derived_key.is_empty() {
|
|
1024
|
-
keys.push(derived_key);
|
|
1025
|
-
}
|
|
1026
|
-
if teams.contains_key("current") {
|
|
1027
|
-
keys.push("current".to_string());
|
|
1028
|
-
}
|
|
1029
|
-
keys.sort();
|
|
1030
|
-
keys.dedup();
|
|
1031
|
-
for key in keys {
|
|
1032
|
-
let is_operation_target = explicit && key == team_key;
|
|
1033
|
-
if is_operation_target {
|
|
1034
|
-
teams.insert(key, compact.clone());
|
|
1035
|
-
continue;
|
|
1036
|
-
}
|
|
1037
|
-
let Some(existing) = teams.get(&key) else {
|
|
1038
|
-
// 别名条目不存在:不新建,避免把 hack alias 变成真队。
|
|
1039
|
-
continue;
|
|
1040
|
-
};
|
|
1041
|
-
if json_team_identity_matches(existing, &compact) {
|
|
1042
|
-
teams.insert(key, compact.clone());
|
|
1043
|
-
}
|
|
1044
|
-
}
|
|
1045
|
-
}
|
|
1046
|
-
|
|
1047
|
-
/// 0.5.28: 别名同步身份门。比较 existing 条目与 compact(即将写入)四个身份字段:
|
|
1048
|
-
/// `team_key`/`session_name`/`team_dir`/`spec_path`。legacy 缺字段容忍(两侧任一
|
|
1049
|
-
/// 缺则该字段不参与判定);已有字段必须相等,冲突即拒绝覆盖。
|
|
1050
|
-
fn json_team_identity_matches(existing: &serde_json::Value, compact: &serde_json::Value) -> bool {
|
|
1051
|
-
const FIELDS: &[&str] = &["team_key", "session_name", "team_dir", "spec_path"];
|
|
1052
|
-
for field in FIELDS {
|
|
1053
|
-
let lhs = existing
|
|
1054
|
-
.get(field)
|
|
1055
|
-
.and_then(serde_json::Value::as_str)
|
|
1056
|
-
.filter(|s| !s.is_empty());
|
|
1057
|
-
let rhs = compact
|
|
1058
|
-
.get(field)
|
|
1059
|
-
.and_then(serde_json::Value::as_str)
|
|
1060
|
-
.filter(|s| !s.is_empty());
|
|
1061
|
-
match (lhs, rhs) {
|
|
1062
|
-
(Some(a), Some(b)) if a != b => return false,
|
|
1063
|
-
_ => {}
|
|
1064
|
-
}
|
|
1065
|
-
}
|
|
1066
|
-
true
|
|
1067
|
-
}
|
|
1068
|
-
|
|
1069
|
-
/// ---
|
|
1070
|
-
/// purpose: 取 state 里的 session 名
|
|
1071
|
-
/// returns: 非空的 session_name,缺失时退到默认名
|
|
1072
|
-
/// ---
|
|
1073
|
-
pub(super) fn state_session_name(state: &serde_json::Value) -> SessionName {
|
|
1074
|
-
state
|
|
1075
|
-
.get("session_name")
|
|
1076
|
-
.and_then(|v| v.as_str())
|
|
1077
|
-
.filter(|s| !s.is_empty())
|
|
1078
|
-
.or_else(|| {
|
|
1079
|
-
state
|
|
1080
|
-
.get("leader_receiver")
|
|
1081
|
-
.and_then(|receiver| receiver.get("session_name"))
|
|
1082
|
-
.and_then(|v| v.as_str())
|
|
1083
|
-
.filter(|s| !s.is_empty())
|
|
1084
|
-
})
|
|
1085
|
-
.map(SessionName::new)
|
|
1086
|
-
.unwrap_or_else(|| SessionName::new("team-agent"))
|
|
1087
|
-
}
|
|
1088
|
-
|
|
1089
|
-
/// ---
|
|
1090
|
-
/// purpose: 判断 state 里是否记了非空 session 名
|
|
1091
|
-
/// returns: 记了则 true
|
|
1092
|
-
/// ---
|
|
1093
|
-
pub(super) fn session_name_present(state: &serde_json::Value) -> bool {
|
|
1094
|
-
state
|
|
1095
|
-
.get("session_name")
|
|
1096
|
-
.and_then(|v| v.as_str())
|
|
1097
|
-
.map(|s| !s.is_empty())
|
|
1098
|
-
.unwrap_or(false)
|
|
1099
|
-
}
|
|
1100
|
-
|
|
1101
|
-
/// Persist the session actually used for spawn when the selected team only
|
|
1102
|
-
/// had it on `leader_receiver`. Send/status route on `session_name`; an
|
|
1103
|
-
/// explicit null must stay a miss and must not rewrite a sibling team.
|
|
1104
|
-
pub(super) fn persist_selected_receiver_session_name(
|
|
1105
|
-
state: &mut serde_json::Value,
|
|
1106
|
-
session: &SessionName,
|
|
1107
|
-
) {
|
|
1108
|
-
if session_name_present(state) {
|
|
1109
|
-
return;
|
|
1110
|
-
}
|
|
1111
|
-
let Some(from_receiver) = state
|
|
1112
|
-
.get("leader_receiver")
|
|
1113
|
-
.and_then(|receiver| receiver.get("session_name"))
|
|
1114
|
-
.and_then(serde_json::Value::as_str)
|
|
1115
|
-
.filter(|name| !name.is_empty())
|
|
1116
|
-
else {
|
|
1117
|
-
return;
|
|
1118
|
-
};
|
|
1119
|
-
if from_receiver != session.as_str() {
|
|
1120
|
-
return;
|
|
1121
|
-
}
|
|
1122
|
-
if let Some(object) = state.as_object_mut() {
|
|
1123
|
-
object.insert(
|
|
1124
|
-
"session_name".to_string(),
|
|
1125
|
-
serde_json::json!(session.as_str()),
|
|
1126
|
-
);
|
|
1127
|
-
}
|
|
1128
|
-
}
|
|
1129
|
-
|
|
1130
|
-
/// ---
|
|
1131
|
-
/// purpose: 探测 session 是否存活
|
|
1132
|
-
/// params:
|
|
1133
|
-
/// default: 探测本身 panic 时采用的兜底判定
|
|
1134
|
-
/// returns: transport 明确回答时用它;返回错误时判为不存活;探测 panic 时用兜底值
|
|
1135
|
-
/// ---
|
|
1136
|
-
pub(super) fn session_live_or_default(
|
|
1137
|
-
transport: &dyn crate::transport::Transport,
|
|
1138
|
-
session_name: &SessionName,
|
|
1139
|
-
default: bool,
|
|
1140
|
-
) -> bool {
|
|
1141
|
-
match std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
|
|
1142
|
-
transport.has_session(session_name)
|
|
1143
|
-
})) {
|
|
1144
|
-
Ok(Ok(live)) => live,
|
|
1145
|
-
Ok(Err(_)) => false,
|
|
1146
|
-
Err(_) => default,
|
|
1147
|
-
}
|
|
1148
|
-
}
|
|
1149
|
-
|
|
1150
|
-
/// ---
|
|
1151
|
-
/// purpose: 取席位行的 provider
|
|
1152
|
-
/// returns: 解析成功用它,缺失或不认识时退到已退役的 legacy codex 键(其后一律拒绝,不会起席)
|
|
1153
|
-
/// ---
|
|
1154
|
-
pub(super) fn agent_provider(agent: &serde_json::Value) -> Provider {
|
|
1155
|
-
agent
|
|
1156
|
-
.get("provider")
|
|
1157
|
-
.and_then(|v| v.as_str())
|
|
1158
|
-
.and_then(parse_provider)
|
|
1159
|
-
.unwrap_or(Provider::Codex)
|
|
1160
|
-
}
|
|
1161
|
-
|
|
1162
|
-
/// ---
|
|
1163
|
-
/// purpose: 取席位行的 auth_mode
|
|
1164
|
-
/// returns: 解析成功用它,缺失或不认识时退到 subscription
|
|
1165
|
-
/// ---
|
|
1166
|
-
pub(super) fn agent_auth_mode(agent: &serde_json::Value) -> AuthMode {
|
|
1167
|
-
agent
|
|
1168
|
-
.get("auth_mode")
|
|
1169
|
-
.and_then(|v| v.as_str())
|
|
1170
|
-
.and_then(parse_auth_mode)
|
|
1171
|
-
.unwrap_or(AuthMode::Subscription)
|
|
1172
|
-
}
|
|
1173
|
-
|
|
1174
|
-
/// ---
|
|
1175
|
-
/// purpose: 取席位行记录的会话 id
|
|
1176
|
-
/// returns: 非空时返回,否则 None
|
|
1177
|
-
/// ---
|
|
1178
|
-
pub(super) fn agent_session_id(agent: &serde_json::Value) -> Option<SessionId> {
|
|
1179
|
-
agent
|
|
1180
|
-
.get("session_id")
|
|
1181
|
-
.and_then(|v| v.as_str())
|
|
1182
|
-
.filter(|s| !s.is_empty())
|
|
1183
|
-
.map(SessionId::new)
|
|
1184
|
-
}
|
|
1185
|
-
|
|
1186
|
-
/// ---
|
|
1187
|
-
/// purpose: 取席位行记录的 rollout 路径
|
|
1188
|
-
/// returns: 非空时返回,否则 None
|
|
1189
|
-
/// ---
|
|
1190
|
-
pub(super) fn agent_rollout_path(agent: &serde_json::Value) -> Option<RolloutPath> {
|
|
1191
|
-
agent
|
|
1192
|
-
.get("rollout_path")
|
|
1193
|
-
.and_then(|v| v.as_str())
|
|
1194
|
-
.filter(|s| !s.is_empty())
|
|
1195
|
-
.map(RolloutPath::new)
|
|
1196
|
-
}
|
|
1197
|
-
|
|
1198
|
-
/// ---
|
|
1199
|
-
/// purpose: 判断该席位的 resume 依据是否存在
|
|
1200
|
-
/// returns: 探测结果里的存在位
|
|
1201
|
-
/// ---
|
|
1202
|
-
pub(super) fn resume_backing_exists_for_agent(
|
|
1203
|
-
workspace: &Path,
|
|
1204
|
-
agent_id: &AgentId,
|
|
1205
|
-
agent: &serde_json::Value,
|
|
1206
|
-
provider: Provider,
|
|
1207
|
-
session_id: &SessionId,
|
|
1208
|
-
rollout_path: Option<&RolloutPath>,
|
|
1209
|
-
) -> bool {
|
|
1210
|
-
resume_backing_probe_for_agent(
|
|
1211
|
-
workspace,
|
|
1212
|
-
agent_id,
|
|
1213
|
-
agent,
|
|
1214
|
-
provider,
|
|
1215
|
-
session_id,
|
|
1216
|
-
rollout_path,
|
|
1217
|
-
)
|
|
1218
|
-
.exists
|
|
1219
|
-
}
|
|
1220
|
-
|
|
1221
|
-
/// Layer 2 self-healing (leader follow-up 2026-06-22): result of probing
|
|
1222
|
-
/// the provider backing store for a resumable session. `checked_paths`
|
|
1223
|
-
/// reports every path the runtime probed so the operator can see WHICH
|
|
1224
|
-
/// places we looked — surfaced into the
|
|
1225
|
-
/// `ResumeRefusalReason::SessionBackingStoreMissing.checked_paths`
|
|
1226
|
-
/// field, the CLI JSON `unresumable[].checked_paths` array, and the
|
|
1227
|
-
/// `restart.resume_decision` event payload.
|
|
1228
|
-
#[derive(Debug, Clone, PartialEq, Eq)]
|
|
1229
|
-
pub(super) struct BackingProbeResult {
|
|
1230
|
-
pub exists: bool,
|
|
1231
|
-
pub checked_paths: Vec<PathBuf>,
|
|
1232
|
-
}
|
|
1233
|
-
|
|
1234
|
-
#[derive(Debug, Clone, PartialEq, Eq)]
|
|
1235
|
-
pub(crate) struct SessionIdentityProbeResult {
|
|
1236
|
-
pub identity_ok: Option<bool>,
|
|
1237
|
-
pub embedded_agent_id: Option<String>,
|
|
1238
|
-
pub rollout_path: Option<PathBuf>,
|
|
1239
|
-
}
|
|
1240
|
-
|
|
1241
|
-
/// ---
|
|
1242
|
-
/// purpose: 探测 rollout 文件里嵌的席位身份是否与本席位一致
|
|
1243
|
-
/// params:
|
|
1244
|
-
/// _provider: 未参与判定
|
|
1245
|
-
/// rollout_path: 没有路径时三项都返回未知
|
|
1246
|
-
/// returns: 一致性判定、读到的嵌入席位名与实际探测路径;读不出嵌入身份时一致性为未知
|
|
1247
|
-
/// ---
|
|
1248
|
-
pub(crate) fn session_identity_probe_for_agent(
|
|
1249
|
-
agent_id: &AgentId,
|
|
1250
|
-
_provider: Provider,
|
|
1251
|
-
rollout_path: Option<&RolloutPath>,
|
|
1252
|
-
) -> SessionIdentityProbeResult {
|
|
1253
|
-
let Some(path) = rollout_path.map(RolloutPath::as_path) else {
|
|
1254
|
-
return SessionIdentityProbeResult {
|
|
1255
|
-
identity_ok: None,
|
|
1256
|
-
embedded_agent_id: None,
|
|
1257
|
-
rollout_path: None,
|
|
1258
|
-
};
|
|
1259
|
-
};
|
|
1260
|
-
let embedded_agent_id =
|
|
1261
|
-
crate::provider::session_scan::common::rollout_path_embedded_team_agent_worker_id(path);
|
|
1262
|
-
let identity_ok = embedded_agent_id
|
|
1263
|
-
.as_deref()
|
|
1264
|
-
.map(|embedded| embedded == agent_id.as_str());
|
|
1265
|
-
SessionIdentityProbeResult {
|
|
1266
|
-
identity_ok,
|
|
1267
|
-
embedded_agent_id,
|
|
1268
|
-
rollout_path: Some(path.to_path_buf()),
|
|
1269
|
-
}
|
|
1270
|
-
}
|
|
1271
|
-
|
|
1272
|
-
/// ---
|
|
1273
|
-
/// purpose: 共享会话身份判定(Pi fork 血统随遗留 Pi worker 运行时一并移除)
|
|
1274
|
-
/// params:
|
|
1275
|
-
/// workspace/state/agent: 保留调用形状
|
|
1276
|
-
/// rollout_path: 当前席位的精确 backing
|
|
1277
|
-
/// returns: 字面身份判定结果
|
|
1278
|
-
/// ---
|
|
1279
|
-
pub(crate) fn session_identity_probe_for_agent_with_fork_ancestry(
|
|
1280
|
-
workspace: &Path,
|
|
1281
|
-
state: &serde_json::Value,
|
|
1282
|
-
agent_id: &AgentId,
|
|
1283
|
-
agent: &serde_json::Value,
|
|
1284
|
-
provider: Provider,
|
|
1285
|
-
rollout_path: Option<&RolloutPath>,
|
|
1286
|
-
) -> SessionIdentityProbeResult {
|
|
1287
|
-
let _ = (workspace, state, agent);
|
|
1288
|
-
session_identity_probe_for_agent(agent_id, provider, rollout_path)
|
|
1289
|
-
}
|
|
1290
|
-
|
|
1291
|
-
/// ---
|
|
1292
|
-
/// purpose: 按 provider 分支探测 resume 依据,并记录所有查过的路径
|
|
1293
|
-
/// returns: 存在位与查过的路径列表;持久化的 rollout 路径即使不存在也记进列表
|
|
1294
|
-
/// ---
|
|
1295
|
-
pub(super) fn resume_backing_probe_for_agent(
|
|
1296
|
-
workspace: &Path,
|
|
1297
|
-
agent_id: &AgentId,
|
|
1298
|
-
agent: &serde_json::Value,
|
|
1299
|
-
provider: Provider,
|
|
1300
|
-
session_id: &SessionId,
|
|
1301
|
-
rollout_path: Option<&RolloutPath>,
|
|
1302
|
-
) -> BackingProbeResult {
|
|
1303
|
-
let mut checked_paths: Vec<PathBuf> = Vec::new();
|
|
1304
|
-
|
|
1305
|
-
// Always record the persisted rollout_path even when it does not
|
|
1306
|
-
// exist — that "we looked here" tells the operator that state has a
|
|
1307
|
-
// pointer but the file is gone.
|
|
1308
|
-
if let Some(path) = rollout_path.map(RolloutPath::as_path) {
|
|
1309
|
-
checked_paths.push(path.to_path_buf());
|
|
1310
|
-
}
|
|
1311
|
-
|
|
1312
|
-
let exists = match provider {
|
|
1313
|
-
provider if !provider_supports_resume(provider) => {
|
|
1314
|
-
let _ = (workspace, agent_id, agent, session_id, rollout_path);
|
|
1315
|
-
false
|
|
1316
|
-
}
|
|
1317
|
-
Provider::Copilot => {
|
|
1318
|
-
if let Some(home) = std::env::var_os("HOME").map(PathBuf::from) {
|
|
1319
|
-
checked_paths.push(home.join(".copilot/session-store.db"));
|
|
1320
|
-
}
|
|
1321
|
-
copilot_session_store_has_session(session_id.as_str())
|
|
1322
|
-
}
|
|
1323
|
-
// Claude Code, Codex, Grok and Pi workers run only through their contract leaves.
|
|
1324
|
-
Provider::Claude
|
|
1325
|
-
| Provider::ClaudeCode
|
|
1326
|
-
| Provider::Codex
|
|
1327
|
-
| Provider::Grok
|
|
1328
|
-
| Provider::Pi
|
|
1329
|
-
| Provider::GeminiCli
|
|
1330
|
-
| Provider::Fake => false,
|
|
1331
|
-
};
|
|
1332
|
-
|
|
1333
|
-
// Deduplicate while preserving order (HashSet would lose deterministic
|
|
1334
|
-
// ordering needed for stable JSON/event output).
|
|
1335
|
-
let mut seen: std::collections::BTreeSet<PathBuf> = std::collections::BTreeSet::new();
|
|
1336
|
-
checked_paths.retain(|p| seen.insert(p.clone()));
|
|
1337
|
-
|
|
1338
|
-
BackingProbeResult {
|
|
1339
|
-
exists,
|
|
1340
|
-
checked_paths,
|
|
1341
|
-
}
|
|
1342
|
-
}
|
|
1343
|
-
|
|
1344
|
-
/// ---
|
|
1345
|
-
/// purpose: 判断该 provider 是否支持 resume
|
|
1346
|
-
/// returns: 由 provider 适配器的能力位给出
|
|
1347
|
-
/// contract_id: lifecycle.common.provider_supports_resume
|
|
1348
|
-
/// ---
|
|
1349
|
-
pub(super) fn provider_supports_resume(provider: Provider) -> bool {
|
|
1350
|
-
crate::provider::get_adapter(provider).caps().resume
|
|
1351
|
-
}
|
|
1352
|
-
|
|
1353
|
-
/// ---
|
|
1354
|
-
/// purpose: 由 provider wire 名判断是否支持 resume
|
|
1355
|
-
/// returns: 名字不认识时为 false
|
|
1356
|
-
/// contract_id: lifecycle.common.provider_supports_resume
|
|
1357
|
-
/// ---
|
|
1358
|
-
pub(super) fn provider_wire_supports_resume(provider: &str) -> bool {
|
|
1359
|
-
parse_provider(provider)
|
|
1360
|
-
.map(provider_supports_resume)
|
|
1361
|
-
.unwrap_or(false)
|
|
1362
|
-
}
|
|
1363
|
-
|
|
1364
|
-
fn copilot_session_store_has_session(session_id: &str) -> bool {
|
|
1365
|
-
let Some(home) = std::env::var_os("HOME").map(PathBuf::from) else {
|
|
1366
|
-
return false;
|
|
1367
|
-
};
|
|
1368
|
-
let db_path = home.join(".copilot").join("session-store.db");
|
|
1369
|
-
if !db_path.exists() {
|
|
1370
|
-
return false;
|
|
1371
|
-
}
|
|
1372
|
-
let Ok(conn) = rusqlite::Connection::open_with_flags(
|
|
1373
|
-
db_path,
|
|
1374
|
-
rusqlite::OpenFlags::SQLITE_OPEN_READ_ONLY | rusqlite::OpenFlags::SQLITE_OPEN_NO_MUTEX,
|
|
1375
|
-
) else {
|
|
1376
|
-
return false;
|
|
1377
|
-
};
|
|
1378
|
-
conn.query_row(
|
|
1379
|
-
"select 1 from sessions where id = ?1 limit 1",
|
|
1380
|
-
[session_id],
|
|
1381
|
-
|_| Ok(()),
|
|
1382
|
-
)
|
|
1383
|
-
.is_ok()
|
|
1384
|
-
}
|
|
1385
|
-
|
|
1386
|
-
/// ---
|
|
1387
|
-
/// purpose: 对缺会话的席位做一次捕获尝试
|
|
1388
|
-
/// params:
|
|
1389
|
-
/// state: 就地改写
|
|
1390
|
-
/// returns: 本次是否改动了 state
|
|
1391
|
-
/// errors: 捕获过程出错时返回 Provider
|
|
1392
|
-
/// ---
|
|
1393
|
-
pub(crate) fn refresh_missing_provider_sessions(
|
|
1394
|
-
state: &mut serde_json::Value,
|
|
1395
|
-
) -> Result<bool, LifecycleError> {
|
|
1396
|
-
crate::session_capture::capture_missing_provider_sessions_once(
|
|
1397
|
-
state,
|
|
1398
|
-
&mut crate::provider::get_adapter,
|
|
1399
|
-
false,
|
|
1400
|
-
0,
|
|
1401
|
-
)
|
|
1402
|
-
.map(|report| report.changed)
|
|
1403
|
-
.map_err(|e| LifecycleError::Provider(e.to_string()))
|
|
1404
|
-
}
|
|
1405
|
-
|
|
1406
|
-
/// ---
|
|
1407
|
-
/// purpose: 有界等待缺会话席位收敛,并逐轮写进度事件
|
|
1408
|
-
/// params:
|
|
1409
|
-
/// deadline: 等待上限
|
|
1410
|
-
/// poll_interval: 轮询间隔
|
|
1411
|
-
/// allow_fresh: 只写进事件载荷,不改变收敛判定
|
|
1412
|
-
/// returns: 收敛结论
|
|
1413
|
-
/// errors: 收敛过程出错时返回 StatePersist
|
|
1414
|
-
/// ---
|
|
1415
|
-
pub(crate) fn converge_missing_provider_sessions(
|
|
1416
|
-
state: &mut serde_json::Value,
|
|
1417
|
-
deadline: std::time::Duration,
|
|
1418
|
-
poll_interval: std::time::Duration,
|
|
1419
|
-
workspace: &Path,
|
|
1420
|
-
allow_fresh: bool,
|
|
1421
|
-
) -> Result<crate::session_capture::SessionConvergence, LifecycleError> {
|
|
1422
|
-
crate::session_capture::converge_missing_provider_sessions(
|
|
1423
|
-
state,
|
|
1424
|
-
&mut crate::provider::get_adapter,
|
|
1425
|
-
deadline,
|
|
1426
|
-
poll_interval,
|
|
1427
|
-
restart_required_missing_session_agent_ids,
|
|
1428
|
-
|progress| {
|
|
1429
|
-
write_session_convergence_progress_event(
|
|
1430
|
-
workspace,
|
|
1431
|
-
serde_json::json!({
|
|
1432
|
-
"iteration": progress.iteration,
|
|
1433
|
-
"elapsed_ms": progress.elapsed_ms,
|
|
1434
|
-
"deadline_ms": progress.deadline_ms,
|
|
1435
|
-
"changed": progress.changed,
|
|
1436
|
-
"assigned": progress.assigned,
|
|
1437
|
-
"missing": progress.missing,
|
|
1438
|
-
"required_missing_agent_ids": progress.required_missing_agent_ids,
|
|
1439
|
-
"pending_agent_ids": progress.pending_agent_ids,
|
|
1440
|
-
"candidate_count_by_agent": progress.candidate_count_by_agent,
|
|
1441
|
-
"remaining_ms": progress.remaining_ms,
|
|
1442
|
-
"allow_fresh": allow_fresh,
|
|
1443
|
-
}),
|
|
1444
|
-
)
|
|
1445
|
-
},
|
|
1446
|
-
)
|
|
1447
|
-
.map_err(LifecycleError::StatePersist)
|
|
1448
|
-
}
|
|
1449
|
-
|
|
1450
|
-
fn write_session_convergence_progress_event(
|
|
1451
|
-
workspace: &Path,
|
|
1452
|
-
fields: serde_json::Value,
|
|
1453
|
-
) -> Result<(), String> {
|
|
1454
|
-
crate::event_log::EventLog::new(workspace)
|
|
1455
|
-
.write(crate::event_log::PROVIDER_SESSION_CONVERGING, fields)
|
|
1456
|
-
.map(|_| ())
|
|
1457
|
-
.map_err(|e| e.to_string())
|
|
1458
|
-
}
|
|
1459
|
-
|
|
1460
|
-
/// ---
|
|
1461
|
-
/// purpose: 列出必须等到会话收敛才能 restart 的席位
|
|
1462
|
-
/// returns: 排序后的席位 id;只保留无会话、状态为 running 且确有待保留上下文的席位,从未捕获过的席位不进入
|
|
1463
|
-
/// ---
|
|
1464
|
-
pub(crate) fn restart_required_missing_session_agent_ids(state: &serde_json::Value) -> Vec<String> {
|
|
1465
|
-
let mut missing = crate::session_capture::incomplete_resumable_agent_ids(state)
|
|
1466
|
-
.into_iter()
|
|
1467
|
-
.filter(|agent_id| {
|
|
1468
|
-
let Some(agent) = state.get("agents").and_then(|agents| agents.get(agent_id)) else {
|
|
1469
|
-
return false;
|
|
1470
|
-
};
|
|
1471
|
-
let missing_session_id = agent
|
|
1472
|
-
.get("session_id")
|
|
1473
|
-
.and_then(|value| value.as_str())
|
|
1474
|
-
.is_none_or(|session| session.is_empty());
|
|
1475
|
-
let is_running = agent
|
|
1476
|
-
.get("status")
|
|
1477
|
-
.and_then(|value| value.as_str())
|
|
1478
|
-
.is_some_and(|status| status == "running");
|
|
1479
|
-
// E6 层2 (C2) + RESTART-RESUME-001 (0.4.8): required-missing
|
|
1480
|
-
// predicate gates on session_id absence + running, but ALSO
|
|
1481
|
-
// skips never-captured workers (no session_id AND no context
|
|
1482
|
-
// signals at all). A never-captured worker has nothing to
|
|
1483
|
-
// lose by fresh-start, so it must not block convergence and
|
|
1484
|
-
// burn the capture deadline. This matches the selection-stage
|
|
1485
|
-
// partial-resume semantic in
|
|
1486
|
-
// selection.rs::classify_resume_decision (never_captured →
|
|
1487
|
-
// FreshStart without --allow-fresh).
|
|
1488
|
-
//
|
|
1489
|
-
// The "has context to preserve" signal is the shared
|
|
1490
|
-
// restart_agent_has_context_to_preserve helper: first_send_at
|
|
1491
|
-
// (leader→worker delivery), last_result_at (MCP report path
|
|
1492
|
-
// that may skip first_send_at), or task_prompt_delivered.
|
|
1493
|
-
// Only context-bearing null-session workers continue to
|
|
1494
|
-
// require convergence (so we never silently drop context).
|
|
1495
|
-
missing_session_id
|
|
1496
|
-
&& is_running
|
|
1497
|
-
&& !super::selection::restart_agent_never_captured(agent, None)
|
|
1498
|
-
})
|
|
1499
|
-
.collect::<Vec<_>>();
|
|
1500
|
-
missing.sort();
|
|
1501
|
-
missing
|
|
1502
|
-
}
|
|
1503
|
-
/// ---
|
|
1504
|
-
/// purpose: 取该席位的窗口名
|
|
1505
|
-
/// returns: 席位行里的非空 window,缺失时用席位 id
|
|
1506
|
-
/// ---
|
|
1507
|
-
pub(super) fn agent_window(agent: &serde_json::Value, agent_id: &AgentId) -> String {
|
|
1508
|
-
agent
|
|
1509
|
-
.get("window")
|
|
1510
|
-
.and_then(|v| v.as_str())
|
|
1511
|
-
.filter(|s| !s.is_empty())
|
|
1512
|
-
.unwrap_or_else(|| agent_id.as_str())
|
|
1513
|
-
.to_string()
|
|
1514
|
-
}
|
|
1515
|
-
|
|
1516
|
-
pub(super) use crate::provider::wire::{parse_provider, provider_wire};
|
|
1517
|
-
|
|
1518
|
-
/// ---
|
|
1519
|
-
/// purpose: 把 auth_mode 字符串解析成枚举
|
|
1520
|
-
/// returns: 只认三种取值,未知返回 None
|
|
1521
|
-
/// ---
|
|
1522
|
-
pub(super) fn parse_auth_mode(raw: &str) -> Option<AuthMode> {
|
|
1523
|
-
match raw {
|
|
1524
|
-
"subscription" => Some(AuthMode::Subscription),
|
|
1525
|
-
"official_api" => Some(AuthMode::OfficialApi),
|
|
1526
|
-
"compatible_api" => Some(AuthMode::CompatibleApi),
|
|
1527
|
-
_ => None,
|
|
1528
|
-
}
|
|
1529
|
-
}
|
|
1530
|
-
|
|
1531
|
-
/// ---
|
|
1532
|
-
/// purpose: 从给定目录读出 team spec
|
|
1533
|
-
/// returns: 解析后的 YAML
|
|
1534
|
-
/// errors: 文件不存在返回 TeamSelect,读文件或解析失败返回 Compile
|
|
1535
|
-
/// ---
|
|
1536
|
-
pub(super) fn load_team_spec(workspace: &Path) -> Result<YamlValue, LifecycleError> {
|
|
1537
|
-
let spec_path = workspace.join("team.spec.yaml");
|
|
1538
|
-
if !spec_path.exists() {
|
|
1539
|
-
return Err(LifecycleError::TeamSelect(format!(
|
|
1540
|
-
"missing spec: {}",
|
|
1541
|
-
spec_path.display()
|
|
1542
|
-
)));
|
|
1543
|
-
}
|
|
1544
|
-
let text = std::fs::read_to_string(&spec_path)
|
|
1545
|
-
.map_err(|e| LifecycleError::Compile(format!("{}: {e}", spec_path.display())))?;
|
|
1546
|
-
yaml::loads(&text).map_err(|e| LifecycleError::Compile(e.to_string()))
|
|
1547
|
-
}
|
|
1548
|
-
|
|
1549
|
-
/// ---
|
|
1550
|
-
/// purpose: 在 spec 的 agents 列表里找该席位
|
|
1551
|
-
/// returns: 命中的节点;该 id 其实是 leader 时返回 None
|
|
1552
|
-
/// ---
|
|
1553
|
-
pub(super) fn find_spec_agent<'a>(
|
|
1554
|
-
spec: &'a YamlValue,
|
|
1555
|
-
agent_id: &AgentId,
|
|
1556
|
-
) -> Option<&'a YamlValue> {
|
|
1557
|
-
let leader_is_agent = spec
|
|
1558
|
-
.get("leader")
|
|
1559
|
-
.and_then(|v| v.get("id"))
|
|
1560
|
-
.and_then(YamlValue::as_str)
|
|
1561
|
-
.map(|id| id == agent_id.as_str())
|
|
1562
|
-
.unwrap_or(false);
|
|
1563
|
-
if leader_is_agent {
|
|
1564
|
-
return None;
|
|
1565
|
-
}
|
|
1566
|
-
spec.get("agents")?.as_list()?.iter().find(|agent| {
|
|
1567
|
-
agent
|
|
1568
|
-
.get("id")
|
|
1569
|
-
.and_then(YamlValue::as_str)
|
|
1570
|
-
.map(|id| id == agent_id.as_str())
|
|
1571
|
-
.unwrap_or(false)
|
|
1572
|
-
})
|
|
1573
|
-
}
|
|
1574
|
-
|
|
1575
|
-
/// ---
|
|
1576
|
-
/// purpose: 构造未知席位的错误
|
|
1577
|
-
/// returns: 带席位 id 的 RequirementUnmet
|
|
1578
|
-
/// ---
|
|
1579
|
-
pub(super) fn unknown_worker(agent_id: &AgentId) -> LifecycleError {
|
|
1580
|
-
LifecycleError::RequirementUnmet(format!("unknown worker agent id: {agent_id}"))
|
|
1581
|
-
}
|
|
1582
|
-
|
|
1583
|
-
/// ---
|
|
1584
|
-
/// purpose: 定出 session 名,state 缺失时回落到 spec
|
|
1585
|
-
/// returns: 依次取 state 的 session_name、spec 的 runtime.session_name、由 team 名派生,最后用默认名
|
|
1586
|
-
/// ---
|
|
1587
|
-
pub(super) fn state_session_name_from_spec(
|
|
1588
|
-
state: &serde_json::Value,
|
|
1589
|
-
spec: &YamlValue,
|
|
1590
|
-
) -> SessionName {
|
|
1591
|
-
state
|
|
1592
|
-
.get("session_name")
|
|
1593
|
-
.and_then(|v| v.as_str())
|
|
1594
|
-
.filter(|s| !s.is_empty())
|
|
1595
|
-
.map(SessionName::new)
|
|
1596
|
-
.or_else(|| {
|
|
1597
|
-
spec.get("runtime")
|
|
1598
|
-
.and_then(|v| v.get("session_name"))
|
|
1599
|
-
.and_then(YamlValue::as_str)
|
|
1600
|
-
.map(SessionName::new)
|
|
1601
|
-
})
|
|
1602
|
-
.or_else(|| {
|
|
1603
|
-
spec.get("team")
|
|
1604
|
-
.and_then(|v| v.get("name"))
|
|
1605
|
-
.and_then(YamlValue::as_str)
|
|
1606
|
-
.map(|name| SessionName::new(format!("team-{name}")))
|
|
1607
|
-
})
|
|
1608
|
-
.unwrap_or_else(|| SessionName::new("team-agent"))
|
|
1609
|
-
}
|
|
1610
|
-
|
|
1611
|
-
/// ---
|
|
1612
|
-
/// purpose: 把席位标记为已停并清掉 window 与 pane_id 字段(pane_pid 等其余字段保留)
|
|
1613
|
-
/// params:
|
|
1614
|
-
/// state: 就地改写,非对象时先重置成空对象
|
|
1615
|
-
/// spec_agent: 提供 provider 等最小投影
|
|
1616
|
-
/// returns: 成功返回空值
|
|
1617
|
-
/// errors: state 结构不是对象时返回 StatePersist
|
|
1618
|
-
/// ---
|
|
1619
|
-
pub(super) fn mark_agent_stopped(
|
|
1620
|
-
state: &mut serde_json::Value,
|
|
1621
|
-
agent_id: &AgentId,
|
|
1622
|
-
spec_agent: &YamlValue,
|
|
1623
|
-
window: &str,
|
|
1624
|
-
) -> Result<(), LifecycleError> {
|
|
1625
|
-
if !state.is_object() {
|
|
1626
|
-
*state = serde_json::json!({});
|
|
1627
|
-
}
|
|
1628
|
-
let Some(root) = state.as_object_mut() else {
|
|
1629
|
-
return Err(LifecycleError::StatePersist(
|
|
1630
|
-
"runtime state root is not an object".to_string(),
|
|
1631
|
-
));
|
|
1632
|
-
};
|
|
1633
|
-
let agents = root
|
|
1634
|
-
.entry("agents".to_string())
|
|
1635
|
-
.or_insert_with(|| serde_json::json!({}));
|
|
1636
|
-
if !agents.is_object() {
|
|
1637
|
-
*agents = serde_json::json!({});
|
|
1638
|
-
}
|
|
1639
|
-
let Some(agent_map) = agents.as_object_mut() else {
|
|
1640
|
-
return Err(LifecycleError::StatePersist(
|
|
1641
|
-
"runtime state agents is not an object".to_string(),
|
|
1642
|
-
));
|
|
1643
|
-
};
|
|
1644
|
-
let provider = spec_agent
|
|
1645
|
-
.get("provider")
|
|
1646
|
-
.and_then(YamlValue::as_str)
|
|
1647
|
-
.unwrap_or("codex");
|
|
1648
|
-
let entry = agent_map
|
|
1649
|
-
.entry(agent_id.as_str().to_string())
|
|
1650
|
-
.or_insert_with(|| serde_json::json!({}));
|
|
1651
|
-
if !entry.is_object() {
|
|
1652
|
-
*entry = serde_json::json!({});
|
|
1653
|
-
}
|
|
1654
|
-
let Some(obj) = entry.as_object_mut() else {
|
|
1655
|
-
return Err(LifecycleError::StatePersist(
|
|
1656
|
-
"agent state is not an object".to_string(),
|
|
1657
|
-
));
|
|
1658
|
-
};
|
|
1659
|
-
obj.insert("status".to_string(), serde_json::json!("stopped"));
|
|
1660
|
-
obj.insert("provider".to_string(), serde_json::json!(provider));
|
|
1661
|
-
obj.insert("agent_id".to_string(), serde_json::json!(agent_id.as_str()));
|
|
1662
|
-
obj.insert("last_window".to_string(), serde_json::json!(window));
|
|
1663
|
-
obj.remove("window");
|
|
1664
|
-
obj.remove("pane_id");
|
|
1665
|
-
Ok(())
|
|
1666
|
-
}
|
|
1667
|
-
|
|
1668
|
-
/// ---
|
|
1669
|
-
/// purpose: 窗口本就存在而未新起进程时,把席位标记为运行中
|
|
1670
|
-
/// params:
|
|
1671
|
-
/// pane: 探到的现有 pane,用于写 pane id 与进程号
|
|
1672
|
-
/// returns: 成功返回空值
|
|
1673
|
-
/// errors: state 结构不是对象时返回 StatePersist
|
|
1674
|
-
/// ---
|
|
1675
|
-
pub(super) fn mark_agent_running_noop(
|
|
1676
|
-
state: &mut serde_json::Value,
|
|
1677
|
-
agent_id: &AgentId,
|
|
1678
|
-
session_name: &SessionName,
|
|
1679
|
-
window: &str,
|
|
1680
|
-
pane: Option<&crate::transport::PaneInfo>,
|
|
1681
|
-
) -> Result<(), LifecycleError> {
|
|
1682
|
-
if !state.is_object() {
|
|
1683
|
-
*state = serde_json::json!({});
|
|
1684
|
-
}
|
|
1685
|
-
let Some(root) = state.as_object_mut() else {
|
|
1686
|
-
return Err(LifecycleError::StatePersist(
|
|
1687
|
-
"runtime state root is not an object".to_string(),
|
|
1688
|
-
));
|
|
1689
|
-
};
|
|
1690
|
-
root.insert(
|
|
1691
|
-
"session_name".to_string(),
|
|
1692
|
-
serde_json::json!(session_name.as_str()),
|
|
1693
|
-
);
|
|
1694
|
-
let agents = root
|
|
1695
|
-
.entry("agents".to_string())
|
|
1696
|
-
.or_insert_with(|| serde_json::json!({}));
|
|
1697
|
-
if !agents.is_object() {
|
|
1698
|
-
*agents = serde_json::json!({});
|
|
1699
|
-
}
|
|
1700
|
-
let Some(agent_map) = agents.as_object_mut() else {
|
|
1701
|
-
return Err(LifecycleError::StatePersist(
|
|
1702
|
-
"runtime state agents is not an object".to_string(),
|
|
1703
|
-
));
|
|
1704
|
-
};
|
|
1705
|
-
let entry = agent_map
|
|
1706
|
-
.entry(agent_id.as_str().to_string())
|
|
1707
|
-
.or_insert_with(|| serde_json::json!({}));
|
|
1708
|
-
if !entry.is_object() {
|
|
1709
|
-
*entry = serde_json::json!({});
|
|
1710
|
-
}
|
|
1711
|
-
let Some(obj) = entry.as_object_mut() else {
|
|
1712
|
-
return Err(LifecycleError::StatePersist(
|
|
1713
|
-
"agent state is not an object".to_string(),
|
|
1714
|
-
));
|
|
1715
|
-
};
|
|
1716
|
-
obj.insert("status".to_string(), serde_json::json!("running"));
|
|
1717
|
-
obj.insert("agent_id".to_string(), serde_json::json!(agent_id.as_str()));
|
|
1718
|
-
obj.insert("window".to_string(), serde_json::json!(window));
|
|
1719
|
-
if let Some(pane) = pane {
|
|
1720
|
-
obj.insert(
|
|
1721
|
-
"pane_id".to_string(),
|
|
1722
|
-
serde_json::json!(pane.pane_id.as_str()),
|
|
1723
|
-
);
|
|
1724
|
-
if let Some(pane_pid) = pane.pane_pid {
|
|
1725
|
-
obj.insert("pane_pid".to_string(), serde_json::json!(pane_pid));
|
|
1726
|
-
} else {
|
|
1727
|
-
obj.remove("pane_pid");
|
|
1728
|
-
}
|
|
1729
|
-
}
|
|
1730
|
-
Ok(())
|
|
1731
|
-
}
|
|
1732
|
-
|
|
1733
|
-
/// ---
|
|
1734
|
-
/// purpose: 写一条起席无操作事件
|
|
1735
|
-
/// returns: 成功返回空值
|
|
1736
|
-
/// errors: 事件写入失败时返回 StatePersist
|
|
1737
|
-
/// ---
|
|
1738
|
-
pub(super) fn write_start_agent_noop_event(
|
|
1739
|
-
workspace: &Path,
|
|
1740
|
-
agent_id: &AgentId,
|
|
1741
|
-
target: &str,
|
|
1742
|
-
coordinator_started: bool,
|
|
1743
|
-
) -> Result<(), LifecycleError> {
|
|
1744
|
-
crate::event_log::EventLog::new(workspace)
|
|
1745
|
-
.write(
|
|
1746
|
-
"start_agent.noop",
|
|
1747
|
-
serde_json::json!({
|
|
1748
|
-
"agent_id": agent_id.as_str(),
|
|
1749
|
-
"target": target,
|
|
1750
|
-
"coordinator": coordinator_started,
|
|
1751
|
-
}),
|
|
1752
|
-
)
|
|
1753
|
-
.map_err(|e| LifecycleError::StatePersist(e.to_string()))?;
|
|
1754
|
-
Ok(())
|
|
1755
|
-
}
|
|
1756
|
-
|
|
1757
|
-
/// ---
|
|
1758
|
-
/// purpose: 判断某 session 里是否存在该窗口
|
|
1759
|
-
/// returns: 明确列出该窗口才为 true;列窗口出错或 panic 时为 false
|
|
1760
|
-
/// ---
|
|
1761
|
-
pub(super) fn window_exists(
|
|
1762
|
-
transport: &dyn crate::transport::Transport,
|
|
1763
|
-
session_name: &SessionName,
|
|
1764
|
-
window: &str,
|
|
1765
|
-
) -> bool {
|
|
1766
|
-
match std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
|
|
1767
|
-
transport.list_windows(session_name)
|
|
1768
|
-
})) {
|
|
1769
|
-
Ok(Ok(windows)) => windows.iter().any(|w| w.as_str() == window),
|
|
1770
|
-
Ok(Err(_)) | Err(_) => false,
|
|
1771
|
-
}
|
|
1772
|
-
}
|
|
1773
|
-
|
|
1774
|
-
/// ---
|
|
1775
|
-
/// purpose: 丢弃该席位的会话捕获字段并标记为已停
|
|
1776
|
-
/// params:
|
|
1777
|
-
/// state: 就地改写;只删会话捕获相关字段,工作目录等状态字段保留
|
|
1778
|
-
/// returns: 成功返回空值
|
|
1779
|
-
/// errors: 席位不存在返回 RequirementUnmet,席位行不是对象返回 StatePersist
|
|
1780
|
-
/// ---
|
|
1781
|
-
pub(super) fn discard_agent_session_fields(
|
|
1782
|
-
state: &mut serde_json::Value,
|
|
1783
|
-
agent_id: &AgentId,
|
|
1784
|
-
) -> Result<(), LifecycleError> {
|
|
1785
|
-
let Some(agent) = state
|
|
1786
|
-
.get_mut("agents")
|
|
1787
|
-
.and_then(|v| v.as_object_mut())
|
|
1788
|
-
.and_then(|agents| agents.get_mut(agent_id.as_str()))
|
|
1789
|
-
else {
|
|
1790
|
-
return Err(unknown_worker(agent_id));
|
|
1791
|
-
};
|
|
1792
|
-
let Some(obj) = agent.as_object_mut() else {
|
|
1793
|
-
return Err(LifecycleError::StatePersist(
|
|
1794
|
-
"agent state is not an object".to_string(),
|
|
1795
|
-
));
|
|
1796
|
-
};
|
|
1797
|
-
// golden operations.py:119 pops EXACTLY `[*SESSION_CAPTURE_FIELDS, "_pending_session_id"]`.
|
|
1798
|
-
// spawn_cwd lives in SESSION_STATE_FIELDS (state.py:26-28), NOT SESSION_CAPTURE_FIELDS, so it is
|
|
1799
|
-
// PRESERVED through the discard. (Probe: SESSION_CAPTURE_FIELDS = session_id, rollout_path,
|
|
1800
|
-
// captured_at, captured_via, attribution_confidence.)
|
|
1801
|
-
//
|
|
1802
|
-
// Bug 2 (0.3.32): also clear `attribution_ambiguous`. The old logic left
|
|
1803
|
-
// this flag set after `reset-agent --discard-session` / fresh start, so a
|
|
1804
|
-
// newly-spawned agent inherited stale ambiguity from a previous lifecycle
|
|
1805
|
-
// even though the session tuple itself was discarded. Architect §4 fix #2:
|
|
1806
|
-
// "On fresh start/reset/start-agent for any provider, clear stale
|
|
1807
|
-
// `attribution_ambiguous` when the old session tuple is discarded or a new
|
|
1808
|
-
// `spawned_at` is written." This is a REMOVE (not a final_ambiguous write
|
|
1809
|
-
// and not a deadline_expired write) — the test source-grep
|
|
1810
|
-
// (attribution_ambiguous_is_final_only_after_convergence_deadline) allows
|
|
1811
|
-
// the literal here because the final_ambiguous / deadline_expired marker
|
|
1812
|
-
// is preserved in this comment.
|
|
1813
|
-
for key in [
|
|
1814
|
-
"session_id",
|
|
1815
|
-
"rollout_path",
|
|
1816
|
-
"captured_at",
|
|
1817
|
-
"captured_via",
|
|
1818
|
-
"attribution_confidence",
|
|
1819
|
-
"_pending_session_id",
|
|
1820
|
-
"attribution_ambiguous",
|
|
1821
|
-
] {
|
|
1822
|
-
obj.remove(key);
|
|
1823
|
-
}
|
|
1824
|
-
obj.insert("status".to_string(), serde_json::json!("stopped"));
|
|
1825
|
-
Ok(())
|
|
1826
|
-
}
|
|
1827
|
-
|
|
1828
|
-
/// ---
|
|
1829
|
-
/// purpose: 判断该席位是否在运行
|
|
1830
|
-
/// returns: 状态为 running 或 busy 直接为真;其余状态都退到按 session 与窗口存在性判定
|
|
1831
|
-
/// ---
|
|
1832
|
-
pub(super) fn agent_is_running(
|
|
1833
|
-
state: &serde_json::Value,
|
|
1834
|
-
agent_id: &AgentId,
|
|
1835
|
-
transport: &dyn crate::transport::Transport,
|
|
1836
|
-
) -> bool {
|
|
1837
|
-
let agent_state = state.get("agents").and_then(|v| v.get(agent_id.as_str()));
|
|
1838
|
-
let status = agent_state
|
|
1839
|
-
.and_then(|v| v.get("status"))
|
|
1840
|
-
.and_then(|v| v.as_str())
|
|
1841
|
-
.map(str::to_ascii_lowercase);
|
|
1842
|
-
// golden agents.py:247-252 (_is_running): True ONLY for {running,busy}; EVERY other status (including
|
|
1843
|
-
// stopped/paused/failed/removed) falls through to the session_name + tmux-window-exists check.
|
|
1844
|
-
if matches!(status.as_deref(), Some("running" | "busy")) {
|
|
1845
|
-
return true;
|
|
1846
|
-
}
|
|
1847
|
-
let Some(session_name) = state
|
|
1848
|
-
.get("session_name")
|
|
1849
|
-
.and_then(|v| v.as_str())
|
|
1850
|
-
.filter(|s| !s.is_empty())
|
|
1851
|
-
.map(SessionName::new)
|
|
1852
|
-
else {
|
|
1853
|
-
return false;
|
|
1854
|
-
};
|
|
1855
|
-
let window = agent_state
|
|
1856
|
-
.and_then(|v| v.get("window"))
|
|
1857
|
-
.and_then(|v| v.as_str())
|
|
1858
|
-
.filter(|s| !s.is_empty())
|
|
1859
|
-
.unwrap_or_else(|| agent_id.as_str());
|
|
1860
|
-
window_exists(transport, &session_name, window)
|
|
1861
|
-
}
|
|
1862
|
-
|
|
1863
|
-
/// ---
|
|
1864
|
-
/// purpose: 判断该席位是不是动态生成的
|
|
1865
|
-
/// returns: state 里记了动态角色文件,或 spec 里标了来源席位时为 true
|
|
1866
|
-
/// ---
|
|
1867
|
-
pub(super) fn is_dynamic_agent(
|
|
1868
|
-
state: &serde_json::Value,
|
|
1869
|
-
spec_agent: &YamlValue,
|
|
1870
|
-
agent_id: &AgentId,
|
|
1871
|
-
) -> bool {
|
|
1872
|
-
let dynamic_role = state
|
|
1873
|
-
.get("agents")
|
|
1874
|
-
.and_then(|v| v.get(agent_id.as_str()))
|
|
1875
|
-
.and_then(|v| v.get("dynamic_role_file"))
|
|
1876
|
-
.and_then(|v| v.as_str())
|
|
1877
|
-
.is_some_and(|s| !s.is_empty());
|
|
1878
|
-
dynamic_role
|
|
1879
|
-
|| spec_agent
|
|
1880
|
-
.get("forked_from")
|
|
1881
|
-
.and_then(YamlValue::as_str)
|
|
1882
|
-
.is_some_and(|s| !s.is_empty())
|
|
1883
|
-
}
|
|
1884
|
-
|
|
1885
|
-
/// ---
|
|
1886
|
-
/// purpose: 在 spawn 之前预判 tmux 的起法,供审计事件记录
|
|
1887
|
-
/// params:
|
|
1888
|
-
/// layout_placement: 有布局位置时按是否起新窗口区分
|
|
1889
|
-
/// into_existing_session: 目标 session 已存在
|
|
1890
|
-
/// returns: new-session、new-window 或 split-window
|
|
1891
|
-
/// ---
|
|
1892
|
-
/// 0.4.6 Stage 2: predict the tmux start mode BEFORE the spawn call, so
|
|
1893
|
-
/// the `provider.worker.spawn_argv` event can record what the spawn will
|
|
1894
|
-
/// actually do. Same logic as `tmux_start_mode_for_spawn` in agent.rs
|
|
1895
|
-
/// but driven by `(layout_placement, into_existing_session)` only — no
|
|
1896
|
-
/// dependency on SpawnedAgentWindow.
|
|
1897
|
-
pub(super) fn predict_tmux_start_mode(
|
|
1898
|
-
layout_placement: Option<&crate::lifecycle::launch::LayoutPlacement>,
|
|
1899
|
-
into_existing_session: bool,
|
|
1900
|
-
) -> &'static str {
|
|
1901
|
-
if let Some(placement) = layout_placement {
|
|
1902
|
-
if placement.starts_window {
|
|
1903
|
-
if into_existing_session {
|
|
1904
|
-
"new-window"
|
|
1905
|
-
} else {
|
|
1906
|
-
"new-session"
|
|
1907
|
-
}
|
|
1908
|
-
} else {
|
|
1909
|
-
"split-window"
|
|
1910
|
-
}
|
|
1911
|
-
} else if into_existing_session {
|
|
1912
|
-
"new-window"
|
|
1913
|
-
} else {
|
|
1914
|
-
"new-session"
|
|
1915
|
-
}
|
|
1916
|
-
}
|
|
1917
|
-
|
|
1918
|
-
/// ---
|
|
1919
|
-
/// purpose: 从盘上读该席位当前的 spawn 世代号
|
|
1920
|
-
/// returns: 读到的值;读不出 state 或字段缺失时为 0
|
|
1921
|
-
/// ---
|
|
1922
|
-
/// 0.4.6 Stage 2: read state.agents[agent_id].spawn_epoch from disk to
|
|
1923
|
-
/// stamp the spawn_argv event with the current cohort identifier. Returns
|
|
1924
|
-
/// 0 if the agent row / field is missing.
|
|
1925
|
-
pub(super) fn state_spawn_epoch_for_agent(workspace: &Path, agent_id: &AgentId) -> u64 {
|
|
1926
|
-
let Ok(state) = crate::state::persist::load_runtime_state(workspace) else {
|
|
1927
|
-
return 0;
|
|
1928
|
-
};
|
|
1929
|
-
state
|
|
1930
|
-
.get("agents")
|
|
1931
|
-
.and_then(|v| v.get(agent_id.as_str()))
|
|
1932
|
-
.and_then(|v| v.get("spawn_epoch"))
|
|
1933
|
-
.and_then(serde_json::Value::as_u64)
|
|
1934
|
-
.unwrap_or(0)
|
|
1935
|
-
}
|
|
1936
|
-
|
|
1937
|
-
/// ---
|
|
1938
|
-
/// purpose: 新起进程后清掉该席位的活动观测字段
|
|
1939
|
-
/// params:
|
|
1940
|
-
/// agent: 就地删除观测字段;生命周期与拓扑字段一概不动
|
|
1941
|
-
/// returns: 清完之后观测缺失表示未知,不等于空闲
|
|
1942
|
-
/// ---
|
|
1943
|
-
/// 0.5.32 (`.team/artifacts/restart-resumed-stale-activity-locate.md` §5):
|
|
1944
|
-
/// clear the per-agent turn/activity observation set on a successful new
|
|
1945
|
-
/// worker process cohort. Called from `mark_agent_started` /
|
|
1946
|
-
/// `mark_agent_respawned` / `mark_fake_harness_agent_respawned`; NOT from
|
|
1947
|
-
/// `mark_agent_running_noop` (no new process was created there).
|
|
1948
|
-
///
|
|
1949
|
-
/// Removes only observation fields — never lifecycle/topology fields. After
|
|
1950
|
-
/// clearing, absence is UNKNOWN, not synthesized idle (T7 unknown-never-idle
|
|
1951
|
-
/// discipline); the next post-spawn tick may repopulate from JSONL freshness
|
|
1952
|
-
/// gate + pane fallback.
|
|
1953
|
-
pub(super) fn clear_agent_runtime_activity_observation(
|
|
1954
|
-
agent: &mut serde_json::Map<String, serde_json::Value>,
|
|
1955
|
-
) {
|
|
1956
|
-
for field in [
|
|
1957
|
-
"activity",
|
|
1958
|
-
"worker_state",
|
|
1959
|
-
"last_output_at",
|
|
1960
|
-
"last_output_hash",
|
|
1961
|
-
"current_turn_message_id",
|
|
1962
|
-
"current_task_id", // ALLOWED-LEGACY-READ (Phase-DX E2): cleanup removal of legacy display-only key on new spawn cohort; helper does not read the value.
|
|
1963
|
-
"task_id",
|
|
1964
|
-
"coordinator_idle_capture_next_at",
|
|
1965
|
-
] {
|
|
1966
|
-
agent.remove(field);
|
|
1967
|
-
}
|
|
1968
|
-
}
|
|
1969
|
-
|
|
1970
|
-
#[cfg(test)]
|
|
1971
|
-
mod e36_transcript_backing_tests {
|
|
1972
|
-
use super::*;
|
|
1973
|
-
|
|
1974
|
-
struct ScratchDir(PathBuf);
|
|
1975
|
-
impl ScratchDir {
|
|
1976
|
-
fn new(tag: &str) -> Self {
|
|
1977
|
-
let pid = std::process::id();
|
|
1978
|
-
let base = std::env::temp_dir().join(format!("ta-e36-{tag}-{pid}"));
|
|
1979
|
-
std::fs::create_dir_all(&base).expect("scratch dir");
|
|
1980
|
-
ScratchDir(base)
|
|
1981
|
-
}
|
|
1982
|
-
fn path(&self) -> &Path {
|
|
1983
|
-
&self.0
|
|
1984
|
-
}
|
|
1985
|
-
}
|
|
1986
|
-
impl Drop for ScratchDir {
|
|
1987
|
-
fn drop(&mut self) {
|
|
1988
|
-
let _ = std::fs::remove_dir_all(&self.0);
|
|
1989
|
-
}
|
|
1990
|
-
}
|
|
1991
|
-
|
|
1992
|
-
#[test]
|
|
1993
|
-
fn agent_rollout_path_keeps_captured_exact_session_file() {
|
|
1994
|
-
// Live T1 public session.captured (events-before.jsonl / events-after-t1.jsonl):
|
|
1995
|
-
// rollout_path was already assigned before shutdown; resume must not
|
|
1996
|
-
// invent a different file or drop the captured path.
|
|
1997
|
-
let captured = "/Volumes/nvme/tmp/team-agent-live-0578-r2-3087abc5/.team/runtime/pi/claim-live-0578-r2/echo/sessions/2026-09-09T15-53-03-460Z_0dac7250-aa88-45be-aa3e-03a6833f9552.jsonl";
|
|
1998
|
-
let agent = serde_json::json!({
|
|
1999
|
-
"session_id": "0dac7250-aa88-45be-aa3e-03a6833f9552",
|
|
2000
|
-
"rollout_path": captured,
|
|
2001
|
-
});
|
|
2002
|
-
let path = agent_rollout_path(&agent).expect("captured exact path");
|
|
2003
|
-
assert_eq!(path.as_path().to_string_lossy(), captured);
|
|
2004
|
-
assert!(
|
|
2005
|
-
agent_rollout_path(&serde_json::json!({
|
|
2006
|
-
"session_id": "0dac7250-aa88-45be-aa3e-03a6833f9552",
|
|
2007
|
-
}))
|
|
2008
|
-
.is_none(),
|
|
2009
|
-
"session_id alone is not an exact session path"
|
|
2010
|
-
);
|
|
2011
|
-
}
|
|
2012
|
-
|
|
2013
|
-
#[test]
|
|
2014
|
-
fn convergence_event_uses_central_scrub_and_preserves_required_failure() {
|
|
2015
|
-
let scratch = ScratchDir::new("event-log-central");
|
|
2016
|
-
let marker = "synthetic-convergence-marker";
|
|
2017
|
-
write_session_convergence_progress_event(
|
|
2018
|
-
scratch.path(),
|
|
2019
|
-
serde_json::json!({
|
|
2020
|
-
"diagnostic": format!("mcp_servers.demo.env.OPENAI_API_KEY=\"{marker}\""),
|
|
2021
|
-
"required_missing_agent_ids": ["worker"],
|
|
2022
|
-
"pending_agent_ids": ["worker"],
|
|
2023
|
-
}),
|
|
2024
|
-
)
|
|
2025
|
-
.expect("central EventLog write");
|
|
2026
|
-
let bytes = std::fs::read_to_string(
|
|
2027
|
-
scratch
|
|
2028
|
-
.path()
|
|
2029
|
-
.join(".team")
|
|
2030
|
-
.join("logs")
|
|
2031
|
-
.join("events.jsonl"),
|
|
2032
|
-
)
|
|
2033
|
-
.expect("events");
|
|
2034
|
-
assert!(!bytes.contains(marker));
|
|
2035
|
-
assert!(bytes.contains("[REDACTED]"));
|
|
2036
|
-
|
|
2037
|
-
let blocked = ScratchDir::new("event-log-required");
|
|
2038
|
-
std::fs::create_dir_all(
|
|
2039
|
-
blocked
|
|
2040
|
-
.path()
|
|
2041
|
-
.join(".team")
|
|
2042
|
-
.join("logs")
|
|
2043
|
-
.join("events.jsonl"),
|
|
2044
|
-
)
|
|
2045
|
-
.expect("occupy event path with directory");
|
|
2046
|
-
assert!(write_session_convergence_progress_event(
|
|
2047
|
-
blocked.path(),
|
|
2048
|
-
serde_json::json!({"pending_agent_ids": ["worker"]}),
|
|
2049
|
-
)
|
|
2050
|
-
.is_err());
|
|
2051
|
-
}
|
|
2052
|
-
}
|