Technical document

Development Standard with CUE, Quint, Apalache, and Bazel in a Single Pipeline

We explain a development standard that defines contracts and constraints with CUE, state transitions and invariants during failures with Quint, inspects models with Apalache, and runs generation, verification, and testing as a single dependency graph with Bazel.
Developer Platform / Formal Verification~20 minPublication Date September 22, 2026Update date November 23, 2026
Illustration of a development pipeline connecting CUE, Quint, Apalache, and Bazel in a single dependency graph

Summary

Key points of this document

  • 1. Static contracts alone cannot prevent re-execution and failures.
  • 2. The four roles connect rather than replace each other.
  • 3. CUE: Data structures, configurations, contracts, constraints, policies

Overview

Define data structures, configurations, contracts, constraints, and policies with CUE; state transitions, concurrency, idempotence, and invariant conditions during failures with Quint; inspect models with Apalache; and run generation, verification, and testing as a single dependency graph with Bazel. This is our company's development standard.

1. Static contracts alone cannot prevent re-execution and failures.

Type generation with CUE as the source of truth is necessary to standardize the forms of APIs, configurations, permissions, and events. However, problems such as duplicate order IDs, payment responses disappearing midway, or confirmation and cancellation running simultaneously can occur even if the types are correct.

Tests provide concrete examples but are not suitable for enumerating combinations of concurrent processes or how failures might interject. Even if a comment says "make it idempotent," a path where a balance is incremented twice upon re-execution might remain.

Therefore, we separate static contracts and dynamic behavior into different sources of truth and put both through the same change validation pipeline.

The type generation of CUE and Bazel itself is covered in a separate document, "Robust and Fast Build Infrastructure with CUE as the Source of Truth for Type Generation Pipelines and Bazel." This paper discusses fixing state transitions and invariant conditions during failures, which are beyond that, as a single pipeline.

  • Static Source of Truth: Fields, enumerations, ranges, requiredness, permissions, allowed transition tables. CUE is responsible for this.
  • Dynamic Source of Truth: Initial state, actions, concurrent execution, retries, crashes, invariant conditions. Quint is responsible for this.
  • Inspection: Quint models are examined with Apalache, and if a counterexample is found, we revert to before implementation.
  • Execution: CUE validation, Quint inspection, code generation, and testing are run in Bazel's dependency graph.

2. The four roles connect rather than replace each other.

CUE, Quint, Apalache, and Bazel are not interchangeable tools. Their responsibilities are divided, and their connection order is fixed.

Trying to write all state machine paths in CUE would exceed the role of a constraint language. Trying to make Quint generate OpenAPI or types for various languages would exceed the role of a specification language. We do not make Apalache create containers, nor do we make Bazel define business invariant conditions.

The key to connection is sharing vocabulary and fixing the order of inspection. The enumeration of order states uses CUE as the source of truth, and the state set and names on the Quint side are matched. The allowed transition table is also placed in CUE, and it is made an invariant condition that Quint's actions do not violate that table. Generated products and implementations flow downstream from these two sources of truth.

  • CUE: Data structures, configurations, contracts, constraints, policies. Timeless correctness.
  • Quint: Correctness of execution, including state, actions, concurrency, idempotence, and failures.
  • Apalache: Translates Quint models into SMT constraints and checks invariants within a specified number of steps.
  • Bazel: Executes the above verification, generation, testing, and artifact creation based on inputs and dependencies.

3. CUE: Data structures, configurations, contracts, constraints, policies

CUE is the source of truth for timeless correctness. It gathers whether values hold true at a given moment, such as the format of an order ID, the range of an amount, required fields, permissions, and allowed transition tables. While CUE can define whether a state can directly transition from "draft" to "paid" as a transition table, it is not CUE's role to determine what happens to a balance after a double payment or re-execution after a crash.

Sharing vocabulary is a prerequisite for aligning subsequent Quint and generated artifacts to the same meaning. Enumeration names for states, units of currency, and order ID identifiers are authoritative in CUE, and the same names are passed to Quint's state sets and generated types. If an enumeration is added on the CUE side, the corresponding state and allowed transitions on the Quint side are verified as the same change.

cue vetfirst verifies whether specific values in configurations and events conform to the contract. It generates types and schemas from validated definitions, and aligns enumerations and transition tables referenced by the Quint model from the same input. If CUE were to describe all business paths, it would exceed the role of a constraint language, so behaviors involving time are passed to Quint.

  • Scope for source of truth: fields, enumerations, ranges, mandatory nature, permissions, allowed transition tables.
  • Vocabulary to pass to Quint: state names, identifiers, meaning of amounts. Ensure names match and that transition tables are not violated as invariants.
  • What not to put in CUE: Time-oriented paths including re-execution, concurrency, and crashes. These are handled by Quint.
