caelum0x 9c55b78072 feat: Builder Track — 3 named-user tools made demo-ready (self-serve, built to last)
For "Built Beyond the Bench": each tool now lets its named user finish end-to-end
without the builder, with a one-click realistic example, honest loading/empty/error
states (incl. a degraded state if the Anthropic key is usage-capped — no white-screen),
and reasoned, provenance-first results.

- Lab Notebook companion (wet-lab scientist): messy/dictated bench notes -> structured,
  searchable record; grounded vs auto-inferred badges (verbatim quote per grounded
  field, dropped if not found — never invented); Copy-as-JSON / Export-.md; 503 degraded
  path on the structure route.
- Trial Matcher (research coordinator): patient vignette -> ranked ClinicalTrials.gov
  candidates with per-criterion inclusion/exclusion reasoning; honest "unknown — add the
  fact and re-run" for unstated criteria; partial-run degraded mode (per-trial failures
  dropped, never fabricated); one-click example patient.
- Claim Verification (medical-affairs reviewer): example claims (one accurate, one
  subtly-overstated), verdict card + trust score + flagged claim-vs-source spans.

tsc + next build clean. Records the multi-source benchmark run (pre source-count fix)
for the honest trail.
2026-07-11 18:47:39 +03:00

82 lines
3.6 KiB
TypeScript

// ERROR CLASSIFICATION for the CLINICAL TRIAL MATCHER.
//
// During a live demo the shared Anthropic key may be usage-capped (429 rate_limit or
// 403/402 quota/billing) rather than genuinely broken. We must degrade honestly — show the
// coordinator what we could extract and tell them WHY the reasoning is unavailable — instead
// of white-screening on a generic 500. This module gives the pipeline and the route one
// shared way to tell "temporarily usage-capped" apart from a true failure.
//
// It does NOT weaken the grounding moat: it only affects how we REPORT a failure. No result
// is ever fabricated to paper over an outage; a degraded run returns fewer results, honestly.
// Why a stage could not complete. `quota` = the Anthropic key is usage-capped / rate-limited
// (a temporary, explainable condition); `error` = an unexpected failure worth retrying/logging.
export type DegradedReason = "quota" | "error";
// HTTP statuses the Anthropic SDK surfaces when the account/key is usage-capped or throttled:
// 402 payment_required, 403 permission/billing, 429 rate_limit/quota exhausted.
// (529/503/overloaded are transient and already retried in lib/claude.ts, so a 529 that
// reaches here has exhausted its retries and is treated as a plain error.)
const QUOTA_STATUSES = new Set([402, 403, 429]);
// Substrings Anthropic uses in error messages/types for capped or throttled keys, matched
// case-insensitively as a fallback when no numeric status is present on the thrown error.
const QUOTA_HINTS = [
"rate limit",
"rate_limit",
"quota",
"insufficient",
"credit balance",
"billing",
"usage limit",
"usage_limit",
"too many requests",
] as const;
function readStatus(err: unknown): number | undefined {
if (typeof err !== "object" || err === null) return undefined;
const status = (err as { status?: unknown }).status;
return typeof status === "number" ? status : undefined;
}
function readMessage(err: unknown): string {
if (err instanceof Error) return err.message;
if (typeof err === "string") return err;
return "";
}
/**
* True when an error looks like the Anthropic key being usage-capped or rate-limited
* (a temporary, explainable condition) rather than a genuine bug. Checks the numeric HTTP
* status first, then falls back to well-known message substrings. Conservative: anything it
* cannot confidently attribute to a quota/limit is treated as a real error by the caller.
*/
export function isQuotaError(err: unknown): boolean {
const status = readStatus(err);
if (status !== undefined && QUOTA_STATUSES.has(status)) return true;
const message = readMessage(err).toLowerCase();
return QUOTA_HINTS.some((hint) => message.includes(hint));
}
// Map any thrown error to a DegradedReason for uniform, honest reporting.
export function classifyError(err: unknown): DegradedReason {
return isQuotaError(err) ? "quota" : "error";
}
// A user-facing, non-sensitive explanation for a degraded run. NEVER echoes the raw error
// (which could contain request context) or any patient text — only a fixed, safe message.
export function degradedMessage(reason: DegradedReason): string {
if (reason === "quota") {
return (
"Claude is temporarily usage-capped, so per-trial eligibility reasoning is paused. " +
"Any profile shown was extracted before the cap; reload a prior run from the history " +
"panel to see full reasoning, or try again shortly."
);
}
return (
"Eligibility reasoning is temporarily unavailable due to an unexpected error. This has " +
"been logged. Any profile shown was extracted successfully — please try again shortly."
);
}