Files
betterxen/scripts/agent_queue/patterns.py
T
elforeign e35df04069 Expand the agent task queue to cpu/gpu/runtime/shared/qa with deterministic checks
The queue now has three engineering tracks plus shared and qa work. Queue
rules move from shell/awk in workflows into a standard-library Python package
so they are testable offline: claim decisions (dependencies, cycles, file
overlap, area permission), leases renewed only by the holder, reassign and
release, acceptance-command approval by hash, pull request ownership/overlap/
stale/verification checks and a machine-readable summary. Workflows apply the
decisions with minimal permissions, never put user text in run blocks and
never check out pull request code. The sync script fetches the real upstream
so interface changes during work are reported.

Not yet run on GitHub.

Signed-off-by: elforeign <324243036+elforeign@users.noreply.github.com>
2026-11-24 00:09:38 -06:00

79 lines
2.1 KiB
Python

# SPDX-License-Identifier: GPL-3.0-or-later
"""File ownership patterns: exact paths, directory prefixes ("dir/") and globs ("*", "**")."""
import re
def _norm(p):
p = p.strip().replace("\\", "/")
while p.startswith("./"):
p = p[2:]
return p.lstrip("/")
def _is_glob(p):
return any(c in p for c in "*?[")
def _regex(pattern):
out, i = [], 0
while i < len(pattern):
if pattern.startswith("**/", i):
out.append("(?:.*/)?")
i += 3
elif pattern.startswith("**", i):
out.append(".*")
i += 2
elif pattern[i] == "*":
out.append("[^/]*")
i += 1
elif pattern[i] == "?":
out.append("[^/]")
i += 1
else:
out.append(re.escape(pattern[i]))
i += 1
return re.compile("^" + "".join(out) + "$")
def path_matches(path, pattern):
path, pattern = _norm(path), _norm(pattern)
if not pattern:
return False
if pattern.endswith("/"):
return path.startswith(pattern)
if _is_glob(pattern):
return bool(_regex(pattern).match(path))
return path == pattern
def _prefix(pattern):
"""Literal leading part of a pattern (the whole directory for a "dir/" pattern)."""
for i, c in enumerate(pattern):
if c in "*?[":
return pattern[:i]
return pattern
def patterns_overlap(a, b):
"""True when some path could match both patterns. Errs towards True."""
a, b = _norm(a), _norm(b)
if not a or not b:
return False
a_exact = not _is_glob(a) and not a.endswith("/")
b_exact = not _is_glob(b) and not b.endswith("/")
if a_exact:
return path_matches(a, b)
if b_exact:
return path_matches(b, a)
pa, pb = _prefix(a), _prefix(b)
return pa.startswith(pb) or pb.startswith(pa)
def sets_overlap(files_a, files_b):
"""First overlapping pair (a, b) between two pattern lists, or None."""
for a in files_a:
for b in files_b:
if patterns_overlap(a, b):
return (a, b)
return None