Keg docs

Keg's events: one protocol, two stores

Keg keeps events in two places: in each app's own database (the app layer, keg.* in Postgres, crates/kegd/src/capabilities/003_events_jobs.sql) and on the node (keg.db, for Keg's own services and the workflow).

The two share a shape, not every word. What is the same in both:

Everything else differs, and is listed below: in the table, and under "How an app's events differ from the node's".

On the node

Event. id (uuid), type (dotted, lower case, thing then what happened: work.created, gate.answered), subject (what it is about, kind/name: work/7, changeset/local/15), at (seconds since 1970), actor (a role and a name), and payload: small JSON. The node's own events carry ids and states only, never a brief, a title, a secret, a diff, a note or the body of evidence; an event a service writes carries what it wrote, and an app's event relayed for a binding carries the app's content (both below). Events have an order, and the place in it is the cursor, which a reader keeps and treats as an opaque string.

Subscription. A name, an owner (the service), and the event types it takes. It is made from what a person allowed of the service's manifest: each listens pattern allowed, and the app events of each binding to an app's events allowed (below). Nothing else makes or changes one.

Delivery.

Reading without subscribing. A person may ask for the events after a cursor, and wait for more: at most 20 s, and at most 64 requests wait at once. If the cursor is older than what is kept, or newer than anything ever written, the answer is the error cursor_expired, and the reader reads the state afresh. The log never skips silently.

Keeping. The node keeps events seven days, or the newest 100,000, whichever is fewer, and never removes one while a delivery that is not done points at it. done deliveries are removed with their events. The node's own events and the events services write are counted apart: the newest 100,000 of each are kept, so a service writing many never pushes the node's own out. A failed delivery of an event a service wrote does not keep that event: once the event is past what is kept, it goes, and its failed deliveries with it. A failed delivery of one of the node's own events keeps it until a person tries it again. The node removes at most 5,000 events at a time, oldest first, and goes on a second later until what is past keeping is gone.

Falling behind. A subscription with 10,000 deliveries waiting or running of what one service wrote is behind on that service. While it is, new events that service writes are not delivered to it: they are written, and others are told of them, but it misses them. It is still given what the node writes, and what every other service writes; a failed delivery is a dead letter, and does not count. Beside that, a subscription with 100,000 deliveries waiting or running in all, of anyone's events, the node's own among them, is given nothing more until it has fewer: that is the store's guard against a service that never claims. Both are counted as missed, and shown the same way. It is never given what it missed later. How many it missed is shown with it (missed in GET /v1/subscriptions, keg events, the console's Events page) until a person installs the service again; a person fixes the service or removes it.

Writing. A service from outside writes at most 20 events a second and sets at most 20 timers a second, after as many as 100 of each at once. It writes no evidence and runs no checks in this version: a manifest from outside that names writes or asks check:run is refused, since only Keg's own checks are told of checks. A service from outside allowed work:read reads every piece of work on the node: its files, its brief, its changes and notes, and the names of every environment's secrets, never their values; the install question says so. Past that it is refused with HTTP 429, rate_limited. The answer says how many seconds to wait, in a Retry-After header and as retry_after in the error; every 429 the node gives says it. A request refused for what it asks, such as a subject not its own or a timer too far ahead, spends nothing of the allowance. Keg's own services (checks, console, realtime) are not held to it: whether a caller is one of them is read from the service the node stored, not from the name it gives. Keg's checks write a record for each check; answered 429 all the same, they wait as told and try again, for at most a minute in all, while the hold is kept.

An app's events. A service bound to app-events:<app>:<pattern> is told of that app's events whose own type the pattern matches, from every organization of the app and every environment it is deployed in with events or jobs. The node reads the app's keg.events as <db>_keg, on a connection that only reads, and writes each app event into its log once, however often it reads it: as app.<app>.<type>, about app/<app>/<id>, by the actor {"role": "app", "name": "app.<app>"}, never by the node. Its payload is the app's content: {"app", "env", "type", "id", "at", "organization", "audience", "about", "payload"}, at in seconds, payload whatever JSON the app wrote (an object, a list, a string or a number, as it is). Every field but payload is the node's, read from the event's row: organization is the row's organization_id, which the app's capability layer sets from the acting user's (or job's) organization in keg.emit, never anything the app wrote in its payload. audience is who the event is for as the row keeps it: null for the whole organization, else {"roles": [...]}, {"users": [...]}, or both in one, {"roles": [...], "users": [...]}, for whoever is in either; one longer than 8 KiB is {"cut": true}, which is for no one in particular, never for everyone. about is what the event is about (ticket/42), or null. A service that shows events to people treats any audience it does not understand as not theirs. A service that keeps each organization's events apart relies on it. More fields of the row may be added beside these; a service ignores any it does not know. An app payload over 16 KiB as text is not fetched from the app's database at all: it is told of with "payload": null, "cut": true and "size", its size in bytes; so is one nested deeper than the node reads (128 levels).

