@team-agent/installer 0.5.128 → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +4 -4
- package/README.zh.md +1 -1
- package/package.json +4 -8
- package/skills/team-agent/SKILL.md +37 -20
- package/skills/team-agent/command-coverage.json +169 -183
- package/skills/team-agent/docs/reference/team-agent-operator.md +64 -30
- package/skills/team-agent/references/recovery-runbook.md +1 -1
- package/skills/team-agent/references/team-agent-operator.md +64 -30
- package/templates/team_state.md +1 -1
- package/Cargo.lock +0 -1063
- package/Cargo.toml +0 -36
- package/crates/team-agent/Cargo.toml +0 -69
- package/crates/team-agent/src/app_server_test_support.rs +0 -258
- package/crates/team-agent/src/cli/adapters.rs +0 -1288
- package/crates/team-agent/src/cli/arguments.rs +0 -405
- package/crates/team-agent/src/cli/attach_app_server_leader.rs +0 -98
- package/crates/team-agent/src/cli/diagnose.rs +0 -1721
- package/crates/team-agent/src/cli/emit.rs +0 -2930
- package/crates/team-agent/src/cli/grok_slot.rs +0 -384
- package/crates/team-agent/src/cli/helpers.rs +0 -91
- package/crates/team-agent/src/cli/intent/runtime.rs +0 -1216
- package/crates/team-agent/src/cli/intent/runtime_tests.rs +0 -912
- package/crates/team-agent/src/cli/intent/tests.rs +0 -689
- package/crates/team-agent/src/cli/intent.rs +0 -664
- package/crates/team-agent/src/cli/leader.rs +0 -300
- package/crates/team-agent/src/cli/leader_prompt.rs +0 -261
- package/crates/team-agent/src/cli/leaders.rs +0 -418
- package/crates/team-agent/src/cli/mod.rs +0 -6545
- package/crates/team-agent/src/cli/models.rs +0 -1206
- package/crates/team-agent/src/cli/named_address.rs +0 -1651
- package/crates/team-agent/src/cli/profile.rs +0 -342
- package/crates/team-agent/src/cli/route.rs +0 -212
- package/crates/team-agent/src/cli/schedule.rs +0 -175
- package/crates/team-agent/src/cli/send/coordinator.rs +0 -164
- package/crates/team-agent/src/cli/send/mailbox.rs +0 -145
- package/crates/team-agent/src/cli/send/persist.rs +0 -156
- package/crates/team-agent/src/cli/send/presentation.rs +0 -370
- package/crates/team-agent/src/cli/send/resolve.rs +0 -573
- package/crates/team-agent/src/cli/send.rs +0 -199
- package/crates/team-agent/src/cli/shutdown_socket.rs +0 -288
- package/crates/team-agent/src/cli/spec.rs +0 -165
- package/crates/team-agent/src/cli/status.rs +0 -280
- package/crates/team-agent/src/cli/status_port/agents.rs +0 -358
- package/crates/team-agent/src/cli/status_port/approvals.rs +0 -79
- package/crates/team-agent/src/cli/status_port/brief.rs +0 -1981
- package/crates/team-agent/src/cli/status_port/compact.rs +0 -240
- package/crates/team-agent/src/cli/status_port/format.rs +0 -146
- package/crates/team-agent/src/cli/status_port/inbox.rs +0 -62
- package/crates/team-agent/src/cli/status_port/runtime.rs +0 -197
- package/crates/team-agent/src/cli/status_port/snapshot.rs +0 -208
- package/crates/team-agent/src/cli/status_port/store.rs +0 -416
- package/crates/team-agent/src/cli/status_port/tests.rs +0 -90
- package/crates/team-agent/src/cli/status_port.rs +0 -92
- package/crates/team-agent/src/cli/tests/base.rs +0 -885
- package/crates/team-agent/src/cli/tests/divergence.rs +0 -518
- package/crates/team-agent/src/cli/tests/lane_c.rs +0 -453
- package/crates/team-agent/src/cli/tests/leader_watch.rs +0 -322
- package/crates/team-agent/src/cli/tests/main_preserved.rs +0 -430
- package/crates/team-agent/src/cli/tests/missing_subcommands.rs +0 -336
- package/crates/team-agent/src/cli/tests/mod.rs +0 -137
- package/crates/team-agent/src/cli/tests/named_address.rs +0 -761
- package/crates/team-agent/src/cli/tests/parser_safety.rs +0 -383
- package/crates/team-agent/src/cli/tests/run_delegation.rs +0 -484
- package/crates/team-agent/src/cli/tests/shutdown_kill_plan.rs +0 -2179
- package/crates/team-agent/src/cli/tests/status_brief.rs +0 -1514
- package/crates/team-agent/src/cli/tests/status_send.rs +0 -888
- package/crates/team-agent/src/cli/tests/verb_install_skill.rs +0 -93
- package/crates/team-agent/src/cli/tests/verb_profile.rs +0 -392
- package/crates/team-agent/src/cli/triage.rs +0 -234
- package/crates/team-agent/src/cli/types.rs +0 -591
- package/crates/team-agent/src/codex_app_server.rs +0 -546
- package/crates/team-agent/src/communication_mode/mod.rs +0 -56
- package/crates/team-agent/src/compiler/tests.rs +0 -827
- package/crates/team-agent/src/compiler.rs +0 -890
- package/crates/team-agent/src/conpty/backend.rs +0 -829
- package/crates/team-agent/src/conpty/mod.rs +0 -33
- package/crates/team-agent/src/contract_runtime/backend.rs +0 -1744
- package/crates/team-agent/src/contract_runtime/commands.rs +0 -671
- package/crates/team-agent/src/contract_runtime/config.rs +0 -364
- package/crates/team-agent/src/contract_runtime/config_tests.rs +0 -76
- package/crates/team-agent/src/contract_runtime/discovery.rs +0 -449
- package/crates/team-agent/src/contract_runtime/fork.rs +0 -361
- package/crates/team-agent/src/contract_runtime/forward.rs +0 -288
- package/crates/team-agent/src/contract_runtime/framework.rs +0 -367
- package/crates/team-agent/src/contract_runtime/mcp.rs +0 -394
- package/crates/team-agent/src/contract_runtime/mod.rs +0 -17
- package/crates/team-agent/src/contract_runtime/prompt.rs +0 -216
- package/crates/team-agent/src/contract_runtime/pump.rs +0 -271
- package/crates/team-agent/src/contract_runtime/registry.rs +0 -553
- package/crates/team-agent/src/contract_runtime/workspace.rs +0 -228
- package/crates/team-agent/src/coordinator/backoff.rs +0 -741
- package/crates/team-agent/src/coordinator/conpty_shim.rs +0 -805
- package/crates/team-agent/src/coordinator/health.rs +0 -1185
- package/crates/team-agent/src/coordinator/mod.rs +0 -119
- package/crates/team-agent/src/coordinator/orphan.rs +0 -110
- package/crates/team-agent/src/coordinator/runtime_detectors.rs +0 -798
- package/crates/team-agent/src/coordinator/runtime_observation.rs +0 -84
- package/crates/team-agent/src/coordinator/steps/abnormal.rs +0 -2309
- package/crates/team-agent/src/coordinator/steps/delivery.rs +0 -15
- package/crates/team-agent/src/coordinator/steps/health_sync.rs +0 -15
- package/crates/team-agent/src/coordinator/steps/mod.rs +0 -126
- package/crates/team-agent/src/coordinator/steps/persist.rs +0 -15
- package/crates/team-agent/src/coordinator/steps/runtime_prompts.rs +0 -15
- package/crates/team-agent/src/coordinator/steps/session_gate.rs +0 -16
- package/crates/team-agent/src/coordinator/tests/a0_lostupdate.rs +0 -158
- package/crates/team-agent/src/coordinator/tests/abnormal.rs +0 -312
- package/crates/team-agent/src/coordinator/tests/api_error_recovery.rs +0 -300
- package/crates/team-agent/src/coordinator/tests/basics.rs +0 -211
- package/crates/team-agent/src/coordinator/tests/daemon.rs +0 -514
- package/crates/team-agent/src/coordinator/tests/energy.rs +0 -549
- package/crates/team-agent/src/coordinator/tests/health_sync.rs +0 -503
- package/crates/team-agent/src/coordinator/tests/main_preserved.rs +0 -186
- package/crates/team-agent/src/coordinator/tests/mod.rs +0 -295
- package/crates/team-agent/src/coordinator/tests/s3_bounded.rs +0 -207
- package/crates/team-agent/src/coordinator/tests/spine.rs +0 -308
- package/crates/team-agent/src/coordinator/tests/takeover.rs +0 -286
- package/crates/team-agent/src/coordinator/tests/tick_core.rs +0 -237
- package/crates/team-agent/src/coordinator/tick.rs +0 -3024
- package/crates/team-agent/src/coordinator/types.rs +0 -540
- package/crates/team-agent/src/db/agent_health_capture.rs +0 -126
- package/crates/team-agent/src/db/message_store.rs +0 -1527
- package/crates/team-agent/src/db/migration.rs +0 -984
- package/crates/team-agent/src/db/mod.rs +0 -26
- package/crates/team-agent/src/db/schema.rs +0 -663
- package/crates/team-agent/src/diagnose/comms.rs +0 -242
- package/crates/team-agent/src/diagnose/mod.rs +0 -44
- package/crates/team-agent/src/diagnose/orphans.rs +0 -889
- package/crates/team-agent/src/direct_delivery.rs +0 -368
- package/crates/team-agent/src/event_log.rs +0 -502
- package/crates/team-agent/src/fake_worker.rs +0 -303
- package/crates/team-agent/src/inbox_store.rs +0 -169
- package/crates/team-agent/src/kill_audit.rs +0 -106
- package/crates/team-agent/src/layout/manager.rs +0 -248
- package/crates/team-agent/src/layout/mod.rs +0 -36
- package/crates/team-agent/src/layout/overlay.rs +0 -134
- package/crates/team-agent/src/layout/placement.rs +0 -52
- package/crates/team-agent/src/layout/recovery.rs +0 -106
- package/crates/team-agent/src/layout/runtime_sessions.rs +0 -317
- package/crates/team-agent/src/layout/sessions.rs +0 -294
- package/crates/team-agent/src/layout/tmux_endpoint.rs +0 -165
- package/crates/team-agent/src/layout/worker_env.rs +0 -721
- package/crates/team-agent/src/layout/worker_window_helpers.rs +0 -37
- package/crates/team-agent/src/leader/helpers.rs +0 -178
- package/crates/team-agent/src/leader/incident.rs +0 -6
- package/crates/team-agent/src/leader/lease.rs +0 -3356
- package/crates/team-agent/src/leader/mod.rs +0 -104
- package/crates/team-agent/src/leader/owner_bind.rs +0 -400
- package/crates/team-agent/src/leader/prompt.rs +0 -905
- package/crates/team-agent/src/leader/provider_attribution.rs +0 -247
- package/crates/team-agent/src/leader/rediscover/tests.rs +0 -689
- package/crates/team-agent/src/leader/rediscover.rs +0 -1192
- package/crates/team-agent/src/leader/registry.rs +0 -888
- package/crates/team-agent/src/leader/start.rs +0 -4501
- package/crates/team-agent/src/leader/takeover.rs +0 -65
- package/crates/team-agent/src/leader/tests/basics.rs +0 -337
- package/crates/team-agent/src/leader/tests/byte_findings.rs +0 -355
- package/crates/team-agent/src/leader/tests/claim_leader_mailbox_flush_contract.rs +0 -544
- package/crates/team-agent/src/leader/tests/historical_inventory_upgrade_gate_red.rs +0 -624
- package/crates/team-agent/src/leader/tests/identity.rs +0 -465
- package/crates/team-agent/src/leader/tests/identity_session_names.rs +0 -42
- package/crates/team-agent/src/leader/tests/idle.rs +0 -113
- package/crates/team-agent/src/leader/tests/lease_api.rs +0 -520
- package/crates/team-agent/src/leader/tests/lease_claim.rs +0 -629
- package/crates/team-agent/src/leader/tests/mod.rs +0 -106
- package/crates/team-agent/src/leader/tests/prompt_contract.rs +0 -268
- package/crates/team-agent/src/leader/tests/rediscover.rs +0 -403
- package/crates/team-agent/src/leader/tests/rehomed_env.rs +0 -83
- package/crates/team-agent/src/leader/tests/team_in_team_state_scope_red.rs +0 -1273
- package/crates/team-agent/src/leader/tests/terminal_and_replay_vs_rerender_invariants_red.rs +0 -417
- package/crates/team-agent/src/leader/tests/wake_start_owner.rs +0 -168
- package/crates/team-agent/src/leader/types.rs +0 -534
- package/crates/team-agent/src/lib.rs +0 -147
- package/crates/team-agent/src/lifecycle/helpers.rs +0 -212
- package/crates/team-agent/src/lifecycle/launch/add_agent.rs +0 -881
- package/crates/team-agent/src/lifecycle/launch/add_agent_state.rs +0 -598
- package/crates/team-agent/src/lifecycle/launch/agent_state.rs +0 -332
- package/crates/team-agent/src/lifecycle/launch/clone_agent.rs +0 -204
- package/crates/team-agent/src/lifecycle/launch/fork_agent.rs +0 -107
- package/crates/team-agent/src/lifecycle/launch/fork_entry.rs +0 -144
- package/crates/team-agent/src/lifecycle/launch/identity.rs +0 -674
- package/crates/team-agent/src/lifecycle/launch/layout.rs +0 -350
- package/crates/team-agent/src/lifecycle/launch/leader_context.rs +0 -589
- package/crates/team-agent/src/lifecycle/launch/mcp_config.rs +0 -160
- package/crates/team-agent/src/lifecycle/launch/ownership.rs +0 -105
- package/crates/team-agent/src/lifecycle/launch/pi_mcp.rs +0 -899
- package/crates/team-agent/src/lifecycle/launch/pi_mcp_extension.js +0 -225
- package/crates/team-agent/src/lifecycle/launch/plan.rs +0 -269
- package/crates/team-agent/src/lifecycle/launch/quick_start.rs +0 -5200
- package/crates/team-agent/src/lifecycle/launch/quick_start_transport.rs +0 -368
- package/crates/team-agent/src/lifecycle/launch/readiness.rs +0 -519
- package/crates/team-agent/src/lifecycle/launch/role_source.rs +0 -287
- package/crates/team-agent/src/lifecycle/launch/spawn.rs +0 -473
- package/crates/team-agent/src/lifecycle/launch/spec_state.rs +0 -801
- package/crates/team-agent/src/lifecycle/launch/state_projection.rs +0 -624
- package/crates/team-agent/src/lifecycle/launch/worker_env.rs +0 -558
- package/crates/team-agent/src/lifecycle/launch.rs +0 -557
- package/crates/team-agent/src/lifecycle/lock.rs +0 -350
- package/crates/team-agent/src/lifecycle/mod.rs +0 -79
- package/crates/team-agent/src/lifecycle/pane_input_lock.rs +0 -239
- package/crates/team-agent/src/lifecycle/profile_launch.rs +0 -1067
- package/crates/team-agent/src/lifecycle/profile_smoke.rs +0 -557
- package/crates/team-agent/src/lifecycle/restart/agent.rs +0 -2199
- package/crates/team-agent/src/lifecycle/restart/avatar.rs +0 -1798
- package/crates/team-agent/src/lifecycle/restart/claude_fork.rs +0 -163
- package/crates/team-agent/src/lifecycle/restart/common.rs +0 -2362
- package/crates/team-agent/src/lifecycle/restart/orchestrator.rs +0 -65
- package/crates/team-agent/src/lifecycle/restart/preflight.rs +0 -113
- package/crates/team-agent/src/lifecycle/restart/rebuild.rs +0 -5135
- package/crates/team-agent/src/lifecycle/restart/remove.rs +0 -1504
- package/crates/team-agent/src/lifecycle/restart/selection.rs +0 -559
- package/crates/team-agent/src/lifecycle/restart/team_state.rs +0 -329
- package/crates/team-agent/src/lifecycle/restart.rs +0 -227
- package/crates/team-agent/src/lifecycle/role_config.rs +0 -1230
- package/crates/team-agent/src/lifecycle/tests/acceptance_b_batch_red.rs +0 -1021
- package/crates/team-agent/src/lifecycle/tests/agent_ops.rs +0 -1205
- package/crates/team-agent/src/lifecycle/tests/b0_legacy_snapshot_nonauthority_contract.rs +0 -265
- package/crates/team-agent/src/lifecycle/tests/b0_reader_hideone_audit_contract.rs +0 -490
- package/crates/team-agent/src/lifecycle/tests/behavioral_diff_264_red.rs +0 -656
- package/crates/team-agent/src/lifecycle/tests/claude_compatible_config_red.rs +0 -325
- package/crates/team-agent/src/lifecycle/tests/claude_profile_launch_red.rs +0 -447
- package/crates/team-agent/src/lifecycle/tests/cli_argv_routing_red.rs +0 -1638
- package/crates/team-agent/src/lifecycle/tests/clone_agent_preserves_source_tools.rs +0 -427
- package/crates/team-agent/src/lifecycle/tests/clone_fork_copilot_perms_red.rs +0 -770
- package/crates/team-agent/src/lifecycle/tests/communication_mode_runtime_contract_red.rs +0 -419
- package/crates/team-agent/src/lifecycle/tests/copilot_provider_red.rs +0 -2035
- package/crates/team-agent/src/lifecycle/tests/core.rs +0 -1327
- package/crates/team-agent/src/lifecycle/tests/f032_startup_prompt_best_effort_red.rs +0 -315
- package/crates/team-agent/src/lifecycle/tests/fixtures/pi_list_models_g0.stdout.txt +0 -222
- package/crates/team-agent/src/lifecycle/tests/fixtures/pi_list_models_spaced_ids.stdout.txt +0 -9
- package/crates/team-agent/src/lifecycle/tests/fixtures/pi_native_mcp_host.mjs +0 -72
- package/crates/team-agent/src/lifecycle/tests/gate_fixtures.rs +0 -562
- package/crates/team-agent/src/lifecycle/tests/grok_require_explicit_model_red.rs +0 -110
- package/crates/team-agent/src/lifecycle/tests/harvest2_a_batch_red.rs +0 -512
- package/crates/team-agent/src/lifecycle/tests/host_cotenant_death_p0_contract.rs +0 -921
- package/crates/team-agent/src/lifecycle/tests/issue238_codex_effort_contract.rs +0 -68
- package/crates/team-agent/src/lifecycle/tests/lane_ops.rs +0 -1851
- package/crates/team-agent/src/lifecycle/tests/launch_spawn.rs +0 -3983
- package/crates/team-agent/src/lifecycle/tests/lifecycle_lock.rs +0 -339
- package/crates/team-agent/src/lifecycle/tests/lifecycle_rollback_red.rs +0 -596
- package/crates/team-agent/src/lifecycle/tests/main_preserved.rs +0 -513
- package/crates/team-agent/src/lifecycle/tests/mcp_tool_name_format_red.rs +0 -69
- package/crates/team-agent/src/lifecycle/tests/phase_b_contracts.rs +0 -824
- package/crates/team-agent/src/lifecycle/tests/phase_golden.rs +0 -723
- package/crates/team-agent/src/lifecycle/tests/pi_catalog_red.rs +0 -164
- package/crates/team-agent/src/lifecycle/tests/pi_command_red.rs +0 -200
- package/crates/team-agent/src/lifecycle/tests/pi_compiler_red.rs +0 -210
- package/crates/team-agent/src/lifecycle/tests/pi_dual_entry_red.rs +0 -527
- package/crates/team-agent/src/lifecycle/tests/pi_executable_mcp_red.rs +0 -1062
- package/crates/team-agent/src/lifecycle/tests/pi_fork_session_inheritance_red.rs +0 -1419
- package/crates/team-agent/src/lifecycle/tests/pi_lifecycle_doctor_red.rs +0 -98
- package/crates/team-agent/src/lifecycle/tests/pi_native_mcp_capability_red.rs +0 -550
- package/crates/team-agent/src/lifecycle/tests/pi_session_delivery_red.rs +0 -226
- package/crates/team-agent/src/lifecycle/tests/quick_start_worker_readiness_red.rs +0 -332
- package/crates/team-agent/src/lifecycle/tests/realmachine_clusters_1_6_red.rs +0 -786
- package/crates/team-agent/src/lifecycle/tests/realmachine_residual_g1_g4_red.rs +0 -648
- package/crates/team-agent/src/lifecycle/tests/restart.rs +0 -185
- package/crates/team-agent/src/lifecycle/tests/restart_build_before_destroy_0540_contract.rs +0 -1053
- package/crates/team-agent/src/lifecycle/tests/restart_liveness_red.rs +0 -679
- package/crates/team-agent/src/lifecycle/tests/restart_rebind_hotfix_252_red.rs +0 -1126
- package/crates/team-agent/src/lifecycle/tests/restart_session_capture_red.rs +0 -153
- package/crates/team-agent/src/lifecycle/tests/resume_recover_red.rs +0 -427
- package/crates/team-agent/src/lifecycle/tests/stale_team_saveconflict_contract.rs +0 -431
- package/crates/team-agent/src/lifecycle/tests/startup_latency_contract.rs +0 -1066
- package/crates/team-agent/src/lifecycle/tests/status_credential_redaction_contract.rs +0 -876
- package/crates/team-agent/src/lifecycle/tests/subprep_codex_trust_roundtrip_red.rs +0 -1026
- package/crates/team-agent/src/lifecycle/tests/swallow_batch4_semantics_red.rs +0 -342
- package/crates/team-agent/src/lifecycle/tests/team_in_team_identity_scope_red.rs +0 -543
- package/crates/team-agent/src/lifecycle/tests/team_in_team_sibling_quick_start_red.rs +0 -1103
- package/crates/team-agent/src/lifecycle/tests/team_in_team_state_scope_red.rs +0 -1276
- package/crates/team-agent/src/lifecycle/tests/team_key_retirement_roster_red.rs +0 -145
- package/crates/team-agent/src/lifecycle/tests/team_key_retirement_txn_red.rs +0 -603
- package/crates/team-agent/src/lifecycle/tests/test_isolation_escape_contract.rs +0 -936
- package/crates/team-agent/src/lifecycle/tests/upgrade_compat_0211_red.rs +0 -970
- package/crates/team-agent/src/lifecycle/tests/verify_rs031_window_consistency_red.rs +0 -995
- package/crates/team-agent/src/lifecycle/tests/worker_spawn_env_red.rs +0 -836
- package/crates/team-agent/src/lifecycle/tests.rs +0 -105
- package/crates/team-agent/src/lifecycle/types.rs +0 -877
- package/crates/team-agent/src/lifecycle/worker_command_context.rs +0 -363
- package/crates/team-agent/src/main.rs +0 -52
- package/crates/team-agent/src/mcp_server/helpers.rs +0 -510
- package/crates/team-agent/src/mcp_server/lifecycle_tools/mod.rs +0 -6
- package/crates/team-agent/src/mcp_server/lifecycle_tools/state_status.rs +0 -43
- package/crates/team-agent/src/mcp_server/mod.rs +0 -119
- package/crates/team-agent/src/mcp_server/normalize.rs +0 -485
- package/crates/team-agent/src/mcp_server/readiness.rs +0 -527
- package/crates/team-agent/src/mcp_server/readiness_tests.rs +0 -348
- package/crates/team-agent/src/mcp_server/tests/golden.rs +0 -285
- package/crates/team-agent/src/mcp_server/tests/normalize.rs +0 -247
- package/crates/team-agent/src/mcp_server/tests/scoped.rs +0 -158
- package/crates/team-agent/src/mcp_server/tests/send.rs +0 -353
- package/crates/team-agent/src/mcp_server/tests/tools.rs +0 -341
- package/crates/team-agent/src/mcp_server/tests/wire.rs +0 -254
- package/crates/team-agent/src/mcp_server/tests.rs +0 -43
- package/crates/team-agent/src/mcp_server/tools.rs +0 -889
- package/crates/team-agent/src/mcp_server/types.rs +0 -396
- package/crates/team-agent/src/mcp_server/wire.rs +0 -867
- package/crates/team-agent/src/messaging/activity.rs +0 -461
- package/crates/team-agent/src/messaging/address.rs +0 -87
- package/crates/team-agent/src/messaging/delivery.rs +0 -3727
- package/crates/team-agent/src/messaging/delivery_consumption_tests.rs +0 -234
- package/crates/team-agent/src/messaging/enter_rescue.rs +0 -362
- package/crates/team-agent/src/messaging/enter_rescue_tests.rs +0 -633
- package/crates/team-agent/src/messaging/helpers.rs +0 -336
- package/crates/team-agent/src/messaging/leader_channel.rs +0 -287
- package/crates/team-agent/src/messaging/leader_receiver.rs +0 -786
- package/crates/team-agent/src/messaging/mod.rs +0 -167
- package/crates/team-agent/src/messaging/persist.rs +0 -377
- package/crates/team-agent/src/messaging/presentation.rs +0 -417
- package/crates/team-agent/src/messaging/reminders.rs +0 -316
- package/crates/team-agent/src/messaging/results/collection_tests.rs +0 -719
- package/crates/team-agent/src/messaging/results.rs +0 -1415
- package/crates/team-agent/src/messaging/scheduler.rs +0 -223
- package/crates/team-agent/src/messaging/selftest.rs +0 -315
- package/crates/team-agent/src/messaging/send.rs +0 -981
- package/crates/team-agent/src/messaging/tests/basic.rs +0 -346
- package/crates/team-agent/src/messaging/tests/dup_inject.rs +0 -657
- package/crates/team-agent/src/messaging/tests/e23.rs +0 -49
- package/crates/team-agent/src/messaging/tests/leader_channel.rs +0 -169
- package/crates/team-agent/src/messaging/tests/leader_inject_acceptance.rs +0 -817
- package/crates/team-agent/src/messaging/tests/main_preserved.rs +0 -85
- package/crates/team-agent/src/messaging/tests/mod.rs +0 -306
- package/crates/team-agent/src/messaging/tests/runtime.rs +0 -3549
- package/crates/team-agent/src/messaging/tests/spine.rs +0 -853
- package/crates/team-agent/src/messaging/tests/wave2.rs +0 -381
- package/crates/team-agent/src/messaging/trust.rs +0 -215
- package/crates/team-agent/src/messaging/types.rs +0 -472
- package/crates/team-agent/src/messaging/wait.rs +0 -366
- package/crates/team-agent/src/messaging/watchers.rs +0 -916
- package/crates/team-agent/src/model/enums.rs +0 -356
- package/crates/team-agent/src/model/errors.rs +0 -18
- package/crates/team-agent/src/model/ids.rs +0 -165
- package/crates/team-agent/src/model/mod.rs +0 -29
- package/crates/team-agent/src/model/name_similarity.rs +0 -252
- package/crates/team-agent/src/model/pane_authority_refusal.rs +0 -359
- package/crates/team-agent/src/model/paths.rs +0 -257
- package/crates/team-agent/src/model/routing.rs +0 -387
- package/crates/team-agent/src/model/spec.rs +0 -1184
- package/crates/team-agent/src/model/task_graph.rs +0 -321
- package/crates/team-agent/src/model/testdata/fuzz.golden.yaml +0 -43
- package/crates/team-agent/src/model/testdata/fuzz.yaml +0 -43
- package/crates/team-agent/src/model/testdata/spec_invalid_a.yaml +0 -209
- package/crates/team-agent/src/model/testdata/team.spec.golden.yaml +0 -208
- package/crates/team-agent/src/model/testdata/team.spec.yaml +0 -208
- package/crates/team-agent/src/model/yaml/tests.rs +0 -188
- package/crates/team-agent/src/model/yaml.rs +0 -802
- package/crates/team-agent/src/os_probe.rs +0 -280
- package/crates/team-agent/src/packaging/install.rs +0 -219
- package/crates/team-agent/src/packaging/migrate.rs +0 -38
- package/crates/team-agent/src/packaging/mod.rs +0 -91
- package/crates/team-agent/src/packaging/repair.rs +0 -33
- package/crates/team-agent/src/packaging/tests.rs +0 -822
- package/crates/team-agent/src/packaging/types.rs +0 -417
- package/crates/team-agent/src/platform/argv.rs +0 -331
- package/crates/team-agent/src/platform/errors.rs +0 -104
- package/crates/team-agent/src/platform/file_lock.rs +0 -279
- package/crates/team-agent/src/platform/mod.rs +0 -5
- package/crates/team-agent/src/platform/process.rs +0 -545
- package/crates/team-agent/src/provider/adapter.rs +0 -1150
- package/crates/team-agent/src/provider/adapters/claude.rs +0 -78
- package/crates/team-agent/src/provider/adapters/claude_fork.rs +0 -131
- package/crates/team-agent/src/provider/adapters/copilot.rs +0 -75
- package/crates/team-agent/src/provider/adapters/copilot_fork.rs +0 -307
- package/crates/team-agent/src/provider/adapters/fake.rs +0 -18
- package/crates/team-agent/src/provider/adapters/mod.rs +0 -30
- package/crates/team-agent/src/provider/adapters/pi.rs +0 -106
- package/crates/team-agent/src/provider/approvals/mod.rs +0 -3
- package/crates/team-agent/src/provider/approvals/parsing.rs +0 -586
- package/crates/team-agent/src/provider/approvals/runtime_prompts.rs +0 -205
- package/crates/team-agent/src/provider/argv_route.rs +0 -405
- package/crates/team-agent/src/provider/bypass_flags.rs +0 -116
- package/crates/team-agent/src/provider/classify.rs +0 -708
- package/crates/team-agent/src/provider/command_helpers.rs +0 -45
- package/crates/team-agent/src/provider/faults.rs +0 -165
- package/crates/team-agent/src/provider/helpers.rs +0 -52
- package/crates/team-agent/src/provider/mod.rs +0 -78
- package/crates/team-agent/src/provider/model_catalog.rs +0 -910
- package/crates/team-agent/src/provider/session/capture.rs +0 -2731
- package/crates/team-agent/src/provider/session/context_fork/claude.rs +0 -123
- package/crates/team-agent/src/provider/session/context_fork/outcome.rs +0 -172
- package/crates/team-agent/src/provider/session/context_fork.rs +0 -396
- package/crates/team-agent/src/provider/session/mod.rs +0 -36
- package/crates/team-agent/src/provider/session/resume.rs +0 -269
- package/crates/team-agent/src/provider/session_scan/claude.rs +0 -523
- package/crates/team-agent/src/provider/session_scan/common/tests.rs +0 -316
- package/crates/team-agent/src/provider/session_scan/common.rs +0 -654
- package/crates/team-agent/src/provider/session_scan/copilot.rs +0 -280
- package/crates/team-agent/src/provider/session_scan/pi.rs +0 -185
- package/crates/team-agent/src/provider/session_scan.rs +0 -71
- package/crates/team-agent/src/provider/startup_prompt.rs +0 -757
- package/crates/team-agent/src/provider/submit_now.rs +0 -71
- package/crates/team-agent/src/provider/testdata/copilot-ready-marker.txt +0 -5
- package/crates/team-agent/src/provider/testdata/copilot-trust-prompt.txt +0 -19
- package/crates/team-agent/src/provider/testdata/grok-models-authenticated.stdout +0 -9
- package/crates/team-agent/src/provider/testdata/grok-models-unauthenticated.stdout +0 -7
- package/crates/team-agent/src/provider/tests/adapter.rs +0 -441
- package/crates/team-agent/src/provider/tests/classify.rs +0 -148
- package/crates/team-agent/src/provider/tests/command_helpers.rs +0 -35
- package/crates/team-agent/src/provider/tests/copilot_fork.rs +0 -191
- package/crates/team-agent/src/provider/tests/faults.rs +0 -200
- package/crates/team-agent/src/provider/tests/idle.rs +0 -177
- package/crates/team-agent/src/provider/tests/wire.rs +0 -213
- package/crates/team-agent/src/provider/tests.rs +0 -32
- package/crates/team-agent/src/provider/types.rs +0 -529
- package/crates/team-agent/src/provider/wire.rs +0 -394
- package/crates/team-agent/src/redaction.rs +0 -259
- package/crates/team-agent/src/state/abnormal_watch/tests/mod.rs +0 -248
- package/crates/team-agent/src/state/abnormal_watch.rs +0 -144
- package/crates/team-agent/src/state/identity.rs +0 -914
- package/crates/team-agent/src/state/identity_keys.rs +0 -132
- package/crates/team-agent/src/state/mod.rs +0 -74
- package/crates/team-agent/src/state/owner_gate.rs +0 -482
- package/crates/team-agent/src/state/ownership.rs +0 -563
- package/crates/team-agent/src/state/paths.rs +0 -395
- package/crates/team-agent/src/state/persist.rs +0 -3811
- package/crates/team-agent/src/state/projection.rs +0 -1508
- package/crates/team-agent/src/state/repository/intent.rs +0 -14
- package/crates/team-agent/src/state/repository/tests.rs +0 -184
- package/crates/team-agent/src/state/repository.rs +0 -866
- package/crates/team-agent/src/state/selector.rs +0 -160
- package/crates/team-agent/src/state/testdata/state-rich.canonical.json +0 -133
- package/crates/team-agent/src/test_support/models_timeout_fixture.rs +0 -144
- package/crates/team-agent/src/tmux_backend/enter_rescue.rs +0 -325
- package/crates/team-agent/src/tmux_backend/enter_rescue_tests.rs +0 -432
- package/crates/team-agent/src/tmux_backend/native_input.rs +0 -513
- package/crates/team-agent/src/tmux_backend/native_input_tests.rs +0 -742
- package/crates/team-agent/src/tmux_backend/tests.rs +0 -4103
- package/crates/team-agent/src/tmux_backend.rs +0 -4492
- package/crates/team-agent/src/topology.rs +0 -537
- package/crates/team-agent/src/transport/enter_rescue.rs +0 -166
- package/crates/team-agent/src/transport/native_input.rs +0 -491
- package/crates/team-agent/src/transport/test_support.rs +0 -743
- package/crates/team-agent/src/transport/tests/behavior.rs +0 -326
- package/crates/team-agent/src/transport/tests/mod.rs +0 -194
- package/crates/team-agent/src/transport/tests/wire.rs +0 -574
- package/crates/team-agent/src/transport.rs +0 -1655
- package/crates/team-agent/src/transport_factory.rs +0 -819
|
@@ -1,1744 +0,0 @@
|
|
|
1
|
-
//! Framework scope and owned native startup. No legacy Provider enum or adapter
|
|
2
|
-
//! is used here. Native admission is resolved before creating the new store.
|
|
3
|
-
use std::collections::BTreeMap;
|
|
4
|
-
use std::io::{Read, Write};
|
|
5
|
-
use std::path::{Path, PathBuf};
|
|
6
|
-
use std::time::Duration;
|
|
7
|
-
|
|
8
|
-
use serde::{Deserialize, Serialize};
|
|
9
|
-
use team_agent_contract::contract::{
|
|
10
|
-
descriptor::AuthMode,
|
|
11
|
-
hooks::*,
|
|
12
|
-
plan::*,
|
|
13
|
-
session::{CwdIdentity, ResumeBinding},
|
|
14
|
-
types::*,
|
|
15
|
-
};
|
|
16
|
-
use team_agent_contract::host::{
|
|
17
|
-
clock::RealClock,
|
|
18
|
-
command::*,
|
|
19
|
-
digest, digest_hex,
|
|
20
|
-
materialize::ScopedMaterializer,
|
|
21
|
-
process::{fingerprint_file, resolve_cwd},
|
|
22
|
-
tmux::{HostLimits, SharedTmux, TmuxHost},
|
|
23
|
-
transport::PhysicalTransport,
|
|
24
|
-
};
|
|
25
|
-
use team_agent_contract::orchestration::{lifecycle::*, physical::*, store::*, Error};
|
|
26
|
-
use thiserror::Error as ThisError;
|
|
27
|
-
|
|
28
|
-
use crate::transport::Transport;
|
|
29
|
-
|
|
30
|
-
use super::{
|
|
31
|
-
config::{MemberRoute, RoleConfig, RuntimeFamily},
|
|
32
|
-
discovery::{self, Discovery, DiscoveryError},
|
|
33
|
-
forward::FrameworkContext,
|
|
34
|
-
registry::{self, NativeAdapter},
|
|
35
|
-
};
|
|
36
|
-
|
|
37
|
-
#[derive(Debug, ThisError)]
|
|
38
|
-
pub enum BackendError {
|
|
39
|
-
#[error("native configuration: {0}")]
|
|
40
|
-
Configuration(#[from] super::config::ConfigError),
|
|
41
|
-
#[error("native discovery: {0}")]
|
|
42
|
-
Discovery(#[from] DiscoveryError),
|
|
43
|
-
#[error("contract runtime: {0}")]
|
|
44
|
-
Runtime(#[from] Error),
|
|
45
|
-
#[error("native contract: {0:?}")]
|
|
46
|
-
Contract(#[from] ContractError),
|
|
47
|
-
#[error("native host: {0}")]
|
|
48
|
-
Host(#[from] team_agent_contract::host::HostError),
|
|
49
|
-
#[error("native runtime I/O: {0}")]
|
|
50
|
-
Io(#[from] std::io::Error),
|
|
51
|
-
#[error("native runtime metadata is invalid")]
|
|
52
|
-
Metadata,
|
|
53
|
-
#[error("native shutdown operation={operation:?} phase={phase:?} outcome={outcome:?} failure={failure:?}")]
|
|
54
|
-
Shutdown {
|
|
55
|
-
operation: OperationId,
|
|
56
|
-
phase: Phase,
|
|
57
|
-
outcome: Outcome,
|
|
58
|
-
failure: Option<String>,
|
|
59
|
-
},
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
#[derive(Clone, Serialize, Deserialize)]
|
|
63
|
-
#[serde(deny_unknown_fields)]
|
|
64
|
-
pub struct Binding {
|
|
65
|
-
pub version: u32,
|
|
66
|
-
pub workspace: CwdIdentity,
|
|
67
|
-
pub team: String,
|
|
68
|
-
pub scope: ScopeId,
|
|
69
|
-
pub endpoint: String,
|
|
70
|
-
pub candidate: PathBuf,
|
|
71
|
-
pub candidate_sha256: Digest,
|
|
72
|
-
pub tmux: PathBuf,
|
|
73
|
-
pub transport_root: PathBuf,
|
|
74
|
-
}
|
|
75
|
-
impl Binding {
|
|
76
|
-
pub fn framework(&self) -> FrameworkContext {
|
|
77
|
-
FrameworkContext {
|
|
78
|
-
workspace: self.workspace.clone(),
|
|
79
|
-
team: self.team.clone(),
|
|
80
|
-
native_scope: self.scope.clone(),
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
pub struct Backend {
|
|
86
|
-
pub store: ContractStore,
|
|
87
|
-
pub binding: Binding,
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
pub fn scope_for(workspace: &CwdIdentity, team: &str) -> Result<ScopeId, BackendError> {
|
|
91
|
-
if team.trim().is_empty() || team.chars().any(char::is_control) {
|
|
92
|
-
return Err(BackendError::Metadata);
|
|
93
|
-
}
|
|
94
|
-
let encoded = serde_json::to_vec(&(workspace, team)).map_err(|_| BackendError::Metadata)?;
|
|
95
|
-
Ok(ScopeId::new(format!(
|
|
96
|
-
"contract-{}",
|
|
97
|
-
&digest_hex(digest(&encoded))[..32]
|
|
98
|
-
))?)
|
|
99
|
-
}
|
|
100
|
-
pub fn root_for(workspace: &Path, scope: &ScopeId) -> PathBuf {
|
|
101
|
-
crate::model::paths::runtime_dir(workspace)
|
|
102
|
-
.join("contract")
|
|
103
|
-
.join(scope.as_str())
|
|
104
|
-
}
|
|
105
|
-
fn endpoint(scope: &ScopeId) -> String {
|
|
106
|
-
format!("framework:{}", scope.as_str())
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
impl Backend {
|
|
110
|
-
pub fn open(workspace: &Path, team: &str) -> Result<Self, BackendError> {
|
|
111
|
-
Self::open_mode(workspace, team, false)
|
|
112
|
-
}
|
|
113
|
-
pub fn open_readonly(workspace: &Path, team: &str) -> Result<Self, BackendError> {
|
|
114
|
-
Self::open_mode(workspace, team, true)
|
|
115
|
-
}
|
|
116
|
-
fn open_mode(workspace: &Path, team: &str, readonly: bool) -> Result<Self, BackendError> {
|
|
117
|
-
let workspace = resolve_cwd(workspace)?;
|
|
118
|
-
let scope = scope_for(&workspace, team)?;
|
|
119
|
-
let root = root_for(&workspace.path, &scope);
|
|
120
|
-
let file_path = root.join("framework-binding.json");
|
|
121
|
-
let metadata = std::fs::symlink_metadata(&file_path)?;
|
|
122
|
-
if metadata.file_type().is_symlink() || !metadata.is_file() {
|
|
123
|
-
return Err(BackendError::Metadata);
|
|
124
|
-
}
|
|
125
|
-
let mut bytes = Vec::new();
|
|
126
|
-
std::fs::File::open(&file_path)?
|
|
127
|
-
.take(65537)
|
|
128
|
-
.read_to_end(&mut bytes)?;
|
|
129
|
-
if bytes.len() > 65536 {
|
|
130
|
-
return Err(BackendError::Metadata);
|
|
131
|
-
}
|
|
132
|
-
let binding: Binding =
|
|
133
|
-
serde_json::from_slice(&bytes).map_err(|_| BackendError::Metadata)?;
|
|
134
|
-
if binding.version != 1
|
|
135
|
-
|| binding.workspace != workspace
|
|
136
|
-
|| binding.team != team
|
|
137
|
-
|| binding.scope != scope
|
|
138
|
-
|| binding.endpoint != endpoint(&scope)
|
|
139
|
-
{
|
|
140
|
-
return Err(BackendError::Metadata);
|
|
141
|
-
}
|
|
142
|
-
let store = if readonly {
|
|
143
|
-
ContractStore::open_readonly(&binding.transport_root, scope, &binding.endpoint)?
|
|
144
|
-
} else {
|
|
145
|
-
ContractStore::open(&binding.transport_root, scope, &binding.endpoint)?
|
|
146
|
-
};
|
|
147
|
-
Ok(Self { store, binding })
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
/// Called only after the caller's pure/native-discovery preflight. Exclusive
|
|
151
|
-
/// roots are not adopted; partial initialization stays visible on failure.
|
|
152
|
-
fn create(
|
|
153
|
-
workspace: CwdIdentity,
|
|
154
|
-
team: &str,
|
|
155
|
-
candidate: PathBuf,
|
|
156
|
-
tmux: PathBuf,
|
|
157
|
-
) -> Result<Self, BackendError> {
|
|
158
|
-
use std::os::unix::fs::{DirBuilderExt, OpenOptionsExt};
|
|
159
|
-
let scope = scope_for(&workspace, team)?;
|
|
160
|
-
let root = root_for(&workspace.path, &scope);
|
|
161
|
-
let parent = root.parent().ok_or(BackendError::Metadata)?;
|
|
162
|
-
let base = parent.parent().ok_or(BackendError::Metadata)?;
|
|
163
|
-
if base.canonicalize()? != base {
|
|
164
|
-
return Err(BackendError::Metadata);
|
|
165
|
-
}
|
|
166
|
-
match std::fs::symlink_metadata(parent) {
|
|
167
|
-
Ok(metadata) if metadata.is_dir() && !metadata.file_type().is_symlink() => {}
|
|
168
|
-
Ok(_) => return Err(BackendError::Metadata),
|
|
169
|
-
Err(error) if error.kind() == std::io::ErrorKind::NotFound => {
|
|
170
|
-
std::fs::DirBuilder::new().mode(0o700).create(parent)?;
|
|
171
|
-
}
|
|
172
|
-
Err(error) => return Err(error.into()),
|
|
173
|
-
}
|
|
174
|
-
let candidate_sha256 =
|
|
175
|
-
fingerprint_file(&candidate, 1024 * 1024 * 1024, Duration::from_secs(10))?;
|
|
176
|
-
std::fs::DirBuilder::new().mode(0o700).create(&root)?;
|
|
177
|
-
// OS entropy gives this transport parent a fresh incarnation even when
|
|
178
|
-
// a logical team/name is reused. This is not a native session identity.
|
|
179
|
-
let mut nonce = [0u8; 32];
|
|
180
|
-
std::fs::File::open("/dev/urandom")?.read_exact(&mut nonce)?;
|
|
181
|
-
let transport_root = Path::new("/tmp")
|
|
182
|
-
.canonicalize()?
|
|
183
|
-
.join(format!("ta-{}", &digest_hex(digest(&nonce))[..24]));
|
|
184
|
-
// K3 and K2 share the same owned short root. Keeping only the pointer
|
|
185
|
-
// under the project avoids macOS sockaddr_un limits without weakening
|
|
186
|
-
// lifecycle's store-root fence or adopting an existing transport.
|
|
187
|
-
let store = ContractStore::create(&transport_root, scope.clone(), &endpoint(&scope))?;
|
|
188
|
-
let binding = Binding {
|
|
189
|
-
version: 1,
|
|
190
|
-
workspace,
|
|
191
|
-
team: team.into(),
|
|
192
|
-
scope: scope.clone(),
|
|
193
|
-
endpoint: endpoint(&scope),
|
|
194
|
-
candidate,
|
|
195
|
-
candidate_sha256,
|
|
196
|
-
tmux,
|
|
197
|
-
transport_root,
|
|
198
|
-
};
|
|
199
|
-
let bytes = serde_json::to_vec(&binding).map_err(|_| BackendError::Metadata)?;
|
|
200
|
-
let mut file = std::fs::OpenOptions::new()
|
|
201
|
-
.write(true)
|
|
202
|
-
.create_new(true)
|
|
203
|
-
.mode(0o600)
|
|
204
|
-
.open(root.join("framework-binding.json"))?;
|
|
205
|
-
file.write_all(&bytes)?;
|
|
206
|
-
file.sync_all()?;
|
|
207
|
-
Ok(Self { store, binding })
|
|
208
|
-
}
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
fn workspace_socket(path: &Path) -> Result<Option<(u64, u64)>, BackendError> {
|
|
212
|
-
use std::os::unix::fs::{FileTypeExt, MetadataExt};
|
|
213
|
-
let metadata = match std::fs::symlink_metadata(path) {
|
|
214
|
-
Ok(metadata) => metadata,
|
|
215
|
-
Err(error) if error.kind() == std::io::ErrorKind::NotFound => return Ok(None),
|
|
216
|
-
Err(error) => return Err(error.into()),
|
|
217
|
-
};
|
|
218
|
-
// Root already uses libc for UID checks; this syscall has no pointer input.
|
|
219
|
-
let uid = unsafe { libc::geteuid() };
|
|
220
|
-
if !metadata.file_type().is_socket() || metadata.uid() != uid {
|
|
221
|
-
return Err(ContractError::Mismatch("workspace socket owner/type").into());
|
|
222
|
-
}
|
|
223
|
-
Ok(Some((metadata.dev(), metadata.ino())))
|
|
224
|
-
}
|
|
225
|
-
|
|
226
|
-
/// The selected root transport is authority; ambient TMUX is never a selector.
|
|
227
|
-
/// This runs only in the owning lifecycle action, after native F0 admission.
|
|
228
|
-
pub(super) fn workspace_tmux(
|
|
229
|
-
binding: &Binding,
|
|
230
|
-
seat: &SeatId,
|
|
231
|
-
claim: Option<&team_agent_contract::host::transport::TargetReceipt>,
|
|
232
|
-
) -> Result<(SharedTmux, crate::tmux_backend::TmuxBackend, String), BackendError> {
|
|
233
|
-
use std::os::unix::fs::{DirBuilderExt, MetadataExt};
|
|
234
|
-
let selected = crate::state::selector::resolve_active_team_readonly(
|
|
235
|
-
&binding.workspace.path, Some(&binding.team), crate::state::selector::SelectorMode::RuntimeOnly,
|
|
236
|
-
).map_err(|_| ContractError::Mismatch("workspace topology scope"))?;
|
|
237
|
-
if selected.run_workspace != binding.workspace.path || selected.team_key != binding.team {
|
|
238
|
-
return Err(Error::Fence.into());
|
|
239
|
-
}
|
|
240
|
-
crate::lifecycle::launch::ensure_owner_allowed_for_state(&selected.state,
|
|
241
|
-
Some(&crate::model::ids::AgentId::new(seat.as_str())))
|
|
242
|
-
.map_err(|_| ContractError::Mismatch("workspace topology caller ownership"))?;
|
|
243
|
-
let transport = crate::lifecycle::restart::lifecycle_worker_tmux_backend_selection_for_state(
|
|
244
|
-
&selected.run_workspace, &selected.state,
|
|
245
|
-
).map_err(|_| ContractError::Mismatch("workspace topology transport"))?.backend;
|
|
246
|
-
let endpoint = transport.tmux_endpoint().filter(|s| !s.is_empty())
|
|
247
|
-
.ok_or(ContractError::Invalid("explicit workspace tmux endpoint required"))?;
|
|
248
|
-
let named = !Path::new(&endpoint).is_absolute();
|
|
249
|
-
let endpoint = if named {
|
|
250
|
-
crate::tmux_backend::socket_path_for_name(&endpoint)
|
|
251
|
-
.ok_or(ContractError::Invalid("workspace tmux socket name"))?
|
|
252
|
-
} else { PathBuf::from(endpoint) };
|
|
253
|
-
let parent = endpoint.parent().ok_or(ContractError::Invalid("workspace socket parent"))?;
|
|
254
|
-
if !parent.exists() && named && claim.is_none() {
|
|
255
|
-
if let Err(error) = std::fs::DirBuilder::new().mode(0o700).create(parent) {
|
|
256
|
-
if error.kind() != std::io::ErrorKind::AlreadyExists { return Err(error.into()); }
|
|
257
|
-
}
|
|
258
|
-
}
|
|
259
|
-
let metadata = std::fs::symlink_metadata(parent)?;
|
|
260
|
-
let physical_parent = parent.canonicalize()?;
|
|
261
|
-
if !metadata.is_dir() || metadata.uid() != unsafe { libc::geteuid() }
|
|
262
|
-
|| metadata.mode() & 0o777 != 0o700 || (!named && physical_parent != parent)
|
|
263
|
-
{ return Err(ContractError::Mismatch("workspace socket parent ownership").into()); }
|
|
264
|
-
let endpoint = physical_parent.join(endpoint.file_name().ok_or(ContractError::Invalid("workspace socket name"))?);
|
|
265
|
-
let session_name = selected.state.get("session_name").and_then(serde_json::Value::as_str)
|
|
266
|
-
.filter(|s| !s.is_empty() && !s.chars().any(|c| c.is_control() || c == ':'))
|
|
267
|
-
.ok_or(ContractError::Invalid("workspace team session name"))?.to_owned();
|
|
268
|
-
let generation = crate::state::projection::session_generation(&selected.state)
|
|
269
|
-
.unwrap_or(&session_name).to_owned();
|
|
270
|
-
let socket = workspace_socket(&endpoint)?;
|
|
271
|
-
let mut session = None;
|
|
272
|
-
if socket.is_some() {
|
|
273
|
-
let result = RealCommandRunner::default().run(&CommandRequest {
|
|
274
|
-
executable: binding.tmux.clone(), arguments: vec!["-u".into(), "-S".into(), endpoint.as_os_str().to_owned(),
|
|
275
|
-
"-N".into(), "list-sessions".into(), "-F".into(), "#{session_id}:#{session_name}".into()],
|
|
276
|
-
cwd: None, environment: EnvironmentDelta { remove: Default::default(), set: Default::default() },
|
|
277
|
-
stdin: None, budget: Duration::from_secs(5), limits: OutputLimits { stdout: 65536, stderr: 4096, stdin: 1 },
|
|
278
|
-
reject_stdout: None,
|
|
279
|
-
});
|
|
280
|
-
if result.success() {
|
|
281
|
-
let text = std::str::from_utf8(&result.stdout).map_err(|_| ContractError::Invalid("workspace session metadata"))?;
|
|
282
|
-
for line in text.lines() {
|
|
283
|
-
let (id, name) = line.split_once(':').ok_or(ContractError::Invalid("workspace session metadata"))?;
|
|
284
|
-
if !id.strip_prefix('$').is_some_and(|s| !s.is_empty() && s.bytes().all(|b| b.is_ascii_digit())) {
|
|
285
|
-
return Err(ContractError::Invalid("workspace session ID").into());
|
|
286
|
-
}
|
|
287
|
-
if name == session_name {
|
|
288
|
-
if session.replace(id.to_owned()).is_some() { return Err(Error::Fence.into()); }
|
|
289
|
-
}
|
|
290
|
-
}
|
|
291
|
-
} else if !(result.end == CommandEnd::Exited && result.exit_code == Some(1) && result.child_reaped
|
|
292
|
-
&& result.stdout.is_empty() && result.stderr == format!("no server running on {}\n", endpoint.display()).as_bytes())
|
|
293
|
-
{ return Err(ContractError::Mismatch("workspace server observation unavailable").into()); }
|
|
294
|
-
if workspace_socket(&endpoint)? != socket { return Err(Error::Fence.into()); }
|
|
295
|
-
}
|
|
296
|
-
if let Some(id) = &session {
|
|
297
|
-
let owner = transport.session_owner(&crate::transport::SessionName::new(&session_name))
|
|
298
|
-
.map_err(|_| ContractError::Mismatch("workspace session owner observation"))?;
|
|
299
|
-
let may_claim = claim.is_some_and(|target| target.shared_endpoint && target.created_session
|
|
300
|
-
&& target.identity().scope == binding.scope && &target.identity().seat == seat
|
|
301
|
-
&& target.endpoint == endpoint && target.session == *id
|
|
302
|
-
&& Some((target.socket_device, target.socket_inode)) == socket);
|
|
303
|
-
if !owner.as_ref().is_some_and(|owner| Path::new(&owner.workspace) == binding.workspace.path
|
|
304
|
-
&& owner.team == binding.team && owner.generation == generation)
|
|
305
|
-
&& !(owner.is_none() && may_claim)
|
|
306
|
-
{ return Err(ContractError::Mismatch("workspace session ownership").into()); }
|
|
307
|
-
} else if claim.is_some() { return Err(ContractError::Mismatch("committed workspace session disappeared").into()); }
|
|
308
|
-
if claim.is_some_and(|target| target.endpoint != endpoint
|
|
309
|
-
|| session.as_deref() != Some(target.session.as_str()))
|
|
310
|
-
{ return Err(ContractError::Mismatch("committed workspace target changed").into()); }
|
|
311
|
-
Ok((SharedTmux { endpoint, socket, session_name, session }, transport, generation))
|
|
312
|
-
}
|
|
313
|
-
|
|
314
|
-
/// Projection is saved first by the caller. Only an exact current target that
|
|
315
|
-
/// actually created this shared session may seed a missing root owner marker.
|
|
316
|
-
pub(super) fn bind_workspace_session(workspace: &Path, team: &str, seat: &SeatId) -> Result<(), BackendError> {
|
|
317
|
-
let backend = Backend::open_readonly(workspace, team)?;
|
|
318
|
-
let current = backend.store.seat(seat)?.ok_or(BackendError::Metadata)?;
|
|
319
|
-
let target = current.physical.as_ref().ok_or(BackendError::Metadata)?;
|
|
320
|
-
if !target.shared_endpoint { return Ok(()); }
|
|
321
|
-
let clock = RealClock::new();
|
|
322
|
-
let deadline = Duration::from_secs(5);
|
|
323
|
-
let mut host = TmuxHost::restore_owned(target.clone(), backend.binding.tmux.clone(),
|
|
324
|
-
settings(&backend.binding, registry::provider(current.provider.as_str())?).limits,
|
|
325
|
-
RealCommandRunner::default(), &clock, deadline)?;
|
|
326
|
-
host.validate_target(target, &clock, deadline)?;
|
|
327
|
-
let (topology, transport, generation) = workspace_tmux(&backend.binding, seat, Some(target))?;
|
|
328
|
-
let session = crate::transport::SessionName::new(&topology.session_name);
|
|
329
|
-
transport.set_session_owner_with_generation(&session, &backend.binding.workspace.path, team, &generation)
|
|
330
|
-
.map_err(|_| ContractError::Mismatch("workspace session owner commit"))?;
|
|
331
|
-
let owner = transport.session_owner(&session).map_err(|_| ContractError::Mismatch("workspace session owner readback"))?;
|
|
332
|
-
if !owner.is_some_and(|owner| Path::new(&owner.workspace) == backend.binding.workspace.path
|
|
333
|
-
&& owner.team == team && owner.generation == generation)
|
|
334
|
-
{ return Err(Error::Fence.into()); }
|
|
335
|
-
host.validate_target(target, &clock, deadline)?;
|
|
336
|
-
Ok(())
|
|
337
|
-
}
|
|
338
|
-
|
|
339
|
-
/// A session-wide legacy kill must not bypass canonical teardown of any
|
|
340
|
-
/// contract window, including a paused or currently unselected seat.
|
|
341
|
-
pub(crate) fn has_shared_windows(workspace: &Path, team: &str) -> Result<bool, BackendError> {
|
|
342
|
-
let cwd = resolve_cwd(workspace)?;
|
|
343
|
-
if !root_for(&cwd.path, &scope_for(&cwd, team)?).exists() { return Ok(false); }
|
|
344
|
-
let backend = Backend::open_readonly(&cwd.path, team)?;
|
|
345
|
-
Ok(backend.store.seats()?.iter().any(|seat|
|
|
346
|
-
seat.physical.as_ref().is_some_and(|target| target.shared_endpoint)))
|
|
347
|
-
}
|
|
348
|
-
|
|
349
|
-
pub(super) fn settings(binding: &Binding, provider: registry::NativeProvider) -> PhysicalSettings {
|
|
350
|
-
PhysicalSettings {
|
|
351
|
-
tmux: binding.tmux.clone(),
|
|
352
|
-
limits: HostLimits {
|
|
353
|
-
command_output: OutputLimits {
|
|
354
|
-
stdout: 256 * 1024,
|
|
355
|
-
stderr: 65536,
|
|
356
|
-
stdin: 2 * 1024 * 1024,
|
|
357
|
-
},
|
|
358
|
-
max_script_bytes: 128 * 1024,
|
|
359
|
-
max_capture_bytes: 256 * 1024,
|
|
360
|
-
max_payload_bytes: 1024 * 1024,
|
|
361
|
-
max_executable_bytes: 1024 * 1024 * 1024,
|
|
362
|
-
poll_interval: Duration::from_millis(100),
|
|
363
|
-
columns: 120,
|
|
364
|
-
rows: 40,
|
|
365
|
-
},
|
|
366
|
-
action_budget: provider.action_budget(),
|
|
367
|
-
freshness: Duration::from_secs(2),
|
|
368
|
-
journal_bytes: 2 * 1024 * 1024,
|
|
369
|
-
}
|
|
370
|
-
}
|
|
371
|
-
pub(super) fn adapter(
|
|
372
|
-
binding: &Binding,
|
|
373
|
-
identity: &InstanceIdentity,
|
|
374
|
-
provider: &ProviderId,
|
|
375
|
-
) -> Result<NativeAdapter, BackendError> {
|
|
376
|
-
Ok(registry::provider(provider.as_str())?.adapter(
|
|
377
|
-
binding.candidate.clone(),
|
|
378
|
-
vec![
|
|
379
|
-
"contract-mcp".into(),
|
|
380
|
-
"--workspace".into(),
|
|
381
|
-
binding
|
|
382
|
-
.workspace
|
|
383
|
-
.path
|
|
384
|
-
.to_str()
|
|
385
|
-
.ok_or(BackendError::Metadata)?
|
|
386
|
-
.into(),
|
|
387
|
-
"--team".into(),
|
|
388
|
-
binding.team.clone(),
|
|
389
|
-
"--seat".into(),
|
|
390
|
-
identity.seat.as_str().into(),
|
|
391
|
-
"--instance".into(),
|
|
392
|
-
identity.instance.as_str().into(),
|
|
393
|
-
"--generation".into(),
|
|
394
|
-
identity.generation.0.to_string(),
|
|
395
|
-
],
|
|
396
|
-
BTreeMap::new(),
|
|
397
|
-
)?.with_launch_context(binding.workspace.path.clone(), None)?)
|
|
398
|
-
}
|
|
399
|
-
pub(super) fn launch_adapter(
|
|
400
|
-
binding: &Binding,
|
|
401
|
-
identity: &InstanceIdentity,
|
|
402
|
-
provider: &ProviderId,
|
|
403
|
-
discovered: &Discovery,
|
|
404
|
-
) -> Result<NativeAdapter, BackendError> {
|
|
405
|
-
Ok(adapter(binding, identity, provider)?.with_launch_context(
|
|
406
|
-
binding.workspace.path.clone(), discovered.runtime.clone(),
|
|
407
|
-
)?)
|
|
408
|
-
}
|
|
409
|
-
pub(super) fn request(
|
|
410
|
-
binding: &Binding,
|
|
411
|
-
role: &RoleConfig,
|
|
412
|
-
identity: InstanceIdentity,
|
|
413
|
-
discovered: &Discovery,
|
|
414
|
-
) -> Result<LaunchRequest, BackendError> {
|
|
415
|
-
let descriptor = registry::provider(role.provider.as_str())?.descriptor();
|
|
416
|
-
if discovered.catalog.provider != role.provider
|
|
417
|
-
|| discovered.catalog.native != discovered.native
|
|
418
|
-
{
|
|
419
|
-
return Err(ContractError::Mismatch("role native catalog").into());
|
|
420
|
-
}
|
|
421
|
-
// Metadata-only discovery cannot authorize a native-subscription worker.
|
|
422
|
-
descriptor.auth.subscription.require("native subscription launch")?;
|
|
423
|
-
let profiles = descriptor.input.profiles.require("native input profiles")?;
|
|
424
|
-
let mut matches = profiles.iter().filter(|profile| {
|
|
425
|
-
profile.matches_native(&discovered.native)
|
|
426
|
-
&& profile.channel == Channel::Tmux
|
|
427
|
-
&& profile.operations.contains(&Operation::FirstBusiness)
|
|
428
|
-
});
|
|
429
|
-
let profile = matches.next().ok_or(ContractError::ProfileUnavailable)?;
|
|
430
|
-
if matches.next().is_some() {
|
|
431
|
-
return Err(ContractError::AmbiguousProfile.into());
|
|
432
|
-
}
|
|
433
|
-
profile.policy.require("native submit policy")?;
|
|
434
|
-
// The worker execs exactly the public command its runtime route was probed on.
|
|
435
|
-
let launcher = discovered.runtime.as_ref()
|
|
436
|
-
.ok_or(ContractError::Invalid("native runtime installation"))?
|
|
437
|
-
.launcher();
|
|
438
|
-
Ok(LaunchRequest {
|
|
439
|
-
provider: role.provider.as_str().into(),
|
|
440
|
-
operation: Operation::Fresh,
|
|
441
|
-
mode: LaunchMode::FullWorker,
|
|
442
|
-
auth: AuthMode::NativeSubscription,
|
|
443
|
-
model: Some(role.model.clone()),
|
|
444
|
-
role_effort: role.effort.clone(),
|
|
445
|
-
team_effort: None,
|
|
446
|
-
bypass: role.bypass,
|
|
447
|
-
prompt: Some(role.prompt.text.clone()),
|
|
448
|
-
identity,
|
|
449
|
-
native: discovered.native.clone(),
|
|
450
|
-
paths: LaunchPaths {
|
|
451
|
-
launcher,
|
|
452
|
-
executable: discovered.engine.clone(),
|
|
453
|
-
candidate: binding.candidate.clone(),
|
|
454
|
-
cwd: binding.workspace.clone(),
|
|
455
|
-
runtime_root: binding.transport_root.clone(),
|
|
456
|
-
},
|
|
457
|
-
channel: Channel::Tmux,
|
|
458
|
-
input_profile: Some(profile.id.into()),
|
|
459
|
-
evidence_kind: EvidenceKind::Native,
|
|
460
|
-
preassigned_session: None,
|
|
461
|
-
resume: None,
|
|
462
|
-
fork: None,
|
|
463
|
-
})
|
|
464
|
-
}
|
|
465
|
-
|
|
466
|
-
fn preflight_request(
|
|
467
|
-
binding: &Binding,
|
|
468
|
-
role: &RoleConfig,
|
|
469
|
-
identity: InstanceIdentity,
|
|
470
|
-
discovered: &Discovery,
|
|
471
|
-
) -> Result<LaunchRequest, BackendError> {
|
|
472
|
-
let mut request = request(binding, role, identity, discovered)?;
|
|
473
|
-
if matches!(registry::provider(role.provider.as_str())?, registry::NativeProvider::Codex) {
|
|
474
|
-
// Pure H2 only: this is a proposed private path, not a filesystem receipt.
|
|
475
|
-
request.paths.cwd = team_agent_contract::contract::session::CwdIdentity {
|
|
476
|
-
path: binding.transport_root.join("workspaces").join(role.id.as_str()),
|
|
477
|
-
identity: digest(b"effect-free-planned-private-cwd"),
|
|
478
|
-
};
|
|
479
|
-
}
|
|
480
|
-
Ok(request)
|
|
481
|
-
}
|
|
482
|
-
|
|
483
|
-
/// Providers whose native config search would otherwise inherit a Git or
|
|
484
|
-
/// project configuration ancestor. Metadata only; no body is read.
|
|
485
|
-
pub(super) fn require_private_primary(
|
|
486
|
-
selected: registry::NativeProvider,
|
|
487
|
-
cwd: &team_agent_contract::contract::session::CwdIdentity,
|
|
488
|
-
) -> Result<(), BackendError> {
|
|
489
|
-
match selected {
|
|
490
|
-
registry::NativeProvider::Codex => team_agent_contract::providers::codex::launcher::require_private_primary(cwd)?,
|
|
491
|
-
registry::NativeProvider::GrokBuild | registry::NativeProvider::Kiro | registry::NativeProvider::Cursor | registry::NativeProvider::Pi | registry::NativeProvider::Claude => {}
|
|
492
|
-
}
|
|
493
|
-
Ok(())
|
|
494
|
-
}
|
|
495
|
-
|
|
496
|
-
/// Team-wide native discovery/admission before the shared launcher creates any
|
|
497
|
-
/// seats (including legacy peers). Bounded read-only native commands run; no
|
|
498
|
-
/// runtime store, worker config, private socket, or worker process is created.
|
|
499
|
-
pub fn preflight(team: &super::config::TeamConfig) -> Result<(), BackendError> {
|
|
500
|
-
if team.roles.iter().all(|role| role.paused) {
|
|
501
|
-
return Ok(());
|
|
502
|
-
}
|
|
503
|
-
let mut discoveries = BTreeMap::new();
|
|
504
|
-
let candidate = std::env::current_exe()?.canonicalize()?;
|
|
505
|
-
let candidate_sha256 =
|
|
506
|
-
fingerprint_file(&candidate, 1024 * 1024 * 1024, Duration::from_secs(10))?;
|
|
507
|
-
let binding = Binding {
|
|
508
|
-
version: 1,
|
|
509
|
-
workspace: team.workspace.clone(),
|
|
510
|
-
team: team.selector.clone(),
|
|
511
|
-
scope: team.scope.clone(),
|
|
512
|
-
endpoint: endpoint(&team.scope),
|
|
513
|
-
candidate,
|
|
514
|
-
candidate_sha256,
|
|
515
|
-
tmux: discovery::executable("tmux")?,
|
|
516
|
-
transport_root: "/tmp/preflight-no-write".into(),
|
|
517
|
-
};
|
|
518
|
-
for role in team.roles.iter().filter(|role| !role.paused) {
|
|
519
|
-
let selected = registry::provider(role.provider.as_str())?;
|
|
520
|
-
if !discoveries.contains_key(&role.provider) {
|
|
521
|
-
discoveries.insert(
|
|
522
|
-
role.provider.clone(),
|
|
523
|
-
discovery::discover_runtime(&role.provider, &team.workspace.path)?,
|
|
524
|
-
);
|
|
525
|
-
}
|
|
526
|
-
let discovered = discoveries.get(&role.provider).ok_or(BackendError::Metadata)?;
|
|
527
|
-
let identity = InstanceIdentity {
|
|
528
|
-
scope: team.scope.clone(),
|
|
529
|
-
seat: role.id.clone(),
|
|
530
|
-
instance: InstanceId::new("preflight")?,
|
|
531
|
-
generation: Generation(1),
|
|
532
|
-
};
|
|
533
|
-
let adapter = launch_adapter(&binding, &identity, &role.provider, discovered)?;
|
|
534
|
-
let hooks = adapter.hooks();
|
|
535
|
-
let request = preflight_request(&binding, role, identity, discovered)?;
|
|
536
|
-
let resolved = resolve_launch_preflight(
|
|
537
|
-
selected.descriptor(),
|
|
538
|
-
&hooks,
|
|
539
|
-
&request,
|
|
540
|
-
Some(&discovered.catalog),
|
|
541
|
-
)?;
|
|
542
|
-
validate_launch_plan(selected.descriptor(), &resolved, &adapter.plan(&resolved)?)?;
|
|
543
|
-
}
|
|
544
|
-
Ok(())
|
|
545
|
-
}
|
|
546
|
-
|
|
547
|
-
#[derive(Debug)]
|
|
548
|
-
enum ExistingStart {
|
|
549
|
-
Committed(OperationRecord),
|
|
550
|
-
FinishReceipt(OperationRecord),
|
|
551
|
-
Pending(OperationRecord),
|
|
552
|
-
Resume(SeatRecord),
|
|
553
|
-
Blocked(OperationRecord),
|
|
554
|
-
}
|
|
555
|
-
|
|
556
|
-
// Receipt classification is read-only, never native admission or permission
|
|
557
|
-
// to replay an unfinished host effect. Check parent leases before old success.
|
|
558
|
-
fn existing_start(backend: &Backend, seat: &SeatId) -> Result<Option<ExistingStart>, BackendError> {
|
|
559
|
-
let unfinished: Vec<_> = backend.store.unfinished()?.into_iter().filter(|record| {
|
|
560
|
-
&record.target.identity.seat == seat
|
|
561
|
-
|| record.parent.as_ref().is_some_and(|parent| &parent.identity.seat == seat)
|
|
562
|
-
}).collect();
|
|
563
|
-
if unfinished.len() > 1 { return Err(Error::Fence.into()); }
|
|
564
|
-
if let Some(record) = unfinished.into_iter().next() {
|
|
565
|
-
if record.target.identity.scope != backend.binding.scope { return Err(Error::Fence.into()); }
|
|
566
|
-
return Ok(Some(if record.phase == Phase::F4Commit && record.pending.is_none()
|
|
567
|
-
&& matches!(record.kind, TransactionKind::Startup | TransactionKind::Restart
|
|
568
|
-
| TransactionKind::NewSeatFullSnapshot | TransactionKind::NativeNewSeat)
|
|
569
|
-
{ ExistingStart::FinishReceipt(record) }
|
|
570
|
-
else if record.outcome == Outcome::Running { ExistingStart::Pending(record) }
|
|
571
|
-
else { ExistingStart::Blocked(record) }));
|
|
572
|
-
}
|
|
573
|
-
let Some(current) = backend.store.seat(seat)? else {
|
|
574
|
-
return match backend.store.creation_receipt(seat, None)? {
|
|
575
|
-
None => Ok(None),
|
|
576
|
-
Some(record) if record.outcome == Outcome::Compensated => Ok(None),
|
|
577
|
-
// remove/reset ended this identity explicitly; the next start is fresh.
|
|
578
|
-
Some(record) if backend.store.retired(&record.target.identity)? => Ok(None),
|
|
579
|
-
Some(_) => Err(Error::Fence.into()),
|
|
580
|
-
};
|
|
581
|
-
};
|
|
582
|
-
if current.identity.scope != backend.binding.scope { return Err(Error::Fence.into()); }
|
|
583
|
-
let record = backend.store.creation_receipt(seat, Some(¤t.identity))?.ok_or(Error::Fence)?;
|
|
584
|
-
if record.outcome != Outcome::Committed || record.phase != Phase::F5Receipt || record.pending.is_some() {
|
|
585
|
-
return Ok(Some(ExistingStart::Blocked(record)));
|
|
586
|
-
}
|
|
587
|
-
Ok(Some(if current.status == SeatStatus::Stopped { ExistingStart::Resume(current) }
|
|
588
|
-
else { ExistingStart::Committed(record) }))
|
|
589
|
-
}
|
|
590
|
-
|
|
591
|
-
pub(super) fn require_candidate(binding: &Binding) -> Result<(), BackendError> {
|
|
592
|
-
let candidate = std::env::current_exe()?.canonicalize()?;
|
|
593
|
-
if candidate != binding.candidate || fingerprint_file(&candidate, 1024 * 1024 * 1024,
|
|
594
|
-
Duration::from_secs(10))? != binding.candidate_sha256 { return Err(Error::Fence.into()); }
|
|
595
|
-
Ok(())
|
|
596
|
-
}
|
|
597
|
-
|
|
598
|
-
/// Explicit discard-session may adopt an upgraded binary at the SAME installed
|
|
599
|
-
/// path, but only while every native seat is stopped and all operations ended.
|
|
600
|
-
/// Ordinary start/resume/MCP calls retain the exact candidate fingerprint fence.
|
|
601
|
-
pub(super) fn refresh_quiescent_candidate(workspace: &Path, team: &str, seat: &SeatId) -> Result<(), BackendError> {
|
|
602
|
-
use std::os::unix::fs::{MetadataExt, OpenOptionsExt};
|
|
603
|
-
let mut backend = Backend::open(workspace, team)?;
|
|
604
|
-
let candidate = std::env::current_exe()?.canonicalize()?;
|
|
605
|
-
if candidate != backend.binding.candidate { return Err(Error::Fence.into()); }
|
|
606
|
-
let fingerprint = fingerprint_file(&candidate, 1024 * 1024 * 1024, Duration::from_secs(10))?;
|
|
607
|
-
if fingerprint == backend.binding.candidate_sha256 { return Ok(()); }
|
|
608
|
-
let root = root_for(workspace, &backend.binding.scope);
|
|
609
|
-
if root.canonicalize()? != root { return Err(Error::Fence.into()); }
|
|
610
|
-
let directory = std::fs::File::open(&root)?;
|
|
611
|
-
if !crate::platform::file_lock::try_lock_once_nonblocking(&directory)? { return Err(Error::Conflict.into()); }
|
|
612
|
-
if !backend.store.unfinished()?.is_empty() { return Err(Error::Conflict.into()); }
|
|
613
|
-
for current in backend.store.seats()? {
|
|
614
|
-
if current.status != SeatStatus::Stopped || backend.store.leased(¤t.identity.seat)?
|
|
615
|
-
|| current.physical.as_ref().is_some_and(|target| !matches!(
|
|
616
|
-
team_agent_contract::host::process::sample_process(&target.process),
|
|
617
|
-
team_agent_contract::host::process::ProcessState::Exited | team_agent_contract::host::process::ProcessState::Replaced)) {
|
|
618
|
-
return Err(Error::Invalid("stop the native cohort before upgrading its candidate binding").into());
|
|
619
|
-
}
|
|
620
|
-
}
|
|
621
|
-
let path = root.join("framework-binding.json");
|
|
622
|
-
let metadata = std::fs::symlink_metadata(&path)?;
|
|
623
|
-
if !metadata.is_file() || metadata.uid() != unsafe { libc::geteuid() }
|
|
624
|
-
|| metadata.nlink() != 1 || metadata.mode() & 0o022 != 0 { return Err(Error::Fence.into()); }
|
|
625
|
-
let before = std::fs::read(&path)?;
|
|
626
|
-
backend.binding.candidate_sha256 = fingerprint;
|
|
627
|
-
let temporary = root.join(format!("binding-{}.tmp", backend.store.propose_identity(seat)?.instance.as_str()));
|
|
628
|
-
let mut file = std::fs::OpenOptions::new().write(true).create_new(true).mode(metadata.mode() & 0o777).open(&temporary)?;
|
|
629
|
-
let result = (|| -> Result<(), BackendError> {
|
|
630
|
-
file.write_all(&serde_json::to_vec(&backend.binding).map_err(|_| BackendError::Metadata)?)?;
|
|
631
|
-
file.sync_all()?;
|
|
632
|
-
if std::fs::read(&path)? != before { return Err(Error::Fence.into()); }
|
|
633
|
-
std::fs::rename(&temporary, &path)?;
|
|
634
|
-
directory.sync_all()?;
|
|
635
|
-
Ok(())
|
|
636
|
-
})();
|
|
637
|
-
if result.is_err() { let _ = std::fs::remove_file(&temporary); }
|
|
638
|
-
result
|
|
639
|
-
}
|
|
640
|
-
|
|
641
|
-
fn validate_existing(backend: &Backend, role: &RoleConfig, record: &OperationRecord) -> Result<SeatRecord, BackendError> {
|
|
642
|
-
let current = backend.store.assert_current(&record.target.identity)?;
|
|
643
|
-
if current.evidence_kind != EvidenceKind::Native || current.provider != role.provider
|
|
644
|
-
|| current.status == SeatStatus::Unknown
|
|
645
|
-
|| serde_json::to_vec(&read_role(&backend.binding, ¤t.identity)?).map_err(|_| BackendError::Metadata)?
|
|
646
|
-
!= serde_json::to_vec(role).map_err(|_| BackendError::Metadata)?
|
|
647
|
-
{ return Err(ContractError::Mismatch("existing native launch configuration").into()); }
|
|
648
|
-
let target = current.physical.as_ref().ok_or(BackendError::Metadata)?;
|
|
649
|
-
let clock = RealClock::new();
|
|
650
|
-
let deadline = Duration::from_secs(5);
|
|
651
|
-
let mut host = TmuxHost::restore_owned(target.clone(), backend.binding.tmux.clone(),
|
|
652
|
-
settings(&backend.binding, registry::provider(current.provider.as_str())?).limits,
|
|
653
|
-
RealCommandRunner::default(), &clock, deadline)?;
|
|
654
|
-
host.validate_target(target, &clock, deadline)?;
|
|
655
|
-
Ok(current)
|
|
656
|
-
}
|
|
657
|
-
|
|
658
|
-
fn receipt_error(record: OperationRecord) -> BackendError {
|
|
659
|
-
BackendError::Shutdown { operation: record.id, phase: record.phase,
|
|
660
|
-
outcome: record.outcome, failure: record.failure.or(record.pending) }
|
|
661
|
-
}
|
|
662
|
-
|
|
663
|
-
/// Plan only against this selected store and exact row (or an unbound seed).
|
|
664
|
-
/// A stale parent can be repaired only by its actual creation receipt, never
|
|
665
|
-
/// by selecting the greatest generation or adopting a discovered process.
|
|
666
|
-
pub(crate) fn start_needs_reconciliation(workspace: &Path, team: &str, seat: &SeatId, row: &serde_json::Value) -> Result<bool, BackendError> {
|
|
667
|
-
let backend = Backend::open_readonly(workspace, team)?;
|
|
668
|
-
require_candidate(&backend.binding)?;
|
|
669
|
-
let class = existing_start(&backend, seat)?;
|
|
670
|
-
let current = backend.store.seat(seat)?;
|
|
671
|
-
if current.as_ref().is_some_and(|seat| !registry::same_provider(row.get("provider").and_then(serde_json::Value::as_str).unwrap_or(""), seat.provider.as_str())) {
|
|
672
|
-
return Err(Error::Fence.into());
|
|
673
|
-
}
|
|
674
|
-
let matches = |identity: &InstanceIdentity| {
|
|
675
|
-
row.get("contract_scope").and_then(serde_json::Value::as_str) == Some(identity.scope.as_str())
|
|
676
|
-
&& row.get("contract_instance").and_then(serde_json::Value::as_str) == Some(identity.instance.as_str())
|
|
677
|
-
&& row.get("generation").and_then(serde_json::Value::as_u64) == Some(identity.generation.0)
|
|
678
|
-
};
|
|
679
|
-
let seed = ["contract_scope", "contract_instance", "generation", "session_id"].iter()
|
|
680
|
-
.all(|key| row.get(*key).is_none_or(serde_json::Value::is_null));
|
|
681
|
-
let record = match &class {
|
|
682
|
-
Some(ExistingStart::Committed(record) | ExistingStart::FinishReceipt(record)) => Some(record),
|
|
683
|
-
_ => None,
|
|
684
|
-
};
|
|
685
|
-
if !seed && !current.as_ref().is_some_and(|seat| matches(&seat.identity))
|
|
686
|
-
&& !record.and_then(|record| record.parent.as_ref()).is_some_and(|parent| matches(&parent.identity)
|
|
687
|
-
&& row.get("session_id").and_then(serde_json::Value::as_str)
|
|
688
|
-
== parent.session.as_ref().map(|session| session.native_session.as_str()))
|
|
689
|
-
{ return Err(Error::Fence.into()); }
|
|
690
|
-
let needs_projection = seed || current.as_ref().is_some_and(|seat| !matches(&seat.identity)
|
|
691
|
-
|| seat.status == SeatStatus::Starting
|
|
692
|
-
|| row.get("session_id").and_then(serde_json::Value::as_str) != seat.session.as_ref().map(|session| session.native_session.as_str())
|
|
693
|
-
|| (seat.status == SeatStatus::Stopped && row.get("status").and_then(serde_json::Value::as_str) != Some("stopped"))
|
|
694
|
-
|| (seat.status != SeatStatus::Stopped && row.get("status").and_then(serde_json::Value::as_str) != Some("running")))
|
|
695
|
-
|| matches!(class, Some(ExistingStart::FinishReceipt(_) | ExistingStart::Pending(_) | ExistingStart::Blocked(_)));
|
|
696
|
-
if needs_projection { return Ok(true); }
|
|
697
|
-
if let Some(current) = current.filter(|current| current.status != SeatStatus::Stopped) {
|
|
698
|
-
if let Some(target) = current.physical.as_ref().filter(|target| target.shared_endpoint) {
|
|
699
|
-
// A crash after JSON projection but before the owner marker is not
|
|
700
|
-
// a verified no-op. Reuse only this exact creator receipt, under
|
|
701
|
-
// the existing lifecycle lock; a foreign marker still refuses.
|
|
702
|
-
let (topology, transport, _) = workspace_tmux(&backend.binding, seat, Some(target))?;
|
|
703
|
-
return Ok(transport.session_owner(&crate::transport::SessionName::new(&topology.session_name))
|
|
704
|
-
.map_err(|_| ContractError::Mismatch("workspace session owner observation"))?.is_none());
|
|
705
|
-
}
|
|
706
|
-
}
|
|
707
|
-
Ok(false)
|
|
708
|
-
}
|
|
709
|
-
|
|
710
|
-
/// Read-only preflight, reused by the CLI before any role write or stop.
|
|
711
|
-
pub(crate) fn preflight_start_role(workspace: &Path, team: &str, role: &RoleConfig) -> Result<bool, BackendError> {
|
|
712
|
-
let backend = Backend::open_readonly(workspace, team)?;
|
|
713
|
-
require_candidate(&backend.binding)?;
|
|
714
|
-
match existing_start(&backend, &role.id)? {
|
|
715
|
-
Some(ExistingStart::Committed(record))
|
|
716
|
-
if backend.store.assert_current(&record.target.identity)?.physical.as_ref().is_some_and(|target|
|
|
717
|
-
team_agent_contract::host::process::sample_process(&target.process) == team_agent_contract::host::process::ProcessState::Exited) => {
|
|
718
|
-
prepare_resume(workspace, team, &role.id, Some(role))?;
|
|
719
|
-
},
|
|
720
|
-
Some(ExistingStart::Committed(record) | ExistingStart::FinishReceipt(record)) => { validate_existing(&backend, role, &record)?; },
|
|
721
|
-
Some(ExistingStart::Resume(_)) => { prepare_resume(workspace, team, &role.id, Some(role))?; },
|
|
722
|
-
Some(ExistingStart::Pending(record) | ExistingStart::Blocked(record)) => return Err(receipt_error(record)),
|
|
723
|
-
None => return Ok(true), // Caller must run full-team fresh preflight before writes.
|
|
724
|
-
}
|
|
725
|
-
Ok(false)
|
|
726
|
-
}
|
|
727
|
-
|
|
728
|
-
fn finish_receipt(backend: &mut Backend, record: OperationRecord) -> Result<OperationRecord, BackendError> {
|
|
729
|
-
let selected = registry::provider(record.target.provider.as_str())?;
|
|
730
|
-
let adapter = adapter(&backend.binding, &record.target.identity, &record.target.provider)?;
|
|
731
|
-
let hooks = adapter.hooks();
|
|
732
|
-
let clock = RealClock::new();
|
|
733
|
-
let mut host = PhysicalRuntime::new(Registration { descriptor: selected.descriptor(), hooks: &hooks,
|
|
734
|
-
controls: selected.controls(), startup: selected.startup(), session_panel: selected.session_panel() },
|
|
735
|
-
settings(&backend.binding, selected), record.id.clone(), &clock)?;
|
|
736
|
-
Ok(Lifecycle { store: &mut backend.store, host: &mut host, io: &mut NoNewIo }.recover(&record.id)?)
|
|
737
|
-
}
|
|
738
|
-
|
|
739
|
-
/// No creation receipt, unfinished lease or seat exists for this ID.
|
|
740
|
-
pub(super) fn seat_unclaimed(backend: &Backend, seat: &SeatId) -> Result<bool, BackendError> {
|
|
741
|
-
Ok(existing_start(backend, seat)?.is_none())
|
|
742
|
-
}
|
|
743
|
-
|
|
744
|
-
/// Team-level forwarding peers (legacy members and the leader) that every new
|
|
745
|
-
/// native seat's framework route depends on. Shared by fresh start and fork.
|
|
746
|
-
pub(super) fn publish_framework_routes(backend: &mut Backend, members: &[MemberRoute]) -> Result<(), BackendError> {
|
|
747
|
-
let route = backend.binding.framework().route()?;
|
|
748
|
-
let mut peers: Vec<_> = members
|
|
749
|
-
.iter()
|
|
750
|
-
.filter(|member| member.runtime == RuntimeFamily::Legacy)
|
|
751
|
-
.map(
|
|
752
|
-
|member| team_agent_contract::orchestration::forward::FrameworkPeer {
|
|
753
|
-
recipient: member.id.clone(),
|
|
754
|
-
route: route.clone(),
|
|
755
|
-
provider: member.provider.clone(),
|
|
756
|
-
},
|
|
757
|
-
)
|
|
758
|
-
.collect();
|
|
759
|
-
peers.push(team_agent_contract::orchestration::forward::FrameworkPeer {
|
|
760
|
-
recipient: "leader".into(),
|
|
761
|
-
route: route.clone(),
|
|
762
|
-
provider: "framework".into(),
|
|
763
|
-
});
|
|
764
|
-
backend.store.set_framework_routes(&peers, Some(&route))?;
|
|
765
|
-
Ok(())
|
|
766
|
-
}
|
|
767
|
-
|
|
768
|
-
/// The native session ID a launch names before spawn, for the leaves whose
|
|
769
|
-
/// fresh session is `Preassigned`. Shared by fresh start and a native fork's child.
|
|
770
|
-
pub(super) fn preassign_session(
|
|
771
|
-
request: &mut LaunchRequest,
|
|
772
|
-
discovered: &Discovery,
|
|
773
|
-
) -> Result<(), BackendError> {
|
|
774
|
-
if matches!(&discovered.runtime, Some(registry::NativeRuntime::Claude(_))) {
|
|
775
|
-
request.preassigned_session = Some(team_agent_contract::providers::claude::session_id(&request.identity));
|
|
776
|
-
}
|
|
777
|
-
if let Some(registry::NativeRuntime::Cursor(runtime)) = &discovered.runtime {
|
|
778
|
-
request.preassigned_session = Some(
|
|
779
|
-
team_agent_contract::providers::cursor::native::allocate_chat(
|
|
780
|
-
runtime, &request.paths.cwd, &mut RealCommandRunner::default(),
|
|
781
|
-
team_agent_contract::contract::hooks::ReadBounds {
|
|
782
|
-
deadline: Duration::from_secs(90), max_output_bytes: 1024 * 1024,
|
|
783
|
-
},
|
|
784
|
-
).map_err(|error| BackendError::Discovery(DiscoveryError::Catalog(error)))?,
|
|
785
|
-
);
|
|
786
|
-
}
|
|
787
|
-
Ok(())
|
|
788
|
-
}
|
|
789
|
-
|
|
790
|
-
/// Fresh startup is distinct from resume. An existing generation is never
|
|
791
|
-
/// silently replaced with a new native session.
|
|
792
|
-
pub fn start(
|
|
793
|
-
workspace: &Path,
|
|
794
|
-
team: &str,
|
|
795
|
-
role: &RoleConfig,
|
|
796
|
-
members: &[MemberRoute],
|
|
797
|
-
) -> Result<(Binding, OperationRecord), BackendError> {
|
|
798
|
-
let cwd = resolve_cwd(workspace)?;
|
|
799
|
-
let scope = scope_for(&cwd, team)?;
|
|
800
|
-
if root_for(&cwd.path, &scope).exists() {
|
|
801
|
-
let mut backend = Backend::open(&cwd.path, team)?;
|
|
802
|
-
require_candidate(&backend.binding)?;
|
|
803
|
-
match existing_start(&backend, &role.id)? {
|
|
804
|
-
Some(ExistingStart::Committed(record)) => {
|
|
805
|
-
let current = backend.store.assert_current(&record.target.identity)?;
|
|
806
|
-
if current.physical.as_ref().is_some_and(|target|
|
|
807
|
-
team_agent_contract::host::process::sample_process(&target.process) == team_agent_contract::host::process::ProcessState::Exited) {
|
|
808
|
-
let prepared = prepare_resume(workspace, team, &role.id, Some(role))?;
|
|
809
|
-
return Ok((backend.binding, resume(prepared)?));
|
|
810
|
-
}
|
|
811
|
-
validate_existing(&backend, role, &record)?;
|
|
812
|
-
return Ok((backend.binding, record));
|
|
813
|
-
},
|
|
814
|
-
Some(ExistingStart::FinishReceipt(record)) => {
|
|
815
|
-
validate_existing(&backend, role, &record)?;
|
|
816
|
-
let record = finish_receipt(&mut backend, record)?;
|
|
817
|
-
return Ok((backend.binding, record));
|
|
818
|
-
},
|
|
819
|
-
Some(ExistingStart::Resume(current)) => {
|
|
820
|
-
if current.provider != role.provider { return Err(Error::Fence.into()); }
|
|
821
|
-
let prepared = prepare_resume(workspace, team, &role.id, Some(role))?;
|
|
822
|
-
return Ok((backend.binding, resume(prepared)?));
|
|
823
|
-
},
|
|
824
|
-
Some(ExistingStart::Pending(record) | ExistingStart::Blocked(record)) => return Err(receipt_error(record)),
|
|
825
|
-
None => {},
|
|
826
|
-
}
|
|
827
|
-
}
|
|
828
|
-
let selected = registry::provider(role.provider.as_str())?;
|
|
829
|
-
let descriptor = selected.descriptor();
|
|
830
|
-
let discovered = discovery::discover_runtime(&role.provider, workspace)?;
|
|
831
|
-
let cwd = resolve_cwd(workspace)?;
|
|
832
|
-
let scope = scope_for(&cwd, team)?;
|
|
833
|
-
let candidate = std::env::current_exe()?.canonicalize()?;
|
|
834
|
-
let tmux = discovery::executable("tmux")?;
|
|
835
|
-
let candidate_sha256 =
|
|
836
|
-
fingerprint_file(&candidate, 1024 * 1024 * 1024, Duration::from_secs(10))?;
|
|
837
|
-
let provisional = Binding {
|
|
838
|
-
version: 1,
|
|
839
|
-
workspace: cwd.clone(),
|
|
840
|
-
team: team.into(),
|
|
841
|
-
scope: scope.clone(),
|
|
842
|
-
endpoint: endpoint(&scope),
|
|
843
|
-
candidate: candidate.clone(),
|
|
844
|
-
candidate_sha256,
|
|
845
|
-
tmux: tmux.clone(),
|
|
846
|
-
transport_root: PathBuf::from("/tmp/preflight-no-write"),
|
|
847
|
-
};
|
|
848
|
-
let provisional_identity = InstanceIdentity {
|
|
849
|
-
scope: scope.clone(),
|
|
850
|
-
seat: role.id.clone(),
|
|
851
|
-
instance: InstanceId::new("preflight")?,
|
|
852
|
-
generation: Generation(1),
|
|
853
|
-
};
|
|
854
|
-
let probe_adapter = launch_adapter(&provisional, &provisional_identity, &role.provider, &discovered)?;
|
|
855
|
-
let probe_hooks = probe_adapter.hooks();
|
|
856
|
-
let preflight = preflight_request(&provisional, role, provisional_identity, &discovered)?;
|
|
857
|
-
let preflight = resolve_launch_preflight(
|
|
858
|
-
descriptor,
|
|
859
|
-
&probe_hooks,
|
|
860
|
-
&preflight,
|
|
861
|
-
Some(&discovered.catalog),
|
|
862
|
-
)?;
|
|
863
|
-
let preflight_plan = probe_adapter.plan(&preflight)?;
|
|
864
|
-
validate_launch_plan(descriptor, &preflight, &preflight_plan)?;
|
|
865
|
-
let mut backend = if root_for(&cwd.path, &scope).exists() {
|
|
866
|
-
Backend::open(&cwd.path, team)?
|
|
867
|
-
} else {
|
|
868
|
-
Backend::create(cwd, team, candidate, tmux)?
|
|
869
|
-
};
|
|
870
|
-
if backend.binding.candidate != provisional.candidate
|
|
871
|
-
|| backend.binding.candidate_sha256 != candidate_sha256
|
|
872
|
-
{
|
|
873
|
-
return Err(Error::Fence.into());
|
|
874
|
-
}
|
|
875
|
-
if existing_start(&backend, &role.id)?.is_some() {
|
|
876
|
-
return Err(Error::Conflict.into());
|
|
877
|
-
}
|
|
878
|
-
publish_framework_routes(&mut backend, members)?;
|
|
879
|
-
let identity = backend.store.propose_identity(&role.id)?;
|
|
880
|
-
let server_key = selected.server_key_for(&identity)?;
|
|
881
|
-
write_role(&backend.binding, &identity, role)?;
|
|
882
|
-
let adapter = launch_adapter(&backend.binding, &identity, &role.provider, &discovered)?;
|
|
883
|
-
let hooks = adapter.hooks();
|
|
884
|
-
let mut request = request(&backend.binding, role, identity, &discovered)?;
|
|
885
|
-
if !descriptor.workspace.shared_cwd {
|
|
886
|
-
request.paths.cwd = super::workspace::prepare(&backend.binding, &role.id)?;
|
|
887
|
-
}
|
|
888
|
-
require_private_primary(selected, &request.paths.cwd)?;
|
|
889
|
-
preassign_session(&mut request, &discovered)?;
|
|
890
|
-
let resolved = resolve_launch(
|
|
891
|
-
descriptor,
|
|
892
|
-
&hooks,
|
|
893
|
-
&request,
|
|
894
|
-
Some(&discovered.catalog),
|
|
895
|
-
)?;
|
|
896
|
-
let plan = adapter.plan(&resolved)?;
|
|
897
|
-
validate_launch_plan(descriptor, &resolved, &plan)?;
|
|
898
|
-
let (workspace_topology, _, _) = workspace_tmux(&backend.binding, &role.id, None)?;
|
|
899
|
-
let operation = OperationId::new(format!("start-{}", request.identity.instance.as_str()))?;
|
|
900
|
-
let mut io = ScopedMaterializer::new(
|
|
901
|
-
descriptor,
|
|
902
|
-
&resolved,
|
|
903
|
-
&plan,
|
|
904
|
-
operation.clone(),
|
|
905
|
-
RealCommandRunner::default(),
|
|
906
|
-
)?;
|
|
907
|
-
let clock = RealClock::new();
|
|
908
|
-
let mut physical = PhysicalRuntime::new(
|
|
909
|
-
Registration {
|
|
910
|
-
descriptor,
|
|
911
|
-
hooks: &hooks,
|
|
912
|
-
controls: selected.controls(),
|
|
913
|
-
startup: selected.startup(),
|
|
914
|
-
session_panel: selected.session_panel(),
|
|
915
|
-
},
|
|
916
|
-
settings(&backend.binding, selected),
|
|
917
|
-
operation.clone(),
|
|
918
|
-
&clock,
|
|
919
|
-
)?;
|
|
920
|
-
physical.evidence = policy_evidence(&role.provider, &request.native, candidate_sha256)?;
|
|
921
|
-
physical.workspace_tmux = Some(workspace_topology);
|
|
922
|
-
let record = Lifecycle {
|
|
923
|
-
store: &mut backend.store,
|
|
924
|
-
host: &mut physical,
|
|
925
|
-
io: &mut io,
|
|
926
|
-
}
|
|
927
|
-
.startup(
|
|
928
|
-
&Adapter {
|
|
929
|
-
descriptor,
|
|
930
|
-
hooks: &hooks,
|
|
931
|
-
catalog: Some(&discovered.catalog),
|
|
932
|
-
},
|
|
933
|
-
&request,
|
|
934
|
-
Routing {
|
|
935
|
-
pane: "unpublished".into(),
|
|
936
|
-
binding_key: format!("binding-{}", request.identity.instance.as_str()),
|
|
937
|
-
server_key: server_key.into(),
|
|
938
|
-
},
|
|
939
|
-
operation,
|
|
940
|
-
)?;
|
|
941
|
-
Ok((backend.binding, record))
|
|
942
|
-
}
|
|
943
|
-
|
|
944
|
-
/// A fully admitted resume proposal. It is still effect-free: no parent stop,
|
|
945
|
-
/// session allocation, snapshot write or config replacement has occurred.
|
|
946
|
-
pub struct PreparedResume {
|
|
947
|
-
binding: Binding,
|
|
948
|
-
role: RoleConfig,
|
|
949
|
-
discovered: Discovery,
|
|
950
|
-
request: LaunchRequest,
|
|
951
|
-
}
|
|
952
|
-
|
|
953
|
-
impl PreparedResume {
|
|
954
|
-
pub(crate) fn seat(&self) -> &SeatId { &self.request.identity.seat }
|
|
955
|
-
pub(crate) fn session(&self) -> Option<&str> {
|
|
956
|
-
self.request.resume.as_ref().map(|resume| resume.binding.native_session.as_str())
|
|
957
|
-
}
|
|
958
|
-
}
|
|
959
|
-
|
|
960
|
-
pub(crate) fn prepare_restart(
|
|
961
|
-
workspace: &Path,
|
|
962
|
-
team: &str,
|
|
963
|
-
directory: &Path,
|
|
964
|
-
state: &serde_json::Value,
|
|
965
|
-
) -> Result<Vec<PreparedResume>, BackendError> {
|
|
966
|
-
let config = super::framework::team_config(workspace, directory, team, None)?.ok_or(BackendError::Metadata)?;
|
|
967
|
-
let mut prepared = Vec::new();
|
|
968
|
-
let agents = state.get("agents").and_then(serde_json::Value::as_object).ok_or(BackendError::Metadata)?;
|
|
969
|
-
for (id, _) in agents.iter().filter(|(_, row)| {
|
|
970
|
-
super::framework::is_contract(row)
|
|
971
|
-
&& !crate::state::projection::agent_stopped_for_restart(row)
|
|
972
|
-
}) {
|
|
973
|
-
let Some(role) = config.roles.iter().find(|role| role.id.as_str() == id) else {
|
|
974
|
-
// Removed roles are not silently re-created from an old snapshot.
|
|
975
|
-
continue;
|
|
976
|
-
};
|
|
977
|
-
if role.paused { continue; }
|
|
978
|
-
// The native store is authoritative even if its framework mirror is
|
|
979
|
-
// stale. Only explicit start-agent may resume a stopped generation.
|
|
980
|
-
let backend = Backend::open_readonly(workspace, team)?;
|
|
981
|
-
let seat = backend.store.seat(&role.id)?.ok_or(BackendError::Metadata)?;
|
|
982
|
-
if seat.status == SeatStatus::Stopped { continue; }
|
|
983
|
-
prepared.push(prepare_resume(workspace, team, &role.id, Some(role))?);
|
|
984
|
-
}
|
|
985
|
-
Ok(prepared)
|
|
986
|
-
}
|
|
987
|
-
|
|
988
|
-
pub fn prepare_resume(
|
|
989
|
-
workspace: &Path,
|
|
990
|
-
team: &str,
|
|
991
|
-
seat: &SeatId,
|
|
992
|
-
role: Option<&RoleConfig>,
|
|
993
|
-
) -> Result<PreparedResume, BackendError> {
|
|
994
|
-
let backend = Backend::open_readonly(workspace, team)?;
|
|
995
|
-
let parent = backend.store.seat(seat)?.ok_or(BackendError::Metadata)?;
|
|
996
|
-
if parent.status == SeatStatus::Unknown {
|
|
997
|
-
return Err(Error::Fence.into());
|
|
998
|
-
}
|
|
999
|
-
let role = match role {
|
|
1000
|
-
Some(role) => role.clone(),
|
|
1001
|
-
None => read_role(&backend.binding, &parent.identity)?,
|
|
1002
|
-
};
|
|
1003
|
-
if role.id != parent.identity.seat || role.provider != parent.provider {
|
|
1004
|
-
return Err(ContractError::Mismatch("resume role provider").into());
|
|
1005
|
-
}
|
|
1006
|
-
let source = parent.session.clone().ok_or(ContractError::Invalid("native resume binding required"))?;
|
|
1007
|
-
if resolve_cwd(&source.cwd.path)? != source.cwd {
|
|
1008
|
-
return Err(ContractError::Mismatch("resume cwd incarnation").into());
|
|
1009
|
-
}
|
|
1010
|
-
let candidate = std::env::current_exe()?.canonicalize()?;
|
|
1011
|
-
let candidate_sha256 = fingerprint_file(&candidate, 1024 * 1024 * 1024, Duration::from_secs(10))?;
|
|
1012
|
-
if backend.binding.candidate != candidate || backend.binding.candidate_sha256 != candidate_sha256 {
|
|
1013
|
-
return Err(Error::Fence.into());
|
|
1014
|
-
}
|
|
1015
|
-
let discovered = discovery::discover_runtime(&role.provider, workspace)?;
|
|
1016
|
-
let identity = backend.store.propose_identity(seat)?;
|
|
1017
|
-
let adapter = launch_adapter(&backend.binding, &identity, &role.provider, &discovered)?;
|
|
1018
|
-
let hooks = adapter.hooks();
|
|
1019
|
-
let mut request = request(&backend.binding, &role, identity, &discovered)?;
|
|
1020
|
-
request.operation = Operation::Resume;
|
|
1021
|
-
request.paths.cwd = source.cwd.clone();
|
|
1022
|
-
request.resume = Some(ResumeRequest {
|
|
1023
|
-
binding: source, expected_source: parent.identity,
|
|
1024
|
-
});
|
|
1025
|
-
let selected = registry::provider(role.provider.as_str())?;
|
|
1026
|
-
require_private_primary(selected, &request.paths.cwd)?;
|
|
1027
|
-
let resolved = resolve_launch(selected.descriptor(), &hooks, &request, Some(&discovered.catalog))?;
|
|
1028
|
-
validate_launch_plan(selected.descriptor(), &resolved, &adapter.plan(&resolved)?)?;
|
|
1029
|
-
Ok(PreparedResume { binding: backend.binding, role, discovered, request })
|
|
1030
|
-
}
|
|
1031
|
-
|
|
1032
|
-
pub fn resume(prepared: PreparedResume) -> Result<OperationRecord, BackendError> {
|
|
1033
|
-
let mut backend = Backend::open(&prepared.binding.workspace.path, &prepared.binding.team)?;
|
|
1034
|
-
if serde_json::to_vec(&backend.binding).map_err(|_| BackendError::Metadata)?
|
|
1035
|
-
!= serde_json::to_vec(&prepared.binding).map_err(|_| BackendError::Metadata)?
|
|
1036
|
-
{
|
|
1037
|
-
return Err(Error::Fence.into());
|
|
1038
|
-
}
|
|
1039
|
-
let source = prepared.request.resume.as_ref().ok_or(BackendError::Metadata)?;
|
|
1040
|
-
let parent = backend.store.assert_current(&source.expected_source)?;
|
|
1041
|
-
if parent.session.as_ref() != Some(&source.binding) {
|
|
1042
|
-
return Err(Error::Fence.into());
|
|
1043
|
-
}
|
|
1044
|
-
let selected = registry::provider(prepared.role.provider.as_str())?;
|
|
1045
|
-
let descriptor = selected.descriptor();
|
|
1046
|
-
let adapter = launch_adapter(&backend.binding, &prepared.request.identity, &prepared.role.provider, &prepared.discovered)?;
|
|
1047
|
-
let hooks = adapter.hooks();
|
|
1048
|
-
// Revalidate the exact plan before publishing the generation snapshot.
|
|
1049
|
-
let resolved = resolve_launch(descriptor, &hooks, &prepared.request, Some(&prepared.discovered.catalog))?;
|
|
1050
|
-
let plan = adapter.plan(&resolved)?;
|
|
1051
|
-
validate_launch_plan(descriptor, &resolved, &plan)?;
|
|
1052
|
-
write_role(&backend.binding, &prepared.request.identity, &prepared.role)?;
|
|
1053
|
-
let shared_parent = parent.physical.as_ref().is_some_and(|target| target.shared_endpoint);
|
|
1054
|
-
let workspace_topology = if shared_parent {
|
|
1055
|
-
// Validate root topology before stopping. Capture again only after the
|
|
1056
|
-
// authorized stop, since its last pane may destroy the old session ID.
|
|
1057
|
-
let (before, _, _) = workspace_tmux(&backend.binding, &prepared.role.id, None)?;
|
|
1058
|
-
if parent.status != SeatStatus::Stopped {
|
|
1059
|
-
let target = parent.physical.as_ref().ok_or(BackendError::Metadata)?;
|
|
1060
|
-
if before.endpoint != target.endpoint || before.session.as_deref() != Some(target.session.as_str()) {
|
|
1061
|
-
return Err(ContractError::Mismatch("resume workspace session").into());
|
|
1062
|
-
}
|
|
1063
|
-
let stopped = stop(&backend.binding.workspace.path, &backend.binding.team, &prepared.role.id)?;
|
|
1064
|
-
if stopped.outcome != Outcome::Committed {
|
|
1065
|
-
return Err(BackendError::Shutdown { operation: stopped.id, phase: stopped.phase,
|
|
1066
|
-
outcome: stopped.outcome, failure: stopped.failure });
|
|
1067
|
-
}
|
|
1068
|
-
}
|
|
1069
|
-
Some(workspace_tmux(&backend.binding, &prepared.role.id, None)?.0)
|
|
1070
|
-
} else { None };
|
|
1071
|
-
let operation = OperationId::new(format!("resume-{}", prepared.request.identity.instance.as_str()))?;
|
|
1072
|
-
let mut io = ScopedMaterializer::new(descriptor, &resolved, &plan, operation.clone(), RealCommandRunner::default())?;
|
|
1073
|
-
let clock = RealClock::new();
|
|
1074
|
-
let mut physical = PhysicalRuntime::new(
|
|
1075
|
-
Registration {
|
|
1076
|
-
descriptor, hooks: &hooks, controls: selected.controls(),
|
|
1077
|
-
startup: selected.startup(), session_panel: selected.session_panel(),
|
|
1078
|
-
},
|
|
1079
|
-
settings(&backend.binding, selected), operation.clone(), &clock,
|
|
1080
|
-
)?;
|
|
1081
|
-
physical.evidence = policy_evidence(&prepared.role.provider, &prepared.request.native, backend.binding.candidate_sha256)?;
|
|
1082
|
-
physical.workspace_tmux = workspace_topology;
|
|
1083
|
-
let record = Lifecycle { store: &mut backend.store, host: &mut physical, io: &mut io }.startup(
|
|
1084
|
-
&Adapter { descriptor, hooks: &hooks, catalog: Some(&prepared.discovered.catalog) },
|
|
1085
|
-
&prepared.request,
|
|
1086
|
-
Routing {
|
|
1087
|
-
pane: "unpublished".into(),
|
|
1088
|
-
binding_key: format!("binding-{}", prepared.request.identity.instance.as_str()),
|
|
1089
|
-
server_key: selected.server_key_for(&prepared.request.identity)?,
|
|
1090
|
-
},
|
|
1091
|
-
operation,
|
|
1092
|
-
)?;
|
|
1093
|
-
Ok(record)
|
|
1094
|
-
}
|
|
1095
|
-
|
|
1096
|
-
fn role_path(binding: &Binding, identity: &InstanceIdentity) -> PathBuf {
|
|
1097
|
-
root_for(&binding.workspace.path, &binding.scope).join(format!(
|
|
1098
|
-
"role-{}-{}.json", identity.seat.as_str(), identity.instance.as_str(),
|
|
1099
|
-
))
|
|
1100
|
-
}
|
|
1101
|
-
pub(super) fn write_role(binding: &Binding, identity: &InstanceIdentity, role: &RoleConfig) -> Result<(), BackendError> {
|
|
1102
|
-
use std::os::unix::fs::OpenOptionsExt;
|
|
1103
|
-
if identity.scope != binding.scope || identity.seat != role.id {
|
|
1104
|
-
return Err(BackendError::Metadata);
|
|
1105
|
-
}
|
|
1106
|
-
// Each generation has an immutable source; an old stopped generation's
|
|
1107
|
-
// model/instructions never change when a new role is admitted.
|
|
1108
|
-
let snapshot = serde_json::to_vec(role).map_err(|_| BackendError::Metadata)?;
|
|
1109
|
-
let mut file = std::fs::OpenOptions::new().write(true).create_new(true)
|
|
1110
|
-
.mode(0o600).custom_flags(libc::O_NOFOLLOW | libc::O_CLOEXEC)
|
|
1111
|
-
.open(role_path(binding, identity))?;
|
|
1112
|
-
file.write_all(&snapshot)?;
|
|
1113
|
-
file.sync_all()?;
|
|
1114
|
-
Ok(())
|
|
1115
|
-
}
|
|
1116
|
-
pub(crate) fn read_role(binding: &Binding, identity: &InstanceIdentity) -> Result<RoleConfig, BackendError> {
|
|
1117
|
-
let mut path = role_path(binding, identity);
|
|
1118
|
-
// Existing generation-one deployments retain their immutable source.
|
|
1119
|
-
if identity.generation == Generation(1) {
|
|
1120
|
-
match std::fs::symlink_metadata(&path) {
|
|
1121
|
-
Err(error) if error.kind() == std::io::ErrorKind::NotFound => {
|
|
1122
|
-
path = root_for(&binding.workspace.path, &binding.scope)
|
|
1123
|
-
.join(format!("role-{}.json", identity.seat.as_str()));
|
|
1124
|
-
}
|
|
1125
|
-
Err(error) => return Err(error.into()),
|
|
1126
|
-
Ok(_) => {},
|
|
1127
|
-
}
|
|
1128
|
-
}
|
|
1129
|
-
let metadata = std::fs::symlink_metadata(&path)?;
|
|
1130
|
-
if metadata.file_type().is_symlink() || !metadata.is_file() {
|
|
1131
|
-
return Err(BackendError::Metadata);
|
|
1132
|
-
}
|
|
1133
|
-
let mut bytes = Vec::new();
|
|
1134
|
-
use std::os::unix::fs::{MetadataExt, OpenOptionsExt};
|
|
1135
|
-
let file = std::fs::OpenOptions::new().read(true)
|
|
1136
|
-
.custom_flags(libc::O_NOFOLLOW | libc::O_CLOEXEC).open(path)?;
|
|
1137
|
-
let opened = file.metadata()?;
|
|
1138
|
-
if metadata.dev() != opened.dev() || metadata.ino() != opened.ino() {
|
|
1139
|
-
return Err(BackendError::Metadata);
|
|
1140
|
-
}
|
|
1141
|
-
file.take(4 * 1024 * 1024 + 1).read_to_end(&mut bytes)?;
|
|
1142
|
-
if bytes.len() > 4 * 1024 * 1024 {
|
|
1143
|
-
return Err(BackendError::Metadata);
|
|
1144
|
-
}
|
|
1145
|
-
let role: RoleConfig = serde_json::from_slice(&bytes).map_err(|_| BackendError::Metadata)?;
|
|
1146
|
-
if role.id != identity.seat || identity.scope != binding.scope
|
|
1147
|
-
|| registry::descriptor(role.provider.as_str())
|
|
1148
|
-
.is_none_or(|descriptor| descriptor.identity.id != role.provider.as_str())
|
|
1149
|
-
|| digest(role.prompt.text.as_bytes()) != role.prompt.sha256
|
|
1150
|
-
{
|
|
1151
|
-
return Err(BackendError::Metadata);
|
|
1152
|
-
}
|
|
1153
|
-
Ok(role)
|
|
1154
|
-
}
|
|
1155
|
-
|
|
1156
|
-
pub(super) fn policy_evidence(
|
|
1157
|
-
provider: &ProviderId,
|
|
1158
|
-
native: &NativeIdentity,
|
|
1159
|
-
candidate_sha256: Digest,
|
|
1160
|
-
) -> Result<Vec<CapabilityEvidence>, ContractError> {
|
|
1161
|
-
let descriptor = registry::provider(provider.as_str())?.descriptor();
|
|
1162
|
-
let profiles = descriptor.input.profiles.require("native input profiles")?;
|
|
1163
|
-
let mut evidence = Vec::new();
|
|
1164
|
-
for profile in profiles.iter().filter(|profile| profile.matches_native(native)) {
|
|
1165
|
-
// Unknown submit recipes never produce Native evidence, even when the
|
|
1166
|
-
// provider has a readable catalog or a recognizable TUI surface.
|
|
1167
|
-
profile.policy.require("native submit policy")?;
|
|
1168
|
-
let binding_sha256 = digest(
|
|
1169
|
-
format!(
|
|
1170
|
-
"{}-runtime-policy-v1\n{native:?}\n{}\n{:?}\n{candidate_sha256:?}",
|
|
1171
|
-
descriptor.identity.id, profile.id, profile.policy_sha256,
|
|
1172
|
-
).as_bytes(),
|
|
1173
|
-
);
|
|
1174
|
-
evidence.extend(profile.operations.iter().map(|operation| CapabilityEvidence {
|
|
1175
|
-
provider: provider.clone(),
|
|
1176
|
-
native: native.clone(),
|
|
1177
|
-
profile_id: profile.id.into(),
|
|
1178
|
-
policy_sha256: profile.policy_sha256,
|
|
1179
|
-
channel: profile.channel,
|
|
1180
|
-
operation: *operation,
|
|
1181
|
-
kind: EvidenceKind::Native,
|
|
1182
|
-
candidate_sha256,
|
|
1183
|
-
evidence_sha256: binding_sha256,
|
|
1184
|
-
}));
|
|
1185
|
-
}
|
|
1186
|
-
Ok(evidence)
|
|
1187
|
-
}
|
|
1188
|
-
|
|
1189
|
-
struct NoNewIo;
|
|
1190
|
-
impl OwnedIo for NoNewIo {
|
|
1191
|
-
fn create_exclusive(
|
|
1192
|
-
&mut self,
|
|
1193
|
-
_: &OwnedResourceRequest,
|
|
1194
|
-
) -> Result<OwnedResourceReceipt, PartialFailure> {
|
|
1195
|
-
Err(PartialFailure {
|
|
1196
|
-
error: ContractError::Invalid("teardown cannot materialize"),
|
|
1197
|
-
receipt: MaterializeReceipt {
|
|
1198
|
-
resources: Vec::new(),
|
|
1199
|
-
},
|
|
1200
|
-
})
|
|
1201
|
-
}
|
|
1202
|
-
fn read_bound_session(
|
|
1203
|
-
&mut self,
|
|
1204
|
-
_: &ResumeBinding,
|
|
1205
|
-
_: ReadBounds,
|
|
1206
|
-
) -> Result<ReadOutput, ReadFailure> {
|
|
1207
|
-
Err(ReadFailure::Error(ContractError::Invalid(
|
|
1208
|
-
"teardown cannot read session backing",
|
|
1209
|
-
)))
|
|
1210
|
-
}
|
|
1211
|
-
fn validate_configuration(
|
|
1212
|
-
&mut self,
|
|
1213
|
-
_: &OwnedValidationRequest,
|
|
1214
|
-
) -> Result<ReadOutput, ReadFailure> {
|
|
1215
|
-
Err(ReadFailure::Error(ContractError::Invalid(
|
|
1216
|
-
"teardown cannot run a validator",
|
|
1217
|
-
)))
|
|
1218
|
-
}
|
|
1219
|
-
}
|
|
1220
|
-
|
|
1221
|
-
/// Retire a stopped seat's current identity. None: the store holds no seat
|
|
1222
|
-
/// for this ID (never started, or already retired).
|
|
1223
|
-
pub(crate) fn retire(workspace: &Path, team: &str, seat: &SeatId) -> Result<Option<SeatRecord>, BackendError> {
|
|
1224
|
-
let mut backend = Backend::open(workspace, team)?;
|
|
1225
|
-
let Some(current) = backend.store.seat(seat)? else {
|
|
1226
|
-
return Ok(None);
|
|
1227
|
-
};
|
|
1228
|
-
Ok(Some(backend.store.retire(¤t.identity)?))
|
|
1229
|
-
}
|
|
1230
|
-
|
|
1231
|
-
pub fn stop(workspace: &Path, team: &str, seat: &SeatId) -> Result<OperationRecord, BackendError> {
|
|
1232
|
-
let mut backend = Backend::open(workspace, team)?;
|
|
1233
|
-
let target = backend
|
|
1234
|
-
.store
|
|
1235
|
-
.seat(seat)?
|
|
1236
|
-
.ok_or(Error::Invalid("unknown native seat"))?;
|
|
1237
|
-
let nonce = backend.store.propose_identity(seat)?.instance;
|
|
1238
|
-
let operation = OperationId::new(format!("stop-{}", nonce.as_str()))?;
|
|
1239
|
-
let selected = registry::provider(target.provider.as_str())?;
|
|
1240
|
-
let adapter = adapter(&backend.binding, &target.identity, &target.provider)?;
|
|
1241
|
-
let hooks = adapter.hooks();
|
|
1242
|
-
let clock = RealClock::new();
|
|
1243
|
-
let mut physical = PhysicalRuntime::new(
|
|
1244
|
-
Registration {
|
|
1245
|
-
descriptor: selected.descriptor(),
|
|
1246
|
-
hooks: &hooks,
|
|
1247
|
-
controls: selected.controls(),
|
|
1248
|
-
startup: selected.startup(),
|
|
1249
|
-
session_panel: selected.session_panel(),
|
|
1250
|
-
},
|
|
1251
|
-
settings(&backend.binding, selected),
|
|
1252
|
-
operation.clone(),
|
|
1253
|
-
&clock,
|
|
1254
|
-
)?;
|
|
1255
|
-
physical.evidence = policy_evidence(&target.provider, &target.native, backend.binding.candidate_sha256)?;
|
|
1256
|
-
Ok(Lifecycle {
|
|
1257
|
-
store: &mut backend.store,
|
|
1258
|
-
host: &mut physical,
|
|
1259
|
-
io: &mut NoNewIo,
|
|
1260
|
-
}
|
|
1261
|
-
.teardown(&target.identity, operation)?)
|
|
1262
|
-
}
|
|
1263
|
-
|
|
1264
|
-
#[cfg(test)]
|
|
1265
|
-
mod tests {
|
|
1266
|
-
use super::*;
|
|
1267
|
-
|
|
1268
|
-
struct RoleFixture { root: PathBuf, binding: Binding, role: RoleConfig, identity: InstanceIdentity }
|
|
1269
|
-
impl RoleFixture {
|
|
1270
|
-
fn new() -> Self {
|
|
1271
|
-
use std::os::unix::fs::DirBuilderExt;
|
|
1272
|
-
let root = std::env::temp_dir().join(format!("contract-role-{}-{}", std::process::id(),
|
|
1273
|
-
std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).unwrap().as_nanos()));
|
|
1274
|
-
std::fs::DirBuilder::new().mode(0o700).create(&root).unwrap();
|
|
1275
|
-
let root = root.canonicalize().unwrap();
|
|
1276
|
-
let cwd = resolve_cwd(&root).unwrap();
|
|
1277
|
-
let scope = scope_for(&cwd, "test").unwrap();
|
|
1278
|
-
std::fs::create_dir_all(root_for(&cwd.path, &scope)).unwrap();
|
|
1279
|
-
let seat = SeatId::new("worker").unwrap();
|
|
1280
|
-
let identity = InstanceIdentity { scope: scope.clone(), seat: seat.clone(), instance: InstanceId::new("first").unwrap(), generation: Generation(1) };
|
|
1281
|
-
let binding = Binding { version: 1, workspace: cwd, team: "test".into(), scope,
|
|
1282
|
-
endpoint: "controlled".into(), candidate: "/controlled/candidate".into(), candidate_sha256: digest(b"candidate"),
|
|
1283
|
-
tmux: "/controlled/tmux".into(), transport_root: "/controlled/transport".into() };
|
|
1284
|
-
let role = RoleConfig { id: seat, provider: ProviderId::new("cursor").unwrap(), source: root.join("role.md"),
|
|
1285
|
-
role: "worker".into(), model: "first-model".into(), effort: None, bypass: false, paused: false,
|
|
1286
|
-
body: "bounded fixture".into(), prompt: super::super::prompt::Prompt {
|
|
1287
|
-
text: "bounded fixture".into(), sha256: digest(b"bounded fixture"), sources: vec![],
|
|
1288
|
-
} };
|
|
1289
|
-
Self { root, binding, role, identity }
|
|
1290
|
-
}
|
|
1291
|
-
|
|
1292
|
-
// Receipt-only fixtures never authorize a native action; the runtime
|
|
1293
|
-
// must separately revalidate candidate, role, socket and process birth.
|
|
1294
|
-
fn canonical(&mut self, status: SeatStatus, phase: Phase, outcome: Outcome, pending: Option<&str>) -> Backend {
|
|
1295
|
-
self.binding.transport_root = self.binding.workspace.path.join("native-store");
|
|
1296
|
-
self.binding.endpoint = endpoint(&self.binding.scope);
|
|
1297
|
-
let store = ContractStore::create(&self.binding.transport_root, self.binding.scope.clone(), &self.binding.endpoint).unwrap();
|
|
1298
|
-
let target = SeatRecord { identity: self.identity.clone(), provider: self.role.provider.clone(),
|
|
1299
|
-
native: NativeIdentity { version: "fixture".into(), harness: "fixture".into(), ui: "controlled".into(),
|
|
1300
|
-
platform: Platform::MacOs, executable_sha256: digest(b"fixture") },
|
|
1301
|
-
cwd: self.binding.workspace.clone(), endpoint: self.binding.endpoint.clone(), pane: "%1".into(),
|
|
1302
|
-
binding_key: "controlled".into(), profile: "fixture".into(), server_key: "fixture".into(),
|
|
1303
|
-
evidence_kind: EvidenceKind::Fixture, status, process: None, physical: None, session: None,
|
|
1304
|
-
resources: vec![], workspace_resources: vec![], bootstrap_used: false };
|
|
1305
|
-
let operation = OperationRecord { id: OperationId::new("controlled-start").unwrap(), kind: TransactionKind::Startup,
|
|
1306
|
-
target: target.clone(), parent: None, phase, pending: pending.map(str::to_owned), resources: vec![],
|
|
1307
|
-
effect: team_agent_contract::contract::delivery::DeliveryEffect::NoEffect, outcome, preserved: vec![], failure: None };
|
|
1308
|
-
let db = rusqlite::Connection::open(self.binding.transport_root.join("contract.db")).unwrap();
|
|
1309
|
-
db.execute("INSERT INTO contract_seats VALUES(?1,?2)", rusqlite::params![self.identity.seat.as_str(), serde_json::to_string(&target).unwrap()]).unwrap();
|
|
1310
|
-
db.execute("INSERT INTO contract_generations VALUES(?1,?2)", rusqlite::params![self.identity.seat.as_str(), self.identity.generation.0]).unwrap();
|
|
1311
|
-
db.execute("INSERT INTO contract_instances VALUES(?1,?2,?3)", rusqlite::params![self.identity.instance.as_str(), self.identity.seat.as_str(), self.identity.generation.0]).unwrap();
|
|
1312
|
-
db.execute("INSERT INTO contract_operations VALUES(?1,?2)", rusqlite::params![operation.id.as_str(), serde_json::to_string(&operation).unwrap()]).unwrap();
|
|
1313
|
-
if matches!(outcome, Outcome::Running | Outcome::NeedsRecovery) {
|
|
1314
|
-
db.execute("INSERT INTO contract_leases VALUES(?1,?2)", rusqlite::params![self.identity.seat.as_str(), operation.id.as_str()]).unwrap();
|
|
1315
|
-
}
|
|
1316
|
-
std::fs::write(root_for(&self.binding.workspace.path, &self.binding.scope).join("framework-binding.json"),
|
|
1317
|
-
serde_json::to_vec(&self.binding).unwrap()).unwrap();
|
|
1318
|
-
drop(db); drop(store);
|
|
1319
|
-
Backend::open_readonly(&self.binding.workspace.path, "test").unwrap()
|
|
1320
|
-
}
|
|
1321
|
-
}
|
|
1322
|
-
impl Drop for RoleFixture { fn drop(&mut self) { let _ = std::fs::remove_dir_all(&self.root); } }
|
|
1323
|
-
|
|
1324
|
-
fn assert_receipt_class(status: SeatStatus, phase: Phase, outcome: Outcome, pending: Option<&str>, expected: &str) {
|
|
1325
|
-
let mut fixture = RoleFixture::new();
|
|
1326
|
-
let backend = fixture.canonical(status, phase, outcome, pending);
|
|
1327
|
-
let before = serde_json::to_vec(&backend.store.seat(&fixture.identity.seat).unwrap()).unwrap();
|
|
1328
|
-
let receipt = backend.store.creation_receipt(&fixture.identity.seat, Some(&fixture.identity)).unwrap().unwrap();
|
|
1329
|
-
assert_eq!(receipt.phase, phase);
|
|
1330
|
-
assert_eq!(receipt.outcome, outcome);
|
|
1331
|
-
let result = existing_start(&backend, &fixture.identity.seat);
|
|
1332
|
-
assert_eq!(serde_json::to_vec(&backend.store.seat(&fixture.identity.seat).unwrap()).unwrap(), before);
|
|
1333
|
-
assert_eq!(backend.store.operation(&receipt.id).unwrap(), receipt);
|
|
1334
|
-
let classified = result.unwrap_or_else(|error| panic!("{expected} must not collapse to unconditional Conflict: {error}"));
|
|
1335
|
-
let matches = match classified {
|
|
1336
|
-
Some(ExistingStart::Committed(record)) => expected == "committed" && record.id == receipt.id,
|
|
1337
|
-
Some(ExistingStart::FinishReceipt(record)) => expected == "finish" && record.id == receipt.id,
|
|
1338
|
-
Some(ExistingStart::Pending(record)) => expected == "pending" && record.id == receipt.id,
|
|
1339
|
-
Some(ExistingStart::Resume(record)) => expected == "resume" && record.identity == fixture.identity,
|
|
1340
|
-
Some(ExistingStart::Blocked(record)) => expected == "blocked" && record.id == receipt.id,
|
|
1341
|
-
None => false,
|
|
1342
|
-
};
|
|
1343
|
-
assert!(matches, "wrong receipt class for {expected}");
|
|
1344
|
-
}
|
|
1345
|
-
|
|
1346
|
-
#[test]
|
|
1347
|
-
fn startup_receipt_red_committed_is_reused_not_conflicted() {
|
|
1348
|
-
assert_receipt_class(SeatStatus::Ready, Phase::F5Receipt, Outcome::Committed, None, "committed");
|
|
1349
|
-
}
|
|
1350
|
-
#[test]
|
|
1351
|
-
fn startup_receipt_red_f4_is_receipt_completion_not_fresh() {
|
|
1352
|
-
assert_receipt_class(SeatStatus::Starting, Phase::F4Commit, Outcome::Running, None, "finish");
|
|
1353
|
-
}
|
|
1354
|
-
#[test]
|
|
1355
|
-
fn startup_receipt_red_inflight_is_pending_not_replayed() {
|
|
1356
|
-
assert_receipt_class(SeatStatus::Starting, Phase::F3Spawn, Outcome::Running, Some("spawn"), "pending");
|
|
1357
|
-
}
|
|
1358
|
-
#[test]
|
|
1359
|
-
fn startup_receipt_red_stopped_is_resume_not_fresh() {
|
|
1360
|
-
assert_receipt_class(SeatStatus::Stopped, Phase::F5Receipt, Outcome::Committed, None, "resume");
|
|
1361
|
-
}
|
|
1362
|
-
#[test]
|
|
1363
|
-
fn startup_receipt_red_uncertain_is_blocked_with_original_receipt() {
|
|
1364
|
-
assert_receipt_class(SeatStatus::Starting, Phase::F3Spawn, Outcome::NeedsRecovery, Some("spawn"), "blocked");
|
|
1365
|
-
}
|
|
1366
|
-
|
|
1367
|
-
#[test]
|
|
1368
|
-
fn startup_receipt_projection_repair_refuses_foreign_or_guessed_identity() {
|
|
1369
|
-
let mut fixture = RoleFixture::new();
|
|
1370
|
-
fixture.binding.candidate = std::env::current_exe().unwrap().canonicalize().unwrap();
|
|
1371
|
-
fixture.binding.candidate_sha256 = fingerprint_file(&fixture.binding.candidate, 1024 * 1024 * 1024, Duration::from_secs(10)).unwrap();
|
|
1372
|
-
let backend = fixture.canonical(SeatStatus::Starting, Phase::F4Commit, Outcome::Running, None);
|
|
1373
|
-
let original = backend.store.creation_receipt(&fixture.identity.seat, Some(&fixture.identity)).unwrap().unwrap();
|
|
1374
|
-
let seed = serde_json::json!({"provider":"cursor","status":"unknown"});
|
|
1375
|
-
assert!(start_needs_reconciliation(&fixture.root, "test", &fixture.identity.seat, &seed).unwrap());
|
|
1376
|
-
let exact = serde_json::json!({"provider":"cursor","status":"unknown","contract_scope":fixture.identity.scope,
|
|
1377
|
-
"contract_instance":fixture.identity.instance,"generation":fixture.identity.generation});
|
|
1378
|
-
assert!(start_needs_reconciliation(&fixture.root, "test", &fixture.identity.seat, &exact).unwrap());
|
|
1379
|
-
for (key, value) in [("provider", serde_json::json!("grok_build")), ("contract_scope", serde_json::json!("foreign")),
|
|
1380
|
-
("contract_instance", serde_json::json!("another")), ("generation", serde_json::json!(2))] {
|
|
1381
|
-
let mut forged = exact.clone(); forged[key] = value;
|
|
1382
|
-
assert!(start_needs_reconciliation(&fixture.root, "test", &fixture.identity.seat, &forged).is_err(), "{key}");
|
|
1383
|
-
}
|
|
1384
|
-
assert_eq!(backend.store.operation(&original.id).unwrap(), original);
|
|
1385
|
-
assert!(preflight_start_role(&fixture.root, "test", &fixture.role).is_err(), "classification is not Native admission");
|
|
1386
|
-
}
|
|
1387
|
-
|
|
1388
|
-
#[test]
|
|
1389
|
-
fn startup_receipt_fixture_cannot_authorize_reuse_or_native_input() {
|
|
1390
|
-
let mut fixture = RoleFixture::new();
|
|
1391
|
-
let backend = fixture.canonical(SeatStatus::Ready, Phase::F5Receipt, Outcome::Committed, None);
|
|
1392
|
-
let record = backend.store.creation_receipt(&fixture.identity.seat, Some(&fixture.identity)).unwrap().unwrap();
|
|
1393
|
-
assert!(validate_existing(&backend, &fixture.role, &record).is_err());
|
|
1394
|
-
assert_eq!(backend.store.operation(&record.id).unwrap(), record);
|
|
1395
|
-
}
|
|
1396
|
-
|
|
1397
|
-
#[test]
|
|
1398
|
-
fn startup_receipt_unfinished_parent_wins_over_old_committed_generation() {
|
|
1399
|
-
let mut fixture = RoleFixture::new();
|
|
1400
|
-
let backend = fixture.canonical(SeatStatus::Ready, Phase::F5Receipt, Outcome::Committed, None);
|
|
1401
|
-
let mut interrupted = backend.store.creation_receipt(&fixture.identity.seat, Some(&fixture.identity)).unwrap().unwrap();
|
|
1402
|
-
interrupted.parent = Some(interrupted.target.clone());
|
|
1403
|
-
interrupted.id = OperationId::new("uncertain-restart").unwrap();
|
|
1404
|
-
interrupted.kind = TransactionKind::Restart;
|
|
1405
|
-
interrupted.target.identity.instance = InstanceId::new("second").unwrap();
|
|
1406
|
-
interrupted.target.identity.generation = Generation(2);
|
|
1407
|
-
interrupted.phase = Phase::F3Spawn;
|
|
1408
|
-
interrupted.pending = Some("spawn".into());
|
|
1409
|
-
interrupted.outcome = Outcome::NeedsRecovery;
|
|
1410
|
-
let db = rusqlite::Connection::open(fixture.binding.transport_root.join("contract.db")).unwrap();
|
|
1411
|
-
db.execute("INSERT INTO contract_operations VALUES(?1,?2)", rusqlite::params![interrupted.id.as_str(), serde_json::to_string(&interrupted).unwrap()]).unwrap();
|
|
1412
|
-
assert!(matches!(existing_start(&backend, &fixture.identity.seat).unwrap(),
|
|
1413
|
-
Some(ExistingStart::Blocked(record)) if record == interrupted));
|
|
1414
|
-
assert_eq!(backend.store.seat(&fixture.identity.seat).unwrap().unwrap().identity, fixture.identity);
|
|
1415
|
-
}
|
|
1416
|
-
|
|
1417
|
-
#[test]
|
|
1418
|
-
fn startup_receipt_missing_committed_seat_refuses_but_compensated_allows_fresh_proposal() {
|
|
1419
|
-
for outcome in [Outcome::Committed, Outcome::Compensated] {
|
|
1420
|
-
let mut fixture = RoleFixture::new();
|
|
1421
|
-
let backend = fixture.canonical(SeatStatus::Stopped, Phase::F5Receipt, outcome, None);
|
|
1422
|
-
let db = rusqlite::Connection::open(fixture.binding.transport_root.join("contract.db")).unwrap();
|
|
1423
|
-
db.execute("DELETE FROM contract_seats", []).unwrap();
|
|
1424
|
-
let result = existing_start(&backend, &fixture.identity.seat);
|
|
1425
|
-
if outcome == Outcome::Committed { assert!(result.is_err()); }
|
|
1426
|
-
else { assert!(result.unwrap().is_none()); }
|
|
1427
|
-
assert_eq!(backend.store.propose_identity(&fixture.identity.seat).unwrap().generation, Generation(2));
|
|
1428
|
-
}
|
|
1429
|
-
}
|
|
1430
|
-
|
|
1431
|
-
#[test]
|
|
1432
|
-
fn retired_identity_lets_its_seat_id_start_fresh_and_a_live_seat_never_retires() {
|
|
1433
|
-
{
|
|
1434
|
-
let mut fixture = RoleFixture::new();
|
|
1435
|
-
let backend = fixture.canonical(SeatStatus::Ready, Phase::F5Receipt, Outcome::Committed, None);
|
|
1436
|
-
let mut writer = ContractStore::open(&fixture.binding.transport_root,
|
|
1437
|
-
fixture.binding.scope.clone(), &fixture.binding.endpoint).unwrap();
|
|
1438
|
-
assert!(writer.retire(&fixture.identity).is_err());
|
|
1439
|
-
assert!(matches!(existing_start(&backend, &fixture.identity.seat).unwrap(),
|
|
1440
|
-
Some(ExistingStart::Committed(_))));
|
|
1441
|
-
}
|
|
1442
|
-
let mut fixture = RoleFixture::new();
|
|
1443
|
-
let backend = fixture.canonical(SeatStatus::Stopped, Phase::F5Receipt, Outcome::Committed, None);
|
|
1444
|
-
assert!(matches!(existing_start(&backend, &fixture.identity.seat).unwrap(),
|
|
1445
|
-
Some(ExistingStart::Resume(_))));
|
|
1446
|
-
let mut writer = ContractStore::open(&fixture.binding.transport_root,
|
|
1447
|
-
fixture.binding.scope.clone(), &fixture.binding.endpoint).unwrap();
|
|
1448
|
-
assert_eq!(writer.retire(&fixture.identity).unwrap().identity, fixture.identity);
|
|
1449
|
-
// Never a resume of the retired session, and never the missing-seat fence.
|
|
1450
|
-
assert!(existing_start(&backend, &fixture.identity.seat).unwrap().is_none());
|
|
1451
|
-
assert!(seat_unclaimed(&backend, &fixture.identity.seat).unwrap());
|
|
1452
|
-
assert_eq!(backend.store.propose_identity(&fixture.identity.seat).unwrap().generation, Generation(2));
|
|
1453
|
-
}
|
|
1454
|
-
|
|
1455
|
-
#[test]
|
|
1456
|
-
fn startup_receipt_lookup_refuses_cross_scope_and_generation_fallback() {
|
|
1457
|
-
let mut fixture = RoleFixture::new();
|
|
1458
|
-
let backend = fixture.canonical(SeatStatus::Ready, Phase::F5Receipt, Outcome::Committed, None);
|
|
1459
|
-
let mut wrong = fixture.identity.clone(); wrong.generation = Generation(2);
|
|
1460
|
-
assert!(backend.store.creation_receipt(&fixture.identity.seat, Some(&wrong)).is_err());
|
|
1461
|
-
wrong = fixture.identity.clone(); wrong.scope = ScopeId::new("foreign").unwrap();
|
|
1462
|
-
assert!(backend.store.creation_receipt(&fixture.identity.seat, Some(&wrong)).is_err());
|
|
1463
|
-
assert!(backend.store.creation_receipt(&SeatId::new("absent").unwrap(), None).unwrap().is_none());
|
|
1464
|
-
}
|
|
1465
|
-
|
|
1466
|
-
#[test]
|
|
1467
|
-
fn restart_skips_stopped_mirror_and_stopped_native_store_before_resume_discovery() {
|
|
1468
|
-
for provider in ["cursor", "grok_build"] {
|
|
1469
|
-
let mut fixture = RoleFixture::new();
|
|
1470
|
-
let directory = fixture.root.join("roles");
|
|
1471
|
-
std::fs::create_dir_all(directory.join("agents")).unwrap();
|
|
1472
|
-
std::fs::write(directory.join("TEAM.md"), "---\nname: test\n---\nFixture\n").unwrap();
|
|
1473
|
-
std::fs::write(directory.join("agents/worker.md"),
|
|
1474
|
-
format!("---\nprovider: {provider}\nmodel: unavailable-fixture-model\n---\nFixture\n")).unwrap();
|
|
1475
|
-
let stopped = serde_json::json!({"agents":{"worker":{"provider":provider,"status":"stopped","session_id":"preserved","generation":1}}});
|
|
1476
|
-
// No framework binding/store/native installation exists yet. A
|
|
1477
|
-
// stopped mirror must not even attempt to open the native store.
|
|
1478
|
-
assert!(prepare_restart(&fixture.root, "test", &directory, &stopped).unwrap().is_empty());
|
|
1479
|
-
|
|
1480
|
-
fixture.binding.transport_root = fixture.binding.workspace.path.join("native-store");
|
|
1481
|
-
fixture.binding.endpoint = endpoint(&fixture.binding.scope);
|
|
1482
|
-
let store = ContractStore::create(&fixture.binding.transport_root, fixture.binding.scope.clone(), &fixture.binding.endpoint).unwrap();
|
|
1483
|
-
let native = NativeIdentity { version: "fixture".into(), harness: "unavailable".into(), ui: "tui".into(),
|
|
1484
|
-
platform: Platform::MacOs, executable_sha256: digest(b"fixture") };
|
|
1485
|
-
let seat = SeatRecord {
|
|
1486
|
-
identity: fixture.identity.clone(), provider: ProviderId::new(provider).unwrap(), native,
|
|
1487
|
-
cwd: fixture.binding.workspace.clone(), endpoint: fixture.binding.endpoint.clone(),
|
|
1488
|
-
pane: "unpublished".into(), binding_key: "fixture".into(), profile: "unavailable".into(), server_key: "fixture".into(),
|
|
1489
|
-
evidence_kind: EvidenceKind::Native, status: SeatStatus::Stopped, process: None, physical: None,
|
|
1490
|
-
session: None, resources: vec![], workspace_resources: vec![], bootstrap_used: false,
|
|
1491
|
-
};
|
|
1492
|
-
// Test-only canonical fixture, never a live-store recovery shortcut.
|
|
1493
|
-
let db = rusqlite::Connection::open(fixture.binding.transport_root.join("contract.db")).unwrap();
|
|
1494
|
-
db.execute("INSERT INTO contract_seats(seat,record) VALUES(?1,?2)",
|
|
1495
|
-
rusqlite::params![fixture.identity.seat.as_str(), serde_json::to_string(&seat).unwrap()]).unwrap();
|
|
1496
|
-
db.execute("INSERT INTO contract_generations(seat,generation) VALUES(?1,1)", [fixture.identity.seat.as_str()]).unwrap();
|
|
1497
|
-
drop(db);
|
|
1498
|
-
std::fs::write(root_for(&fixture.binding.workspace.path, &fixture.binding.scope).join("framework-binding.json"),
|
|
1499
|
-
serde_json::to_vec(&fixture.binding).unwrap()).unwrap();
|
|
1500
|
-
let stale_running = serde_json::json!({"agents":{"worker":{"provider":provider,"status":"running"}}});
|
|
1501
|
-
assert!(prepare_restart(&fixture.root, "test", &directory, &stale_running).unwrap().is_empty());
|
|
1502
|
-
assert_eq!(store.seat(&fixture.identity.seat).unwrap(), Some(seat));
|
|
1503
|
-
assert_eq!(store.propose_identity(&fixture.identity.seat).unwrap().generation, Generation(2));
|
|
1504
|
-
}
|
|
1505
|
-
}
|
|
1506
|
-
|
|
1507
|
-
#[test]
|
|
1508
|
-
fn explicit_discard_can_refresh_only_a_quiescent_same_path_candidate() {
|
|
1509
|
-
for status in [SeatStatus::Ready, SeatStatus::Stopped] {
|
|
1510
|
-
let mut fixture = RoleFixture::new();
|
|
1511
|
-
fixture.binding.candidate = std::env::current_exe().unwrap().canonicalize().unwrap();
|
|
1512
|
-
fixture.binding.candidate_sha256 = Digest([0; 32]);
|
|
1513
|
-
let _backend = fixture.canonical(status, Phase::F5Receipt, Outcome::Committed, None);
|
|
1514
|
-
let path = root_for(&fixture.root, &fixture.binding.scope).join("framework-binding.json");
|
|
1515
|
-
let before = std::fs::read(&path).unwrap();
|
|
1516
|
-
assert!(require_candidate(&fixture.binding).is_err());
|
|
1517
|
-
let result = refresh_quiescent_candidate(&fixture.root, "test", &fixture.identity.seat);
|
|
1518
|
-
if status == SeatStatus::Ready {
|
|
1519
|
-
assert!(result.is_err());
|
|
1520
|
-
assert_eq!(std::fs::read(&path).unwrap(), before);
|
|
1521
|
-
} else {
|
|
1522
|
-
result.unwrap();
|
|
1523
|
-
require_candidate(&Backend::open_readonly(&fixture.root, "test").unwrap().binding).unwrap();
|
|
1524
|
-
fixture.binding.candidate = fixture.root.join("foreign-candidate");
|
|
1525
|
-
std::fs::write(&path, serde_json::to_vec(&fixture.binding).unwrap()).unwrap();
|
|
1526
|
-
assert!(refresh_quiescent_candidate(&fixture.root, "test", &fixture.identity.seat).is_err());
|
|
1527
|
-
}
|
|
1528
|
-
}
|
|
1529
|
-
}
|
|
1530
|
-
|
|
1531
|
-
#[test]
|
|
1532
|
-
fn native_business_tools_use_independent_inbox_without_submitted_tasks() {
|
|
1533
|
-
use team_agent_contract::orchestration::mcp::CallContext;
|
|
1534
|
-
let mut fixture = RoleFixture::new();
|
|
1535
|
-
fixture.role.provider = ProviderId::new("grok_build").unwrap();
|
|
1536
|
-
let _backend = fixture.canonical(SeatStatus::Ready, Phase::F5Receipt, Outcome::Committed, None);
|
|
1537
|
-
let mut backend = Backend::open(&fixture.root, "test").unwrap();
|
|
1538
|
-
let state = serde_json::json!({"active_team_key":"test", "teams":{"test":{
|
|
1539
|
-
"team_key":"test", "team_dir":fixture.root, "agents":{"worker":{"provider":"grok","status":"running"}}
|
|
1540
|
-
}}});
|
|
1541
|
-
let state_file = crate::state::persist::runtime_state_path(&fixture.root);
|
|
1542
|
-
std::fs::write(state_file, serde_json::to_vec(&state).unwrap()).unwrap();
|
|
1543
|
-
let tools = crate::mcp_server::TeamOrchestratorTools::with_identity(
|
|
1544
|
-
&fixture.root, Some(crate::model::ids::AgentId::new("worker")),
|
|
1545
|
-
Some(crate::model::ids::TeamKey::new("test")));
|
|
1546
|
-
let context = CallContext { identity: fixture.identity.clone(), binding_key:"controlled".into(),
|
|
1547
|
-
connection_id:InstanceId::new("direct-callback").unwrap(), task_id:String::new() };
|
|
1548
|
-
assert!(backend.store.submitted_task(&fixture.identity).unwrap().is_none());
|
|
1549
|
-
for (id, name, arguments) in [
|
|
1550
|
-
(1, "send_message", serde_json::json!({"to":"leader","content":"unbound native callback","mailbox":true})),
|
|
1551
|
-
(2, "get_team_status", serde_json::json!({})),
|
|
1552
|
-
(3, "report_result", serde_json::json!({"summary":"independent native result"})),
|
|
1553
|
-
] {
|
|
1554
|
-
let request = serde_json::json!({"jsonrpc":"2.0","id":id,"method":"tools/call","params":{"name":name,"arguments":arguments}});
|
|
1555
|
-
let result = super::super::mcp::direct_request(&mut backend.store, &context, &request, &tools).unwrap().unwrap();
|
|
1556
|
-
assert_eq!(result["result"]["isError"], false, "{result}");
|
|
1557
|
-
}
|
|
1558
|
-
let inbox = crate::inbox_store::read(&fixture.root, "leader", 10, Some("test")).unwrap();
|
|
1559
|
-
assert_eq!(inbox["messages"].as_array().unwrap().len(), 2);
|
|
1560
|
-
let db = rusqlite::Connection::open(fixture.binding.transport_root.join("contract.db")).unwrap();
|
|
1561
|
-
let outbox: i64 = db.query_row("select count(*) from contract_outbox", [], |row| row.get(0)).unwrap();
|
|
1562
|
-
assert_eq!(outbox, 0, "no synthetic task or message-delivery ledger row");
|
|
1563
|
-
let mut foreign = context.clone(); foreign.identity.generation = Generation(2);
|
|
1564
|
-
let request = serde_json::json!({"jsonrpc":"2.0","id":4,"method":"tools/call","params":{"name":"send_message","arguments":{"to":"leader","content":"must not arrive","mailbox":true}}});
|
|
1565
|
-
assert!(super::super::mcp::direct_request(&mut backend.store, &foreign, &request, &tools).is_err());
|
|
1566
|
-
}
|
|
1567
|
-
|
|
1568
|
-
#[test]
|
|
1569
|
-
fn grok_projection_exposes_public_provider_and_keeps_internal_contract_id() {
|
|
1570
|
-
let mut fixture = RoleFixture::new();
|
|
1571
|
-
fixture.role.provider = ProviderId::new("grok_build").unwrap();
|
|
1572
|
-
let _backend = fixture.canonical(SeatStatus::Ready, Phase::F5Receipt, Outcome::Committed, None);
|
|
1573
|
-
write_role(&fixture.binding, &fixture.identity, &fixture.role).unwrap();
|
|
1574
|
-
let projected = super::super::framework::project_seat(&fixture.root, "test", fixture.identity.seat.as_str()).unwrap();
|
|
1575
|
-
assert_eq!(projected["provider"], "grok");
|
|
1576
|
-
assert_eq!(projected["contract_provider"], "grok_build");
|
|
1577
|
-
}
|
|
1578
|
-
|
|
1579
|
-
#[test]
|
|
1580
|
-
fn native_snapshot_publishes_the_private_pane_endpoint_pair() {
|
|
1581
|
-
let mut fixture = RoleFixture::new();
|
|
1582
|
-
fixture.role.provider = ProviderId::new("pi_tui").unwrap();
|
|
1583
|
-
let _backend = fixture.canonical(SeatStatus::Ready, Phase::F5Receipt, Outcome::Committed, None);
|
|
1584
|
-
let db = rusqlite::Connection::open(fixture.binding.transport_root.join("contract.db")).unwrap();
|
|
1585
|
-
db.execute("update contract_operations set record=json_set(record, '$.failure', ?1)",
|
|
1586
|
-
["session observation deferred: capture-failed; tmux capture: Deadline"]).unwrap();
|
|
1587
|
-
write_role(&fixture.binding, &fixture.identity, &fixture.role).unwrap();
|
|
1588
|
-
let state = super::super::framework::project_seat(
|
|
1589
|
-
&fixture.binding.workspace.path, "test", "worker",
|
|
1590
|
-
).unwrap();
|
|
1591
|
-
assert_eq!(state["pane_id"], "%1");
|
|
1592
|
-
assert_eq!(state["pane_id"], state["native_pane"]);
|
|
1593
|
-
assert_eq!(state["tmux_endpoint"], fixture.binding.endpoint);
|
|
1594
|
-
assert_eq!(state["tmux_endpoint"], state["native_endpoint"]);
|
|
1595
|
-
assert!(state["session_name"].is_null());
|
|
1596
|
-
assert!(state["session_id"].is_null());
|
|
1597
|
-
assert_eq!(state["session_observation"], "unobserved");
|
|
1598
|
-
assert_eq!(state["session_warning"], "session observation deferred: capture-failed; tmux capture: Deadline");
|
|
1599
|
-
assert_eq!(state["mcp_binding"], "unverified");
|
|
1600
|
-
}
|
|
1601
|
-
|
|
1602
|
-
#[test]
|
|
1603
|
-
fn role_snapshots_are_instance_scoped_and_exclusive() {
|
|
1604
|
-
let fixture = RoleFixture::new();
|
|
1605
|
-
write_role(&fixture.binding, &fixture.identity, &fixture.role).unwrap();
|
|
1606
|
-
let mut second = fixture.identity.clone();
|
|
1607
|
-
second.instance = InstanceId::new("second").unwrap();
|
|
1608
|
-
second.generation = Generation(2);
|
|
1609
|
-
let mut role = fixture.role.clone();
|
|
1610
|
-
role.model = "second-model".into();
|
|
1611
|
-
write_role(&fixture.binding, &second, &role).unwrap();
|
|
1612
|
-
assert_eq!(read_role(&fixture.binding, &fixture.identity).unwrap().model, "first-model");
|
|
1613
|
-
assert_eq!(read_role(&fixture.binding, &second).unwrap().model, "second-model");
|
|
1614
|
-
assert!(write_role(&fixture.binding, &fixture.identity, &role).is_err());
|
|
1615
|
-
assert_eq!(read_role(&fixture.binding, &fixture.identity).unwrap().model, "first-model");
|
|
1616
|
-
}
|
|
1617
|
-
|
|
1618
|
-
#[test]
|
|
1619
|
-
fn legacy_role_fallback_is_only_for_missing_generation_one_not_symlinks() {
|
|
1620
|
-
let fixture = RoleFixture::new();
|
|
1621
|
-
let old = root_for(&fixture.binding.workspace.path, &fixture.binding.scope).join("role-worker.json");
|
|
1622
|
-
std::fs::write(&old, serde_json::to_vec(&fixture.role).unwrap()).unwrap();
|
|
1623
|
-
assert_eq!(read_role(&fixture.binding, &fixture.identity).unwrap().model, "first-model");
|
|
1624
|
-
let mut later = fixture.identity.clone();
|
|
1625
|
-
later.generation = Generation(2);
|
|
1626
|
-
assert!(read_role(&fixture.binding, &later).is_err());
|
|
1627
|
-
std::os::unix::fs::symlink(fixture.root.join("missing"), role_path(&fixture.binding, &fixture.identity)).unwrap();
|
|
1628
|
-
assert!(matches!(read_role(&fixture.binding, &fixture.identity), Err(BackendError::Metadata)));
|
|
1629
|
-
}
|
|
1630
|
-
|
|
1631
|
-
#[test]
|
|
1632
|
-
fn mismatched_role_owner_cannot_write_a_snapshot() {
|
|
1633
|
-
let fixture = RoleFixture::new();
|
|
1634
|
-
let mut identity = fixture.identity.clone();
|
|
1635
|
-
identity.seat = SeatId::new("other").unwrap();
|
|
1636
|
-
assert!(matches!(write_role(&fixture.binding, &identity, &fixture.role), Err(BackendError::Metadata)));
|
|
1637
|
-
assert!(!role_path(&fixture.binding, &identity).exists());
|
|
1638
|
-
}
|
|
1639
|
-
|
|
1640
|
-
#[test]
|
|
1641
|
-
fn shutdown_error_exposes_the_durable_operation_failure_not_metadata() {
|
|
1642
|
-
let failure = "native instance not alive or replaced: Unknown; stage=check-bound; expected_pid=44123; process_state=Some(Replaced)";
|
|
1643
|
-
let error = BackendError::Shutdown {
|
|
1644
|
-
operation: OperationId::new("stop-instance-test").unwrap(),
|
|
1645
|
-
phase: Phase::F0Preflight,
|
|
1646
|
-
outcome: Outcome::NeedsRecovery,
|
|
1647
|
-
failure: Some(failure.into()),
|
|
1648
|
-
};
|
|
1649
|
-
let shown = error.to_string();
|
|
1650
|
-
for expected in ["stop-instance-test", "F0Preflight", "NeedsRecovery", failure] {
|
|
1651
|
-
assert!(shown.contains(expected), "{shown}");
|
|
1652
|
-
}
|
|
1653
|
-
assert!(!shown.contains("metadata is invalid"));
|
|
1654
|
-
}
|
|
1655
|
-
|
|
1656
|
-
#[test]
|
|
1657
|
-
fn cursor_metadata_never_mints_runtime_policy_evidence() {
|
|
1658
|
-
let provider = ProviderId::new("cursor").unwrap();
|
|
1659
|
-
let mut native = NativeIdentity {
|
|
1660
|
-
version: "2026.09.29-a1b2c3".into(),
|
|
1661
|
-
harness: "cursor-launcher".into(),
|
|
1662
|
-
ui: "metadata".into(),
|
|
1663
|
-
platform: Platform::MacOs,
|
|
1664
|
-
executable_sha256: digest(b"launcher only"),
|
|
1665
|
-
};
|
|
1666
|
-
let candidate = digest(b"candidate");
|
|
1667
|
-
assert!(policy_evidence(&provider, &native, candidate).unwrap().is_empty());
|
|
1668
|
-
native.harness = "cursor-agent".into();
|
|
1669
|
-
native.ui = "tui".into();
|
|
1670
|
-
// Admission supplies a separately image-fenced runtime identity. The
|
|
1671
|
-
// recipe still cannot be borrowed from metadata or another provider.
|
|
1672
|
-
assert!(!policy_evidence(&provider, &native, candidate).unwrap().is_empty());
|
|
1673
|
-
// Even a Kiro-looking image cannot reuse its evidence under Cursor.
|
|
1674
|
-
native.harness = "v2".into();
|
|
1675
|
-
native.version = "2.29.0".into();
|
|
1676
|
-
assert!(policy_evidence(&provider, &native, candidate).unwrap().is_empty());
|
|
1677
|
-
}
|
|
1678
|
-
|
|
1679
|
-
#[test]
|
|
1680
|
-
fn cursor_request_refuses_metadata_launch_and_wrong_provider_catalog() {
|
|
1681
|
-
let provider = ProviderId::new("cursor").unwrap();
|
|
1682
|
-
let scope = ScopeId::new("controlled").unwrap();
|
|
1683
|
-
let seat = SeatId::new("worker").unwrap();
|
|
1684
|
-
let binding = Binding {
|
|
1685
|
-
version: 1,
|
|
1686
|
-
workspace: CwdIdentity { path: "/controlled".into(), identity: digest(b"cwd") },
|
|
1687
|
-
team: "demo".into(), scope: scope.clone(), endpoint: "/controlled/socket".into(),
|
|
1688
|
-
candidate: "/controlled/candidate".into(), candidate_sha256: digest(b"candidate"),
|
|
1689
|
-
tmux: "/controlled/tmux".into(), transport_root: "/controlled/runtime".into(),
|
|
1690
|
-
};
|
|
1691
|
-
let role = RoleConfig {
|
|
1692
|
-
id: seat.clone(), provider: provider.clone(), source: "/controlled/role.md".into(),
|
|
1693
|
-
role: "worker".into(), model: "gpt-5.6-luna-xhigh".into(), effort: None,
|
|
1694
|
-
bypass: true, paused: false, body: "role".into(),
|
|
1695
|
-
prompt: super::super::prompt::Prompt { text: "prompt".into(), sha256: digest(b"prompt"), sources: vec![] },
|
|
1696
|
-
};
|
|
1697
|
-
let identity = InstanceIdentity { scope, seat, instance: InstanceId::new("preflight").unwrap(), generation: Generation(1) };
|
|
1698
|
-
let native = NativeIdentity {
|
|
1699
|
-
version: "2026.09.29-a1b2c3".into(), harness: "cursor-launcher".into(),
|
|
1700
|
-
ui: "metadata".into(), platform: Platform::MacOs, executable_sha256: digest(b"launcher"),
|
|
1701
|
-
};
|
|
1702
|
-
let mut discovery = Discovery {
|
|
1703
|
-
command: team_agent_contract::host::command::PublicCommand {
|
|
1704
|
-
name: "cursor-agent".into(), search_path: "/controlled".into(), entry: "/controlled/cursor-agent".into(),
|
|
1705
|
-
},
|
|
1706
|
-
runtime: None,
|
|
1707
|
-
engine: "/controlled/cursor-agent".into(), native: native.clone(),
|
|
1708
|
-
catalog: CatalogObservation { provider, native,
|
|
1709
|
-
schema: team_agent_contract::providers::cursor::native::CATALOG_SOURCE.schema.into(), models: vec![] },
|
|
1710
|
-
};
|
|
1711
|
-
assert!(matches!(request(&binding, &role, identity.clone(), &discovery),
|
|
1712
|
-
Err(BackendError::Contract(ContractError::ProfileUnavailable))));
|
|
1713
|
-
discovery.catalog.provider = ProviderId::new("kiro").unwrap();
|
|
1714
|
-
assert!(matches!(request(&binding, &role, identity, &discovery),
|
|
1715
|
-
Err(BackendError::Contract(ContractError::Mismatch("role native catalog")))));
|
|
1716
|
-
}
|
|
1717
|
-
|
|
1718
|
-
#[test]
|
|
1719
|
-
fn policy_bindings_track_runtime_native_identity_not_a_release_receipt() {
|
|
1720
|
-
let mut native = NativeIdentity {
|
|
1721
|
-
version: "2.28.0".into(),
|
|
1722
|
-
harness: "v2".into(),
|
|
1723
|
-
ui: "tui".into(),
|
|
1724
|
-
platform: Platform::MacOs,
|
|
1725
|
-
executable_sha256: digest(b"old image"),
|
|
1726
|
-
};
|
|
1727
|
-
let candidate = digest(b"candidate");
|
|
1728
|
-
let provider = ProviderId::new("kiro").unwrap();
|
|
1729
|
-
let old = policy_evidence(&provider, &native, candidate).unwrap();
|
|
1730
|
-
assert!(!old.is_empty());
|
|
1731
|
-
native.version = "2.29.0".into();
|
|
1732
|
-
native.executable_sha256 = digest(b"updated image");
|
|
1733
|
-
let new = policy_evidence(&provider, &native, candidate).unwrap();
|
|
1734
|
-
assert_eq!(new.len(), old.len());
|
|
1735
|
-
for (old, new) in old.iter().zip(&new) {
|
|
1736
|
-
assert_eq!(new.native, native);
|
|
1737
|
-
assert_eq!(new.candidate_sha256, candidate);
|
|
1738
|
-
assert_eq!(new.policy_sha256, old.policy_sha256);
|
|
1739
|
-
assert_ne!(new.evidence_sha256, old.evidence_sha256);
|
|
1740
|
-
}
|
|
1741
|
-
native.harness = "v3".into();
|
|
1742
|
-
assert!(policy_evidence(&provider, &native, candidate).unwrap().is_empty());
|
|
1743
|
-
}
|
|
1744
|
-
}
|