Metadata-Version: 2.4
Name: gama-decision-os
Version: 1.2.2
Summary: Open-core three-gate decision runtime for autonomous agents: pre-action risk, tool gating, and evidence receipts.
Author-email: GAMA <info@gamai.io>
License: Apache-2.0
Project-URL: Homepage, https://gamai.io
Project-URL: Repository, https://github.com/gamai-io/gama-decision-os
Project-URL: Issues, https://github.com/gamai-io/gama-decision-os/issues
Project-URL: Changelog, https://github.com/gamai-io/gama-decision-os/releases
Keywords: agents,safety,decision,calibration,evidence,llm,guardrails
Classifier: Development Status :: 5 - Production/Stable
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: Apache Software License
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Topic :: Software Development :: Libraries
Classifier: Topic :: Software Development :: Libraries :: Python Modules
Requires-Python: >=3.9
Description-Content-Type: text/markdown
License-File: LICENSE
License-File: LICENSE-ENTERPRISE.md
Provides-Extra: test
Requires-Dist: pytest>=7; extra == "test"
Provides-Extra: calibration
Requires-Dist: scipy>=1.10; extra == "calibration"
Provides-Extra: yaml
Requires-Dist: PyYAML>=5.4; extra == "yaml"
Dynamic: license-file

# GAMA Decision OS

