egzoscore 0.2.0__py3-none-any.whl

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 (62) hide show
  1. egzos/__init__.py +11 -0
  2. egzos/_ids.py +29 -0
  3. egzos/_signals.py +39 -0
  4. egzos/_term.py +33 -0
  5. egzos/_types.py +1023 -0
  6. egzos/api/__init__.py +150 -0
  7. egzos/api/app.py +1574 -0
  8. egzos/api/blobs.py +92 -0
  9. egzos/api/throttle.py +190 -0
  10. egzos/auth.py +176 -0
  11. egzos/authz/__init__.py +5 -0
  12. egzos/authz/presence.py +778 -0
  13. egzos/authz/values.py +32 -0
  14. egzos/backends/__init__.py +8 -0
  15. egzos/backends/base.py +91 -0
  16. egzos/backends/postgres.py +490 -0
  17. egzos/backends/sqlite.py +524 -0
  18. egzos/binding.py +309 -0
  19. egzos/cli/__init__.py +1092 -0
  20. egzos/cli/__main__.py +11 -0
  21. egzos/confirm.py +455 -0
  22. egzos/container.py +222 -0
  23. egzos/identity.py +1104 -0
  24. egzos/ledger.py +167 -0
  25. egzos/mcp/__init__.py +3 -0
  26. egzos/mcp/http.py +90 -0
  27. egzos/mcp/server.py +287 -0
  28. egzos/model.py +242 -0
  29. egzos/py.typed +0 -0
  30. egzos/resolver.py +115 -0
  31. egzos/store/__init__.py +10 -0
  32. egzos/store/autotitle.py +31 -0
  33. egzos/store/blobs.py +120 -0
  34. egzos/store/find.py +120 -0
  35. egzos/store/items.py +233 -0
  36. egzos/store/nodes.py +193 -0
  37. egzos/store/objects.py +143 -0
  38. egzos/store/uploads.py +369 -0
  39. egzos/trust.py +675 -0
  40. egzos/web/__init__.py +88 -0
  41. egzos/web/app.py +965 -0
  42. egzos/web/static/htmx.LICENSE +13 -0
  43. egzos/web/static/htmx.min.js +1 -0
  44. egzos/web/static/lifeboat.css +140 -0
  45. egzos/web/static/lifeboat.js +12 -0
  46. egzos/web/strings.py +133 -0
  47. egzos/web/templates/_manifest_row.html +3 -0
  48. egzos/web/templates/_shell.html +29 -0
  49. egzos/web/templates/_window.html +7 -0
  50. egzos/web/templates/base.html +24 -0
  51. egzos/web/templates/error.html +10 -0
  52. egzos/web/templates/home.html +39 -0
  53. egzos/web/templates/item.html +91 -0
  54. egzos/web/templates/notfound.html +6 -0
  55. egzos/web/templates/pending.html +107 -0
  56. egzos/web/tokens.css +294 -0
  57. egzoscore-0.2.0.dist-info/METADATA +135 -0
  58. egzoscore-0.2.0.dist-info/RECORD +62 -0
  59. egzoscore-0.2.0.dist-info/WHEEL +4 -0
  60. egzoscore-0.2.0.dist-info/entry_points.txt +2 -0
  61. egzoscore-0.2.0.dist-info/licenses/LICENSE +65 -0
  62. egzoscore-0.2.0.dist-info/licenses/NOTICE +6 -0