// Example of defining order/payment contracts and allowed transitions in CUE
#OrderStatus: "draft" | "confirmed" | "cancelled" | "paid"

#Order: {
  id:       string
  status:   #OrderStatus
  total:    number & >=0
  currency: *"JPY" | "JPY" | "USD"
}

#Payment: {
  orderId: string
  amount:  number & >=0
  attempt: int & >=1
}

#allowedTransitions: {
  draft:     ["confirmed", "cancelled"]
  confirmed: ["paid", "cancelled"]
  paid:      []
  cancelled: []
}

order: #Order & {
  id:     "order-001"
  status: "confirmed"
  total:  1200
}

4. Quint: State transitions, concurrency, idempotence, invariants during failures

Quint is the source of truth for how transitions are allowed. It models initial states, actions, concurrent execution, retries, crashes, and invariants. Based on CUE's enumerations and transition tables, it models, rather than comments, rules such as: the balance does not increase if the same order ID is received again; confirmation and cancellation running simultaneously do not lead to an unauthorized state; and re-execution after a lost response does not result in double counting.

Tests demonstrate "this input leads to this outcome." Quint is a specification for exploring possible paths within a given number of steps. In a payment module, rules such as only unprocessed payments increasing the balance, re-sending processed IDs not changing the state, and payments being rejected if cancellation is confirmed first, are separated into actions and invariants.

The model is not a draft for implementation. Screen rendering, HTTP retry implementation, and physical ledger placement are not written in Quint. Only business-critical paths that must not break are retained, and vocabulary is kept consistent with CUE. If the model becomes too large, the scope of inspection is narrowed back down.

// Example of defining state transitions and invariants for a payment module in Quint
module payment {
  type Status = "draft" | "confirmed" | "paid" | "cancelled"

  var status: Status
  var balance: int
  var processed: Set[str]

  def allowed(from: Status, to: Status): bool = or {
    from == "draft" and to == "confirmed",
    from == "draft" and to == "cancelled",
    from == "confirmed" and to == "paid",
    from == "confirmed" and to == "cancelled",
  }

  action init = all {
    status' = "draft",
    balance' = 0,
    processed' = Set(),
  }

  action confirm = all {
    allowed(status, "confirmed"),
    status' = "confirmed",
    balance' = balance,
    processed' = processed,
  }

  action cancel = all {
    allowed(status, "cancelled"),
    status' = "cancelled",
    balance' = balance,
    processed' = processed,
  }

  action pay(id: str, amount: int): bool = all {
    allowed(status, "paid"),
    amount > 0,
    not(id.in(processed)),
    status' = "paid",
    balance' = balance + amount,
    processed' = processed.union(Set(id)),
  }

  action replay(id: str): bool = all {
    id.in(processed),
    status' = status,
    balance' = balance,
    processed' = processed,
  }

  val inv_balance_nonneg = balance >= 0
  val inv_paid_has_id = status != "paid" or processed.size() >= 1
  val inv_replay_keeps_balance = true
}

5. Apalache: Not an implementation substitute, but a model checking backend

Apalache translates Quint models into SMT constraints and checks invariants within a specified number of steps. It is not a replacement for implementation, but ratherquint verifya verification backend called from. If a counterexample is found, we revert to the model or contract before fixing the code.

quint runconfirms intuition through random execution, andquint verifyuses Apalache to explore the range of steps. Verification is not a proof of all paths. Depending on the number of steps, state space, and how invariants are placed, some paths may be missed. Do not expect Apalache to fully guarantee infinite execution or livelocks.

If too many items are checked, the time taken will increase and feedback will be delayed. Start with conditions that, if broken, would compromise monetary value or authority, such as payment idempotency, unauthorized transitions, or re-execution after failure. Even if the model passes, the implementation is not necessarily correct, so generation and testing remain separate gates.

  • Apalache checks Quint models. It is not responsible for implementation, containers, or type generation.
  • It checks invariants within a certain number of steps, and if a counterexample is found, it reverts to the model before implementation.
  • Instead of proving all paths, it first fixes conditions that must not be broken.
// quint run and quint verify
quint run models/payment.qnt \
  --invariant=inv_balance_nonneg \
  --invariant=inv_paid_has_id \
  --max-samples=200

quint verify models/payment.qnt \
  --invariant=inv_balance_nonneg \
  --invariant=inv_paid_has_id \
  --max-steps=12

6. Bazel: Running generation, verification, and testing as a single dependency graph

