"""Instructions about the policy itself, taken out of the sentence before it is read. Two kinds, both of which the vocabulary would otherwise misread: - the policy's NAME: "..., the policy name is Endpoint", "name it USB lock", "a policy called Interns USB that blocks usb". Left in, "name" reads as the PERSON_NAME detector and the name itself as an unknown group or ignored words. - WHERE it is enforced, said as the product rather than a control: "in the agent", "on all laptops", "an endpoint policy". These all mean the Devices & Browsers policy. Left in, "the agent" reads as a group called "agent". - an EXISTING policy to add to: "add printing to the USB Lock policy", "update Finance Lockdown to also block bluetooth". Only with an editing cue (add, update, also, "the X policy" ...): copilot-made names read like requests ("Block USB storage"), so a bare match proves nothing. extract() returns the sentence without them plus what they said. It is purely textual and never raises; anything it does not recognise is left for the interpreter. """ from __future__ import annotations import re from dataclasses import dataclass MAX_NAME = 120 _Q = r'["\'“”‘’]' # The name runs to the end of the clause: a comma, semicolon, full stop or the end of the text. _TAIL = r'(?P' + _Q + r'[^"\'“”‘’]{1,200}' + _Q + r'|[^,;.]{1,200}?)\s*(?=[,;.]|$)' _NAME_PATTERNS = [ # "the policy name is X", "policy name: X", "its name should be X", "name of the policy is X" re.compile(r'(?:^|[,;.]|\band\b)\s*(?:and\s+)?(?:the\s+|its\s+|with\s+)?(?:policy\s+name|name\s+of\s+(?:the|this)\s+policy|' r'its\s+name|the\s+name)\s*(?:is|should\s+be|will\s+be|=|:|as)\s*' + _TAIL, re.I), # "name it X", "call this policy X", "title it as X", "save it as X" re.compile(r'(?:^|[,;.]|\band\b)\s*(?:and\s+)?(?:please\s+)?(?:name|call|title|label|save)\s+' r'(?:this\s+policy|the\s+policy|it|this)\s+(?:as\s+)?' + _TAIL, re.I), # "with the name X", "with name X" re.compile(r'\s*\bwith\s+(?:the\s+)?(?:policy\s+)?name\s+' + _TAIL, re.I), # "..., named X" / "..., called X" at the end of a clause re.compile(r'[,;]\s*(?:and\s+)?(?:named|called|titled)\s+' + _TAIL, re.I), ] # "a policy named X that blocks usb": the name stops at the verb clause; the rest is kept. _NAMED_POLICY = re.compile( r'\b(?P(?:an?|the|new)?\s*polic(?:y|ies))\s+(?:named|called|titled)\s+' r'(?P' + _Q + r'[^"\'“”‘’]{1,200}' + _Q + r'|.{1,200}?)' r'(?=\s+(?:that|which|to|for|blocking|which|where)\b|[,;.]|$)', re.I) _NOUN = r'(?:agents?|endpoints?|laptops?|desktops?|computers?|pcs?|machines?|workstations?|devices?|browsers?|chrome|edge)' _ADJ = r'(?:(?:cloak|windows|desktop|endpoint|employee|employees|user|users|company|work|staff|corporate|managed)\s+)?' # "in the agent", "on all laptops", "via the CLOAK agent", "across every endpoint" _SURFACE = re.compile(r'\s*\b(?:in|on|via|through|using|with|from|across|at)\s+' r'(?:the\s+|our\s+|all\s+(?:the\s+|our\s+)?|every\s+|each\s+|their\s+|employees\'?\s+)?' + _ADJ + _NOUN + r'\b', re.I) # "an endpoint policy", "agent policy", "device policy" _SURFACE_POLICY = re.compile(r'\b(?:an?\s+)?(?:endpoint|agent|device|devices|laptop)\s+(?=polic(?:y|ies)\b)', re.I) # A sentence that opens with the surface as a label: "Endpoint: block usb", "agent - block usb" _SURFACE_LABEL = re.compile(r'^\s*(?:endpoint|agent|devices?)\s*[:\-–]\s*', re.I) _EDIT_CUE = re.compile(r'\b(?:add|adding|also|update|edit|modify|change|extend|amend|include|append|existing|' r'remove|delete|drop|unblock|from|' r'as\s+well|too)\b', re.I) _EDIT_VERB = re.compile(r'^\s*(?:please\s+|kindly\s+)?(?:update|edit|modify|change|extend|amend)\b\s*', re.I) _REMOVE_VERB = re.compile(r'^\s*(?:please\s+|kindly\s+)?(?:remove|delete|drop|unblock|allow|disable|turn\s+off|' r'take\s+out|stop\s+blocking)\b', re.I) _ADD_VERB = re.compile(r'^\s*(?:please\s+|kindly\s+)?(?:also\s+)?(?:add|include|append|put)\b\s*', re.I) @dataclass class Directives: name: str | None = None surface: str | None = None # 'endpoint' when the sentence names the agent / laptops target: str | None = None # id of an existing policy the request should go into target_add: bool = False # "add X to ": the policy's own action applies to X target_remove: bool = False # "remove X from ": not something the copilot does def _clean_name(raw: str) -> str | None: n = raw.strip().strip('"\'“”‘’').strip() n = re.sub(r'\s+', ' ', n).rstrip(' .,:;!-') if not n or not any(c.isalnum() for c in n): return None return n[:MAX_NAME] def _find_target(text: str, policies: list[tuple[str, str]], d: Directives) -> str: """Take out a reference to an existing policy, when the sentence is about changing one.""" if not policies or not _EDIT_CUE.search(text) and not re.search(r'\bpolicy\b', text, re.I): return text for pid, name in sorted(policies, key=lambda p: -len(p[1])): if len(name.strip()) < 3: continue esc = re.escape(name.strip()).replace('\\ ', '\\s+') quoted = _Q + esc + _Q rx = re.compile(r'\s*(?:\b(?:to|into|in|on|for|from)\s+)?(?:the\s+)?(?:existing\s+|current\s+)?(?:policy\s+)?' r'(?:' + quoted + r'|\b' + esc + r'\b)(?:\s+polic(?:y|ies)\b)?', re.I) m = rx.search(text) if not m: continue said = m.group(0).lower() is_quoted = bool(re.search(quoted, m.group(0), re.I)) if not (is_quoted or 'policy' in said or _EDIT_CUE.search(text)): continue d.target = pid d.target_remove = bool(_REMOVE_VERB.match(text)) text = (text[:m.start()] + ' ' + text[m.end():]).strip() text = _EDIT_VERB.sub('', text) if _ADD_VERB.match(text): d.target_add = True text = _ADD_VERB.sub('', text) text = re.sub(r'^\s*(?:to|so\s+that\s+it|so\s+it|and)\s+', '', text, flags=re.I) text = re.sub(r'\b(?:also|as\s+well|too)\b', '', text, flags=re.I) break return text def extract(text: str, group_names: set[str] | None = None, policies: list[tuple[str, str]] | None = None) -> tuple[str, Directives]: d = Directives() if not isinstance(text, str) or not text: return text, d groups = {g.lower() for g in (group_names or set())} for rx in _NAME_PATTERNS: m = rx.search(text) if m and (name := _clean_name(m.group('n'))): d.name = name text = (text[:m.start()] + text[m.end():]).strip(' ,;') break if d.name is None: m = _NAMED_POLICY.search(text) if m and (name := _clean_name(m.group('n'))): d.name = name text = (text[:m.start()] + m.group('lead') + text[m.end():]).strip(' ,;') text = _find_target(text, policies or [], d) # A tenant group really called "Agents" or "Devices" keeps its words. def surface(m: re.Match) -> str: words = m.group(0).strip().lower().split() if words and words[-1] in groups: return m.group(0) d.surface = 'endpoint' return '' text = _SURFACE.sub(surface, text) text = _SURFACE_POLICY.sub(lambda m: (setattr(d, 'surface', 'endpoint'), '')[1], text) text = _SURFACE_LABEL.sub(lambda m: (setattr(d, 'surface', 'endpoint'), '')[1], text) text = re.sub(r'\s{2,}', ' ', text).strip(' ,;') return text, d