leangage 0.1.1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (42) hide show
  1. leangage-0.1.1/.gitignore +11 -0
  2. leangage-0.1.1/PKG-INFO +16 -0
  3. leangage-0.1.1/README.md +5 -0
  4. leangage-0.1.1/pyproject.toml +30 -0
  5. leangage-0.1.1/src/leangage/__init__.py +39 -0
  6. leangage-0.1.1/src/leangage/engine.py +125 -0
  7. leangage-0.1.1/src/leangage/gen/http_format.py +2798 -0
  8. leangage-0.1.1/src/leangage/gen/json_format.py +1776 -0
  9. leangage-0.1.1/src/leangage/gen/json_lexemes.py +2311 -0
  10. leangage-0.1.1/src/leangage/gen/sql.py +4071 -0
  11. leangage-0.1.1/src/leangage/gen/token_format.py +2512 -0
  12. leangage-0.1.1/src/leangage/http.py +94 -0
  13. leangage-0.1.1/src/leangage/json_text.py +86 -0
  14. leangage-0.1.1/src/leangage/pg/__init__.py +825 -0
  15. leangage-0.1.1/src/leangage/pg/testing.py +114 -0
  16. leangage-0.1.1/src/leangage/server.py +45 -0
  17. leangage-0.1.1/src/leangage/token.py +72 -0
  18. leangage-0.1.1/src/leangage_calculator/__init__.py +78 -0
  19. leangage-0.1.1/src/leangage_calculator/__main__.py +62 -0
  20. leangage-0.1.1/src/leangage_calculator/bench.py +58 -0
  21. leangage-0.1.1/src/leangage_calculator/bytes_bench.py +260 -0
  22. leangage-0.1.1/src/leangage_calculator/gen/kernel.py +3059 -0
  23. leangage-0.1.1/src/leangage_calculator/gen/store.py +757 -0
  24. leangage-0.1.1/src/leangage_calculator/gen/wire.py +5066 -0
  25. leangage-0.1.1/src/leangage_notes/__init__.py +45 -0
  26. leangage-0.1.1/src/leangage_notes/gen/kernel.py +3505 -0
  27. leangage-0.1.1/src/leangage_notes/gen/store.py +1220 -0
  28. leangage-0.1.1/test/pg_common.py +374 -0
  29. leangage-0.1.1/test/test_differential.py +45 -0
  30. leangage-0.1.1/test/test_http.py +111 -0
  31. leangage-0.1.1/test/test_json_differential.py +93 -0
  32. leangage-0.1.1/test/test_json_library.py +53 -0
  33. leangage-0.1.1/test/test_notes_differential.py +83 -0
  34. leangage-0.1.1/test/test_pg_calculator.py +367 -0
  35. leangage-0.1.1/test/test_pg_contract.py +777 -0
  36. leangage-0.1.1/test/test_pg_values.py +144 -0
  37. leangage-0.1.1/test/test_request_differential.py +150 -0
  38. leangage-0.1.1/test/test_sql_differential.py +105 -0
  39. leangage-0.1.1/test/test_store.py +19 -0
  40. leangage-0.1.1/test/test_token.py +46 -0
  41. leangage-0.1.1/test/test_token_differential.py +68 -0
  42. leangage-0.1.1/uv.lock +336 -0
