diff --git a/src/chatgpt_web2api/cdp_driver.py b/src/chatgpt_web2api/cdp_driver.py index 3bc3eae4..17fbcbda 100644 --- a/src/chatgpt_web2api/cdp_driver.py +++ b/src/chatgpt_web2api/cdp_driver.py @@ -382,6 +382,10 @@ def __init__( # refresh tried Ns ago" from "never refreshed." self._last_refresh_attempt_at: float = 0.0 self._current_conv_id: str | None = None + # Last VERIFIED conversation/fresh-route target. Navigation establishes + # this authority; the send DOM uses it to reject a displaced tab. + # None remains possible for legacy GPT entrypoints (tracked NAV-07). + self._expected_send_context: tuple[str, str] | None = None self._current_model: str | None = None # CDP response routing (#7): id-keyed futures + background reader self._pending: dict[int, asyncio.Future] = {} @@ -475,6 +479,9 @@ async def connect(self) -> None: If already connected (e.g. Service reconnects after login), reuses the existing owned tab instead of creating a new one. """ + # CDP attachment can change on reconnect; do not carry an old + # route proof across a new WebSocket/target association. + self._expected_send_context = None # If we already own a tab from a prior connect attempt, reuse it if self._ws is not None: await self._ws.close() @@ -725,6 +732,7 @@ async def reconnect(self) -> None: self._ws = None # Clear stale state (#18) — the page we reconnect to may be different self._current_conv_id = None + self._expected_send_context = None # PR4: capture the pre-reconnect target so we can detect a target change # (drift) after a successful reconnect in parallel mode. _pre_reconnect_target_id = self._target_id @@ -1209,53 +1217,125 @@ async def select_model(self, slug: str) -> bool: # ── Navigation ──────────────────────────────────────────── + @staticmethod + def _is_url_at_new_chat(url: str, gizmo_id: str | None = None) -> bool: + """Require the exact HTTPS ChatGPT origin and the requested fresh route. + + A composer's presence on /c/{id}, a different project, or a host + merely containing 'chatgpt.com' must never qualify a new chat. + Query parameters and a trailing slash may be normalized by the SPA. + """ + if not url: + return False + try: + parsed = urllib.parse.urlparse(url) + except ValueError: + return False + if parsed.scheme != "https" or parsed.netloc.lower() != "chatgpt.com": + return False + expected = ( + f"/g/{urllib.parse.quote(gizmo_id, safe='')}/project" + if gizmo_id else "/" + ) + return parsed.path.rstrip("/") == expected.rstrip("/") + async def navigate_new_chat(self, gizmo_id: str = None) -> None: - """Navigate to a fresh chat. Optionally scope to a project gizmo.""" + """Navigate to a verified, idle fresh-chat composer or fail closed. + + An old conversation ID must not survive a failed new-chat navigation. + A usable editor and the exact requested route are verified together; + an in-progress generation (Stop visible) is not send-ready. A second + probe after a short settle detects immediate SPA displacement. + """ + # Clear stale continuation state BEFORE any navigation or probe can fail. + self._current_conv_id = None + self._expected_send_context = None if gizmo_id: - url = f"https://chatgpt.com/g/{gizmo_id}/project" + url = f"https://chatgpt.com/g/{urllib.parse.quote(gizmo_id, safe='')}/project" else: - # The bare ``chatgpt.com/`` home shell renders only the hidden - # fallback textarea (``name=prompt-textarea``, no id, not visible), - # so neither COMPOSER_SELECTOR nor COMPOSER_FALLBACK_SELECTOR matches - # and type_message fails with "No composer found". The - # ``?model=auto`` query triggers the SPA to render the real - # ProseMirror composer reliably. Verified live: bare home → no - # composer after 20s; ``?model=auto`` → composer present. + # The bare home shell may render only a hidden fallback textarea; + # ?model=auto has been observed to hydrate the real composer. url = "https://chatgpt.com/?model=auto" logger.info("Navigate: %s", url) - await self._cdp("Page.navigate", {"url": url}) + navigation = await self._cdp("Page.navigate", {"url": url}) + if isinstance(navigation, dict) and navigation.get("errorText"): + raise SendReadinessError( + f"Fresh chat navigation rejected: {navigation['errorText']}" + ) await asyncio.sleep(2) - # Wait for the composer. The new composer is a contenteditable - # ProseMirror div (#prompt-textarea is now a hidden fallback); - # COMPOSER_SELECTOR matches the real textbox, with the legacy - # textarea as a last resort for older deployments. - for _ in range(30): - result = await self._js( - "(function() {" - " return JSON.stringify({" - f" ready: !!document.querySelector('{COMPOSER_SELECTOR}') || !!document.querySelector('{COMPOSER_FALLBACK_SELECTOR}')," - " url: location.href" - " });" - "})()" - ) + # This probe is read-only. In particular, never try a submit-button + # fallback to infer readiness: the same button can become Stop while + # a foreign tab is generating. + probe_js = ( + "(function(){" + " function visible(el) {" + " return !!el && el.getClientRects().length > 0 &&" + " !el.disabled && el.getAttribute('aria-disabled') !== 'true';" + " }" + f" var composer = document.querySelector('{COMPOSER_SELECTOR}')" + f" || document.querySelector('{COMPOSER_FALLBACK_SELECTOR}');" + ' var stops = document.querySelectorAll(\'[data-testid="stop-button"], ' + 'button[aria-label*="Stop" i]\');' + " var stopPresent = false;" + " for (var si = 0; si < stops.length; si++) {" + " if (stops[si].getClientRects().length > 0) {" + " stopPresent = true; break;" + " }" + " }" + " return JSON.stringify({" + " url: location.href, ready_state: document.readyState," + " composer: visible(composer), stop_present: stopPresent" + " });" + "})()" + ) + last_state: dict | None = None + + def is_ready(raw: str) -> bool: + nonlocal last_state try: - state = json.loads(result) - if state.get("ready"): - actual_url = state.get("url", "") - # #14: verify we actually landed on chatgpt.com, not an - # error/recovery page that happens to have a textarea. - if "chatgpt.com" not in actual_url: - raise RuntimeError(f"Navigation landed on unexpected URL: {actual_url}") - logger.info("Page ready: %s", actual_url) - break - except (json.JSONDecodeError, TypeError): - pass + state = json.loads(raw) + except (ValueError, TypeError): + return False + if not isinstance(state, dict): + return False + last_state = state + return ( + self._is_url_at_new_chat(state.get("url", ""), gizmo_id) + and state.get("ready_state") == "complete" + and state.get("composer") is True + and state.get("stop_present") is False + ) + + for _ in range(30): + if is_ready(await self._js(probe_js)): + await asyncio.sleep(0.25) + if is_ready(await self._js(probe_js)): + logger.info("Fresh chat ready: %s", last_state.get("url")) + self._expected_send_context = ( + "fresh", f"/g/{urllib.parse.quote(gizmo_id, safe='')}/project" + if gizmo_id else "/", + ) + return await asyncio.sleep(0.5) - # Settle time for sentinel init - await asyncio.sleep(2) - self._current_conv_id = None + if last_state is None: + stage = "no readable readiness probes" + elif not self._is_url_at_new_chat(last_state.get("url", ""), gizmo_id): + stage = "wrong fresh-chat route" + elif last_state.get("stop_present") is True: + stage = "generation active (Stop visible)" + elif last_state.get("ready_state") != "complete": + stage = "document not ready" + elif last_state.get("composer") is not True: + stage = "usable composer absent" + else: + stage = "readiness not stable" + if self._breakers: + self._breakers.record_failure(BreakerKind.COMPOSER_SEND_READINESS) + raise SendReadinessError( + f"Fresh chat navigation failed closed after readiness polling: {stage}" + ) async def _has_composer(self) -> bool: """Is a send-capable composer present on the live tab? @@ -1296,6 +1376,8 @@ async def navigate_conversation(self, conversation_id: str) -> None: fast-fails with ``nav_displaced`` if the URL moves away from the target mid-poll (detects SPA redirects / access-denied states). """ + # Invalidate previous authority even if Page.navigate raises. + self._expected_send_context = None url = f"https://chatgpt.com/c/{conversation_id}" logger.info("Navigate to conversation: %s", url) await self._cdp("Page.navigate", {"url": url}) @@ -1384,6 +1466,7 @@ async def navigate_conversation(self, conversation_id: str) -> None: await asyncio.sleep(1) self._current_conv_id = conversation_id + self._expected_send_context = ("conversation", conversation_id) @staticmethod def _is_url_at_conversation(url: str, conversation_id: str) -> bool: @@ -1401,7 +1484,7 @@ def _is_url_at_conversation(url: str, conversation_id: str) -> bool: parsed = urllib.parse.urlparse(url) except ValueError: return False - if "chatgpt.com" not in (parsed.netloc or "").lower(): + if parsed.scheme != "https" or parsed.netloc.lower() != "chatgpt.com": return False parts = [p for p in parsed.path.split("/") if p] # Find the ("c", conversation_id) adjacent pair — the conversation @@ -1437,6 +1520,7 @@ async def ensure_current_conversation(self, conversation_id: str) -> None: (by ``navigate_conversation``); on failure it is cleared if it matched. """ if await self._is_live_conversation_url(conversation_id): + self._expected_send_context = ("conversation", conversation_id) return await self.navigate_conversation(conversation_id) # navigate_conversation raises on failure, so reaching here means it @@ -1596,7 +1680,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: @@ -1839,6 +1924,7 @@ async def send_and_stream( if conv_id: logger.info("Conversation: %s", conv_id) self._current_conv_id = conv_id + self._expected_send_context = ("conversation", conv_id) last_dom_text = self._completion.last_dom_text had_non_text_content = self._completion.had_non_text_content # A2: anchored final-text reconciliation. The selector resolves @@ -2079,6 +2165,9 @@ async def delete_project(self, project_id: str) -> dict: async def navigate_gpt(self, gizmo_id: str) -> None: """Navigate to a Custom GPT for interaction.""" + # Legacy GPT navigation is not yet route-qualified (NAV-07). + # Never preserve authority from a previous fresh/conversation route. + self._expected_send_context = None url = f"https://chatgpt.com/g/{gizmo_id}" logger.info("Navigate to GPT: %s", url) await self._cdp("Page.navigate", {"url": url}) @@ -2126,6 +2215,7 @@ async def ensure_token(self) -> str: # ── Lifecycle ───────────────────────────────────────────── async def close(self) -> None: + self._expected_send_context = None # Stop the background reader first if self._reader_task and not self._reader_task.done(): self._reader_task.cancel() diff --git a/src/chatgpt_web2api/chatgpt_dom.py b/src/chatgpt_web2api/chatgpt_dom.py index fc76f168..fe137e22 100644 --- a/src/chatgpt_web2api/chatgpt_dom.py +++ b/src/chatgpt_web2api/chatgpt_dom.py @@ -84,16 +84,9 @@ # the legacy testid for older deployments. SEND_BUTTON_SELECTOR = 'button[aria-label*="Send" i]:not([data-testid="stop-button"])' SEND_BUTTON_FALLBACK_SELECTOR = 'button[data-testid="send-button"]' -# P2.5: broader fallback — a submit-type button inside the COMPOSER FORM -# (scoped to the form containing #prompt-textarea, not page-global, to avoid -# hitting login/search/feedback forms if the page is displaced). If ChatGPT -# changes the aria-label (selector drift), a submit-type button inside the -# composer form is still the send affordance. This is the LAST resort. -SEND_BUTTON_BROAD_SELECTOR = ( - 'form:has(#prompt-textarea) button[type="submit"],' - 'form:has(.ProseMirror) button[type="submit"]' -) - +# No type=submit fallback: during generation the only submit-like control +# may be Stop. A widened fallback could cancel a foreign in-progress turn. +# An unknown send control must fail closed until its semantics are verified. # Send-button readiness poll. After a prior send completes (or under parallel # mode, where the MutationLock releases the instant a send finishes), ChatGPT's # composer can take several seconds to re-enable the send button — the composer @@ -118,6 +111,51 @@ class ChatGPTDom: def __init__(self, driver) -> None: self._driver = driver + def _send_route_guard_js(self) -> str: + """Produce the read-only in-page route guard used at type and click. + + A verified navigation supplies exact fresh-route or conversation-ID + authority. For legacy entrypoints without such authority (NAV-07 GPT), + this only validates the HTTPS ChatGPT origin; it cannot establish + conversation ownership. That limitation is explicitly tracked. + """ + context = getattr(self._driver, "_expected_send_context", None) + if not (isinstance(context, tuple) and len(context) == 2): + context = None + + if context and context[0] == "fresh": + route_check = ( + " routeOk = routeOk && path === " + + json.dumps(context[1].rstrip("/") or "/") + + ";" + ) + elif context and context[0] == "conversation": + route_check = ( + " var parts = path.split('/').filter(Boolean);" + " routeOk = routeOk && (" + " (parts.length === 2 && parts[0] === 'c' &&" + " parts[1] === " + json.dumps(context[1]) + ") ||" + " (parts.length === 4 && parts[0] === 'g' &&" + " parts[2] === 'c' &&" + " parts[3] === " + json.dumps(context[1]) + ")" + " );" + ) + else: + route_check = "" # origin-only for unbound/legacy entrypoints + + return ( + " var routeOk = false;" + " try {" + " var currentUrl = new URL(location.href);" + r" var path = currentUrl.pathname.replace(/\/+$/, '') || '/';" + " routeOk = currentUrl.protocol === 'https:' &&" + " currentUrl.hostname === 'chatgpt.com' &&" + " currentUrl.port === '' &&" + " currentUrl.username === '' && currentUrl.password === '';" + + route_check + + " } catch(e) { routeOk = false; }" + ) + # ── Send readiness ──────────────────────────────────────── async def _has_composer(self) -> bool: @@ -214,6 +252,8 @@ async def type_message(self, text: str) -> None: # 'no composer') so the verify step reads the right element. focus_result = await d._js( "(function() {" + + self._send_route_guard_js() + + " if (!routeOk) return 'wrong send route';" f" var el = document.querySelector('{COMPOSER_SELECTOR}');" " if (el) { el.focus(); return 'composer'; }" f" var fb = document.querySelector('{COMPOSER_FALLBACK_SELECTOR}');" @@ -221,7 +261,11 @@ async def type_message(self, text: str) -> None: " return 'no composer';" "})()" ) - if focus_result == "no composer": + if focus_result == "wrong send route": + from .cdp_driver import SendReadinessError + + raise SendReadinessError("Type refused: wrong send route") + if focus_result not in ("composer", "fallback"): await d._capture_selector_diagnostic("composer (type_message)") if d._breakers: d._breakers.record_failure(BreakerKind.COMPOSER_SEND_READINESS) @@ -421,42 +465,64 @@ async def _verify_composer_text(self, selector: str, expected: str) -> bool: return canon_actual == canon_expected or canon_actual == canon_expected + "\n" async def click_send(self) -> None: - """Click the send button via JS MouseEvent sequence. - - The new composer has no ``data-testid="send-button"``; its send - affordance is the submit ``