How a model response enters the Demiurge Assistant knowledge base
Engineering notes · Part 2 · Ilia Gurikov
In the first article, I explained why a regular AI chat was not enough for working on Eon. Ideas arrived quickly, but I still had to transfer characters, events, and relationships into the world database by hand. I also had to keep an incidental detail in a response from becoming an established fact.
In Demiurge Assistant, there is a separate step between a model response and the knowledge base: a change proposal. It contains specific entities and relationships that can be reviewed and edited before they are saved.
Now I want to explain how this transition works in the code. We will start with a small example and follow it through to a database write. The response format matters, as do the order of operations and what happens when something fails.
One paragraph can contain several changes
Consider a hypothetical example that is not part of Eon's existing canon. The model proposes a merchant named Mira: she works in the Old Port, supplies a local guild, and secretly passes information to its opponents.
For a reader, this is a short NPC description. For the knowledge base, it contains several different elements: a character, a location, a faction, and directed relationships between them. The Old Port and the guild may already exist. Creating them again every time they are mentioned would be a mistake.
Another detail needs its own decision. Mira's association with the guild might be public, while her information sharing should remain a game master secret. If the whole paragraph is stored in a single field, these details are harder to use separately.
Extraction therefore breaks the source text into elements of the world model. At this stage, the content is still a proposal. Even a neatly completed record may describe a character I do not want to add to the campaign.
What extraction actually returns
The contract for this stage is defined in the ExtractionPayload class. It contains separate lists of entities, relationships, world rules, random tables, and table rows. Our example only needs the first two, but the other content uses the same proposal mechanism.
An entity has a type, a name, a summary, a description, aliases, and tags. Secrecy and verification status are separate fields. The attributes field holds a dictionary of additional properties. This makes it possible to pass a meaningful record instead of trying to parse one large piece of prose later.
A relationship is also an object in its own right. It has a source, a target, and a type. Optional fields include a description, a validity period, evidence, secrecy, confidence, and weight. The confidence field describes certainty about a relationship, while the weight field reflects its importance or strength. A number in the confidence field does not establish that the model's estimate is statistically calibrated.
The large language model (LLM) is asked for a structured response in JSON format. The response then goes through parsing, normalization, and validation using the Pydantic library. For example, each end of a relationship must have exactly one reference, and temporary entity identifiers must be unique within the payload.
The system first attempts to repair some malformed JSON locally. If the result still cannot be parsed, it makes one additional request to the model with the error description. If that also fails, extraction ends with an error. This bounded retry avoids quietly repeating generation until a response happens to fit.
The limit of this validation matters. Valid JSON and acceptable fields establish the shape of the data. They do not establish that Mira really belongs to the right guild or that the proposed story fits the world.

Referencing a character before it exists in the database
An existing record already has a permanent identifier (ID). A new character does not, but a relationship involving that character needs to be described in the same proposal.
The client_id field on an entity draft holds a temporary identifier. In our example, this field contains mira for Mira's draft. In the relationship draft, the source_client_id field contains mira, while the target_entity_id field contains the permanent ID of the existing Old Port record. If both entities are new, the relationship's source_client_id and target_client_id fields refer to the values in their client_id fields.
A separate match_entity_id field on an entity draft indicates that its information belongs to an existing record. A proposal can therefore introduce new content and extend known content at the same time.
During application, entities are processed first. The service creates records or updates the ones it finds, obtains their permanent IDs, and builds a mapping from temporary identifiers to permanent ones. It then uses that mapping to resolve relationship references.
The system also checks that referenced objects belong to the current world. An existing ID from another world must not accidentally become the endpoint of a relationship in Eon.
The LLM response path includes additional cleanup. An unknown ID invented for a supposedly existing entity can be recovered as a new draft entity. A relationship whose endpoints cannot be resolved may be dropped. This helps retain useful parts of the result, but it also means the original response and the final set of changes can differ. They need to be compared during review.

