Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
660d170
Fix conversation projection endpoint
lekthailtd-bit Sep 27, 2026
4dcebbe
Support current ChatGPT turn role selectors
lekthailtd-bit Sep 27, 2026
b108a91
feat(dom): centralize live aria-label and legacy per-turn counters
Alajmah Nov 20, 2026
9bd5f86
fix(dom): use aria/legacy assistant and user counters at send and ack…
Alajmah Nov 20, 2026
236ff6b
fix(detector): detect current aria user/Copy deltas with anchored and…
Alajmah Nov 20, 2026
d4c2b27
fix(detector): initialize action before thinking and reconcile text t…
Alajmah Nov 20, 2026
bc871e4
test(dom): replace source greps with real Node VM current/legacy DOM …
Alajmah Nov 20, 2026
7eb0a1f
test(dom): advance phase-1 above chosen baseline in fixture capture
Alajmah Nov 20, 2026
305284e
style: resolve inherited Ruff baseline findings on PR #55 revision
Alajmah Nov 20, 2026
87d3ce1
style: resolve inherited Ruff baseline findings on PR #55 revision
Alajmah Nov 20, 2026
6fa451f
style: resolve inherited Ruff baseline findings on PR #55 revision
Alajmah Nov 20, 2026
9498748
test(dom): keep Copy fallback subordinate to backend not-ready and ac…
Alajmah Nov 20, 2026
13343b6
fix(detector): keep anchored end_turn available after user turn even …
Alajmah Nov 20, 2026
7f200ad
test(detector): anchored completed turn recovers answer even with no …
Alajmah Nov 20, 2026
76e1f9c
test(dom): pass parametrized Copy state only to anchored recovery test
Alajmah Nov 20, 2026
43b4d8b
fix(detector): never treat Copy toolbar wrapper as non-text answer ev…
Alajmah Nov 20, 2026
5c1d977
test(detector): no phantom non-text answer from completed action-tool…
Alajmah Nov 20, 2026
b4b19c0
test(dom): fixture the full observed user/assistant aria action rows
Alajmah Nov 20, 2026
3a62cf3
fix(detector): never stream action-row article chrome as assistant re…
Alajmah Nov 20, 2026
5704df0
fix(reconcile): require exact DOM prefix before emitting backend text…
Alajmah Nov 20, 2026
8221c2b
test(detector): reproduce live role-chrome article and require exact …
Alajmah Nov 20, 2026
364397f
docs(detector): document why unscoped article innerText is unsafe
Alajmah Nov 20, 2026
9a9aeb7
test(reconciliation): guard against role chrome and nonprefix truncat…
Alajmah Nov 20, 2026
766d19b
fix(detector): only emit monotonic prefix-confirmed DOM deltas; never…
Alajmah Nov 20, 2026
405ae8b
test(detector): reject length-only delta after nonprefix DOM hydratio…
Alajmah Nov 20, 2026
68c3106
test(dom): match live two-newline role chrome in Node fixture
Alajmah Nov 20, 2026
8f68eae
fix(completion): defer anchored backend text to one final driver fetch
Alajmah Nov 20, 2026
5d1e317
fix(reconcile): diagnose nonprefix text without exposing answer content
Alajmah Nov 20, 2026
36fbfa6
test(completion): assert detector defers anchored text without a seco…
Alajmah Nov 20, 2026
9461dc5
test(dom): end-to-end real Node current DOM and one canonical backend…
Alajmah Nov 20, 2026
d9390f2
test(reconcile): retain exact-match failure on whitespace-only differ…
Alajmah Nov 20, 2026
3c23107
test(dom): model observed Copy-without-article October geometry
Alajmah Nov 20, 2026
e98db91
test(dom): exercise fresh-chat mid-loop conversation-ID discovery bef…
Alajmah Nov 20, 2026
da0f948
fix(detector): strip transient standalone ChatGPT role header before …
Alajmah Nov 20, 2026
54c4b09
test(dom): reproduce transient role-label-only hydration and preserve…
Alajmah Nov 20, 2026
5d3f1d8
fix(reconciliation): surface only turn-anchored conversation ID from …
Alajmah Nov 20, 2026
c50a81d
fix(driver): fail closed without conversation ID; accept only anchore…
Alajmah Nov 20, 2026
9b59567
fix(detector): ignore transient role-label-only wrapper as non-text c…
Alajmah Nov 20, 2026
d980a0c
fix(dom): snapshot aria and legacy turn signals independently for mix…
Alajmah Nov 20, 2026
443adb0
fix(send): atomic mixed-DOM baseline and per-signal acceptance delta
Alajmah Nov 20, 2026
9f8d192
fix(detector): compare aria and legacy counts against independent pre…
Alajmah Nov 20, 2026
47a3e3e
test(send): exercise atomic mixed-DOM baseline and fail-closed invali…
Alajmah Nov 20, 2026
5cb2ff7
test(detector): assert trusted conversation ID resets and requires an…
Alajmah Nov 20, 2026
9211d13
test(reconcile): fail closed on absent or conflicting conv ID; permit…
Alajmah Nov 20, 2026
4d035f1
test(dom): execute mixed-layout counters, send ack, and role-only non…
Alajmah Nov 20, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions src/chatgpt_web2api/backend_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -286,7 +286,6 @@ async def _fetch_recent_conversation_projection(
{
"conv_id": conversation_id,
"token": d._access_token,
"limit": TURN_PROJECTION_LIMIT,
},
timeout=15,
)
Expand Down
2 changes: 1 addition & 1 deletion src/chatgpt_web2api/backend_projection.py
Original file line number Diff line number Diff line change
Expand Up @@ -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});
Expand Down
226 changes: 163 additions & 63 deletions src/chatgpt_web2api/cdp_driver.py

Large diffs are not rendered by default.

233 changes: 192 additions & 41 deletions src/chatgpt_web2api/completion_detector.py

Large diffs are not rendered by default.

80 changes: 80 additions & 0 deletions src/chatgpt_web2api/dom_turn_signals.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
"""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"
")"
)


# 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)
6 changes: 3 additions & 3 deletions tests/test_backend_projection_js.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 6 additions & 1 deletion tests/test_completion_detector.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)}"
)

Expand All @@ -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": ...}
Expand Down Expand Up @@ -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 ─────────────────────────
Expand Down
3 changes: 1 addition & 2 deletions tests/test_composer_verification.py
Original file line number Diff line number Diff line change
Expand Up @@ -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=""):
Expand Down
Loading
Loading