The node reads an app's events in the order their transactions were given ids, not by time: 005_relay.sql gives each keg.events row the id of the transaction that wrote it (xid, an xid8, 64 bits, which never wraps; rows from before that file have 0, and come first). Each read takes only rows of transactions below the app's database's horizon: the oldest transaction id held by a backend connected to that database, or by a transaction prepared in it, and never past the next id to be given as it is read (a transaction with no id yet is given one at least that). The server's own superuser makes keg_horizon.oldest() in each app's database for this, with a search path of its own; only Keg's role of the app may call it. Every transaction below the horizon has ended, and the rows are read in a snapshot taken after it, so nothing can appear behind the node's place later, whenever it commits and however long the node was stopped. The consequences:

An app's pages. A service asks for such a binding at GET /v1/bind/<name> and is answered {"kind": "app-events", "app", "types", "origins"}, and "token" where its manifest asks for one (token = true on the bind; false unless said). token is 256 bits the node made at random for that service and that app (64 hex digits), the same each time it is asked; the node gives it to the app's own services in each environment the app is deployed in, as the environment value KEG_SERVICE_<NAME>_TOKEN (<NAME> the service's name in capitals, - written _), with KEG_APP, the app's name, and, for a service with a page, the address a person's browser opens it on, KEG_SERVICE_<NAME>_URL (http://127.0.0.1:<port>, on the port that leads to this node from the computer a person uses; not given at all where none does), set when the app is next deployed: the node starts no app's containers again by itself. Until then the service's row in keg system ls and the console says so ("supportdesk in local is given its token for query when it is next deployed: keg up"), as do the answers to installing it and removing it (DELETE /v1/system/services/<name> answers {"waiting": [...]}, and an app keeps a removed service's token, which reads nothing, until it is next deployed). It is made anew when the service is removed or replaced from another folder, and it is never in an event, a log, keg system ls or anything an agent reads (but code an agent deploys as the app runs with it, as it runs with the app's database). A service with no token asked for gives the app nothing: NATS's readers, say, read with a password a person sets, which is for a person who may see every organization, never for the app's users.

The app's token reads every organization's content the service holds of the app, so it never goes to a browser. It is for the app's server; a service takes it only from a request no browser made (no Origin, and no Sec-Fetch-Site but none). For a page, the app's server makes a token for one organization, the signed-in user's, for at most 15 minutes:

v1.<payload>.<mac>
payload = base64url(JSON {"app": "<app>", "org": "<organization id>", "exp": <seconds since 1970>})
mac     = base64url(HMAC-SHA256(key = the app's token as text, message = the payload's base64url text))

base64url without padding. In JavaScript, serviceToken(name, { org, ttl }) of @keg/app/service makes one (ttl in seconds, 300 unless said, at most 900). A service checks the mac in a time that does not depend on how much of it is right, that exp is not past (30 seconds are allowed for the clocks) nor more than 15 minutes and 30 seconds ahead, and that app is one it is bound to with a token, and answers that organization's content alone. A token of an app it is not bound to is refused in the same words as one whose mac is wrong, so that a refusal says nothing of which apps are bound, and the mac is taken in its one base64url spelling alone. Such a token is a bearer token: whoever holds it, from wherever, reads that organization's content with it until it expires, up to its 15 minutes. It is not bound to a page's origin or to a connection, so an app hands it only to its own signed-in user's page, and keeps its ttl short. A page whose origin the node vouched for (below) is answered only with such a token of that app. Not built in this version: an app's containers cannot reach a service's page, so the app's server cannot ask the service itself; a path limited to the bound app's own containers is for later. origins are the exact origins, scheme, name and port, of the pages the node serves for that app: for each environment the app is deployed in, the name of the service it is made of there that has a port (<service>.<env>.keg.localhost, and in local <service>.keg.localhost too), on the ports that lead to the node's names from the computer a person uses (below, "Where a node is reached"; on a node reached directly, http on --names and https on --names-tls, with no port when that is 80 or 443). It is empty when the node serves no names, or does not yet know where it is reached. A service that answers a browser trusts a page as the app's only by these, never by a name that looks like the app's: anything on this computer may listen on another port, and a browser sends every *.localhost name here. So that nothing else is served by those names, a name in local that is an app's service is that service's, and never a person's container of the same name: while the service has no port there, between one release and the next, the name leads nowhere, and a container of that name is not listed as leading anywhere by it. A name that is no app's goes to a container of that name first, as it always has. The origins change as the app is deployed and removed, so a service asks again from time to time (the query service every minute, and at once, at most once in five seconds, when a page's origin or a token is not one it was told of). A program that is not a browser can send any Origin it likes: an origin tells a browser's page apart, not a program, so it is never a credential. Anything that carries an app's content asks for a credential, of a browser's page and a program alike: a token for one organization from a page, with the origin besides.

