Skip to content

RFC: derived facts that retract themselves - #116

Open
bjacobso wants to merge 1 commit into
mainfrom
rfc/derived-facts
Open

bjacobso wants to merge 1 commit into
mainfrom
rfc/derived-facts

Conversation

@bjacobso

Copy link
Copy Markdown
Owner

Summary

Design proposal, for review: derived facts that retract themselves. Adds docs/rfcs/derived-facts.md only. No package source, exports or Changeset; nothing to release.

Triplex already covers most of a forward-chaining rules engine (facts as working memory, Datalog matching, Derivation candidates with provenance, reconcile, Materialization and the runner). Two pieces are missing, and hosts rebuild them in onDiff today:

  1. A derivation can't write its results back as ordinary facts that are retracted automatically when their support goes (Clara Rules' insert-logical).
  2. Chained derivations settle one runner batch per hop instead of within one commit.

Proposal: logical rules. A rule is an existing Derivation.Definition plus data-only emit templates. A runtime-wide layer, built like CommitValidation.layer, runs a new derivation stage inside each application transaction, after the command's operations and before enforce, the journal entry and commit validators. It evaluates the affected rules against the in-progress state and writes whatever makes each owned attribute match the current output:

  • Ownership per attribute. Only the stage writes an owned attribute, and application writes to one are rejected. Reads are ordinary.
  • Truth maintenance by re-evaluation. Desired facts are the union over owning rules. Missing ones are asserted, and unsupported ones are ended at the instant with Change.end, which keeps past valid time. Several supports give one fact.
  • Fixpoint, small form. Rule graphs must be acyclic. One topological pass is the fixpoint, so a chain settles in one commit and one journal entry.
  • Time. Temporal boundaries are applied by a maintenance commit at the exact valid instant, scheduled by the runner's existing timer.
  • Safety. The active rule set ID is checked in every transaction, and changing rules is an explicit rebuild. Derived writes go through enforce and validators, and a rejection rolls back the whole command.

Out of scope: incremental matching (Rete or differential), salience, accumulators, and recursion through derived facts. The RFC asks for a commit-latency benchmark, beside docs/datalog-performance.md, before the API is called supported. Section 9 lists open questions for the owner: naming, declaring ownership in the release, retroactive writes, where the maintainer lives, and cost budgets.

Verification

  • oxfmt --check docs/rfcs/derived-facts.md
  • vitepress build docs succeeds. The Markdown fence check reports nothing for this file; its only failures are pre-existing ones in files that need built packages.
  • pnpm check (not run; docs-only change)
  • pnpm pack:check (not applicable; no package change)

Architecture and release

  • The one-way package graph in ARCHITECTURE.md is preserved (the design keeps everything in core, browser-safe).
  • No dependency changes.
  • No export changes.
  • No Changeset: documentation only.

Proposes logical rules: derivations with data-only emit templates whose
facts are maintained inside the transaction that changes their support,
ended (not erased) when support goes, and settled across chained rules in
one stratified pass. Design only; no package changes.

Amp-Thread-ID: https://ampcode.com/threads/T-01a1fc4c-95ef-732d-b7e6-65a5567128de
Co-authored-by: Ben Jacobson <[email protected]>
@github-actions

Copy link
Copy Markdown
Contributor

Documentation preview: https://triplex-docs-pr-116.bjacobso.workers.dev

Commit: da4098a

This branch has not been deployed

No deployments
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.

2 participants