A proposal can be read before it is applied
After extraction, an instance of the ExtractionProposal class is saved with the source text, structured changes, and a pending status. This is a separate record that can be opened and reviewed. Saving the proposal does not itself create its world entities.
Enabling save to wiki in chat uses this path. It creates a proposal rather than applying the changes. If extraction fails, the ordinary model response still stands on its own: the API returns it with a separate wiki-save error.
The active draft interface shows the source text and proposed items. For entities, it identifies whether the intent is to create or update a record, with field comparisons for updates. Items also receive source excerpts to help explain where a proposal came from.
There is a limit to what these excerpts establish. If the source is a model response, quoting it helps trace the origin of an idea. It does not provide independent evidence that the idea is correct.
The editor allows fields on existing items to be changed and unwanted items to be removed before publication to the world database. I could refine Mira's description or remove a questionable relationship. However, the current editor does not expose arbitrary relationship endpoint changes or each item's verification status. Those controls should not be assumed to exist simply because the data schema supports the fields.
There is another implementation detail: new extractions can be merged into a proposal with a pending status. The active draft may therefore contain material from several sources. It needs a complete review before application, especially if new extractions have arrived since the previous review.
Saving information and verifying it are separate decisions
A proposal and its contents have different statuses.
A proposal can have a pending, applied, or rejected status. These statuses describe what happened to the change package. An entity or relationship has its own verification status: proposed, verified, unknown, or rejected.
As a result, an applied proposal status does not mean that every saved item automatically received a verified status. For example, a newly created record can retain a proposed or unknown status. The database can retain a proposed idea or an unknown fact that still needs a decision.
This distinction is useful for a fictional world. I can save a character draft to continue working on later without declaring every detail established canon. The decision to record information and the decision to treat it as established both need to be deliberate.
Secrecy is stored separately from these statuses. Information can have a verified status and still be intended only for the game master. Access control deserves its own detailed examination: the existence of an is_secret field is not enough to establish the security of the whole system.
What happens when changes are applied
Application starts by checking the proposal's state and contents again. The service then creates or updates entities, resolves references, and saves relationships and the remaining contents of the edited proposal. The changes are also linked to the proposal in the world's history. Application is allowed only while the proposal has a pending status: a repeat request for an already applied proposal receives HTTP status code 409.
The writes and the proposal's transition to the applied status are committed together in one call to the database session's commit() method. When an exception of the ProposalError class is caught, the rollback() method is called before the error is recorded separately. This is intended to prevent that failure path from leaving half the package successfully created while the other half remains unwritten.
The tests contain a specific scenario: an error occurs after an entity has been added and the database session's flush() method has been called. The test checks that the entity is absent after rollback, the proposal retains its pending status, and the error is saved. This is more informative than a broad claim that saving is reliable: it identifies the exact case the test covers.
For the PostgreSQL database system, the code also uses a world-level proposal-pipeline lock and a row lock on the proposal. The SQLite database system does not have the equivalent locking branch. An apply request contains the proposal ID, but not the version of the draft that was reviewed. Locks alone do not bind the game master's decision to the exact state they saw. This does not justify a universal claim of freedom from races or exactly-once behavior under every possible failure.
A more ordinary problem is repeated mentions of the same characters. The service matches names and aliases and looks for existing relationships with the same source, target, and type. This reduces repeated records, but a heuristic name match does not prove that two mentions describe the same character.
What the game master gets
The result in Mira's example can be reviewed piece by piece: whether a new NPC is needed, whether the location is correct, whether to keep the guild relationship, and which details should remain secret. After this review, the data can be applied to the world database.
The process gives the model useful work: proposing content and organizing it into a structure. The game master can see the specific changes before they are written.
Demiurge remains an experiment. Structured responses do not eliminate extraction errors, name matching does not solve every duplicate, and a stored status does not replace a human decision. But these problems can now be examined at the level of a particular record, relationship, and write operation.
This is the next step I need after text generation: a clear set of changes that can be reviewed, adjusted, and used in the continuing work on the world.