memory.sharing.projection¶
memory.sharing.projection ¶
Per-caller PLAINTEXT projection of a shared memory store.
The Iceberg hosted-federation broker (see
documents/260708_iceberg-revocation-and-binding.md, Part D) owns the
plaintext store and must hand each authenticated caller a .memory-shaped
view containing ONLY the units that caller is entitled to see — the same
filtering and reference-scrubbing a shared bundle applies, but WITHOUT the
encrypt/decrypt round-trip (and without ever holding the caller's private key).
:func:project_for_caller produces exactly that. It reuses
:func:memory.sharing.bundle._collect_shared_units — the single source of truth
for the filter + per-unit scrub that :func:memory.sharing.bundle.build_bundle
also uses — so the result satisfies the PARITY CONTRACT:
project_for_caller(caller) == build_bundle(...) then
decrypt_bundle(..., private_key=caller_key)
i.e. a caller's plaintext projection is byte-for-byte the SAME set of units, and the SAME scrubbed content, that caller would obtain by decrypting the encrypted bundle.
Why the per-unit SUPERSET scrub — not a per-caller "everything I can see" set¶
It is tempting to scrub each projected unit against {x : caller in
recipients(x)} (everything the caller can see). That is WRONG and breaks
parity. build_bundle scrubs each unit exactly ONCE, against allowed_ids =
the bundled units whose recipient set is a SUPERSET of THAT unit's recipient set.
That scrub is caller-independent and is baked into the single ciphertext every
recipient of the unit decrypts. So when a caller decrypts the bundle they see
each unit scrubbed to that SUPERSET set — which can be strictly NARROWER than the
full set of units the caller can see elsewhere.
Concretely: a session shared to {alice, bob} keeps only refs whose recipient
set is a superset of {alice, bob} (e.g. a firm-wide entry). Alice's decrypted
copy therefore omits an alice-only ref even though alice can open that entry
in her own unit. Scrubbing the projection with the caller's global visible set
would RETAIN that ref and diverge from the decrypt. Reusing the shared collector
(which applies the superset rule per unit, once) guarantees identical scrubbed
bytes on both paths — so the projection and the decrypt agree byte-for-byte.
ProjectionResult
dataclass
¶
Summary of a :func:project_for_caller run.
Mirrors the useful fields of :class:memory.sharing.bundle.DecryptResult
(the projection is defined to equal what that caller would decrypt), plus the
caller_id it was scoped to. total_units counts the entry/document/
skill/session units in this caller's view (the project stub is reported
separately via project_written).
Source code in memory/sharing/projection.py
project_for_caller ¶
project_for_caller(caller_id: str, *, registry: IdentityRegistry, output_memory_dir: Path | str, source_memory_dir: Path | str | None = None, sharing: SharingConfig | None = None, include_discarded: bool = False, allowed_transports: set[str] | None = None) -> ProjectionResult
Write a .memory-shaped PLAINTEXT store of ONLY what caller_id may see.
Reuses the shared filter+scrub collector (the same code
:func:memory.sharing.bundle.build_bundle runs), then materializes — as
PLAINTEXT, with no encryption round-trip — exactly the units the caller is a
registered recipient of, into output_memory_dir (project.md plus
entries/<id>.md, documents/<id>.md, sessions/<id>.md and
skills/<name>.md), matching :func:~memory.sharing.bundle.decrypt_bundle's
output shape so :func:memory.federation.read_repo reads it unchanged.
Parity: the produced set of units and their scrubbed content are byte-for-byte
what caller_id would obtain via build_bundle + decrypt_bundle with
that caller's key (see the module docstring). sharing defaults to the
policy parsed from <source_memory_dir>/config.yaml; source_memory_dir
defaults to the local .memory store.
A caller that is unregistered or has nothing shared to it yields an empty
projection (only an empty entries/ dir, no project.md) — matching a
decrypt that opens nothing.
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 output_memory_dir and RECONCILES it on every call,
exactly like :func:~memory.sharing.bundle.decrypt_bundle: re-projecting a
caller into a previously-populated dir evicts the *.md units the caller is
no longer entitled to (from entries/, documents/, skills/,
sessions/) and drops a stale project.md when the caller ends up with
zero units, so a revoked grant genuinely disappears on the next read. Foreign
(unowned) files are preserved, and a transient I/O fault while scanning the
output skips eviction that run (never destroying a valid cache). The result is
byte-identical to a fresh build_bundle + decrypt_bundle for the
caller's CURRENT grants even when projecting into a reused output dir.
Source code in memory/sharing/projection.py
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 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 | |