@@ -938,7 +938,7 @@ def test_existing_invalid_state_root_is_not_absent_intent(restart_box, kind):
938938 assert not box .claude_runs ()
939939
940940
941- @pytest .mark .parametrize ("extra" , ["future_key: preserve-me\n " , "garbage\n " , "profile:test\n " ,
941+ @pytest .mark .parametrize ("extra" , ["future_key: preserve-me\n " , "garbage\n " , "profile:test\n " , " \n " ,
942942 "schema: 1\n " , "prepared_digest: none\n " ,
943943 "pane: testsess:@1.%1\t ignored\n " ])
944944def test_restart_strict_schema_preserves_unrecognized_or_malformed_records (restart_box , extra ):
@@ -1189,3 +1189,81 @@ def test_new_restart_explicitly_clears_previous_operations_profile(restart_box):
11891189 assert record ["profile" ] == "none"
11901190 assert record ["operation" ] != "op-test"
11911191 assert "respawn-pane" in box .tmux_log .read_text ()
1192+
1193+
1194+ @pytest .mark .parametrize ("claim" , ["success" , "lost" ])
1195+ def test_signal_during_claim_publication_reconciles_only_owned_attempt (restart_box , claim ):
1196+ box = restart_box
1197+ prepare_restart (box )
1198+ before = intent_path (box ).read_bytes ()
1199+ real = box .bin / "lanes-edit-real.sh"
1200+ (box .bin / "lanes-edit.sh" ).rename (real )
1201+ action = f"'{ real } ' \" $@\" ; result=$?" if claim == "success" else "result=7"
1202+ _write (box .bin / "lanes-edit.sh" , f"""#!/usr/bin/env bash
1203+ case "$*" in
1204+ *"supervisor claimed the operation"*)
1205+ { action }
1206+ kill -TERM "$PPID"
1207+ exit "$result" ;;
1208+ esac
1209+ exec '{ real } ' "$@"
1210+ """ )
1211+ result = supervisor (box , "--supervise" , "--lane" , LANE , "--operation" , "op-test" )
1212+ assert result .returncode == 143 , (result .stdout , result .stderr )
1213+ assert not box .claude_runs ()
1214+ if claim == "lost" :
1215+ assert intent_path (box ).read_bytes () == before
1216+ assert "before the restart claim" in result .stdout
1217+ else :
1218+ record = dict (line .split ("\t " , 1 ) for line in helper (box , "restart-intent" , LANE ).stdout .splitlines ())
1219+ assert record ["state" ] == "failed" and record ["attempt" ] == "1"
1220+ assert record ["generation" ] == "7" and record ["operation" ] == "op-test"
1221+ assert "no child was launched" in record ["reason" ]
1222+ assert "retry:" in result .stdout
1223+
1224+
1225+ def test_handoff_target_is_resolved_after_waiting_for_writer_mutex (restart_box ):
1226+ box = restart_box
1227+ original = prepare_restart (box )
1228+ assert helper (box , "set-restart-intent" , LANE , "ready" , "--expect" , "pending" ).returncode == 0
1229+ other = _write (box .wip / "handoffs" / "other.md" , "other preserved work\n " , 0o600 )
1230+ alias = box .wip / "handoffs" / "alias.md"
1231+ alias .symlink_to (original .name )
1232+ prepared = _write (box .root / "prepared.md" , "replacement bytes\n " , 0o600 )
1233+ digest = hashlib .sha256 (original .read_bytes ()).hexdigest ()
1234+ before = (original .read_bytes (), other .read_bytes ())
1235+ lock = box .wip / "lanes" / ".lanes-edit.lock"
1236+ lock .mkdir ()
1237+ (lock / "pid" ).write_text (str (os .getpid ()) + "\n " )
1238+ marker = box .root / "waiting-for-writer"
1239+ real_mkdir = shutil .which ("mkdir" )
1240+ _write (box .fakebin / "mkdir" , f"""#!/usr/bin/env bash
1241+ case "$*" in *.lanes-edit.lock*) touch '{ marker } ' ;; esac
1242+ exec '{ real_mkdir } ' "$@"
1243+ """ )
1244+ proc = subprocess .Popen ([str (box .bin / "lanes-edit.sh" ), "publish-handoff" , LANE ,
1245+ str (alias ), str (prepared ), "--expect-digest" , digest ],
1246+ env = box .env , cwd = box .lane_dir , stdout = subprocess .PIPE ,
1247+ stderr = subprocess .PIPE , text = True )
1248+ try :
1249+ deadline = time .monotonic () + 10
1250+ while not marker .exists () and time .monotonic () < deadline :
1251+ assert proc .poll () is None
1252+ time .sleep (.02 )
1253+ assert marker .exists ()
1254+ alias .unlink ()
1255+ alias .symlink_to (other .name )
1256+ (lock / "pid" ).unlink ()
1257+ lock .rmdir ()
1258+ out , err = proc .communicate (timeout = 15 )
1259+ assert proc .returncode == 7 , (out , err )
1260+ assert "changed before publication" in err
1261+ assert (original .read_bytes (), other .read_bytes ()) == before
1262+ assert alias .is_symlink () and alias .resolve () == other
1263+ finally :
1264+ if lock .exists ():
1265+ (lock / "pid" ).unlink (missing_ok = True )
1266+ lock .rmdir ()
1267+ if proc .poll () is None :
1268+ proc .terminate ()
1269+ proc .communicate (timeout = 5 )
0 commit comments