feat(jobs): robustness layer + agentic orchestrator#85
Merged
Conversation
Two additions on top of the freshly-merged jobs+reports infra:
Deterministic foundation
src/jobs/retry.ts whitelist retry with exp backoff + jitter
src/jobs/anti-bot.ts pure pattern match for blocked pages
src/jobs/cost-history.ts adaptive per-audit cost from prior jobs
schemaVersion on tokens.json + reader gate
bad jobs resume <id> re-run missing targets only
Agentic orchestrator
src/jobs/orchestrator.ts deterministic fan-out then LLM loop only
if intervention is warranted
5 tools: getJobState, resampleWayback, retryTarget, markSkipped,
concludeJob
hard caps: 2 retries/target, 1 resample/url, cost <= cap*0.9
bad jobs orchestrate --spec <file>
Architectural rule: protocols are deterministic (retry, anti-bot
detection, schema gating). Judgment is agentic (when to re-sample
broken wayback snapshots, retry vs skip, conclude). The agent never
runs unless needsIntervention() returns true on the deterministic
fan-out result, so the happy path pays no LLM tax.
+34 tests across retry / anti-bot / cost-history / orchestrator gate /
orchestrator agent (MockLanguageModelV3). Total: 1494 passing.
24fd4b3 to
8a0a8f7
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Two additions on top of the freshly-merged jobs+reports infra (#83 / #84):
The architectural rule (it actually matters)
Protocols are deterministic. Judgment is agentic.
So the stack looks like:
```
spec → discoverTargets → runJob (retry + anti-bot built in, deterministic)
↓
needsIntervention()? ──no──→ done
↓ yes
orchestrator agent loop
├─ getJobState
├─ resampleWayback (≤1/url)
├─ retryTarget (≤2/target)
├─ markSkipped
└─ concludeJob
```
What's in it
`src/jobs/retry.ts` — whitelist-based retry with exp backoff + jitter. Retries 429 / 5xx / network / timeouts. Everything else (4xx, anti-bot, schema, unknown) is treated as deterministic and not retried.
`src/jobs/anti-bot.ts` — pure pattern match against `report.json`. Cloudflare interstitial titles, "Just a moment...", "Access denied", challenge-page intents, plus a last-resort heuristic (zero findings + low confidence + unknown type). When fired the runner records `skipped` with a reason instead of putting a bogus score on the leaderboard.
`src/jobs/cost-history.ts` — adaptive cost estimate from prior `~/.bad/jobs/` records. Uses static default until N≥3 completed jobs exist; afterward averages per-target cost from the last 20. Floors at 50% of static default to prevent runaway optimism on a stretch of zero-cost claude-code jobs.
Schema versioning — `tokens.json` is now stamped with `schemaVersion: 1` at write time. Aggregator refuses files older than `MIN_TOKENS_SCHEMA`.
Resume — `bad jobs resume ` re-runs only targets that aren't already `ok`/`skipped`. Same on the API via `RunJobOptions.resume`.
`src/jobs/orchestrator.ts` — `orchestrateJob(job, opts)` runs the deterministic fan-out then enters a control loop only if `needsIntervention()` returns true. Five tools (`getJobState`, `resampleWayback`, `retryTarget`, `markSkipped`, `concludeJob`) with hard caps: 2 retries per target, 1 resample per URL, cost ≤ `spec.maxCostUSD * 0.9`. Default brain uses `claude-code` provider (subscription-based, no API key required). CLI: `bad jobs orchestrate --spec <file.json>`.
Tests
+34 across:
Total: 1494 passing (up from 1460).
Test plan
Notes
The orchestrator's `needsIntervention` gate is the single most important seam in the design. Without it every job pays the LLM tax even when there's nothing to do. With it, a clean run (every target ok) skips the agent entirely and produces deterministic output. Verified end-to-end: clean stripe.com run → orchestrator prints `no intervention needed` and exits.