Bazel does not run CUE, Quint, Apalache, generators, and tests as separate procedures. From inputs and dependencies, it executes verification, generation, testing, and artifact creation as a single graph. If CUE changes,cue_vetand generation are re-executed; if the transition table changes, Quint's checks are re-executed; and if generated artifacts change, diff checks and tests follow.

Even if custom rule names differ by organization, their roles are fixed.cue_vetis for static contracts,quint_verifyfor dynamic models,genrulefor generating types and schemas,sh_testand for zero-diff generated artifacts and contract tests. Bazel is not used to write business invariants. Invariants are placed in Quint, and Bazel calls its checks as a dependent target.

If the graph is accurate, only changed contracts and models, and their reverse dependencies, can be executed. The same BUILD definition is shared between local and CI, and feedback is shortened through caching and parallel execution. Reproducibility is only achieved by fixing the versions of inputs, toolchain, models, and generators.

  • CUE: Contract and constraint validation.
  • Quint / Apalache: State transition and invariant checking.
  • genrule: Type, schema, and code generation.
  • sh_test: Generated diffs, contract tests, and conformance checks during failures.
// Example of Bazel target concept
cue_vet(
    name = "contracts_vet",
    srcs = ["//contracts:order.cue"],
)

quint_verify(
    name = "payment_verify",
    srcs = ["//models:payment.qnt"],
    invariants = [
        "inv_balance_nonneg",
        "inv_paid_has_id",
    ],
    max_steps = 12,
)

genrule(
    name = "generate_types",
    srcs = ["//contracts:order.cue"],
    outs = [
        "order.go",
        "order.ts",
    ],
    cmd = "$(location //tools:cuegen) $< $(OUTS)",
    tools = ["//tools:cuegen"],
)

sh_test(
    name = "generated_diff_zero",
    srcs = ["check_generated.sh"],
    data = [
        ":generate_types",
        "//generated:all",
    ],
)

7. Overall Pipeline

The entry point is the CUE contract. After validating enumerations, constraints, and allowed transitions, we place the Quint model using the same vocabulary. Apalache inspects that modelquint verify and, if a counterexample is found, it's returned before generation. Types and code are generated from the passed contract, and Bazel runs validation, generation, and testing as a dependency graph.

At the quality gate, we confirm that the generated artifacts have not been directly edited, that contract tests and idempotency conformance tests pass, and that the deliverables are created from the dependency graph. Implementation discrepancies found in operation are either confined to individual implementations or reverted to the canonical CUE or Quint. Without this rollback path, on-site exceptions would be scattered across comments and tests.

  • CUE: Defines what is allowed.
  • Quint: Defines how transitions may occur.
  • Apalache: Checks invariants within a specified number of steps.
  • Generation and Bazel: Creates derived artifacts from the canonical source and executes them via a dependency graph.
  • Gate: Confirms generated differences, contract conformance, and artifact validity.
Loading diagram...

8. Stage validation in CI

In CI, we don't immediately build all services. We stage static contracts, dynamic models, generation reproducibility, implementation conformance, and artifact validity. Failing early allows us to trace the cause back to CUE or Quint before reading container logs.

In the first stage, CUE is validated, and in the second stage, Quint's syntax is checked and a simulation is performedquint run. In the third stage, Apalache runs invariant checks, and if a counterexample is found, generation does not proceed. In the fourth stage, schemas and code are generated, and in the fifth stage, it's confirmed that there are no differences in the Git-managed generated artifacts.

From the sixth stage onwards, Bazel executes static analysis and differential tests. In the seventh stage, we test conformance conditions that cannot be guaranteed by types alone, such as idempotency, re-execution, and fault recovery, in addition to API input/output. Finally, binaries and containers are generated. Even in operations where generated artifacts are not included in Git, we verify that the same input produces the same output.

    1. CUE syntax, type, and constraint validation
    1. Quint model syntax check and simulation
    1. Invariant checking by Apalache
    1. Schema and code generation from CUE
    1. Zero-diff confirmation after regeneration
    1. Static analysis and diff testing with Bazel
    1. Conformance testing for contracts, idempotency, and fault recovery
    1. Binary and container generation
// Example to ensure generated artifacts are not directly edited
bazel test //contracts:contracts_vet //models:payment_verify
bazel run //contracts:generate_all
git diff --exit-code -- generated/ \
  || { echo "Please regenerate artifacts from CUE"; exit 1; }

bazel test //contracts/changed:all \
  --test_output=errors

9. Keep the scope of modeling narrow

Formal verification failures occur more often from over-scoping than from a lack of tools. If you write every screen transition, all HTTP headers, the physical layout of ledgers, and all enumeration values in Quint, the model becomes a copy of the implementation, inspection time increases, and it becomes unmaintainable. At our company, we only model paths where monetary values, permissions, or duplicate executions would be corrupted if they broke.

