diff --git a/promptpilot/merge_done_proof.py b/promptpilot/merge_done_proof.py index f38dca1..df34732 100644 --- a/promptpilot/merge_done_proof.py +++ b/promptpilot/merge_done_proof.py @@ -342,7 +342,7 @@ def _history(gh, config, cp, pr, main): intents = [n for n in comments if pp.comment_id(n) == iid] _need(len(intents) == 1, "missing checkpoint intent") saved = intents[0] - match = lineage._INTENT.fullmatch(saved["body"]) + match = lineage.intent_line(saved["body"]) _proven(match and match[1] == old, "invalid checkpoint intent") intent_base, review, claim, completion, ship, previous = match.groups()[1:] fields = f"from={old} to={head} base={base} previous={previous} ship-event={ship}" @@ -445,6 +445,7 @@ def _history(gh, config, cp, pr, main): previous != "none" and ( lineage._INTENT.fullmatch(protocol) + or lineage.intent_line(n["body"]) or lineage._DONE.fullmatch(protocol) ), "unknown protocol marker in source epoch", diff --git a/promptpilot/merge_finish_proof.py b/promptpilot/merge_finish_proof.py index dd4420a..c5179f4 100644 --- a/promptpilot/merge_finish_proof.py +++ b/promptpilot/merge_finish_proof.py @@ -11,6 +11,7 @@ import subprocess from . import base_sync_carry, project_pipeline as pp +from .merge_ship_authorization import intent_line from .review_routing_policy import require_legacy @@ -225,9 +226,10 @@ def _history(gh, config, checkpoint): if len(intents) != 1 or len(dones) > 1: raise OutsideScope("terminal route requires a unique first-hop intent and at most one done") saved = intents[0] + marker = intent_line(saved["body"]) _need( pp.comment_id(saved) == intent - and saved["body"] == intent_body + and marker and marker[0] == intent_body and saved["lastEditedAt"] is None and saved["author"]["login"] == TRUSTED, "checkpoint differs from canonical intent", @@ -338,16 +340,10 @@ def observe_delivery(gh, record, *, config, expected_node_id=None): ): return None general = record.get("proof_kind") == "legacy-general-done-v1" - intent_base = "[0-9a-f]{40}" if general else base - previous_pattern = r"(none|[1-9][0-9]*)" if general else "(none)" - match = re.fullmatch( - rf"", - saved["body"], - ) - if not match: + marker = intent_line(saved["body"]) + if not marker or marker[1] != old or (not general and (marker[2] != base or marker[7] != "none")): return None - ship, previous = match.groups() + ship, previous = marker[6], marker[7] expected = f"{prefix}from={old} to={head} base={base} previous={previous} ship-event={ship} -->" order = [n.get("id") for n in nodes] if ( diff --git a/promptpilot/merge_ship_authorization.py b/promptpilot/merge_ship_authorization.py index 727630c..ad7c706 100644 --- a/promptpilot/merge_ship_authorization.py +++ b/promptpilot/merge_ship_authorization.py @@ -51,6 +51,19 @@ def _protocol_body(body): return '\n'.join(lines) +def intent_line(body): + """Read an intent alongside prose. Callers retain the entire body for bytewise readback.""" + # The whole comment is removed when reconstructing the previous epoch. + # Never remove another transaction, a review reset, or an HTML example with it. + if (body.count('" + ) + assert hashlib.sha256(actual.encode()).hexdigest() == "4c5c522e4213d3a4b05f51d061c8ac7cf326b2dcb4aa809db1344f5e6c48ea65" + assert lineage.intent_line(actual)[0] == actual.split("\n")[1] + intent = replay.edges[-2]["node"] + body, _, fingerprint = replay.prepare() + intent["body"] = INTENT_PREAMBLE + INTENT + observed, receipt, with_preamble = replay.prepare() + assert observed == body and receipt is None and with_preamble != fingerprint + replay.add_hop() + replay.edges[-2]["node"]["body"] = INTENT_PREAMBLE + replay.edges[-2]["node"]["body"] + assert "previous=888" in replay.prepare()[0] and not replay.posts + + +@pytest.mark.parametrize("body", [ + "> " + INTENT, " " + INTENT, "`" + INTENT + "`", "~~~md\n" + INTENT + "\n~~~", + "````\n```\n" + INTENT + "\n````", "
\n" + INTENT + "\n
", + INTENT + "\n" + INTENT, INTENT + "\n", + INTENT + "\n" + DONE, INTENT + "\npp:review-again", INTENT.replace("claim=102", "claim=0102"), + INTENT.replace("from=" + OLD, "from=" + HEAD), +]) +def test_one_standalone_intent_required_before_recovery(replay, body): + replay.edges[-2]["node"]["body"] = body + with pytest.raises(pp.PipelineError): + replay.prepare() + assert not replay.posts + + +def test_full_preparation_requires_identical_explanatory_text_in_rest(replay, monkeypatch): + replay.edges[-2]["node"]["body"] = INTENT_PREAMBLE + INTENT + get = replay.json + + def drift(verb, path, *args, **kwargs): + value = get(verb, path, *args, **kwargs) + if path.endswith("/issues/comments/164"): + return dict(value, body="Different explanation.\n" + INTENT) + return value + + monkeypatch.setattr(replay, "json", drift) + with pytest.raises(pp.PipelineError, match="exact UTF-8 readback"): + replay.prepare() + assert not replay.posts + + @pytest.mark.parametrize("state", ["failure", None, "absent"]) def test_new_ci_is_observed_even_before_it_passes(replay, state): green = replay.prepare()[2] diff --git a/tests/test_merge_finish_proof.py b/tests/test_merge_finish_proof.py index 189da48..793d5eb 100644 --- a/tests/test_merge_finish_proof.py +++ b/tests/test_merge_finish_proof.py @@ -39,6 +39,7 @@ f"" ) +INTENT_PREAMBLE = "PromptPilot service marker: MERGE base synchronization intent.\n" DONE = ( f"" ) @@ -243,6 +244,40 @@ def test_confirm_and_recover_existing_done(replay): assert replay.posts == [{"body": DONE}] and len(replay.gates) == 1 +def test_2731_service_comment_and_its_uncertain_delivery_keep_exact_bytes(replay, monkeypatch): + intent = replay.edges[-2]["node"] + intent["body"] = INTENT_PREAMBLE + INTENT + replay.drop_post = True + with pytest.raises(pp.PipelineError, match="lost"): + replay.run() + assert replay.run()["published"] is False and replay.posts == [{"body": DONE}] + record = dict(CHECKPOINT, body_sha256=hashlib.sha256(DONE.encode()).hexdigest()) + assert finish.observe_receipt(replay, record, config=CONFIG) == 888 + get = replay.json + + def drift(verb, path, *args, **kwargs): + value = get(verb, path, *args, **kwargs) + if path.endswith("/issues/comments/164"): + return dict(value, body=INTENT) + return value + + monkeypatch.setattr(replay, "json", drift) + with pytest.raises(pp.PipelineError, match="exact UTF-8 readback"): + finish.observe_receipt(replay, record, config=CONFIG) + with pytest.raises(pp.PipelineError, match="exact UTF-8 readback"): + replay.run() + assert replay.posts == [{"body": DONE}] + + +def test_only_the_explanatory_line_changes_across_final_source(replay): + intent = replay.edges[-2]["node"] + intent["body"] = INTENT_PREAMBLE + INTENT + replay.on_source = lambda: intent.update(body="Another description.\n" + INTENT) + with pytest.raises(pp.PipelineError, match="changed across final Source"): + replay.run() + assert not replay.posts and not replay.past_send + + @pytest.mark.parametrize( "change", [