Implementation:ALwrity ALwrity Plan Context Threshold
| Knowledge Sources | |
|---|---|
| Domains | Daily_Workflow, Validation, Configuration |
| Last Updated | 2026-10-24 00:00 GMT |
Overview
Pattern doc for an environment-overridable contextuality threshold that gates whether a generated daily plan is accepted or rejected, ensuring plans are relevant to the user's actual context.
Description
The PLAN_CONTEXT_THRESHOLD is a module-level constant (float, 0.0-1.0) loaded by _load_plan_context_threshold at import time. It defaults to _DEFAULT_PLAN_CONTEXT_THRESHOLD and can be overridden via the ALWRITY_PLAN_CONTEXT_THRESHOLD environment variable. The threshold represents the minimum cosine similarity between a generated daily plan and the user's context data (onboarding, calendar, persona). If the plan's contextuality score falls below the threshold, the plan is rejected and regenerated. Invalid values (non-float, outside [0.0, 1.0]) log a warning and fall back to the default.
This is a Pattern Doc — it documents a configuration pattern (env-overridable threshold with validation), not a class or function API.
Usage
Reference this pattern when:
- A daily plan's contextuality must be validated against a configurable threshold.
- An operator needs to tune the quality gate via an environment variable.
- Invalid threshold values must fall back to a safe default rather than crash.
Code Reference
Source Location
- Repository: ALwrity
- File: backend/services/today_workflow_service.py (L43-69)
Signature
_DEFAULT_PLAN_CONTEXT_THRESHOLD: float = 0.65 # default value
def _load_plan_context_threshold() -> float:
"""Load the configured plan contextuality threshold (0.0-1.0).
Override with the ``ALWRITY_PLAN_CONTEXT_THRESHOLD`` env var.
Invalid values fall back to the default with a warning.
"""
PLAN_CONTEXT_THRESHOLD = _load_plan_context_threshold() # module constant
Import
from services.today_workflow_service import PLAN_CONTEXT_THRESHOLD
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| ALWRITY_PLAN_CONTEXT_THRESHOLD | env var | No | Override value (float 0.0-1.0) |
Outputs
| Name | Type | Description |
|---|---|---|
| PLAN_CONTEXT_THRESHOLD | float | Module constant (default 0.65, or env override) |
Usage Examples
Using the threshold to gate a plan
from services.today_workflow_service import PLAN_CONTEXT_THRESHOLD
def validate_plan(plan, user_context):
score = cosine_similarity(embed(plan), embed(user_context))
if score < PLAN_CONTEXT_THRESHOLD:
logger.warning(f"Plan rejected: context score {score} < threshold {PLAN_CONTEXT_THRESHOLD}")
return None # trigger regeneration
return plan
Overriding via environment
# Tighten the quality gate
export ALWRITY_PLAN_CONTEXT_THRESHOLD=0.75
# Loosen it (accept more plans)
export ALWRITY_PLAN_CONTEXT_THRESHOLD=0.50
# Invalid values fall back to default with a warning
export ALWRITY_PLAN_CONTEXT_THRESHOLD=invalid # logs warning, uses 0.65