Writing Kindred rules: instructions for a coding agent
You are helping a person write the transformation rules of a Kindred config. A rule turns one raw record from a target system into one Kindred event. You write the rules. Two Kindred commands check them. The person decides what the rules mean. This page tells you what you need. Follow it in order.
Rules for you
- Do not guess what a status code means. The meaning comes from the vendor's documentation and from the people at the site. If the person has not told you, ask. Do not infer it from a sample.
- Do not handle a credential. Never write, print or ask for a password, an API key or a token, and never put one in a file you can open. If a rule needs one, stop and tell the person. They set it on the appliance, through the draft route of the management API, which stores it encrypted and never shows it back.
- A sample file holds site data, and you can read it. Synthetic records are the safe choice. If the records are real, the person should use a model the site controls. If you cannot tell whether they are real, ask before you open the file.
- Do not say the checks prove a mapping right. They do not. See What the checks do not prove.
- Do not reach a target system. Never add
--connect,--show-sampleor--db-sampleto a command. The person decides whether to test a connection. - The output of a command can quote a value. The preview report holds names and counts only. A schema message of the offline check can quote a value that has the wrong type. Treat what a command prints as site data.
The rule model
A config holds its rules inside one connector section: opcua, mqtt, database_log or rest. The Node Type
decides which sections a config can have.
state_transition_rulesis a list of rules. Each rule has three parts.whenis{source key: value}. The rule applies to a record only if every entry matches.thenis{Kindred field: source key}. It copies the record's value into the event.literalsis{Kindred field: constant}. It sets a fixed value in the event.
- Rules are tried in order. The first rule whose
whenmatches is the only one used. - A rule whose
whenholds every entry of an earlier rule'swhen, and more, is never reached. Put the narrower rule first. The preview reports a rule that is never reached. field_mapis{Kindred field: source key}. It renames fields and does nothing else. Use it when every record already means what the Kindred fields mean.- If
state_transition_rulesis not empty,field_mapis ignored. Write one or the other. - A
thenthat names a key the record does not hold givesNone. It is not an error. - Every source key the section declares (
source_keysfor MQTT) must be read by somewhenorthen. If one is not, the offline check fails. - The Kindred field names are the Node Type's. Read them from the
event_typesin the Node Type'snode_type.jsonundernode-types/in the Kindred checkout. Do not invent a field. Where the list gives the allowedstatusvalues, use only those. For MES, astatusofflaggedmakes the preview report a flagged event and exit 1, so use it only if the person wants that.
How when matches
A when entry matches only when the key is present and its value is equal.
4and"4"are different. A rule for one never matches the other. Copy the type from the sample: a number in the sample is a number in the rule.- A key that is not in the record never matches, even an entry whose value is
null.{"error_code": null}matches a record that holds"error_code": null. It does not match a record with noerror_code. 1andtrueare the same value: a rule for one matches a record that holds the other, and the preview treats them as the same when it looks for a rule that is never reached. Write the type the sample holds.- An empty
when,{}, matches every record, so every rule after it is never reached.
The catch-all
A catch-all is the last rule, with when set to {}.
- Without one, a record that matches no rule passes through unchanged, and the preview reports it as unmatched.
It also prints a note that no rule has an empty
when. The note is not a gap and does not change the exit code. It does not mean you must add one. - It must be last. A rule after it is never reached, and the preview reports that.
- With one, no record is unmatched. That hides a rule that never matches, such as a code written with the wrong type: the record falls into the catch-all and the preview says nothing.
- So preview first without the catch-all, then add it. Whether to have one, and what it labels, is the person's choice. Ask.
Records built from separate OPC UA values
A source that sends a whole record at once, such as a message or a database row, has none of the gaps in this
section. The MES Node builds a record from separate OPC UA values when its opcua section lists extra_fields.
Each value is a node that reports on its own.
- The first value each node reports is kept. It starts no event.
- Nothing is sent until the node that gives the identity value, reported under the key
value, has a value. - After that, a change in any one node sends the whole record as kept, not only the value that changed.
Two gaps follow. Neither is bad data.
- Cold start. The record holds only what nodes have reported. A key the record does not hold yet gives
Nonein athen. - Stale field. A rule can match on a change in one node while another key still holds an older value. If the target system does not update its values together, a rule can read a value from before the change.
Write such a when so that it does not depend on two keys changing at the same moment. The preview cannot show
either gap, because a sample file is a list of whole records. Tell the person about them.
The loop
Write, check, preview, read the report, fix. Repeat until both commands exit 0.
The person gives you the Python that can import the engine and the node-types/ directory of the Kindred checkout.
Below, python3 and node-types stand for them. A Python that cannot import the engine prints
No module named 'engine' and exits 1, which is the same code as an invalid config. If you see that message, the
interpreter is wrong and the config has not been judged.
python3 -m engine.offline_check_cli CONFIG NODE_TYPES
python3 -m engine.mapping_preview_cli CONFIG NODE_TYPES SAMPLE [--identity-field FIELD] [--section SECTION]
- A PLC or ERP config needs
--identity-field FIELDfor the preview. MES and Production Scheduling do not. - Add
--sectionif the config has more than one connector section. - Read the exit code to decide. Do not parse the printed text. It can change between releases.
The examples below are synthetic. Each runs from the repository root of a Kindred checkout, in the order shown.
In a configuration repository, run the same commands with the Python and the node-types/ path the person gives you.
The setup block writes a sample of three records, where state_code 1 is in progress, 4 is complete and 9 is a
code no rule is written for yet. It writes a first draft with a mistake in it.
WORK=$(mktemp -d)
cat > "$WORK/sample.json" <<'EOF'
[
{"order_ref": "SYN-1", "state_code": 1},
{"order_ref": "SYN-2", "state_code": 4},
{"order_ref": "SYN-3", "state_code": 9}
]
EOF
cat > "$WORK/draft.json" <<'EOF'
{
"node_type": "MES",
"node_type_version": "0.7.0",
"config_version": 1,
"config": {
"mqtt": {
"host": "192.0.2.10",
"subscriptions": [{ "topic": "example/synthetic/#" }],
"source_keys": ["order_ref", "state_code"],
"state_transition_rules": [
{ "when": { "state_code": 1 }, "then": { "order_id": "order_ref" }, "literals": { "status": "in-progress" } },
{ "when": { "state_code": "4" }, "then": { "order_id": "order_ref" }, "literals": { "status": "completed" } }
]
}
}
}
EOF
1. Check the draft, then preview it. The offline check passes: the shape is right. The preview does not.
python3 -m engine.offline_check_cli "$WORK/draft.json" node-types
python3 -m engine.mapping_preview_cli "$WORK/draft.json" node-types "$WORK/sample.json"
Offline check: the config is valid (Node Type MES).
Mapping preview: gaps found (Node Type MES, section mqtt).
records: 3 (1 matched, 2 unmatched, 0 flagged)
record 1: unmatched
record 2: unmatched
note: no rule has an empty when, so a record no rule matches passes through unchanged
2. Read the report. Records are counted from 0, in the sample file. The report never prints a value, so read
the sample. Record 2 has code 9, and no rule is written for it yet. Record 1 has code 4, and a rule is written for
it, so that rule is wrong. The sample holds 4 as a number and the rule holds "4" as text. The note at the
end says the draft has no catch-all. Step 3 adds one.
3. Fix it. Write the code as a number. Add a catch-all for code 9, after asking the person what it should
label. Here the person chose at-risk. Then check and preview again.
cat > "$WORK/final.json" <<'EOF'
{
"node_type": "MES",
"node_type_version": "0.7.0",
"config_version": 1,
"config": {
"mqtt": {
"host": "192.0.2.10",
"subscriptions": [{ "topic": "example/synthetic/#" }],
"source_keys": ["order_ref", "state_code"],
"state_transition_rules": [
{ "when": { "state_code": 1 }, "then": { "order_id": "order_ref" }, "literals": { "status": "in-progress" } },
{ "when": { "state_code": 4 }, "then": { "order_id": "order_ref" }, "literals": { "status": "completed" } },
{ "when": {}, "then": { "order_id": "order_ref" }, "literals": { "status": "at-risk" } }
]
}
}
}
EOF
python3 -m engine.offline_check_cli "$WORK/final.json" node-types
python3 -m engine.mapping_preview_cli "$WORK/final.json" node-types "$WORK/sample.json"
Offline check: the config is valid (Node Type MES).
Mapping preview: no gaps found (Node Type MES, section mqtt).
records: 3 (3 matched, 0 unmatched, 0 flagged)
Both commands exit 0, and the config can still be wrong. Put the draft's mistake back, with the catch-all in place:
sed 's/"state_code": 4 }/"state_code": "4" }/' "$WORK/final.json" > "$WORK/hidden.json"
python3 -m engine.mapping_preview_cli "$WORK/hidden.json" node-types "$WORK/sample.json"
Mapping preview: no gaps found (Node Type MES, section mqtt).
records: 3 (3 matched, 0 unmatched, 0 flagged)
Record 1 now falls into the catch-all and is labelled at-risk, and the report says nothing. This is why you
preview without the catch-all first.
A status of flagged in the catch-all makes the preview report a flagged event:
sed 's/"at-risk"/"flagged"/' "$WORK/final.json" > "$WORK/flagged.json"
python3 -m engine.mapping_preview_cli "$WORK/flagged.json" node-types "$WORK/sample.json"
Mapping preview: gaps found (Node Type MES, section mqtt).
records: 3 (3 matched, 0 unmatched, 1 flagged)
record 2: flagged
A catch-all that is not last takes every record, and the rules after it are never reached. Put it first by mistake:
sed 's/"when": { "state_code": 1 }/"when": {}/' "$WORK/final.json" > "$WORK/dead.json"
python3 -m engine.mapping_preview_cli "$WORK/dead.json" node-types "$WORK/sample.json"
Mapping preview: gaps found (Node Type MES, section mqtt).
records: 3 (3 matched, 0 unmatched, 0 flagged)
rule 1 is never reached: rule 0 matches every record it would
rule 2 is never reached: rule 0 matches every record it would
Rules are counted from 0, in the order of the list, as the offline check counts them in
state_transition_rules.2.when. The report names a rule by its position and never prints what is in it. Open the
config to see it.
What the commands tell you
The preview reports seven kinds of gap. Any one of them makes it exit 1.
- A source key that nothing reads.
- A rule that no record can reach, because an earlier rule's
whenis contained in its own. Reorder the rules, or delete the rule. - A record that no rule matches.
- A Kindred field left empty because the record had no key for it.
- A key in a record that no rule reads. Cut such a key from a copy of the sample, or add a rule that reads it.
- A required event field that is empty.
- A flagged event.
The note about a missing catch-all is not a gap. A preview that prints it and finds nothing else exits 0.
The offline check fails when the config's shape is wrong. Here the catch-all's when is a list, and it must be
an object. A schema message names the place and can quote the value that failed:
sed 's/"when": {}/"when": []/' "$WORK/final.json" > "$WORK/invalid.json"
python3 -m engine.offline_check_cli "$WORK/invalid.json" node-types
Offline check: the config is invalid.
schema: config: config invalid at mqtt.state_transition_rules.2.when: [] is not of type 'object'
A file that cannot be read exits 2, and so does a Node Type directory that cannot be used:
python3 -m engine.offline_check_cli "$WORK/absent.json" node-types
Offline check: the config could not be checked.
unreadable: the config could not be read
Exit codes
| Command | Code | Meaning | What you do |
|---|---|---|---|
| Offline check | 0 | The config is valid. | Go on to the preview. |
| 1 | The config is invalid. | Fix the config. | |
| 2 | The check could not run: a usage error, an unreadable file or a Node Type directory that cannot be used. | Fix the command. The config was not judged. | |
| 5 | No verdict: the file is encrypted and no age key is available. | Stop. Tell the person. | |
| Mapping preview | 0 | No gaps found. A note about a missing catch-all can still be printed. | Tell the person what you wrote and what you could not check. |
| 1 | A gap or a flagged event was found. A rule that is never reached is a gap. | Read the report and fix. | |
| 2 | The inputs could not be used. | Fix the command. The mapping was not judged. |
The offline check has two more codes, 3 and 4, and only --connect gives them. You do not use --connect.
What the checks do not prove
A config can pass both commands and still be wrong. The commands find a rule set that cannot be loaded, a key that nothing reads, a rule that is never reached and a record that nothing matches. They do not know the following.
- What a code means. That
4is a finished cycle comes from the vendor's documentation and from the people at the site. A rule that says otherwise passes both commands. - Whether the sample is complete. A code that is not in the sample is not tested. Ask the person for the codes the target system can send, and check that each has a rule or falls into the catch-all on purpose.
- Whether the target system sends what the sample shows. Neither command connects to it.
- How a record is built from separate values. See the section above.
When both commands exit 0, say so, and then say what you could not check. Give the person the rules and the meaning you used for each code, so that they can compare them with what the site told them.
Release: v0.11.0