forge
Explore
Log in

Don't you dare go Hollow

hezarfen_rule_based_chatbot

Clone
★ 0

0 watching0 forksPython1.2 MiB3c4e59dKadir Yönak3 months ago15 commits

access.py92 lines, 2.5 KiBHistoryRawDownload
1"""Typed role-space access contract.
2
3The legacy catalog describes content. This module describes whether a piece of
4content is actionable for one concrete, trusted role. Keeping those concerns
5separate prevents a global retrieval result from becoming authorised merely
6because a rank-based ``min_role`` comparison happened to pass.
7"""
8
9from __future__ import annotations
10
11from dataclasses import dataclass
12from enum import Enum
13from types import MappingProxyType
14from typing import Any, Mapping
15
16
17class AccessOutcome(str, Enum):
18 ALLOW = "allow"
19 DENY = "deny"
20 CLARIFY = "clarify"
21 FALLBACK = "fallback"
22
23
24class AccessScope(str, Enum):
25 GENERAL = "general"
26 OWN = "own"
27 LINKED_CHILD = "linked_child"
28 ENROLLED_COURSE = "enrolled_course"
29 MANAGED_COURSE = "managed_course"
30 OWNED_RESOURCE = "owned_resource"
31 PARTICIPANT = "participant"
32 REQUESTER = "requester"
33 EVENT_AUDIENCE = "event_audience"
34 SCHOOL = "school"
35
36
37class ReasonCode(str, Enum):
38 ALLOWED = "allowed"
39 AUTHENTICATION_REQUIRED = "authentication_required"
40 ROLE_NOT_PERMITTED = "role_not_permitted"
41 EXACT_STUDENT_ONLY = "exact_student_only"
42 LINKED_STUDENT_SCOPE = "linked_student_scope"
43 TEACHER_OR_HIGHER_REQUIRED = "teacher_or_higher_required"
44 UI_ROUTE_UNAVAILABLE = "ui_route_unavailable"
45 CONTEXT_REQUIRED = "context_required"
46
47
48@dataclass(frozen=True)
49class RoleIntentView:
50 """One intent as compiled for exactly one role."""
51
52 role: str
53 intent: str
54 outcome: AccessOutcome
55 scope: AccessScope | None
56 reason_code: ReasonCode
57 response_id: str
58 response_template: str
59 examples: tuple[str, ...]
60 action_id: str
61 clarification: str | None = None
62 required_role: str | None = None
63 route_key: str | None = None
64 route_label: str | None = None
65
66 @property
67 def view_id(self) -> str:
68 return f"{self.role}:{self.intent}"
69
70
71@dataclass(frozen=True)
72class CompiledRoleSpace:
73 """Immutable retrieval and policy bundle for one trusted role."""
74
75 role: str
76 version: str
77 content_hash: str
78 views: Mapping[str, RoleIntentView]
79 rule_matcher: Any
80 similarity_matcher: Any
81 domain_vocab: frozenset[str]
82
83 def view_for(self, intent: str) -> RoleIntentView | None:
84 return self.views.get(intent)
85
86 @property
87 def space_id(self) -> str:
88 return f"{self.version}:{self.role}:{self.content_hash[:12]}"
89
90 @classmethod
91 def immutable_views(cls, views: dict[str, RoleIntentView]) -> Mapping[str, RoleIntentView]:
92 return MappingProxyType(dict(views))