papersearch 0.4.8 → 0.4.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.en.md +50 -62
- package/README.md +50 -62
- package/assets/overview.en.png +0 -0
- package/assets/overview.png +0 -0
- package/assets/progress-capsule.en.png +0 -0
- package/assets/progress-capsule.png +0 -0
- package/carsi/README.md +5 -6
- package/carsi/paper_search_carsi/adapters/elsevier/__init__.py +1 -1
- package/carsi/paper_search_carsi/adapters/ieee/__init__.py +1 -1
- package/carsi/paper_search_carsi/adapters/jama/__init__.py +3 -1
- package/carsi/paper_search_carsi/adapters/karger/__init__.py +2 -1
- package/carsi/paper_search_carsi/adapters/lww/ovid.py +2 -2
- package/carsi/paper_search_carsi/adapters/oup/__init__.py +3 -4
- package/carsi/paper_search_carsi/adapters/world_scientific/__init__.py +5 -4
- package/carsi/paper_search_carsi/assets/capsule.html +199 -24
- package/carsi/paper_search_carsi/browser/backend.py +270 -227
- package/carsi/paper_search_carsi/browser/batch_session.py +42 -0
- package/carsi/paper_search_carsi/browser/challenge_interaction.py +79 -43
- package/carsi/paper_search_carsi/browser/cookies.py +8 -8
- package/carsi/paper_search_carsi/browser/driver.py +29 -0
- package/carsi/paper_search_carsi/browser/engine.py +17 -23
- package/carsi/paper_search_carsi/browser/foreground.py +21 -15
- package/carsi/paper_search_carsi/browser/login.py +12 -18
- package/carsi/paper_search_carsi/browser/preview.py +31 -12
- package/carsi/paper_search_carsi/browser/profiles.py +16 -23
- package/carsi/paper_search_carsi/browser/watchdog.py +61 -55
- package/carsi/paper_search_carsi/capsule_placement.py +266 -0
- package/carsi/paper_search_carsi/cli.py +22 -7
- package/carsi/paper_search_carsi/cold_start.py +65 -51
- package/carsi/paper_search_carsi/config.py +6 -4
- package/carsi/paper_search_carsi/engine/batch.py +9 -5
- package/carsi/paper_search_carsi/engine/batch_access.py +2 -4
- package/carsi/paper_search_carsi/engine/batch_capture.py +56 -20
- package/carsi/paper_search_carsi/engine/batch_common.py +56 -16
- package/carsi/paper_search_carsi/engine/batch_diagnostics.py +4 -2
- package/carsi/paper_search_carsi/engine/batch_institution.py +146 -17
- package/carsi/paper_search_carsi/engine/batch_login.py +79 -26
- package/carsi/paper_search_carsi/engine/batch_user.py +1 -1
- package/carsi/paper_search_carsi/engine/batch_waits.py +92 -37
- package/carsi/paper_search_carsi/engine/bridge.py +23 -13
- package/carsi/paper_search_carsi/engine/login_wait.py +1 -1
- package/carsi/paper_search_carsi/engine/page_activity.py +15 -15
- package/carsi/paper_search_carsi/mcp_progress.py +13 -5
- package/carsi/paper_search_carsi/network.py +1 -1
- package/carsi/paper_search_carsi/progress_capsule.py +233 -93
- package/carsi/paper_search_carsi/progress_reporter.py +88 -7
- package/carsi/paper_search_carsi/progress_view.py +61 -0
- package/carsi/paper_search_carsi/server.py +47 -10
- package/carsi/paper_search_carsi/session_broker.py +1 -1
- package/carsi/paper_search_carsi/user_settings.py +1 -1
- package/carsi/pyproject.toml +7 -7
- package/dist/capabilities/pdf-discovery/OpenAccessFallbackService.d.ts.map +1 -1
- package/dist/capabilities/pdf-discovery/OpenAccessFallbackService.js +6 -0
- package/dist/capabilities/pdf-discovery/OpenAccessFallbackService.js.map +1 -1
- package/dist/capabilities/pdf-discovery/batch.d.ts.map +1 -1
- package/dist/capabilities/pdf-discovery/batch.js +15 -5
- package/dist/capabilities/pdf-discovery/batch.js.map +1 -1
- package/dist/capabilities/pdf-discovery/batchSummary.d.ts +22 -0
- package/dist/capabilities/pdf-discovery/batchSummary.d.ts.map +1 -0
- package/dist/capabilities/pdf-discovery/batchSummary.js +131 -0
- package/dist/capabilities/pdf-discovery/batchSummary.js.map +1 -0
- package/dist/capabilities/pdf-discovery/carsi/CarsiAttempt.d.ts +1 -0
- package/dist/capabilities/pdf-discovery/carsi/CarsiAttempt.d.ts.map +1 -1
- package/dist/capabilities/pdf-discovery/carsi/CarsiAttempt.js +52 -1
- package/dist/capabilities/pdf-discovery/carsi/CarsiAttempt.js.map +1 -1
- package/dist/capabilities/pdf-discovery/carsi/CarsiMcpClient.d.ts +8 -4
- package/dist/capabilities/pdf-discovery/carsi/CarsiMcpClient.d.ts.map +1 -1
- package/dist/capabilities/pdf-discovery/carsi/CarsiMcpClient.js +89 -30
- package/dist/capabilities/pdf-discovery/carsi/CarsiMcpClient.js.map +1 -1
- package/dist/capabilities/pdf-discovery/carsi/CarsiReadiness.d.ts +4 -0
- package/dist/capabilities/pdf-discovery/carsi/CarsiReadiness.d.ts.map +1 -1
- package/dist/capabilities/pdf-discovery/carsi/CarsiReadiness.js +10 -0
- package/dist/capabilities/pdf-discovery/carsi/CarsiReadiness.js.map +1 -1
- package/dist/capabilities/pdf-discovery/carsi/index.d.ts +2 -2
- package/dist/capabilities/pdf-discovery/carsi/index.d.ts.map +1 -1
- package/dist/capabilities/pdf-discovery/carsi/index.js +1 -1
- package/dist/capabilities/pdf-discovery/carsi/index.js.map +1 -1
- package/dist/capabilities/pdf-discovery/carsi/types.d.ts +36 -0
- package/dist/capabilities/pdf-discovery/carsi/types.d.ts.map +1 -1
- package/dist/capabilities/pdf-discovery/handler.d.ts.map +1 -1
- package/dist/capabilities/pdf-discovery/handler.js +11 -6
- package/dist/capabilities/pdf-discovery/handler.js.map +1 -1
- package/dist/capabilities/pdf-discovery/types.d.ts +3 -0
- package/dist/capabilities/pdf-discovery/types.d.ts.map +1 -1
- package/dist/cli.js +13 -9
- package/dist/cli.js.map +1 -1
- package/dist/management/carsi/installer.d.ts +26 -14
- package/dist/management/carsi/installer.d.ts.map +1 -1
- package/dist/management/carsi/installer.js +113 -69
- package/dist/management/carsi/installer.js.map +1 -1
- package/dist/management/carsi/runtime.d.ts +12 -1
- package/dist/management/carsi/runtime.d.ts.map +1 -1
- package/dist/management/carsi/runtime.js +30 -1
- package/dist/management/carsi/runtime.js.map +1 -1
- package/package.json +1 -1
- package/skills/paper-search/SKILL.md +4 -4
- package/skills/paper-search/references/browser-download.md +3 -3
- package/skills/paper-search/references/carsi-setup.md +7 -6
- package/skills/paper-search/references/cli-contract.md +4 -4
- package/skills/paper-search/references/management-layer.md +2 -2
- package/carsi/paper_search_carsi/assets/quiet-browser/api.js +0 -142
- package/carsi/paper_search_carsi/assets/quiet-browser/manifest.json +0 -24
- package/carsi/paper_search_carsi/assets/quiet-browser/schema.json +0 -1
- package/carsi/paper_search_carsi/browser/background.py +0 -68
|
@@ -125,6 +125,7 @@ class UserWaitMixin:
|
|
|
125
125
|
except Exception:
|
|
126
126
|
reporter = None
|
|
127
127
|
shown = "" # "verification" | "login" | "unknown"
|
|
128
|
+
verification_tab = None
|
|
128
129
|
try:
|
|
129
130
|
while time.time() < deadline:
|
|
130
131
|
self._check_user_decision(result, page)
|
|
@@ -151,18 +152,22 @@ class UserWaitMixin:
|
|
|
151
152
|
return True
|
|
152
153
|
state, tab = "", None
|
|
153
154
|
for candidate in pages:
|
|
155
|
+
if (urlparse(str(candidate.url or "")).hostname or "").lower() == suspect_host:
|
|
156
|
+
self._raise_on_publisher_access_blocked(candidate)
|
|
154
157
|
try:
|
|
155
|
-
if self._is_challenge_page(candidate)
|
|
158
|
+
if (self._is_challenge_page(candidate)
|
|
159
|
+
or (candidate is verification_tab and self._challenge_continues(candidate))):
|
|
156
160
|
state, tab = "verification", candidate
|
|
157
161
|
break
|
|
158
162
|
if self._pdf_step_needs_login(candidate):
|
|
159
|
-
state
|
|
163
|
+
state = "login" if candidate == page or self._users_login_step(candidate) else "unknown"
|
|
164
|
+
tab = candidate
|
|
160
165
|
break
|
|
161
166
|
except (watchdog.BrowserUnresponsive, UserMarkedNoEntitlement):
|
|
162
167
|
raise
|
|
163
168
|
except Exception:
|
|
164
169
|
continue
|
|
165
|
-
if not state and
|
|
170
|
+
if not state and not automatic and shown not in {"verification", "login"}:
|
|
166
171
|
for candidate in pages:
|
|
167
172
|
try:
|
|
168
173
|
if suspect_host and (urlparse(str(candidate.url or "")).hostname or "").lower() == suspect_host:
|
|
@@ -170,6 +175,10 @@ class UserWaitMixin:
|
|
|
170
175
|
break
|
|
171
176
|
except Exception:
|
|
172
177
|
continue
|
|
178
|
+
if state == "unknown" and time.time() - started >= self.SUSPECT_UNKNOWN_GRACE_SEC:
|
|
179
|
+
captured["block_reason"] = "ip_blocked"
|
|
180
|
+
self._event(result, "ip_blocked_response", "PDF response remained blocked; no verification transition")
|
|
181
|
+
return False
|
|
173
182
|
if not state:
|
|
174
183
|
if shown or automatic:
|
|
175
184
|
self._event(result, "challenge_resolved", f"{shown or 'verification'} page gone")
|
|
@@ -184,6 +193,7 @@ class UserWaitMixin:
|
|
|
184
193
|
captured["suspect_block"] = None
|
|
185
194
|
return False
|
|
186
195
|
if state == "verification":
|
|
196
|
+
verification_tab = tab
|
|
187
197
|
self._raise_on_publisher_access_blocked(tab)
|
|
188
198
|
result._attention_page = tab
|
|
189
199
|
activity.observe(tab)
|
|
@@ -193,8 +203,12 @@ class UserWaitMixin:
|
|
|
193
203
|
page.wait_for_timeout(min(1000, max(0, (deadline - time.time()) * 1000)))
|
|
194
204
|
continue
|
|
195
205
|
activity.automatic_for(0)
|
|
206
|
+
if state == "verification":
|
|
207
|
+
self._verification_attention(result, deadline)
|
|
196
208
|
result._attention_page = tab
|
|
197
209
|
if state != shown:
|
|
210
|
+
if state != "verification" and shown == "verification":
|
|
211
|
+
self._verification_attention(result, None)
|
|
198
212
|
if state in ("verification", "login"):
|
|
199
213
|
deadline = min(deadline, self._wait_deadline(result))
|
|
200
214
|
self._event(result, {"verification": "challenge_manual_wait", "login": "pdf_step_login_wait",
|
|
@@ -236,6 +250,7 @@ class UserWaitMixin:
|
|
|
236
250
|
self._event(result, "ip_blocked_response", f"{suspect} | no PDF after waiting ({shown or 'nothing shown'})")
|
|
237
251
|
return False
|
|
238
252
|
finally:
|
|
253
|
+
self._verification_attention(result, None)
|
|
239
254
|
activity.automatic_for(0)
|
|
240
255
|
result._attention_page = None
|
|
241
256
|
if activity_owned:
|
|
@@ -365,55 +380,79 @@ class UserWaitMixin:
|
|
|
365
380
|
|
|
366
381
|
def _automatic_challenge_step(self, page: Any, result: DownloadResult,
|
|
367
382
|
attempt: dict[str, Any], deadline: float) -> bool:
|
|
368
|
-
"""
|
|
383
|
+
"""Count dispatched clicks only; reserve time for an observed user step.
|
|
369
384
|
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
new budget. Observe for at least three seconds after each attempt.
|
|
385
|
+
An absent/loading widget stays automatic, including late arrivals. A
|
|
386
|
+
visible widget we cannot operate hands off within the existing budget.
|
|
373
387
|
"""
|
|
374
388
|
now = time.time()
|
|
375
389
|
if not attempt:
|
|
376
390
|
attempt.update(started=now, attempts=0, retry_at=now + 2)
|
|
377
391
|
self._event(result, "challenge_wait", "automatic verification check")
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
automatic_end = min(deadline - manual_reserve, attempt["automatic_end"])
|
|
392
|
+
remaining = watchdog.operation_remaining()
|
|
393
|
+
if "handoff_at" not in attempt:
|
|
394
|
+
# Default 60 s wait: look automatically for 30 s and leave 30 s for
|
|
395
|
+
# the user. Short waits retain half; the original deadline is fixed.
|
|
396
|
+
after = float(self.config.get("browser_stall_notify_after", 30))
|
|
397
|
+
budget = max(0, deadline - now)
|
|
398
|
+
if remaining is not None:
|
|
399
|
+
budget = min(budget, max(0, remaining))
|
|
400
|
+
attempt["handoff_at"] = now + min(after, budget / 2)
|
|
401
|
+
result._verification_notify_at = attempt["handoff_at"]
|
|
402
|
+
automatic_end = min(deadline, now + remaining) if remaining is not None else deadline
|
|
390
403
|
activity = getattr(result, "_page_activity", None)
|
|
391
404
|
if activity is not None:
|
|
392
|
-
# The separate request watcher also checks idle time during these
|
|
393
|
-
# RPCs. Use the frozen automatic bound; never renew the wait budget.
|
|
394
405
|
activity.automatic_for(automatic_end - now)
|
|
395
|
-
if now >= automatic_end or (attempt["attempts"] >= 3 and now >= attempt["retry_at"]):
|
|
396
|
-
return False
|
|
397
406
|
if challenge_interaction.needs_manual_verification(page):
|
|
398
|
-
# We never operate this control. Waiting for it must not consume
|
|
399
|
-
# the enclosing automation budget, even if the site clears itself.
|
|
400
|
-
# The independent idle timer still decides when to remind the user.
|
|
401
407
|
return False
|
|
408
|
+
if ((attempt["attempts"] >= 3 and now >= attempt["retry_at"])
|
|
409
|
+
or (attempt.get("unavailable") and now >= attempt["handoff_at"])):
|
|
410
|
+
return False
|
|
411
|
+
if now >= automatic_end:
|
|
412
|
+
# No observed user action: the caller ends at its original bound.
|
|
413
|
+
return True
|
|
402
414
|
if now >= attempt["retry_at"] and attempt["attempts"] < 3:
|
|
403
|
-
|
|
404
|
-
# Leave time for that action, still inside the original wait budget.
|
|
415
|
+
reasons = []
|
|
405
416
|
outcome = challenge_interaction.click_checkbox(
|
|
406
417
|
page, timeout_ms=min(2500, max(1, int((automatic_end - now) * 1000))),
|
|
407
418
|
checkpoint=lambda: self._check_user_decision(result, page),
|
|
419
|
+
observation=reasons.append,
|
|
408
420
|
)
|
|
409
|
-
|
|
421
|
+
reason = ", ".join(dict.fromkeys(reasons)) or outcome
|
|
422
|
+
if reason != attempt.get("last_observation"):
|
|
423
|
+
self._event(result, "challenge_observation", reason)
|
|
424
|
+
attempt["last_observation"] = reason
|
|
425
|
+
attempt["unavailable"] = outcome == "unavailable" and (not reasons or any(
|
|
426
|
+
value.startswith(("actionability_timeout", "unsupported_geometry_or_title", "multiple_controls"))
|
|
427
|
+
for value in reasons))
|
|
428
|
+
attempt["retry_at"] = time.time() + (3 if outcome != "absent" else 1)
|
|
429
|
+
if outcome == "clicked":
|
|
410
430
|
attempt["attempts"] += 1
|
|
411
|
-
|
|
412
|
-
if outcome == "clicked" and activity is not None:
|
|
431
|
+
if activity is not None:
|
|
413
432
|
activity.progress(page)
|
|
414
433
|
self._event(result, "challenge_interaction",
|
|
415
|
-
f"checkbox
|
|
416
|
-
return
|
|
434
|
+
f"checkbox clicked; attempt {attempt['attempts']}/3; awaiting verification")
|
|
435
|
+
return True
|
|
436
|
+
|
|
437
|
+
def _verification_attention(self, result: DownloadResult, deadline: float | None) -> None:
|
|
438
|
+
from .. import progress_reporter
|
|
439
|
+
key = f"verification:{id(self)}:{result.doi}"
|
|
440
|
+
if deadline is None:
|
|
441
|
+
if getattr(result, "_verification_attention", False):
|
|
442
|
+
progress_reporter.clear_attention(key)
|
|
443
|
+
progress = progress_reporter.read_state() or {}
|
|
444
|
+
if progress.get("stage") == "waiting_verification":
|
|
445
|
+
progress_reporter.update(stage="checking_access", phase="安全验证等待已结束,正在检查页面")
|
|
446
|
+
result._verification_attention = False
|
|
447
|
+
elif time.time() < getattr(result, "_verification_notify_at", 0):
|
|
448
|
+
return
|
|
449
|
+
elif not getattr(result, "_verification_attention", False):
|
|
450
|
+
progress_reporter.set_attention(key, "自动验证尚未完成,请在浏览器中检查并完成验证",
|
|
451
|
+
current=result.doi, phase="安全验证", deadline=deadline)
|
|
452
|
+
progress_reporter.report_stage("waiting_verification", current=result.doi)
|
|
453
|
+
result._verification_attention = True
|
|
454
|
+
else:
|
|
455
|
+
progress_reporter.update_attention_deadline(key, deadline)
|
|
417
456
|
|
|
418
457
|
def _wait_for_challenge(self, page: Any, result: DownloadResult, *, deadline: float | None = None) -> bool:
|
|
419
458
|
"""Wait while a verification page is up, until ``_wait_deadline``."""
|
|
@@ -435,8 +474,7 @@ class UserWaitMixin:
|
|
|
435
474
|
shell = self._challenge_shell(page)
|
|
436
475
|
if not shell:
|
|
437
476
|
self._raise_on_publisher_access_blocked(page)
|
|
438
|
-
if
|
|
439
|
-
or (waited and challenges.continues_manual_challenge(self._title(page)))):
|
|
477
|
+
if shell or self._is_challenge_page(page) or (waited and self._challenge_continues(page)):
|
|
440
478
|
if not waited:
|
|
441
479
|
deadline = self._wait_deadline(result, deadline)
|
|
442
480
|
started, phase_before = time.time(), self._phase(result, "verification")
|
|
@@ -445,6 +483,10 @@ class UserWaitMixin:
|
|
|
445
483
|
if verification_wait is not None:
|
|
446
484
|
verification_wait.observe(page, True)
|
|
447
485
|
deadline = verification_wait.deadline
|
|
486
|
+
self._verification_attention(result, deadline)
|
|
487
|
+
from .. import progress_reporter
|
|
488
|
+
progress_reporter.update_attention_deadline(
|
|
489
|
+
f"verification:{id(self)}:{result.doi}", None if verification_wait.paused else deadline)
|
|
448
490
|
if time.time() >= deadline:
|
|
449
491
|
break
|
|
450
492
|
result._attention_page = page
|
|
@@ -460,6 +502,7 @@ class UserWaitMixin:
|
|
|
460
502
|
verification_deadline = deadline
|
|
461
503
|
verification_wait = result._verification_wait = LoginWait(deadline, time.time)
|
|
462
504
|
verification_wait.observe(page, True)
|
|
505
|
+
self._verification_attention(result, deadline)
|
|
463
506
|
self._event(result, "challenge_manual_wait", "complete verification in visible browser")
|
|
464
507
|
if not any(s["why"] == "waiting for the user: verification" for s in result.snapshots):
|
|
465
508
|
self._snapshot(page, result, "waiting for the user: verification")
|
|
@@ -471,6 +514,12 @@ class UserWaitMixin:
|
|
|
471
514
|
if wait_ms <= 0:
|
|
472
515
|
break
|
|
473
516
|
_observe_preview(page) # the verification page, for anyone checking in
|
|
517
|
+
remaining = watchdog.operation_remaining()
|
|
518
|
+
if remaining is not None and wait_ms >= remaining * 1000:
|
|
519
|
+
# No room for another polling interval. A scheduled
|
|
520
|
+
# delay running into the deadline is not a driver hang.
|
|
521
|
+
raise watchdog.BrowserBudgetExhausted(
|
|
522
|
+
"article page budget exhausted before next verification poll")
|
|
474
523
|
page.wait_for_timeout(wait_ms)
|
|
475
524
|
continue
|
|
476
525
|
if waited:
|
|
@@ -480,14 +529,14 @@ class UserWaitMixin:
|
|
|
480
529
|
self._event(result, "challenge_resolved", getattr(page, "url", ""))
|
|
481
530
|
return True
|
|
482
531
|
self._raise_on_publisher_access_blocked(page)
|
|
483
|
-
passed = not (self._is_challenge_page(page)
|
|
484
|
-
or (waited and challenges.continues_manual_challenge(self._title(page))))
|
|
532
|
+
passed = not (self._is_challenge_page(page) or (waited and self._challenge_continues(page)))
|
|
485
533
|
if passed:
|
|
486
534
|
result.state = state_before
|
|
487
535
|
else:
|
|
488
536
|
self._wait_ran_out(result)
|
|
489
537
|
return passed
|
|
490
538
|
finally:
|
|
539
|
+
self._verification_attention(result, None)
|
|
491
540
|
activity.automatic_for(0)
|
|
492
541
|
if verification_wait is not None:
|
|
493
542
|
excluded = max(0, verification_wait.close() - verification_deadline)
|
|
@@ -508,6 +557,12 @@ class UserWaitMixin:
|
|
|
508
557
|
if getattr(result, "_attention_page", None) is page:
|
|
509
558
|
result._attention_page = None
|
|
510
559
|
|
|
560
|
+
def _challenge_continues(self, page: Any) -> bool:
|
|
561
|
+
# Only after a recognized challenge: the outer page can change title
|
|
562
|
+
# before its second, user-operated prompt is gone.
|
|
563
|
+
return (challenges.continues_manual_challenge(self._title(page))
|
|
564
|
+
or challenge_interaction.has_visible_recaptcha(page))
|
|
565
|
+
|
|
511
566
|
def _challenge_shell(self, page: Any) -> bool:
|
|
512
567
|
"""Read only URL/title while a verification interstitial is active."""
|
|
513
568
|
try:
|
|
@@ -465,9 +465,9 @@ def _try_browser_batch_download(
|
|
|
465
465
|
config: dict[str, Any],
|
|
466
466
|
profile: Any | None,
|
|
467
467
|
) -> dict[str, Any] | None:
|
|
468
|
-
"""Try single-DOI download via PublisherBatchDownloader
|
|
468
|
+
"""Try single-DOI download via PublisherBatchDownloader in the download browser.
|
|
469
469
|
|
|
470
|
-
Uses the
|
|
470
|
+
Uses the browser state machine: navigate → SSO login → capture PDF → verify.
|
|
471
471
|
This is the most capable built-in path — handles Cloudflare, SSO redirects,
|
|
472
472
|
and publisher-specific PDF extraction.
|
|
473
473
|
"""
|
|
@@ -482,8 +482,8 @@ def _try_browser_batch_download(
|
|
|
482
482
|
|
|
483
483
|
try:
|
|
484
484
|
from ..browser.backend import launch_persistent_context # noqa: F401
|
|
485
|
-
# The configured browser
|
|
486
|
-
#
|
|
485
|
+
# The configured browser, not the default one (2026-11-01: the
|
|
486
|
+
# default-backend check skipped every ego download).
|
|
487
487
|
from ..browser.engine import is_available as _browser_backend_available
|
|
488
488
|
if not _browser_backend_available(config):
|
|
489
489
|
log.info(" [Institutional] no browser backend available, skipping browser download")
|
|
@@ -519,24 +519,30 @@ def _try_browser_batch_download(
|
|
|
519
519
|
|
|
520
520
|
from ..browser import watchdog
|
|
521
521
|
|
|
522
|
+
from ..browser.backend import supports
|
|
523
|
+
from ..browser.batch_session import active_batch
|
|
524
|
+
|
|
522
525
|
supplements_mode = str(config.get("supplements") or "")
|
|
526
|
+
batch = active_batch.get() if supports("batch_window", config) else None
|
|
523
527
|
try:
|
|
524
|
-
context = downloader._launch_context()
|
|
528
|
+
context = batch.open(downloader) if batch is not None else downloader._launch_context()
|
|
525
529
|
try:
|
|
526
530
|
if supplements_mode == "only":
|
|
527
531
|
result = DownloadResult(doi=doi, status="failed", state="supplements_only",
|
|
528
532
|
reason="supplements_only", article_url=downloader._article_url(doi))
|
|
529
533
|
else:
|
|
530
|
-
result = downloader.fetch_one(context, record, run_dir)
|
|
534
|
+
result = downloader.fetch_one(batch.page if batch is not None else context, record, run_dir)
|
|
531
535
|
if _browser_closed_at_start(result):
|
|
532
536
|
# The browser closed under the item's first page: the previous
|
|
533
537
|
# item's browser was still releasing the profile. Once more.
|
|
534
538
|
try:
|
|
535
|
-
context.close()
|
|
539
|
+
(batch.close if batch is not None else context.close)()
|
|
536
540
|
except Exception:
|
|
537
541
|
pass
|
|
538
|
-
|
|
539
|
-
|
|
542
|
+
relaunch = "the browser closed at the first page"
|
|
543
|
+
context = (batch.open(downloader, relaunch=relaunch) if batch is not None
|
|
544
|
+
else downloader._launch_context(relaunch=relaunch))
|
|
545
|
+
result = downloader.fetch_one(batch.page if batch is not None else context, record, run_dir)
|
|
540
546
|
if watchdog.fired and not result.ok:
|
|
541
547
|
# The browser call never returned and the driver was stopped:
|
|
542
548
|
# whatever the engine reported after that is a consequence.
|
|
@@ -571,10 +577,14 @@ def _try_browser_batch_download(
|
|
|
571
577
|
downloader._drop_snapshot_pictures(result)
|
|
572
578
|
_note_supplements(result, written)
|
|
573
579
|
finally:
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
580
|
+
if batch is not None:
|
|
581
|
+
if not watchdog.fired:
|
|
582
|
+
batch.finish_item()
|
|
583
|
+
else:
|
|
584
|
+
try:
|
|
585
|
+
context.close()
|
|
586
|
+
except Exception:
|
|
587
|
+
pass
|
|
578
588
|
except Exception as e:
|
|
579
589
|
log.info(" [Institutional] Browser batch download error: %s", type(e).__name__)
|
|
580
590
|
if isinstance(e, watchdog.BrowserOperationTimeout):
|
|
@@ -18,7 +18,7 @@ _READ_ACTIVITY = """token => {
|
|
|
18
18
|
if (!state || state.token !== token) {
|
|
19
19
|
if (state) state.dispose();
|
|
20
20
|
state = {token, count: 0};
|
|
21
|
-
//
|
|
21
|
+
// Some automation marks script events trusted. Require native input
|
|
22
22
|
// metadata as well; submit/change alone cannot establish takeover.
|
|
23
23
|
const events = ['input', 'keydown', 'pointerdown'];
|
|
24
24
|
const record = event => {
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
"""
|
|
1
|
+
"""Track idle owned pages without treating missing progress as a user action.
|
|
2
2
|
|
|
3
3
|
Main-document requests, navigation, a loaded document, a workflow step or
|
|
4
4
|
received file bytes refresh the timer. An in-flight navigation gets only the
|
|
@@ -7,6 +7,7 @@ The request watcher can check the timer without making a browser RPC.
|
|
|
7
7
|
"""
|
|
8
8
|
from __future__ import annotations
|
|
9
9
|
|
|
10
|
+
import logging
|
|
10
11
|
import threading
|
|
11
12
|
import time
|
|
12
13
|
from contextlib import contextmanager
|
|
@@ -25,9 +26,8 @@ class PageActivity:
|
|
|
25
26
|
self._clock = clock or time.monotonic
|
|
26
27
|
self._last_progress = self._clock()
|
|
27
28
|
self._lock = threading.RLock()
|
|
28
|
-
self._key = f"page-stalled:{id(self)}:{doi}"
|
|
29
29
|
self._doi = doi
|
|
30
|
-
self.
|
|
30
|
+
self._reported = False
|
|
31
31
|
self._transfers = 0
|
|
32
32
|
self._automatic_until = 0.0
|
|
33
33
|
self.closed = False
|
|
@@ -45,16 +45,14 @@ class PageActivity:
|
|
|
45
45
|
self._clear()
|
|
46
46
|
|
|
47
47
|
def _clear(self) -> None:
|
|
48
|
-
|
|
49
|
-
progress_reporter.clear_attention(self._key)
|
|
50
|
-
self._shown = False
|
|
48
|
+
self._reported = False
|
|
51
49
|
|
|
52
50
|
def automatic_for(self, seconds: float) -> None:
|
|
53
|
-
"""Defer idle
|
|
51
|
+
"""Defer idle diagnostics until the caller's existing automatic deadline.
|
|
54
52
|
|
|
55
53
|
This is a finite lease, not progress: polling must pass only the time
|
|
56
54
|
remaining on the original bound. Zero releases it at manual handoff.
|
|
57
|
-
The watcher can still
|
|
55
|
+
The watcher can still log if the browser call never returns.
|
|
58
56
|
"""
|
|
59
57
|
with self._lock:
|
|
60
58
|
self._automatic_until = self._clock() + max(0.0, seconds)
|
|
@@ -66,19 +64,21 @@ class PageActivity:
|
|
|
66
64
|
with self._lock:
|
|
67
65
|
if self.closed:
|
|
68
66
|
return
|
|
69
|
-
if progress_reporter.has_attention(current=self._doi
|
|
67
|
+
if progress_reporter.has_attention(current=self._doi):
|
|
70
68
|
# School login and other explicit requests have priority. Start
|
|
71
69
|
# a fresh quiet interval when they finish rather than duplicating them.
|
|
72
70
|
self._last_progress = self._clock()
|
|
73
71
|
self._clear()
|
|
74
|
-
elif (not self.
|
|
72
|
+
elif (not self._reported and not self._transfers
|
|
75
73
|
and self._clock() >= self._automatic_until
|
|
76
74
|
and not self._navigation_loading()
|
|
77
75
|
and self._clock() - self._last_progress >= self.after):
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
76
|
+
# Silence is not evidence of a login or verification control.
|
|
77
|
+
# Keep a bounded diagnostic; only the workflow can request the
|
|
78
|
+
# user's attention after inspecting the current page.
|
|
79
|
+
logging.getLogger(__name__).info("page_idle: no observed progress for %.0f seconds",
|
|
80
|
+
self._clock() - self._last_progress)
|
|
81
|
+
self._reported = True
|
|
82
82
|
|
|
83
83
|
def _navigation_loading(self) -> bool:
|
|
84
84
|
pending = self._navigations.get(id(self.page))
|
|
@@ -156,7 +156,7 @@ class PageActivity:
|
|
|
156
156
|
self.progress()
|
|
157
157
|
|
|
158
158
|
def close(self) -> None:
|
|
159
|
-
"""Detach on the browser thread
|
|
159
|
+
"""Detach on the browser thread before the next item."""
|
|
160
160
|
with self._lock:
|
|
161
161
|
self.closed = True
|
|
162
162
|
self._navigations.clear()
|
|
@@ -3,6 +3,8 @@ from __future__ import annotations
|
|
|
3
3
|
|
|
4
4
|
import asyncio
|
|
5
5
|
from contextlib import suppress
|
|
6
|
+
from concurrent.futures import Executor
|
|
7
|
+
from contextvars import copy_context
|
|
6
8
|
import threading
|
|
7
9
|
from typing import Callable, Any
|
|
8
10
|
|
|
@@ -11,16 +13,22 @@ from . import progress_reporter
|
|
|
11
13
|
STAGE_PREFIX = "carsi.stage:"
|
|
12
14
|
|
|
13
15
|
|
|
14
|
-
async def run_with_progress(context: Any, identifier: str, operation: Callable[[], str]
|
|
16
|
+
async def run_with_progress(context: Any, identifier: str, operation: Callable[[], str],
|
|
17
|
+
*, executor: Executor | None = None) -> str:
|
|
15
18
|
"""Stream fixed stages while the blocking operation runs off the event loop.
|
|
16
19
|
|
|
17
20
|
The injected MCP context owns the caller's progress token (or sends nothing
|
|
18
21
|
for an older client). Neither a global progress file nor GUI prose is read.
|
|
19
22
|
"""
|
|
20
|
-
if context is None:
|
|
21
|
-
return await asyncio.to_thread(operation)
|
|
22
|
-
|
|
23
23
|
loop = asyncio.get_running_loop()
|
|
24
|
+
|
|
25
|
+
async def run() -> str:
|
|
26
|
+
if executor is None:
|
|
27
|
+
return await asyncio.to_thread(operation)
|
|
28
|
+
return await loop.run_in_executor(executor, copy_context().run, operation)
|
|
29
|
+
|
|
30
|
+
if context is None:
|
|
31
|
+
return await run()
|
|
24
32
|
queue: asyncio.Queue[str | None] = asyncio.Queue(maxsize=64)
|
|
25
33
|
active = threading.Event()
|
|
26
34
|
active.set()
|
|
@@ -64,7 +72,7 @@ async def run_with_progress(context: Any, identifier: str, operation: Callable[[
|
|
|
64
72
|
cancelled = False
|
|
65
73
|
try:
|
|
66
74
|
with progress_reporter.listen_for_stages(identifier, observe):
|
|
67
|
-
return await
|
|
75
|
+
return await run()
|
|
68
76
|
except asyncio.CancelledError:
|
|
69
77
|
cancelled = True
|
|
70
78
|
raise
|
|
@@ -238,7 +238,7 @@ def fetch_with_browser(
|
|
|
238
238
|
*,
|
|
239
239
|
stream: bool = False,
|
|
240
240
|
) -> requests.Response | None:
|
|
241
|
-
"""Fetch URL
|
|
241
|
+
"""Fetch URL in the shared browser to pass Cloudflare challenges."""
|
|
242
242
|
from .browser.engine import solve_url, is_available
|
|
243
243
|
if not is_available(config):
|
|
244
244
|
return None
|