Skip to content

How ankka-reasoning works

ankka-reasoning is one ankka service with four parts: records, graph consumers, answers, and the blueprints it hosts on ankka's agent runtime. A writer sends a record, or a run of a blueprint writes one through a tool; the service checks it and keeps it; the record is published to a Kafka topic as a node and its edges; ankka's graph sink copies the topic into Neo4j; and a reader's question is answered by walking that graph. The write never waits for the graph, and the graph is never written by the service.

How ankka-reasoning works. Writers, a person, a script or a test, send records to the ankka-reasoning service, which runs in an ankka project; and a run of the appraiser blueprint, hosted by the service on ankka's agent runtime, writes records through its tools, each with an act. Inside the service, Records holds one entity per record, never edited, and checks every rule against the service's own records; every record is kept in the service's Postgres database. Each change goes to the graph consumers, one per kind of record, which turn each record into a node and each link it stated into an edge and publish them as graph deltas to a compacted delta topic in Kafka under the contract ankka.graph-delta.v1. The graph sink, a second service in the same project that runs ankka's graph sink over ankka-contrib's Neo4j store, reads the topic and merges each delta into the graph database, Neo4j, which is a copy that can be emptied and rebuilt from the topic alone. Readers ask the service's Answers, which query the graph database, read only, and reply with records only. A write never waits for the graph.

Records: where the truth is

Each record is one ankka entity, kept in the service's Postgres database. A question, a holder, a source, a piece of evidence, a claim and a market are each a key value entity; a belief, one holder's probability for one hypothesis, is an event sourced entity whose events are its revisions.

Whether a record may be held is decided here and nowhere else. A claim must name evidence that is held; a belief revision cannot rest on a claim dated later than itself; a writer must speak for the holder it writes as. Each rule is checked against the service's own records before the record is kept, and a record that breaks one is refused by the rule's name with nothing changed. Refusals lists them.

The graph database is never consulted for a rule. It is a moment behind the records, so a rule checked against it could pass for a record that should be refused.

The check runs before the entity's command and is still true after it, with no lock, because nothing a rule depends on can stop being true: a record that is held stays held, its date and its links never change, and a question's hypotheses and a holder's writers are only ever added to. Two rules do depend on state that changes, which revision of a belief is current and which resolution of a market is, and those two are decided inside the one entity that owns that state.

Graph consumers: one writer per element

Every record is published as a node, and every link it stated as an edge, by an ankka graph consumer. There is one consumer per kind of record, and each publishes the record whole: its node and every edge that leaves it.

A link is stated by one record, the newer of the two it joins, and is published with that record and by nothing else. So no node and no edge has two writers. That is what makes the topic safe to compact and the graph safe to rebuild: the latest delta under an element's key is the whole truth about that element.

The path of one change. A writer sends a record, here POST /claims, and is answered once the record is held. Inside the ankka-reasoning service the record is one entity, which checks every rule and is kept in the service's Postgres database: a key value record's state, or a belief's events. Each change, the new state or a new event at sequence number n, is polled from the database once a second and handed at least once to the graph consumer for that kind of record, which builds the record's node and an edge for each link the record stated, through the vocabulary. ankka publishes each element as one graph delta to the compacted Kafka topic reasoning-graph, keyed by the element, carrying version n and the element's whole state. The graph sink, a service of its own, applies a delta only when its version is newer than what Neo4j holds, merging the element by its key. The worked example: the claim launch.barrier-gone, stated by agent-a, deriving from a notice, supporting the hypothesis launch/yes and revising the claim launch.pending, becomes five deltas: the claim's node and four edges, all at the claim's version. In Neo4j each edge leaves the claim and points to a node that was already held, published by its own record. Withdrawing the statement later is a new change: the same five keys at a newer version, the node without its text; the sink replaces the node's properties whole, and compaction drops the older delta from the topic.

What may be published is fixed by the vocabulary, a value in the code that names every kind of node and edge, its properties, and the kind of record that publishes it. A consumer builds an element only through the vocabulary, so an undeclared label or property is an error in a unit test and never reaches the topic.

The topic and the sink: the graph is a copy

The deltas go to one compacted Kafka topic, reasoning-graph, keyed by element. ankka's graph sink, deployed as a service of its own from ankka-contrib's image ghcr.io/thinkmorestupidless/ankka-graph-sink-neo4j, reads the topic and merges each delta into Neo4j, replacing a node's properties whole and passing over a delta older than what the graph holds. It keeps no state of its own: the topic is where it starts from.

