Skip to content

feat(ontology): classes and properties over a Domain model - #3873

Draft
sam-goodwin wants to merge 18 commits into
mainfrom
t3/ossie-effect-schema-package
Draft

sam-goodwin wants to merge 18 commits into
mainfrom
t3/ossie-effect-schema-package

Conversation

@sam-goodwin

@sam-goodwin sam-goodwin commented Nov 29, 2026 •

Copy link
Copy Markdown
Member

@alchemy.run/ontology: a @alchemy.run/domain model says what happens; its ontology says what things mean. You define the classes and properties once, over the aggregates and events you already have, and the tables, views, guarded reads and agent tools come from that definition.

// the Domain model you already have
class AccountOpened extends Domain.Event("AccountOpened", { customerId: CustomerId }) {}
class MoneyMoved extends Domain.Event("MoneyMoved", { amount: Cents, balanceAfter: Cents }) {}
class Account extends Domain.Aggregate("Account", { id: AccountId, /* … */ }) {}
class Customer extends Domain.Aggregate("Customer", { id: CustomerId, /* … */ }) {}

An aggregate is a class and its id is the individual. A property goes from an aggregate to another aggregate or to a Schema, and names the events that set it.

import * as Ontology from "@alchemy.run/ontology";
import * as Facts from "@alchemy.run/ontology/Facts";

export class HeldBy extends Ontology.Property("HeldBy", Account, Customer).pipe(
  Ontology.functional,
  Ontology.on(AccountOpened, (event) => event.customerId),
  Ontology.on(AccountClosed, () => undefined), // takes it back
) {}
export class Balance extends Ontology.Property("Balance", Account, Cents).pipe(
  Ontology.functional,
  Ontology.on(MoneyMoved, (event) => event.balanceAfter),
) {}

// what only a view can add up is read from the view's state
export class TotalOutflow extends Ontology.Property("TotalOutflow", Account, Cents).pipe(
  Ontology.functional,
  Facts.readFrom(Outflow, (state) => state.total),
) {}

A class is defined by an expression over those, across aggregate boundaries:

export class Overdrawn extends Ontology.Class("Overdrawn").pipe(
  Ontology.equivalentTo(
    Ontology.and(Account, Ontology.some(Balance, Cents.check(Schema.isLessThan(0)))),
  ),
) {}

// a customer holding three accounts: no aggregate or view knows this on its own
export class PremiumCustomer extends Ontology.Class("PremiumCustomer").pipe(
  Ontology.equivalentTo(
    Ontology.and(Customer, Ontology.min(Ontology.inverse(HeldBy), 3, Account)),
  ),
) {}

Ontology.some(Balance, Customer); // type error: Balance ranges over cents

Filled by the model's events

Each term is a class, like the aggregates and events it is defined over. A Facts.Projection is an ordinary Domain.Projection, listed in the model like any other. It writes each property as its events arrive, and a new one is backfilled from history.

export class BankOntology extends Ontology.Graph("Bank", {
  classes: [Overdrawn, PremiumCustomer],
  properties: [HeldBy, Balance, TotalOutflow],
}) {}

export class AccountFacts extends Facts.Projection("AccountFacts", {
  from: Account,
  properties: [HeldBy, Balance],
}) {}

export class Bank extends Domain.Model("Bank", {
  aggregates: [Customer, Account],
  views: [Outflow],
  projections: [AccountFacts],
}) {}

export const BankLive = Bank.layer(Facts.toLayer(AccountFacts, BankOntology)).pipe(
  Layer.provideMerge(Layer.mergeAll(InMemory, Ontology.MemoryStore(BankOntology))),
);
const receipt = yield* accounts.send(new MoveMoney({ accountId, amount: -20 }));
yield* AccountFacts.caughtUp(receipt);

const store = yield* BankOntology.Store;
yield* store.members(Overdrawn); // [accountId]
yield* store.violations;         // what breaks a subClassOf restriction, disjointWith, functional…

Expressions are evaluated closed-world, so not, only, max and counts have answers.

In a SQL database

Swap the store Layer and the same projection fills tables: one per aggregate, a column per functional property, a pair table per many-valued one, and a view per defined class. Postgres and SQLite, on any SqlClient.

