@jayyuen66/dsh-scrapling 0.0.1-rc.2 → 0.0.1

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.md CHANGED
@@ -68,7 +68,13 @@ would throw all of that away.
68
68
 
69
69
  `scrapling_crawl` returns a **job id immediately** rather than blocking: a crawl takes
70
70
  minutes, a tool call has a timeout. Watch it with `job_list`, read it with
71
- `job_output`, stop it with `job_kill`.
71
+ `job_output`, stop it with `job_kill`. A crawl also has a wall-clock ceiling
72
+ (`crawlMaxRuntimeSeconds`) and never leaves the start domain. Politeness is the
73
+ deployment's call, stated in the open: Scrapling's spider defaults are kept as-is —
74
+ robots.txt is **not** obeyed, per-domain delay is 0, and up to 4 requests run in
75
+ parallel. The crawl is confined to the start domain, so it never fans out across
76
+ unrelated sites; a target that needs gentler behaviour should sit behind a rate
77
+ limiter on the deployment side.
72
78
 
73
79
  ## Requirements
74
80
 
@@ -174,6 +180,10 @@ Notable switches:
174
180
  ceiling over the model-supplied `maxPages`; an out-of-range `maxPages` (0, negative,
175
181
  NaN) is passed through unchanged so the sidecar reports the parameter error rather
176
182
  than this plugin silently clamping it into a legal-looking value.
183
+ `crawlMaxRuntimeSeconds` is the crawl's wall-clock ceiling: the page cap says nothing
184
+ about a site that takes a minute per page, and a crawl is a background thread nobody
185
+ is watching. On expiry the spider winds down through its own stop channel instead of
186
+ running until the site runs out.
177
187
  - `stripInlineImages` - **on** by default: inline `data:` URI images are dropped from
178
188
  the body and only their alt text is kept. Such an image is pure token noise - a
179
189
  base64 SVG costs characters in the context while the model cannot read a shape out
@@ -223,8 +233,8 @@ Notable switches:
223
233
 
224
234
  **Deployment-grade knobs** (not on the settings card; set them in the `config:` block of
225
235
  `cordis.patch.yml`): `requestTimeoutSeconds`, `maxUrlLength`, `browserExecutablePath`,
226
- `browserCdpUrl`, `searchEndpoint`, `searchRenderTopN`. The first two of the browser pair are
227
- strong capabilities - one hands Chromium a file
236
+ `browserCdpUrl`, `searchEndpoint`, `searchRenderTopN`, `crawlMaxRuntimeSeconds`. The first
237
+ two of the browser pair are strong capabilities - one hands Chromium a file
228
238
  to execute, the other hands the browser to a debugger endpoint - so they come only from
229
239
  deployment config and are unreachable from model input.
230
240
 
@@ -283,11 +293,21 @@ Details worth knowing:
283
293
  makes the plugin stop working. The plugin **detects** that class of resolver on its
284
294
  own: it resolves three control names unrelated to any target - `example.com`,
285
295
  `www.iana.org`, `www.wikipedia.org` - and if all of them land in one non-public pool
286
- while staying distinct, it treats the resolver as fake-IP and **learns that pool's
287
- boundary from the control answers**, allowing only addresses inside it.
288
- `syntheticDnsRanges` is the manual fallback for detection failures. When detection does
289
- not fire, the gate **stays strict**; an IP literal written into the URL is still judged
290
- strictly, so `http://169.254.169.254/` stays blocked either way.
296
+ while staying distinct, it treats the resolver as fake-IP and relaxes to the **whole of
297
+ the known synthetic pools** - 198.18.0.0/15 (the clash / mihomo default) and
298
+ 100.64.0.0/10 (some TUN clients). Private, loopback and link-local ranges are in neither
299
+ pool, so `http://192.168.1.10/`, `http://127.0.0.1/` and `http://169.254.169.254/` stay
300
+ blocked on a proxied machine.
301
+
302
+ The control names only **trigger** that verdict; they never size the pool. That was learned
303
+ the hard way: the three answers are hashed by name, so their spread is chance. Measured on a
304
+ real machine they were .126/.134/.135 - a spread of 10, which sliced out 198.18.0.112..127.
305
+ `www.iana.org` and `www.wikipedia.org` then failed their own gate, and `github.com` (.38)
306
+ and `opencode.ai` (.61) were refused outright. Three samples carry no size information.
307
+
308
+ `syntheticDnsRanges` is the manual fallback when detection fails (a self-built pool outside
309
+ the two ranges above). When detection does not fire, the gate **stays strict**; an IP literal
310
+ written into the URL is still judged strictly.
291
311
  - **Through a proxy, a host name this machine cannot resolve is no longer rejected.** The
292
312
  request never leaves this machine's network namespace, the proxy resolves the name, and
