Adr log
Architecural Decision Record Log¶
This document helps keep track why certain decisions are made so that I don't have to keep everything in my head. It's pretty unstructured and basic for now because it's fairly early on in the project
- Routing keys:
We have these because we want to allow a source action to be able to have multiple outgoing edges. Routing keys help us distinguish edges with the same (sourceActionId, sinkActionId) tuple.
Some points to consider here are that the
- (sourceActionId, sinkActionId, routingKey) tuple is unique
- (sourceActionId, routingKey) is also unique. This ensures that we can't have multiple outgoing edges with the same routing key. Because then we wouldn't be able to track them correctly
-
(sinkActionId, routingKey) is not unique. This ensures that we can have multiple edges from the same source action go to the same sink action
-
Synthetic Ids:
Edges have a synthetic "id" primary key while they also have a unique constraint on (sourceActionId, sinkActionId, routingKey). The (sourceActionId, sinkActionId, routingKey) fields of an edge are immutable as well anyway. But having an id is easier to use for foreign keys, joins, conditions, etc. The unique constraint on (sourceActionId, sinkActionId, routingKey) is just for data integrity.
We have a similar synthetic id in the case of edge instances where we also have a unique constraint on (edge id, flow instance id, created_at). Made everything simpler to work with.
-
Tool Ids: The primary key of an tool is (id, version). We do this so that different versions of the same tool can have the same id.
-
Edge and Edge Instance Denormalization:
When a flow is being created, we expect that edges will often be created and deleted. Especially, since all outgoing edges are deleted when their corresponding sink edge are deleted. This creates a problem with edge instances. Edge instances have an "edge_id" which is a foreign key of the edge table. If a flow has been created and has had a few runs, there are edge instances with edge_ids. Now when said edge is deleted, we lose a lot of information about what ran at that edge because all our action information is also retrieved using the edge. Thus, we also store sourceActionId, sinkActionId and routing key on the edge instance itself. This ensures that even if the edge is deleted, we don't lose that information. We also only have soft deletes for actions. Thus, using the source and sink action id, we can easily get all the information about how an edge instance was executed.
-
Directed Graph Data Structure: We were pretty clear that we wanted to have a directed graph to represent flows. We these requirements that led to use choosing the particular representation we have right now:
-
Loops: We were likely to use Skemata to create conversational chat flows. Loops are super useful in those cases because conversations often loop back in chatbots. This led us to allowing cycles in the graph. We have a unique constraint on (edgeId, flowInstanceId, createdAt). We added the createdAt so we could keep track of multiple traversals of the same edge.
- Asynchronous Execution: We had a use case for sending messages where a message could have multiple states like pending, sent, delivered, read. We wanted the action to be able to keep track of these states. Thus we added a status field to edge instances. Namely, the "awaiting-updates" status. Each message state could then be mapped to a routing key. Thus, if we wanted to execute particular actions for a given state, that became possible
- Dynamic Routing Keys: In some cases, we wanted to route the flow to a particular node for a given input. Especially array inputs. Our use case in particular was sending messages with buttons. The number of buttons could be variable. But we wanted to execute particular actions for each specific button. Thus we came up with dynamic routing keys that are generated from a JSONPath based on the input button array. Thus a user could then connect say the edge associated with button one (routing key: button[0]) and reply with "Hi" and then for button two (routing key: button[1]) reply with "Bye" or something like that. Do note however, that we only allow a subset of JSONPath that we can derive the canonical JSONPaths for easily. So we disallow filters, recursive descent, etc and just allow the wildcard operator.
The result is a highly flexible system that can be adapted to run any type of workflow while having granular observability over executions.
-
JSON Validation: One of the goals of the project was to remove the guesswork from building flows. We want to have all the information about the execution of an action statically. We can also use this information to render a UI. We need to ensure that the data is valid and the action executors are respecting their schemas.
-
Edge Instance Logging: We use edge instances to keep track of flow executions. This ensures that we know exactly which action took place at a given edge. Along with the inputs it took and its output. The goal is to have a complete audit of the system. Ideally, all the actions are idempotent. So given the edge instance inputs, we should be able to run the flow idempotently as well. We wanted to make the system super easy to debug.
-
Input variables: There are some cases where the input of a given action relies on the output of another action. We want to be able to denote this easily. So we use JSONPath to do so. Let's say action A has output
action B can use the it as
- Tool Context