The graph database holds nothing the topic does not. It can be emptied and filled again from the topic alone, with the service not involved, and every answer is the same afterwards. Operate the service does it.

Runs: a blueprint writes through tools

The service hosts one blueprint, the appraiser, on ankka's agent runtime. A run of it is started by a request and carried out by ankka: its workers call the three tools the service gives them, and each tool writes a record exactly as a request to the belief layer's route would, under the same rules, and then writes the record's act. The run, its ending and the blueprint's versions are ankka's own records, which the service copies into records of its own as ankka's events arrive, so that they reach the graph beside what the run wrote. The process layer is the model, and Run a blueprint the requests.

A run asks a text model and a judgment through ankka's providers, from keys in the environment. Without a key the service starts and serves everything else; a run that asks ends failed naming the key. The build's suites script both providers and never call out.

Answers: a walk, made of records

An answer is a Cypher query that starts from one node found by its identifier and walks outwards: from a belief revision to the claims it rests on, the evidence each derives from, and each piece's source. The reply is those records and their identifiers. No text is generated, so an explanation cannot say anything the records do not.

Answers read the graph database; nothing else does. A route that reads it answers 503 when it cannot be reached, and every other route, writes included, is unaffected. Ask for explanations shows each answer.

What a write waits for

A write is answered when the service's database holds the record. It does not wait for Kafka, the sink or Neo4j, so a graph database that is down costs a writer nothing.

The graph follows within a few seconds: a consumer learns of a changed record by polling the database once a second, then the delta crosses Kafka and the sink applies it. A writer that needs to read its own write from the graph asks POST /graph/wait, which ends when the graph holds the record's node and every edge it stated, or when a limit passes.

Where each part runs

ankka-reasoning deploys as two services in one ankka project. You or a CI job use the ankka CLI, through the installation's gateway, to create the project, declare the delta topic, set the secret and apply the two descriptors each release attaches; the control plane and the operator create and own each service's Deployment, database and route. The service reasoning is one image, a Scala service of one or more JVM instances forming one Pekko cluster, with the layers compiled in: graph, belief, market and process, composed by the service module; the layers are also published as libraries. Running, it has graph consumers, HTTP endpoints on port 9000, one entity per record, and answers. Its records are kept in its own database in the project's Postgres. Its graph consumers publish deltas to the topic reasoning-graph in the installation's Kafka, declared on the project and compacted. The second service, reasoning-graph-sink, is ankka's graph sink in ankka-contrib's image, with no HTTP, no database and no code of ankka-reasoning in it: it reads the topic and merges each delta into Neo4j 5.26 or later, which you provide where the project can reach it. Both read the Neo4j credentials from the project secret reasoning-graph. The service's answers read Neo4j over Bolt, read only. People reach the service through the gateway at its exposed hostname with a token from an issuer the service lists; another ankka service reaches it in the cluster over mutual TLS and writes as service, its project and name. Readers with a Cypher client query Neo4j directly, read only.

ankka-reasoning is two services in one ankka project. reasoning is one image: the layers are compiled into it and composed by the service module, and it runs as one Pekko cluster of one or more instances, its records in its own database in the project's Postgres. reasoning-graph-sink is ankka's graph sink in ankka-contrib's image; no code of ankka-reasoning is in it, and it shares nothing with the service but the topic and the secret that names Neo4j's user. The topic is in the installation's Kafka, declared on the project. Neo4j is provided by whoever deploys it.

There are three ways in. People reach the service through the installation's gateway at its exposed hostname, with a token from an issuer the service lists. Another ankka service reaches it inside the cluster over mutual TLS and writes as service:<its project>/<its name>. A reader with a Cypher client reads Neo4j directly, as Read the graph shows. Only the sink writes to Neo4j. Deploy on ankka applies each part.

The modules

The code is six sbt modules, and the direction they depend in is the layering:

Module Holds
graph the vocabulary as a value, the only way to build an element, the reader of the graph database
belief the belief layer: its records, rules, graph consumers, answers and routes
market the market layer, built on belief
process the process layer, built on market: acts, runs, versions, the tools, the appraiser and the golden set
service the three layers composed into the one deployable, with the providers from the environment
seed a client of the service over HTTP: seed files and the two measuring tools

belief does not depend on market, and neither on process, so a market word in the belief layer or a process word in either does not compile. Layers and the vocabulary says what that buys.