We target allowed transitions, idempotent retries, concurrent confirmations and cancellations, re-executions after response loss, and invariants immediately after crashes. We do not target UI appearance, log text, index selection methods, retry backoff implementations, or the specifics of generated types. These specifics are left to CUE and the implementation side.

A model is not "something that describes the current implementation," but rather "conditions that the implementation must not violate." When an implementation discrepancy is found, if it's a business exception, it's promoted to Quint; if it's specific to a particular service, it's confined to the implementation side. Returning everything to the model would bloat the canonical source.

  • To model: allowed transitions, idempotent retries, concurrent confirmations and cancellations, re-execution after failure, and invariants for balances and permissions.
  • Not to model: UI, communication details, persistence optimization, and format-specific aspects of generated types.
  • Promotion criteria: Only failures shared by multiple services are returned to the canonical source.

10. Compatibility with AI Agent Development

In environments where AI agents increase implementation, it's more important to converge changes to the same contracts and verification rules than to focus on the quantity generated. Instead of letting AI freely design state machines or retries, we make it adhere to CUE's vocabulary, Quint's actions, and Bazel's allowed dependency directions.

Code written by AIcue_vet, passesquint verify zero-diff regeneration, static analysis, contract testing, and idempotency conformance testing. If generated artifacts are directly modified, it is detected by zero-diff inspection. In case of failure, labels indicate which CUE contract, Quint invariant, or Bazel target was broken, and the scope for retry is specified.

Even when parallel agents modify different services, they are converged to common static and dynamic canonical sources. Instead of adding exceptions to each implementation, contract changes go to CUE, behavior changes go to Quint, and implementation-specific details go back to the service boundary. Constraining freedom is not to slow down, but to reduce the scope for humans to later verify consistency.

  • Scope for AI: Implementation, testing, and localized fixes according to contracts and models.
  • Scope for human judgment: Meaning of transitions, compatibility, authorization, and whether to promote exceptions to the source of truth.
  • Mechanical gates: CUE vet, Quint verify, regeneration diffs, dependency direction, contract and conformance tests.

11. Our Company's Adoption Policy

Our company will use CUE as the source of truth for data structures, configurations, contracts, constraints, and policies. The authorized transition table will also be placed in CUE, and Quint's state sets and names will be matched. Protocol Buffers, OpenAPI, JSON Schema, and language-specific types are derived artifacts from CUE.

Quint will be the source of truth for state transitions, concurrency, idempotence, and invariants in the event of failure. Apalache will be used asquint verify a backend and not for implementation or artifact generation. Inspection efforts and invariants are fixed based on conditions that must not be broken, such as monetary amounts, permissions, and duplicate executions.

Bazel targets CUE validation, Quint inspection, code generation, conformance testing, and binary and container generation, consolidating them into a dependency graph that can execute only the changed scope. As a rule, generated artifacts are not manually edited. Changes are made from CUE or Quint, and generation diffs and inspection results are confirmed in CI.

This policy also supports future parallel development by AI agents. Even if multiple agents implement simultaneously, changes will converge to two sources of truth—static contracts and dynamic behavior—and Bazel's build rules.

  • CUE: Structure, contracts, constraints, policies, authorized transition tables.
  • Quint: State transitions, concurrency, retries, crashes, invariants.
  • Apalache: Inspection of Quint models. Not a substitute for implementation.
  • Bazel: Executes validation, generation, testing, and artifact creation in a single dependency graph.

12. Summary: Aligning Static Contracts and Dynamic Behavior into a Single Path

Even with consistent types and configurations, behaviors that break due to re-execution, concurrency, or mid-process failures remain. Defining what is allowed with CUE, how to transition with Quint, inspecting that model with Apalache, and running generation, validation, and testing as a single dependency graph with Bazel—this is our development standard.

The four tools are not interchangeable. CUE does not write all paths, Quint does not generate types, Apalache does not create containers, and Bazel does not define business invariants. They share vocabulary, fix the order of inspection, and flow generated artifacts and implementations downstream.

Idempotence and invariant conditions in the event of failure are not left solely to comments or post-hoc tests. The scope of modeling is kept narrow, and inspection is performed from paths where monetary values or permissions would collapse if broken. Declarations are made the authoritative source, inspections are placed on the dependency graph, and changes are delivered to merge through the same path. If this distinction can be maintained, the combination of CUE, Quint, Apalache, and Bazel is not merely a tool selection, but a design that fixes static contracts and dynamic behavior into a single development standard.

Technical documents

We will continue to increase technical documentation.

We will continue to organize our thoughts on AI, cloud, business app development, requirements definition, and operational design into documents.

Go to technical documentation list