var D=Object.defineProperty;var E=(f)=>f;function F(f,q){this[f]=E.bind(null,q)}var J=(f,q)=>{for(var w in q)D(f,w,{get:q[w],enumerable:!0,configurable:!0,set:F.bind(q,w)})};var K=(f,q)=>()=>(f&&(q=f(f=0)),q);import{Code as z,H1 as G,H2 as A,H3 as I,P as B}from"@contractspec/lib.design-system/components/typography";import{jsx as k,jsxs as v}from"react/jsx-runtime";function N(){return v("div",{className:"space-y-8",children:[v("div",{className:"space-y-4",children:[k(G,{className:"font-bold text-4xl",children:"Spec Experiments"}),k(B,{className:"text-lg text-muted-foreground",children:"Run controlled experiments on ContractSpec operations, gradually shift traffic, and roll back automatically when guardrails trip."})]}),v("div",{className:"space-y-3",children:[k(A,{className:"font-bold text-2xl",children:"Define control + variants"}),k(z,{className:"rounded-lg border bg-muted p-4 text-sm",children:`import { SpecExperimentRegistry } from '@contractspec/lib.growth/spec-experiments';

const registry = new SpecExperimentRegistry().register({
  target: { name: 'billing.createInvoice', version: 4 },
  experiment: {
    key: 'billing.createInvoice.evolution',
    version: '1.0.0',
    goal: 'Reduce PO validation failures',
    primaryMetric: 'latency_ms',
    variants: [
      { id: 'control' },
      { id: 'po-required', description: 'Force PO field', weight: 1 },
    ],
  },
  control: BillingCreateInvoiceV4,
  variants: [
    { id: 'po-required', spec: BillingCreateInvoiceForAcme },
  ],
  rolloutStages: [0.01, 0.1, 0.5, 1],
  guardrails: { errorRateThreshold: 0.02, latencyP99ThresholdMs: 500 },
});`})]}),v("div",{className:"space-y-3",children:[k(A,{className:"font-bold text-2xl",children:"Attach to runtime"}),k(z,{className:"rounded-lg border bg-muted p-4 text-sm",children:`import { createSpecVariantResolver } from '@contractspec/lib.growth/spec-experiments';

adapterContext.specVariantResolver = createSpecVariantResolver({
  adapter,
  resolveUserId: (ctx) => ctx.userId ?? ctx.organizationId ?? 'anon',
});`})]}),v("div",{className:"space-y-3",children:[k(A,{className:"font-bold text-2xl",children:"Track outcomes + auto-rollback"}),k(z,{className:"rounded-lg border bg-muted p-4 text-sm",children:`import {
  SpecExperimentAnalyzer,
  SpecExperimentController,
} from '@contractspec/lib.growth/spec-experiments';

const analyzer = new SpecExperimentAnalyzer(tracker);
const controller = new SpecExperimentController({
  registry,
  analyzer,
  onRollback: (target, evaluation) => notifyOps(target, evaluation.reasons),
});`})]}),k("div",{className:"grid gap-4 md:grid-cols-2",children:[{title:"Deterministic bucketing",description:"ExperimentRunner reuses the same hashing logic as growth experiments—every user sticks to a variant."},{title:"Multi-stage rollouts",description:"Use `rolloutStages` to shift 1% → 10% → 50% → 100%. Guardrails trigger rollbacks automatically."}].map((f)=>v("div",{className:"card-subtle space-y-2 p-4",children:[k(I,{className:"font-semibold text-lg",children:f.title}),k(B,{className:"text-muted-foreground text-sm",children:f.description})]},f.title))})]})}export{N as AdvancedSpecExperimentsPage};
