Surface / foldkit-surface

Modules

A Module collects an application's contracts as pure data so tools can inspect ownership and catch conflicts before starting a runtime. Sync, Remote, and Agent contracts carry a contract property; describe a Surface with Surface.contract(surface, params).

ts
import { Module } from 'foldkit-surface'

const Project = Module.make(App, [TodoDetail]) // and any Sync, Remote or Agent contracts

Module.validate(Project) // [] or findings
Module.manifest(Project) // fields, Messages, who owns each Model path, contracts
Module.toMarkdown(Project)
Module.toMermaid(Project)

For example, Module.manifest might list route and selectedNoteId as LOCAL, notes as SYNC notes, and remote as REMOTE remote. Module.validate catches cross-app contracts, duplicate kind:name entries, overlapping Model path owners, Messages claimed by two replication contracts, and undeclared paths or Messages. Feature modules can extend a Module with Module.add(module, ...more).

Laws in the graph

Contracts can declare laws and transitions that might break them in Contract.laws (laws.entities for Entity invariants and writes, laws.model for Sync document laws and durable:<tag> transitions, plus closure:<name> for closed Surfaces). Graph.of(module, { evidence }) builds the dependency graph between transitions that write paths, laws that read paths, and Evidence records (Enumerated, Walked, or Evidence.runtimeOnly) that attest a transition preserves a law.

Graph is split from Module so bundle readers that only render Module.manifest omit graph validation code. Run both Module.validate and Graph.validate in tests since each checks a separate rule set:

ts
import { Schema } from 'effect'
import { defineMessageUnion } from 'foldkit/message'
import { Evidence, Graph, Module, Surface } from 'foldkit-surface'

const App = Surface.application({
  Model: Schema.Struct({ todos: Schema.Array(Schema.String) }),
  Message: defineMessageUnion({ Added: { title: Schema.String } }),
})

// What `Sync.forApplication(App).make` declares for a document with one law.
const todos = {
  kind: 'sync',
  name: 'todos',
  owner: App.owner,
  owns: [['todos']],
  observes: [['todos']],
  messages: ['Added'],
  metadata: [],
  laws: { model: ['capped'] },
}
const Todos = Module.make(App, [todos])

Module.validate(Todos) // []: one owner per path, every name declared
Graph.validate(Todos)
// [{ rule: 'law-unwitnessed', message: 'durable:Added writes todos, which sync:todos.capped reads, …' }]

const judged = Evidence.runtimeOnly({
  transition: 'durable:Added',
  law: 'sync:todos.capped',
  why: 'the journal judges every append, and refuses one past the cap',
})
Graph.validate(Todos, { evidence: [judged] }) // []
Graph.breakersOf(Graph.of(Todos, { evidence: [judged] }), 'sync:todos.capped')
// [{ transition: 'durable:Added', paths: [['todos']], evidence: [judged] }]

Graph validation rules:

RuleReported when
law-unwitnessedA transition writes a path a law reads with no matching evidence.
evidence-unmatchedEvidence was constructed manually, or names an unknown or unrelated pair.
level-declared-twiceTwo closed Surfaces observe the same paths and list the same Messages.
transition-declared-twiceTwo contracts declare a write of the same name (such as write:Sell).

For law-unwitnessed, a law that reads before counts as reading the entire row because any row write can affect it. Graph.toMarkdown and Graph.toMermaid render the law table and flowchart (see LAWS.md in examples/writes). Local modifyFields writes are checked by walks rather than static graph edges.