zettelkasten.sharing¶
zettelkasten.sharing ¶
Encryption-first sharing for the committed .zettelkasten store.
Mirrors :mod:memory.sharing: filter a store slice by owner-authored
sharing/grants policy, encrypt each retained unit natively to only its
recipients' age keys, and package a bundle a consumer can git-fetch and
decrypt into a .zettelkasten-shaped federation cache.
Crypto and identity are REUSED from :mod:memory.sharing unchanged — there is a
single firm identity registry (.memory/identities.yaml) and one local key
(~/.angelo/identity.yaml); this package does NOT fork them or create a
.zettelkasten/identities.yaml. Only the policy/scrub/bundle layers are
Zettelkasten-specific.
Typical producer flow::
from memory import sharing as mem_sharing
from zettelkasten import sharing
registry = mem_sharing.load_registry()
result = sharing.build_bundle("/path/to/bundle-repo", registry=registry)
Typical consumer flow::
from zettelkasten import sharing
sharing.decrypt_bundle(
"/path/to/fetched-bundle-repo",
"/path/to/federation-cache/peer", # writes <peer>/.zettelkasten/...
) # uses ~/.angelo/identity.yaml
IdentityRegistry
dataclass
¶
An id -> RegistryEntry map of publishable (public-key) identities.
Source code in memory/sharing/identity.py
member_ids ¶
public_key_for ¶
The public key for recipient_id, or None if unregistered.
signing_public_key_for ¶
The Ed25519 signing public key for recipient_id.
Returns None when the id is unregistered or when the registered
entry predates signing (legacy entry with no signing_public_key).
Source code in memory/sharing/identity.py
public_keys_for ¶
Resolve ids to public keys, silently dropping unregistered ids.
Source code in memory/sharing/identity.py
with_entry ¶
with_entry(entry: RegistryEntry) -> 'IdentityRegistry'
A copy of this registry with entry added/overwritten (immutably).
LocalIdentity
dataclass
¶
This machine's identity: a logical id plus its keypair(s).
Persisted (with the private keys) to ~/.angelo/identity.yaml. Carries both
the age X25519 encryption keypair and the Ed25519 signing keypair; the
signing fields are optional (default None) so a legacy identity file with
no signing key still loads.
Source code in memory/sharing/identity.py
to_pyrage_identity ¶
to_registry_entry ¶
to_registry_entry() -> RegistryEntry
The public-only :class:RegistryEntry for publishing to the registry.
Source code in memory/sharing/identity.py
RegistryEntry
dataclass
¶
One published identity: a logical id bound to a public key.
display_name and github are optional and used only for attribution /
discovery — never for access control (the public key is the anchor).
signing_public_key is the identity's Ed25519 signing public key (base64,
public part only); optional so legacy registries without it still parse.
Source code in memory/sharing/identity.py
BuildResult
dataclass
¶
Summary of a :func:build_bundle run (returned in memory, never written).
Source code in zettelkasten/sharing/bundle.py
BundleManifest
dataclass
¶
The cleartext manifest.json at the bundle root.
Source code in zettelkasten/sharing/bundle.py
BundleUnit
dataclass
¶
A pointer to one encrypted unit inside a bundle (kind + relative path).
Source code in zettelkasten/sharing/bundle.py
DecryptResult
dataclass
¶
Summary of a :func:decrypt_bundle run.
Source code in zettelkasten/sharing/bundle.py
ProjectionResult
dataclass
¶
Summary of a :func:project_for_caller run.
Mirrors :class:zettelkasten.sharing.bundle.BuildResult's per-kind counts and
:class:~zettelkasten.sharing.bundle.DecryptResult's per-kind identifiers, so
a projection can be compared directly against a decrypt for parity. ids
use the same scheme :func:~zettelkasten.sharing.bundle.decrypt_bundle
reports (note "<box>/<id>", source box name, project name, citation id,
review name, org id, table/outline review name, sidecar "<box>/<rel>").
Source code in zettelkasten/sharing/projection.py
Scope
dataclass
¶
A visibility scope attached to a project or source box.
audience is only meaningful when kind == 'bilateral' and holds the
recipient ids that may access the subtree.
Source code in zettelkasten/sharing/config.py
SharingConfig
dataclass
¶
Parsed, typed view of the sharing + grants config blocks.
scopesmaps a project name (or a source-box name) to its :class:Scope.grantsmaps a key (project name, box name,box/note_id,cross/note_idalias, orcitation:<id>) to the tuple of recipient ids granted access to that unit (and, for a project/box key, its closure).
Source code in zettelkasten/sharing/config.py
is_empty ¶
decrypt ¶
Decrypt ciphertext with private_key.
private_key may be an AGE-SECRET-KEY-... string or a pre-parsed
:class:pyrage.x25519.Identity. Raises :class:DecryptError if the
ciphertext was not encrypted to this key.
Source code in memory/sharing/crypto.py
encrypt ¶
Encrypt plaintext to every recipient in recipient_pubkeys.
Each key is an age X25519 recipient string (age1...). Any recipient
holding the matching private key can :func:decrypt the result. Returns the
binary age ciphertext.
Raises :class:ValueError if no recipients are given (encrypting to nobody
would produce an unopenable blob) or if a recipient string is malformed
(surfaced as :class:pyrage.RecipientError).
Source code in memory/sharing/crypto.py
create_identity ¶
create_identity(identity_id: str, *, display_name: str | None = None, github: str | None = None, path: Path | str | None = None, overwrite: bool = False) -> LocalIdentity
Generate a keypair, build a :class:LocalIdentity, and persist it.
Refuses to clobber an existing identity file unless overwrite=True — a
lost private key cannot be recovered, so overwriting is opt-in.
Source code in memory/sharing/identity.py
default_identity_path ¶
Path of this machine's private identity file: ~/.angelo/identity.yaml.
Under HOME (never the workspace) so the private key cannot be committed.
default_registry_path ¶
Default path of the shared identity registry (.memory/identities.yaml).
Anchored to the memory store so it is versioned alongside the tree. Contains public keys only, so committing it is safe. Callers may override the path.
Source code in memory/sharing/identity.py
load_local_identity ¶
load_local_identity(path: Path | str | None = None) -> LocalIdentity | None
Load this machine's identity, or None if the file is absent/invalid.
The public key is derived from the private key when the file omits it, so an
identity file carrying only id + private_key still loads.
Source code in memory/sharing/identity.py
load_registry ¶
load_registry(path: Path | str | None = None) -> IdentityRegistry
Load the shared identity registry, or an empty one if absent/invalid.
Expected YAML shape::
identities:
alice:
public_key: age1...
display_name: Alice
github: alice-gh
signing_public_key: ... # optional Ed25519 signing public key (base64)
bob: age1... # shorthand: id -> public key (no signing key)
Malformed rows are skipped rather than raising, so a partially-authored registry still loads the good rows.
Source code in memory/sharing/identity.py
save_registry ¶
save_registry(registry: IdentityRegistry, path: Path | str | None = None) -> Path
Write the identity registry (public keys only) to disk.
Source code in memory/sharing/identity.py
build_bundle ¶
build_bundle(output_dir: Path | str, *, registry: IdentityRegistry, store_root: Path | str | None = None, sharing: SharingConfig | None = None, producer_id: str | None = None) -> BuildResult
Filter store units by grants, encrypt per recipient, and write a bundle.
Pipeline (filtering happens BEFORE any encryption):
- Enumerate
note/source_meta/project/citationunits fromstore_root(default: the local.zettelkastenstore). - Resolve each unit's recipient id set from
sharing(default: parsed fromstore_root/config.yaml), expanding afirmscope to theregistrymembers. Units resolving to no registered recipient are dropped (physically omitted). - For each retained unit compute its ALLOWED set — the unit-keys whose
recipient set is a superset of this unit's recipients — scrub cross-unit
references down to that set (see :mod:
zettelkasten.sharing.scrub), wrap the scrubbed bytes in a placement envelope, encrypt to the recipients' public keys and writeunits/<digest>.age. - Write the cleartext
manifest.json.
This function OWNS output_dir/units and output_dir/manifest.json —
the units directory is rebuilt from scratch on every call so stale/revoked
units never linger. Other files (e.g. .git) are left untouched. With no
sharing policy configured it writes an empty bundle.
Source code in zettelkasten/sharing/bundle.py
905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 | |
decrypt_bundle ¶
decrypt_bundle(bundle_dir: Path | str, out_root: Path | str, *, identity: LocalIdentity | Any | None = None, private_key: str | None = None) -> DecryptResult
Decrypt every unit this key can open and materialize a .zettelkasten cache.
Units are written into <out_root>/.zettelkasten/ as
<box>/<id>.md (notes), <box>/_meta.yaml (source meta),
_projects/<name>.yaml (projects) and _citations/<id>.yaml
(citations), so :func:zettelkasten.federation.configured_repos renders the
result like any other peer. Each unit's placement is read from its decrypted
envelope header and validated / path-jailed before use.
The cache is RECONCILED to the current bundle: stale units (dropped from a newer, re-scoped bundle or no longer openable by this key) are evicted from each owned location. A transient I/O fault during the pass SKIPS eviction that run (never destroys valid cached plaintext); revocation still works on any clean pass. A missing/corrupt manifest is treated as an empty bundle (still reconciled); an unreadable manifest (transient I/O) aborts without reconciling.
Provide the key via identity (a :class:LocalIdentity / pyrage
identity), private_key (an AGE-SECRET-KEY-... string), or neither (to
load ~/.angelo/identity.yaml).
Source code in zettelkasten/sharing/bundle.py
1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 | |
open_bundle ¶
open_bundle(bundle_dir: Path | str, out_root: Path | str, *, identity: LocalIdentity | Any | None = None, private_key: str | None = None) -> DecryptResult
Alias for :func:decrypt_bundle.
Source code in zettelkasten/sharing/bundle.py
read_manifest ¶
read_manifest(bundle_dir: Path | str) -> BundleManifest
Read and parse a bundle's cleartext manifest.json.
Source code in zettelkasten/sharing/bundle.py
project_for_caller ¶
project_for_caller(caller_id: str, *, registry: IdentityRegistry, out_root: Path | str, store_root: Path | str | None = None, sharing: SharingConfig | None = None, allowed_transports: set[str] | None = None) -> ProjectionResult
Materialize the PLAINTEXT units caller_id may open under out_root.
Writes a .zettelkasten-shaped plaintext store into
<out_root>/.zettelkasten/ — the SAME layout
:func:~zettelkasten.sharing.bundle.decrypt_bundle produces — containing only
the units caller_id is entitled to, with references scrubbed exactly as
:func:~zettelkasten.sharing.bundle.build_bundle scrubs them.
store_root defaults to the local .zettelkasten store and sharing
to the policy parsed from <store_root>/config.yaml (both resolved inside
the shared :func:~zettelkasten.sharing.bundle._collect_pending).
allowed_transports restricts the projection to units of a given transport
tier (design decision D3). The hosted-federation broker passes {"hosted"}
so it serves ONLY hosted units — structurally, a unit whose effective
transport is offline can never be projected to a broker caller, so an
offline-only store is never served over the network. The default None
applies no transport filter (every entitled unit is projected), preserving
the pre-transport-tier behaviour and keeping the projection byte-identical to
an offline build_bundle + decrypt_bundle when the store has no
transport annotations.
This function OWNS <out_root>/.zettelkasten and RECONCILES it on every
call, exactly like :func:~zettelkasten.sharing.bundle.decrypt_bundle: a
re-projection (e.g. after a grant change) surgically evicts only the units this
projection previously produced that the caller is no longer entitled to, while
PRESERVING foreign/unowned files and honoring the same transient-I/O guard
(a read glitch skips eviction rather than destroying a valid cache). The result
matches a fresh decrypt of the caller's CURRENT grants even when projecting
into a previously-populated store.
Source code in zettelkasten/sharing/projection.py
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 | |
citation_recipients ¶
citation_recipients(sharing: SharingConfig, note_recipients_map: Mapping[str, set[str]], note_citation_refs: Mapping[str, Iterable[str]]) -> dict[str, set[str]]
Map each shareable citation (citation:<id>) to its recipient set.
A citation is a leaf bibliographic entity referenced BY notes (via a
links[].graph == "_citations" edge). Its recipients are the UNION over
every SHARED note that references it of that note's recipient set — so every
recipient of a note can also open the citation the note points at (the
superset property that keeps the citation link from ever dangling). Any
direct grants['citation:<id>'] recipients are added on top. Citations
resolving to no recipients are omitted.
Source code in zettelkasten/sharing/config.py
note_recipients ¶
note_recipients(sharing: SharingConfig, projects: Iterable[Mapping[str, Any]], notes_by_box: Mapping[str, Iterable[str]], firm_members: Iterable[str] = ()) -> dict[str, set[str]]
Map each shareable note (<box>/<note_id>) to its recipient set.
The recipients of a note are the UNION of:
- every project closure that includes it — a note is in project P's closure
when it lives in one of P's
sources[]boxes, or when its id is listed in P'scross[](a_crosssynthesis note); such notes get P's recipients; - a direct source-box scope/grant on the note's box; and
- a direct per-note grant keyed on
<box>/<note_id>(cross/<id>alias accepted).
Notes resolving to no recipients are omitted.
Source code in zettelkasten/sharing/config.py
org_recipients ¶
org_recipients(sharing: SharingConfig, organizations: Iterable[Mapping[str, Any]], note_recipients_map: Mapping[str, set[str]], source_recipients_map: Mapping[str, set[str]], project_recipients_map: Mapping[str, set[str]]) -> dict[str, set[str]]
Map each organization id to its recipient set.
An organization is owned by exactly one project OR one graph (see
:data:zettelkasten.organizations.OWNER_TYPES). Its recipients are the
owner's recipients — the project's recipients, or (for a graph owner)
everyone who can see that box — plus any direct grants['org:<id>'].
Organizations resolving to no recipients are omitted.
Source code in zettelkasten/sharing/config.py
parse_sharing_config ¶
parse_sharing_config(config: Mapping[str, Any] | None) -> SharingConfig
Parse the sharing and grants blocks of a loaded config dict.
config is the plain dict read from .zettelkasten/config.yaml.
Missing/empty/malformed blocks yield an empty :class:SharingConfig
(nothing shared) — this never raises on a partially-formed config; unusable
items are skipped. The existing rerank/federated_repos blocks are
ignored here (left untouched for their own readers).
Source code in zettelkasten/sharing/config.py
project_recipients ¶
project_recipients(sharing: SharingConfig, project_names: Iterable[str], firm_members: Iterable[str] = ()) -> dict[str, set[str]]
Map each project name to the concrete set of recipient ids.
A project's recipients are its scopes[name] (firm expanded to
firm_members) UNIONED with any grants[name]. Projects resolving to no
recipients are omitted (the caller uses this to drop them from the bundle).
Source code in zettelkasten/sharing/config.py
review_recipients ¶
review_recipients(sharing: SharingConfig, reviews: Iterable[Mapping[str, Any]], note_recipients_map: Mapping[str, set[str]], source_recipients_map: Mapping[str, set[str]], project_recipients_map: Mapping[str, set[str]]) -> dict[str, set[str]]
Map each review name to its recipient set (also used for its tables/outlines).
A review is shared to the recipients who can see its SCOPE, plus any direct
grants['review:<name>']:
graphset -> box-scoped: everyone who can see boxG(see :func:_box_recipients);- else
projectset -> project-scoped: the project's recipients; - empty/empty -> full-corpus: everyone who can see any shared unit.
A direct grants['review:<name>'] widens the audience. Reviews resolving
to no recipients are omitted. The returned map is reused verbatim for a
review's KIND_TABLE grid file and KIND_OUTLINE sidecar (both keyed by
the review name), so a table/outline can never reach a recipient the review
itself could not.
Source code in zettelkasten/sharing/config.py
source_recipients ¶
source_recipients(sharing: SharingConfig, projects: Iterable[Mapping[str, Any]], boxes: Iterable[str], firm_members: Iterable[str] = ()) -> dict[str, set[str]]
Map each shareable source box to its _meta.yaml recipient set.
A box's meta is shared to the UNION of every project that lists the box in
its sources[] (getting that project's recipients) plus any direct
box-level scope/grant. Boxes resolving to no recipients are omitted.