@team-agent/installer 0.4.10 → 0.4.11

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/Cargo.lock CHANGED
@@ -566,7 +566,7 @@ dependencies = [
566
566
 
567
567
  [[package]]
568
568
  name = "team-agent"
569
- version = "0.4.10"
569
+ version = "0.4.11"
570
570
  dependencies = [
571
571
  "anyhow",
572
572
  "chrono",
package/Cargo.toml CHANGED
@@ -9,7 +9,7 @@ members = ["crates/team-agent"]
9
9
 
10
10
  [workspace.package]
11
11
  edition = "2021"
12
- version = "0.4.10"
12
+ version = "0.4.11"
13
13
  license = "AGPL-3.0"
14
14
  rust-version = "1.95"
15
15
 
@@ -61,12 +61,20 @@ pub fn leader_start_plan(
61
61
  // `managed_team_session_name(identity) = team-<team_id>` which is the
62
62
  // worker session — that co-location is the structural root of
63
63
  // E49/E51/E53/E57-3/E60.
64
+ // 0.4.10+ mirror-session fix v2 (option B): managed-mode session name
65
+ // gets a per-launch nonce so each `team-agent <provider>` entry in the
66
+ // same workspace creates its OWN tmux session — matching the user
67
+ // expectation that `tmux new-session + claude` is independent every
68
+ // time. Pre-fix the managed path used the workspace-keyed name (same
69
+ // session for every entry) and silently attached the second client →
70
+ // UI mirror. The external/attach paths keep the stable workspace-keyed
71
+ // name (per-launch nonce would break `--attach-session` reattach).
64
72
  let session_name = if external_path {
65
73
  attach_session
66
74
  .cloned()
67
75
  .or_else(|| Some(leader_session_name(provider, workspace)))
68
76
  } else {
69
- Some(leader_session_name(provider, workspace))
77
+ Some(managed_leader_session_name(provider, workspace))
70
78
  };
71
79
  let in_tmux = std::env::var_os("TMUX").is_some();
72
80
  if !in_tmux {
@@ -266,6 +274,42 @@ pub fn leader_session_name(provider: Provider, workspace: &Path) -> SessionName
266
274
  ))
267
275
  }
268
276
 
277
+ /// 0.4.10+ mirror-session fix v2: managed-mode session name with a
278
+ /// per-launch nonce.
279
+ ///
280
+ /// Format: `team-agent-leader-<provider>-<folder>-<hash>-<nonce>`.
281
+ /// `nonce` = `<pid_hex>-<epoch_nanos_hex>`. The pid distinguishes
282
+ /// concurrent launches; the epoch_nanos distinguishes sequential ones.
283
+ ///
284
+ /// Each `team-agent <provider>` entry in the managed (non-tmux) path gets
285
+ /// its OWN session, matching the user expectation that `tmux new-session
286
+ /// + claude` is independent every time. The workspace-keyed prefix is
287
+ /// preserved so existing leader-session-anchored protections
288
+ /// (`LEADER_SESSION_PREFIX` matchers in shutdown/cli/mod.rs) still
289
+ /// classify these sessions as leader sessions.
290
+ ///
291
+ /// External / attach paths keep the stable `leader_session_name` —
292
+ /// per-launch nonce would break `--attach-session <name>` reattach
293
+ /// semantics.
294
+ pub fn managed_leader_session_name(provider: Provider, workspace: &Path) -> SessionName {
295
+ let resolved = resolve_workspace_for_hash(workspace);
296
+ let folder_raw = resolved
297
+ .file_name()
298
+ .and_then(|n| n.to_str())
299
+ .unwrap_or("workspace");
300
+ let folder = sanitize_session_folder(folder_raw);
301
+ let hash = sha1_hex_prefix(resolved.to_string_lossy().as_bytes(), 8);
302
+ let pid = std::process::id();
303
+ let epoch_nanos = std::time::SystemTime::now()
304
+ .duration_since(std::time::UNIX_EPOCH)
305
+ .map(|d| d.as_nanos())
306
+ .unwrap_or(0);
307
+ SessionName::new(format!(
308
+ "{LEADER_SESSION_PREFIX}{}-{folder}-{hash}-{pid:x}-{epoch_nanos:x}",
309
+ provider_wire(provider)
310
+ ))
311
+ }
312
+
269
313
  fn start_argv(
270
314
  mode: LeaderStartMode,
271
315
  provider: Provider,
@@ -423,31 +467,14 @@ fn ensure_managed_leader_pane(
423
467
  plan: &LeaderStartPlan,
424
468
  workspace: &Path,
425
469
  ) -> Result<SpawnResult, LeaderError> {
470
+ // 0.4.10+ mirror-session fix v2 (option B): each managed launch gets
471
+ // its OWN session name (via `managed_leader_session_name`'s nonce).
472
+ // The historical "session already exists → reuse pane" branch is
473
+ // structurally unreachable now (the per-launch session never
474
+ // preexists). If a caller bypasses the nonce path and reuses a name,
475
+ // tmux's own duplicate-session error will surface — better than
476
+ // silently attaching a second client.
426
477
  if transport.has_session(session).unwrap_or(false) {
427
- if transport
428
- .list_windows(session)
429
- .unwrap_or_default()
430
- .iter()
431
- .any(|existing| existing.as_str() == window.as_str())
432
- {
433
- if let Some(existing) = transport
434
- .list_targets()
435
- .unwrap_or_default()
436
- .into_iter()
437
- .find(|pane| {
438
- pane.session.as_str() == session.as_str()
439
- && pane.window_name.as_ref().map(WindowName::as_str)
440
- == Some(window.as_str())
441
- })
442
- {
443
- return Ok(SpawnResult {
444
- pane_id: existing.pane_id,
445
- session: session.clone(),
446
- window: window.clone(),
447
- child_pid: existing.pane_pid,
448
- });
449
- }
450
- }
451
478
  // 0.4.x (CR C-1 + C-2): leader env_unset reuses the worker
452
479
  // provider_env_unsets (single source of truth) + spawn through the
453
480
  // leader shell wrapper so provider exit returns to a shell, not
@@ -1586,4 +1613,121 @@ mod tests {
1586
1613
  assert_eq!(sent[0].0, Target::Pane(PaneId::new("%0")));
1587
1614
  assert_eq!(sent[0].1, vec![Key::Enter]);
1588
1615
  }
1616
+
1617
+ // ═══════════════════════════════════════════════════════════════════
1618
+ // 0.4.10+ mirror-session fix v2 (option B: independent session per launch).
1619
+ //
1620
+ // managed_leader_session_name appends a per-launch nonce so each
1621
+ // `team-agent <provider>` entry in the same workspace creates its own
1622
+ // tmux session, matching `tmux new-session + claude` semantics.
1623
+ // ═══════════════════════════════════════════════════════════════════
1624
+
1625
+ #[test]
1626
+ fn managed_leader_session_name_carries_leader_prefix() {
1627
+ // Protection invariant: every session name returned by the
1628
+ // managed path must still match LEADER_SESSION_PREFIX so the
1629
+ // shutdown / cli/mod.rs prefix matchers still classify it as a
1630
+ // leader session.
1631
+ let workspace = std::env::temp_dir().join(format!(
1632
+ "ta_rs_mgr_prefix_{}",
1633
+ std::process::id()
1634
+ ));
1635
+ let _ = std::fs::create_dir_all(&workspace);
1636
+ let name = super::managed_leader_session_name(Provider::ClaudeCode, &workspace);
1637
+ assert!(
1638
+ name.as_str().starts_with(super::super::LEADER_SESSION_PREFIX),
1639
+ "managed session name must carry LEADER_SESSION_PREFIX (`{}`); \
1640
+ got `{}`",
1641
+ super::super::LEADER_SESSION_PREFIX,
1642
+ name.as_str()
1643
+ );
1644
+ assert!(
1645
+ name.as_str().contains("claude_code"),
1646
+ "managed session name must include provider wire; got `{}`",
1647
+ name.as_str()
1648
+ );
1649
+ let _ = std::fs::remove_dir_all(&workspace);
1650
+ }
1651
+
1652
+ #[test]
1653
+ fn managed_leader_session_name_is_unique_per_call() {
1654
+ // Two consecutive calls must return DIFFERENT names — the
1655
+ // per-launch nonce defeats the mirror-session bug.
1656
+ let workspace = std::env::temp_dir().join(format!(
1657
+ "ta_rs_mgr_unique_{}",
1658
+ std::process::id()
1659
+ ));
1660
+ let _ = std::fs::create_dir_all(&workspace);
1661
+ let a = super::managed_leader_session_name(Provider::ClaudeCode, &workspace);
1662
+ // Sleep > 1ns to ensure epoch_nanos advances even on coarse clocks.
1663
+ std::thread::sleep(std::time::Duration::from_millis(1));
1664
+ let b = super::managed_leader_session_name(Provider::ClaudeCode, &workspace);
1665
+ assert_ne!(
1666
+ a.as_str(),
1667
+ b.as_str(),
1668
+ "two managed launches in the same workspace must get \
1669
+ DIFFERENT session names (mirror-session fix v2); got \
1670
+ `{}` vs `{}`",
1671
+ a.as_str(),
1672
+ b.as_str()
1673
+ );
1674
+ let _ = std::fs::remove_dir_all(&workspace);
1675
+ }
1676
+
1677
+ #[test]
1678
+ fn leader_session_name_is_stable_across_calls() {
1679
+ // External / attach paths must keep the stable workspace-keyed
1680
+ // name — `--attach-session <name>` reattach would break if the
1681
+ // name varied per call.
1682
+ let workspace = std::env::temp_dir().join(format!(
1683
+ "ta_rs_lsn_stable_{}",
1684
+ std::process::id()
1685
+ ));
1686
+ let _ = std::fs::create_dir_all(&workspace);
1687
+ let a = super::leader_session_name(Provider::ClaudeCode, &workspace);
1688
+ let b = super::leader_session_name(Provider::ClaudeCode, &workspace);
1689
+ assert_eq!(
1690
+ a.as_str(),
1691
+ b.as_str(),
1692
+ "leader_session_name must be deterministic per workspace \
1693
+ (external/attach paths depend on stability); got `{}` vs `{}`",
1694
+ a.as_str(),
1695
+ b.as_str()
1696
+ );
1697
+ let _ = std::fs::remove_dir_all(&workspace);
1698
+ }
1699
+
1700
+ /// CR C-5 source grep guard: managed mode passes the nonce session
1701
+ /// name through `leader_start_plan` so the per-launch independence
1702
+ /// reaches `ensure_managed_leader_pane`. External/attach paths must
1703
+ /// keep `leader_session_name`.
1704
+ #[test]
1705
+ fn managed_path_uses_nonce_session_name_grep_guard() {
1706
+ let manifest = std::path::Path::new(env!("CARGO_MANIFEST_DIR"));
1707
+ let start_rs = manifest.join("src").join("leader").join("start.rs");
1708
+ let contents =
1709
+ std::fs::read_to_string(&start_rs).expect("read leader/start.rs");
1710
+ // leader_start_plan must dispatch to managed_leader_session_name
1711
+ // on the managed path.
1712
+ let start = contents
1713
+ .find("pub fn leader_start_plan(")
1714
+ .expect("leader_start_plan must exist");
1715
+ let end = contents[start + 1..]
1716
+ .find("\npub fn ")
1717
+ .or_else(|| contents[start + 1..].find("\nfn "))
1718
+ .map(|p| start + 1 + p)
1719
+ .unwrap_or(contents.len());
1720
+ let body = &contents[start..end];
1721
+ assert!(
1722
+ body.contains("managed_leader_session_name(provider, workspace)"),
1723
+ "leader_start_plan must call managed_leader_session_name on \
1724
+ the managed path (mirror-session fix v2); body excerpt: {body}"
1725
+ );
1726
+ // The external_path branch must still use the stable name.
1727
+ assert!(
1728
+ body.contains("leader_session_name(provider, workspace)"),
1729
+ "leader_start_plan must keep leader_session_name on the \
1730
+ external/attach paths; body excerpt: {body}"
1731
+ );
1732
+ }
1589
1733
  }
@@ -420,6 +420,48 @@ fn agent_pane_live_by_id(
420
420
  }
421
421
  }
422
422
 
423
+ /// 0.4.10+ reset duplicate-window fix (CR-approved, plan §1).
424
+ ///
425
+ /// Enumerate live panes whose `(session, window_name)` match the given pair.
426
+ /// Used by `stop_agent_at_paths` (when the stored pane_id is stale/dead but a
427
+ /// same-role window survives) and by the reset hard gate (to prove no
428
+ /// duplicate window residue remains before `start_agent_at_paths`).
429
+ ///
430
+ /// `list_targets()` is a point-in-time tmux snapshot — duplicates ARE
431
+ /// preserved in the result so callers can see the full set.
432
+ ///
433
+ /// Caller MUST also check `is_per_agent_window(window, agent_id)` before
434
+ /// using this list to kill panes (plan §2 safety constraint): a shared
435
+ /// layout window may host co-tenants.
436
+ fn list_same_role_panes(
437
+ transport: &dyn crate::transport::Transport,
438
+ session: &crate::transport::SessionName,
439
+ window: &str,
440
+ ) -> Vec<crate::transport::PaneInfo> {
441
+ transport
442
+ .list_targets()
443
+ .unwrap_or_default()
444
+ .into_iter()
445
+ .filter(|pane| {
446
+ pane.session.as_str() == session.as_str()
447
+ && pane.window_name.as_ref().map(WindowName::as_str) == Some(window)
448
+ })
449
+ .collect()
450
+ }
451
+
452
+ /// 0.4.10+ reset duplicate-window fix (plan §2 safety constraint).
453
+ ///
454
+ /// Returns true only when `window == agent_id`, i.e. the canonical
455
+ /// per-agent window. Adaptive/shared layout windows (`workers`, `team`,
456
+ /// or any layout window name produced by `is_adaptive_layout_window_pub`)
457
+ /// MUST NOT be broad-killed by name — they may host co-tenants. The
458
+ /// caller falls back to safer behavior (refuse stop, surface
459
+ /// RequirementUnmet/transport error) when this returns false.
460
+ fn is_per_agent_window(window: &str, agent_id: &AgentId) -> bool {
461
+ window == agent_id.as_str()
462
+ && !crate::lifecycle::launch::is_adaptive_layout_window_pub(window)
463
+ }
464
+
423
465
  fn tmux_start_mode_for_spawn(
424
466
  spawn: &SpawnedAgentWindow,
425
467
  into_existing_session: bool,
@@ -602,22 +644,62 @@ pub(super) fn stop_agent_at_paths(
602
644
  .as_ref()
603
645
  .map(|pane| pane.as_str().to_string())
604
646
  .unwrap_or_else(|| format!("{}:{window}", session_name.as_str()));
605
- let stopped = pane_id
647
+ // 0.4.10+ reset duplicate-window fix (plan §2): stop must resolve a
648
+ // STALE stored pane_id to live same-role panes by `(session, window)`
649
+ // enumeration BEFORE concluding the worker is absent. Pre-fix logic
650
+ // returned stopped=false when pane_id was dead even if a residual
651
+ // duplicate window survived — that residue then collided with
652
+ // reset's unconditional start, producing the observed
653
+ // `stopped=false, started=true` duplicate-window pattern.
654
+ //
655
+ // Only the STALE-pane-id branch enumerates same-role panes. When
656
+ // pane_id was never set in state (legacy state shape, never observed
657
+ // a real spawn), the existing window-based fallback is preserved —
658
+ // the duplicate-window bug requires a stored-but-stale pane_id as
659
+ // the trigger.
660
+ let stored_pane_live = pane_id
606
661
  .as_ref()
607
662
  .map(|pane| agent_pane_live_by_id(transport, pane))
608
- .unwrap_or_else(|| window_exists(transport, &session_name, &window));
663
+ .unwrap_or(false);
664
+ let stored_pane_stale = pane_id.is_some() && !stored_pane_live;
665
+ let same_role_panes: Vec<crate::transport::PaneInfo> =
666
+ if stored_pane_stale && is_per_agent_window(&window, agent_id) {
667
+ list_same_role_panes(transport, &session_name, &window)
668
+ } else {
669
+ Vec::new()
670
+ };
671
+ let stopped = stored_pane_live
672
+ || !same_role_panes.is_empty()
673
+ || (pane_id.is_none() && window_exists(transport, &session_name, &window));
609
674
  if stopped {
610
675
  // golden operations.py:84-86: a non-zero kill-window raises
611
676
  // RuntimeError(f"failed to stop agent {agent_id}: {proc.stderr.strip()}").
612
- let kill_result = if let Some(pane) = pane_id.as_ref() {
613
- transport.kill_pane(pane)
614
- } else {
615
- let target = Target::SessionWindow {
616
- session: session_name.clone(),
617
- window: WindowName::new(&window),
677
+ //
678
+ // 0.4.10+ kill resolution order (plan §2):
679
+ // 1. stored pane_id is live → kill it by pane_id.
680
+ // 2. stored pane_id is stale BUT same-role panes survive →
681
+ // kill each by pane_id (duplicate window names make
682
+ // kill-window -t session:window ambiguous).
683
+ // 3. no pane_id at all but window exists → kill_window as
684
+ // before (legacy compat for state without pane_id field).
685
+ let kill_result: Result<(), crate::transport::TransportError> =
686
+ if let Some(pane) = pane_id.as_ref().filter(|_| stored_pane_live) {
687
+ transport.kill_pane(pane)
688
+ } else if !same_role_panes.is_empty() {
689
+ let mut last_err: Option<crate::transport::TransportError> = None;
690
+ for residual in &same_role_panes {
691
+ if let Err(e) = transport.kill_pane(&residual.pane_id) {
692
+ last_err = Some(e);
693
+ }
694
+ }
695
+ last_err.map(Err).unwrap_or(Ok(()))
696
+ } else {
697
+ let target = Target::SessionWindow {
698
+ session: session_name.clone(),
699
+ window: WindowName::new(&window),
700
+ };
701
+ transport.kill_window(&target)
618
702
  };
619
- transport.kill_window(&target)
620
- };
621
703
  if let Err(e) = kill_result {
622
704
  let stderr = match &e {
623
705
  crate::transport::TransportError::Subprocess { stderr, .. } => stderr.trim().to_string(),
@@ -906,7 +988,158 @@ fn reset_agent_at_paths(
906
988
  .filter(|session| !session.is_empty())
907
989
  .map(SessionId::new);
908
990
  crate::lifecycle::launch::ensure_owner_allowed_for_state(&state_before_stop, Some(agent_id))?;
991
+ // Capture old pane_id / pane_pid / window BEFORE stop, so the hard gate
992
+ // below can prove the same prior instance is gone (or refuse start).
993
+ let old_pane_id_before = state_before_stop
994
+ .get("agents")
995
+ .and_then(|v| v.get(agent_id.as_str()))
996
+ .and_then(|v| v.get("pane_id"))
997
+ .and_then(|v| v.as_str())
998
+ .filter(|p| !p.is_empty())
999
+ .map(crate::transport::PaneId::new);
1000
+ let old_pane_pid_before = state_pane_pid(&state_before_stop, agent_id);
1001
+ // CR C-2: take ONE pre-stop snapshot of the team session's panes so
1002
+ // the gate below can compute "what survived stop" by set difference,
1003
+ // not "what panes exist at all" (which would refuse legitimate
1004
+ // reset flows where stop killed the pane and the post-stop snapshot
1005
+ // includes that same pane id in a transport mock that does not
1006
+ // reflect kill removal).
1007
+ let pre_stop_window = state_before_stop
1008
+ .get("agents")
1009
+ .and_then(|v| v.get(agent_id.as_str()))
1010
+ .and_then(|v| v.get("window"))
1011
+ .and_then(|v| v.as_str())
1012
+ .filter(|s| !s.is_empty())
1013
+ .unwrap_or_else(|| agent_id.as_str())
1014
+ .to_string();
1015
+ let pre_stop_spec = load_team_spec(spec_workspace).ok();
1016
+ let pre_stop_session = pre_stop_spec
1017
+ .as_ref()
1018
+ .map(|spec| state_session_name_from_spec(&state_before_stop, spec));
1019
+ let pre_stop_pane_ids: std::collections::BTreeSet<String> =
1020
+ if let Some(session) = pre_stop_session.as_ref() {
1021
+ if is_per_agent_window(&pre_stop_window, agent_id) {
1022
+ list_same_role_panes(transport, session, &pre_stop_window)
1023
+ .iter()
1024
+ .map(|p| p.pane_id.as_str().to_string())
1025
+ .collect()
1026
+ } else {
1027
+ std::collections::BTreeSet::new()
1028
+ }
1029
+ } else {
1030
+ std::collections::BTreeSet::new()
1031
+ };
909
1032
  let stop = stop_agent_at_paths(workspace, spec_workspace, agent_id, team, transport)?;
1033
+
1034
+ // 0.4.10+ paused agent skip: a paused agent's start path returns
1035
+ // StartAgentOutcome::Paused (no spawn). There is no duplicate-window
1036
+ // hazard, so the gate is a no-op for paused agents.
1037
+ let agent_is_paused = state_before_stop
1038
+ .get("agents")
1039
+ .and_then(|v| v.get(agent_id.as_str()))
1040
+ .and_then(|v| v.get("paused"))
1041
+ .and_then(|v| v.as_bool())
1042
+ .unwrap_or(false);
1043
+ // 0.4.10+ reset duplicate-window fix (plan §3): HARD GATE before start.
1044
+ // After stop_agent_at_paths returns, prove the old instance is gone OR
1045
+ // refuse to spawn. The pre-fix path unconditionally proceeded to
1046
+ // discard/save/start even when stop returned stopped=false (stop's
1047
+ // pane_id was stale), creating the observed duplicate-window pattern.
1048
+ //
1049
+ // Residue definition (correct: differential, not absolute):
1050
+ // A pane is RESIDUE iff it appears in BOTH the pre-stop snapshot
1051
+ // AND the post-stop snapshot. The pre-fix attempt used "any
1052
+ // matching pane exists post-stop" which broke legitimate flows
1053
+ // where the transport mock does not model kill_pane removal.
1054
+ //
1055
+ // Old pane id / pid checks:
1056
+ // The OLD stored pane_id / pane_pid must be gone (not just
1057
+ // reachable but actually killed). For real tmux this is the
1058
+ // structural truth source; for mocks the differential approach
1059
+ // above covers the post-stop visibility.
1060
+ //
1061
+ // CR C-5: gate is reset-specific; standalone stop-agent path keeps
1062
+ // existing "already absent is ok" behavior.
1063
+ //
1064
+ // Gate scope refinement: when stop.stopped == true, the kill_pane
1065
+ // call already succeeded (and drain_old_pane_and_pid polled for
1066
+ // the pane to become unreachable). Treat that as the authoritative
1067
+ // signal — running the gate again post-stop introduces a race
1068
+ // window where tmux's has_pane lag can spuriously report Live.
1069
+ // Only gate the dangerous case: stop reported stopped == false
1070
+ // (state's stale pane_id pointed at nothing kill-able), which is
1071
+ // exactly the duplicate-window bug pattern from the macmini
1072
+ // evidence: `stop_agent.complete stopped=false` followed by an
1073
+ // unconditional `start_agent.agent_start`.
1074
+ if !agent_is_paused && !stop.stopped {
1075
+ let spec_for_gate = load_team_spec(spec_workspace)?;
1076
+ let gate_state = resolve_team_scoped_state_or_refuse(workspace, team)?;
1077
+ let session_name_gate = state_session_name_from_spec(&gate_state, &spec_for_gate);
1078
+ let gate_window = gate_state
1079
+ .get("agents")
1080
+ .and_then(|v| v.get(agent_id.as_str()))
1081
+ .and_then(|v| v.get("window"))
1082
+ .and_then(|v| v.as_str())
1083
+ .filter(|s| !s.is_empty())
1084
+ .unwrap_or_else(|| agent_id.as_str())
1085
+ .to_string();
1086
+ let old_pane_still_live = old_pane_id_before
1087
+ .as_ref()
1088
+ .map(|p| agent_pane_live_by_id(transport, p))
1089
+ .unwrap_or(false);
1090
+ // Take a SECOND snapshot post-stop and compute the differential.
1091
+ // Only panes present in BOTH snapshots are residue (stop did not
1092
+ // remove them).
1093
+ let post_stop_panes: Vec<crate::transport::PaneInfo> =
1094
+ if is_per_agent_window(&gate_window, agent_id) {
1095
+ list_same_role_panes(transport, &session_name_gate, &gate_window)
1096
+ } else {
1097
+ Vec::new()
1098
+ };
1099
+ let remaining_panes: Vec<crate::transport::PaneInfo> = post_stop_panes
1100
+ .into_iter()
1101
+ .filter(|p| pre_stop_pane_ids.contains(p.pane_id.as_str()))
1102
+ .collect();
1103
+ // Pid-alone aliveness is secondary evidence and noisy under fixtures
1104
+ // (synthetic pids may by chance be live on the test machine). Block
1105
+ // ONLY on tmux-visible residue: old pane still live OR same-role
1106
+ // panes survived stop. The pid is still recorded in the event for
1107
+ // diagnostics.
1108
+ let old_pid_still_live = old_pane_pid_before
1109
+ .filter(|_| old_pane_still_live)
1110
+ .map(|pid| pid_is_alive(pid))
1111
+ .unwrap_or(false);
1112
+ if old_pane_still_live || !remaining_panes.is_empty() {
1113
+ let remaining_pane_ids: Vec<String> = remaining_panes
1114
+ .iter()
1115
+ .map(|p| p.pane_id.as_str().to_string())
1116
+ .collect();
1117
+ let old_pane_str = old_pane_id_before
1118
+ .as_ref()
1119
+ .map(|p| p.as_str().to_string())
1120
+ .unwrap_or_default();
1121
+ let old_pid_val = old_pane_pid_before.unwrap_or(0);
1122
+ let _ = write_reset_stop_not_proven_event(
1123
+ workspace,
1124
+ agent_id,
1125
+ &old_pane_str,
1126
+ old_pid_val,
1127
+ &remaining_pane_ids,
1128
+ );
1129
+ // CR C-1 N38 three-line error: error / action / log_hint.
1130
+ let _ = stop; // silence unused on the refusal path
1131
+ return Err(LifecycleError::RequirementUnmet(format!(
1132
+ "reset refused: old agent instance still live for {agent_id}\n\
1133
+ action: stop the worker manually with `team-agent stop-agent {agent_id} --team <team>` then retry reset, or kill the residual tmux panes [{ids}]\n\
1134
+ log_hint: see reset_agent.stop_not_proven event (old_pane_id={old}, old_pane_pid={pid}, remaining_panes=[{ids}])",
1135
+ agent_id = agent_id.as_str(),
1136
+ ids = remaining_pane_ids.join(", "),
1137
+ old = old_pane_str,
1138
+ pid = old_pid_val,
1139
+ )));
1140
+ }
1141
+ }
1142
+
910
1143
  let mut state = resolve_team_scoped_state_or_refuse(workspace, team)?;
911
1144
  let spec = load_team_spec(spec_workspace)?;
912
1145
  discard_agent_session_fields(&mut state, agent_id)?;
@@ -1198,6 +1431,32 @@ fn write_reset_tombstone_event(
1198
1431
  Ok(())
1199
1432
  }
1200
1433
 
1434
+ /// 0.4.10+ reset duplicate-window fix (plan §3): emit a structured event
1435
+ /// when the reset hard gate refuses to start because the old instance is
1436
+ /// proven still live (old pane id reachable, old pane pid alive, or
1437
+ /// same-role panes remain in the team session).
1438
+ fn write_reset_stop_not_proven_event(
1439
+ workspace: &Path,
1440
+ agent_id: &AgentId,
1441
+ old_pane_id: &str,
1442
+ old_pane_pid: u32,
1443
+ remaining_panes: &[String],
1444
+ ) -> Result<(), LifecycleError> {
1445
+ crate::event_log::EventLog::new(workspace)
1446
+ .write(
1447
+ "reset_agent.stop_not_proven",
1448
+ serde_json::json!({
1449
+ "agent_id": agent_id.as_str(),
1450
+ "old_pane_id": old_pane_id,
1451
+ "old_pane_pid": old_pane_pid,
1452
+ "remaining_panes": remaining_panes,
1453
+ "action": "stop the worker manually then retry reset, or kill the residual tmux panes",
1454
+ }),
1455
+ )
1456
+ .map_err(|e| LifecycleError::StatePersist(e.to_string()))?;
1457
+ Ok(())
1458
+ }
1459
+
1201
1460
  fn write_reset_complete_event(
1202
1461
  workspace: &Path,
1203
1462
  agent_id: &AgentId,
@@ -322,6 +322,7 @@ log="${TEAM_AGENT_E27_TMUX_LOG}"
322
322
  expected="${TEAM_AGENT_E27_EXPECTED_ENDPOINT}"
323
323
  session="${TEAM_AGENT_E27_SESSION_NAME}"
324
324
  pane="${TEAM_AGENT_E27_PANE_ID}"
325
+ killed_marker="${log}.killed"
325
326
  track=0
326
327
  case "$*" in
327
328
  *"$expected"*|*"$session"*|*"$pane"*) track=1 ;;
@@ -340,8 +341,22 @@ case "$*" in
340
341
  exit 18
341
342
  ;;
342
343
  esac
344
+ # 0.4.10+ reset-gate fix: track kill-pane so the next display-message
345
+ # probe reports the pane as gone. The reset hard gate calls has_pane
346
+ # after stop_agent_at_paths returns; a shim that always reports the
347
+ # original pane as live would trip the gate.
348
+ case "$*" in
349
+ *"kill-pane -t $pane"*)
350
+ : > "$killed_marker"
351
+ exit 0
352
+ ;;
353
+ esac
343
354
  case "$*" in
344
355
  *"display-message -p -t $pane #{pane_id}"*)
356
+ if [ -f "$killed_marker" ]; then
357
+ echo "can't find pane: $pane" >&2
358
+ exit 1
359
+ fi
345
360
  printf '%s\n' "$pane"
346
361
  exit 0
347
362
  ;;
@@ -352,6 +367,13 @@ case "$*" in
352
367
  *"list-windows -t $session -F #{window_name}"*)
353
368
  exit 0
354
369
  ;;
370
+ *"list-panes -a -F"*)
371
+ # Hard-gate post-stop snapshot: no residual same-role panes after kill.
372
+ if [ -f "$killed_marker" ]; then
373
+ exit 0
374
+ fi
375
+ exit 0
376
+ ;;
355
377
  *"has-session -t $session"*)
356
378
  exit 0
357
379
  ;;
@@ -609,6 +631,171 @@ fn reset_agent_emits_golden_lifecycle_event_payloads() {
609
631
  assert_eq!(complete.get("agent_id").and_then(|v| v.as_str()), Some("alpha"));
610
632
  }
611
633
 
634
+ // 0.4.10+ reset duplicate-window fix RED tests
635
+ // (plan §1-§3, CR C-1/C-2/C-5 acceptance).
636
+
637
+ /// Plan §2: stop must resolve a stale pane_id to live same-role panes
638
+ /// and kill them by pane_id. Pre-fix path returned stopped=false when
639
+ /// pane_id was stale even if a residue survived; this lets reset's
640
+ /// unconditional start spawn a duplicate window.
641
+ ///
642
+ /// With the fix: stale pane_id + same-role residue → stop enumerates,
643
+ /// kills by pane_id, returns stopped=true. Reset proceeds normally.
644
+ #[test]
645
+ fn reset_agent_stale_pane_with_same_role_residue_kills_by_pane_id() {
646
+ use crate::transport::{PaneInfo, PaneId as PaneIdT, SessionName, WindowName};
647
+ let ws = lanea_team_ws("running");
648
+ let mut state = crate::state::persist::load_runtime_state(&ws).unwrap();
649
+ state["agents"]["alpha"]["pane_id"] = json!("%STALE");
650
+ state["agents"]["alpha"]["window"] = json!("alpha");
651
+ crate::state::persist::save_runtime_state(&ws, &state).unwrap();
652
+
653
+ // Transport: stale %STALE absent; same-role residual %RESIDUAL listed.
654
+ let residual = PaneInfo {
655
+ pane_id: PaneIdT::new("%RESIDUAL"),
656
+ session: SessionName::new("team-laneateam"),
657
+ window_index: Some(0),
658
+ window_name: Some(WindowName::new("alpha")),
659
+ pane_index: None,
660
+ tty: None,
661
+ current_command: None,
662
+ current_path: None,
663
+ active: true,
664
+ pane_pid: None,
665
+ leader_env: std::collections::BTreeMap::new(),
666
+ };
667
+ let transport = OfflineTransport::new()
668
+ .with_session_present(true)
669
+ .with_targets(vec![residual.clone()])
670
+ .with_pane_presence("%STALE", false);
671
+
672
+ // The stop sub-step must enumerate same-role panes and kill %RESIDUAL.
673
+ // The reset overall proceeds (stop.stopped=true → gate skipped).
674
+ let outcome = crate::lifecycle::reset_agent_with_transport(
675
+ &ws,
676
+ &aid("alpha"),
677
+ true,
678
+ false,
679
+ None,
680
+ &transport,
681
+ );
682
+ let outcome_dbg = format!("{outcome:?}");
683
+ // Outcome may be Ok or downstream Err from spawn, but it MUST NOT be
684
+ // the stop-not-proven RequirementUnmet (the gate did not need to fire
685
+ // because stop correctly killed the residue).
686
+ assert!(
687
+ !outcome_dbg.contains("old agent instance still live"),
688
+ "stop must kill stale-pane residue (no stop-not-proven gate \
689
+ trigger); got {outcome_dbg}"
690
+ );
691
+ // The stop_complete event must report stopped=true (residual was killed).
692
+ let events = lifecycle_events(&ws);
693
+ let stop_complete = find_event(&events, "stop_agent.complete")
694
+ .expect("stop_agent.complete must be emitted");
695
+ assert_eq!(
696
+ stop_complete.get("stopped").and_then(|v| v.as_bool()),
697
+ Some(true),
698
+ "stop must report stopped=true when same-role residue was killed; \
699
+ got {stop_complete:?}"
700
+ );
701
+ }
702
+
703
+ /// Plan §3 hard gate: structural verification — the helpers exist and
704
+ /// are wired into reset_agent_at_paths. The full race-condition scenario
705
+ /// (stop returns stopped=false BUT residue appears between stop and
706
+ /// gate-time list_targets snapshot) requires concurrency the
707
+ /// OfflineTransport cannot model — the live macmini batch reset
708
+ /// evidence (.team/evidence/) is the truth source for that race. The
709
+ /// unit test below verifies the gate code path is reachable + the
710
+ /// event writer + N38 error text via a synthetic state with a still-live
711
+ /// stored pane_id that stop's kill_pane targeted but the
712
+ /// post-stop has_pane re-reads as live (a deterministic mock).
713
+ #[test]
714
+ fn reset_agent_stop_not_proven_grep_guard_hard_gate_wired() {
715
+ let manifest = std::path::Path::new(env!("CARGO_MANIFEST_DIR"));
716
+ let agent_rs = manifest
717
+ .join("src")
718
+ .join("lifecycle")
719
+ .join("restart")
720
+ .join("agent.rs");
721
+ let contents = std::fs::read_to_string(&agent_rs).expect("read agent.rs");
722
+ // Gate must exist with the N38 three-line text and stop_not_proven event.
723
+ assert!(
724
+ contents.contains("reset refused: old agent instance still live"),
725
+ "reset_agent_at_paths must carry the N38 three-line error (CR C-1)"
726
+ );
727
+ assert!(
728
+ contents.contains("write_reset_stop_not_proven_event"),
729
+ "reset_agent_at_paths must emit reset_agent.stop_not_proven via the writer"
730
+ );
731
+ assert!(
732
+ contents.contains("if !agent_is_paused && !stop.stopped"),
733
+ "gate must run only when stop.stopped=false (the dangerous case)"
734
+ );
735
+ assert!(
736
+ contents.contains("list_same_role_panes")
737
+ && contents.contains("is_per_agent_window"),
738
+ "stop_agent_at_paths must enumerate same-role panes via the new helpers"
739
+ );
740
+ }
741
+
742
+ /// Plan §3 hard gate: when no residue (clean stop), reset is allowed to
743
+ /// proceed to start. Covers the legitimate `stopped=false` case (worker
744
+ /// already absent — nothing to gate, nothing to refuse).
745
+ #[test]
746
+ fn reset_agent_already_absent_can_start() {
747
+ let ws = lanea_team_ws("stopped");
748
+ // No stale pane_id, no residual panes — gate is a no-op.
749
+ let transport = OfflineTransport::new().with_session_present(true);
750
+ let outcome = crate::lifecycle::reset_agent_with_transport(
751
+ &ws,
752
+ &aid("alpha"),
753
+ true,
754
+ false,
755
+ None,
756
+ &transport,
757
+ );
758
+ // The transport is offline so spawn may fail downstream; the contract
759
+ // for THIS test is that the hard gate did NOT short-circuit to
760
+ // RequirementUnmet with "old agent instance still live".
761
+ let outcome_dbg = format!("{outcome:?}");
762
+ assert!(
763
+ !outcome_dbg.contains("old agent instance still live"),
764
+ "reset with no residue must NOT trigger the stop-not-proven gate; \
765
+ got {outcome_dbg}"
766
+ );
767
+ // The stop_not_proven event must NOT be present.
768
+ let events = lifecycle_events(&ws);
769
+ assert!(
770
+ find_event(&events, "reset_agent.stop_not_proven").is_none(),
771
+ "no reset_agent.stop_not_proven event when nothing to gate; \
772
+ events seen: {:?}",
773
+ names(&events)
774
+ );
775
+ }
776
+
777
+ /// Plan §2 + CR C-5: standalone stop-agent must keep "already absent is
778
+ /// stopped=false, ok" behavior. The hard gate only runs in reset, not
779
+ /// in stop-agent. This locks the boundary so the fix does not regress
780
+ /// existing stop-agent contract.
781
+ #[test]
782
+ fn stop_agent_already_absent_still_returns_stopped_false() {
783
+ let ws = lanea_team_ws("stopped");
784
+ let transport = OfflineTransport::new().with_session_present(true);
785
+ let result = crate::lifecycle::stop_agent_with_transport(
786
+ &ws,
787
+ &aid("alpha"),
788
+ None,
789
+ &transport,
790
+ );
791
+ let report = result.expect("stop_agent must return Ok for absent agent");
792
+ assert!(
793
+ !report.stopped,
794
+ "stop_agent on already-absent worker returns stopped=false (no \
795
+ kill, contract preserved); got {report:?}"
796
+ );
797
+ }
798
+
612
799
  // stop-agent — golden operations.py:98 writes stop_agent.complete {agent_id, target, stopped}
613
800
  // (+ stop_agent.window_stop_failed {agent_id, target, stderr} on kill failure). Rust emits none.
614
801
  #[test]
@@ -34,6 +34,12 @@ type RecordedSpawns = LaneSpawns;
34
34
  struct SessionProbeRecordingTransport {
35
35
  spawns: RecordedSpawns,
36
36
  session_exists: bool,
37
+ // 0.4.10+ reset hard-gate: record killed panes so liveness/has_pane
38
+ // reflect the kill (the gate checks "did stop actually remove the
39
+ // old pane"). Default mock behavior reported Live unconditionally —
40
+ // safe pre-fix because no gate ran, but the gate now needs the kill
41
+ // to be observable.
42
+ killed_panes: std::sync::Mutex<std::collections::BTreeSet<String>>,
37
43
  }
38
44
  impl crate::transport::Transport for SessionProbeRecordingTransport {
39
45
  fn kind(&self) -> crate::transport::BackendKind {
@@ -62,9 +68,21 @@ impl crate::transport::Transport for SessionProbeRecordingTransport {
62
68
  fn query(&self, _t: &crate::transport::Target, _f: crate::transport::PaneField) -> Result<Option<String>, crate::transport::TransportError> {
63
69
  unimplemented!("not reached")
64
70
  }
65
- fn liveness(&self, _p: &crate::transport::PaneId) -> Result<crate::model::enums::PaneLiveness, crate::transport::TransportError> {
71
+ fn liveness(&self, p: &crate::transport::PaneId) -> Result<crate::model::enums::PaneLiveness, crate::transport::TransportError> {
72
+ let killed = self.killed_panes.lock().unwrap();
73
+ if killed.contains(p.as_str()) {
74
+ return Ok(crate::model::enums::PaneLiveness::Dead);
75
+ }
66
76
  Ok(crate::model::enums::PaneLiveness::Live)
67
77
  }
78
+ fn has_pane(&self, p: &crate::transport::PaneId) -> Result<Option<bool>, crate::transport::TransportError> {
79
+ let killed = self.killed_panes.lock().unwrap();
80
+ Ok(Some(!killed.contains(p.as_str())))
81
+ }
82
+ fn kill_pane(&self, p: &crate::transport::PaneId) -> Result<(), crate::transport::TransportError> {
83
+ self.killed_panes.lock().unwrap().insert(p.as_str().to_string());
84
+ Ok(())
85
+ }
68
86
  fn list_targets(&self) -> Result<Vec<crate::transport::PaneInfo>, crate::transport::TransportError> {
69
87
  Ok(Vec::new())
70
88
  }
@@ -115,10 +133,7 @@ fn start_agent_respawn_into_dead_session_uses_new_session_not_new_window() {
115
133
  let ws = respawn_ws_one_resumable_worker();
116
134
  let spawns = std::sync::Arc::new(std::sync::Mutex::new(Vec::new()));
117
135
  // session_exists=false models the dead CP-1 `-L` server after --discard-session.
118
- let transport = SessionProbeRecordingTransport {
119
- spawns: std::sync::Arc::clone(&spawns),
120
- session_exists: false,
121
- };
136
+ let transport = SessionProbeRecordingTransport { spawns: std::sync::Arc::clone(&spawns), session_exists: false, killed_panes: std::sync::Mutex::new(std::collections::BTreeSet::new()) };
122
137
  let _ = start_agent_with_transport(&ws, &AgentId::new("alpha"), false, false, false, None, &transport);
123
138
  let recorded = spawns.lock().unwrap().clone();
124
139
  assert_eq!(recorded.len(), 1, "exactly one respawn for alpha; got {recorded:?}");
@@ -150,10 +165,7 @@ fn reset_agent_discard_session_rebuilds_window_via_start_respawn() {
150
165
  let ws = restart_ws_two_resumable_workers(); // compiled spec + state(alpha,bravo running) + seeded coordinator
151
166
  let spawns = std::sync::Arc::new(std::sync::Mutex::new(Vec::new()));
152
167
  // live session present (other workers keep it alive) but alpha's window was discarded.
153
- let transport = SessionProbeRecordingTransport {
154
- spawns: std::sync::Arc::clone(&spawns),
155
- session_exists: true,
156
- };
168
+ let transport = SessionProbeRecordingTransport { spawns: std::sync::Arc::clone(&spawns), session_exists: true, killed_panes: std::sync::Mutex::new(std::collections::BTreeSet::new()) };
157
169
  let _ = reset_agent_with_transport(&ws, &AgentId::new("alpha"), true, false, None, &transport);
158
170
  let recorded = spawns.lock().unwrap().clone();
159
171
  assert!(
@@ -191,10 +203,7 @@ fn reset_agent_discard_session_syncs_projection_epoch_inputs_for_restart_agent_c
191
203
 
192
204
  let before = chrono::Utc::now();
193
205
  let spawns = std::sync::Arc::new(std::sync::Mutex::new(Vec::new()));
194
- let transport = SessionProbeRecordingTransport {
195
- spawns: std::sync::Arc::clone(&spawns),
196
- session_exists: true,
197
- };
206
+ let transport = SessionProbeRecordingTransport { spawns: std::sync::Arc::clone(&spawns), session_exists: true, killed_panes: std::sync::Mutex::new(std::collections::BTreeSet::new()) };
198
207
  let outcome = reset_agent_with_transport(&ws, &AgentId::new("alpha"), true, false, None, &transport)
199
208
  .expect("reset-agent alpha --discard-session --no-display");
200
209
  assert!(
@@ -7,45 +7,35 @@ use crate::model::permissions::{resolve_permissions, AgentPermissionInput};
7
7
 
8
8
  const RUNTIME_CONTRACT_SECTION: &str = r#"# Team Agent Teammate Runtime Contract
9
9
 
10
- You are a teammate in a Team Agent runtime, not the user's primary assistant.
11
- The user normally talks to the team lead. Plain text you write in this worker
12
- session is local to this session and is not a team message.
10
+ You are a teammate in a Team Agent runtime. The leader cannot see your terminal
11
+ output. All communication must go through Team Agent MCP tools.
13
12
 
14
- Use Team Agent MCP tools for team-visible coordination:
15
- - Send progress, blockers, permission needs, tool failures, scope changes, and
16
- long-running status updates with team_orchestrator.send_message(to='leader',
17
- content='<short message>').
18
- - Send to another teammate by agent id when coordination is useful, or use
19
- to='*' to notify every other team member. The runtime resolves only this team
20
- and excludes your own worker.
21
- - When the task is complete, call team_orchestrator.report_result exactly once.
22
- - Do not pass sender, task_id, agent_id, schema_version, or ack fields unless
23
- doing a low-level compatibility diagnostic. The MCP runtime fills protocol
24
- fields from the current worker and task state.
25
- - Emergency fallback exception: if a leader-bound MCP send/report payload is
26
- already built but the MCP transport itself fails or the primary delivery path
27
- errors (for example `Transport closed`, `Connection refused`, `Broken pipe`,
28
- `EOF`, timeout, or internal delivery error), use `team-agent fallback-send-leader`
29
- or `team-agent fallback-report-result` exactly once with `--primary-error`.
30
- Do not use fallback for business refusals such as permission, quota, or unknown
31
- target. After fallback, tell the leader to run `team-agent restart-agent` to
32
- refresh the worker MCP transport.
13
+ ## Communication (mandatory)
33
14
 
34
- If you are blocked or cannot continue, message the leader promptly instead of
35
- waiting silently. If work takes several minutes, send a short progress update.
15
+ - Progress, blockers, questions: team_orchestrator.send_message(to='leader', content='...')
16
+ - Coordinate with teammate: team_orchestrator.send_message(to='<agent_id>', content='...')
17
+ - Broadcast to all teammates: team_orchestrator.send_message(to='*', content='...')
18
+ - Task complete: team_orchestrator.report_result(summary='...') — call exactly once
36
19
 
37
- When any Team Agent worker hits a 500/529/rate-limit/overloaded API error,
38
- slow the team down before retrying: wait 1-2 minutes, keep active workers low,
39
- and avoid blind immediate retries."#;
20
+ When you receive a message from the leader or a teammate, you MUST respond
21
+ through MCP tools. Writing a reply in your terminal does nothing the sender
22
+ will never see it.
40
23
 
24
+ ## Rules
25
+
26
+ - Do not pass sender, task_id, or schema_version — the MCP runtime fills them.
27
+ - If blocked or waiting, send_message to the leader. Do not wait silently.
28
+ - On 500/529/rate-limit errors, wait 1-2 minutes before retrying."#;
29
+
30
+ // 0.4.11 trimmed: the runtime contract section above already covers
31
+ // send_message signatures and report_result exactly-once. The output
32
+ // contract now only carries the RESULT-ENVELOPE-SPECIFIC delivery
33
+ // semantics (leader-attach dependence + fallback status) that the
34
+ // generic runtime section deliberately leaves out.
41
35
  const RESULT_ENVELOPE_OUTPUT_CONTRACT: &str =
42
- "For progress or blockers, call team_orchestrator.send_message(to='leader', content='<short message>'); \
43
- for teammate coordination, send to another agent id or to='*' for every other team member. \
44
- do not pass sender, task_id, or requires_ack because the MCP runtime fills protocol fields. \
45
- the runtime injects it into the attached Codex leader pane when the leader has run attach-leader. \
46
- If no leader is attached, the tool returns a fallback/failed result instead of completion. \
47
- Final completion must call team_orchestrator.report_result exactly once with a short summary \
48
- and optional status/changes/tests; MCP fills schema_version, task_id, and agent_id.";
36
+ "Final completion must call team_orchestrator.report_result exactly once with a short summary \
37
+ and optional status/changes/tests; the MCP runtime injects the result into the attached leader pane. \
38
+ If no leader is attached, the tool returns a fallback/failed result instead of completion.";
49
39
 
50
40
  pub(crate) struct WorkerCommandAgent {
51
41
  id: Option<String>,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@team-agent/installer",
3
- "version": "0.4.10",
3
+ "version": "0.4.11",
4
4
  "description": "npx installer for Team Agent",
5
5
  "keywords": [
6
6
  "codex",
@@ -20,9 +20,9 @@
20
20
  "team-agent-installer": "npm/install.mjs"
21
21
  },
22
22
  "optionalDependencies": {
23
- "@team-agent/cli-darwin-arm64": "0.4.10",
24
- "@team-agent/cli-darwin-x64": "0.4.10",
25
- "@team-agent/cli-linux-x64": "0.4.10"
23
+ "@team-agent/cli-darwin-arm64": "0.4.11",
24
+ "@team-agent/cli-darwin-x64": "0.4.11",
25
+ "@team-agent/cli-linux-x64": "0.4.11"
26
26
  },
27
27
  "scripts": {
28
28
  "postinstall": "node npm/bincheck.mjs",