293
313
  a local resolver coming up empty says nothing - while on a network with DNS filtered by
@@ -359,14 +379,15 @@ JSON endpoint.
359
379
  address, not only the ones inside the RFC 6052 segment.
360
380
  - **No port allowlist** (any port allowed by default), matching dsh's own built-in
361
381
  fetch provider.
362
- - **`scrapling_crawl` re-checks addresses itself, but redirect targets still rest on
363
- domain consistency alone.** The start URL goes through `guard.url_allowed` (the same
364
- implementation the browser path uses, so both layers agree), the crawl session sets
365
- `follow_redirects="safe"` so curl_cffi refuses to follow a jump into a private range, and
366
- each page's final URL is re-checked before it enters the result set. Scrapling's
367
- `allow_domains`, though, is a string comparison that covers links found in the page and
368
- not redirect targets - so on that seam the host gate's start-URL check is still the only
369
- defence.
382
+ - **`scrapling_crawl` re-checks addresses itself, but the start-URL gate is still the
383
+ first line.** The start URL goes through `guard.url_allowed` (the same implementation
384
+ the browser path uses, so both layers agree), the crawl session sets
385
+ `follow_redirects="safe"` so curl_cffi refuses to follow a jump into a private range,
386
+ and each page is re-checked **twice** before it enters the result set: once against
387
+ the guard (the final URL may still have landed somewhere private) and once against the
388
+ start domain - Scrapling's `allow_domains` is a string comparison that covers links
389
+ found in the page and not redirect targets, so an off-site jump on a public host has to
390
+ be caught here.
370
391
  - **WebSocket coverage depends on the Playwright version.** The guard intercepts
371
392
  handshakes via `route_web_socket`. Older builds lack that method, WebSocket is then
372
393
  outside the guard, and the plugin says so in the log and in the capability report
@@ -377,7 +398,9 @@ JSON endpoint.
377
398
 
378
399
  The host-side policy is enforced by `SidecarClient.call`, so it covers every tool and
379
400
  the `web_fetch` provider alike; a rejection throws `URL_BLOCKED` before the request is
380
- written to the pipe.
401
+ written to the pipe. The `web_search` provider runs the same gate over the URLs it is
402
+ about to hand back: a hit the gate rejects never becomes a citeable source, so the model
403
+ is never offered an address the next fetch would refuse.
381
404
  - **Browser engines ignore proxy environment variables.** Playwright does not read
382
405
  `HTTP_PROXY` / `HTTPS_PROXY`, so `scrapling_render` / `scrapling_capture_xhr` /
383
406
  `scrapling_stealth_fetch` connect **directly** even when the rest of the system is
@@ -405,11 +428,41 @@ JSON endpoint.
405
428
 
406
429
  ```sh
407
430
  pnpm install # install dependencies
408
- pnpm run check # typecheck + lint + build + test + format check
431
+ pnpm run check # typecheck + lint + build + test + format check, TS *and* Python
409
432
  pnpm run build # bundles host.ts / src/client-entry.ts into host.js / client.js
410
433
  pnpm run test:py # end-to-end check of the Python sidecar (needs _env/.venv)
411
434
  ```
412
435
 
