From 660d170ff388bdb63bf0ba5cffd2a8f492247983 Mon Sep 17 00:00:00 2001 From: Alex Date: Sun, 27 Sep 2026 10:12:34 +0000 Subject: [PATCH 01/45] Fix conversation projection endpoint --- src/chatgpt_web2api/backend_client.py | 3 +-- src/chatgpt_web2api/backend_projection.py | 2 +- tests/test_backend_projection_js.py | 6 +++--- 3 files changed, 5 insertions(+), 6 deletions(-) diff --git a/src/chatgpt_web2api/backend_client.py b/src/chatgpt_web2api/backend_client.py index 4ea582bb..e2f06d00 100644 --- a/src/chatgpt_web2api/backend_client.py +++ b/src/chatgpt_web2api/backend_client.py @@ -276,7 +276,7 @@ async def _fetch_recent_conversation_projection( on 401 (with breaker trip), ``_Transient404`` on 404, ``RuntimeError`` on other non-OK status, and ``CDPJSError`` on transport failure. """ - from .backend_projection import CONVERSATION_PROJECTION_JS, TURN_PROJECTION_LIMIT + from .backend_projection import CONVERSATION_PROJECTION_JS from .cdp_driver import AuthExpiredError, CDPJSError d = self._driver @@ -286,7 +286,6 @@ async def _fetch_recent_conversation_projection( { "conv_id": conversation_id, "token": d._access_token, - "limit": TURN_PROJECTION_LIMIT, }, timeout=15, ) diff --git a/src/chatgpt_web2api/backend_projection.py b/src/chatgpt_web2api/backend_projection.py index 6f40b9a7..4215cb59 100644 --- a/src/chatgpt_web2api/backend_projection.py +++ b/src/chatgpt_web2api/backend_projection.py @@ -41,7 +41,7 @@ CONVERSATION_PROJECTION_JS = """ (async function() { try { - var r = await fetch('/backend-api/conversation/' + __D.conv_id + '?offset=0&limit=' + __D.limit, { + var r = await fetch('/backend-api/conversation/' + __D.conv_id, { headers: {'Authorization': 'Bearer ' + __D.token} }); if (!r.ok) return JSON.stringify({__status: r.status}); diff --git a/tests/test_backend_projection_js.py b/tests/test_backend_projection_js.py index c7df08ec..6dc7c499 100644 --- a/tests/test_backend_projection_js.py +++ b/tests/test_backend_projection_js.py @@ -34,13 +34,13 @@ def test_js_is_nonempty_string(self): def test_js_fetches_correct_endpoint(self): assert "/backend-api/conversation/" in CONVERSATION_PROJECTION_JS - assert "offset=0" in CONVERSATION_PROJECTION_JS - assert "limit=" in CONVERSATION_PROJECTION_JS + assert "?offset=" not in CONVERSATION_PROJECTION_JS + assert "&limit=" not in CONVERSATION_PROJECTION_JS def test_js_threads_data_slots(self): assert "__D.conv_id" in CONVERSATION_PROJECTION_JS assert "__D.token" in CONVERSATION_PROJECTION_JS - assert "__D.limit" in CONVERSATION_PROJECTION_JS + assert "__D.limit" not in CONVERSATION_PROJECTION_JS def test_js_status_decode(self): assert "__status" in CONVERSATION_PROJECTION_JS From 4dcebbe492c5caeea5e712534d51ccaf7df9fc86 Mon Sep 17 00:00:00 2001 From: Alex Date: Sun, 27 Sep 2026 10:17:52 +0000 Subject: [PATCH 02/45] Support current ChatGPT turn role selectors --- src/chatgpt_web2api/cdp_driver.py | 6 +++--- src/chatgpt_web2api/completion_detector.py | 4 ++-- tests/test_current_dom_role_selectors.py | 9 +++++++++ 3 files changed, 14 insertions(+), 5 deletions(-) create mode 100644 tests/test_current_dom_role_selectors.py diff --git a/src/chatgpt_web2api/cdp_driver.py b/src/chatgpt_web2api/cdp_driver.py index 3bc3eae4..ff104df3 100644 --- a/src/chatgpt_web2api/cdp_driver.py +++ b/src/chatgpt_web2api/cdp_driver.py @@ -1499,12 +1499,12 @@ async def _read_assistant_count_baseline(self) -> int: selector = ( "document.querySelectorAll(" - "'[data-message-author-role=\"assistant\"]'" + "'[data-message-author-role=\"assistant\"], [data-content-search-unit-key$=\":assistant\"], [data-chatgpt-search-unit-key$=\":assistant\"]'" ").length" ) user_selector = ( "document.querySelectorAll(" - "'[data-message-author-role=\"user\"]'" + "'[data-message-author-role=\"user\"], [data-content-search-unit-key$=\":user\"], [data-chatgpt-search-unit-key$=\":user\"]'" ").length" ) max_attempts = 3 @@ -1610,7 +1610,7 @@ async def _verify_send_acknowledged(self) -> bool | None: result = await self._js_strict( "(function() {" " var userMsgs = document.querySelectorAll(" - " '[data-message-author-role=\"user\"]').length;" + " '[data-message-author-role=\"user\"], [data-content-search-unit-key$=\":user\"], [data-chatgpt-search-unit-key$=\":user\"]').length;" f" var composer = document.querySelector('{COMPOSER_SELECTOR}')" f" || document.querySelector('{COMPOSER_FALLBACK_SELECTOR}');" " var composerPresent = !!composer;" diff --git a/src/chatgpt_web2api/completion_detector.py b/src/chatgpt_web2api/completion_detector.py index c5a47d8f..237973d5 100644 --- a/src/chatgpt_web2api/completion_detector.py +++ b/src/chatgpt_web2api/completion_detector.py @@ -377,7 +377,7 @@ async def stream_until_complete( try: raw = await d._js_strict( - "document.querySelectorAll('[data-message-author-role=\"assistant\"]').length" + "document.querySelectorAll('[data-message-author-role=\"assistant\"], [data-content-search-unit-key$=\":assistant\"], [data-chatgpt-search-unit-key$=\":assistant\"]').length" ) current_count = int(raw or 0) except CDPJSError: @@ -474,7 +474,7 @@ async def stream_until_complete( try: result = await d._js_strict( "(function() {" - " var msgs = document.querySelectorAll('[data-message-author-role=\"assistant\"]');" + " var msgs = document.querySelectorAll('[data-message-author-role=\"assistant\"], [data-content-search-unit-key$=\":assistant\"], [data-chatgpt-search-unit-key$=\":assistant\"]');" " if (!msgs.length) return JSON.stringify({text:'', md_text:'', html_len:0, child_count:0, has_action:false, is_thinking:false});" " var last = msgs[msgs.length - 1];" # Text: the clean answer lives in ``.markdown`` textContent. diff --git a/tests/test_current_dom_role_selectors.py b/tests/test_current_dom_role_selectors.py new file mode 100644 index 00000000..74674b4d --- /dev/null +++ b/tests/test_current_dom_role_selectors.py @@ -0,0 +1,9 @@ +import inspect +from chatgpt_web2api.cdp_driver import CDPDriver +from chatgpt_web2api.completion_detector import CompletionDetector + +def test_current_chatgpt_role_selectors_are_supported(): + src = inspect.getsource(CDPDriver) + inspect.getsource(CompletionDetector) + assert "data-content-search-unit-key$=\\\":assistant\\\"" in src + assert "data-content-search-unit-key$=\\\":user\\\"" in src + assert "data-message-author-role" in src From b108a913cdd08c8c0e179713e7a1c1a0fd41b0e2 Mon Sep 17 00:00:00 2001 From: Alajmah <55954113+Alajmah@users.noreply.github.com> Date: Fri, 20 Nov 2026 11:10:26 +0300 Subject: [PATCH 03/45] feat(dom): centralize live aria-label and legacy per-turn counters --- src/chatgpt_web2api/dom_turn_signals.py | 36 +++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 src/chatgpt_web2api/dom_turn_signals.py diff --git a/src/chatgpt_web2api/dom_turn_signals.py b/src/chatgpt_web2api/dom_turn_signals.py new file mode 100644 index 00000000..11eda059 --- /dev/null +++ b/src/chatgpt_web2api/dom_turn_signals.py @@ -0,0 +1,36 @@ +"""Current and legacy ChatGPT DOM turn signals. + +On 2026-11-20 the live UI rendered one aria-label=Copy per completed +assistant turn and one aria-label=Edit message per user turn. Legacy +message role/search-unit attributes remain as compatibility fallbacks. + +Counts are observations, not proof of remote acceptance or turn identity. +The caller still uses the anchored backend end_turn reconciliation. +""" + +COPY_BUTTON_SELECTOR = 'button[aria-label="Copy"]' +EDIT_MESSAGE_BUTTON_SELECTOR = 'button[aria-label="Edit message"]' +LEGACY_ASSISTANT_SELECTOR = ( + '[data-message-author-role="assistant"], ' + '[data-content-search-unit-key$=":assistant"], ' + '[data-chatgpt-search-unit-key$=":assistant"]' +) +LEGACY_USER_SELECTOR = ( + '[data-message-author-role="user"], ' + '[data-content-search-unit-key$=":user"], ' + '[data-chatgpt-search-unit-key$=":user"]' +) + +# Avoid double counting transitional pages that expose both representations. +ASSISTANT_TURN_COUNT_JS = ( + "Math.max(" + f"document.querySelectorAll('{COPY_BUTTON_SELECTOR}').length, " + f"document.querySelectorAll('{LEGACY_ASSISTANT_SELECTOR}').length" + ")" +) +USER_TURN_COUNT_JS = ( + "Math.max(" + f"document.querySelectorAll('{EDIT_MESSAGE_BUTTON_SELECTOR}').length, " + f"document.querySelectorAll('{LEGACY_USER_SELECTOR}').length" + ")" +) From 9bd5f86bcc16c16b1233ce3f29ff12378456a90a Mon Sep 17 00:00:00 2001 From: Alajmah <55954113+Alajmah@users.noreply.github.com> Date: Fri, 20 Nov 2026 11:10:46 +0300 Subject: [PATCH 04/45] fix(dom): use aria/legacy assistant and user counters at send and acknowledgment baselines --- src/chatgpt_web2api/cdp_driver.py | 22 +++++++++------------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/src/chatgpt_web2api/cdp_driver.py b/src/chatgpt_web2api/cdp_driver.py index ff104df3..65212b43 100644 --- a/src/chatgpt_web2api/cdp_driver.py +++ b/src/chatgpt_web2api/cdp_driver.py @@ -77,6 +77,10 @@ class StreamChunk: # Canonical home moved to chatgpt_dom.py in Phase 5 PR3; re-exported here for # back-compat (tests import these from cdp_driver, and the navigation methods # that stay here still reference them). +from .dom_turn_signals import ( # noqa: E402 + ASSISTANT_TURN_COUNT_JS, + USER_TURN_COUNT_JS, +) from .chatgpt_dom import ( # noqa: E402,F401 COMPOSER_FALLBACK_SELECTOR, COMPOSER_SELECTOR, @@ -1497,16 +1501,8 @@ async def _read_assistant_count_baseline(self) -> int: """ import time as _time - selector = ( - "document.querySelectorAll(" - "'[data-message-author-role=\"assistant\"], [data-content-search-unit-key$=\":assistant\"], [data-chatgpt-search-unit-key$=\":assistant\"]'" - ").length" - ) - user_selector = ( - "document.querySelectorAll(" - "'[data-message-author-role=\"user\"], [data-content-search-unit-key$=\":user\"], [data-chatgpt-search-unit-key$=\":user\"]'" - ").length" - ) + selector = ASSISTANT_TURN_COUNT_JS + user_selector = USER_TURN_COUNT_JS max_attempts = 3 for attempt in range(1, max_attempts + 1): t0 = _time.monotonic() @@ -1596,7 +1592,8 @@ async def _verify_send_acknowledged(self) -> bool | None: Polls briefly (3s at 0.5s intervals). Never raises. """ import time as _time - from .chatgpt_dom import COMPOSER_SELECTOR, COMPOSER_FALLBACK_SELECTOR + + from .chatgpt_dom import COMPOSER_FALLBACK_SELECTOR, COMPOSER_SELECTOR pre_send_count = getattr(self, "_pre_send_user_count", None) if pre_send_count is None: @@ -1609,8 +1606,7 @@ async def _verify_send_acknowledged(self) -> bool | None: try: result = await self._js_strict( "(function() {" - " var userMsgs = document.querySelectorAll(" - " '[data-message-author-role=\"user\"], [data-content-search-unit-key$=\":user\"], [data-chatgpt-search-unit-key$=\":user\"]').length;" + f" var userMsgs = {USER_TURN_COUNT_JS};" f" var composer = document.querySelector('{COMPOSER_SELECTOR}')" f" || document.querySelector('{COMPOSER_FALLBACK_SELECTOR}');" " var composerPresent = !!composer;" From 236ff6b68a0e752716324c25a8a44adb673fb4af Mon Sep 17 00:00:00 2001 From: Alajmah <55954113+Alajmah@users.noreply.github.com> Date: Fri, 20 Nov 2026 11:12:10 +0300 Subject: [PATCH 05/45] fix(detector): detect current aria user/Copy deltas with anchored and legacy fallback --- src/chatgpt_web2api/completion_detector.py | 89 +++++++++++++++++++--- 1 file changed, 79 insertions(+), 10 deletions(-) diff --git a/src/chatgpt_web2api/completion_detector.py b/src/chatgpt_web2api/completion_detector.py index 237973d5..a4c09790 100644 --- a/src/chatgpt_web2api/completion_detector.py +++ b/src/chatgpt_web2api/completion_detector.py @@ -73,6 +73,13 @@ from .cdp_driver import StreamChunk from .config import ChatGPTConfig +from .dom_turn_signals import ( + ASSISTANT_TURN_COUNT_JS, + COPY_BUTTON_SELECTOR, + LEGACY_ASSISTANT_SELECTOR, + USER_TURN_COUNT_JS, +) + logger = logging.getLogger(__name__) @@ -376,9 +383,7 @@ async def stream_until_complete( raise RateLimitError.from_text(scanned_text) try: - raw = await d._js_strict( - "document.querySelectorAll('[data-message-author-role=\"assistant\"], [data-content-search-unit-key$=\":assistant\"], [data-chatgpt-search-unit-key$=\":assistant\"]').length" - ) + raw = await d._js_strict(ASSISTANT_TURN_COUNT_JS) current_count = int(raw or 0) except CDPJSError: current_count = last_node_count # no progress signal @@ -389,6 +394,21 @@ async def stream_until_complete( last_progress = time.monotonic() if current_count > initial_count: break + + # The current UI only adds the per-turn Copy affordance when + # the assistant FINISHES, so an assistant-count delta alone cannot + # start phase 2. The new user message's Edit message control is + # available earlier. It permits observation, NOT completion. + pre_user_count = getattr(d, "_pre_send_user_count", None) + if pre_user_count is not None: + try: + user_raw = await d._js_strict(USER_TURN_COUNT_JS) + if int(user_raw) > pre_user_count: + logger.debug("User turn appeared via Edit-message delta") + break + except (CDPJSError, ValueError, TypeError): + pass + if time.monotonic() - last_progress > PHASE_STALL_SECONDS: raise GenerationStuckError("phase_1_appear", time.monotonic() - last_progress) await asyncio.sleep(0.5) @@ -453,6 +473,12 @@ async def stream_until_complete( # think silently); stream-idle uses budgets.stream_idle_timeout_seconds. phase_2_start = time.monotonic() first_content_seen = False + # A current-DOM Copy affordance appears only after a completed turn. + # Require its count to grow beyond the pre-send baseline AND one + # subsequent identical-text observation before enabling DOM fallback. + # An old Copy button may never mark the new turn complete. + last_copy_text = "" + copy_stable_polls = 0 generation_active_signal = False # advisory liveness (DOM thinking/generating) # Backend end_turn fallback throttle (R4): if the DOM action-button # selector drifts again, the conversation API's end_turn flag is a @@ -474,9 +500,22 @@ async def stream_until_complete( try: result = await d._js_strict( "(function() {" - " var msgs = document.querySelectorAll('[data-message-author-role=\"assistant\"], [data-content-search-unit-key$=\":assistant\"], [data-chatgpt-search-unit-key$=\":assistant\"]');" - " if (!msgs.length) return JSON.stringify({text:'', md_text:'', html_len:0, child_count:0, has_action:false, is_thinking:false});" - " var last = msgs[msgs.length - 1];" + f" var msgs = document.querySelectorAll('{LEGACY_ASSISTANT_SELECTOR}');" + f" var copyButtons = document.querySelectorAll('{COPY_BUTTON_SELECTOR}');" + f" var copyDelta = copyButtons.length > {int(0)} + {initial_count};" + # The old assistant node must not be read while a new + # USER node has appeared but no new assistant node yet. + f" var last = msgs.length > {initial_count} ? msgs[msgs.length - 1] : null;" + # New layout: the completed Copy affordance anchors a + # semantic turn container; never fall back to body text. + " if (!last && copyDelta) {" + " var copyBtn = copyButtons[copyButtons.length - 1];" + ' var root = copyBtn.closest(\'[data-testid^="conversation-turn-"], article\');' + " if (root) last = root;" + " }" + " var stops = document.querySelectorAll('[data-testid=\\\"stop-button\\\"], button[aria-label*=\\\"Stop\\\" i]');" + " var stopPresent = Array.prototype.some.call(stops, function(b){return b.getClientRects().length > 0;});" + " if (!last) return JSON.stringify({text:'', md_text:'', html_len:0, child_count:0, has_action:false, copy_delta:copyDelta, stop_present:stopPresent, is_thinking:false});" # Text: the clean answer lives in ``.markdown`` textContent. # It's empty during streaming and populates as the turn # settles — so we ALSO capture ``innerText`` (populated @@ -484,7 +523,7 @@ async def stream_until_complete( # reasoning UI label ("Thinking.../Thought for N seconds"), # so md_text is captured SEPARATELY and Python prefers it; # the innerText fallback is trimmed of the leading label. - " var md = last.querySelector('.markdown');" + " var md = last.querySelector('.markdown, .prose, [data-message-content]');" " var mdText = md ? (md.textContent || '') : '';" " var rawText = (last.innerText || '').trim();" # Strip a leading "Thinking..." / "Thought for …" reasoning @@ -519,7 +558,7 @@ async def stream_until_complete( ' [data-testid*="turn-action-button"],' ' [data-testid*="copy"],' ' [data-testid*="response-turn"]\';' - " var has_action = (function() {" + " var legacy_action = (function() {" " var lastRect = last.getBoundingClientRect();" " var scope = last;" " for (var d = 0; scope && d <= 8; d++, scope = scope.parentElement) {" @@ -555,7 +594,8 @@ async def stream_until_complete( " var hasThinkingEl = !!last.querySelector('.result-thinking');" " var visibleThinking = /^(thinking|reasoning)\\b/i.test(rawText.trim());" " var is_thinking = !has_action && (hasThinkingEl || (visibleThinking && !mdText));" - " return JSON.stringify({text: text, md_text: mdText, html_len: html_len, child_count: child_count, has_action: has_action, is_thinking: is_thinking});" + " var has_action = legacy_action || copyDelta;" + " return JSON.stringify({text: text, md_text: mdText, html_len: html_len, child_count: child_count, has_action: has_action, copy_delta:copyDelta, stop_present:stopPresent, is_thinking: is_thinking});" "})()", ) data = json.loads(result) @@ -568,6 +608,8 @@ async def stream_until_complete( html_len = data.get("html_len", 0) child_count = data.get("child_count", 0) has_action = data.get("has_action", False) + copy_delta = data.get("copy_delta", False) + stop_present = data.get("stop_present", False) is_thinking = data.get("is_thinking", False) # Streaming source: prefer the clean .markdown answer container @@ -577,6 +619,20 @@ async def stream_until_complete( # in JS) is what carries the streamed answer. current = md_text or current + if copy_delta: + if current and current == last_copy_text and not stop_present: + copy_stable_polls += 1 + else: + copy_stable_polls = 0 + last_copy_text = current + # A Copy delta is a terminal DOM signal only after one extra + # same-text poll, with Stop absent as a prerequisite. Do not + # use Stop *absence* as proof of completion. + has_action = bool(current) and copy_stable_polls >= 1 and not stop_present + else: + copy_stable_polls = 0 + last_copy_text = "" + # is_thinking means the model is actively reasoning — the DOM is # legitimately static for tens of seconds, which is NOT a stall. # It MUST reset the stall clock so a genuine reasoning phase isn't @@ -669,7 +725,7 @@ async def stream_until_complete( backend_fetch_failed = False if ( conv_id_for_check - and (last_dom_text or saw_thinking or is_thinking or had_non_text_content) + and (last_dom_text or saw_thinking or is_thinking or had_non_text_content or copy_delta) and time.monotonic() - last_backend_check > 3.0 ): last_backend_check = time.monotonic() @@ -687,6 +743,18 @@ async def stream_until_complete( ) status = collapse_to_end_turn_status(end_result) if status == "complete": + # If the current markup has no usable text-bearing + # assistant container, read the ANCHORED backend text. + # This is observation-only; no message is resent. + # Do not report success based on the Copy icon alone. + if not last_dom_text and not had_non_text_content: + text_result = await d._fetch_text_for_turn( + conv_id_for_check, turn_anchor, + ) + if text_result.status == "matched" and text_result.text: + yield StreamChunk(delta=text_result.text) + last_dom_text = text_result.text + self.last_dom_text = last_dom_text # STRICT: end_turn AND usable content. The saw_thinking # unlock lets us CONSULT the backend during thinking, but # we must not finish on a bare end_turn with no answer. @@ -732,6 +800,7 @@ async def stream_until_complete( has_action and (not conv_id_for_check or backend_fetch_failed) and (last_dom_text or had_non_text_content) + and not stop_present ): logger.info("DOM has_action (fallback completion) — no backend signal") break From d4c2b275399ba77e9d4aa680829af497369d5365 Mon Sep 17 00:00:00 2001 From: Alajmah <55954113+Alajmah@users.noreply.github.com> Date: Fri, 20 Nov 2026 11:13:20 +0300 Subject: [PATCH 06/45] fix(detector): initialize action before thinking and reconcile text through backend --- src/chatgpt_web2api/completion_detector.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/chatgpt_web2api/completion_detector.py b/src/chatgpt_web2api/completion_detector.py index a4c09790..1cfdd23a 100644 --- a/src/chatgpt_web2api/completion_detector.py +++ b/src/chatgpt_web2api/completion_detector.py @@ -502,7 +502,7 @@ async def stream_until_complete( "(function() {" f" var msgs = document.querySelectorAll('{LEGACY_ASSISTANT_SELECTOR}');" f" var copyButtons = document.querySelectorAll('{COPY_BUTTON_SELECTOR}');" - f" var copyDelta = copyButtons.length > {int(0)} + {initial_count};" + f" var copyDelta = copyButtons.length > {initial_count};" # The old assistant node must not be read while a new # USER node has appeared but no new assistant node yet. f" var last = msgs.length > {initial_count} ? msgs[msgs.length - 1] : null;" @@ -593,8 +593,8 @@ async def stream_until_complete( # so the stall clock treats it as active generation, not a stall. " var hasThinkingEl = !!last.querySelector('.result-thinking');" " var visibleThinking = /^(thinking|reasoning)\\b/i.test(rawText.trim());" - " var is_thinking = !has_action && (hasThinkingEl || (visibleThinking && !mdText));" " var has_action = legacy_action || copyDelta;" + " var is_thinking = !has_action && (hasThinkingEl || (visibleThinking && !mdText));" " return JSON.stringify({text: text, md_text: mdText, html_len: html_len, child_count: child_count, has_action: has_action, copy_delta:copyDelta, stop_present:stopPresent, is_thinking: is_thinking});" "})()", ) @@ -747,7 +747,7 @@ async def stream_until_complete( # assistant container, read the ANCHORED backend text. # This is observation-only; no message is resent. # Do not report success based on the Copy icon alone. - if not last_dom_text and not had_non_text_content: + if not last_dom_text: text_result = await d._fetch_text_for_turn( conv_id_for_check, turn_anchor, ) From bc871e465dc7176772b08b38e08380b2894f8487 Mon Sep 17 00:00:00 2001 From: Alajmah <55954113+Alajmah@users.noreply.github.com> Date: Fri, 20 Nov 2026 11:14:51 +0300 Subject: [PATCH 07/45] test(dom): replace source greps with real Node VM current/legacy DOM probes and completion gates --- tests/test_current_dom_role_selectors.py | 264 ++++++++++++++++++++++- 1 file changed, 257 insertions(+), 7 deletions(-) diff --git a/tests/test_current_dom_role_selectors.py b/tests/test_current_dom_role_selectors.py index 74674b4d..12d1bcf6 100644 --- a/tests/test_current_dom_role_selectors.py +++ b/tests/test_current_dom_role_selectors.py @@ -1,9 +1,259 @@ -import inspect -from chatgpt_web2api.cdp_driver import CDPDriver +"""Behavioral CDP DOM tests for the 2026-11-20 aria-only ChatGPT UI. + +Executes the real Python-composed selectors and phase-2 JavaScript in Node vm +against miniature current and legacy DOM fixtures (not a live browser). +No source-text grep or fake returns standing in for CSS query semantics. +""" + +import json +import shutil +import subprocess +from unittest.mock import AsyncMock, MagicMock + +import pytest + from chatgpt_web2api.completion_detector import CompletionDetector +from chatgpt_web2api.dom_turn_signals import ( + ASSISTANT_TURN_COUNT_JS, + USER_TURN_COUNT_JS, +) +from chatgpt_web2api.turn_anchor import TurnAnchor, TurnEndResult, TurnTextResult + +NODE_AVAILABLE = shutil.which("node") is not None + + +_JS_FIXTURE = r""" +const fs = require('fs'); +const vm = require('vm'); +const {script, mode, scenario} = JSON.parse(fs.readFileSync(0, 'utf8')); +const state = scenario || {}; +function el(kind) { + return { + offsetParent: {}, + getClientRects: () => [{}], + getBoundingClientRect: () => ({top:100, bottom:180}), + getAttribute: k => k === 'aria-label' ? kind : null, + closest: selector => selector.includes('article') ? root : null, + }; +} +const root = { + innerText: state.answer || 'Current UI answer.', + innerHTML: '

Current UI answer

'.repeat(3), + children: [1], + parentElement: null, + getBoundingClientRect: () => ({top:100, bottom:180}), + querySelector: selector => { + if (selector.includes('result-thinking')) return null; + if (mode === 'legacy' && selector.includes('markdown')) { + return {textContent: state.answer || 'Legacy answer.'}; + } + return null; // Current UI has NO .markdown or data-message-author-role + }, + querySelectorAll: selector => { + if (selector.includes('turn-action-button') && mode === 'legacy') { + return [el('old-action')]; + } + return []; + }, +}; +const copies = Array.from({length:state.copies ?? (mode === 'current' ? 1 : 0)}, () => el('Copy')); +const edits = Array.from({length:state.edits ?? (mode === 'current' ? 1 : 0)}, () => el('Edit message')); +const legacyAssistants = mode === 'legacy' ? [root] : []; +const legacyUsers = mode === 'legacy' ? [el('user')] : []; +const hiddenMenus = state.hiddenMenus || []; // irrelevant role=menuitem nodes +const document = { + body: {innerText: state.bodyText || ''}, + querySelectorAll: selector => { + if (selector.startsWith('button[aria-label="Copy"]')) return copies; + if (selector.startsWith('button[aria-label="Edit message"]')) return edits; + if (selector.startsWith('[data-message-author-role="assistant"]')) return legacyAssistants; + if (selector.startsWith('[data-message-author-role="user"]')) return legacyUsers; + if (selector.includes('stop-button')) return state.stop ? [el('Stop')] : []; + if (selector.includes('[role="menuitem"]')) return hiddenMenus; + return []; + }, +}; +try { + const result = vm.runInNewContext(script, {document}, {timeout:1000}); + console.log(JSON.stringify({result})); +} catch(e) { + console.log(JSON.stringify({error:String(e)})); + process.exit(1); +} +""" + + +def _eval_js(script, mode="current", **scenario): + if not NODE_AVAILABLE: + pytest.skip("Node.js not installed") + p = subprocess.run( + ["node", "-e", _JS_FIXTURE], + input=json.dumps({"script": script, "mode": mode, "scenario": scenario}), + text=True, + capture_output=True, + check=True, + timeout=10, + ) + return json.loads(p.stdout)["result"] + + +class _CapturedProbe(Exception): + pass + + +async def _actual_phase2_expression(initial_count=0): + """Collect the actual generated JS, rather than reimplementing it in tests.""" + d = MagicMock() + d._current_conv_id = None + d._pre_send_user_count = None + seen = [] + + async def _js(expr, timeout=15): + if "body.innerText" in expr: + return json.dumps({"text": ""}) + if "copyButtons" in expr and "has_action" in expr: + seen.append(expr) + raise _CapturedProbe + return "1" # Phase-1 assistant-count delta + + d._js_strict = _js + det = CompletionDetector(d) + with pytest.raises(_CapturedProbe): + async for _ in det.stream_until_complete( + initial_count=initial_count, + timeout=10, + turn_anchor=TurnAnchor(sent_text="probe", mode="fresh_chat"), + ): + pass + assert len(seen) == 1 + return seen[0] + + +@pytest.mark.parametrize("mode,copies,edits,assistant,user", [ + ("current", 4, 4, 4, 4), + ("current", 0, 0, 0, 0), + ("legacy", 0, 0, 1, 1), +]) +def test_executable_count_selectors(mode, copies, edits, assistant, user): + kw = {"copies": copies, "edits": edits} + assert _eval_js(ASSISTANT_TURN_COUNT_JS, mode, **kw) == assistant + assert _eval_js(USER_TURN_COUNT_JS, mode, **kw) == user + + +@pytest.mark.parametrize("mode,baseline,expect_new,expected_text", [ + ("current", 0, True, "October visible answer."), + ("legacy", 0, False, "Legacy answer."), + ("current", 1, False, ""), +]) +@pytest.mark.asyncio +async def test_phase2_executes_real_js_in_new_and_legacy_layouts( + mode, baseline, expect_new, expected_text +): + js = await _actual_phase2_expression(baseline) + output = json.loads( + _eval_js( + js, mode, + copies=(1 if mode == "current" else 0), + answer=expected_text or "Prior answer.", + ) + ) + assert output["copy_delta"] is expect_new + if expected_text: + assert output["text"] == expected_text + if baseline: + assert output["text"] == "" # no stale previous turn + assert output["has_action"] is False + + +@pytest.mark.asyncio +async def test_hidden_menu_and_script_text_do_not_create_turn_signals(): + """Counts operate on ELEMENTS, not source text or hidden sidebar labels.""" + assert _eval_js( + ASSISTANT_TURN_COUNT_JS, + "current", + copies=0, + edits=0, + bodyText='', + hiddenMenus=["mounted-but-hidden"], + ) == 0 + assert _eval_js(USER_TURN_COUNT_JS, "current", copies=0, edits=0) == 0 + + +@pytest.mark.asyncio +async def test_phase1_user_edit_delta_unblocks_before_copy_action(): + d = MagicMock() + d._current_conv_id = None + d._pre_send_user_count = 0 + d._get_live_conversation_id_best_effort = AsyncMock(return_value="") + + phase2_calls = [0] + + async def evaluate(expr, timeout=15): + if "body.innerText" in expr: + return '{"text": ""}' + if expr == ASSISTANT_TURN_COUNT_JS: + return "0" # Copy not yet mounted, despite user turn appearing + if expr == USER_TURN_COUNT_JS: + return "1" # Edit message appeared after send + if "has_action" in expr: + phase2_calls[0] += 1 + return json.dumps({ + "text": "Answer", "md_text": "Answer", "html_len": 60, + "child_count": 1, "has_action": True, + "copy_delta": True, "stop_present": False, + "is_thinking": False, + }) + raise AssertionError(expr) + + d._js_strict = evaluate + det = CompletionDetector(d) + chunks = [] + async for chunk in det.stream_until_complete( + initial_count=0, timeout=10, + turn_anchor=TurnAnchor(sent_text="hello", mode="fresh_chat"), + ): + chunks.append(chunk) + + assert phase2_calls[0] >= 2 # one extra stable-text poll + assert "".join(c.delta for c in chunks) == "Answer" + + +@pytest.mark.asyncio +async def test_anchored_backend_text_recovers_when_current_dom_has_no_container(): + d = MagicMock() + d._current_conv_id = "conv-current" + d._pre_send_user_count = 0 + d._fetch_end_turn_for_turn = AsyncMock( + return_value=TurnEndResult(status="matched") + ) + d._fetch_text_for_turn = AsyncMock( + return_value=TurnTextResult(status="matched", text="Anchored final text") + ) + + async def evaluate(expr, timeout=15): + if "body.innerText" in expr: + return '{"text": ""}' + if expr == ASSISTANT_TURN_COUNT_JS: + return "0" + if expr == USER_TURN_COUNT_JS: + return "1" + if "has_action" in expr: + return json.dumps({ + "text": "", "md_text": "", "html_len": 0, + "child_count": 0, "has_action": False, + "copy_delta": True, "stop_present": False, + "is_thinking": False, + }) + raise AssertionError(expr) -def test_current_chatgpt_role_selectors_are_supported(): - src = inspect.getsource(CDPDriver) + inspect.getsource(CompletionDetector) - assert "data-content-search-unit-key$=\\\":assistant\\\"" in src - assert "data-content-search-unit-key$=\\\":user\\\"" in src - assert "data-message-author-role" in src + d._js_strict = evaluate + det = CompletionDetector(d) + chunks = [] + async for chunk in det.stream_until_complete( + initial_count=0, timeout=10, + turn_anchor=TurnAnchor(sent_text="hello", mode="fresh_chat"), + ): + chunks.append(chunk) + assert [c.delta for c in chunks] == ["Anchored final text"] + assert det.last_dom_text == "Anchored final text" + d._fetch_text_for_turn.assert_awaited_once() From 7eb0a1f5c57ba48d70b832d1f8387aef617f67ff Mon Sep 17 00:00:00 2001 From: Alajmah <55954113+Alajmah@users.noreply.github.com> Date: Fri, 20 Nov 2026 11:15:14 +0300 Subject: [PATCH 08/45] test(dom): advance phase-1 above chosen baseline in fixture capture --- tests/test_current_dom_role_selectors.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_current_dom_role_selectors.py b/tests/test_current_dom_role_selectors.py index 12d1bcf6..d3b5ba1c 100644 --- a/tests/test_current_dom_role_selectors.py +++ b/tests/test_current_dom_role_selectors.py @@ -114,7 +114,7 @@ async def _js(expr, timeout=15): if "copyButtons" in expr and "has_action" in expr: seen.append(expr) raise _CapturedProbe - return "1" # Phase-1 assistant-count delta + return str(initial_count + 1) # Phase-1 assistant-count delta d._js_strict = _js det = CompletionDetector(d) From 305284e72bd91ec9188174decfb90d1a97d5065c Mon Sep 17 00:00:00 2001 From: Alajmah <55954113+Alajmah@users.noreply.github.com> Date: Fri, 20 Nov 2026 11:16:51 +0300 Subject: [PATCH 09/45] style: resolve inherited Ruff baseline findings on PR #55 revision --- src/chatgpt_web2api/cdp_driver.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/chatgpt_web2api/cdp_driver.py b/src/chatgpt_web2api/cdp_driver.py index 65212b43..4fe3ccad 100644 --- a/src/chatgpt_web2api/cdp_driver.py +++ b/src/chatgpt_web2api/cdp_driver.py @@ -77,16 +77,16 @@ class StreamChunk: # Canonical home moved to chatgpt_dom.py in Phase 5 PR3; re-exported here for # back-compat (tests import these from cdp_driver, and the navigation methods # that stay here still reference them). -from .dom_turn_signals import ( # noqa: E402 - ASSISTANT_TURN_COUNT_JS, - USER_TURN_COUNT_JS, -) from .chatgpt_dom import ( # noqa: E402,F401 COMPOSER_FALLBACK_SELECTOR, COMPOSER_SELECTOR, SEND_BUTTON_FALLBACK_SELECTOR, SEND_BUTTON_SELECTOR, ) +from .dom_turn_signals import ( # noqa: E402 + ASSISTANT_TURN_COUNT_JS, + USER_TURN_COUNT_JS, +) # ── P2: Navigation readiness probe ──────────────────────────────────────── # From 87d3ce1b194b0fb22b31b9d1b021ad668ce959e1 Mon Sep 17 00:00:00 2001 From: Alajmah <55954113+Alajmah@users.noreply.github.com> Date: Fri, 20 Nov 2026 11:16:54 +0300 Subject: [PATCH 10/45] style: resolve inherited Ruff baseline findings on PR #55 revision --- tests/test_composer_verification.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/test_composer_verification.py b/tests/test_composer_verification.py index c1423cc3..778ac7f6 100644 --- a/tests/test_composer_verification.py +++ b/tests/test_composer_verification.py @@ -12,13 +12,12 @@ realistic ProseMirror DOM shapes — not just mocked output strings. """ -import unicodedata from unittest.mock import AsyncMock import pytest -from chatgpt_web2api.chatgpt_dom import ChatGPTDom from chatgpt_web2api.cdp_driver import CDPDriver +from chatgpt_web2api.chatgpt_dom import ChatGPTDom def _make_dom(js_return_value=""): From 6fa451f43f8ff0f9fcb032d27895610ea4869833 Mon Sep 17 00:00:00 2001 From: Alajmah <55954113+Alajmah@users.noreply.github.com> Date: Fri, 20 Nov 2026 11:16:57 +0300 Subject: [PATCH 11/45] style: resolve inherited Ruff baseline findings on PR #55 revision --- tests/test_send_acknowledgment.py | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/tests/test_send_acknowledgment.py b/tests/test_send_acknowledgment.py index 9fdf70e2..a2f954ac 100644 --- a/tests/test_send_acknowledgment.py +++ b/tests/test_send_acknowledgment.py @@ -18,14 +18,13 @@ Fix 2: include last_result.diagnostic in TurnReconciliationError. """ -import asyncio import json from unittest.mock import AsyncMock, MagicMock import pytest -from chatgpt_web2api.cdp_driver import CDPDriver, SendReadinessError -from chatgpt_web2api.turn_anchor import TurnReconciliationError, TurnTextResult +from chatgpt_web2api.cdp_driver import CDPDriver +from chatgpt_web2api.turn_anchor import TurnReconciliationError def _make_driver(): @@ -49,8 +48,6 @@ async def test_send_not_acknowledged_raises_when_no_signals(monkeypatch): """When click_send fires but no acknowledgment appears (no UUID, no DOM count increase, composer not cleared), the bridge must raise a typed error instead of silently entering completion detection.""" - from chatgpt_web2api.cdp_driver import CDPDriver - driver = _make_driver() # Mock the send path driver.type_message = AsyncMock() @@ -107,8 +104,6 @@ async def test_send_acknowledged_when_user_count_increases(monkeypatch): driver._capture_pre_send_fallback_anchor = AsyncMock(return_value=anchor) # After send: user count goes from 0 to 1 (message landed) - poll_count = {"n": 0} - async def fake_js_strict(expr, timeout=15): # Send acknowledgment check: user count + composer present + empty if "userCount" in expr and "composerEmpty" in expr: @@ -126,7 +121,6 @@ async def fake_js_strict(expr, timeout=15): driver._js_strict = fake_js_strict # Mock the detector to return immediately - from chatgpt_web2api.completion_detector import CompletionDetector driver._completion = MagicMock() driver._completion.stream_until_complete = MagicMock() From 94987486d25a6ac870cb8c48aa3272ad98e272f1 Mon Sep 17 00:00:00 2001 From: Alajmah <55954113+Alajmah@users.noreply.github.com> Date: Fri, 20 Nov 2026 11:17:36 +0300 Subject: [PATCH 12/45] test(dom): keep Copy fallback subordinate to backend not-ready and active Stop --- tests/test_current_dom_role_selectors.py | 57 ++++++++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/tests/test_current_dom_role_selectors.py b/tests/test_current_dom_role_selectors.py index d3b5ba1c..b3c81b88 100644 --- a/tests/test_current_dom_role_selectors.py +++ b/tests/test_current_dom_role_selectors.py @@ -257,3 +257,60 @@ async def evaluate(expr, timeout=15): assert [c.delta for c in chunks] == ["Anchored final text"] assert det.last_dom_text == "Anchored final text" d._fetch_text_for_turn.assert_awaited_once() + + + +@pytest.mark.asyncio +@pytest.mark.parametrize("backend_enabled,stop_present", [ + (True, False), # backend 'not_ready' must not be overridden by Copy + (False, True), # Stop still visible blocks DOM-only completion +]) +async def test_copy_cannot_override_backend_not_ready_or_visible_stop( + backend_enabled, stop_present +): + class _EndProbe(Exception): + pass + + d = MagicMock() + d._current_conv_id = "known-conv" if backend_enabled else None + d._pre_send_user_count = 0 + d._get_live_conversation_id_best_effort = AsyncMock(return_value="") + d._fetch_end_turn_for_turn = AsyncMock( + return_value=TurnEndResult(status="not_ready") + ) + phase2_calls = [0] + + async def evaluate(expr, timeout=15): + if "body.innerText" in expr: + return '{"text": ""}' + if expr == ASSISTANT_TURN_COUNT_JS: + return "0" + if expr == USER_TURN_COUNT_JS: + return "1" + if "has_action" in expr: + phase2_calls[0] += 1 + if phase2_calls[0] > 4: + raise _EndProbe + return json.dumps({ + "text": "In-progress answer", + "md_text": "In-progress answer", + "html_len": 60, + "child_count": 1, + "has_action": True, + "copy_delta": True, + "stop_present": stop_present, + "is_thinking": False, + }) + raise AssertionError(expr) + + d._js_strict = evaluate + det = CompletionDetector(d) + with pytest.raises(_EndProbe): + async for _ in det.stream_until_complete( + initial_count=0, timeout=10, + turn_anchor=TurnAnchor(sent_text="hello", mode="fresh_chat"), + ): + pass + assert phase2_calls[0] == 5 + if backend_enabled: + d._fetch_end_turn_for_turn.assert_awaited() From 13343b6352ec0da3fe7feede7a2a99eb1fd60214 Mon Sep 17 00:00:00 2001 From: Alajmah <55954113+Alajmah@users.noreply.github.com> Date: Fri, 20 Nov 2026 11:21:06 +0300 Subject: [PATCH 13/45] fix(detector): keep anchored end_turn available after user turn even when Copy markup is absent --- src/chatgpt_web2api/completion_detector.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/chatgpt_web2api/completion_detector.py b/src/chatgpt_web2api/completion_detector.py index 1cfdd23a..ef5bcf15 100644 --- a/src/chatgpt_web2api/completion_detector.py +++ b/src/chatgpt_web2api/completion_detector.py @@ -362,6 +362,7 @@ async def stream_until_complete( deadline = time.monotonic() + timeout last_node_count = initial_count last_progress = time.monotonic() + user_turn_seen = False while time.monotonic() < deadline: # First check for ChatGPT's rate-limit pop-up — if present, fail # fast with a clear error instead of waiting out the whole timeout. @@ -404,6 +405,7 @@ async def stream_until_complete( try: user_raw = await d._js_strict(USER_TURN_COUNT_JS) if int(user_raw) > pre_user_count: + user_turn_seen = True logger.debug("User turn appeared via Edit-message delta") break except (CDPJSError, ValueError, TypeError): @@ -725,7 +727,10 @@ async def stream_until_complete( backend_fetch_failed = False if ( conv_id_for_check - and (last_dom_text or saw_thinking or is_thinking or had_non_text_content or copy_delta) + and ( + last_dom_text or saw_thinking or is_thinking + or had_non_text_content or copy_delta or user_turn_seen + ) and time.monotonic() - last_backend_check > 3.0 ): last_backend_check = time.monotonic() From 7f200ad79057ffb2cc01f71cf005309daf2910c3 Mon Sep 17 00:00:00 2001 From: Alajmah <55954113+Alajmah@users.noreply.github.com> Date: Fri, 20 Nov 2026 11:21:22 +0300 Subject: [PATCH 14/45] test(detector): anchored completed turn recovers answer even with no Copy button --- tests/test_current_dom_role_selectors.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/tests/test_current_dom_role_selectors.py b/tests/test_current_dom_role_selectors.py index b3c81b88..ecda1d90 100644 --- a/tests/test_current_dom_role_selectors.py +++ b/tests/test_current_dom_role_selectors.py @@ -200,7 +200,7 @@ async def evaluate(expr, timeout=15): return json.dumps({ "text": "Answer", "md_text": "Answer", "html_len": 60, "child_count": 1, "has_action": True, - "copy_delta": True, "stop_present": False, + "copy_delta": copy_delta, "stop_present": False, "is_thinking": False, }) raise AssertionError(expr) @@ -219,7 +219,10 @@ async def evaluate(expr, timeout=15): @pytest.mark.asyncio -async def test_anchored_backend_text_recovers_when_current_dom_has_no_container(): +@pytest.mark.parametrize("copy_delta", [False, True]) +async def test_anchored_backend_text_recovers_when_current_dom_has_no_container( + copy_delta, +): d = MagicMock() d._current_conv_id = "conv-current" d._pre_send_user_count = 0 From 76e1f9c35926a7b295b70e4d8dd12364b9f664d9 Mon Sep 17 00:00:00 2001 From: Alajmah <55954113+Alajmah@users.noreply.github.com> Date: Fri, 20 Nov 2026 11:23:27 +0300 Subject: [PATCH 15/45] test(dom): pass parametrized Copy state only to anchored recovery test --- tests/test_current_dom_role_selectors.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test_current_dom_role_selectors.py b/tests/test_current_dom_role_selectors.py index ecda1d90..2df67178 100644 --- a/tests/test_current_dom_role_selectors.py +++ b/tests/test_current_dom_role_selectors.py @@ -200,7 +200,7 @@ async def evaluate(expr, timeout=15): return json.dumps({ "text": "Answer", "md_text": "Answer", "html_len": 60, "child_count": 1, "has_action": True, - "copy_delta": copy_delta, "stop_present": False, + "copy_delta": True, "stop_present": False, "is_thinking": False, }) raise AssertionError(expr) @@ -244,7 +244,7 @@ async def evaluate(expr, timeout=15): return json.dumps({ "text": "", "md_text": "", "html_len": 0, "child_count": 0, "has_action": False, - "copy_delta": True, "stop_present": False, + "copy_delta": copy_delta, "stop_present": False, "is_thinking": False, }) raise AssertionError(expr) From 43b4d8b310d86df364da1daac5bc3d5b9c478aef Mon Sep 17 00:00:00 2001 From: Alajmah <55954113+Alajmah@users.noreply.github.com> Date: Fri, 20 Nov 2026 11:25:00 +0300 Subject: [PATCH 16/45] fix(detector): never treat Copy toolbar wrapper as non-text answer evidence --- src/chatgpt_web2api/completion_detector.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/chatgpt_web2api/completion_detector.py b/src/chatgpt_web2api/completion_detector.py index ef5bcf15..4584868a 100644 --- a/src/chatgpt_web2api/completion_detector.py +++ b/src/chatgpt_web2api/completion_detector.py @@ -510,10 +510,11 @@ async def stream_until_complete( f" var last = msgs.length > {initial_count} ? msgs[msgs.length - 1] : null;" # New layout: the completed Copy affordance anchors a # semantic turn container; never fall back to body text. + " var fromCopyRoot = false;" " if (!last && copyDelta) {" " var copyBtn = copyButtons[copyButtons.length - 1];" ' var root = copyBtn.closest(\'[data-testid^="conversation-turn-"], article\');' - " if (root) last = root;" + " if (root) { last = root; fromCopyRoot = true; }" " }" " var stops = document.querySelectorAll('[data-testid=\\\"stop-button\\\"], button[aria-label*=\\\"Stop\\\" i]');" " var stopPresent = Array.prototype.some.call(stops, function(b){return b.getClientRects().length > 0;});" @@ -531,8 +532,13 @@ async def stream_until_complete( # Strip a leading "Thinking..." / "Thought for …" reasoning # label so the innerText fallback can't leak it as a delta. " var text = mdText || rawText.replace(/^Think(ing|\\s+for)[^\\n]*\\n?/i, '');" - " var html_len = last.innerHTML.length;" - " var child_count = last.children.length;" + # A current article includes an action toolbar whose + # HTML can exceed 50 chars even with ZERO answer content. + # Do not mistake that wrapper for non-text assistant + # content (which would bypass strict backend text gating). + " var hasRichContent = !!last.querySelector('img, canvas, video, audio');" + " var html_len = fromCopyRoot && !hasRichContent ? 0 : last.innerHTML.length;" + " var child_count = fromCopyRoot && !hasRichContent ? 0 : last.children.length;" # has_action: the per-turn copy/feedback action row appears # only on a COMPLETED message. ChatGPT's DOM layout puts these # buttons in a SIBLING/UNCLE container, NOT as descendants of From 5c1d977b6e28991682d69f4c4fb56a9d1fb9b62e Mon Sep 17 00:00:00 2001 From: Alajmah <55954113+Alajmah@users.noreply.github.com> Date: Fri, 20 Nov 2026 11:25:12 +0300 Subject: [PATCH 17/45] test(detector): no phantom non-text answer from completed action-toolbar HTML --- tests/test_current_dom_role_selectors.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/tests/test_current_dom_role_selectors.py b/tests/test_current_dom_role_selectors.py index 2df67178..ccc6f81e 100644 --- a/tests/test_current_dom_role_selectors.py +++ b/tests/test_current_dom_role_selectors.py @@ -37,7 +37,7 @@ }; } const root = { - innerText: state.answer || 'Current UI answer.', + innerText: state.answer ?? 'Current UI answer.', innerHTML: '

Current UI answer

'.repeat(3), children: [1], parentElement: null, @@ -317,3 +317,15 @@ async def evaluate(expr, timeout=15): assert phase2_calls[0] == 5 if backend_enabled: d._fetch_end_turn_for_turn.assert_awaited() + + + +@pytest.mark.asyncio +async def test_action_toolbar_alone_is_not_a_non_text_response(): + """A newly mounted Copy toolbar alone must not imply usable rich content.""" + js = await _actual_phase2_expression(0) + state = json.loads(_eval_js(js, "current", copies=1, answer="")) + assert state["copy_delta"] is True + assert state["text"] == "" + assert state["html_len"] == 0 + assert state["child_count"] == 0 From b4b19c04522b6391cb9fee6db1970d836074447a Mon Sep 17 00:00:00 2001 From: Alajmah <55954113+Alajmah@users.noreply.github.com> Date: Fri, 20 Nov 2026 11:26:12 +0300 Subject: [PATCH 18/45] test(dom): fixture the full observed user/assistant aria action rows --- tests/test_current_dom_role_selectors.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/tests/test_current_dom_role_selectors.py b/tests/test_current_dom_role_selectors.py index ccc6f81e..e2a88fc6 100644 --- a/tests/test_current_dom_role_selectors.py +++ b/tests/test_current_dom_role_selectors.py @@ -61,11 +61,18 @@ const legacyAssistants = mode === 'legacy' ? [root] : []; const legacyUsers = mode === 'legacy' ? [el('user')] : []; const hiddenMenus = state.hiddenMenus || []; // irrelevant role=menuitem nodes +const assistantExtras = ['Read aloud', 'Share', 'More actions']; +const userExtras = ['Share prompt']; const document = { body: {innerText: state.bodyText || ''}, querySelectorAll: selector => { if (selector.startsWith('button[aria-label="Copy"]')) return copies; if (selector.startsWith('button[aria-label="Edit message"]')) return edits; + if (mode === 'current') { + for (const label of [...assistantExtras, ...userExtras]) { + if (selector === 'button[aria-label="' + label + '"]') return [el(label)]; + } + } if (selector.startsWith('[data-message-author-role="assistant"]')) return legacyAssistants; if (selector.startsWith('[data-message-author-role="user"]')) return legacyUsers; if (selector.includes('stop-button')) return state.stop ? [el('Stop')] : []; @@ -329,3 +336,17 @@ async def test_action_toolbar_alone_is_not_a_non_text_response(): assert state["text"] == "" assert state["html_len"] == 0 assert state["child_count"] == 0 + + + +@pytest.mark.parametrize("label", [ + "Copy", "Read aloud", "Share", "More actions", + "Edit message", "Share prompt", +]) +def test_current_layout_exposes_exact_action_row_labels_without_role_attrs( + label, +): + selector = json.dumps(f'button[aria-label="{label}"]') + expression = f"document.querySelectorAll({selector}).length" + assert _eval_js(expression, "current", copies=1, edits=1) == 1 + assert _eval_js(expression, "legacy", copies=0, edits=0) == 0 From 3a62cf321f568c9006b622ef1708973a48621eda Mon Sep 17 00:00:00 2001 From: Alajmah <55954113+Alajmah@users.noreply.github.com> Date: Fri, 20 Nov 2026 12:20:37 +0300 Subject: [PATCH 19/45] fix(detector): never stream action-row article chrome as assistant response --- src/chatgpt_web2api/completion_detector.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/chatgpt_web2api/completion_detector.py b/src/chatgpt_web2api/completion_detector.py index 4584868a..702fe4dc 100644 --- a/src/chatgpt_web2api/completion_detector.py +++ b/src/chatgpt_web2api/completion_detector.py @@ -531,7 +531,9 @@ async def stream_until_complete( " var rawText = (last.innerText || '').trim();" # Strip a leading "Thinking..." / "Thought for …" reasoning # label so the innerText fallback can't leak it as a delta. - " var text = mdText || rawText.replace(/^Think(ing|\\s+for)[^\\n]*\\n?/i, '');" + # The current-only Copy/article root contains UI chrome" + " var text = mdText || (fromCopyRoot ? '' :" + " rawText.replace(/^Think(ing|\\s+for)[^\\n]*\\n?/i, ''));" # A current article includes an action toolbar whose # HTML can exceed 50 chars even with ZERO answer content. # Do not mistake that wrapper for non-text assistant From 5704df0c5965f9064d6a05fa92c853b6a8095b66 Mon Sep 17 00:00:00 2001 From: Alajmah <55954113+Alajmah@users.noreply.github.com> Date: Fri, 20 Nov 2026 12:20:57 +0300 Subject: [PATCH 20/45] fix(reconcile): require exact DOM prefix before emitting backend text suffix --- src/chatgpt_web2api/cdp_driver.py | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/src/chatgpt_web2api/cdp_driver.py b/src/chatgpt_web2api/cdp_driver.py index 4fe3ccad..3a963758 100644 --- a/src/chatgpt_web2api/cdp_driver.py +++ b/src/chatgpt_web2api/cdp_driver.py @@ -1848,9 +1848,29 @@ async def send_and_stream( last_status = result.status last_diagnostic = result.diagnostic or {} if result.status == "matched" and result.text: - if len(result.text) > len(last_dom_text): - yield StreamChunk(delta=result.text[len(last_dom_text):]) - last_dom_text = result.text + # DOM deltas may contain stale UI chrome or shift + # their prefix during hydration. A length-only suffix + # silently truncates canonical text in either case. + # Once a chunk was streamed it cannot be retracted: + # fail closed instead of returning HTTP 200 with a + # mixed/garbled answer. An empty DOM baseline safely + # emits the entire anchored backend text. + if not result.text.startswith(last_dom_text): + raise TurnReconciliationError( + conversation_id=conv_id, + anchor_mode=turn_anchor.mode, + last_status="text_prefix_mismatch", + diagnostic={ + "captured_id": turn_anchor.captured_user_message_id, + "dom_text_length": len(last_dom_text), + "backend_text_length": len(result.text), + "reason": "DOM delta is not a prefix of anchored text", + }, + ) + suffix = result.text[len(last_dom_text):] + if suffix: + yield StreamChunk(delta=suffix) + last_dom_text = result.text break if result.status == "non_text": # P2.5 RCA fix: non_text is NOT terminal here. The backend From 8221c2bcedacd8a5ae0ba7628d79daf1940d7dfe Mon Sep 17 00:00:00 2001 From: Alajmah <55954113+Alajmah@users.noreply.github.com> Date: Fri, 20 Nov 2026 12:21:29 +0300 Subject: [PATCH 21/45] test(detector): reproduce live role-chrome article and require exact anchored sentinel --- tests/test_current_dom_role_selectors.py | 60 +++++++++++++++++++++++- 1 file changed, 58 insertions(+), 2 deletions(-) diff --git a/tests/test_current_dom_role_selectors.py b/tests/test_current_dom_role_selectors.py index e2a88fc6..9c40c857 100644 --- a/tests/test_current_dom_role_selectors.py +++ b/tests/test_current_dom_role_selectors.py @@ -37,7 +37,9 @@ }; } const root = { - innerText: state.answer ?? 'Current UI answer.', + innerText: mode === 'current' + ? 'ChatGPT said:\\n\\n' + (state.answer ?? 'Current UI answer.') + : (state.answer ?? 'Legacy answer.'), innerHTML: '

Current UI answer

'.repeat(3), children: [1], parentElement: null, @@ -165,8 +167,14 @@ async def test_phase2_executes_real_js_in_new_and_legacy_layouts( ) ) assert output["copy_delta"] is expect_new - if expected_text: + if mode == "legacy": assert output["text"] == expected_text + else: + # The Copy/article wrapper is not a clean answer container: the + # current UI includes the prefix "ChatGPT said:" in innerText. + # Its text MUST NOT become any response delta, even on a new turn. + assert output["text"] == "" + assert output["md_text"] == "" if baseline: assert output["text"] == "" # no stale previous turn assert output["has_action"] is False @@ -350,3 +358,51 @@ def test_current_layout_exposes_exact_action_row_labels_without_role_attrs( expression = f"document.querySelectorAll({selector}).length" assert _eval_js(expression, "current", copies=1, edits=1) == 1 assert _eval_js(expression, "legacy", copies=0, edits=0) == 0 + + +@pytest.mark.asyncio +async def test_current_dom_role_chrome_is_replaced_by_anchored_final_text(): + """Reproduce live HTTP-200 corruption: article has ChatGPT said + answer. + + Phase 1 sees the new user Edit control, but no assistant role nodes. + Phase 2 executes the REAL detector JS in the Node current-DOM fixture, + where a Copy/article wrapper has the role label. Only the anchored + backend answer may be yielded; no role chrome or sliced head. + """ + sentinel = "PR57-LIVE-dd2290aa" + expr = await _actual_phase2_expression(0) + + d = MagicMock() + d._current_conv_id = "6b000e1c-5974-83eb-9691-bc3450677411" + d._pre_send_user_count = 0 + d._fetch_end_turn_for_turn = AsyncMock( + return_value=TurnEndResult(status="matched") + ) + d._fetch_text_for_turn = AsyncMock( + return_value=TurnTextResult(status="matched", text=sentinel) + ) + + async def evaluate(js, timeout=15): + if "body.innerText" in js: + return '{"text": ""}' + if js == ASSISTANT_TURN_COUNT_JS: + return "0" + if js == USER_TURN_COUNT_JS: + return "1" + if "copyButtons" in js and "has_action" in js: + assert js == expr + return _eval_js(expr, "current", copies=1, answer=sentinel) + raise AssertionError(js) + + d._js_strict = evaluate + detector = CompletionDetector(d) + pieces = [] + async for chunk in detector.stream_until_complete( + initial_count=0, timeout=10, + turn_anchor=TurnAnchor(sent_text="test", mode="fresh_chat"), + ): + pieces.append(chunk.delta) + + assert pieces == [sentinel] # no optimistic 'ChatGPT said:' chunk + assert detector.last_dom_text == sentinel + d._fetch_text_for_turn.assert_awaited_once() From 364397f9b2f82ba879ee24cfd3a401df5929fe0c Mon Sep 17 00:00:00 2001 From: Alajmah <55954113+Alajmah@users.noreply.github.com> Date: Fri, 20 Nov 2026 12:22:02 +0300 Subject: [PATCH 22/45] docs(detector): document why unscoped article innerText is unsafe --- src/chatgpt_web2api/completion_detector.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/chatgpt_web2api/completion_detector.py b/src/chatgpt_web2api/completion_detector.py index 702fe4dc..512e3830 100644 --- a/src/chatgpt_web2api/completion_detector.py +++ b/src/chatgpt_web2api/completion_detector.py @@ -531,7 +531,10 @@ async def stream_until_complete( " var rawText = (last.innerText || '').trim();" # Strip a leading "Thinking..." / "Thought for …" reasoning # label so the innerText fallback can't leak it as a delta. - # The current-only Copy/article root contains UI chrome" + # The current Copy/article root can include role chrome + # such as "ChatGPT said:" and action-toolbar labels. With + # no semantic content descendant, its innerText is NOT + # assistant text and must not be streamed optimistically. " var text = mdText || (fromCopyRoot ? '' :" " rawText.replace(/^Think(ing|\\s+for)[^\\n]*\\n?/i, ''));" # A current article includes an action toolbar whose From 9a9aeb72414cd822ecf5513c61ec3c86736e9355 Mon Sep 17 00:00:00 2001 From: Alajmah <55954113+Alajmah@users.noreply.github.com> Date: Fri, 20 Nov 2026 12:22:26 +0300 Subject: [PATCH 23/45] test(reconciliation): guard against role chrome and nonprefix truncation after live canary --- tests/test_response_text_integrity.py | 82 +++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 tests/test_response_text_integrity.py diff --git a/tests/test_response_text_integrity.py b/tests/test_response_text_integrity.py new file mode 100644 index 00000000..de302cfd --- /dev/null +++ b/tests/test_response_text_integrity.py @@ -0,0 +1,82 @@ +"""Response text integrity regression tests from the PR #57 live REST canary. + +A finished turn is not a valid HTTP-200 response when optimistic DOM +deltas and the exact-turn backend text disagree. Earlier code used length- +only suffixes and could return a clipped answer with role UI chrome. +""" + +from unittest.mock import AsyncMock, MagicMock + +import pytest + +from chatgpt_web2api.cdp_driver import CDPDriver, StreamChunk +from chatgpt_web2api.turn_anchor import ( + TurnAnchor, + TurnReconciliationError, + TurnTextResult, +) + + +def _driver_with_texts(dom_text: str, canonical_text: str) -> CDPDriver: + d = CDPDriver(cdp_port=9222) + d._assert_owned_tab_required = MagicMock() + d._read_assistant_count_baseline = AsyncMock(return_value=0) + d._identity_listener = None + d._capture_pre_send_fallback_anchor = AsyncMock( + return_value=TurnAnchor(sent_text="one send", mode="fresh_chat") + ) + d.type_message = AsyncMock() + d.click_send = AsyncMock() + d._verify_send_acknowledged = AsyncMock(return_value=None) + d._js_strict = AsyncMock(return_value="https://chatgpt.com/c/conv-integrity") + d._fetch_text_for_turn = AsyncMock( + return_value=TurnTextResult(status="matched", text=canonical_text) + ) + + async def fake_stream(**kwargs): + if dom_text: + yield StreamChunk(delta=dom_text) + + d._completion = MagicMock() + d._completion.stream_until_complete = fake_stream + d._completion.last_dom_text = dom_text + d._completion.had_non_text_content = False + return d + + +@pytest.mark.asyncio +@pytest.mark.parametrize("dom_text,canonical", [ + ("", "PR57-LIVE-dd2290aa"), + ("PR57-LIVE-", "PR57-LIVE-dd2290aa"), + ("PR57-LIVE-dd2290aa", "PR57-LIVE-dd2290aa"), +]) +async def test_final_backend_reconciliation_emits_exact_prefix_suffix( + dom_text, canonical +): + d = _driver_with_texts(dom_text, canonical) + chunks = [c async for c in d.send_and_stream("one send", timeout=5)] + + assert "".join(c.delta for c in chunks) == canonical + assert chunks[-1].finish_reason == "stop" + d.click_send.assert_awaited_once() + d._fetch_text_for_turn.assert_awaited_once() + + +@pytest.mark.asyncio +@pytest.mark.parametrize("untrusted_dom", [ + "ChatGPT said:", + "ChatGPT said:290aa", + "PR57-LIVE-xx", # same-ish content but NOT an actual prefix + "PR57-LIVE-dd2290ab", # equal-length mismatch: old code silently 200 +]) +async def test_nonprefix_dom_text_fails_closed_not_http_200(untrusted_dom): + canonical = "PR57-LIVE-dd2290aa" + d = _driver_with_texts(untrusted_dom, canonical) + + with pytest.raises(TurnReconciliationError, match="text_prefix_mismatch") as caught: + _ = [c async for c in d.send_and_stream("one send", timeout=5)] + + assert caught.value.diagnostic["dom_text_length"] == len(untrusted_dom) + assert caught.value.diagnostic["backend_text_length"] == len(canonical) + # The observation reconciliation must NOT resend the user message. + d.click_send.assert_awaited_once() From 766d19b73e8d4db322e7330560644c42e689f9cf Mon Sep 17 00:00:00 2001 From: Alajmah <55954113+Alajmah@users.noreply.github.com> Date: Fri, 20 Nov 2026 12:26:40 +0300 Subject: [PATCH 24/45] fix(detector): only emit monotonic prefix-confirmed DOM deltas; never reset emitted baseline --- src/chatgpt_web2api/completion_detector.py | 24 ++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/src/chatgpt_web2api/completion_detector.py b/src/chatgpt_web2api/completion_detector.py index 512e3830..250bb078 100644 --- a/src/chatgpt_web2api/completion_detector.py +++ b/src/chatgpt_web2api/completion_detector.py @@ -435,6 +435,11 @@ async def stream_until_complete( # (either .markdown text OR non-trivial HTML footprint). The threshold # (> 50 chars) prevents false 'done' from an empty/partial node. last_dom_text = "" + # The emitted prefix must never be replaced by a shorter/unrelated + # DOM snapshot: emitted StreamChunks cannot be retracted. Track raw + # observations separately to detect real progress without advancing + # the emitted baseline on non-prefix revisions. + last_observed_dom_text = "" last_html_len = 0 last_child_count = 0 had_non_text_content = False @@ -671,8 +676,12 @@ async def stream_until_complete( # logging, but does NOT pause the stall clock (a stuck indicator # must not create an infinite hang). generation_active_signal = bool(is_thinking) - if current != last_dom_text: + observation_changed = current != last_observed_dom_text + if observation_changed: + last_observed_dom_text = current last_change_time = time.monotonic() + + if current.startswith(last_dom_text) and len(current) > len(last_dom_text): # P1: first text content transitions us from awaiting_first_content # to streaming_after_first_content. The stall budget changes with # the state (see the stall check below). When the state flips, @@ -682,11 +691,18 @@ async def stream_until_complete( if current and not first_content_seen: first_content_seen = True last_change_time = time.monotonic() # reset stream-idle clock - if len(current) > len(last_dom_text): - delta = current[len(last_dom_text) :] - yield StreamChunk(delta=delta) + # Only a genuine append to bytes already emitted is a safe + # delta. Never slice a non-prefix DOM mutation by length. + delta = current[len(last_dom_text):] + yield StreamChunk(delta=delta) last_dom_text = current self.last_dom_text = last_dom_text + elif observation_changed and not current.startswith(last_dom_text): + logger.debug( + "Non-prefix DOM text revision; retain emitted baseline " + "(observed_len=%d, emitted_len=%d)", + len(current), len(last_dom_text), + ) # Non-text progress signals (images, tool-use, etc.) if html_len != last_html_len or child_count != last_child_count: From 405ae8b3d99fc472295aa70317814d46f3074f67 Mon Sep 17 00:00:00 2001 From: Alajmah <55954113+Alajmah@users.noreply.github.com> Date: Fri, 20 Nov 2026 12:27:27 +0300 Subject: [PATCH 25/45] test(detector): reject length-only delta after nonprefix DOM hydration rewrite --- tests/test_response_text_integrity.py | 70 +++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) diff --git a/tests/test_response_text_integrity.py b/tests/test_response_text_integrity.py index de302cfd..7422a5d5 100644 --- a/tests/test_response_text_integrity.py +++ b/tests/test_response_text_integrity.py @@ -5,13 +5,17 @@ only suffixes and could return a clipped answer with role UI chrome. """ +import json from unittest.mock import AsyncMock, MagicMock import pytest from chatgpt_web2api.cdp_driver import CDPDriver, StreamChunk +from chatgpt_web2api.completion_detector import CompletionDetector +from chatgpt_web2api.dom_turn_signals import ASSISTANT_TURN_COUNT_JS from chatgpt_web2api.turn_anchor import ( TurnAnchor, + TurnEndResult, TurnReconciliationError, TurnTextResult, ) @@ -80,3 +84,69 @@ async def test_nonprefix_dom_text_fails_closed_not_http_200(untrusted_dom): assert caught.value.diagnostic["backend_text_length"] == len(canonical) # The observation reconciliation must NOT resend the user message. d.click_send.assert_awaited_once() + + + +@pytest.mark.asyncio +async def test_phase2_ignores_nonprefix_hydration_rewrite(monkeypatch): + """The old length-only logic would append unrelated UI text or reset its + baseline, causing a garbled 200 even if anchored reconciliation was exact. + Keep the actual emitted prefix intact across a DOM text shape rewrite. + """ + clock = [10.0] + + async def fast_sleep(seconds): + clock[0] += seconds + + monkeypatch.setattr( + "chatgpt_web2api.completion_detector.time.monotonic", + lambda: clock[0], + ) + monkeypatch.setattr( + "chatgpt_web2api.completion_detector.asyncio.sleep", fast_sleep, + ) + d = MagicMock() + d._current_conv_id = "conv-integrity" + d._pre_send_user_count = 0 + d._fetch_end_turn_for_turn = AsyncMock( + side_effect=[ + TurnEndResult(status="not_ready"), + TurnEndResult(status="matched"), + ] + ) + samples = [0] + + async def evaluate(script, timeout=15): + if "body.innerText" in script: + return '{"text": ""}' + if script == ASSISTANT_TURN_COUNT_JS: + return "1" # Legacy assistant role appeared + if "has_action" in script: + samples[0] += 1 + text = ( + "PR57-LIVE-" + if samples[0] == 1 + else "unrelated UI hydration snapshot with more characters" + ) + return json.dumps({ + "text": text, "md_text": text, "html_len": 60, + "child_count": 1, "has_action": False, + "copy_delta": False, "stop_present": False, + "is_thinking": False, + }) + raise AssertionError(script) + + d._js_strict = evaluate + detector = CompletionDetector(d) + chunks = [ + chunk.delta + async for chunk in detector.stream_until_complete( + initial_count=0, + timeout=20, + turn_anchor=TurnAnchor(sent_text="one send", mode="fresh_chat"), + ) + ] + assert samples[0] >= 2 + assert chunks == ["PR57-LIVE-"] + assert detector.last_dom_text == "PR57-LIVE-" + assert d._fetch_end_turn_for_turn.await_count == 2 From 68c3106348da1489453c96ce9eeab771059b40a4 Mon Sep 17 00:00:00 2001 From: Alajmah <55954113+Alajmah@users.noreply.github.com> Date: Fri, 20 Nov 2026 12:30:11 +0300 Subject: [PATCH 26/45] test(dom): match live two-newline role chrome in Node fixture --- tests/test_current_dom_role_selectors.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_current_dom_role_selectors.py b/tests/test_current_dom_role_selectors.py index 9c40c857..a008c3b7 100644 --- a/tests/test_current_dom_role_selectors.py +++ b/tests/test_current_dom_role_selectors.py @@ -38,7 +38,7 @@ } const root = { innerText: mode === 'current' - ? 'ChatGPT said:\\n\\n' + (state.answer ?? 'Current UI answer.') + ? 'ChatGPT said:\n\n' + (state.answer ?? 'Current UI answer.') : (state.answer ?? 'Legacy answer.'), innerHTML: '

Current UI answer

'.repeat(3), children: [1], From 8f68eae18d6043a50c26b5a81424017b36cff90a Mon Sep 17 00:00:00 2001 From: Alajmah <55954113+Alajmah@users.noreply.github.com> Date: Fri, 20 Nov 2026 13:09:07 +0300 Subject: [PATCH 27/45] fix(completion): defer anchored backend text to one final driver fetch --- src/chatgpt_web2api/completion_detector.py | 42 ++++++++++------------ 1 file changed, 18 insertions(+), 24 deletions(-) diff --git a/src/chatgpt_web2api/completion_detector.py b/src/chatgpt_web2api/completion_detector.py index 250bb078..68723c01 100644 --- a/src/chatgpt_web2api/completion_detector.py +++ b/src/chatgpt_web2api/completion_detector.py @@ -775,32 +775,26 @@ async def stream_until_complete( ) status = collapse_to_end_turn_status(end_result) if status == "complete": - # If the current markup has no usable text-bearing - # assistant container, read the ANCHORED backend text. - # This is observation-only; no message is resent. - # Do not report success based on the Copy icon alone. - if not last_dom_text: - text_result = await d._fetch_text_for_turn( - conv_id_for_check, turn_anchor, - ) - if text_result.status == "matched" and text_result.text: - yield StreamChunk(delta=text_result.text) - last_dom_text = text_result.text - self.last_dom_text = last_dom_text - # STRICT: end_turn AND usable content. The saw_thinking - # unlock lets us CONSULT the backend during thinking, but - # we must not finish on a bare end_turn with no answer. - if last_dom_text or had_non_text_content: - logger.info( - "Backend end_turn=true (primary completion) for %s", - conv_id_for_check, - ) - break - logger.debug( - "Backend end_turn=true but no content yet for %s — " - "not completing (strict content guard)", + # STRICT completion is established by the turn-anchored + # selector, not a generic end_turn flag. Its "matched" + # status requires an exact-turn assistant with + # nonempty terminal text, OR a terminal non-text node + # with the existing DOM non-text guard. "not_ready", + # ambiguous and degraded states never enter here. + # + # IMPORTANT: this detector MUST NOT fetch or stream the + # backend text here. The driver is the sole owner of + # the final anchored text read/emission. Fetching once + # here and again in the driver can produce different + # snapshots of the same turn, causing a false prefix + # mismatch after v1 has already escaped to the client. + # Existing clean DOM streaming remains unchanged. + logger.info( + "Backend end_turn=true (primary completion) for %s; " + "terminal text deferred to driver", conv_id_for_check, ) + break elif status == "fetch_failed": backend_fetch_failed = True logger.debug( From 5d1e317aceb27d2fdf220b100c3b8ed5439f8e03 Mon Sep 17 00:00:00 2001 From: Alajmah <55954113+Alajmah@users.noreply.github.com> Date: Fri, 20 Nov 2026 13:09:39 +0300 Subject: [PATCH 28/45] fix(reconcile): diagnose nonprefix text without exposing answer content --- src/chatgpt_web2api/cdp_driver.py | 35 +++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/src/chatgpt_web2api/cdp_driver.py b/src/chatgpt_web2api/cdp_driver.py index 3a963758..3dbeba07 100644 --- a/src/chatgpt_web2api/cdp_driver.py +++ b/src/chatgpt_web2api/cdp_driver.py @@ -12,6 +12,7 @@ from __future__ import annotations import asyncio +import hashlib import json import logging import re @@ -1856,6 +1857,36 @@ async def send_and_stream( # mixed/garbled answer. An empty DOM baseline safely # emits the entire anchored backend text. if not result.text.startswith(last_dom_text): + # Log classification only, not raw assistant text: + # private responses should not escape via WARN logs. + # A whitespace-only difference is diagnostically + # useful but is NOT an exact-match success. + mismatch_at = next( + ( + i for i, (x, y) in enumerate( + zip(last_dom_text, result.text) + ) if x != y + ), + min(len(last_dom_text), len(result.text)), + ) + dom_digest = hashlib.sha256( + last_dom_text.encode("utf-8") + ).hexdigest()[:12] + backend_digest = hashlib.sha256( + result.text.encode("utf-8") + ).hexdigest()[:12] + whitespace_only = ( + last_dom_text.strip() == result.text.strip() + ) + logger.warning( + "Anchored text prefix mismatch: conv_id=%s " + "dom_len=%d backend_len=%d first_diff=%d " + "whitespace_only=%s dom_sha256_12=%s " + "backend_sha256_12=%s", + conv_id, len(last_dom_text), len(result.text), + mismatch_at, whitespace_only, + dom_digest, backend_digest, + ) raise TurnReconciliationError( conversation_id=conv_id, anchor_mode=turn_anchor.mode, @@ -1864,6 +1895,10 @@ async def send_and_stream( "captured_id": turn_anchor.captured_user_message_id, "dom_text_length": len(last_dom_text), "backend_text_length": len(result.text), + "first_mismatch_offset": mismatch_at, + "whitespace_only": whitespace_only, + "dom_sha256_12": dom_digest, + "backend_sha256_12": backend_digest, "reason": "DOM delta is not a prefix of anchored text", }, ) From 36fbfa68a07bc47b237cf72cca446d52ac21c123 Mon Sep 17 00:00:00 2001 From: Alajmah <55954113+Alajmah@users.noreply.github.com> Date: Fri, 20 Nov 2026 13:10:26 +0300 Subject: [PATCH 29/45] test(completion): assert detector defers anchored text without a second fetch --- tests/test_current_dom_role_selectors.py | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/tests/test_current_dom_role_selectors.py b/tests/test_current_dom_role_selectors.py index a008c3b7..c7c3583f 100644 --- a/tests/test_current_dom_role_selectors.py +++ b/tests/test_current_dom_role_selectors.py @@ -235,7 +235,7 @@ async def evaluate(expr, timeout=15): @pytest.mark.asyncio @pytest.mark.parametrize("copy_delta", [False, True]) -async def test_anchored_backend_text_recovers_when_current_dom_has_no_container( +async def test_anchored_backend_text_is_deferred_when_current_dom_has_no_container( copy_delta, ): d = MagicMock() @@ -272,9 +272,10 @@ async def evaluate(expr, timeout=15): turn_anchor=TurnAnchor(sent_text="hello", mode="fresh_chat"), ): chunks.append(chunk) - assert [c.delta for c in chunks] == ["Anchored final text"] - assert det.last_dom_text == "Anchored final text" - d._fetch_text_for_turn.assert_awaited_once() + assert chunks == [] # no double-fetched text escapes from detector + assert det.last_dom_text == "" + d._fetch_text_for_turn.assert_not_awaited() + d._fetch_end_turn_for_turn.assert_awaited_once() @@ -361,7 +362,7 @@ def test_current_layout_exposes_exact_action_row_labels_without_role_attrs( @pytest.mark.asyncio -async def test_current_dom_role_chrome_is_replaced_by_anchored_final_text(): +async def test_current_dom_role_chrome_is_not_streamed_by_detector(): """Reproduce live HTTP-200 corruption: article has ChatGPT said + answer. Phase 1 sees the new user Edit control, but no assistant role nodes. @@ -403,6 +404,7 @@ async def evaluate(js, timeout=15): ): pieces.append(chunk.delta) - assert pieces == [sentinel] # no optimistic 'ChatGPT said:' chunk - assert detector.last_dom_text == sentinel - d._fetch_text_for_turn.assert_awaited_once() + assert pieces == [] # the driver will emit ONE canonical text fetch + assert detector.last_dom_text == "" + d._fetch_text_for_turn.assert_not_awaited() + d._fetch_end_turn_for_turn.assert_awaited_once() From 9461dc5a5361ee615b8e19ed5239e849f7766906 Mon Sep 17 00:00:00 2001 From: Alajmah <55954113+Alajmah@users.noreply.github.com> Date: Fri, 20 Nov 2026 13:11:03 +0300 Subject: [PATCH 30/45] test(dom): end-to-end real Node current DOM and one canonical backend read --- tests/test_current_dom_role_selectors.py | 67 ++++++++++++++++++++++++ 1 file changed, 67 insertions(+) diff --git a/tests/test_current_dom_role_selectors.py b/tests/test_current_dom_role_selectors.py index c7c3583f..2ddbd470 100644 --- a/tests/test_current_dom_role_selectors.py +++ b/tests/test_current_dom_role_selectors.py @@ -12,6 +12,7 @@ import pytest +from chatgpt_web2api.cdp_driver import CDPDriver from chatgpt_web2api.completion_detector import CompletionDetector from chatgpt_web2api.dom_turn_signals import ( ASSISTANT_TURN_COUNT_JS, @@ -408,3 +409,69 @@ async def evaluate(js, timeout=15): assert detector.last_dom_text == "" d._fetch_text_for_turn.assert_not_awaited() d._fetch_end_turn_for_turn.assert_awaited_once() + + +@pytest.mark.asyncio +async def test_live_shape_driver_fetches_anchored_text_once_after_end_turn(): + """One Send, current DOM with no assistant root, two DIFFERENT snapshots. + + v1 and v2 are intentionally divergent to reproduce the confirmed + two-fetch failure class. The driver should only ever request the first + matched snapshot: the detector proves end_turn but does not fetch text. + """ + sentinel = "PR57-LIVE-dd2290aa" + conv_id = "6b0016ad-different-from-the-prior-live-conversation" + phase2_js = await _actual_phase2_expression(0) + d = CDPDriver(cdp_port=9222) + d._current_conv_id = conv_id + d._identity_listener = None + d._assert_owned_tab_required = MagicMock() + d._read_assistant_count_baseline = AsyncMock(return_value=0) + d._pre_send_user_count = 0 + d._capture_pre_send_fallback_anchor = AsyncMock( + return_value=TurnAnchor(sent_text="single send", mode="fresh_chat") + ) + d.type_message = AsyncMock() + d.click_send = AsyncMock() + d._verify_send_acknowledged = AsyncMock(return_value=True) + d._completion = CompletionDetector(d) + d._fetch_end_turn_for_turn = AsyncMock( + return_value=TurnEndResult( + status="matched", + diagnostic={"reason": "text_end_turn"}, + ) + ) + # If the implementation reintroduces a second text read, the returned + # canonical text will disagree and the old mismatch defect reappears. + d._fetch_text_for_turn = AsyncMock( + side_effect=[ + TurnTextResult(status="matched", text=sentinel), + TurnTextResult(status="matched", text="different second version"), + ] + ) + + async def evaluate(expr, timeout=15): + if expr == "window.location.href": + return f"https://chatgpt.com/c/{conv_id}" + if "body.innerText" in expr: + return '{"text": ""}' + if expr == ASSISTANT_TURN_COUNT_JS: + return "0" + if expr == USER_TURN_COUNT_JS: + return "1" + if "copyButtons" in expr and "has_action" in expr: + assert expr == phase2_js + return _eval_js( + phase2_js, "current", copies=1, answer=sentinel + ) + raise AssertionError(expr) + + d._js_strict = evaluate + chunks = [c async for c in d.send_and_stream("single send", timeout=10)] + + assert "".join(c.delta for c in chunks) == sentinel + assert chunks[-1].finish_reason == "stop" + d._fetch_end_turn_for_turn.assert_awaited_once() + d._fetch_text_for_turn.assert_awaited_once() + d.click_send.assert_awaited_once() + assert d._completion.last_dom_text == "" # zero untrusted DOM bytes From d9390f2453ed66230f23e197c490c7cba56b8362 Mon Sep 17 00:00:00 2001 From: Alajmah <55954113+Alajmah@users.noreply.github.com> Date: Fri, 20 Nov 2026 13:11:39 +0300 Subject: [PATCH 31/45] test(reconcile): retain exact-match failure on whitespace-only differences without logging content --- tests/test_response_text_integrity.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/tests/test_response_text_integrity.py b/tests/test_response_text_integrity.py index 7422a5d5..862b5e35 100644 --- a/tests/test_response_text_integrity.py +++ b/tests/test_response_text_integrity.py @@ -82,6 +82,9 @@ async def test_nonprefix_dom_text_fails_closed_not_http_200(untrusted_dom): assert caught.value.diagnostic["dom_text_length"] == len(untrusted_dom) assert caught.value.diagnostic["backend_text_length"] == len(canonical) + assert 0 <= caught.value.diagnostic["first_mismatch_offset"] <= len(untrusted_dom) + assert len(caught.value.diagnostic["dom_sha256_12"]) == 12 + assert len(caught.value.diagnostic["backend_sha256_12"]) == 12 # The observation reconciliation must NOT resend the user message. d.click_send.assert_awaited_once() @@ -150,3 +153,26 @@ async def evaluate(script, timeout=15): assert chunks == ["PR57-LIVE-"] assert detector.last_dom_text == "PR57-LIVE-" assert d._fetch_end_turn_for_turn.await_count == 2 + + + +@pytest.mark.asyncio +async def test_whitespace_only_difference_stays_fail_closed_and_private(caplog): + """Literal REST equality is mandatory: strip-equality is diagnostic only. + + Logs may include lengths, first differing offset, and short hashes, + never the actual assistant answer (even under mismatch). + """ + import logging + + answer = "CANARY-PRIVATE-DO-NOT-LOG" + d = _driver_with_texts(" " + answer, answer) + with caplog.at_level(logging.WARNING): + with pytest.raises(TurnReconciliationError) as raised: + _ = [c async for c in d.send_and_stream("one send", timeout=5)] + + assert raised.value.last_status == "text_prefix_mismatch" + assert raised.value.diagnostic["whitespace_only"] is True + assert answer not in caplog.text + assert "whitespace_only=True" in caplog.text + d.click_send.assert_awaited_once() From 3c23107f3f3ec1e77a090b12fa194dc81c4c0150 Mon Sep 17 00:00:00 2001 From: Alajmah <55954113+Alajmah@users.noreply.github.com> Date: Fri, 20 Nov 2026 13:12:15 +0300 Subject: [PATCH 32/45] test(dom): model observed Copy-without-article October geometry --- tests/test_current_dom_role_selectors.py | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/tests/test_current_dom_role_selectors.py b/tests/test_current_dom_role_selectors.py index 2ddbd470..636c0ae8 100644 --- a/tests/test_current_dom_role_selectors.py +++ b/tests/test_current_dom_role_selectors.py @@ -34,7 +34,7 @@ getClientRects: () => [{}], getBoundingClientRect: () => ({top:100, bottom:180}), getAttribute: k => k === 'aria-label' ? kind : null, - closest: selector => selector.includes('article') ? root : null, + closest: selector => state.rootFound === false ? null : (selector.includes('article') ? root : null), }; } const root = { @@ -462,7 +462,8 @@ async def evaluate(expr, timeout=15): if "copyButtons" in expr and "has_action" in expr: assert expr == phase2_js return _eval_js( - phase2_js, "current", copies=1, answer=sentinel + phase2_js, "current", copies=1, + answer=sentinel, rootFound=False, ) raise AssertionError(expr) @@ -475,3 +476,17 @@ async def evaluate(expr, timeout=15): d._fetch_text_for_turn.assert_awaited_once() d.click_send.assert_awaited_once() assert d._completion.last_dom_text == "" # zero untrusted DOM bytes + + + +@pytest.mark.asyncio +async def test_copy_button_without_any_turn_root_keeps_dom_text_empty(): + """October live DOM: no article or conversation-turn ancestor for Copy.""" + js = await _actual_phase2_expression(0) + result = json.loads( + _eval_js(js, "current", copies=1, rootFound=False) + ) + assert result["copy_delta"] is True + assert result["text"] == "" + assert result["md_text"] == "" + assert result["html_len"] == 0 From e98db918147b2587c2a1cab7a717de49b404f3ec Mon Sep 17 00:00:00 2001 From: Alajmah <55954113+Alajmah@users.noreply.github.com> Date: Fri, 20 Nov 2026 13:13:35 +0300 Subject: [PATCH 33/45] test(dom): exercise fresh-chat mid-loop conversation-ID discovery before one backend read --- tests/test_current_dom_role_selectors.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/test_current_dom_role_selectors.py b/tests/test_current_dom_role_selectors.py index 636c0ae8..a2b51a58 100644 --- a/tests/test_current_dom_role_selectors.py +++ b/tests/test_current_dom_role_selectors.py @@ -423,7 +423,8 @@ async def test_live_shape_driver_fetches_anchored_text_once_after_end_turn(): conv_id = "6b0016ad-different-from-the-prior-live-conversation" phase2_js = await _actual_phase2_expression(0) d = CDPDriver(cdp_port=9222) - d._current_conv_id = conv_id + d._current_conv_id = None # true fresh-chat path + d._get_live_conversation_id_best_effort = AsyncMock(return_value=conv_id) d._identity_listener = None d._assert_owned_tab_required = MagicMock() d._read_assistant_count_baseline = AsyncMock(return_value=0) From da0f948315a5cb45d5e8b4962ef9d6626767d398 Mon Sep 17 00:00:00 2001 From: Alajmah <55954113+Alajmah@users.noreply.github.com> Date: Fri, 20 Nov 2026 16:44:32 +0300 Subject: [PATCH 34/45] fix(detector): strip transient standalone ChatGPT role header before DOM delta --- src/chatgpt_web2api/completion_detector.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/chatgpt_web2api/completion_detector.py b/src/chatgpt_web2api/completion_detector.py index 68723c01..e5d14937 100644 --- a/src/chatgpt_web2api/completion_detector.py +++ b/src/chatgpt_web2api/completion_detector.py @@ -540,8 +540,17 @@ async def stream_until_complete( # such as "ChatGPT said:" and action-toolbar labels. With # no semantic content descendant, its innerText is NOT # assistant text and must not be streamed optimistically. - " var text = mdText || (fromCopyRoot ? '' :" - " rawText.replace(/^Think(ing|\\s+for)[^\\n]*\\n?/i, ''));" + # The fallback can briefly bind a legacy role wrapper + # during UI hydration. Its screen-reader h4 label may be + # the ONLY text: "ChatGPT said:". Strip that standalone + # role header BEFORE any StreamChunk can be committed. + # Never remove same-line actual answer content or modify + # text from a semantic .markdown/.prose container. + " var fallbackText = rawText" + " .replace(/^Think(ing|\\s+for)[^\\n]*\\n?/i, '')" + " .replace(/^ChatGPT said:(?:[ \\t]*\\r?\\n|$)/i, '')" + " .trim();" + " var text = mdText || (fromCopyRoot ? '' : fallbackText);" # A current article includes an action toolbar whose # HTML can exceed 50 chars even with ZERO answer content. # Do not mistake that wrapper for non-text assistant From 54c4b0921cae871db2b3551d6119c3a25afed20e Mon Sep 17 00:00:00 2001 From: Alajmah <55954113+Alajmah@users.noreply.github.com> Date: Fri, 20 Nov 2026 16:45:17 +0300 Subject: [PATCH 35/45] test(dom): reproduce transient role-label-only hydration and preserve real answer text --- tests/test_current_dom_role_selectors.py | 64 ++++++++++++++++++++++-- 1 file changed, 60 insertions(+), 4 deletions(-) diff --git a/tests/test_current_dom_role_selectors.py b/tests/test_current_dom_role_selectors.py index a2b51a58..f7963a6d 100644 --- a/tests/test_current_dom_role_selectors.py +++ b/tests/test_current_dom_role_selectors.py @@ -61,7 +61,7 @@ }; const copies = Array.from({length:state.copies ?? (mode === 'current' ? 1 : 0)}, () => el('Copy')); const edits = Array.from({length:state.edits ?? (mode === 'current' ? 1 : 0)}, () => el('Edit message')); -const legacyAssistants = mode === 'legacy' ? [root] : []; +const legacyAssistants = mode === 'legacy' || state.transientLegacyAssistant ? [root] : []; const legacyUsers = mode === 'legacy' ? [el('user')] : []; const hiddenMenus = state.hiddenMenus || []; // irrelevant role=menuitem nodes const assistantExtras = ['Read aloud', 'Share', 'More actions']; @@ -412,7 +412,10 @@ async def evaluate(js, timeout=15): @pytest.mark.asyncio -async def test_live_shape_driver_fetches_anchored_text_once_after_end_turn(): +@pytest.mark.parametrize("transient_legacy_assistant", [False, True]) +async def test_live_shape_driver_fetches_anchored_text_once_after_end_turn( + transient_legacy_assistant, +): """One Send, current DOM with no assistant root, two DIFFERENT snapshots. v1 and v2 are intentionally divergent to reproduce the confirmed @@ -463,8 +466,11 @@ async def evaluate(expr, timeout=15): if "copyButtons" in expr and "has_action" in expr: assert expr == phase2_js return _eval_js( - phase2_js, "current", copies=1, - answer=sentinel, rootFound=False, + phase2_js, "current", + copies=0 if transient_legacy_assistant else 1, + answer="" if transient_legacy_assistant else sentinel, + transientLegacyAssistant=transient_legacy_assistant, + rootFound=False, ) raise AssertionError(expr) @@ -491,3 +497,53 @@ async def test_copy_button_without_any_turn_root_keeps_dom_text_empty(): assert result["text"] == "" assert result["md_text"] == "" assert result["html_len"] == 0 + + +@pytest.mark.asyncio +async def test_transient_legacy_role_label_then_empty_dom_before_copy(): + """A hydration-only legacy assistant wrapper must never emit h4 chrome. + + Simulates the observed first poll "ChatGPT said:" followed by disappearance + of the transient role attribute and finally a completed Copy affordance + without an article/turn ancestor. The actual detector JS is executed in + Node vm for all three snapshots. + """ + script = await _actual_phase2_expression(0) + snapshots = [ + dict(copies=0, edits=1, transientLegacyAssistant=True, answer=""), + dict(copies=0, edits=1, transientLegacyAssistant=False, answer=""), + dict(copies=1, edits=1, rootFound=False, answer="PR57-LIVE-dd2290aa"), + ] + values = [json.loads(_eval_js(script, "current", **snapshot)) + for snapshot in snapshots] + assert all(value["text"] == "" for value in values) + assert all(value["md_text"] == "" for value in values) + assert [value["copy_delta"] for value in values] == [ + False, False, True, + ] + + +@pytest.mark.asyncio +async def test_role_label_removed_only_when_standalone_and_only_in_raw_fallback(): + """Protect legitimate content and legacy semantic markdown from regex. + + A line-level role heading may be removed from raw innerText, but a reply + that literally starts "ChatGPT said:" on its own content line must remain + untouched when that content follows the separate header. + """ + script = await _actual_phase2_expression(0) + sentinel = "PR57-LIVE-dd2290aa" + transient = json.loads(_eval_js( + script, "current", copies=0, transientLegacyAssistant=True, + answer=sentinel, + )) + assert transient["text"] == sentinel + literal = "ChatGPT said: literal answer text" + raw = json.loads(_eval_js( + script, "current", copies=0, transientLegacyAssistant=True, + answer=literal, + )) + assert raw["text"] == literal + semantic = json.loads(_eval_js(script, "legacy", answer=literal)) + assert semantic["md_text"] == literal + assert semantic["text"] == literal From 5d3f1d806430c3def67e156bf1db35dd55fdad38 Mon Sep 17 00:00:00 2001 From: Alajmah <55954113+Alajmah@users.noreply.github.com> Date: Fri, 20 Nov 2026 23:45:11 +0300 Subject: [PATCH 36/45] fix(reconciliation): surface only turn-anchored conversation ID from detector --- src/chatgpt_web2api/completion_detector.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/chatgpt_web2api/completion_detector.py b/src/chatgpt_web2api/completion_detector.py index e5d14937..92f75557 100644 --- a/src/chatgpt_web2api/completion_detector.py +++ b/src/chatgpt_web2api/completion_detector.py @@ -248,6 +248,9 @@ def __init__(self, driver) -> None: # Per-call results surfaced for the driver tail; reset each call. self.last_dom_text: str = "" self.had_non_text_content: bool = False + # Only the exact-turn backend completion selector can attest this ID. + # A DOM action button alone must never populate the fallback ID. + self.last_anchored_conv_id: str = "" async def _reconcile_before_stall( self, d, conv_id: str, turn_anchor, had_non_text_content: bool, @@ -281,7 +284,10 @@ async def _reconcile_before_stall( had_non_text_content=had_non_text_content, ) status = collapse_to_end_turn_status(end_result) - return status == "complete" + if status == "complete": + self.last_anchored_conv_id = conv_id + return True + return False except AuthExpiredError: raise # never swallow auth expiry — it must surface as auth expiry except Exception as e: @@ -349,6 +355,7 @@ async def stream_until_complete( # Reset per-call results surfaced to the driver tail. self.last_dom_text = "" self.had_non_text_content = False + self.last_anchored_conv_id = "" # Wait for a new assistant message. The full `timeout` governs (was # capped at 60s, which killed slow-to-appear responses like image @@ -784,6 +791,7 @@ async def stream_until_complete( ) status = collapse_to_end_turn_status(end_result) if status == "complete": + self.last_anchored_conv_id = conv_id_for_check # STRICT completion is established by the turn-anchored # selector, not a generic end_turn flag. Its "matched" # status requires an exact-turn assistant with From c50a81d0eb39dc64db31141aa53e77f5f27f5056 Mon Sep 17 00:00:00 2001 From: Alajmah <55954113+Alajmah@users.noreply.github.com> Date: Fri, 20 Nov 2026 23:45:31 +0300 Subject: [PATCH 37/45] fix(driver): fail closed without conversation ID; accept only anchored backend fallback --- src/chatgpt_web2api/cdp_driver.py | 65 +++++++++++++++++++++++++++---- 1 file changed, 57 insertions(+), 8 deletions(-) diff --git a/src/chatgpt_web2api/cdp_driver.py b/src/chatgpt_web2api/cdp_driver.py index 3dbeba07..7d47e998 100644 --- a/src/chatgpt_web2api/cdp_driver.py +++ b/src/chatgpt_web2api/cdp_driver.py @@ -1820,18 +1820,67 @@ async def send_and_stream( ): yield chunk - # Wait for URL to become /c/{id} + # R1: Prefer the ID independently confirmed by the detector's + # *anchored* end_turn selector. This is NOT the old _current_conv_id + # or an uncorrelated DOM Copy button. Verify ownership, the + # pre-send anchor, and any currently visible conversation URL. + # Otherwise use the bounded live-URL probe as before. + trusted_id = getattr(self._completion, "last_anchored_conv_id", "") + if not isinstance(trusted_id, str): + trusted_id = "" conv_id = "" - for _ in range(30): + if trusted_id: + self._assert_owned_tab_required() + expected_id = turn_anchor.conversation_id_at_capture + if expected_id and trusted_id != expected_id: + raise TurnReconciliationError( + conversation_id=trusted_id, + anchor_mode=turn_anchor.mode, + last_status="conversation_id_anchor_mismatch", + diagnostic={"captured_id": turn_anchor.captured_user_message_id, + "expected_id": expected_id}, + ) try: - url = await self._js_strict("window.location.href") + live_url = await self._js_strict("window.location.href") except CDPJSError: + live_url = "" + if isinstance(live_url, str) and "/c/" in live_url: + live_id = live_url.split("/c/")[1].split("/")[0].split("?")[0] + if live_id != trusted_id: + raise TurnReconciliationError( + conversation_id=trusted_id, + anchor_mode=turn_anchor.mode, + last_status="conversation_id_mismatch", + diagnostic={"captured_id": turn_anchor.captured_user_message_id, + "url_conversation_id": live_id}, + ) + conv_id = trusted_id + else: + # Without backend proof, the only usable ID is the live + # conversation URL. No old/stale _current_conv_id fallback. + for _ in range(30): + try: + url = await self._js_strict("window.location.href") + except CDPJSError: + await asyncio.sleep(0.5) + continue + if isinstance(url, str) and "/c/" in url: + conv_id = url.split("/c/")[1].split("/")[0].split("?")[0] + if conv_id: + break await asyncio.sleep(0.5) - continue - if "/c/" in url: - conv_id = url.split("/c/")[1].split("/")[0].split("?")[0] - break - await asyncio.sleep(0.5) + + # The driver is now the SOLE text owner. Skipping reconciliation + # and emitting finish_reason=stop would create an HTTP 200 with + # empty/unverified text in both REST and SSE. + if not conv_id: + raise TurnReconciliationError( + conversation_id="", + anchor_mode=turn_anchor.mode, + last_status="conversation_id_unavailable", + diagnostic={"captured_id": turn_anchor.captured_user_message_id, + "reason": "No trusted backend ID or conversation URL"}, + ) if conv_id: logger.info("Conversation: %s", conv_id) From 9b59567807871deced59c37f09a5cd3734093700 Mon Sep 17 00:00:00 2001 From: Alajmah <55954113+Alajmah@users.noreply.github.com> Date: Fri, 20 Nov 2026 23:45:47 +0300 Subject: [PATCH 38/45] fix(detector): ignore transient role-label-only wrapper as non-text completion evidence --- src/chatgpt_web2api/completion_detector.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/chatgpt_web2api/completion_detector.py b/src/chatgpt_web2api/completion_detector.py index 92f75557..d09be8d7 100644 --- a/src/chatgpt_web2api/completion_detector.py +++ b/src/chatgpt_web2api/completion_detector.py @@ -563,8 +563,16 @@ async def stream_until_complete( # Do not mistake that wrapper for non-text assistant # content (which would bypass strict backend text gating). " var hasRichContent = !!last.querySelector('img, canvas, video, audio');" - " var html_len = fromCopyRoot && !hasRichContent ? 0 : last.innerHTML.length;" - " var child_count = fromCopyRoot && !hasRichContent ? 0 : last.children.length;" + # R3: during hydration a transient legacy role node can + # contain ONLY the screen-reader role label yet have a + # large HTML wrapper. Its label is already stripped from + # text; do not set the sticky non-text evidence flag from + # its empty UI shell. Real image/media content is retained. + " var roleChromeOnly = !mdText && !hasRichContent" + " && /^ChatGPT said:\\s*$/i.test(rawText);" + " var wrapperOnly = (fromCopyRoot && !hasRichContent) || roleChromeOnly;" + " var html_len = wrapperOnly ? 0 : last.innerHTML.length;" + " var child_count = wrapperOnly ? 0 : last.children.length;" # has_action: the per-turn copy/feedback action row appears # only on a COMPLETED message. ChatGPT's DOM layout puts these # buttons in a SIBLING/UNCLE container, NOT as descendants of From d980a0ca1f680aafdc72fda8ded2591ed3610615 Mon Sep 17 00:00:00 2001 From: Alajmah <55954113+Alajmah@users.noreply.github.com> Date: Fri, 20 Nov 2026 23:46:07 +0300 Subject: [PATCH 39/45] fix(dom): snapshot aria and legacy turn signals independently for mixed layouts --- src/chatgpt_web2api/dom_turn_signals.py | 44 +++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/src/chatgpt_web2api/dom_turn_signals.py b/src/chatgpt_web2api/dom_turn_signals.py index 11eda059..6011a870 100644 --- a/src/chatgpt_web2api/dom_turn_signals.py +++ b/src/chatgpt_web2api/dom_turn_signals.py @@ -34,3 +34,47 @@ f"document.querySelectorAll('{LEGACY_USER_SELECTOR}').length" ")" ) + + +# R2: Math.max is ONLY a legacy numeric presentation. It is not suitable +# for observing new turns in a mixed-layout page: max(4 legacy, 0 Copy) and +# max(4 legacy, 1 Copy) are both 4. Sample the four representations together, +# then compare each signal to its *own* pre-send baseline. +TURN_SIGNAL_COUNTS_JS = ( + "(function(){return JSON.stringify({" + f"copy:document.querySelectorAll('{COPY_BUTTON_SELECTOR}').length," + f"legacy_assistant:document.querySelectorAll('{LEGACY_ASSISTANT_SELECTOR}').length," + f"edit:document.querySelectorAll('{EDIT_MESSAGE_BUTTON_SELECTOR}').length," + f"legacy_user:document.querySelectorAll('{LEGACY_USER_SELECTOR}').length" + "});})()" +) + + +def parse_turn_signal_counts(raw: str) -> dict[str, int]: + """Reject partial/invalid DOM snapshots rather than assuming zero. + + The snapshot is an observation of four independent signals; a missing or + malformed counter must NEVER manufacture a new-turn delta. + """ + import json + + value = json.loads(raw) + keys = ("copy", "legacy_assistant", "edit", "legacy_user") + if not isinstance(value, dict) or any( + type(value.get(key)) is not int or value[key] < 0 for key in keys + ): + raise ValueError("Invalid turn signal count snapshot") + return {key: value[key] for key in keys} + + +def turn_signal_increased( + current: dict[str, int], baseline: dict[str, int], role: str, +) -> bool: + """Compare signals independently; no Math.max masking across layouts.""" + if role == "assistant": + keys = ("copy", "legacy_assistant") + elif role == "user": + keys = ("edit", "legacy_user") + else: + raise ValueError(f"Unsupported turn-signal role: {role}") + return any(current[key] > baseline[key] for key in keys) From 443adb0e34630492153f83c7e290d3440660ff17 Mon Sep 17 00:00:00 2001 From: Alajmah <55954113+Alajmah@users.noreply.github.com> Date: Fri, 20 Nov 2026 23:46:45 +0300 Subject: [PATCH 40/45] fix(send): atomic mixed-DOM baseline and per-signal acceptance delta --- src/chatgpt_web2api/cdp_driver.py | 88 +++++++++++++++---------------- 1 file changed, 44 insertions(+), 44 deletions(-) diff --git a/src/chatgpt_web2api/cdp_driver.py b/src/chatgpt_web2api/cdp_driver.py index 7d47e998..67fda368 100644 --- a/src/chatgpt_web2api/cdp_driver.py +++ b/src/chatgpt_web2api/cdp_driver.py @@ -85,8 +85,10 @@ class StreamChunk: SEND_BUTTON_SELECTOR, ) from .dom_turn_signals import ( # noqa: E402 - ASSISTANT_TURN_COUNT_JS, - USER_TURN_COUNT_JS, + EDIT_MESSAGE_BUTTON_SELECTOR, + LEGACY_USER_SELECTOR, + TURN_SIGNAL_COUNTS_JS, + parse_turn_signal_counts, ) # ── P2: Navigation readiness probe ──────────────────────────────────────── @@ -1502,58 +1504,38 @@ async def _read_assistant_count_baseline(self) -> int: """ import time as _time - selector = ASSISTANT_TURN_COUNT_JS - user_selector = USER_TURN_COUNT_JS + # One atomic pre-send DOM snapshot. The four counts are kept + # separately because Math.max masks growth in mixed old/new layouts. + # Bad/partial snapshots retry and ultimately fail closed before Send. + self._pre_send_signal_counts = None + self._pre_send_user_count = None max_attempts = 3 for attempt in range(1, max_attempts + 1): t0 = _time.monotonic() - err: Exception | None = None try: - raw = await self._js_strict(selector) - except CDPJSError as e: - err = e + raw = await self._js_strict(TURN_SIGNAL_COUNTS_JS) + signals = parse_turn_signal_counts(raw) + except (CDPJSError, ValueError, TypeError) as err: + pass else: - try: - # Explicit parse — do NOT use truthiness (numeric 0 from - # CDP is falsy but valid for a fresh chat). ChatGPT's - # review caught that `raw and int(raw)` rejects numeric 0. - count = int(raw) - except (ValueError, TypeError) as e: - err = e - else: - if count < 0: - err = ValueError(f"negative assistant count: {count}") - - # If we got a valid count, log + return. - if err is None: + count = max(signals["copy"], signals["legacy_assistant"]) + user_count = max(signals["edit"], signals["legacy_user"]) + self._pre_send_signal_counts = signals + self._pre_send_user_count = user_count elapsed_ms = int((_time.monotonic() - t0) * 1000) - # Best-effort user-count for diagnostics (non-fatal). - try: - user_raw = await self._js_strict(user_selector) - user_count = int(user_raw) - except (CDPJSError, ValueError, TypeError): - user_count = None logger.info( "send_baseline: attempt=%d assistant_count=%d " "user_count=%s elapsed_ms=%d conv_id=%s", - attempt, - count, - user_count, - elapsed_ms, + attempt, count, user_count, elapsed_ms, self._current_conv_id or "(none)", ) - # Store for send-acknowledgment baseline (ChatGPT review A). - self._pre_send_user_count = user_count return count - # Retry or fail-closed. if attempt < max_attempts: logger.warning( "send_baseline_failed: attempt=%d error=%s " "conv_id=%s — retrying", - attempt, - err, - self._current_conv_id or "(none)", + attempt, err, self._current_conv_id or "(none)", ) await asyncio.sleep(0.3 * attempt) else: @@ -1561,9 +1543,7 @@ async def _read_assistant_count_baseline(self) -> int: "send_baseline_unavailable: attempts=%d last_error=%s " "conv_id=%s — refusing to send with untrusted baseline " "(stale-return risk)", - attempt, - err, - self._current_conv_id or "(none)", + attempt, err, self._current_conv_id or "(none)", ) raise SendReadinessError( f"Cannot establish pre-send assistant-count baseline " @@ -1597,6 +1577,7 @@ async def _verify_send_acknowledged(self) -> bool | None: from .chatgpt_dom import COMPOSER_FALLBACK_SELECTOR, COMPOSER_SELECTOR pre_send_count = getattr(self, "_pre_send_user_count", None) + pre_signals = getattr(self, "_pre_send_signal_counts", None) if pre_send_count is None: # No baseline — can't verify a delta. Non-blocking. return None @@ -1607,12 +1588,14 @@ async def _verify_send_acknowledged(self) -> bool | None: try: result = await self._js_strict( "(function() {" - f" var userMsgs = {USER_TURN_COUNT_JS};" + f" var editCount = document.querySelectorAll('{EDIT_MESSAGE_BUTTON_SELECTOR}').length;" + f" var legacyCount = document.querySelectorAll('{LEGACY_USER_SELECTOR}').length;" + " var userMsgs = Math.max(editCount, legacyCount);" f" var composer = document.querySelector('{COMPOSER_SELECTOR}')" f" || document.querySelector('{COMPOSER_FALLBACK_SELECTOR}');" " var composerPresent = !!composer;" " var composerEmpty = composer ? !(composer.innerText || composer.value || '').trim() : false;" - " return JSON.stringify({userCount: userMsgs, composerPresent: composerPresent, composerEmpty: composerEmpty});" + " return JSON.stringify({userCount: userMsgs, editCount: editCount, legacyCount: legacyCount, composerPresent: composerPresent, composerEmpty: composerEmpty});" "})()" ) if not result or not result.strip().startswith("{"): @@ -1627,9 +1610,23 @@ async def _verify_send_acknowledged(self) -> bool | None: continue # wait for next poll — might be transient # Only count as a valid probe when the composer is present # and we can actually evaluate the acknowledgment condition. + if pre_signals is not None: + edit_count = state.get("editCount") + legacy_count = state.get("legacyCount") + if ( + type(edit_count) is not int or type(legacy_count) is not int + or edit_count < 0 or legacy_count < 0 + ): + return None # no trustworthy per-signal observation + user_delta = ( + edit_count > pre_signals["edit"] + or legacy_count > pre_signals["legacy_user"] + ) + else: + # Compatibility with callers that mock the baseline read. + user_delta = state.get("userCount", 0) > pre_send_count valid_probe_seen = True - current_count = state.get("userCount", 0) - if current_count > pre_send_count and state.get("composerEmpty"): + if user_delta and state.get("composerEmpty"): return True except Exception: pass @@ -1744,6 +1741,9 @@ async def send_and_stream( # PR4 belt-and-suspenders: refuse to mutate the DOM in parallel mode. self._assert_owned_tab_required() # A1: count existing assistants BEFORE sending (fail-closed baseline). + # Clear stale per-signal state even if a test or subclass overrides + # _read_assistant_count_baseline. + self._pre_send_signal_counts = None initial_count = await self._read_assistant_count_baseline() # A2 Step 2: identity-listener health check. From 9f8d192da2dbde6e3d315c00ac07852283670f96 Mon Sep 17 00:00:00 2001 From: Alajmah <55954113+Alajmah@users.noreply.github.com> Date: Fri, 20 Nov 2026 23:47:08 +0300 Subject: [PATCH 41/45] fix(detector): compare aria and legacy counts against independent pre-send baselines --- src/chatgpt_web2api/completion_detector.py | 83 +++++++++++++++------- 1 file changed, 57 insertions(+), 26 deletions(-) diff --git a/src/chatgpt_web2api/completion_detector.py b/src/chatgpt_web2api/completion_detector.py index d09be8d7..57194509 100644 --- a/src/chatgpt_web2api/completion_detector.py +++ b/src/chatgpt_web2api/completion_detector.py @@ -77,7 +77,10 @@ ASSISTANT_TURN_COUNT_JS, COPY_BUTTON_SELECTOR, LEGACY_ASSISTANT_SELECTOR, + TURN_SIGNAL_COUNTS_JS, USER_TURN_COUNT_JS, + parse_turn_signal_counts, + turn_signal_increased, ) logger = logging.getLogger(__name__) @@ -370,6 +373,13 @@ async def stream_until_complete( last_node_count = initial_count last_progress = time.monotonic() user_turn_seen = False + # When supplied by the production driver, the atomic baseline keeps + # aria and legacy counters independent; mixed-layout growth must not + # disappear behind a Math.max of the two representations. + pre_signals = getattr(d, "_pre_send_signal_counts", None) + if not isinstance(pre_signals, dict): + pre_signals = None # compatibility with lightweight detector mocks + last_observed_signals = pre_signals while time.monotonic() < deadline: # First check for ChatGPT's rate-limit pop-up — if present, fail # fast with a clear error instead of waiting out the whole timeout. @@ -390,33 +400,49 @@ async def stream_until_complete( # from_text parses any explicit wait from the pop-up copy. raise RateLimitError.from_text(scanned_text) - try: - raw = await d._js_strict(ASSISTANT_TURN_COUNT_JS) - current_count = int(raw or 0) - except CDPJSError: - current_count = last_node_count # no progress signal - if current_count != last_node_count: - # Any node-count change is progress (incl. 0→1 with empty text, - # the slow-render case). Reset the stall clock. - last_node_count = current_count - last_progress = time.monotonic() - if current_count > initial_count: - break - - # The current UI only adds the per-turn Copy affordance when - # the assistant FINISHES, so an assistant-count delta alone cannot - # start phase 2. The new user message's Edit message control is - # available earlier. It permits observation, NOT completion. - pre_user_count = getattr(d, "_pre_send_user_count", None) - if pre_user_count is not None: + if pre_signals is not None: try: - user_raw = await d._js_strict(USER_TURN_COUNT_JS) - if int(user_raw) > pre_user_count: + raw = await d._js_strict(TURN_SIGNAL_COUNTS_JS) + signals = parse_turn_signal_counts(raw) + except (CDPJSError, ValueError, TypeError): + signals = None + if signals is not None: + # Independent changes count as progress even when their + # Math.max presentation remains unchanged (e.g. 4 legacy, + # Copy 0 -> 1). This is an observation, not completion. + if signals != last_observed_signals: + last_progress = time.monotonic() + last_observed_signals = signals + if turn_signal_increased(signals, pre_signals, "assistant"): + break + if turn_signal_increased(signals, pre_signals, "user"): user_turn_seen = True - logger.debug("User turn appeared via Edit-message delta") + logger.debug("User turn appeared via independent signal delta") break - except (CDPJSError, ValueError, TypeError): - pass + else: + # Compatibility path for tests/older callers that supply only + # a numeric count; production must collect a full snapshot + # before sending, or fail closed. + try: + raw = await d._js_strict(ASSISTANT_TURN_COUNT_JS) + current_count = int(raw or 0) + except CDPJSError: + current_count = last_node_count # no progress signal + if current_count != last_node_count: + last_node_count = current_count + last_progress = time.monotonic() + if current_count > initial_count: + break + pre_user_count = getattr(d, "_pre_send_user_count", None) + if pre_user_count is not None: + try: + user_raw = await d._js_strict(USER_TURN_COUNT_JS) + if int(user_raw) > pre_user_count: + user_turn_seen = True + logger.debug("User turn appeared via Edit-message delta") + break + except (CDPJSError, ValueError, TypeError): + pass if time.monotonic() - last_progress > PHASE_STALL_SECONDS: raise GenerationStuckError("phase_1_appear", time.monotonic() - last_progress) @@ -510,16 +536,21 @@ async def stream_until_complete( # URL (cheap) until a conv_id is available, then the existing backend # check can fire. See _get_live_conversation_id_best_effort. last_conv_id_probe = 0.0 + pre_copy_count = pre_signals["copy"] if pre_signals is not None else initial_count + pre_legacy_count = ( + pre_signals["legacy_assistant"] + if pre_signals is not None else initial_count + ) while time.monotonic() < deadline: try: result = await d._js_strict( "(function() {" f" var msgs = document.querySelectorAll('{LEGACY_ASSISTANT_SELECTOR}');" f" var copyButtons = document.querySelectorAll('{COPY_BUTTON_SELECTOR}');" - f" var copyDelta = copyButtons.length > {initial_count};" + f" var copyDelta = copyButtons.length > {pre_copy_count};" # The old assistant node must not be read while a new # USER node has appeared but no new assistant node yet. - f" var last = msgs.length > {initial_count} ? msgs[msgs.length - 1] : null;" + f" var last = msgs.length > {pre_legacy_count} ? msgs[msgs.length - 1] : null;" # New layout: the completed Copy affordance anchors a # semantic turn container; never fall back to body text. " var fromCopyRoot = false;" From 47a3e3eac26b796d1aa8ac7c561094cc6c37d90f Mon Sep 17 00:00:00 2001 From: Alajmah <55954113+Alajmah@users.noreply.github.com> Date: Fri, 20 Nov 2026 23:47:26 +0300 Subject: [PATCH 42/45] test(send): exercise atomic mixed-DOM baseline and fail-closed invalid counters --- tests/test_send_baseline.py | 55 +++++++++++++++++++++++++++++++------ 1 file changed, 47 insertions(+), 8 deletions(-) diff --git a/tests/test_send_baseline.py b/tests/test_send_baseline.py index 32405ab3..023f5910 100644 --- a/tests/test_send_baseline.py +++ b/tests/test_send_baseline.py @@ -13,6 +13,7 @@ - The returned count is never 0 when the DOM actually has assistants. """ +import json from unittest.mock import AsyncMock import pytest @@ -20,6 +21,13 @@ from chatgpt_web2api.cdp_driver import CDPDriver, CDPJSError, SendReadinessError +def _snapshot(copy=0, legacy_assistant=0, edit=0, legacy_user=0): + return json.dumps({ + "copy": copy, "legacy_assistant": legacy_assistant, + "edit": edit, "legacy_user": legacy_user, + }) + + def _make_driver() -> CDPDriver: """A minimal CDPDriver with just enough state for the baseline helper.""" d = CDPDriver.__new__(CDPDriver) @@ -32,7 +40,7 @@ def _make_driver() -> CDPDriver: async def test_baseline_succeeds_first_try(monkeypatch): """Normal path: _js_strict returns a valid count on the first attempt.""" d = _make_driver() - d._js_strict = AsyncMock(side_effect=["3", "5"]) # assistant=3, user=5 + d._js_strict = AsyncMock(return_value=_snapshot(copy=3, edit=5)) # Patch asyncio.sleep so retries don't actually wait import chatgpt_web2api.cdp_driver as drv_mod @@ -47,7 +55,7 @@ async def test_baseline_retries_then_succeeds(monkeypatch): """First attempt fails (CDPJSError), second succeeds → returns count.""" d = _make_driver() d._js_strict = AsyncMock( - side_effect=[CDPJSError("timeout"), "2", "1"] # fail, then assistant=2, user=1 + side_effect=[CDPJSError("timeout"), _snapshot(copy=2, edit=1)] ) import chatgpt_web2api.cdp_driver as drv_mod @@ -105,7 +113,7 @@ async def test_baseline_logs_diagnostics(monkeypatch, caplog): import logging d = _make_driver() - d._js_strict = AsyncMock(side_effect=["7", "4"]) # assistant=7, user=4 + d._js_strict = AsyncMock(return_value=_snapshot(copy=7, edit=4)) d._current_conv_id = "test-conv-123" import chatgpt_web2api.cdp_driver as drv_mod @@ -125,12 +133,10 @@ async def test_baseline_logs_diagnostics(monkeypatch, caplog): @pytest.mark.asyncio async def test_baseline_accepts_numeric_zero_when_dom_is_empty(monkeypatch): - """If the DOM genuinely has 0 assistant messages (fresh chat), count=0 is - correct and must be accepted — it's not a failure, just a new conversation. - CDP returns numeric 0, not string '0' — ChatGPT's review caught that the - truthiness check rejected numeric 0.""" + """A full four-counter snapshot of zeros is valid for a fresh chat. + Count=0 is not a transport failure or a reason to fabricate stale turns.""" d = _make_driver() - d._js_strict = AsyncMock(side_effect=[0, 0]) # numeric 0, not string + d._js_strict = AsyncMock(return_value=_snapshot()) # valid zero counts import chatgpt_web2api.cdp_driver as drv_mod monkeypatch.setattr(drv_mod.asyncio, "sleep", AsyncMock()) @@ -151,3 +157,36 @@ async def test_baseline_malformed_string_retries_then_fails(monkeypatch): with pytest.raises(SendReadinessError, match="Cannot establish pre-send"): await d._read_assistant_count_baseline() + + + +@pytest.mark.asyncio +async def test_mixed_layout_snapshot_preserves_each_counter(): + """max(4 legacy, 0 Copy) == max(4 legacy, 1 Copy), but the raw + baseline must retain Copy=0 so phase-1 and action rows detect growth. + """ + d = _make_driver() + d._js_strict = AsyncMock( + return_value=_snapshot(copy=0, legacy_assistant=4, + edit=0, legacy_user=4) + ) + count = await d._read_assistant_count_baseline() + assert count == 4 + assert d._pre_send_user_count == 4 + assert d._pre_send_signal_counts == { + "copy": 0, "legacy_assistant": 4, + "edit": 0, "legacy_user": 4, + } + d._js_strict.assert_awaited_once() + + +@pytest.mark.asyncio +async def test_malformed_mixed_snapshot_fails_closed_before_send(monkeypatch): + d = _make_driver() + d._js_strict = AsyncMock(return_value=_snapshot(copy=-1, legacy_assistant=4)) + import chatgpt_web2api.cdp_driver as drv_mod + monkeypatch.setattr(drv_mod.asyncio, "sleep", AsyncMock()) + with pytest.raises(SendReadinessError, match="Cannot establish pre-send"): + await d._read_assistant_count_baseline() + assert d._js_strict.await_count == 3 + assert d._pre_send_signal_counts is None From 5cb2ff7333323f61fea75eaea0e6a524efff8c86 Mon Sep 17 00:00:00 2001 From: Alajmah <55954113+Alajmah@users.noreply.github.com> Date: Fri, 20 Nov 2026 23:47:40 +0300 Subject: [PATCH 43/45] test(detector): assert trusted conversation ID resets and requires anchored end_turn --- tests/test_completion_detector.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/tests/test_completion_detector.py b/tests/test_completion_detector.py index d7a50cf2..5b1b8972 100644 --- a/tests/test_completion_detector.py +++ b/tests/test_completion_detector.py @@ -92,7 +92,10 @@ def test_detector_has_only_driver_and_transient_results(): (last_dom_text / had_non_text_content). No long-lived config migrates in.""" detector, _ = _make_detector() own = vars(detector) - assert set(own) == {"_driver", "last_dom_text", "had_non_text_content"}, ( + assert set(own) == { + "_driver", "last_dom_text", "had_non_text_content", + "last_anchored_conv_id", + }, ( f"unexpected instance state on CompletionDetector: {set(own)}" ) @@ -104,6 +107,7 @@ def test_per_call_results_reset_on_each_call(): # Pollute them; the first thing the method does is reset both to defaults. detector.last_dom_text = "stale" detector.had_non_text_content = True + detector.last_anchored_conv_id = "stale-conversation" # The detector calls _js_strict with several distinct JS expressions: # - the rate-limit body scan -> JSON {"text": ...} @@ -143,6 +147,7 @@ async def drain(): # the poll carrying text="hi", last_dom_text reflects THIS call ("hi"). assert detector.last_dom_text == "hi" assert detector.last_dom_text != "stale" + assert detector.last_anchored_conv_id == "conv-1" # ── 4. Back-compat re-exports from cdp_driver ───────────────────────── From 9211d13ee733b7c1e42f8f387b53f22c60d824d0 Mon Sep 17 00:00:00 2001 From: Alajmah <55954113+Alajmah@users.noreply.github.com> Date: Fri, 20 Nov 2026 23:47:57 +0300 Subject: [PATCH 44/45] test(reconcile): fail closed on absent or conflicting conv ID; permit anchored midloop ID --- tests/test_response_text_integrity.py | 62 +++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) diff --git a/tests/test_response_text_integrity.py b/tests/test_response_text_integrity.py index 862b5e35..23a1d06a 100644 --- a/tests/test_response_text_integrity.py +++ b/tests/test_response_text_integrity.py @@ -176,3 +176,65 @@ async def test_whitespace_only_difference_stays_fail_closed_and_private(caplog): assert answer not in caplog.text assert "whitespace_only=True" in caplog.text d.click_send.assert_awaited_once() + + + +@pytest.mark.asyncio +async def test_r1_no_conversation_id_never_finishes_http_200(monkeypatch): + """Zero DOM/backend ID and no /c/ URL cannot be a successful empty REST.""" + d = _driver_with_texts("", "PR57-LIVE-exact") + d._completion.last_anchored_conv_id = "" + d._js_strict = AsyncMock(return_value="https://chatgpt.com/") + + from chatgpt_web2api import cdp_driver as drv_mod + monkeypatch.setattr(drv_mod.asyncio, "sleep", AsyncMock()) + + with pytest.raises( + TurnReconciliationError, match="conversation_id_unavailable" + ) as caught: + _ = [c async for c in d.send_and_stream("one send", timeout=5)] + assert caught.value.last_status == "conversation_id_unavailable" + d._fetch_text_for_turn.assert_not_awaited() + d.click_send.assert_awaited_once() + + +@pytest.mark.asyncio +async def test_r1_anchored_midloop_id_rescues_missing_final_url(): + """Only a matched per-turn backend end_turn can supply a fallback ID.""" + sentinel = "PR57-LIVE-exact" + d = _driver_with_texts("", sentinel) + d._completion.last_anchored_conv_id = "conv-integrity" + d._js_strict = AsyncMock(return_value="https://chatgpt.com/") + chunks = [c async for c in d.send_and_stream("one send", timeout=5)] + assert "".join(c.delta for c in chunks) == sentinel + assert chunks[-1].finish_reason == "stop" + assert d._current_conv_id == "conv-integrity" + d._fetch_text_for_turn.assert_awaited_once() + d.click_send.assert_awaited_once() + + +@pytest.mark.asyncio +@pytest.mark.parametrize("visible_url,anchor_mode,anchor_conversation,expected_status", [ + ("https://chatgpt.com/c/other-conv", "fresh_chat", None, + "conversation_id_mismatch"), + ("https://chatgpt.com/", "existing_conversation", "other-conv", + "conversation_id_anchor_mismatch"), +]) +async def test_r1_trusted_id_refuses_foreign_url_or_anchor( + visible_url, anchor_mode, anchor_conversation, expected_status, +): + d = _driver_with_texts("", "PR57-LIVE-exact") + d._completion.last_anchored_conv_id = "conv-integrity" + d._capture_pre_send_fallback_anchor = AsyncMock( + return_value=TurnAnchor( + sent_text="one send", + mode=anchor_mode, + conversation_id_at_capture=anchor_conversation, + ) + ) + d._js_strict = AsyncMock(return_value=visible_url) + with pytest.raises(TurnReconciliationError) as caught: + _ = [c async for c in d.send_and_stream("one send", timeout=5)] + assert caught.value.last_status == expected_status + d._fetch_text_for_turn.assert_not_awaited() + d.click_send.assert_awaited_once() From 4d035f1d1654052092e962ccd794fd91062de0c3 Mon Sep 17 00:00:00 2001 From: Alajmah <55954113+Alajmah@users.noreply.github.com> Date: Fri, 20 Nov 2026 23:48:37 +0300 Subject: [PATCH 45/45] test(dom): execute mixed-layout counters, send ack, and role-only non-text regression --- tests/test_current_dom_role_selectors.py | 180 ++++++++++++++++++++++- 1 file changed, 177 insertions(+), 3 deletions(-) diff --git a/tests/test_current_dom_role_selectors.py b/tests/test_current_dom_role_selectors.py index f7963a6d..4184b7a5 100644 --- a/tests/test_current_dom_role_selectors.py +++ b/tests/test_current_dom_role_selectors.py @@ -16,7 +16,10 @@ from chatgpt_web2api.completion_detector import CompletionDetector from chatgpt_web2api.dom_turn_signals import ( ASSISTANT_TURN_COUNT_JS, + TURN_SIGNAL_COUNTS_JS, USER_TURN_COUNT_JS, + parse_turn_signal_counts, + turn_signal_increased, ) from chatgpt_web2api.turn_anchor import TurnAnchor, TurnEndResult, TurnTextResult @@ -47,6 +50,9 @@ getBoundingClientRect: () => ({top:100, bottom:180}), querySelector: selector => { if (selector.includes('result-thinking')) return null; + if (selector.includes('img, canvas, video, audio')) { + return state.richContent ? {nodeName:'IMG'} : null; + } if (mode === 'legacy' && selector.includes('markdown')) { return {textContent: state.answer || 'Legacy answer.'}; } @@ -61,13 +67,23 @@ }; const copies = Array.from({length:state.copies ?? (mode === 'current' ? 1 : 0)}, () => el('Copy')); const edits = Array.from({length:state.edits ?? (mode === 'current' ? 1 : 0)}, () => el('Edit message')); -const legacyAssistants = mode === 'legacy' || state.transientLegacyAssistant ? [root] : []; -const legacyUsers = mode === 'legacy' ? [el('user')] : []; +const legacyAssistants = Array.from({ + length:state.legacyAssistantCount ?? + ((mode === 'legacy' || state.transientLegacyAssistant) ? 1 : 0) +}, () => root); +const legacyUsers = Array.from({ + length:state.legacyUserCount ?? (mode === 'legacy' ? 1 : 0) +}, () => el('user')); const hiddenMenus = state.hiddenMenus || []; // irrelevant role=menuitem nodes const assistantExtras = ['Read aloud', 'Share', 'More actions']; const userExtras = ['Share prompt']; const document = { body: {innerText: state.bodyText || ''}, + querySelector: selector => ( + selector.includes('prompt-textarea') || selector.includes('role="textbox"') + ) ? (state.composerPresent === false ? null + : {innerText:state.composerText || '', value:state.composerText || ''}) + : null, querySelectorAll: selector => { if (selector.startsWith('button[aria-label="Copy"]')) return copies; if (selector.startsWith('button[aria-label="Edit message"]')) return edits; @@ -111,16 +127,21 @@ class _CapturedProbe(Exception): pass -async def _actual_phase2_expression(initial_count=0): +async def _actual_phase2_expression(initial_count=0, pre_signals=None): """Collect the actual generated JS, rather than reimplementing it in tests.""" d = MagicMock() d._current_conv_id = None d._pre_send_user_count = None + d._pre_send_signal_counts = pre_signals seen = [] async def _js(expr, timeout=15): if "body.innerText" in expr: return json.dumps({"text": ""}) + if pre_signals is not None and expr == TURN_SIGNAL_COUNTS_JS: + new = dict(pre_signals) + new["copy"] += 1 + return json.dumps(new) if "copyButtons" in expr and "has_action" in expr: seen.append(expr) raise _CapturedProbe @@ -547,3 +568,156 @@ async def test_role_label_removed_only_when_standalone_and_only_in_raw_fallback( semantic = json.loads(_eval_js(script, "legacy", answer=literal)) assert semantic["md_text"] == literal assert semantic["text"] == literal + + + +@pytest.mark.asyncio +async def test_r2_mixed_dom_signals_and_phase2_copy_use_independent_baselines(): + """Real executable JS: four legacy turns mask the new Copy/Edit in max().""" + before = parse_turn_signal_counts(_eval_js( + TURN_SIGNAL_COUNTS_JS, "current", copies=0, edits=0, + legacyAssistantCount=4, legacyUserCount=4, + )) + after = parse_turn_signal_counts(_eval_js( + TURN_SIGNAL_COUNTS_JS, "current", copies=1, edits=1, + legacyAssistantCount=4, legacyUserCount=4, + )) + assert max(before["copy"], before["legacy_assistant"]) == 4 + assert max(after["copy"], after["legacy_assistant"]) == 4 + assert turn_signal_increased(after, before, "assistant") + assert turn_signal_increased(after, before, "user") + + # Capture the actual detector-composed JS for the mixed baseline of 4, + # rather than reimplementing the phase-2 selector in the test. + js = await _actual_phase2_expression(initial_count=4, pre_signals=before) + state = json.loads(_eval_js( + js, "current", copies=1, edits=1, + legacyAssistantCount=4, legacyUserCount=4, rootFound=False, + )) + assert state["copy_delta"] is True + assert state["text"] == "" # old legacy nodes never treated as a new turn + + +@pytest.mark.asyncio +async def test_r2_mixed_dom_appearance_uses_independent_snapshot(): + before = parse_turn_signal_counts(_eval_js( + TURN_SIGNAL_COUNTS_JS, "current", copies=0, edits=0, + legacyAssistantCount=4, legacyUserCount=4, + )) + after = _eval_js( + TURN_SIGNAL_COUNTS_JS, "current", copies=1, edits=1, + legacyAssistantCount=4, legacyUserCount=4, + ) + d = MagicMock() + d._pre_send_signal_counts = before + d._pre_send_user_count = 4 + d._current_conv_id = "conv-mixed" + d._fetch_end_turn_for_turn = AsyncMock( + return_value=TurnEndResult(status="matched") + ) + script = await _actual_phase2_expression(initial_count=4, pre_signals=before) + counts_queried = [0] + + async def evaluate(expr, timeout=15): + if "body.innerText" in expr: + return '{"text": ""}' + if expr == TURN_SIGNAL_COUNTS_JS: + counts_queried[0] += 1 + return after + if "copyButtons" in expr and "has_action" in expr: + assert expr == script + return _eval_js( + expr, "current", copies=1, edits=1, + legacyAssistantCount=4, legacyUserCount=4, + rootFound=False, + ) + raise AssertionError(expr) + + d._js_strict = evaluate + det = CompletionDetector(d) + chunks = [c async for c in det.stream_until_complete( + initial_count=4, timeout=10, + turn_anchor=TurnAnchor(sent_text="one send", mode="fresh_chat"), + )] + assert counts_queried[0] > 0 + assert chunks == [] + assert det.last_anchored_conv_id == "conv-mixed" + d._fetch_end_turn_for_turn.assert_awaited_once() + + +@pytest.mark.asyncio +async def test_r2_mixed_dom_send_acknowledgment_checks_edit_delta(): + before = parse_turn_signal_counts(_eval_js( + TURN_SIGNAL_COUNTS_JS, "current", copies=0, edits=0, + legacyAssistantCount=4, legacyUserCount=4, + )) + d = CDPDriver(cdp_port=9222) + d._pre_send_signal_counts = before + d._pre_send_user_count = 4 + + async def evaluate(expr, timeout=15): + assert "userCount" in expr and "composerEmpty" in expr + return _eval_js( + expr, "current", copies=1, edits=1, + legacyAssistantCount=4, legacyUserCount=4, composerText="", + ) + + d._js_strict = evaluate + assert await d._verify_send_acknowledged() is True + + +@pytest.mark.asyncio +async def test_r3_role_only_html_does_not_set_sticky_non_text_evidence(): + """Real detector loop: tall legacy chrome has no image/tool response.""" + js = await _actual_phase2_expression(0) + raw = _eval_js( + js, "current", copies=0, edits=1, + transientLegacyAssistant=True, answer="", + ) + state = json.loads(raw) + assert state["text"] == "" + assert state["html_len"] == 0 + assert state["child_count"] == 0 + + d = MagicMock() + d._pre_send_signal_counts = None + d._pre_send_user_count = 0 + d._current_conv_id = "conv-role-only" + d._fetch_end_turn_for_turn = AsyncMock( + return_value=TurnEndResult(status="matched") + ) + + async def evaluate(expr, timeout=15): + if "body.innerText" in expr: + return '{"text": ""}' + if expr == ASSISTANT_TURN_COUNT_JS: + return "0" + if expr == USER_TURN_COUNT_JS: + return "1" + if "copyButtons" in expr and "has_action" in expr: + assert expr == js + return raw + raise AssertionError(expr) + + d._js_strict = evaluate + det = CompletionDetector(d) + _ = [c async for c in det.stream_until_complete( + initial_count=0, timeout=10, + turn_anchor=TurnAnchor(sent_text="one send", mode="fresh_chat"), + )] + assert det.had_non_text_content is False + assert d._fetch_end_turn_for_turn.await_args.kwargs[ + "had_non_text_content" + ] is False + + +@pytest.mark.asyncio +async def test_r3_real_rich_content_not_removed_with_role_header(): + js = await _actual_phase2_expression(0) + state = json.loads(_eval_js( + js, "current", copies=0, edits=1, + transientLegacyAssistant=True, answer="", richContent=True, + )) + assert state["text"] == "" + assert state["html_len"] > 50 + assert state["child_count"] > 0