$ fssg build --deterministic --validate |

FACET Code Generation: Turn AI Tool Contracts into Typed SDK Boundaries

FACET

An AI tool contract is incomplete if the runtime validates one signature while the host application calls another.

That split is common. A schema is written for the model, a client wrapper is written for the application, and both evolve independently. The names still look familiar, so review passes. Then one side changes amount: number to amount: string, the other does not, and the mismatch appears only after the agent has selected the tool.

FACET already contains a smaller, more useful answer than another runtime validator: generate host-language boundaries directly from @interface declarations.

@interface Payments
  fn charge(
    amount: number,
    currency: string
  ) -> struct {
    transaction_id: string
    status: string
  }

The current facet-fct codegen command reads interfaces from the parsed FACET document and can emit SDK skeletons for TypeScript, Python, and Rust. I see this as more than convenience. It turns the contract into a source for both sides of the call.

One declaration, two consumers

The model-facing side needs a tool definition with a stable name, parameters, and return shape. The host-facing side needs types and a method boundary that application code can implement. These are different representations of the same agreement.

                    @interface
                        |
             +----------+----------+
             |                     |
      canonical tool schema   host SDK boundary
             |                     |
          provider              application

Without generation, the branch on the right is usually handwritten. That creates a shadow interface: structurally similar, operationally separate, and easy to forget during a contract change.

Code generation removes that second source of truth. A FACET interface becomes an input to compilation rather than documentation copied into another repository.

This extends the idea behind typed runtime inputs. Runtime inputs define how values enter a contract. Generated SDK boundaries define how declared operations leave it. Together they narrow the host/compiler seam in both directions.

Generate a boundary, not an implementation

A generated client should not invent transport, credentials, retries, authorization, or business semantics. Those belong to the host.

The reference compiler follows that boundary: it extracts interface and method names, parameter types, and return types, then produces language-specific types and method skeletons. The generated methods are integration points, not hidden provider logic.

That is the correct default. If a generator silently chooses an HTTP route, retry policy, or authentication scheme, it stops translating a contract and starts designing the application. The same rule applies to provider adapters: translation may change representation, but it must not repair or invent semantics.

A useful generation pipeline therefore has three explicit stages:

  1. parse the FACET document;
  2. project @interface nodes into a language-neutral interface model;
  3. render that model into target-language types and stubs.

The middle representation matters. It is the point where a generator can say, “this FACET type has no lossless target-language form,” instead of quietly widening it.

Type loss must be visible

Cross-language generation is never a purely syntactic exercise. A FACET union, map, media type, or structured return value may not map identically into TypeScript, Python, and Rust.

The dangerous response is permissive widening:

precise FACET union -> any
structured value    -> object

The output compiles, but the contract becomes weaker exactly where the host expects type safety.

I would treat every lossy mapping as a first-class compiler result. A generator should either preserve the type, reject the target, or emit an explicit diagnostic and machine-readable generation report. Silence is not compatibility.

A report could be as small as:

{
  "target": "typescript",
  "interface": "Payments",
  "lossless": true,
  "generated_files": ["payments.types.ts", "payments.ts"],
  "source_document_hash": "sha256:..."
}

That hash is important. Generated code should identify the contract revision that produced it. Then CI can compare the embedded or adjacent source hash with the current FACET document and fail when the SDK is stale.

This is where canonical JSON as an intermediate representation becomes practical: stable identity gives generated artifacts an address, not merely a timestamp.

The CI gate is the real feature

The most valuable use of code generation is not running it once. It is making drift impossible to merge unnoticed.

facet-fct build --input payments.facet
facet-fct codegen \
  --input payments.facet \
  --output generated/payments \
  --language typescript

git diff --exit-code -- generated/payments

This gate asks a precise question: does the checked-in host boundary still correspond to the current AI contract?

A stronger pipeline can compile the generated target, run contract tests, and compare a generation manifest. FACET’s @test blocks can verify deterministic contract behavior; target-language tests can verify that the host implementation satisfies the generated shape. Neither substitutes for the other.

The separation is useful:

Layer What it proves
FACET build the source contract parses and passes semantic checks
FACET tests deterministic inputs, mocks, policy, and assertions behave as declared
Code generation the host boundary was derived from the declared interfaces
Target compile/tests the application implements that boundary correctly

None of these steps proves that a model will produce a correct answer or that an external API will succeed. They remove a narrower failure class: interface drift between the compiled AI request and the host code.

Contract changes should look like API changes

Once interfaces generate host artifacts, changing a FACET signature becomes an API change with visible consequences. A renamed parameter changes generated code. A narrowed return type breaks an implementation. A removed method creates a diff that reviewers can inspect.

That is healthy friction. AI architecture should not bypass the compatibility discipline that ordinary software already learned.

I do not want tool schemas to live as model-only metadata. I want them to participate in the same build graph as the code that executes them. The compiler should not merely tell the model what may be called. It should also give the host a typed shape it must implement—and a deterministic way to prove that both sides still agree.

Sources

FACET AI CLI Terminal
facet@ai:~$