005_relay.sql reaches an app bound before it at its next deploy, or as the node begins to follow the app (when the node starts, or the app is first bound). The node refuses to read from a Postgres older than 13, and says so in its log. Where the app uses realtime, Postgres tells the node of each commit, and that is only a reason to read; it asks every minute besides. It reads at most 500 rows at a time, and writes at most 1200 of an app's events a minute into its log for each environment; the rest wait, in order: by transaction, and a transaction's events in the order they were emitted (011_events_order.sql; before it, in the order of their random ids). What an app wrote before a service was first bound to it is not told of, and a binding added later hears only what began after it was allowed or committed after the node first read for it, unless it asks for the past: from_start = true on the bind, which the person installing it is asked about ("It is sent every event of that is still kept, from the first, not only those after it is allowed."; its id ends +from-start). Such a binding, allowed, is also sent each event of its types still in the app's keg.events that it was not told of as it came, from the first, in order, to it alone (each delivery as the live one is, in the app's queue beside what comes now, each with its own at). That past is read for each environment as far as what had committed when the node first read for the binding, a batch of 500 at a time, only while fewer than 500 of the app's events wait for the service (far below the 10,000 at which it would miss what comes) and within the 1200 a minute; keg system ls and the console's System page say how far it got ("sending the past events of desk in local from the first, as query takes them: 340 so far", then "sent every past event of desk in local from the first: 1234"). Installed again, or the app's database made again, it is sent the past afresh. An app's events are kept, held back and counted as what a service writes is, each app as one writer, so they never push the node's own out. Nothing but such a binding brings an app's events to a service, and a service cannot listen to an app. type itself.

A service in a check. A check that names services (services: [query] with db: true) is given an instance of each, made for it and removed when it ends, at most two checks' at once (a third waits, in order, for ten minutes, and is then given back to be run again). Its socket is a door of its own: its claims are answered from the check's own Postgres, read as above (as <db>_keg, read only, below the horizon, in the order of the transactions), each as a delivery of app.<app>.<type> about app/<app>/<id>, with env check, one at a time; a hold, an extend, a finish and a fail work as they do on the node, and one tried five times is let go. Nothing of it is written into the node's log, and nothing of the node's reaches it. Its bindings are what the check gives it: a volume of its own, empty; a value made for each secret; for the app's events, a token made for this instance and no origins. It cannot write events or set timers. The check's command is given KEG_SERVICE_<NAME>_TOKEN and KEG_SERVICE_<NAME>_URL (http://<its walled address>:<port>) and KEG_APP; the instance has KEG_ADDRESS, that address. The instance is walled as a check's Postgres is: no port on the node and no internet; the node and what shares its network (work machines among them) reach its ports, and the token, 256 random bits given to that check alone, is what keeps another work out. One whose image is not here, or that the node cannot run, is the verdict error; one that stops or is not ready in 60 seconds fails the check. What a node that stopped left is removed as it starts.

Withdrawing. The node can withdraw the deliveries for a subject: they are marked done with last_error "withdrawn", their tokens stop working, and no delivery.done is written. It does so when a piece of work is stopped, sent back, retried or rejected, and for a subscription's deliveries when its service is removed. There is no call for it.

A service's own events

A service allowed emits:<type> writes events of <service>.<type>, and no other type: POST /v1/events {"type": "<type>", "subject": "<service>/...", "payload": {}}, the type sent bare, which needs the grant events:emit:<service>.<type> that allowing it gives. The subject is one of its own, beginning <service>/, 1 to 200 of A-Z a-z 0-9 . _ : / -; the payload a JSON object of at most 16384 bytes (left out, {}); the type lowercase words joined by dots, at most four. It is written by the actor {"role": "service", "name": "<service>"}, never by the node, and the answer is the event as written. A name the node writes events under (work, gate, service and the like) is no service's.

Such an event is delivered to every subscription that takes it, but never to the service that wrote it: no service listens to its own namespace. Its payload is what the service wrote; the node does not read it, and it is for the services that listen to it, and the person who allowed them, to trust it as far as they do. It counts with that service's own events as the log is kept and as how far behind on it each subscriber is.

Timers

A timer is an event written when its time comes, once. A service allowed asks:timers:set, and emits:<type>, sets one for that type: PUT /v1/timers {"type": "<type>", "subject": "<service>/...", "at": <seconds since 1970>}, the type sent bare. Setting the same type and subject again moves it. at is at most 366 days ahead; left out, or past, it fires at once. A service has at most 1000 set; GET /v1/timers lists its own, and DELETE /v1/timers?subject=S&type=T unsets one, answering {"removed": true} or false. A person's GET /v1/timers lists every timer on the node (keg events timers, the console's Events page).

