{
  "objective": "Design a shared stateful autoscaling framework from measured knobs and constraints",
  "portable_view": [
    "nodes and capacity",
    "router instances",
    "logical datasets/shards and co-location groups",
    "replicas, roles and upstream links",
    "resource and storage allocations",
    "consistency and routing policy",
    "workload mix, achieved throughput, errors and admission drops",
    "in-flight action state and receipts"
  ],
  "action_contracts": [
    {
      "action": "resize_resources",
      "scope": "replica or router",
      "signals": [
        "CPU quota throttling",
        "per-role CPU",
        "queue-inclusive tail latency",
        "available node allocation"
      ],
      "preconditions": [
        "admissible on existing node",
        "preserve database role and storage",
        "memory-safe configuration"
      ],
      "completion": [
        "requested",
        "admitted",
        "effective cgroup quota verified",
        "service recovered",
        "performance observed"
      ],
      "evidence": [
        "cpu-1-r1",
        "cpu-1-r2",
        "cpu-4-r1",
        "transition-cpu-1-to-4",
        "transition-coordinator-cpu-2-to-3"
      ],
      "finding": "CPU quota bursts and throttled periods can worsen tails despite moderate average CPU. Router and data-instance CPU are separate knobs. Client parallelism can hide router benefits."
    },
    {
      "action": "configure_cache",
      "scope": "database instance",
      "signals": [
        "physical read bytes and I/O latency",
        "database cache misses",
        "memory pressure",
        "working-set size"
      ],
      "preconditions": [
        "engine-specific memory accounting",
        "restart requirement known",
        "enough memory beyond shared buffers"
      ],
      "completion": [
        "configuration applied",
        "restart completed if needed",
        "recovery complete",
        "warm-up state recorded"
      ],
      "evidence": [
        "memory-1Gi-128MB-r1",
        "memory-4Gi-128MB-r1",
        "memory-4Gi-1GB-r1"
      ],
      "finding": "Limited latency sensitivity in the tested regime. This does not establish that memory is unimportant; larger working sets and controlled cold-cache tests remain necessary."
    },
    {
      "action": "set_read_routing",
      "scope": "shard and connection pool",
      "signals": [
        "actual per-replica read traffic",
        "replica lag",
        "read consistency requirement"
      ],
      "preconditions": [
        "standby readable",
        "staleness permitted",
        "new connections use correct routing options"
      ],
      "completion": [
        "routing configured",
        "pool rolled over",
        "traffic observed on intended replicas"
      ],
      "evidence": [
        "core-A-skew50-secondary-r1",
        "core-B-skew90-secondary-r1",
        "added-replica-B-skew90-r1"
      ],
      "finding": "Read routing changes which role is busiest. Citus routing is selected at connection start. Registering an extra replica did not by itself spread reads onto it."
    },
    {
      "action": "add_replica",
      "scope": "replication group",
      "signals": [
        "bootstrap progress",
        "WAL catch-up",
        "source I/O",
        "actual routed reads"
      ],
      "preconditions": [
        "source healthy",
        "storage and existing-node capacity available",
        "failure-domain constraints checked"
      ],
      "completion": [
        "storage ready",
        "base copy complete",
        "streaming",
        "caught up",
        "registered",
        "routed",
        "useful"
      ],
      "evidence": [
        "transition-add-read-replica"
      ],
      "finding": "Measured readiness plus registration cost is distinct from obtaining useful serving capacity."
    },
    {
      "action": "reparent_replica",
      "scope": "replication topology",
      "signals": [
        "WAL sender CPU",
        "relay CPU",
        "replication lag"
      ],
      "preconditions": [
        "new upstream valid",
        "no replication cycle",
        "required WAL available"
      ],
      "completion": [
        "configuration persisted",
        "WAL receiver follows intended upstream",
        "streaming and lag verified"
      ],
      "evidence": [
        "direct-A-r1",
        "core-A-skew50-primary-r1"
      ],
      "finding": "Changing cascading versus direct replication redistributes CPU work among primary and replicas without necessarily improving application latency."
    },
    {
      "action": "set_commit_policy",
      "scope": "replication group",
      "signals": [
        "commit latency",
        "standby eligibility",
        "flush/apply lag"
      ],
      "preconditions": [
        "required acknowledgers available",
        "application durability requirement explicit"
      ],
      "completion": [
        "top-level engine configuration committed",
        "named standby state verified",
        "latency and availability effects observed"
      ],
      "evidence": [
        "sync-flush-A-r1",
        "sync-flush-A-r2",
        "sync-flush-A-r3"
      ],
      "finding": "Waiting for synchronous remote flush raises the observed tail. PostgreSQL acknowledgement semantics are not interchangeable with MongoDB majority write concern."
    },
    {
      "action": "change_shard_count",
      "scope": "logical dataset",
      "signals": [
        "distribution key",
        "shard sizes and traffic",
        "query fan-out",
        "co-location groups"
      ],
      "preconditions": [
        "supported schema and replica identity",
        "co-location invariants preserved",
        "migration resource budget"
      ],
      "completion": [
        "new mapping committed",
        "data coverage verified",
        "old state safely removed"
      ],
      "evidence": [
        "logical-shard-count-2-to-8.json",
        "eight-shards-two-workers-r1"
      ],
      "finding": "Logical shard count and physical worker count are independent. The tested shard-count change was quiescent, not an online splitting benchmark."
    },
    {
      "action": "register_worker_and_rebalance",
      "scope": "dataset and existing worker pool",
      "signals": [
        "actual placements",
        "migration progress",
        "network and I/O",
        "latency during copying"
      ],
      "preconditions": [
        "worker reachable",
        "correct version and authentication",
        "no conflicting migration",
        "existing-node capacity only"
      ],
      "completion": [
        "worker registered",
        "moves planned",
        "copy/catch-up finished",
        "metadata switched",
        "placements verified",
        "post-move service observed"
      ],
      "evidence": [
        "transition-add-worker-rebalance",
        "final-shard-placement.json"
      ],
      "finding": "An API acknowledgement or empty worker registration is not a completed scale-out. The initial failed call was retained and excluded; corrected placement verification is required."
    },
    {
      "action": "change_data_layout",
      "scope": "related tables",
      "signals": [
        "query plan",
        "local versus repartitioned joins",
        "per-table traffic",
        "cross-shard transaction cost"
      ],
      "preconditions": [
        "application semantics and keys known",
        "co-location requirements explicit"
      ],
      "completion": [
        "mapping verified",
        "plans inspected",
        "workload succeeds within its deadlines"
      ],
      "evidence": [
        "layout-together-J-r1",
        "layout-colocated-J-r1",
        "layout-separated-J-r1"
      ],
      "finding": "Table placement changes the execution graph. The separated layout overload case includes query timeouts and Service endpoint failures and is not a clean latency comparison."
    }
  ],
  "runtime_rules": [
    "Serialize conflicting actions per dataset or replication group.",
    "Enforce user constraints before acting; EC2 creation was prohibited in this campaign.",
    "Differentiate requested, ready, routed, effective and useful states.",
    "Use per-role and per-shard signals rather than a single average CPU metric.",
    "Include router and load-generator bottlenecks in the dependency graph.",
    "Keep query semantics and consistency policy explicit; do not silently weaken them.",
    "Bound operation time, retain failed-action receipts, and verify physical/logical state after completion.",
    "Treat readiness, error rate, drops and lag as feedback; readiness can fail under CPU overload without a process crash.",
    "Distinguish released container allocation from actual cloud billing savings.",
    "Apply the declared replication-factor policy to every new writable group before routing data. The third group in the actuation demonstration was explicitly unreplicated."
  ],
  "not_established": [
    "Generalization to production datasets, long-duration workloads, many tenants or multiple availability zones.",
    "Failover safety, disaster recovery and RPO/RTO.",
    "Automatic EC2 node scaling; forbidden and not performed.",
    "Storage volume shrink or billing savings.",
    "A validated predictive autoscaling model or a deployed shared autoscaler.",
    "A memory-scaling law or an I/O saturation curve.",
    "Hot-key isolation, range sharding or skewed-storage scenarios.",
    "Cross-database equivalence of MongoDB and PostgreSQL consistency semantics."
  ],
  "qualifications": {
    "stock_key_access": "Uniform keys forced for A-F; D is an operation-mix variant, not latest-key access.",
    "rmw": "Stock F read and update are not one atomic SQL transaction.",
    "third_worker": "Prestarted pod on an existing node; no standby for group c; transition timing excludes pod/VM startup.",
    "generator_diagnostic": "Two-process diagnostic has eight logical shards and one run per router allocation; single-process series has two shards and three repeats. Do not pool."
  }
}