import * as OntologySql from "@alchemy.run/ontology/Sql";
import * as SQL from "@alchemy.run/sql/Postgres";

const BankStore = OntologySql.Store(BankOntology).pipe(
  Layer.provide(SQL.PostgresLayer({ url: hyperdrive.connectionString })),
);
-- made on first use, or from OntologySql.ddl(BankOntology, "postgres") in a migration
CREATE TABLE "account" ("id" TEXT NOT NULL, "balance" BIGINT, "held_by" TEXT, …, PRIMARY KEY ("id"));
CREATE VIEW "overdrawn" AS SELECT …;

-- anything that speaks SQL can use them, knowing nothing of the ontology
SELECT a.id, a.balance FROM account a JOIN overdrawn o ON o.id = a.id;
OntologySql.select(BankOntology, PremiumCustomer, "sqlite"); // the query for any expression

store.members, has, values, subjects and violations compile to queries, chains and transitive properties included. A property added later is a column added on first use. A schema check SQL has no words for fails the read and names the check.

In a lake

An Iceberg table written by a stream is only appended to, so the lake is one table with a row per statement, and the same tables and class views are views over it. Lake.toLayer is the same projection with a different Layer.

import * as Lake from "@alchemy.run/ontology/Lake";

export class AccountHistory extends Facts.Projection("AccountHistory", {
  from: Account,
  properties: [HeldBy, Balance],
}) {}

export const AccountHistoryLive = Lake.toLayer(AccountHistory, BankOntology); // needs a Lake.Writer
-- facts: term     subject  op      integer  seq  at
--        Balance  a-1      assert  -20      3    …
--        Balance  a-1      assert   30      4    …
SELECT id, balance, held_by FROM account; -- a view: the values as they stand
SELECT id FROM overdrawn;                 -- the same class view

A property added later is new rows and a new view, never a change to the table. Lake.SqlWriter and Lake.Store keep the layout in Postgres or SQLite.

On Cloudflare the writer is a Pipelines stream, and the table is an Iceberg table of an R2 Data Catalog:

import * as OntologyCloudflare from "@alchemy.run/ontology/Cloudflare";

export const Statements = Cloudflare.Basin.Stream("Statements", { schema: Lake.Row });

export const AccountHistoryLive = Lake.toLayer(AccountHistory, BankOntology).pipe(
  Layer.provide(OntologyCloudflare.Writer(Statements)),
  Layer.provide(Cloudflare.Basin.WriteStreamBinding),
);
// in the stack: the table the stream fills
const catalog = yield* Cloudflare.Basin.Catalog("Catalog", { bucket });
const table = yield* Cloudflare.Basin.Sink("Facts", {
  type: "basin_catalog",
  catalog,
  table: { namespace: "bank", name: "facts" },
  token,
});
yield* Cloudflare.Basin.Pipeline("FactsPipeline", {
  sql: Output.interpolate`INSERT INTO ${table.name} SELECT * FROM ${statements.name}`,
});

