Reversible cancellation and normalization with audit trails; fail-loud on contradiction
Project description
muqabalah (المقابلة) — Reversible Cancellation + Normalization
Part of the Mizan stack — the Arabic-first reliability scale for AI agents.
The Balance operation as a standalone primitive. Companion to jabr. Reversible. Audited. Fail-loud on contradiction.
What it does
Takes a prompt with duplications, redundancies, or contradictions. Produces a canonical form with a full audit trail. The original is recoverable.
Critically: contradictions are never silently resolved. If two phrases conflict and one would have to be chosen over the other, balance() raises CancellationConflict and surfaces both options to the caller.
from muqabalah import balance, unbalance, CancellationContext
ctx = CancellationContext(
user_normalizations={"u.s.": "United States", "usa": "United States"},
contradiction_predicates=[("alice", "bob")], # detect conflict
)
# Duplication + normalization
result = balance("I live in u.s. I live in u.s.", ctx)
print(result.output)
# I live in United States.
assert unbalance(result.output, result.trace) == "I live in u.s. I live in u.s."
# Contradiction — raises
try:
balance("send to alice and send to bob", ctx)
except CancellationConflict as e:
print(e.conflicts) # → [{"predicate_a": "alice", "predicate_b": "bob", ...}]
Design properties
-
Reversibility.
unbalance(balance(p, ctx).output, trace) == pwheneverbalancesucceeds. Verified by 19 tests. -
Fail-loud on conflict. When contradictions are detected,
balanceraisesCancellationConflictwith all conflicting spans. The library never picks a side silently. -
Trace integrity. Every removal records the exact removed text, span, and rationale.
unbalanceverifies both per-entry kept-text and a finalinput_hashround-trip. Wrong traces raiseCancellationError. -
Determinism. Given the same
(prompt, context, detectors), byte-identical output and trace.
Why fail-loud matters
Modern agent pipelines silently resolve contradictions all the time. A user says "send to A; also send to B" and the agent just picks one. The user never knows. The audit log shows only the chosen action.
muqabalah makes contradiction explicit. The agent must decide — and the decision is recorded in a separate step, not buried inside an opaque chain-of-thought.
Built-in detectors
| Detector | Behavior |
|---|---|
ContradictionDetector |
Reports conflicts (no silent resolution); runs first |
NormalizationDetector |
Replaces user-defined non-canonical forms with canonical ones |
DuplicateDetector |
Removes literal duplicate sentences (keeps first occurrence) |
Add a custom detector:
from muqabalah import Detector, DetectorResult, CancellationContext
class MyDetector:
name = "my_detector"
def find(self, input: str, context: CancellationContext) -> DetectorResult:
# Return DetectorResult(actions=[...], conflicts=[...])
...
CLI
# Balance
muqabalah balance --prompt "..." --context ctx.json
# Round-trip verify
muqabalah roundtrip --prompt "Hello. Hello."
# Reverse balance to original
muqabalah unbalance --output "..." --trace-file trace.json
Where ctx.json is:
{
"user_normalizations": {"u.s.": "United States"},
"contradiction_predicates": [["alice", "bob"]]
}
Install
pip install -e .
Tests
pytest tests/ -v
19/19 pass on Python 3.10+. No runtime dependencies.
What it is not
- Not a paraphraser.
muqabalahonly removes, replaces, or surfaces conflicts. It never rewrites for "clarity" or "tone." - Not a fact-checker. It detects self-contradictions declared by the caller in
contradiction_predicates. It does not call out to a knowledge base. - Not a deduplicator for non-sentences. The default
DuplicateDetectorworks on sentences. For other granularities, write a custom detector.
Composition with jabr
jabr and muqabalah are designed to compose:
from jabr import restore
from muqabalah import balance
# Pipeline: restore → balance
restored = restore(prompt, restoration_ctx)
balanced = balance(restored.output, cancellation_ctx)
# Both reversible:
from jabr import unrestore
from muqabalah import unbalance
recovered = unrestore(unbalance(balanced.output, balanced.trace), restored.trace)
assert recovered == prompt
Failure modes
See FAILURES.md.
License
MIT.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file muqabalah-0.1.0.tar.gz.
File metadata
- Download URL: muqabalah-0.1.0.tar.gz
- Upload date:
- Size: 16.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0f7682ec353cc0ef3a875e084cfcc1b7a01049102df8d9fbe6e7d64eca364500
|
|
| MD5 |
79023a07d6bd2fbf8f067a405664c06c
|
|
| BLAKE2b-256 |
ff539e70c7bbe7a9e2e26bc2a9d63c6bbaab478f120bbba58f7b14c544daca18
|
Provenance
The following attestation bundles were made for muqabalah-0.1.0.tar.gz:
Publisher:
release.yml on Moshe-ship/muqabalah
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
muqabalah-0.1.0.tar.gz -
Subject digest:
0f7682ec353cc0ef3a875e084cfcc1b7a01049102df8d9fbe6e7d64eca364500 - Sigstore transparency entry: 1688006570
- Sigstore integration time:
-
Permalink:
Moshe-ship/muqabalah@d20689c7fd2629cc181c89d4589e064cd74e2938 -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/Moshe-ship
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@d20689c7fd2629cc181c89d4589e064cd74e2938 -
Trigger Event:
push
-
Statement type:
File details
Details for the file muqabalah-0.1.0-py3-none-any.whl.
File metadata
- Download URL: muqabalah-0.1.0-py3-none-any.whl
- Upload date:
- Size: 13.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6e7535b260a340ac0a10258735a7b1eaf7fdeb1b85d70e86a861c6333dc763a3
|
|
| MD5 |
7e0b6576ae8271b93845d347c365f050
|
|
| BLAKE2b-256 |
52dcdb896ffbf3d2ffdb27e32b1d1dc09658031f52576d3b655df4acaf90f8ef
|
Provenance
The following attestation bundles were made for muqabalah-0.1.0-py3-none-any.whl:
Publisher:
release.yml on Moshe-ship/muqabalah
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
muqabalah-0.1.0-py3-none-any.whl -
Subject digest:
6e7535b260a340ac0a10258735a7b1eaf7fdeb1b85d70e86a861c6333dc763a3 - Sigstore transparency entry: 1688006599
- Sigstore integration time:
-
Permalink:
Moshe-ship/muqabalah@d20689c7fd2629cc181c89d4589e064cd74e2938 -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/Moshe-ship
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@d20689c7fd2629cc181c89d4589e064cd74e2938 -
Trigger Event:
push
-
Statement type: