diff --git a/README.md b/README.md index 1028eb1..6168bb4 100644 --- a/README.md +++ b/README.md @@ -154,7 +154,8 @@ zenrows extract --output markdown On a domain Extract hasn't prepared yet, `zenrows extract` fails with `EXTRACT_DOMAIN_NOT_PREPARED` (API code REQS007) and prints the command to prepare it. Preparing takes a few minutes and costs no credits; each new domain -uses one slot of the monthly allowance. `EXTRACT_PAGE_NOT_RECOGNISED` (also +uses one slot of the monthly allowance, and a preparation rejected by +verification gives its slot back. `EXTRACT_PAGE_NOT_RECOGNISED` (also REQS007) is different: the domain is prepared, but this page is not one Extract extracts. Try a product, article or listing page instead, retry it once in a few minutes if it should work, or use `--css`; preparing again does not change @@ -175,6 +176,14 @@ zenrows extract prepare status example.com # progress + monthly allowance zenrows extract https://www.example.com/products/a # then extract again ``` +`prepare` sends each request once and names the next step when it is refused: +`EXTRACT_PREPARE_OUT_OF_ALLOWANCE` means the account has no slots left this +month. `EXTRACT_PREPARE_DOMAIN_REJECTION_LIMIT` means this domain's +preparations were rejected by verification as many times as the monthly limit +per domain allows: do not retry this domain this month (other domains can still +be prepared, and nothing was spent); meanwhile use `--css` on it. +`EXTRACT_PREPARE_UNAVAILABLE` means retry in a few minutes, without a loop. + ## 9. Batch Zenrows **Batch** (`https://async.api.zenrows.com/v1`) fans a diff --git a/agent-plugin/skills/extract/SKILL.md b/agent-plugin/skills/extract/SKILL.md index 415ae1b..11bb1fc 100644 --- a/agent-plugin/skills/extract/SKILL.md +++ b/agent-plugin/skills/extract/SKILL.md @@ -67,7 +67,11 @@ extract({ url }) # 4. extract again - Preparing takes a few minutes and costs no credits. Each new domain uses one slot of the account's monthly allowance; `prepared_domain_status` returns it (`used` / `limit` / `remaining`) and a `next` field with what to do now. + A preparation rejected by verification gives its slot back. - `out_of_allowance`: no slots left this month; use `mode=css`. + `domain_rejection_limit`: this domain's preparations were rejected by + verification as often as the monthly limit allows. Do not retry this domain + this month; other domains can still be prepared. Use `mode=css` on it. `overloaded` / `disabled`: retry in a few minutes; do not loop. A `failed` status carries `failure.code` and `failure.slot_consumed`; follow `next`. - A domain that is already prepared answers `ready` at once diff --git a/skills/extract/SKILL.md b/skills/extract/SKILL.md index 4de84ca..faac41d 100644 --- a/skills/extract/SKILL.md +++ b/skills/extract/SKILL.md @@ -65,8 +65,11 @@ zenrows extract https://www.example.com/products/a # 3. extract again separately, and a leading `www.` is dropped. - Preparing takes a few minutes and costs no credits. Each new domain uses one slot of the account's monthly allowance; `prepare status` shows `used` / - `limit` / `remaining`. + `limit` / `remaining`. A preparation rejected by verification gives its slot back. - `EXTRACT_PREPARE_OUT_OF_ALLOWANCE`: no slots left this month; use `--css`. + `EXTRACT_PREPARE_DOMAIN_REJECTION_LIMIT`: this domain's preparations were + rejected by verification as often as the monthly limit allows. Do not retry + this domain this month; other domains can still be prepared. Use `--css` on it. `EXTRACT_PREPARE_UNAVAILABLE`: retry in a few minutes; do not loop. `prepare` sends each request once: `--retries` does not apply to it. A `failed` status prints the next step for its failure code and says whether diff --git a/src/core/errors.ts b/src/core/errors.ts index cd32a64..e30a4fb 100644 --- a/src/core/errors.ts +++ b/src/core/errors.ts @@ -39,6 +39,7 @@ export type ErrorCode = | "EXTRACT_PREPARE_NOT_FOUND" | "EXTRACT_PREPARE_IN_PROGRESS" | "EXTRACT_PREPARE_OUT_OF_ALLOWANCE" + | "EXTRACT_PREPARE_DOMAIN_REJECTION_LIMIT" | "EXTRACT_PREPARE_UNAVAILABLE" | "EXTRACT_PREPARE_FAILED" | "EXTRACT_PREPARE_TIMEOUT" diff --git a/src/core/prepared-domains-api.ts b/src/core/prepared-domains-api.ts index 384e253..21f7de8 100644 --- a/src/core/prepared-domains-api.ts +++ b/src/core/prepared-domains-api.ts @@ -184,7 +184,22 @@ export function prepareError(status: number, body: string, domain: string): Tool server_code: code, }); } - if (code === "out_of_allowance" || status === 429) { + if (code === "domain_rejection_limit") { + const counts = + p.rejected !== undefined && p.limit !== undefined + ? ` (${String(p.rejected)} rejected this month, limit ${String(p.limit)} per domain)` + : ""; + return new ToolkitError({ + code: "EXTRACT_PREPARE_DOMAIN_REJECTION_LIMIT", + message: `${domain} has reached the monthly limit of preparations rejected by verification${counts}.`, + likely_cause: cause, + next_action: + "Do not retry this domain this month; new attempts reopen at the start of next month (UTC). This submission used no allowance slot, and other domains can still be prepared. Meanwhile extract this domain with --css and explicit selectors.", + suggested_commands: [`zenrows extract --css '{"title":"h1"}'`], + server_code: code, + }); + } + if (code === "out_of_allowance") { const used = p.used !== undefined && p.limit !== undefined ? ` (${String(p.used)} of ${String(p.limit)} used)` : ""; return new ToolkitError({ code: "EXTRACT_PREPARE_OUT_OF_ALLOWANCE", @@ -195,6 +210,16 @@ export function prepareError(status: number, body: string, domain: string): Tool server_code: code, }); } + if (status === 429) { + // A 429 this CLI does not know: the server's detail says what to do, not the allowance. + return new ToolkitError({ + code: "EXTRACT_PREPARE_FAILED", + message: `Zenrows refused to prepare ${domain} (HTTP 429): ${detail}`, + likely_cause: cause, + next_action: "Follow the message above; do not retry in a loop. Meanwhile extract with --css and explicit selectors.", + server_code: code, + }); + } if (code === "overloaded" || code === "disabled" || status === 503) { return new ToolkitError({ code: "EXTRACT_PREPARE_UNAVAILABLE", diff --git a/tests/extract-prepare.test.ts b/tests/extract-prepare.test.ts index 0f3a8be..ff1a855 100644 --- a/tests/extract-prepare.test.ts +++ b/tests/extract-prepare.test.ts @@ -176,6 +176,57 @@ for (const [status, serverCode, code, pattern] of [ }); } +const REJECTION_DETAIL = + "example.com had 3 rejected preparation attempts this month (failed verification); the limit is 3 per domain per month. This submission was not accepted and not counted against your allowance. New attempts for this domain reopen at the start of next month (UTC)."; + +for (const [rejected, limit] of [ + [3, 3], + [1, 0], +] as const) { + test(`prepare maps 429 domain_rejection_limit (limit ${limit}) to EXTRACT_PREPARE_DOMAIN_REJECTION_LIMIT, not the allowance`, async () => { + const { calls, fetchImpl } = stub({ + [`POST ${BASE}`]: () => + res( + { + code: "domain_rejection_limit", + title: "Domain rejection limit reached", + status: 429, + detail: REJECTION_DETAIL, + domain: "example.com", + rejected, + limit, + month: "2026-11", + }, + 429, + ), + }); + const { code: exit, out } = await run(["example.com", "--url", "https://example.com/p/1"], fetchImpl); + assert.equal(exit, 1); + assert.equal(out.error.code, "EXTRACT_PREPARE_DOMAIN_REJECTION_LIMIT"); + assert.equal(out.error.server_code, "domain_rejection_limit"); + assert.match(out.error.message, new RegExp(`${rejected} rejected this month, limit ${limit} per domain`)); + assert.doesNotMatch(out.error.message, /allowance/); + assert.ok(out.error.likely_cause.includes(REJECTION_DETAIL)); + assert.match(out.error.next_action, /Do not retry this domain this month/); + assert.match(out.error.next_action, /other domains can still be prepared/); + assert.match(out.error.next_action, /--css/); + assert.equal(calls.length, 1); + }); +} + +test("prepare reports an unknown 429 with the server's detail, not as the allowance", async () => { + const { fetchImpl } = stub({ + [`POST ${BASE}`]: () => res({ code: "something_new", status: 429, detail: "Slow down for this domain." }, 429), + }); + const { code: exit, out } = await run(["example.com", "--url", "https://example.com/p/1"], fetchImpl); + assert.equal(exit, 1); + assert.equal(out.error.code, "EXTRACT_PREPARE_FAILED"); + assert.equal(out.error.server_code, "something_new"); + assert.match(out.error.message, /Slow down for this domain\./); + assert.doesNotMatch(out.error.message, /allowance/); + assert.match(out.error.next_action, /do not retry in a loop/); +}); + test("prepare rejects zero, six or non-URL --url values before any request", async () => { const { calls, fetchImpl } = stub({}); for (const args of [