On AWS the writer puts rows straight into an Iceberg table of an S3 table bucket (PutRows, from #3905, which this branch includes until it merges):

import * as OntologyAWS from "@alchemy.run/ontology/AWS";

const facts = yield* AWS.S3Tables.Table("Facts", {
  tableBucket: bucket.tableBucketArn,
  namespace: "bank",
  name: "facts",
  schema: { fields: OntologyAWS.columns },
});

export const AccountHistoryLive = Lake.toLayer(AccountHistory, BankOntology).pipe(
  Layer.provide(OntologyAWS.Writer(facts)),
  Layer.provide(AWS.S3Tables.PutRowsHttp),
);

Reads on behalf of a caller

A rule is a class expression, applied inside the query. A kind with no rule is read by nobody.

export const AccountReadable = Ontology.readable(
  Account,
  Effect.gen(function* () {
    const actor = yield* CurrentActor;
    return actor._tag === "Customer" ? Ontology.value(HeldBy, actor.customerId) : Account;
  }),
);

yield* Ontology.members(Overdrawn, BankOntology);        // only the caller's
yield* Ontology.has(Overdrawn, accountId, BankOntology); // NotReadable for someone else's

Mentioned by an AI.Process

import { mention } from "@alchemy.run/ontology/Mention";

`Find the customer's overdrawn accounts with ${mention(BankOntology, Overdrawn, HeldBy)}.`
// tools: Bank_Overdrawn_members, Bank_Overdrawn_has, Bank_HeldBy_values, Bank_HeldBy_subjects

Each tool's description carries the term's definition, and each call reads as whoever the session was started as.

As text

The expressions are OWL 2's, so the definition prints in its notations.

Ontology.describe(Overdrawn);
// Class: Overdrawn
//     EquivalentTo: Account and (Balance some integer[< 0])

Ontology.toFunctional(BankOntology);
Ontology.toTurtle(BankOntology);

@alchemy-version-bot

alchemy-version-bot Bot commented Nov 29, 2026 •

Copy link
Copy Markdown
Contributor

Install the packages built from this commit:

alchemy

pnpm install https://pkg.alchemy.run/alchemy/pr:3873:ef405fb
@alchemy.run (37)
pnpm install https://pkg.alchemy.run/@alchemy.run/better-auth/pr:3873:ef405fb
pnpm install https://pkg.alchemy.run/@alchemy.run/git/pr:3873:ef405fb
pnpm install https://pkg.alchemy.run/@alchemy.run/drizzle/pr:3873:ef405fb
pnpm install https://pkg.alchemy.run/@alchemy.run/acme/pr:3873:ef405fb
pnpm install https://pkg.alchemy.run/@alchemy.run/archil/pr:3873:ef405fb
pnpm install https://pkg.alchemy.run/@alchemy.run/boat/pr:3873:ef405fb
pnpm install https://pkg.alchemy.run/@alchemy.run/axiom/pr:3873:ef405fb
pnpm install https://pkg.alchemy.run/@alchemy.run/doppler/pr:3873:ef405fb
pnpm install https://pkg.alchemy.run/@alchemy.run/infisical/pr:3873:ef405fb
pnpm install https://pkg.alchemy.run/@alchemy.run/1password/pr:3873:ef405fb
pnpm install https://pkg.alchemy.run/@alchemy.run/turso/pr:3873:ef405fb
pnpm install https://pkg.alchemy.run/@alchemy.run/convex/pr:3873:ef405fb
pnpm install https://pkg.alchemy.run/@alchemy.run/e2b/pr:3873:ef405fb
pnpm install https://pkg.alchemy.run/@alchemy.run/daytona/pr:3873:ef405fb
pnpm install https://pkg.alchemy.run/@alchemy.run/modal/pr:3873:ef405fb
pnpm install https://pkg.alchemy.run/@alchemy.run/planetscale/pr:3873:ef405fb
pnpm install https://pkg.alchemy.run/@alchemy.run/neon/pr:3873:ef405fb
pnpm install https://pkg.alchemy.run/@alchemy.run/prisma/pr:3873:ef405fb
pnpm install https://pkg.alchemy.run/@alchemy.run/hetzner/pr:3873:ef405fb
pnpm install https://pkg.alchemy.run/@alchemy.run/digitalocean/pr:3873:ef405fb
pnpm install https://pkg.alchemy.run/@alchemy.run/railway/pr:3873:ef405fb
pnpm install https://pkg.alchemy.run/@alchemy.run/fly/pr:3873:ef405fb
pnpm install https://pkg.alchemy.run/@alchemy.run/github/pr:3873:ef405fb
pnpm install https://pkg.alchemy.run/@alchemy.run/stripe/pr:3873:ef405fb
pnpm install https://pkg.alchemy.run/@alchemy.run/cloudflare/pr:3873:ef405fb
pnpm install https://pkg.alchemy.run/@alchemy.run/gcp/pr:3873:ef405fb
pnpm install https://pkg.alchemy.run/@alchemy.run/aws/pr:3873:ef405fb
pnpm install https://pkg.alchemy.run/@alchemy.run/domain/pr:3873:ef405fb
pnpm install https://pkg.alchemy.run/@alchemy.run/ontology/pr:3873:ef405fb
pnpm install https://pkg.alchemy.run/@alchemy.run/kubernetes/pr:3873:ef405fb
pnpm install https://pkg.alchemy.run/@alchemy.run/docker/pr:3873:ef405fb
pnpm install https://pkg.alchemy.run/@alchemy.run/redis/pr:3873:ef405fb
pnpm install https://pkg.alchemy.run/@alchemy.run/sql/pr:3873:ef405fb
pnpm install https://pkg.alchemy.run/@alchemy.run/cloudflare-runtime/pr:3873:ef405fb
pnpm install https://pkg.alchemy.run/@alchemy.run/frontend-frameworks/pr:3873:ef405fb
pnpm install https://pkg.alchemy.run/@alchemy.run/node-utils/pr:3873:ef405fb
pnpm install https://pkg.alchemy.run/@alchemy.run/pkg/pr:3873:ef405fb
Transitive Dependencies (30)
pnpm install https://pkg.alchemy.run/@distilled.cloud/aws/pr:3873:ef405fb
pnpm install https://pkg.alchemy.run/@distilled.cloud/acme/pr:3873:ef405fb
pnpm install https://pkg.alchemy.run/@distilled.cloud/archil/pr:3873:ef405fb
pnpm install https://pkg.alchemy.run/@distilled.cloud/core/pr:3873:ef405fb
pnpm install https://pkg.alchemy.run/@distilled.cloud/boat-dev/pr:3873:ef405fb
pnpm install https://pkg.alchemy.run/@distilled.cloud/axiom/pr:3873:ef405fb
pnpm install https://pkg.alchemy.run/@distilled.cloud/doppler/pr:3873:ef405fb
pnpm install https://pkg.alchemy.run/@distilled.cloud/infisical/pr:3873:ef405fb
pnpm install https://pkg.alchemy.run/@distilled.cloud/turso/pr:3873:ef405fb
pnpm install https://pkg.alchemy.run/@distilled.cloud/convex/pr:3873:ef405fb
pnpm install https://pkg.alchemy.run/@distilled.cloud/e2b/pr:3873:ef405fb
pnpm install https://pkg.alchemy.run/@distilled.cloud/daytona/pr:3873:ef405fb
pnpm install https://pkg.alchemy.run/@distilled.cloud/modal/pr:3873:ef405fb
pnpm install https://pkg.alchemy.run/@distilled.cloud/planetscale/pr:3873:ef405fb
pnpm install https://pkg.alchemy.run/@distilled.cloud/neon/pr:3873:ef405fb
pnpm install https://pkg.alchemy.run/@distilled.cloud/s3/pr:3873:ef405fb
pnpm install https://pkg.alchemy.run/@distilled.cloud/prisma/pr:3873:ef405fb
pnpm install https://pkg.alchemy.run/@distilled.cloud/hetzner/pr:3873:ef405fb
pnpm install https://pkg.alchemy.run/@distilled.cloud/digitalocean/pr:3873:ef405fb
pnpm install https://pkg.alchemy.run/@distilled.cloud/railway/pr:3873:ef405fb
pnpm install https://pkg.alchemy.run/@distilled.cloud/fly-io/pr:3873:ef405fb
pnpm install https://pkg.alchemy.run/@distilled.cloud/github/pr:3873:ef405fb
pnpm install https://pkg.alchemy.run/@distilled.cloud/stripe/pr:3873:ef405fb
pnpm install https://pkg.alchemy.run/@distilled.cloud/aws4signer/pr:3873:ef405fb
pnpm install https://pkg.alchemy.run/@distilled.cloud/cloudflare/pr:3873:ef405fb
pnpm install https://pkg.alchemy.run/@distilled.cloud/iceberg/pr:3873:ef405fb
pnpm install https://pkg.alchemy.run/@distilled.cloud/gcp/pr:3873:ef405fb
pnpm install https://pkg.alchemy.run/@alchemy.run/effect-cluster/pr:3873:ef405fb
pnpm install https://pkg.alchemy.run/@alchemy.run/rivet/pr:3873:ef405fb
pnpm install https://pkg.alchemy.run/@distilled.cloud/rivet/pr:3873:ef405fb

Published Dec 1, 2026, 6:16 AM UTC. Expires Dec 8, 2026, 6:16 AM UTC, extended while this pull request is open.

@sam-goodwin sam-goodwin changed the title feat(ontology): OWL-level modeling on Effect Schema feat(ontology): classes and properties over a Domain model Dec 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant