|
| 1 | +#!/usr/bin/env logica |
| 2 | +# |
| 3 | +# Copyright 2026 Logica Authors |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | + |
| 17 | +@Engine("sqlite"); |
| 18 | + |
| 19 | +# A tiny demo of "team contracts": promises vs requirements. |
| 20 | +# The output is a list of objectively checkable violations. |
| 21 | + |
| 22 | +Team("billing"); |
| 23 | +Team("risk"); |
| 24 | +Team("analytics"); |
| 25 | + |
| 26 | +System("charge_api"); |
| 27 | +System("events_stream"); |
| 28 | + |
| 29 | +Owns("billing", "charge_api"); |
| 30 | +Owns("analytics", "events_stream"); |
| 31 | + |
| 32 | +# Promises: what the owner team claims to provide. |
| 33 | +# Example: latency_p95_ms <= 200 |
| 34 | +PromiseSla("billing", "charge_api", "latency_p95_ms", 200); |
| 35 | +PromiseSla("analytics", "events_stream", "delivery_lag_p95_s", 60); |
| 36 | + |
| 37 | +# Requirements: what a consumer team needs. |
| 38 | +NeedSla("risk", "charge_api", "latency_p95_ms", 150); |
| 39 | +NeedSla("billing", "events_stream", "delivery_lag_p95_s", 10); |
| 40 | + |
| 41 | +# Schema promises/requirements (simplified). |
| 42 | +PromiseFieldType("billing", "charge_api", "amount", "Int64"); |
| 43 | +NeedFieldType("risk", "charge_api", "amount", "Float64"); |
| 44 | + |
| 45 | +# Violations. |
| 46 | +SlaViolation(consumer, owner, system, metric, need, promise) :- |
| 47 | + NeedSla(consumer, system, metric, need), |
| 48 | + Owns(owner, system), |
| 49 | + PromiseSla(owner, system, metric, promise), |
| 50 | + need < promise; |
| 51 | + |
| 52 | +MissingPromise(consumer, owner, system, metric, need) :- |
| 53 | + NeedSla(consumer, system, metric, need), |
| 54 | + Owns(owner, system), |
| 55 | + ~PromiseSla(owner, system, metric, _); |
| 56 | + |
| 57 | +TypeViolation(consumer, owner, system, field, need_type, promise_type) :- |
| 58 | + NeedFieldType(consumer, system, field, need_type), |
| 59 | + Owns(owner, system), |
| 60 | + PromiseFieldType(owner, system, field, promise_type), |
| 61 | + need_type != promise_type; |
| 62 | + |
| 63 | +Q(kind, consumer, owner, system, subject, expected, actual) :- |
| 64 | + SlaViolation(consumer, owner, system, metric, need, promise), |
| 65 | + kind == "sla", |
| 66 | + subject == metric, |
| 67 | + expected == ToString(need), |
| 68 | + actual == ToString(promise); |
| 69 | + |
| 70 | +Q(kind, consumer, owner, system, subject, expected, actual) :- |
| 71 | + MissingPromise(consumer, owner, system, metric, need), |
| 72 | + kind == "missing_promise", |
| 73 | + subject == metric, |
| 74 | + expected == ToString(need), |
| 75 | + actual == "(no promise)"; |
| 76 | + |
| 77 | +Q(kind, consumer, owner, system, subject, expected, actual) :- |
| 78 | + TypeViolation(consumer, owner, system, field, need_type, promise_type), |
| 79 | + kind == "type", |
| 80 | + subject == field, |
| 81 | + expected == need_type, |
| 82 | + actual == promise_type; |
| 83 | + |
| 84 | +@OrderBy(Q, "col0"); |
0 commit comments