README.md
Monocle
Monocle is what a site needs in order to host somebody else's application behind tollbooths and pay its author: the record of each deployment and its terms, the declaration of a toll, the seam a payment provider plugs into, the books, and the manifest an application declares its tolls in.
It is a small library in plain Common Lisp (ASDF, yason,
bordeaux-threads). It serves no page, compiles no application and
moves no money; the site that uses it does those, and calls Monocle to
keep the terms and the accounts straight.
The idea
An author's application says what it charges for. The house that hosts it takes the payment, keeps its fee, and owes the author the rest. The application carries no payment code.
- A toll is a plist:
(:key :drawing :label "SVG drawing" :mites 200), with:uses n(the payment is good for n uses) or:seconds n(for that long) when wanted. Its price is in the house's unit: a house's own currency, which visitors buy in bundles and which turns into money only when an author is paid --:miteson common-lisp.net,:rivetson Genworks, each a currency of its own at its own rate -- or:centsfor a house that prices in money. The price goes by the unit's name::mites 200,:rivets 200,:cents 200. - A deployment is a directory,
<root>/<runtime>/<name>/, holdingdeployment.json: the name, the owner, where the owner's share is paid, and the terms as they were the day it was deployed (the fee does not change under an author afterwards). - The books are one file,
<root>/revenue.jsonl, a line appended per payment: the gross, what the payment cost to take by card (off the top), the house's fee, the author's share, and the runtime it ran on. Fees are exact on every line and rounded only where a sum is paid or reported. - A house is whoever hosts: its root directory, its runtime's name,
its unit, its fee table, its payment provider and, if it keeps one,
its community pot. Every function takes the house it works for, so
two houses can share one image. A house may carry a name of its
own for its payment gate (
:name; otherwise its runtime at its root names it), so that every runtime serving its deployments -- the house's own image and an application hosted in a machine of its own -- reports under one name; and a runtime that serves a deployment elsewhere says:books? nil: it grants a toll on the gate's settled word and books nothing, and the house's own sweep (settle-unbooked!) books what the gate settled. - The fee is the house's share of every payment: its hosting charge.
A house with a community pot may let authors add to it: an author
chooses how many more points of every payment go to the pot (a slider
from the fee upwards), frozen into the deployment's record like the
fee, and booked apart from it. An author may also give some of what
they are owed to the pot at any time (
contribute!). - An author's share is held in the house's unit until it is paid out;
payout-amountvalues it at that day's rate, less any spread.
Two ways to run an application
Monocle does not run anything, so it does not mind how a deployment runs. Two hosts use it today or are meant to:
- In the host's own image. The application is Lisp source compiled
into the image that serves it, and it places its own tollbooths on
its page. The Gendl prompt lab
(genworks/demos,
prompt-lab/) works this way. - As a program of its own, in a container behind the house's gate.
The application declares its tolls and the paths they stand on in a
manifest,
monetize.sexp, and the gate answers402 Payment Requiredon those paths until the toll is paid. PROFILE.md is the contract such an application meets;source/manifest.lispreads and checks the manifest.
Using it
(asdf:load-system :monocle)
(defparameter *house*
(monocle:make-house :root "/var/lib/my-site/deployed/"
:runtime "sbcl"
:unit :mites
:fee-percents '(:open 25)
:pot-percent 0 ; a community pot, nothing added unless the author does
:provider :test))
;; a deployment and its terms
(monocle:save-record!
*house*
(monocle:make-record *house* :name "plate-sizer" :title "Plate sizer"
:owner "an-owner-key" :payee "author@example.org"
:pot-percent 10)) ; the author adds 10 points for the pot
;; a toll taken (the :test provider grants it and books a test line)
(monocle:charge-toll! *house* "plate-sizer"
'(:key :drawing :label "SVG drawing" :mites 200)
:reference "visit-42")
;; a real payment settled elsewhere, booked here
(monocle:book-revenue! *house* "plate-sizer" 1000 :reference "jar-123")
(monocle:earnings *house* "plate-sizer") ; by quarter, for the author
(monocle:payables *house*) ; what each author is owed
(monocle:contribute! *house* "plate-sizer" 50) ; a gift to the pot
(monocle:payout-amount (monocle:held *house* "plate-sizer") 1 :spread 1/50)
(monocle:revenue-report *house* :year 2026 :quarter 4) ; by runtime
A payment provider
monocle:take-toll is a generic function of the house's provider. The
:test method grants every toll and books it marked as a test, so the
whole flow can be exercised with no money moving; the reports leave
test lines out. A real provider is a method that takes the money and
then books the line:
(defmethod monocle:take-toll ((provider (eql :my-gateway)) house name toll reference)
(let ((payment (debit-somehow reference (monocle:toll-price toll))))
(when payment
(monocle:book-revenue! house name (monocle:toll-price toll)
:toll (string-downcase (getf toll :key))
:card (payment-cost payment) ; 0 when the unit was bought in bundles
:reference (payment-id payment))
t)))
A toll is granted on a settled payment, never on a browser's word, and its price is read from the application's declaration, never from the request.
A toll paid through a gate: the :stripe provider
A provider that takes money elsewhere has two halves, since a card
payment is not settled in the request that asks for it. The house is
given a GATE (make-house :gate), a function of a door name and a
plist of string keys that asks the house's payment gate at that door
and answers a hash table and the HTTP status -- for the prompt lab,
the cyclops :llm-gate rule's Connect doors, which hold the keys --
and :cents-per-unit, what one of the house's unit costs a payer at a
toll (a rivet is a cent).
(monocle:open-toll :stripe house name toll reference :return-url :cancel-url)asks the gate'spaydoor for a payment of the toll's cents -- a destination charge on the platform, the author's account (the record'sconnect_account) the destination, the house's share the record's fee plus its pot percent -- and answers the address to send the payer to and the payment's id.(monocle:settle-toll! house name payment)asks the gate'spaymentdoor; when the gate says the payment settled (its word comes from the processor's webhook, never the browser) the line is booked once -- the gross in the house's unit, the card cost off the top as the charge's balance transaction reports it, the fee and the author's share at the record's terms, the referencestripe:<intent>, and the gate'spayment,account,fee_taken_centsandtransfer_centsas extra fields -- and the gate is told (booked).take-toll :stripeis this, with the payment's id as the reference.(monocle:settle-unbooked! house)asks the gate'spaymentsdoor for what settled and was never booked (a payer who closed the tab) and books each; the host runs it now and then.- THE TERMS AT THE GATE. A deployment may be served by a runtime that
runs its author's code, and that runtime is the one that opens its
tolls, so what the house keeps must not be what it says. The house
registers each deployment's terms at its gate (
record-terms: the deployment, the house's percent, the author's account, the engine) with(monocle:register-deployment! house name)when it deploys,(monocle:deregister-deployment! house name)when it takes one down, and(monocle:register-deployments! house)-- the whole set, replacing what the gate held for this house -- at its start and now and then. A gate that registers opens every payment at the registered terms, whatever the caller sends. Only the house's own runtime registers: its gate function proves it to the gate with a credential the serving runtimes never hold.open-tollstill sends the record's terms, for a gate that registers nothing. - EACH DEPLOYMENT'S KEY. Several runtimes serving one house's
deployments may look alike to the gate, so the terms alone would let
one of them speak for another deployment of the house.
make-recordmints a key into the record ("gate_key",record-gate-key), the house registers it with the terms, and the gate keeps only its hash. Whatever serves a deployment is given its record, and so its key and no other, and sends it back on every call that names the deployment:open-tollandsettle-toll!do, and a host's own calls add(monocle:deployment-key-plist house name). A record from before keys gets one when the house registers it (ensure-gate-key!). The key is a credential: never answer it to a visitor or an author.
book-revenue! takes :extra, a plist of the host's own fields for
the line.
Money given back is a NEGATIVE line. (monocle:book-refund! house
name refunded :reversed :fee-refunded :reference :payment :refund-of)
books a refund: the gross goes back; the author's net give-back is
their transfer reversed less the fee the house refunded (the processor
pays a refunded application fee to the author's account, where it was
collected from), the payee amount; the house's fee refund is a negative
fee; and house_absorbed_<unit> is the refund less the reversal, what
the house eats beyond its fee. (monocle:book-dispute! house name
disputed :fee :stage ...) books a chargeback: at stage created the
amount and the processor's dispute fee are taken from the house, which
fronts disputes (the author's transfer stands), so fee and payee are 0
and the house absorbs both; at stage won a dispute-won line brings
them back. (monocle:upstream-report house rates) is an upstream licensor's share
of the house's fees, quarter by quarter -- a house that runs
applications on a licensed engine owes its licensor a share of what it
keeps from them. RATES says how and holds the only numbers: :percent
of the attributable fee, :lisp-match (a runtime whose Lisp names it is
the licensor's), :attribution by engine (how much of such a runtime's
fee counts), :minimum (a quarter's share under it is carried into the
next). The base is the fee before the house's share of the card cost,
net of refunds and disputes; the share is summed over the quarter and
rounded once, half up; what was not attributed is kept as a running
total; runtimes the licensor has no part in are summed the same way
with no share.
settle-unbooked! books each refund and each dispute stage
once from what the gate reports. A house that sells HOSTING for a
deployment (compute credits, its own sale, nothing of it the author's)
books it with (monocle:book-hosting! house name gross :card :credits
:reference): a line of kind hosting, the whole amount the house's
fee (fee_before_card the gross), counted by revenue-report and
upstream-report as :hosting and treated by the latter as a fee, so
a licensor's share of a licensed runtime's hosting is the same share of
the same base -- and which runtime is the licensor's is the line's Lisp
name alone (:lisp-match); the engine only sets the fraction. A host that lets a visitor prepay a
balance for an application's tolls books the pack as a toll when it is
bought and each draw on it with (monocle:book-draw! house name amount
:toll :purse :reference): a line of kind draw, no money moving, left
out of the reports (draw-line?). revenue-report is net of them
(:refunds counts the lines, :house-absorbed-<unit> what the house
ate); payables and earnings sum every line, so they are net too.
A manifest
(multiple-value-bind (manifest reason) (monocle:read-manifest "monetize.sexp")
(if manifest
(or (monocle:manifest-faults manifest) ; a list of strings, or nil
(monocle:toll-for-path manifest "/export/svg?size=a3"))
reason))
A manifest is data from a stranger. It is read with the reader's evaluation off and checked against a closed list of fields before anything acts on it.
A house of containers
house/ holds the two programs a host needs to run applications as
programs of their own. Neither is part of the system; each is loaded
after it.
house/render.lispreads the house'sapps.sexp(one entry per application: its name, its image, the house's copy of its manifest, its payee and its slider) and writes a Compose file with a read-only container per application, each on a network of its own with no way out, and the gate's configuration: each application's name to its container, with its tolls. An entry with faults is left out, with its reasons.house/keeper.lispkeeps the house's records and books behind a small door,POST /book, where the gate books each toll it takes;GET /earnings?app=answers an application's quarters. It runs on a network only the gate shares.
The gate itself is not here: it is whatever reverse proxy the house
runs, configured from what render writes. common-lisp.net's apps
host (clo/cl-site,
clnet/deploy/apps/) is the first kit built on them.
Checks
ci/test.lisp runs the library against a scratch directory:
(asdf:load-system :monocle)
(load "ci/test.lisp")
Status
The records, tolls, the provider's seam, the books and the manifest are
here and in use, and so are the renderer and the keeper for a house of
containers, and the :stripe provider that settles a toll paid by
card through a gate that holds the keys, with refunds and disputes as
negative lines. Not here yet: the check that builds and runs an
application against the profile.
Licence
GNU Affero General Public License, version 3 or later; see LICENSE.