When it fires, the event is written as the service's own (above), with the payload {}, and the timer goes, in one transaction: a node that was down fires what fell due as it starts. It fires only if the service may still write that type then; one removed takes its timers with it.

The node sets one timer itself: gate.waiting, a day (86400 s) after gate.asked, for a question nobody has answered. It is told of once, and unset when the question is answered or withdrawn.

Bindings

A binding is something a person allowed a service to be handed, by a name of its manifest's own (binds = [{ name = "data", to = "volume" }]). Its kinds:

A service lists its own with GET /v1/bind ([{"name", "kind"}]) and asks for one with GET /v1/bind/<name>, answered only for its own: {"kind": "volume", "path": "/data"}, {"kind": "secret", "value"} (asked again each time it is needed; a secret not set is not_found, saying how a person sets it), {"kind": "publish", "port"}, or {"kind": "app-events", "app", "types", "origins"}, with "token" where the manifest asks for one (above, "An app's pages").

In an app

Event. A row of keg.events: id (uuid), organization_id, type (any text that is not empty), payload (any JSON the app writes), actor_user (the acting user's uuid, or none), and at (a timestamptz). It has no subject, no actor role or name, and no cursor.

Subscription. A row of keg.subscriptions: (type, job), written by the app's migrations with keg.on(type, job). It has no name and no owner.

Delivery. A row of keg.jobs, made for each subscription by the event_jobs trigger in the event's transaction. Its payload is {"event": id, "type": type, "payload": payload}. A job can also be made without an event, with keg.enqueue. Its run_at and held_until are timestamptz.

The same act in both stores

Act In an app (SQL) On the node (HTTP on the socket)
Write an event keg.emit(type, payload), as the acting user, any payload the node's emit, in the transaction of its change, ids and states only; a service's own types with POST /v1/events, its payload what it wrote
When it happened at, a timestamptz at, seconds since 1970
Who did it actor_user (a uuid, or none) and organization_id actor: a role and a name
Its place in the order none cursor
Listen for a type keg.on(type, job): a (type, job) row, no name, no owner the service's manifest: listens = ["app.bound"]; a subscription with a name and an owner
Make a delivery with its event the event_jobs trigger makes a keg.jobs row a deliveries row, in the event's transaction
Claim keg.claim(names, seconds): one job, the caller chooses the hold POST /v1/deliveries/claim {"max": 1, "wait": 20000}: 1 to 10, held 60 s
Extend none POST /v1/deliveries/ID/extend {"token": "...", "seconds": 60}
Finish keg.run(id, token) checks the token; keg.finish(id) takes none and keeps no result POST /v1/deliveries/ID/finish {"token": "...", "result": {}}, a result of at most 4096 bytes
Fail keg.fail(id, token, why) POST /v1/deliveries/ID/fail {"token": "...", "error": "..."}
A hold that runs out ready again at once waits attempts² × 10 s, as a failure does
See what failed keg app show: how many failed keg events failed, GET /v1/deliveries?state=failed, the console's Events page
Try a failed one again none keg events retry ID, POST /v1/deliveries/ID/retry
Read the log none keg events [--follow] [--type T], GET /v1/events?after=C&type=T&limit=100&wait=20000

GET /v1/subscriptions lists each of the node's subscriptions, with how many of its deliveries are waiting, running and failed, and how many events it missed while it was behind. In JavaScript, require('@keg/app/node') gives connectNode() with claim, extend, finish, fail and watch, shaped like the app client's job and work, and emit, setTimer, unsetTimer and binding for a service's own events, its timers and what it was allowed; require('@keg/app/service') gives an app's servers serviceFor(name), the token and address of a service bound to the app's events, and serviceToken(name, { org, ttl }), a token for one organization for its page.

What the node writes

Type Subject
work.created, work.changed work/<id>
stage.started, stage.finished work/<id>
check.requested work/<id>
gate.asked, gate.answered work/<id>
gate.withdrawn work/<id>, with {gate}, when a person stops, sends back, retries or rejects work that has an open question
gate.waiting work/<id>, with {gate}, a day after gate.asked when the question is still open; once
evidence.added work/<id>
changeset.created, changeset.changed changeset/<env>/<id>
service.installed, service.started, service.stopped, service.removed service/<name>
service.failed service/<name>, with {name, code}, when a service that kept stopping by itself is left failed for a person
app.bound app/<name>, once the app's release is recorded as deployed
container.started, container.stopped, container.removed container/<id>
delivery.done, delivery.failed the delivery's subject
vault.set, vault.granted, vault.revoked, vault.removed, vault.used vault/<name>, with the entry's name and the use; never a value (docs/VAULT.md)

Beside the node's own, the log holds what others write, never by the node:

Type Subject
app.<app>.<type> app/<app>/<id>, an app's event relayed for a binding, by the actor {"role": "app", "name": "app.<app>"}, with the app's payload, which is content
<service>.<type> <service>/..., a service's own event, or one of its timers, by that service

delivery.done and delivery.failed are written for a service's deliveries. delivery.failed carries no error text; the error is read from the delivery. For a service from outside, delivery.done carries no result either, only ids and the state: what it returned is read from the delivery, as its result in GET /v1/deliveries, by a person. Its delivery.done and delivery.failed count with that service's own events, as the log is kept and as how far behind on it each subscriber is, so a busy service never pushes out the node's events. Keg's own services' delivery.done carries their result, which the workflow reads, and counts as the node's.

Who may do what on the node

How an app's events differ from the node's

Step B built the relay above: an app's events reach the node's log, as app.<app>.<type> events written by the app, and from there reach the services bound to them like any other event. In the app's own database they stay as the app writes them, and these differences remain:

An app's threads, for a person

The threads of an app's agents, of every organization, are a person's to read, start, stop and wake; an agent, a service and the assistant are refused each route.

An organisation's own tools

Capability 023. These are tools an app's organisations write in JavaScript, which the node runs for a step of a thread of that organisation.

An app's jobs, for a person

A person, or the console, reads and retries an app's jobs; an agent and a service are refused both, as they are GET /v1/apps/<app>.

Where a node is reached

A node's ports are not always on the same numbers on the computer a person uses. On a Mac, Keg's VM serves names on 7380 and 7443 inside it, and keg vm forward opens them on the Mac's localhost. Two nodes on one Mac (each with its own HOME) cannot both have 7380 there. So:

On Linux kegd binds --names and --names-tls itself. A port that is taken there is said in kegd's log ("names are not served: ..."), and the node goes on without names, handing out none.

HTTP/1.1 through the names

One connection to the names is for one name: the first request's Host (over --names-tls, the name the caller asked a certificate for, which the first request must match, else 421). Every request on the connection is read as far as its head and the framing of its body before it is sent on, and:

A public service (experimental, public-services)

A service whose keg.yaml says public: true under run, and has a port, is answered to the world by the node's front door at https://SERVICE.<the node's public name>, from the moment a deploy of it is serving and until a deploy whose definition no longer says so. The front door ends TLS, with a certificate got for that name as a share's is, and passes the connection to the service's port as it is.

service: web
image: example/web
run:
  port: 8080
  public: true

A member's invitation

keg node invite NAME --member issues an invitation for a person who uses the node and does not decide who else is let in. Its requests are a person's in everything but this: asking for an invitation, the list of them or the withdrawal of one, and changing whose node it is (/v1/account), are refused with forbidden. It cannot have a port opened at the node for a share. A node keeps at most 200 invitations.

A member's invitation keeps someone from being handed these by the node. It is no wall between people: a member runs containers as a person does, and Keg's containers are not a sandbox for someone set on getting out of one. Do not give one to someone you would not let run a container on the node.

A folder sent to be built (experimental, remote-builds)

A node whose processor is not this one's builds what it is sent, since what is built here would not run there.

What is taken: files, folders and symbolic links only; no path that is absolute or has .. in it; nothing written through a link; a link only up as far as the folder itself and then down, never .. after a name; modes kept to read, write and execute; at most 2 GiB and 200,000 entries, and never into the last 2 GiB of the node's disk. Anything else refuses the whole folder, and nothing of it is left.

A long name, or a note before an entry, is 64 KiB at most, and the whole is counted as it is unpacked, so a few bytes sent cannot become more than a folder may be.

The sending node sends only what the build reads: the folder that is built from, less what the .dockerignore there leaves out (never the Dockerfile or the ignore file), the keg.yaml, and an app's migrations. Nothing else beside them in the folder is sent. An ignore file that is there and cannot be read stops the send. It sends only when the other node's processor, as its GET /v1/ping says, is not its own.

HTTP/2 and gRPC through the names

A service whose keg.yaml says run.protocol: h2c (its port speaks HTTP/2 without TLS, as gRPC servers do, beside or instead of HTTP/1.1) is reached with HTTP/2 by its names: with prior knowledge on --names, and on --names-tls by ALPN h2. Nothing else is: a service that has not said so, a container run by hand, the console and Keg's own services (*.svc) answer HTTP/2 with 505 and are reached over HTTP/1.1 only, and over TLS are offered only http/1.1, since a browser offered h2 would use it with any name.

Keg ends the caller's HTTP/2 and opens its own to the service, checking every request before it is sent on:

A gRPC client dials <app>.keg.localhost:<names port> (grpc-go: passthrough:/// and insecure credentials, or TLS trusting keg trust --print). The app's own port (localhost:<port>) reaches it as before.

Cookies are not kept apart by port: a cookie a page by desk.keg.localhost sets on one node is sent to desk.keg.localhost on every port of the computer, and so to another node's app of the same name. Keg itself sets no cookie: the console keeps what opens it in the page's sessionStorage, which is kept apart by port, and the realtime gateway takes a session only as Authorization: Bearer. An app that keeps a session in a cookie should not count on its name being its own when two nodes run on one computer.

The app door's claim (Keg-Claim)

POST {KEG_DOOR_URL}/v1/model/calls takes an optional Keg-Claim header: v1.<thread>.<organization>.<0|1>.<expires>.<mac>, as keg.thread_door_claim(thread, token) returns it to the worker that holds the thread (capability 021). <0|1> is whether the thread is a dry run, <expires> seconds since 1970 (five minutes from when it was made), and <mac> the HMAC-SHA256, in hex, of keg-door-claim|<database>| followed by everything before the last dot, under a key the node puts in the app's database (keg.door_key, readable by <db>_keg alone). The key is made anew at each deploy; the door takes a claim signed with the key before it for six minutes after, which is as long as such a claim can still be good, so a step under way across a deploy is not refused. The node gives the key to psql in the Postgres container as a setting of the connection, in that process's environment, as it gives the role's password: Keg's driver runs a command with arguments and an environment and no input.

The door refuses with 403 forbidden a claim that is not of that shape, whose MAC does not check for that app and environment's database, whose time has passed or is more than six minutes off, or that is for another thread or organization than the call's Keg-Thread or Keg-Organization names. A call with a claim is counted for the claim's organization and held to its ceiling where one is set; a dry run's is counted as an evaluation. A call with no claim is counted under no organization: Keg-Organization alone is never counted.

The done line's usage may carry cached_input_tokens: how many of input_tokens the provider read from its cache.

Hooks (/.keg/hooks/<token>)

At the node's own public name (--public-domain), on its TLS listener: POST https://<name>/.keg/hooks/<tag>.<secret>, where tag is 16 lowercase hex (the node's HMAC of the app's database name, so it names no app to anyone) and secret is 48 lowercase hex.

A node with no public name (a Mac's, say) takes the same deliveries on its own names, over plain HTTP: POST http://hooks.keg.localhost/.keg/hooks/<token> (on the names' port, which a Mac forwards to its own localhost), or at any name that is none of Keg's, as a tunnel pointed at http://localhost:<names port> sends. Never at an app's or a container's name, whose paths are theirs; never on a node that has a public name. Every sender there counts as one network, 127.0.0.1's. keg app hooks says where its node takes them. The node:

Every count below is reserved when it is reached, under one lock: all of a step's counts go up at once, or none does, and a refused request moves no count.

  1. Its sender's network is held to 120 a minute (/24 for IPv4, /48 for IPv6), looked at before anything is read or worked out; it pays one for each request not sent with POST, to an address that is no hook's, or forged, never for a verified delivery (a busy sender, such as GitHub, comes from a few networks).
  2. The node looks, in memory, for the hash of the whole token among the addresses of the app its tag names. It knows every app's addresses and each hook's secret (the vault entry granted to the app's service in its environment under the hook's secret name), read again after every deploy, removal and write of the vault (what was read while one of those came is thrown away, and a delivery checked against what changed before it is written is answered 503), each minute, and again for one app at most each ten seconds when an address it does not know is asked for under that app's tag (an administrator may have just made one). They are read by one request at a time: the others answer from what was known, or, with nothing known, wait for that read. A read that fails keeps what was known; with nothing known, the request is answered 503, never 404. Nothing is read from a database or the vault for a request. An address that is not there is answered at once and pays only the node's count for such addresses (20,000 a minute, for everyone): no app's count is touched.
  3. The signature is checked against the secret in memory (github, stripe, slack, hmac-sha256; none checks nothing). One that does not match pays that address's count of failures (60 a minute), which only someone who knows that address can reach.
  4. A verified delivery pays its address's count (600 a minute) and its app's (1,200; a hook that checks nothing pays a count of its app's of its own, 600, so it takes nothing of signed senders), and then the database writes it once (keg.hook_receive): an event keg.hook.<name> of the address's organization, for its administrators, from_outside: true, unless the address or its app has taken its bytes for the day (503 with Retry-After: 3600, and nothing written). It starts the hook's agent's thread unless the organization has had its threads from outside for the day. What the app says of these, and how long what was received is kept, is keg.inbound_limits. A body Postgres's JSON refuses (a \u0000, half a surrogate pair) is written as text.

Answers, JSON: 200 {"received": true} ("again": true when it was written before), 401 signature, 404 {"error": "there is nothing at this address"} for every address that is no hook's, 405 not POST (with Allow: POST), 429 a budget, 503 the app cannot take it now (no secret granted, or its database is down): senders retry. A body is at most 4 MiB, read only once its head has passed the cheap checks (the sender's rate, the address a hook's) and held as it arrives in the hooks' own 64 MiB, of which one network or one address holds at most 16 MiB; past that, 503. GET /v1/apps/{app}/hooks?env= is a person's (the node's socket, not an agent's): the hooks and the last fifty deliveries, never a token or a secret.

Mail in (SMTP)

Off until a person gives the node a domain and turns it on (POST /v1/mail {"domain"}, then {"on": true, "port"?}; in a Mac's machine also "on_a_mac": true). GET /v1/mail says how it stands and the last fifty refusals (when, from which network, why; never what was sent). Both are a person's: the node's socket, not an agent's.

On, the node listens on 0.0.0.0:<port> (25 by default) and takes mail for one kind of address only: <word>+<tag>.<secret>@<domain>, where word is an address an app declared (keg.mail_address), tag is the 16-hex tag of the app's database (as a hook's) and secret 24 lowercase hex; only the SHA-256 of <tag>.<secret> is kept. It speaks ESMTP with SIZE 10485760, 8BITMIME, ENHANCEDSTATUSCODES, and STARTTLS only where the node holds a certificate for the domain. No PIPELINING, AUTH, VRFY, EXPN, ETRN or BDAT (502). It never relays, never sends and never bounces: everything it will not take is refused while the sender is connected.

GET /v1/apps/{app}/mail?env= is a person's: the addresses, how many organizations have each, and the last fifty messages, never a token.

Keg's documentation. Something wrong or missing? Say so.