[![PyPI](https://img.shields.io/pypi/v/gama-decision-os.svg)](https://pypi.org/project/gama-decision-os/)
[![License: Apache-2.0](https://img.shields.io/badge/license-Apache--2.0-blue.svg)](LICENSE)
[![Python](https://img.shields.io/pypi/pyversions/gama-decision-os.svg)](https://pypi.org/project/gama-decision-os/)

**An open-core control plane for autonomous agents: decide before acting, constrain execution, and require evidence that the action actually happened.**

GAMA Decision OS sits between your agent and its tools. It answers three questions that most agent stacks leave to the model:

1. **Should this action run at all?** Risk, intent/tier, confidence and the evidence that will be required.
2. **How should it run?** Allow, sandbox, ask a human, or deny.
3. **Did it actually work?** A verifiable evidence receipt instead of the model's own `ok: true`.

The core is licensed **Apache-2.0**. Enterprise capabilities are licensed separately; see [Open-core](#open-core).

- Website: https://gamai.io
- Contact: info@gamai.io

---

## Three gates

| Gate | Entry point | What it does |
| --- | --- | --- |
| **Gate 1 — pre-action** | `decide_request(text)` | Deterministic precheck plus calibrated classification into `READ`, `DRAFT`, `SEND`, `IRREVERSIBLE`. Produces a decision envelope with `verdict`, `route`, `confidence`, `expected_evidence_spec`, `decision_hash`. |
| **Gate 2 — in-action** | `decide_tool(tool, args, ctx)` | Tool-level gate: `allow`, `sandbox`, `ask`, `deny`. Blocks mutations in `READ`, blocks unrequested destructive shell and mail calls. |
| **Gate 3 — post-action** | `verify_evidence(envelope, tool, result)` | Checks the expected evidence contract and emits a hashed `EvidenceReceipt`. Verifies deletes on disk, SQL rows, external sends, code changes. |

```
agent ──▶ decide_request ──▶ decide_tool ──▶ tool ──▶ verify_evidence ──▶ done / rollback
```

## Install

The core has **no third-party runtime dependencies** (Python standard library only).

```bash
pip install gama-decision-os
```

From a checkout (editable, for development):

```bash
python3 -m pip install -e .
python3 -m pytest tests/ -q
```

Requirements: Python 3.9+.

The wheel also installs a `gama-decision` command that dispatches to the bundled
tools (`gama-decision mcda`, `gama-decision ledger`, `gama-decision eval`,
`gama-decision calibration`, `gama-decision dpo-near-miss`, `gama-decision dpo-dataset`).

## Quick start

```python
from gama_decision import decide_request, decide_tool, verify_evidence

envelope = decide_request("delete /tmp/report.txt")
print(envelope["tier"], envelope["verdict"], envelope["expected_evidence_spec"])

decision = decide_tool("delete_file", {"path": "/tmp/report.txt"}, envelope)
receipt = verify_evidence(decision, "delete_file", {"ok": True, "verified": True, "exists": False})
print(receipt["verified"], receipt["receipt_hash"])
```

See [`examples/`](examples/) for a Python client and curl examples.

## Decision framework (MCDA)

The three gates answer "may this run?" and "did it happen?". They do not answer
"which of these options should we run?". `gama_decision/mcda.py` adds that layer:
explicit options, weighted criteria, a sensitivity check and one falsifier per option.

**This is a layer above the L0 gate, not a replacement.** MCDA picks the option;
`decide_request` / `decide_tool` / `verify_evidence` still gate every action that
the chosen option produces.

```python
from gama_decision.mcda import decide

result = decide({
    "id": "portfolio_90d",
    "criteria": [
        {"id": "cash", "weight": 0.6},
        {"id": "proof", "weight": 0.4},
    ],
    "options": [
        {"id": "A", "scores": {"cash": 4, "proof": 1}},
        {"id": "B", "scores": {"cash": 2, "proof": 5}},
    ],
    "falsifiers": {
        "A": "no 2 paid pilots in 30 days",
        "B": "no reproducer passing in CI by 2027-01-15",
    },
})
print(result["winner"], result["margin"], result["warnings"])
```

```bash
python3 -m gama_decision.mcda score    --input examples/portfolio_90d.yaml --json
python3 -m gama_decision.mcda validate --input examples/portfolio_90d.yaml
python3 -m gama_decision.mcda example
```

The real 90-day portfolio case lives in [`examples/portfolio_90d.yaml`](examples/portfolio_90d.yaml):
B (Anna) scores 3.60, C (proofgate) 3.15, A (Floor Copilot) 2.50, D (status quo) 1.40;
margin 0.45, and the winner does not flip for any single weight shift of ±0.2.
`validate` fails a decision that has no falsifier, a score outside 1..5, or weights
that do not sum to 1 (±1e-6). YAML input uses PyYAML when installed; otherwise the
CLI tells you to install it or use JSON. JSON always works with zero dependencies.

## Decision ledger

`gama_decision/ledger.py` closes the loop from decision to outcome. `log()` appends the
MCDA result to a hash-chained JSONL file (`decisions.jsonl` by default; override with
`GAMA_DECISION_LEDGER` or `--ledger`). `resolve()` later records whether the winner was
verified or its falsifier fired, without editing the original record. `stats()` reports, per
decision, confirmed vs rejected, calibration split by margin (`< 0.5` vs `>= 0.5`), and
decisions still unresolved after N days (default 90). `verify_chain()` recomputes every
SHA-256 and `prev_sha256`, so one edited byte fails verification. Standard library only.

```bash
python3 -m gama_decision.ledger log     --input examples/portfolio_90d.yaml --json
python3 -m gama_decision.ledger resolve --decision-id portfolio_90d_2026-12-04 \
    --option B --outcome verified --evidence "grant terms signed 2026-12-10" --json
python3 -m gama_decision.ledger stats   --json
python3 -m gama_decision.ledger verify  --json
```

## What is in the core

- `gama_decision/decision_l0.py` — deterministic rules, tiering, calibrated probabilities, session slot filling, speculative override hook, tool gate, evidence gate.
- `gama_decision/mcda.py` — MCDA layer above the gate: options, weighted criteria, ranking/margin, weight sensitivity with break-even points, falsifier testability.
- `gama_decision/ledger.py` — append-only, hash-chained decision log: log the winner and falsifiers, resolve outcomes later, calibration by margin bucket, chain verification.
- `gama_decision/decision_context.py` — dependency-free repository/context compression (optional `gama_repomap3` backend when installed).
- `gama_decision/calibration_evaluator.py` — ECE and Brier score with a pure-Python fallback.
- `gama_decision/eval_adapter_ece.py` — adapter evaluation and temperature-scaling calibration (optional `scipy`/`torch` backends).
- `gama_decision/dpo_near_miss.py`, `dpo_dataset_generator.py` — near-miss DPO dataset generation from receipt logs.
- `tests/` — decision, speculative, context and calibration tests.

## Open-core

| | Core (Apache-2.0) | Enterprise |
| --- | --- | --- |
| Three-gate runtime | Yes | Yes |
| Evidence receipts | Yes | Yes |
| Calibration tooling | Yes | Yes |
| Compliance reporting / retention | — | Yes |
| SSO / SCIM, multi-tenancy | — | Yes |
| ERP / database connectors | — | Yes |
| Signed releases, SLA, support | — | Yes |

The enterprise license template is in [`LICENSE-ENTERPRISE.md`](LICENSE-ENTERPRISE.md). Core code is governed only by Apache-2.0.

## Security

- No secrets are required to run the core.
- All network features are opt-in via environment variables.
- Report vulnerabilities per [`SECURITY.md`](SECURITY.md).

## Documentation

- [`docs/GAMA_DECISION_OS_MANIFESTO.md`](docs/GAMA_DECISION_OS_MANIFESTO.md) — design principles.
- [`docs/SITE_ARCHITECTURE.md`](docs/SITE_ARCHITECTURE.md) — public site and information architecture.
- [`docs/BENCHMARK.md`](docs/BENCHMARK.md) — reproducible benchmark notes and caveats.
- [`docs/BENCHMARK_LUMI_10K.md`](docs/BENCHMARK_LUMI_10K.md) — LUMI-G 10k async agents: 10.000/10.000, 0 grešaka, 0 retry (4× MI250X; 2026-11-15). Provjera: `python3 benchmarks/lumi_10k/verify_stress.py` (čita `results/stress_v5_*.json`).
- [`docs/BENCHMARK_COMPETITIVE.md`](docs/BENCHMARK_COMPETITIVE.md) — competitive benchmark vs agent control-plane / guardrails / observability market (2026-11-13).

## License

Apache License 2.0. See [`LICENSE`](LICENSE).

## Brojevi (jedan izvor istine)

Svi brojevi u dokumentaciji i na sajtu dolaze iz `config/FACTS.json` u internom repou:

| Stavka | Vrijednost | Kako je izmjereno |
|---|---|---|
| Alati (katalog) | **398** | `len(all_tools())` |
| Alati (runtime prompt) | **296** | `len(TOOLS)` |
| Vještine (biblioteka) | **5.024** | direktoriji u `~/.agents/skills` |
| Persone | **409** | `gama_persona_ondemand.list_personas()` |
| Baseline šema | **53.226 tokena** | sve šeme prije kompresije |
| Prosječno šema (live) | **2.312 tokena** | `_token_ledger.db`, 24 h |
| Ušteda (live, 24 h) | **85,4 %** (n=3.252) | `python3 benchmarks/ledger_stats.py` (token_ledger, 2026-11-26) |
| Ušteda (7 dana) | **78,8 %** (n=14.669) | isto; projekcija do 93 % važi samo za veći udio ponavljanja |
| Short-circuit | **0,6 %** upita (24 h) · **2,4 %** (7 d) | isto. Ranije navedenih „28 %" **nije reproducirano** |
| L0 odluka | **0,0025 ms** p50 (READ) · 0,0021 (IRREVERSIBLE) · 0,0022 (SEND) | `python3 benchmarks/decision_latency.py --n 500`, 0 pogrešnih odluka, 2026-11-26 |
| ECE (kalibracija, dry-run) | **0,407** (Brier 0,317, accuracy 0,643, n=42) | `python3 -m gama_decision.eval_adapter_ece --dry-run` (2026-11-23). Broj sa adapterom (0,2973 → 0,0347) traži GPU + adapter i **nije reproduciran ovdje** |

> **Napomena o brojevima iz žive baze:** `token_ledger` raste, pa `ledger_stats.py`
> danas vraća drugačiji `n` i procenat od onog u tabeli. Brojevi u tabeli su **snimak na
> navedeni datum** i nisu CI-provjerljivi; provjerljiva je komanda, ne vrijednost.
> „Short-circuit“ je 0,6 % (24 h) i 2,4 % (7 d); ranije je pisalo 7,2 % — udio zavisi od
> sastava saobraćaja u prozoru i ne treba ga čitati kao fiksnu osobinu sistema.

### Nije reproducirano u ovom repou
Sljedeće je mjereno interno, ali **artefakt nije objavljen** — tretirati kao nepotvrđeno dok se ne doda skripta:
`Tier model 40/40 (100%)` · `Tool paging 97,3 %` · `Code Mode −82,3 % tokena`

## Šta je novo (2026-11-15)

| Sloj | Rezultat | Kako je izmjereno |
|---|---|---|
| **Code Mode** (kod umjesto tool-callova) | **−82,3 %** tokena na 3 koraka (−70,6 % na 1, −89,2 % na 10) | bench, 5 realnih multi-step zadataka, 40 alata |
| **Deterministički obrasci** | **10 obrazaca = 27 %** stvarnog prometa, 0 tokena | regex na 318 stvarnih upita |
| **Canary ramp** | 5 % (shadow mjeri prije 25 % / 100 %); sve uštede aktivne na **6 čvorova** (16 flagova) | hash bucket po (sid, obrazac) |
| **Gated semantički keš** | 89 % hit, **USR = 0 %** (20/20 trapova blokirano) | bench 200 upita |
| **Exact cache na ingress** | −13,8 % ponovljenih poziva (prije se gubilo) | live, cache hit verifikovan |
| **Dedup tool rezultata** (TOOL_RESULT_DEDUP) | **0–37 %**, zavisno od ponavljanja: 0 % bez ponavljanja, 19,4 % na 10 %, 24,2 % na 25 %, 36,9 % na 40 % | bench sa realnim, unikatnim sadržajima (pošteno mjereno, bez lažnih dedupova) |
| **Token ledger po fazi** (TOKEN_PHASE_V1) | ingress / loop / chat odvojeno; prvi snimak 26 zapisa, 8.670 prompt, 1.502.633 ušteđenih tokena | token ledger (SQLite), izvještaj po fazi i modelu |
| **Audit popravke** (Claude + Codex) | decision cache scopiran po sid (sigurnosni), exact cache u stream putu, output budget u streamu, paging 4k / truncate 6k, Code Mode timeout guard | 28 testova u ovom repou: 27 prošlo, 1 skip bez `GAMA_SPECULATIVE_URL` (`python3 -m pytest -q tests`); deploy na svih 6 čvorova |
| **Brain v2** | 100.222 zapisa, sync 60 s, opoziv + stale detekcija | `/api/stats` |
| **Javna verifikacija dokaza** | `https://gamai.io/api/verify?hash=...` → `chain_ok=true` | live curl |

**Sigurnosne ograde Code Modea:** samo READ/DRAFT tier, AST validacija (bez importa/atributa/lambde),
max 40 linija i 10 poziva, automatski fallback na normalan tok ako kod ne prođe.
