Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions bin/backends/herdr.sh
Original file line number Diff line number Diff line change
Expand Up @@ -1721,8 +1721,8 @@ fm_backend_herdr_server_owner_name() { # <owner-marker>
# has-session || tmux new-session -d`. Verified: a bare socket CLI call does
# NOT auto-start the server, so this must run before any workspace/tab/pane
# call. The server outlives its launcher and passes its startup environment to
# every later pane, so remove home, harness identity, and supervision selection
# inherited from whichever agent happened to start it. Bounded poll for the
# every later pane, so remove home, harness identity, a parent Claude session,
# and supervision selection inherited from whichever agent happened to start it. Bounded poll for the
# server to report running. `session-leader` starts a Firstmate-owned server
# as a POSIX session leader so Herdr 0.9.1 saved machines accept it.
#
Expand Down Expand Up @@ -1765,6 +1765,8 @@ fm_backend_herdr_server_ensure() { # <session> [session-leader]
(
unset FM_HOME FM_ROOT_OVERRIDE FM_STATE_OVERRIDE FM_DATA_OVERRIDE FM_PROJECTS_OVERRIDE FM_CONFIG_OVERRIDE \
CURSOR_AGENT CURSOR_INVOKED_AS CLAUDECODE PI_CODING_AGENT FM_PI_HARNESS GROK_AGENT FM_SUPERVISION_MODEL \
CLAUDE_CODE_CHILD_SESSION CLAUDE_CODE_SESSION_ID CLAUDE_PID CLAUDE_CODE_MESSAGING_SOCKET \
CLAUDE_CODE_MESSAGING_TOKEN CLAUDE_CODE_SESSION_ATTENDED CLAUDE_CODE_ENTRYPOINT \
FM_TIMEOUT_OWNER_PID FM_EXEC_TIMED_OWNER_PID FM_BACKEND_HERDR_SERVER_SESSION_LEADER
[ "${2:-}" != session-leader ] || FM_BACKEND_HERDR_SERVER_SESSION_LEADER=1
fm_backend_herdr_cli "$session" server </dev/null >/dev/null 2>&1 &
Expand Down
5 changes: 5 additions & 0 deletions bin/fm-primary.sh
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ fi
echo "fm-primary: claude executable not found" >&2
exit 127
}
# Shed a parent Claude session's markers inherited from the terminal or a
# multiplexer server started inside Claude, so every launch below is its own
# session and saves its transcript rather than running as a child session.
unset CLAUDECODE CLAUDE_CODE_CHILD_SESSION CLAUDE_CODE_SESSION_ID CLAUDE_PID CLAUDE_CODE_MESSAGING_SOCKET \
CLAUDE_CODE_MESSAGING_TOKEN CLAUDE_CODE_SESSION_ATTENDED CLAUDE_CODE_ENTRYPOINT

# Without an explicit budget, the wrapper is exactly a plain Claude launch.
if [ ! -e "$CONFIG/context-restart-budget" ] && [ ! -L "$CONFIG/context-restart-budget" ]; then
Expand Down
7 changes: 6 additions & 1 deletion bin/fm-spawn.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2720,8 +2720,13 @@ launch_template() {
# Claude's system-prompt carrier while preserving the normal distrust of
# project and fetched content. A persistent secondmate receives its own
# supervisor contract instead, so this task-worker statement does not apply.
# The env -u list sheds a parent Claude session's markers, which a multiplexer
# server started from inside Claude passes to every pane: an inherited
# CLAUDE_CODE_CHILD_SESSION makes Claude run as a child session that saves no
# transcript, and the session id, pid, and messaging socket name the parent.
# A fresh Claude publishes its own values to the tools and hooks it runs.
claude)
printf '%s' 'CLAUDE_CODE_ENABLE_PROMPT_SUGGESTION=false CLAUDE_CODE_SEND_FEEDBACK=0 claude __CLAUDEPERMFLAG__ __CLAUDEADDDIRS__--settings __CLAUDESETTINGS__ '
printf '%s' 'env -u CLAUDECODE -u CLAUDE_CODE_CHILD_SESSION -u CLAUDE_CODE_SESSION_ID -u CLAUDE_PID -u CLAUDE_CODE_MESSAGING_SOCKET -u CLAUDE_CODE_MESSAGING_TOKEN -u CLAUDE_CODE_SESSION_ATTENDED -u CLAUDE_CODE_ENTRYPOINT CLAUDE_CODE_ENABLE_PROMPT_SUGGESTION=false CLAUDE_CODE_SEND_FEEDBACK=0 claude __CLAUDEPERMFLAG__ __CLAUDEADDDIRS__--settings __CLAUDESETTINGS__ '
if [ "$kind" != secondmate ]; then
printf '%s' "--append-system-prompt '$(spawn_claude_task_channel_statement)' "
fi
Expand Down
10 changes: 7 additions & 3 deletions tests/fm-backend-herdr.test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ case "${1:-}" in
;;
server)
{
for name in FM_HOME FM_ROOT_OVERRIDE FM_STATE_OVERRIDE FM_DATA_OVERRIDE FM_PROJECTS_OVERRIDE FM_CONFIG_OVERRIDE CURSOR_AGENT CURSOR_INVOKED_AS CLAUDECODE PI_CODING_AGENT FM_PI_HARNESS GROK_AGENT FM_SUPERVISION_MODEL FM_HERDR_SENTINEL HERDR_SESSION; do
for name in FM_HOME FM_ROOT_OVERRIDE FM_STATE_OVERRIDE FM_DATA_OVERRIDE FM_PROJECTS_OVERRIDE FM_CONFIG_OVERRIDE CURSOR_AGENT CURSOR_INVOKED_AS CLAUDECODE PI_CODING_AGENT FM_PI_HARNESS GROK_AGENT FM_SUPERVISION_MODEL CLAUDE_CODE_CHILD_SESSION CLAUDE_CODE_SESSION_ID CLAUDE_PID CLAUDE_CODE_MESSAGING_SOCKET CLAUDE_CODE_MESSAGING_TOKEN CLAUDE_CODE_SESSION_ATTENDED CLAUDE_CODE_ENTRYPOINT FM_HERDR_SENTINEL HERDR_SESSION; do
eval 'value=${'"$name"'-<unset>}'
printf '%s=%s\n' "$name" "$value"
done
Expand Down Expand Up @@ -1269,17 +1269,21 @@ test_server_ensure_scrubs_home_and_harness_identity() {
FM_HOME=/tmp/wrong-home FM_ROOT_OVERRIDE=/tmp/wrong-root FM_STATE_OVERRIDE=/tmp/wrong-state \
FM_DATA_OVERRIDE=/tmp/wrong-data FM_PROJECTS_OVERRIDE=/tmp/wrong-projects FM_CONFIG_OVERRIDE=/tmp/wrong-config \
CURSOR_AGENT=1 CURSOR_INVOKED_AS=cursor-agent CLAUDECODE=1 PI_CODING_AGENT=true FM_PI_HARNESS=pi-signed GROK_AGENT=1 FM_SUPERVISION_MODEL=autoarm \
CLAUDE_CODE_CHILD_SESSION=1 CLAUDE_CODE_SESSION_ID=parent-session CLAUDE_PID=4242 CLAUDE_CODE_MESSAGING_SOCKET=/tmp/parent.sock \
CLAUDE_CODE_MESSAGING_TOKEN=parent-token CLAUDE_CODE_SESSION_ATTENDED=1 CLAUDE_CODE_ENTRYPOINT=cli \
bash -c '. "$0/bin/backends/herdr.sh"; fm_backend_herdr_server_ensure fmtest' "$ROOT"
expect_code 0 $? "server_ensure should start under a polluted launcher environment"
output=$(cat "$log")
for name in FM_HOME FM_ROOT_OVERRIDE FM_STATE_OVERRIDE FM_DATA_OVERRIDE FM_PROJECTS_OVERRIDE FM_CONFIG_OVERRIDE \
CURSOR_AGENT CURSOR_INVOKED_AS CLAUDECODE PI_CODING_AGENT FM_PI_HARNESS GROK_AGENT FM_SUPERVISION_MODEL; do
CURSOR_AGENT CURSOR_INVOKED_AS CLAUDECODE PI_CODING_AGENT FM_PI_HARNESS GROK_AGENT FM_SUPERVISION_MODEL \
CLAUDE_CODE_CHILD_SESSION CLAUDE_CODE_SESSION_ID CLAUDE_PID CLAUDE_CODE_MESSAGING_SOCKET CLAUDE_CODE_MESSAGING_TOKEN \
CLAUDE_CODE_SESSION_ATTENDED CLAUDE_CODE_ENTRYPOINT; do
assert_contains "$output" "$name=<unset>" "server_ensure leaked $name into the long-lived Herdr server"
done
assert_contains "$output" "FM_HERDR_SENTINEL=kept" "server_ensure removed an unrelated environment variable"
assert_contains "$output" "HERDR_SESSION=fmtest" "server_ensure lost explicit Herdr session routing"
assert_contains "$output" "args=server --session fmtest" "server_ensure lost the trailing Herdr session flag"
pass "fm_backend_herdr_server_ensure: scrubs home and harness identity without disturbing unrelated environment or session routing"
pass "fm_backend_herdr_server_ensure: scrubs home, harness identity, and a parent Claude session without disturbing unrelated environment or session routing"
}

# run_owned_server_ensure: run server_ensure for fmtest against
Expand Down
33 changes: 33 additions & 0 deletions tests/fm-context-restart.test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -605,6 +605,38 @@ test_wrapper_ordinary_exit_and_resume_refusal() {
pass "context restart: ordinary exit never loops and resume options cannot restore old context"
}

test_wrapper_sheds_parent_claude_session() {
local home fake rc out budget name
for budget in off on; do
home="$TMP_ROOT/parent-session-$budget"
make_primary "$home" 1
[ "$budget" = on ] || rm "$home/config/context-restart-budget"
fake="$home/fake-claude"
cat > "$fake" <<'SCRIPT'
#!/usr/bin/env bash
for name in CLAUDECODE CLAUDE_CODE_CHILD_SESSION CLAUDE_CODE_SESSION_ID CLAUDE_PID CLAUDE_CODE_MESSAGING_SOCKET \
CLAUDE_CODE_MESSAGING_TOKEN CLAUDE_CODE_SESSION_ATTENDED CLAUDE_CODE_ENTRYPOINT FM_PARENT_SENTINEL; do
eval 'value=${'"$name"'-<unset>}'
printf '%s=%s\n' "$name" "$value"
done
exit 0
SCRIPT
chmod +x "$fake"
rc=0
out=$(FM_ROOT_OVERRIDE="$home" FM_HOME="$home" FM_CLAUDE_BIN="$fake" FM_PARENT_SENTINEL=kept \
CLAUDECODE=1 CLAUDE_CODE_CHILD_SESSION=1 CLAUDE_CODE_SESSION_ID=parent-session CLAUDE_PID=4242 \
CLAUDE_CODE_MESSAGING_SOCKET=/tmp/parent.sock CLAUDE_CODE_MESSAGING_TOKEN=parent-token \
CLAUDE_CODE_SESSION_ATTENDED=1 CLAUDE_CODE_ENTRYPOINT=cli "$WRAPPER" 2>&1) || rc=$?
expect_code 0 "$rc" "budget-$budget wrapper must launch Claude under a parent session's environment: $out"
for name in CLAUDECODE CLAUDE_CODE_CHILD_SESSION CLAUDE_CODE_SESSION_ID CLAUDE_PID CLAUDE_CODE_MESSAGING_SOCKET \
CLAUDE_CODE_MESSAGING_TOKEN CLAUDE_CODE_SESSION_ATTENDED CLAUDE_CODE_ENTRYPOINT; do
assert_contains "$out" "$name=<unset>" "budget-$budget wrapper passed the parent session's $name to Claude"
done
assert_contains "$out" "FM_PARENT_SENTINEL=kept" "budget-$budget wrapper removed an unrelated environment variable"
done
pass "context restart: the primary wrapper launches Claude as its own session, never a parent's child"
}

test_wrapper_refuses_incomplete_or_foreign_handoffs() {
local home fake scenario rc expected
for scenario in ready foreign-token foreign-lock; do
Expand Down Expand Up @@ -879,6 +911,7 @@ test_wrapper_waits_for_publication_and_revalidates_ownership
test_opt_out_paths_are_unchanged
test_foreign_hooks_and_handoff_guards
test_wrapper_ordinary_exit_and_resume_refusal
test_wrapper_sheds_parent_claude_session
test_wrapper_refuses_incomplete_or_foreign_handoffs
test_supervision_transfer_and_queued_wakes
test_bridge_continuity_race contention
Expand Down
52 changes: 50 additions & 2 deletions tests/fm-spawn-dispatch-profile.test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2209,7 +2209,7 @@ test_claude_forwards_firstmate_config_dir_when_set() {
status=$?
expect_code 0 "$status" "claude spawn with CLAUDE_CONFIG_DIR set should succeed"
launch=$(cat "$LAUNCH_LOG")
assert_contains "$launch" "CLAUDE_CONFIG_DIR='$CASE_DIR/claude-work' env -u CURSOR_AGENT -u CURSOR_INVOKED_AS -u GEMINI_CLI CLAUDE_CODE_ENABLE_PROMPT_SUGGESTION=false CLAUDE_CODE_SEND_FEEDBACK=0 claude --dangerously-skip-permissions $(claude_worker_add_dirs "$HOME_DIR" "$id")--settings '{\"feedbackDrafts\":\"off\",\"attribution\":{\"commit\":\"\",\"pr\":\"\",\"sessionUrl\":false}}'" \
assert_contains "$launch" "CLAUDE_CONFIG_DIR='$CASE_DIR/claude-work' env -u CURSOR_AGENT -u CURSOR_INVOKED_AS -u GEMINI_CLI ${CLAUDE_PARENT_SESSION_SCRUB}CLAUDE_CODE_ENABLE_PROMPT_SUGGESTION=false CLAUDE_CODE_SEND_FEEDBACK=0 claude --dangerously-skip-permissions $(claude_worker_add_dirs "$HOME_DIR" "$id")--settings '{\"feedbackDrafts\":\"off\",\"attribution\":{\"commit\":\"\",\"pr\":\"\",\"sessionUrl\":false}}'" \
"claude launch did not forward firstmate's CLAUDE_CONFIG_DIR to the crewmate pane"
pass "claude forwards firstmate's CLAUDE_CONFIG_DIR so the crewmate uses the same credential store"
}
Expand Down Expand Up @@ -2960,13 +2960,60 @@ claude_worker_add_dirs() { # <home> <id>
printf '%s ' "--add-dir '$state_real/operational-inbox' --add-dir '$state_real/$2.inbox' --add-dir '$data_real/$2' --add-dir '$root_real/.agents/skills'"
}

# The parent Claude session markers every Claude launch sheds, so a multiplexer
# server started from inside Claude never makes a worker its child session.
CLAUDE_PARENT_SESSION_VARS='CLAUDECODE CLAUDE_CODE_CHILD_SESSION CLAUDE_CODE_SESSION_ID CLAUDE_PID CLAUDE_CODE_MESSAGING_SOCKET CLAUDE_CODE_MESSAGING_TOKEN CLAUDE_CODE_SESSION_ATTENDED CLAUDE_CODE_ENTRYPOINT'
CLAUDE_PARENT_SESSION_SCRUB="env $(for _v in $CLAUDE_PARENT_SESSION_VARS; do printf -- '-u %s ' "$_v"; done)"

claude_expected_launch() { # <launch> <home> <id> <permission-flag>
local doorbell quoted
doorbell=$(claude_launch_brief_arg "$1")
[ "$(printf '%s' "$doorbell" | "$ROOT/bin/fm-operational-input.sh" doorbell-kind)" = launch-brief ] \
|| doorbell="not a launch-brief doorbell"
quoted="'$(printf '%s' "$doorbell" | sed "s/'/'\\\\''/g")'"
printf '%s' "unset FM_TIMEOUT_OWNER_PID FM_EXEC_TIMED_OWNER_PID; export COMPACT_ADVISER_DISABLE=1; $(task_inbox_export "$2" "$3")$(fm_test_launch_git_prefix "$1")env -u CURSOR_AGENT -u CURSOR_INVOKED_AS -u GEMINI_CLI CLAUDE_CODE_ENABLE_PROMPT_SUGGESTION=false CLAUDE_CODE_SEND_FEEDBACK=0 claude $4 $(claude_worker_add_dirs "$2" "$3")--settings '{\"feedbackDrafts\":\"off\",\"attribution\":{\"commit\":\"\",\"pr\":\"\",\"sessionUrl\":false}}' $CLAUDE_CONTROL_CHANNEL_FLAG $quoted"
printf '%s' "unset FM_TIMEOUT_OWNER_PID FM_EXEC_TIMED_OWNER_PID; export COMPACT_ADVISER_DISABLE=1; $(task_inbox_export "$2" "$3")$(fm_test_launch_git_prefix "$1")env -u CURSOR_AGENT -u CURSOR_INVOKED_AS -u GEMINI_CLI ${CLAUDE_PARENT_SESSION_SCRUB}CLAUDE_CODE_ENABLE_PROMPT_SUGGESTION=false CLAUDE_CODE_SEND_FEEDBACK=0 claude $4 $(claude_worker_add_dirs "$2" "$3")--settings '{\"feedbackDrafts\":\"off\",\"attribution\":{\"commit\":\"\",\"pr\":\"\",\"sessionUrl\":false}}' $CLAUDE_CONTROL_CHANNEL_FLAG $quoted"
}

# A multiplexer server started from inside a Claude session hands that
# session's markers to every pane; an inherited CLAUDE_CODE_CHILD_SESSION makes
# the worker a child session that saves no transcript. Run the generated launch
# command itself under that polluted environment with a probe claude.
test_claude_launch_sheds_parent_claude_session() {
local rec id out status launch command probe env_out name kind
for kind in ship secondmate; do
id=parent-session-$kind-z31
rec=$(make_spawn_case "parent-session-$kind" claude "$id")
read_case_record "$rec"
if [ "$kind" = secondmate ]; then
make_seeded_secondmate_home "$CASE_DIR/secondmate-home" "$id"
out=$(run_spawn "$HOME_DIR" "$WT_DIR" "$FAKEBIN_DIR" "$LAUNCH_LOG" "$id" "$CASE_DIR/secondmate-home" --secondmate)
else
out=$(run_ship_spawn "$HOME_DIR" "$WT_DIR" "$FAKEBIN_DIR" "$LAUNCH_LOG" "$id" "$PROJ_DIR")
fi
status=$?
expect_code 0 "$status" "claude $kind spawn should succeed: $out"
launch=$(cat "$LAUNCH_LOG")
command=$(fm_test_launch_command "$launch")
probe="$CASE_DIR/parent-session-probe"
mkdir -p "$probe"
cat > "$probe/claude" <<'SH'
#!/bin/bash
for name in $CLAUDE_PARENT_SESSION_VARS FM_PARENT_SENTINEL; do
eval 'value=${'"$name"'-<unset>}'
printf '%s=%s\n' "$name" "$value"
done
SH
chmod +x "$probe/claude"
env_out=$(PATH="$probe:/usr/bin:/bin" CLAUDE_PARENT_SESSION_VARS="$CLAUDE_PARENT_SESSION_VARS" FM_PARENT_SENTINEL=kept \
CLAUDECODE=1 CLAUDE_CODE_CHILD_SESSION=1 CLAUDE_CODE_SESSION_ID=parent-session CLAUDE_PID=4242 \
CLAUDE_CODE_MESSAGING_SOCKET=/tmp/parent.sock CLAUDE_CODE_MESSAGING_TOKEN=parent-token \
CLAUDE_CODE_SESSION_ATTENDED=1 CLAUDE_CODE_ENTRYPOINT=cli /bin/bash -c "$command" 2>&1)
assert_contains "$env_out" "FM_PARENT_SENTINEL=kept" "claude $kind launch did not run the probe or dropped unrelated environment: $env_out"
for name in $CLAUDE_PARENT_SESSION_VARS; do
assert_contains "$env_out" "$name=<unset>" "claude $kind launch passed the parent session's $name to the worker"
done
done
pass "claude worker and secondmate launches shed an inherited parent Claude session"
}

test_claude_permission_mode_absent_defaults_to_bypass() {
Expand Down Expand Up @@ -3307,6 +3354,7 @@ test_lavish_server_address_is_exported_to_worker_launch
test_lavish_absent_config_preserves_destination_ambient
test_claude_omits_config_dir_prefix_when_unset
test_claude_permission_mode_absent_defaults_to_bypass
test_claude_launch_sheds_parent_claude_session
test_claude_permission_mode_auto_opts_into_model_reviewed_launch
test_claude_permission_mode_bypass_swaps_only_the_permission_flag
test_claude_permission_mode_auto_reaches_scout_launch
Expand Down
Loading