@@ -0,0 +1,11 @@
1
+ /result
2
+ /rust/target
3
+ /python/.venv
4
+ /python/dist
5
+ __pycache__
6
+ .pytest_cache
7
+ .ruff_cache
8
+ /bench/rust/target
9
+ /bench/python/.venv
10
+ gen/
11
+ !/go/*/gen/
@@ -0,0 +1,16 @@
1
+ Metadata-Version: 2.5
2
+ Name: leangage
3
+ Version: 0.1.1
4
+ Summary: The trusted server that runs a Lean-proved kernel behind HTTP
5
+ Project-URL: Repository, https://github.com/conao3/lean-leangage
6
+ License-Expression: Apache-2.0
7
+ Requires-Python: >=3.13
8
+ Requires-Dist: leanfran==0.13.2
9
+ Requires-Dist: psycopg[binary]>=3.2
10
+ Description-Content-Type: text/markdown
11
+
12
+ # leangage for Python
13
+
14
+ The same kernel that the Rust server runs, generated to Python by
15
+ [leanfran](https://github.com/conao3/lean-leanfran). See the
16
+ [repository README](https://github.com/conao3/lean-leangage#readme).
@@ -0,0 +1,5 @@
1
+ # leangage for Python
2
+
3
+ The same kernel that the Rust server runs, generated to Python by
4
+ [leanfran](https://github.com/conao3/lean-leanfran). See the
5
+ [repository README](https://github.com/conao3/lean-leangage#readme).
@@ -0,0 +1,30 @@
1
+ [project]
2
+ name = "leangage"
3
+ version = "0.1.1"
4
+ description = "The trusted server that runs a Lean-proved kernel behind HTTP"
5
+ readme = "README.md"
6
+ license = "Apache-2.0"
7
+ requires-python = ">=3.13"
8
+ dependencies = [
9
+ "leanfran==0.13.2",
10
+ "psycopg[binary]>=3.2",
11
+ ]
12
+
13
+ [project.urls]
14
+ Repository = "https://github.com/conao3/lean-leangage"
15
+
16
+ [dependency-groups]
17
+ dev = ["pytest>=8", "pytest-icdiff>=0.9"]
18
+
19
+ [build-system]
20
+ requires = ["hatchling"]
21
+ build-backend = "hatchling.build"
22
+
23
+ [tool.hatch.build]
24
+ artifacts = ["src/*/gen/*.py"]
25
+
26
+ [tool.hatch.build.targets.wheel]
27
+ packages = ["src/leangage"]
28
+
29
+ [tool.pytest.ini_options]
30
+ testpaths = ["test"]
@@ -0,0 +1,39 @@
1
+ """The trusted server around a kernel proved in Lean."""
2
+
3
+ from leangage.engine import (
4
+ Broken,
5
+ CommitUnknown,
6
+ DatabaseError,
7
+ Engine,
8
+ EngineError,
9
+ IdempotencyConflict,
10
+ Kernel,
11
+ MemoryEngine,
12
+ Refused,
13
+ RetriesExhausted,
14
+ Storage,
15
+ )
16
+ from leangage.http import Api, handle
17
+ from leangage.server import serve
18
+ from leangage.token import Claims, Verifier, admit, issue
19
+
20
+ __all__ = [
21
+ "Api",
22
+ "Broken",
23
+ "Claims",
24
+ "CommitUnknown",
25
+ "DatabaseError",
26
+ "Engine",
27
+ "EngineError",
28
+ "IdempotencyConflict",
29
+ "Kernel",
30
+ "MemoryEngine",
31
+ "Refused",
32
+ "RetriesExhausted",
33
+ "Storage",
34
+ "Verifier",
35
+ "admit",
36
+ "handle",
37
+ "issue",
38
+ "serve",
39
+ ]
@@ -0,0 +1,125 @@
1
+ """The contract of a kernel and of its storage, and an engine that keeps every tenant's snapshot
2
+ in this process."""
3
+
4
+ import threading
5
+ from collections.abc import Callable
6
+ from dataclasses import dataclass, field
7
+ from typing import Any, Protocol
8
+
9
+
10
+ @dataclass(frozen=True)
11
+ class Kernel:
12
+ """The contract of `Leangage.Kernel`, filled with functions generated from Lean.
13
+
14
+ `transition` returns the writes and the reply of an accepted command, or raises `Refused`.
15
+ `read_only` says whether a command only reads: `ReadOnlySound` says it then writes nothing.
16
+ """
17
+
18
+ init: Callable[[], Any]
19
+ tenant: Callable[[Any], int]
20
+ transition: Callable[[Any, Any, Any], tuple[Any, Any]]
21
+ apply: Callable[[Any, Any], Any]
22
+ read_only: Callable[[Any], bool]
23
+
24
+
25
+ @dataclass(frozen=True)
26
+ class Storage:
27
+ """The contract of `Leangage.Storage`: how a kernel's snapshots are stored in PostgreSQL,
28
+ filled with functions generated from Lean, and the bytes an engine stores for a reply under an
29
+ idempotency key.
30
+
31
+ `tables` gives the tables in the order `load` receives their rows. `load` gives the snapshot
32
+ `kernel.transition` and `kernel.apply` take, or `None` when a row is not one the kernel
33
+ stores. `writes` gives
34
+ the rows a write set stores. `encode_reply` is trusted: no theorem covers it. `decode_reply`
35
+ gives the reply `encode_reply` stored, or `None` for bytes it does not write.
36
+ """
37
+
38
+ kernel: Kernel
39
+ tables: Callable[[], list[Any]]
40
+ load: Callable[[list[list[list[Any]]]], Any | None]
41
+ writes: Callable[[Any], list[Any]]
42
+ encode_reply: Callable[[Any], bytes]
43
+ decode_reply: Callable[[bytes], Any | None]
44
+
45
+
46
+ class Refused(Exception):
47
+ """The kernel refused the command; nothing is stored."""
48
+
49
+ def __init__(self, error: Any) -> None:
50
+ super().__init__(error)
51
+ self.error = error
52
+
53
+
54
+ class EngineError(Exception):
55
+ """The request did not reach the kernel's decision, or its decision was not stored."""
56
+
57
+
58
+ class IdempotencyConflict(EngineError):
59
+ """The idempotency key was used before with another command."""
60
+
61
+
62
+ class RetriesExhausted(EngineError):
63
+ """Every attempt failed to serialize or lost its connection; none committed, except that a
64
+ request with an idempotency key may have committed on an attempt whose COMMIT was lost."""
65
+
66
+
67
+ class CommitUnknown(EngineError):
68
+ """The connection was lost during the COMMIT of a request without an idempotency key: it may
69
+ or may not have committed."""
70
+
71
+
72
+ class DatabaseError(EngineError):
73
+ """The database failed the request, which did not commit."""
74
+
75
+
76
+ class Broken(EngineError):
77
+ """The stored rows or reply do not decode, a write set does not compile, or a read-only
78
+ command wrote. Nothing was committed."""
79
+
80
+
81
+ class Engine(Protocol):
82
+ """Runs one command for its principal's tenant: returns the reply, or raises `Refused` or an
83
+ `EngineError`."""
84
+
85
+ def execute(self, actor: Any, cmd: Any) -> Any: ...
86
+
87
+ def execute_idempotent(
88
+ self, actor: Any, scope: str, key: str, fingerprint: bytes, cmd: Any
89
+ ) -> Any: ...
90
+
91
+
92
+ @dataclass
93
+ class MemoryEngine:
94
+ kernel: Kernel
95
+ _tenants: dict[int, Any] = field(default_factory=dict)
96
+ _replies: dict[tuple[int, str, str], tuple[bytes, Any]] = field(
97
+ default_factory=dict
98
+ )
99
+ _lock: threading.Lock = field(default_factory=threading.Lock)
100
+
101
+ def _run(self, actor: Any, cmd: Any) -> Any:
102
+ tenant = self.kernel.tenant(actor)
103
+ snap = self._tenants[tenant] if tenant in self._tenants else self.kernel.init()
104
+ writes, reply = self.kernel.transition(actor, snap, cmd)
105
+ self._tenants[tenant] = self.kernel.apply(snap, writes)
106
+ return reply
107
+
108
+ def execute(self, actor: Any, cmd: Any) -> Any:
109
+ with self._lock:
110
+ return self._run(actor, cmd)
111
+
112
+ def execute_idempotent(
113
+ self, actor: Any, scope: str, key: str, fingerprint: bytes, cmd: Any
114
+ ) -> Any:
115
+ """Runs the command once per `(tenant, scope, key)`; refusals are not stored."""
116
+ with self._lock:
117
+ slot = (self.kernel.tenant(actor), scope, key)
118
+ if slot in self._replies:
119
+ stored, reply = self._replies[slot]
120
+ if stored != fingerprint:
121
+ raise IdempotencyConflict
122
+ return reply
123
+ reply = self._run(actor, cmd)
124
+ self._replies[slot] = (fingerprint, reply)
125
+ return reply