egzos/container.py ADDED
@@ -0,0 +1,222 @@
1
+ # Copyright 2026 Ali Sasanian
2
+ # SPDX-License-Identifier: LicenseRef-PolyForm-Strict-1.0.0
3
+ """
4
+ The container — the user's home (v0.4 §1). One process-local assembly of backend, ledger, nodes,
5
+ store, blobs and auth rooted at EGZOS_HOME (default ~/.egzos). The container is the availability
6
+ boundary (v0.4 §16): nothing else can serve what it can't.
7
+ """
8
+
9
+ from __future__ import annotations
10
+
11
+ import logging
12
+ import os
13
+ from pathlib import Path
14
+ from typing import Any
15
+
16
+ from egzos._types import CONTAINER_CONFIG_DEFAULTS, CONTAINER_CONFIG_FIELD_FROM_WIRE_KEY
17
+ from egzos.auth import Auth, home_secret
18
+ from egzos.binding import HomeBinding, HomeBindingError
19
+ from egzos.confirm import Confirm
20
+ from egzos.identity import (
21
+ SETTABLE_KEYS,
22
+ ConfigError,
23
+ GrantToken,
24
+ Identity,
25
+ node_ancestors,
26
+ validate,
27
+ )
28
+ from egzos.ledger import Ledger
29
+ from egzos.model import Token
30
+ from egzos.resolver import Resolver
31
+ from egzos.store import NodeService, Store
32
+ from egzos.store.blobs import _is_digest
33
+ from egzos.store.find import shown
34
+ from egzos.store.uploads import Uploads
35
+ from egzos.trust import GRANT_KEY_BYTES, GRANT_KEY_FILE, TrustEngine
36
+
37
+ OWNER = "self"
38
+ log = logging.getLogger(__name__)
39
+
40
+ __all__ = ["OWNER", "Container", "HomeBindingError", "default_home"]
41
+
42
+
43
+ def _step_up_window() -> int:
44
+ from egzos.authz.presence import window_seconds
45
+
46
+ return window_seconds()
47
+
48
+
49
+ def default_home() -> Path:
50
+ return Path(os.environ.get("EGZOS_HOME", Path.home() / ".egzos"))
51
+
52
+
53
+ class Container:
54
+ def __init__(self, home: Path | None = None):
55
+ self.home = Path(home or default_home())
56
+ # The database holds the chain, every item and the token hashes: the owner's only. Created
57
+ # 0700 (no window at the umask's mode), then pinned 0700 if it already existed.
58
+ self.home.mkdir(parents=True, exist_ok=True, mode=0o700)
59
+ os.chmod(self.home, 0o700)
60
+ # storage.md §3.7: the database EGZOS_DATABASE_URL names, else the home's own egzos.db,
61
+ # and the object store the EGZOS_BLOBS_S3_* variables name, else the home's blobs/ (§4).
62
+ # The home's binding is checked here, before anything is built that could serve.
63
+ self.binding = HomeBinding(self.home)
64
+ self.backend, self.blobs = self.binding.open()
65
+ self._closed = False
66
+ self.ledger = Ledger(self.backend)
67
+ self.nodes = NodeService(self.backend, self.ledger)
68
+ self.store = Store(self.backend, self.ledger, self.nodes, self.blobs)
69
+ self.auth = Auth(self.home, self.backend, self.ledger)
70
+ self.trust = TrustEngine(self.backend, self.ledger, self.nodes)
71
+ self.resolver = Resolver(self.backend, self.ledger, self.nodes, self.trust)
72
+ self.store.covers = self.trust.covers
73
+ self.store.shown = lambda token, item, node: shown(self, token, item, node)
74
+ self.nodes.covers = self.trust.covers
75
+ self.identity = Identity(
76
+ self.backend,
77
+ self.ledger,
78
+ self.config_value,
79
+ ancestors=node_ancestors(self.backend.get_node, self.nodes.ancestors),
80
+ atomic=self.backend.atomic,
81
+ )
82
+ self.trust.principals = self.identity.audience_tokens
83
+ self.trust.refresh = self._refresh
84
+ self.trust.promote_blob = lambda sha: _is_digest(sha) and self.blobs.promote(sha)
85
+ self.trust.blob_exists = self.blobs.exists
86
+ # rest.md §6: the grant key, made at first need in the home, never a config key or a row;
87
+ # whom a grant names, as they stand now, an id no record holds any more as a revoked
88
+ # stand-in, so a refusal is still attributed to it; and what is shown.
89
+ self.trust.grant_key = lambda: home_secret(self.home / GRANT_KEY_FILE, GRANT_KEY_BYTES)
90
+ self.trust.named = lambda tid: (
91
+ self.identity.named(tid) or self.backend.get_token(tid)
92
+ or Token(id=tid, principal="client", owner=tid, client="", capabilities=[],
93
+ scopes=[], revoked=True))
94
+ self.trust.shown = self.store.shown
95
+ self.uploads = Uploads(self.backend, self.ledger, self.blobs, self.store)
96
+ # storage.md §3.6: a staged delete waits for its act's COMMIT, then re-checks the address.
97
+ self.blobs.defer = lambda sha: self.backend.after_commit(lambda: self.uploads.release(sha))
98
+ self.confirm = Confirm(
99
+ self.backend, self.ledger, self.config_value, window_seconds=_step_up_window,
100
+ salt=self._salt,
101
+ )
102
+ self._sweep()
103
+
104
+ def _sweep(self) -> None:
105
+ """storage.md §3.6, §4: at open, under the lock, staged bytes nothing holds are deleted
106
+ (`Uploads.sweep`). How many is the operator's log's, never the chain's; a sweep that cannot
107
+ run, the lock busy or the store unreachable, leaves them to the next open."""
108
+ try:
109
+ log.debug("the open-time sweep deleted %d staged addresses", self.uploads.sweep())
110
+ except Exception as e:
111
+ log.warning("the open-time sweep did not run (%s)", type(e).__name__)
112
+
113
+ def _salt(self) -> str:
114
+ """Per container and fixed for its life: the personal root's id. With no root there is
115
+ no salt, and no handle is handed out (a1r on #290)."""
116
+ root = self.nodes.user_root()
117
+ if root is None:
118
+ raise RuntimeError("this container is not initialised")
119
+ return root.id
120
+
121
+ def _refresh(self, token: Token) -> Token | None:
122
+ """The caller as it stands now: a minted token re-read, a grant re-derived (§4)."""
123
+ if isinstance(token, GrantToken):
124
+ return self.identity.refresh(token)
125
+ return self.backend.get_token(token.id)
126
+
127
+ # -- the container-config object (container.md §8) ------------------------------------------
128
+ def config_value(self, key: str) -> Any:
129
+ """The value of a wire key as set, or its default. Absence is tested by absence, never by
130
+ truthiness (`CONTAINER_CONFIG_FIELD_FROM_WIRE_KEY`): an empty string set is a value."""
131
+ stored = self.backend.get_config()
132
+ if key in stored:
133
+ return stored[key]
134
+ return CONTAINER_CONFIG_DEFAULTS[CONTAINER_CONFIG_FIELD_FROM_WIRE_KEY[key]] # type: ignore[literal-required]
135
+
136
+ def set_config(self, key: str, value: Any, *, token: Token) -> Any:
137
+ """Set a key: an `admin` act over the whole container, from a person (§8), recorded by
138
+ `config.set` with the value as it then stands. Only the keys code here reads are settable;
139
+ the rest are still the constants they always were, and setting one would be a lie."""
140
+ if token.principal != "interactive" or not token.has("admin") or "*" not in token.scopes:
141
+ raise PermissionError("changing the container's config is an admin's act")
142
+ if key not in SETTABLE_KEYS:
143
+ raise ConfigError(f"{key}: not a key this container sets")
144
+ value = validate(key, value)
145
+ with self.backend.atomic():
146
+ self.backend.put_config(key, value)
147
+ self.ledger.append(
148
+ "config.set", actor=token.owner, principal=token.principal, key=key, value=value
149
+ )
150
+ return value
151
+
152
+ @property
153
+ def initialized(self) -> bool:
154
+ return self.backend.find_root("user") is not None
155
+
156
+ def init(self) -> Token:
157
+ """Create the roots and the owner's interactive token; put it in the keychain stand-in.
158
+ It is the only step that mints the owner's token; people sign in through the container's
159
+ identity provider instead (identity.md §3).
160
+
161
+ The check and the creation are one transaction (#136): BEGIN IMMEDIATE takes the write
162
+ lock first, so a second `init` racing this one waits, then finds the container
163
+ initialized, and never mints a second owner or a second set of roots.
164
+
165
+ On a named database this block is the home's make (storage.md §3.7): it finds the chain
166
+ empty and appends its first entry, or stops, and the binding is written once it commits."""
167
+ with self.backend.atomic():
168
+ making = self.binding.making(self.backend)
169
+ token = self._init_locked()
170
+ if making:
171
+ self.binding.made(self.backend)
172
+ return token
173
+
174
+ def close(self) -> None:
175
+ """A clean close: a bound home records the newest entry it has seen (storage.md §3.7),
176
+ and the connection is let go. A second call does nothing."""
177
+ if self._closed:
178
+ return
179
+ self._closed = True
180
+ self.binding.record(self.backend)
181
+ self.backend.db.close()
182
+
183
+ def _init_locked(self) -> Token:
184
+ if self.initialized:
185
+ token = self.auth.interactive_token()
186
+ if token and token.principal == "interactive":
187
+ return token
188
+ if token:
189
+ raise PermissionError(
190
+ "this container is already initialized and the token presented is a client "
191
+ "principal's; init answers only for the owner"
192
+ )
193
+ # Never mint a second owner, and never overwrite the keychain, because a token did
194
+ # not resolve: that is exactly what a wrong or revoked EGZOS_TOKEN looks like.
195
+ raise PermissionError(
196
+ "this container is already initialized and the token presented does not resolve; "
197
+ "unset EGZOS_TOKEN or restore the keychain — init will not mint a new owner"
198
+ )
199
+ self.ledger.append(
200
+ "container.init", actor=OWNER, principal="interactive", home=str(self.home)
201
+ )
202
+ self.nodes.ensure_roots(actor=OWNER, principal="interactive")
203
+ token = self.auth.mint(
204
+ principal="interactive",
205
+ owner=OWNER,
206
+ client="cli",
207
+ role="admin",
208
+ scopes=["*"],
209
+ actor=OWNER,
210
+ by_principal="interactive",
211
+ )
212
+ # Inside the lock, so a racing init that waits on it finds the keychain already written.
213
+ self.auth.keychain_store(token)
214
+ return token
215
+
216
+ def require_token(self) -> Token:
217
+ token = self.auth.interactive_token()
218
+ if not token:
219
+ raise PermissionError(
220
+ "no token — run `egzoscore init`"
221
+ )
222
+ return token