Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
16 changes: 14 additions & 2 deletions browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,11 @@
panel (Chrome refuses: ``net::ERR_BLOCKED_BY_CLIENT``) or one of the operator's tabs. So the
launch step is ``tab <label>`` / ``tab new --label <label>`` carrying the launch flags — the
flags make the CLI send its full-options launch first, exactly as on ``open`` — and every
navigation switches to that tab first. Nothing navigates a tab it did not create, and the
only targets ever closed are Chrome's own panels, never an ordinary page. Verified live on a
navigation switches to that tab first. The one residual gap is a sub-second race: 0.27.1's
``open`` cannot name a tab, so a target that appears between the switch and the navigation
is what gets navigated (the read-side check in tools catches the result). The only targets
ever closed are Chrome's own panels, and the plugin's own tab when it is wedged — never an
operator's page. Verified live on a
throwaway session: the labelled-tab command launched on the requested profile with the
stealth argument, was reused on repeat, and ``open`` then landed in the labelled tab.
"""
Expand Down Expand Up @@ -221,6 +224,15 @@ def focus_own_tab(self) -> str:
if _run(self._tab_cmd(self.tab_label), timeout=self.timeout_s).ok:
return "switched"
res = _run(self._tab_cmd("new", "--label", self.tab_label), timeout=self.timeout_s)
if not res.ok and "already used" in (res.stderr + res.stdout):
# Our tab exists but would not take focus (a hung or crashed tab). Left alone, every
# navigation after this fails until someone closes it by hand — so close OUR tab,
# the only one carrying this label, and start a fresh one.
_run(self._tab_cmd("close", self.tab_label), timeout=self.timeout_s)
res = _run(self._tab_cmd("new", "--label", self.tab_label), timeout=self.timeout_s)
if res.ok:
log.warning("[ebay] the plugin's browser tab would not take focus — replaced it")
return "recreated"
if not res.ok:
raise BrowserError(self._last_line(res))
return "created"
Expand Down
2 changes: 1 addition & 1 deletion protoagent.plugin.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
id: ebay
name: Marketplace prices (eBay + Amazon, via browser)
version: 0.3.2
version: 0.3.3
description: >-
Price research across eBay and Amazon — what an item actually SOLD for on eBay, what it
is being asked on both, and where today's price sits against what we've recorded before.
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "ebay-plugin"
version = "0.3.2"
version = "0.3.3"
description = "Marketplace price research (eBay + Amazon) + seller economics for protoAgent, via a browser session"
requires-python = ">=3.11"

Expand Down
89 changes: 89 additions & 0 deletions tests/test_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -819,3 +819,92 @@ def test_a_read_without_a_url_is_not_judged(self):
from ebay_plugin.tools import _hijacked

assert _hijacked({"found_container": True}, _EBAY) is False


class TestOwnTabFollowUps:
"""Review round 2's non-blocking findings, closed."""

def test_a_wedged_own_tab_is_replaced_not_left_to_block_every_navigation(self, monkeypatch, tmp_path):
monkeypatch.setattr(browser_mod.shutil, "which", lambda b: "/usr/bin/agent-browser")
calls, state = [], {"closed": False}

def fake(args, timeout):
calls.append(args)
sub = args[1:]
if sub[:2] == ["tab", "ebaytab"]:
return browser_mod.Result(False, "", "✗ Tab t4 is not responding")
if sub[:2] == ["tab", "new"]:
if state["closed"]:
return browser_mod.Result(True, "✓ Done\n", "")
return browser_mod.Result(False, "", "✗ Label `ebaytab` is already used by another tab")
if sub[:3] == ["tab", "close", "ebaytab"]:
state["closed"] = True
return browser_mod.Result(True, "✓ Done\n", "")

monkeypatch.setattr(browser_mod, "_run", fake)
assert Browser(profile=str(tmp_path)).focus_own_tab() == "recreated"
closes = [c[1:4] for c in calls if c[1:3] == ["tab", "close"]]
assert closes == [["tab", "close", "ebaytab"]] # only OUR labelled tab, nothing else