436
+ **The Python half is gated too.** The four `.py` files ship with this package
437
+ (`package.json`'s `files` lists them one by one) and run on the user's machine, so they
438
+ get the same treatment as `host.ts`: `pnpm run check` runs `ruff check` and
439
+ `ruff format --check` over `py/`, and fails hard when the tools are missing rather than
440
+ skipping quietly. Install them into whichever interpreter `python3` resolves to:
441
+
442
+ ```sh
443
+ pip install -r py/requirements-dev.txt
444
+ ```
445
+
446
+ Two more commands measure rather than gate:
447
+
448
+ ```sh
449
+ pnpm run typecheck:py # mypy over py/ (currently clean; why it stays advisory is below)
450
+ pnpm run cov:py # coverage for the four shipped modules + the test script
451
+ ```
452
+
453
+ Run both with an interpreter that has Scrapling installed (the same one `test:py` uses).
454
+ That is not decoration: mypy's findings depend on it. From an interpreter without
455
+ Scrapling every Scrapling type degrades to `Any` and the check reports nothing at all.
456
+
457
+ `mypy` stays advisory because this side is duck-typed on purpose — Playwright's `page`,
458
+ `route` and `websocket` have no shape until runtime — and a strict profile would buy a
459
+ pile of `ignore` comments that exist only to silence it. `cov:py` is advisory because its
460
+ honest number needs the subprocess wired in: `bridge.py` and the `adapters` methods live
461
+ in the sidecar child, so `test_bridge.py` spawns that child under `coverage run` when
462
+ `DSH_PY_COVERAGE=1`. Without it the report reads `adapters.py` 20% while none of its fetch
463
+ paths have been counted at all — a number that measures nothing. Current shape:
464
+ `adapters` 65%, `bridge` 37%, `crawl` 89%, `guard` 85%.
465
+
413
466
  Sources are all TypeScript. `host.js` and `client.js` are **build artifacts** and are
414
467
  deliberately `.js`: Node refuses to strip types for packages inside `node_modules`,
415
468
  which is exactly where dsh loads the plugin from.
package/README.zh-CN.md CHANGED
@@ -59,7 +59,11 @@ cookie,浏览器引擎要保留页面池,`adaptive` 要保留 SQLite 指纹
59
59
  这些东西全都会丢。
60
60
 
61
61
  `scrapling_crawl` **立刻返回作业 id** 而不是阻塞:一次爬取是分钟级的,而工具调用有超时。
62
- 用 `job_list` 看进度、`job_output` 读结果、`job_kill` 中断。
62
+ 用 `job_list` 看进度、`job_output` 读结果、`job_kill` 中断。爬虫另有总时长上限
63
+ (`crawlMaxRuntimeSeconds`),且不会爬出起始域。礼貌性由部署方定夺,这里如实声明:
64
+ 沿用了 Scrapling spider 的默认值 —— **不遵守** robots.txt、每域延迟为 0、最多 4 个请求
65
+ 并发。爬取被锁在起始域内,不会向无关站点扩散;需要更温和行为的站点,请在部署侧套一层
66
+ 限速。
63
67
 
64
68
  ## 环境要求
65
69
 
@@ -163,7 +167,9 @@ Scrapling 的 Markdown 输出报成 `text`,内置渲染路径显示完全正
163
167
  - `*MaxOutputChars` —— 各工具的输出封顶。宿主侧的封顶只影响进模型上下文的那一段,
164
168
  真正的字节上限在 Python 侧(`maxContentChars`)。`crawlMaxItems` 是部署侧给模型可传的
165
169
  `maxPages` 封的顶;越界的 `maxPages`(0、负数、NaN)**原样透传**,让 sidecar 报参数错误,
166
- 而不是由本插件悄悄夹成一个看起来合法的值。
170
+ 而不是由本插件悄悄夹成一个看起来合法的值。`crawlMaxRuntimeSeconds` 是爬虫的**墙上时钟**
171
+ 上限:页数上限管不了一页等一分钟的站点,而爬虫是没人看的后台线程。到点由蜘蛛走引擎自己
172
+ 的停机通道收尾,而不是等站点自己没得爬。
167
173
  - `stripInlineImages` —— 默认**开**:剥掉正文里内嵌的 `data:` URI 图片,只留 alt 文本。
168
174
  这类图片对模型是纯 token 噪音 —— 一段 base64 SVG 编码进上下文要按字符计费,而模型从里面
169
175
  读不出任何图形信息。**不含内联图的页面逐字节不变**;含内联的页面省掉的量取决于内联图有多少 ——
@@ -184,7 +190,7 @@ Scrapling 的 Markdown 输出报成 `text`,内置渲染路径显示完全正
184
190
 
185
191
  **部署级旋钮**(不在设置卡上,只走 [`cordis.patch.yml`](./cordis.patch.yml) 的 `config:`
186
192
  块):`requestTimeoutSeconds`、`maxUrlLength`、`browserExecutablePath`、`browserCdpUrl`、
187
- `searchEndpoint`、`searchRenderTopN`。
193
+ `searchEndpoint`、`searchRenderTopN`、`crawlMaxRuntimeSeconds`。
188
194
  其中 `browserExecutablePath` 与 `browserCdpUrl` 是强能力 —— 前者等于让 Chromium 执行指定文件,
189
195
  后者等于把浏览器交给指定调试端点 —— 所以它们只从部署配置来,模型入参到不了。
190
196
 
@@ -232,9 +238,18 @@ PAC 是唯一还需要手工填一次的形态:求值 PAC 要下载脚本并
232
238
  (TUN 客户端常落在 `100.64.0.0/10`),两段都不是公网 unicast,闸门于是把每个 URL 都判成
233
239
  私网——开了代理反而全都抓不了。插件**自动识别**这一类解析器:从 `example.com` /
234
240
  `www.iana.org` / `www.wikipedia.org` 三个与用户目标无关的控制域名各解析一次,全部落在同一段
235
- 非公网小池且互不相同,即判为 fake-ip,并**从控制域名的答案里学出那个池子的边界**,只放行
236
- 落在池内的地址。`syntheticDnsRanges` 是识别失灵时的手动兜底。识别不出来时**维持最严**,
237
- 不会误放行;URL 里写死的 IP 字面量照旧严格判定,`http://169.254.169.254/` 始终被拒。
241
+ 非公网段里且互不相同,即判为 fake-ip,随后放宽到**已知合成池的整段**——198.18.0.0/15
242
+ (clash / mihomo 的默认落点)与 100.64.0.0/10(部分 TUN 客户端)。私网、回环、链路本地
243
+ 一个都不在池子里,所以 `http://192.168.1.10/`、`http://127.0.0.1/`、
244
+ `http://169.254.169.254/` 在开着代理的机器上**照旧被拒**。
245
+
246
+ 控制域名只用来**触发**判定,不用来推断池的大小。这一点踩过坑:三个答案是按名字哈希散列的,
247
+ 跨度纯属偶然——真机上实测是 .126/.134/.135,跨度 10,按「覆盖样本的最小 2 的幂块」切出来是
248
+ 198.18.0.112..127 这么个 /28,于是 `www.iana.org`、`www.wikipedia.org` **自己都过不去**,
249
+ `github.com`(.38)与 `opencode.ai`(.61)更被挡在门外。三个样本不含任何池大小的信息。
250
+
251
+ `syntheticDnsRanges` 是识别失灵时的手动兜底(自建池若落在上面两段之外)。识别不出来时
252
+ **维持最严**,不会误放行;URL 里写死的 IP 字面量照旧严格判定。
238
253
  - **走了代理时,本机解析不出来的主机名不再判否。** 请求不从本机网络命名空间出去,域名由代理
239
254
  解析,本机解析器答不上来什么也说明不了——而在 DNS 被按地区过滤的网络里,恰恰是这些站点才
240
255
  需要代理才能访问。IP 字面量、本地能解析出来的私网答案、单标签主机名(`intranet`、`router`
@@ -289,11 +304,12 @@ PAC 是唯一还需要手工填一次的形态:求值 PAC 要下载脚本并
289
304
  会被当成普通公网 IPv6 放行。反过来,自建前缀一旦声明就**对所有 IPv6 生效**,不限于
290
305
  落在 RFC 6052 段里的那些。
291
306
  - **没有端口白名单**(默认不限端口),与 dsh 内置 fetch provider 的取舍一致。
292
- - **`scrapling_crawl` 已有自己的地址复核,但重定向目标仍只靠域名一致性。** 起点 URL 过
307
+ - **`scrapling_crawl` 已有自己的地址复核,但起点那一关仍是第一道。** 起点 URL 过
293
308
  `guard.url_allowed`(与浏览器路径同一个实现,两侧同解),爬虫会话设了
294
- `follow_redirects="safe"` 让 curl_cffi 拒绝跳向私网的跳转,每一页收进结果前再复判一次
295
- 最终 URL。但 Scrapling 的 `allow_domains` 是字符串比较,只管页面里发现的链接、不管重定向
296
- 目标 —— 重定向这条缝上真正的防线仍只有 Host 闸门起点那一道。
309
+ `follow_redirects="safe"` 让 curl_cffi 拒绝跳向私网的跳转,每一页收进结果前再复判
310
+ **两次**:一次过守卫(最终 URL 仍可能落在私网上),一次过起始域 —— Scrapling 的
311
+ `allow_domains` 是字符串比较,只管页面里发现的链接、不管重定向目标,所以跳向站外
312
+ 公网主机的那一跳必须在这里拦住。
297
313
  - **WebSocket 覆盖依赖 Playwright 版本。** 守卫用 `route_web_socket` 拦握手;更老的
298
314
  Playwright 没有这个方法,那种情况下 WebSocket 不在守卫范围内,插件会往日志与能力报告
299
315
  里如实标出,不会静默降级。
@@ -301,7 +317,9 @@ PAC 是唯一还需要手工填一次的形态:求值 PAC 要下载脚本并
301
317
  指向它们等于交出「执行这个文件」与「把浏览器交给这个调试端点」。
302
318
 
303
319
  Host 侧那道策略由 `SidecarClient.call` 统一执行,因此覆盖每一个工具,也覆盖
304
- `web_fetch` provider;判否时抛 `URL_BLOCKED`,请求根本不会写进管道。
320
+ `web_fetch` provider;判否时抛 `URL_BLOCKED`,请求根本不会写进管道。`web_search` provider
321
+ 也对它准备交回的 URL 跑同一道闸门:被闸门判否的命中不会成为可引用的 source,模型因此不会
322
+ 拿到一个「下一次抓取必然被拒」的地址。
305
323
 
306
324
  - **浏览器引擎不认代理环境变量。** Playwright 不读 `HTTP_PROXY` / `HTTPS_PROXY`,所以
307
325
  `scrapling_render` / `scrapling_capture_xhr` / `scrapling_stealth_fetch` 即使在整台机器