Source code for cadrumo.application.workbench_capture_memory
"""Session-owned reuse of workbench capture work whose exact inputs have not changed.A workbench generation is captured after every write and every return to Home.Most of that work is a pure function of a few stored revisions and theauthority generation: the legal calendar, for example, changes only when theprofile, the work units, the filing records, the date or the authority does.The capture memory keeps the last result of such work beside the exactidentity of its inputs, and hands it back only when every one of thoseidentities is unchanged. Anything whose identity cannot be stated is rebuilt.One memory belongs to one authenticated session. It holds only projectionsthe session already publishes, never a decrypted catalogue, and the compositionthat owns the session closes it on sign-out, handover or expiry."""from__future__importannotationsfromcollections.abcimportCallable,HashablefromdataclassesimportdataclassfromdatetimeimportdatefromthreadingimportLockfromtypingimportTYPE_CHECKINGifTYPE_CHECKING:from..domain.calculations.registry.authority_artifactimportAuthorityGenerationPinfrom.overview.agendaimportOverviewAgendafrom.overview.calendar_modelsimportOverviewCalendar__all__=["WorkbenchCalendarMemoKey","WorkbenchCalendarWork","WorkbenchCaptureMemory","WorkbenchMemoSlot",]
[docs]classWorkbenchMemoSlot[KeyT:Hashable,ValueT]:"""One reusable result and the input identity it was computed from. The key and value are swapped together under a lock, so a reader sees either the previous pair or the next one, never a mix. Two captures that compute the same work concurrently both build it; the later store wins, which loses nothing because both results belong to their own key. """def__init__(self)->None:"""Start empty."""self._lock=Lock()self._entry:tuple[KeyT,ValueT]|None=Noneself._closed=False
[docs]defreuse(self,key:KeyT,build:Callable[[],ValueT])->ValueT:"""Return the stored result for ``key``, or build, store and return it."""withself._lock:entry=self._entryclosed=self._closedifentryisnotNoneandentry[0]==key:returnentry[1]value=build()ifnotclosed:withself._lock:ifnotself._closed:self._entry=(key,value)returnvalue
[docs]defclose(self)->None:"""Drop the stored result and store nothing further."""withself._lock:self._closed=Trueself._entry=None
[docs]@dataclass(frozen=True,slots=True)classWorkbenchCalendarMemoKey:"""Everything the legal calendar and agenda of one capture are computed from."""profile_content_digest:strwork_units_revision:strfilings_revision:stras_of:dategeneration:AuthorityGenerationPin
[docs]@dataclass(frozen=True,slots=True)classWorkbenchCalendarWork:"""The registry-heavy calendar work of one capture. ``schedule_calendar`` scopes the filing evidence, ``calendar`` carries that evidence, and ``agenda`` is Home's upcoming window. Each carries the instant it was actually computed. """schedule_calendar:OverviewCalendarcalendar:OverviewCalendaragenda:OverviewAgenda
[docs]classWorkbenchCaptureMemory:"""The reusable work of one authenticated session's workbench captures."""def__init__(self)->None:"""Create the empty slots one session reuses."""self.calendar:WorkbenchMemoSlot[WorkbenchCalendarMemoKey,WorkbenchCalendarWork]=WorkbenchMemoSlot()
[docs]defclose(self)->None:"""Drop every stored result deterministically when the session ends."""self.calendar.close()