def test_reclaiming_never_closes_an_operator_tab(self, monkeypatch, tmp_path):
"""Locks in round 1's blocker fix through the real path (refocus from a blocked
navigation), not by calling close_panels directly."""
tabs = [
{"tabId": "t1", "type": "page", "url": "https://mail.google.com/", "active": False},
{"tabId": "t2", "type": "page", "url": "https://www.ebay.com/sch/i.html?_nkw=mine", "active": False},
{"tabId": "t9", "type": "webview", "url": "https://gemini.google.com/glic", "active": True},
]
calls = _tab_cli(monkeypatch, tabs=tabs, open_results=[browser_mod.Result(False, "", _BLOCKED)])
Browser(profile=str(tmp_path), min_interval_s=0).open("https://www.ebay.com/sch/i.html?_nkw=x")
assert [c[3] for c in calls if c[1:3] == ["tab", "close"]] == ["t9"]

@pytest.mark.parametrize(
"landed,requested,hijacked",
[
# an operator's own eBay search answering ours: same host, different terms
(
"https://www.ebay.com/sch/i.html?_nkw=my+own+search&LH_Sold=1",
"https://www.ebay.com/sch/i.html?_nkw=kill+team+volkus&LH_Sold=1",
True,
),
# our search, however eBay re-encodes or reorders it
(
"https://www.ebay.com/sch/i.html?LH_Sold=1&_nkw=Kill%20Team%20Volkus&rt=nc",
"https://www.ebay.com/sch/i.html?_nkw=kill+team+volkus&LH_Sold=1",
False,
),
# an item page answering a search
("https://www.ebay.com/itm/1234567890", "https://www.ebay.com/sch/i.html?_nkw=x", True),
# the sign-in and challenge hops are not judged as a different search
("https://signin.ebay.com/ws/eBayISAPI.dll?SignIn", "https://www.ebay.com/sch/i.html?_nkw=x", False),
("https://www.ebay.com/splashui/captcha?ap=1", "https://www.ebay.com/sch/i.html?_nkw=x", False),
# Amazon: k= carries the terms; a product page answering a search
("https://www.amazon.com/s?k=other+thing", "https://www.amazon.com/s?k=hierotek+circle", True),
("https://www.amazon.com/s?k=Hierotek+Circle&ref=nb", "https://www.amazon.com/s?k=hierotek+circle", False),
("https://www.amazon.com/dp/B0ABC", "https://www.amazon.com/s?k=x", True),
# not a search request: only the host is judged
("https://www.ebay.com/", "https://www.ebay.com/", False),
# a search page that states no terms (an eBay rewrite) is not judged a different search
("https://www.ebay.com/sch/i.html?LH_Sold=1", "https://www.ebay.com/sch/i.html?_nkw=x&LH_Sold=1", False),
# `domain: ebay.com` configured without www: eBay lands on www — the same-search check still applies
("https://www.ebay.com/sch/i.html?_nkw=other", "https://ebay.com/sch/i.html?_nkw=x", True),
("https://www.ebay.com/sch/i.html?_nkw=x", "https://ebay.com/sch/i.html?_nkw=x", False),
# a search landing on a sign-in SUBDOMAIN is a hop, not a different search
("https://signin.ebay.com/sch/i.html?_nkw=other", "https://www.ebay.com/sch/i.html?_nkw=x", False),
],
)
def test_a_search_must_be_answered_by_our_search(self, landed, requested, hijacked):
from ebay_plugin.tools import _hijacked

assert _hijacked({"url": landed}, requested) is hijacked


def test_a_same_site_misfire_still_settles(monkeypatch):
"""Review of #4 (FP2): only a different SITE counts as settled. A same-site page flagged by
the same-search rule that is also still loading must settle, not fail as "no results list"."""
import ebay_plugin.tools as tools_mod

