Something went wrong. Try again.
small gleam coding and persistent agent daemon with a detachable cli and a canvas ui forked from okami.mom/albedo
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128"""A stand-in redaction worker for the export scenarios: the export-redactprotocol over 4-byte framed JSON, with regular expressions in place of themodel. A file named ``fixture-mode`` in its model directory makes itmisbehave the way a scenario asks: crash, garbage, mismatch, badspan, slow."""
import jsonimport osfrom pathlib import Pathimport reimport structimport sysimport time
RULES = [ ("email", re.compile(r"[A-Za-z0-9._+-]+@[A-Za-z0-9-]+\.[A-Za-z0-9.]+")), ("phone", re.compile(r"\+?\d[\d -]{7,}\d")), ("secret_token", re.compile(r"tok_[A-Za-z0-9]{8,}")),]
def read(stream): header = stream.read(4) if len(header) < 4: return None (size,) = struct.unpack(">I", header) return json.loads(stream.read(size))
def write(stream, message): body = json.dumps(message, ensure_ascii=False).encode("utf-8") stream.write(struct.pack(">I", len(body)) + body) stream.flush()
def redact(text, keeps, numbers): """The text with every rule match outside the kept spans replaced, and the output spans of both.""" found = [] for category, rule in RULES: for match in rule.finditer(text): start, end = match.span() if any(start < k["end"] and k["start"] < end for k in keeps): continue if any(start < f[1] and f[0] < end for f in found): continue found.append((start, end, category, "rule")) for k in keeps: found.append((k["start"], k["end"], k["category"], "keep")) found.sort() out, spans, at = [], [], 0 length = 0 for start, end, category, source in found: piece = text[at:start] out.append(piece) length += len(piece) if source == "keep": marker = text[start:end] else: key = (category, text[start:end]) numbers.setdefault(key, len([k for k in numbers if k[0] == category]) + 1) marker = f"[REDACTED:{category}#{numbers[key]}]" spans.append( { "start": length, "end": length + len(marker), "category": category, "source": source, } ) out.append(marker) length += len(marker) at = end out.append(text[at:]) return "".join(out), spans
def main(): writer = os.fdopen(os.dup(1), "wb") reader = sys.stdin.buffer directory = Path(os.environ["ALBEDO_REDACT_MODEL_DIR"]) (directory / "fixture-pid").write_text(str(os.getpid())) mode_file = directory / "fixture-mode" mode = mode_file.read_text().strip() if mode_file.exists() else "" manifest = json.loads(Path(os.environ["ALBEDO_EXPORT_TAGGER_MANIFEST"]).read_text()) model_id = "someone-else/model" if mode == "mismatch" else manifest["model_id"] write( writer, { "type": "hello", "protocol": 1, "model_id": model_id, "rules_version": manifest["rules_version"], "categories": ["email", "phone", "secret_token"], }, ) numbers = {} while True: request = read(reader) if request is None or request.get("type") == "close": return 0 batch = request["batch"] if mode == "crash": os._exit(3) if mode == "garbage": body = b"not json" writer.write(struct.pack(">I", len(body)) + body) writer.flush() continue if mode == "slow": while True: write( writer, {"type": "progress", "batch": batch, "done": 1, "total": 2} ) time.sleep(0.5) items = [] for item in request["items"]: text, spans = redact(item["text"], item.get("keep", []), numbers) if mode == "badspan" and spans: spans[0]["end"] = len(text) + 5 items.append({"key": item["key"], "text": text, "spans": spans}) write(writer, {"type": "progress", "batch": batch, "done": 0, "total": 1}) write(writer, {"type": "result", "batch": batch, "items": items})
if __name__ == "__main__": sys.exit(main())