Source code for cadrumo.adapters.persistence.storage.master_key.idle_timeout
"""Idle-timeout evaluation for `BucketSession`.Every CLI invocation runs `evaluate_idle(session, now)` before granting accessto the session. The window it evaluates was fixed when the session was opened,from `cadrumo_bucket_default_idle_lock_minutes`; the default is 15 minutes.There is no per-bucket override to read: the plaintext bucket manifest thatonce declared one is retired, and nothing replaced it.The evaluator is a pure function over the session's idle deadline andthe supplied `now`; it never mutates the session. Mutation happensthrough `session.touch(now)` which the caller invokes on a successfulauthentication so the deadline rolls forward by the configured window.Returning a typed `IdleEvaluation` record (rather than a bare boolean)lets the CLI render an actionable "remaining N seconds" hint withoutre-deriving the math at the verb layer."""from__future__importannotationsfromdatetimeimportdatetime,timedeltafrompydanticimportBaseModel,NonNegativeIntfrom.....core.modelsimportSTRICT_FROZEN_CONFIGas_STRICT_FROZENfrom..errorsimport(storage_validation_erroras_storage_validation_error,)from.bucket_sessionimportBucketSessionDEFAULT_IDLE_LOCK_MINUTES=15
[docs]classIdleEvaluation(BaseModel):"""Typed outcome of an idle-window evaluation."""model_config=_STRICT_FROZENexpired:boolremaining_seconds:NonNegativeInt
[docs]defevaluate_idle(*,session:BucketSession,now:datetime,configured_minutes:int=DEFAULT_IDLE_LOCK_MINUTES,)->IdleEvaluation:"""Evaluate the session's idle window without mutating it. Args: session: The session whose idle deadline to evaluate. now: UTC timestamp at which the evaluation runs. configured_minutes: Idle-lock window in minutes, defaulting to `DEFAULT_IDLE_LOCK_MINUTES` (15). Validated as a strict positive integer and otherwise unused: the deadlines this function compares were computed when the session opened, so the value bounds nothing here. The live caller passes none. Returns: An :class:`IdleEvaluation` record carrying `expired` and the floor-truncated `remaining_seconds` until the earlier of the idle deadline and the absolute session cap (zero when either has elapsed). Raises: StorageValidationError: When ``configured_minutes`` is not a strict positive integer. """ifconfigured_minutes<=0:raise_storage_validation_error("configured_minutes must be a strict positive integer")ifsession.sealed:returnIdleEvaluation(expired=True,remaining_seconds=0)# Enforce both the sliding idle window and the immutable absolute cap: the# session is expired once the earlier of the two deadlines is reached.deadline=min(session.idle_deadline,session.absolute_deadline)ifnow>=deadline:returnIdleEvaluation(expired=True,remaining_seconds=0)delta:timedelta=deadline-nowreturnIdleEvaluation(expired=False,remaining_seconds=int(delta.total_seconds()))