monkeypatch.setattr(tools_mod, "_SETTLE_S", 0)
loading = {"url": "https://www.ebay.com/sch/i.html?_nkw=other", "found_container": False, "count": 0}
assert tools_mod._is_undecided(loading, _EBAY) is True
assert tools_mod._is_undecided(_PANEL_READ, _EBAY) is False
53 changes: 45 additions & 8 deletions tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
import json
import logging
import time
from urllib.parse import urlparse
from urllib.parse import parse_qs, unquote_plus, urlparse

from . import amazon
from .browser import Browser, BrowserError
Expand Down Expand Up @@ -75,7 +75,7 @@ def _is_undecided(data, url: str = "") -> bool:
"""True while the page shows neither results nor a reason — i.e. mid-redirect. A read that
came from a tab not showing the site we asked for is DECIDED: waiting will not change it,
the tab has to be taken back, so the settle loop must not spin on it."""
if isinstance(data, dict) and _hijacked(data, url):
if isinstance(data, dict) and _foreign_host(data, url):
return False
return isinstance(data, dict) and not any(
(data.get("count"), data.get("found_container"), data.get("signin_wall"), data.get("challenge"))
Expand Down Expand Up @@ -202,18 +202,55 @@ def _site(host: str) -> str:
return host[4:] if host.startswith("www.") else host


def _hijacked(data, url: str) -> bool:
"""The script ran in a tab that is not showing the site we navigated to — the Gemini panel,
about:blank, or an operator's tab that took the active slot. Subdomains of the requested
site (signin.ebay.com, the /splashui/ challenge pages) are the site. A read without a url
(older payloads, test stubs) is not judged."""
#: The query parameter that carries a search's terms, per marketplace search path.
_SEARCH_TERM = {"/sch/": "_nkw", "/s": "k"}


def _search_term(parsed) -> tuple[str, str] | None:
"""``(search path, normalized terms)`` for an eBay or Amazon search URL, else ``None``."""
for path, key in _SEARCH_TERM.items():
if parsed.path == path or (path.endswith("/") and parsed.path.startswith(path)):
terms = (parse_qs(parsed.query).get(key) or [""])[0]
return path, " ".join(unquote_plus(terms).lower().split())
return None


def _foreign_host(data, url: str) -> bool:
"""The read came from a different SITE than the one requested (the Gemini panel, about:blank,
an unrelated tab). Only this counts as "decided" for the settle loop: a same-site page that
is still loading must still be allowed to settle."""
if not isinstance(data, dict) or not data.get("url") or not url:
return False
want = _site(urlparse(url).hostname or "")
host = (urlparse(str(data["url"])).hostname or "").lower()
return bool(want) and not (host == want or host.endswith("." + want))


def _hijacked(data, url: str) -> bool:
"""The script ran in a tab that is not showing what we navigated to — the Gemini panel,
about:blank, an operator's tab that took the active slot. Subdomains of the requested
site (signin.ebay.com, the /splashui/ challenge pages) are the site. For a SEARCH, the
page must also be our search: an operator's own eBay search, or an item page, answering
in its place would otherwise be read as our comps — prices for the wrong query. A read
without a url (older payloads, test stubs) is not judged."""
if not isinstance(data, dict) or not data.get("url") or not url:
return False
want_p, got_p = urlparse(url), urlparse(str(data["url"]))
want = _site(want_p.hostname or "")
host = (got_p.hostname or "").lower()
if not want:
return False
return not (host == want or host.endswith("." + want))
if not (host == want or host.endswith("." + want)):
return True
wanted = _search_term(want_p)
if wanted is None or _site(host) != want:
return False # not a search, or a sign-in / challenge hop on a subdomain: not judged here
if got_p.path.startswith(("/itm/", "/dp/", "/gp/product/")):
return True # an item page answered a search
got = _search_term(got_p)
# Judge only a search page that states its own terms: an operator's search always does,
# while an eBay rewrite that drops them is not evidence of another tab.
return got is not None and got[1] != "" and got[1] != wanted[1]


def _page_meta(data: dict) -> dict:
Expand Down
Loading