From e8a4bcb3fa0ca4203f60e51182e319b57ba5bd59 Mon Sep 17 00:00:00 2001 From: Grok Bot Date: Thu, 29 Oct 2026 16:09:02 -0400 Subject: [PATCH 01/19] bench: add scripts/bench_host.py (time-to-first / time-to-final) --- scripts/bench_host.py | 208 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 208 insertions(+) create mode 100644 scripts/bench_host.py diff --git a/scripts/bench_host.py b/scripts/bench_host.py new file mode 100644 index 0000000..f4a6aeb --- /dev/null +++ b/scripts/bench_host.py @@ -0,0 +1,208 @@ +#!/usr/bin/env python3 +"""Measure Pallet host latency: time-to-first and time-to-final per query. + +The bench copies the plugin into a temporary directory, points the host at a +synthetic $HOME (so the files provider indexes a realistic tree without +touching real data), optionally adds a slow "straggler" provider that stands +in for a heavy AT-SPI menu walk, and then drives the host over its normal +NDJSON stdin/stdout contract. + + python3 scripts/bench_host.py # default scenario + python3 scripts/bench_host.py --files 60000 --straggler-ms 1500 --json + +Older hosts emit one message per query; newer hosts may emit early partial +messages and mark the last one with "final": true. Both are handled: a message +without a "final" key is treated as final. +""" +from __future__ import annotations + +import argparse +import json +import os +import random +import select +import shutil +import statistics +import subprocess +import sys +import tempfile +import time +from pathlib import Path + +ROOT = Path(__file__).resolve().parent.parent +WORDS = ("notes report config project invoice draft photo screenshot budget " + "meeting todo readme design src lib test build docs archive music " + "video thesis resume letter plan backup theme dotfiles scripts").split() +EXTS = (".md", ".txt", ".py", ".pdf", ".png", ".jpg", ".json", ".toml", ".sh", ".lua") +WARM_QUERIES = ("fire", "lock", "screenshot", "theme", "notes", "report.pdf", + "f:budget", "reboot", "term", "config") +TYPING = ("c", "co", "con", "conf", "confi", "config") + +STRAGGLER = r'''#!/usr/bin/env python3 +import json, sys, time +DELAY = float(sys.argv[1]) +for line in sys.stdin: + try: msg = json.loads(line) + except json.JSONDecodeError: continue + if msg.get("op") != "search": continue + time.sleep(DELAY) + print(json.dumps({"id": msg["id"], "results": [{"id": "slow:" + msg["query"], + "title": "Slow " + msg["query"], "score": 0.3, + "actions": [{"type": "copy", "text": "x"}]}]}), flush=True) +''' + + +def make_home(count: int) -> Path: + home = Path(tempfile.gettempdir()) / f"pallet-bench-home-{count}" + marker = home / ".pallet-bench-complete" + if marker.exists(): + return home + shutil.rmtree(home, ignore_errors=True) + rng = random.Random(42) + made = 0 + dirs = [home] + while made < count: + parent = rng.choice(dirs) + if len(dirs) < count // 20 and rng.random() < 0.08: + d = parent / f"{rng.choice(WORDS)}-{made}" + d.mkdir(parents=True, exist_ok=True) + dirs.append(d) + else: + parent.mkdir(parents=True, exist_ok=True) + name = f"{rng.choice(WORDS)}_{rng.choice(WORDS)}_{made}{rng.choice(EXTS)}" + (parent / name).touch() + made += 1 + marker.touch() + return home + + +def stage_plugin(straggler_ms: int) -> Path: + stage = Path(tempfile.mkdtemp(prefix="pallet-bench-")) + for part in ("src", "providers"): + shutil.copytree(ROOT / part, stage / part, ignore=shutil.ignore_patterns("__pycache__")) + if straggler_ms > 0: + d = stage / "providers" / "slowpoke" + d.mkdir() + (d / "provider.py").write_text(STRAGGLER) + (d / "manifest.json").write_text(json.dumps({ + "name": "slowpoke", "exec": ["python3", "providers/slowpoke/provider.py", str(straggler_ms / 1000)]})) + return stage + + +class HostClient: + def __init__(self, stage: Path, home: Path): + env = dict(os.environ, HOME=str(home), XDG_DATA_HOME=str(home / ".local/share")) + env.pop("OMARCHY_PATH", None) + self.t0 = time.monotonic() + self.proc = subprocess.Popen([sys.executable, str(stage / "src" / "host.py")], stdin=subprocess.PIPE, + stdout=subprocess.PIPE, stderr=subprocess.DEVNULL, env=env, cwd=stage, text=True, bufsize=1) + self.buf = "" + self.n = 0 + + def send(self, msg): + self.proc.stdin.write(json.dumps(msg) + "\n") + self.proc.stdin.flush() + + def readline(self, timeout: float): + end = time.monotonic() + timeout + while "\n" not in self.buf: + left = end - time.monotonic() + if left <= 0: + return None + ready, _, _ = select.select([self.proc.stdout], [], [], left) + if not ready: + return None + chunk = os.read(self.proc.stdout.fileno(), 65536).decode() + if not chunk: + return None + self.buf += chunk + line, self.buf = self.buf.split("\n", 1) + return json.loads(line) if line.strip() else {} + + def query(self, text: str, timeout: float = 5.0, sent_at: float | None = None): + self.n += 1 + qid = f"b{self.n}" + start = sent_at if sent_at is not None else time.monotonic() + self.send({"id": qid, "op": "search", "query": text}) + first = final = None + first_files = None + count = 0 + while True: + msg = self.readline(max(0.0, start + timeout - time.monotonic())) + if msg is None: + break + if msg.get("id") != qid or "results" not in msg: + continue + now = (time.monotonic() - start) * 1000 + first = now if first is None else first + if first_files is None and any(r.get("provider") == "files" for r in msg["results"]): + first_files = now + count = len(msg["results"]) + if msg.get("final", True): + final = now + break + return {"query": text, "first_ms": first, "final_ms": final, "files_ms": first_files, "results": count} + + def close(self): + try: + self.proc.stdin.close() + self.proc.wait(timeout=3) + except Exception: + self.proc.kill() + + +def fmt(v): + return " - " if v is None else f"{v:7.1f}" + + +def main(): + ap = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter) + ap.add_argument("--files", type=int, default=60000, help="synthetic $HOME entries (default 60000)") + ap.add_argument("--straggler-ms", type=int, default=0, help="add a provider that answers after N ms") + ap.add_argument("--rounds", type=int, default=3, help="warm rounds per query (median reported)") + ap.add_argument("--json", action="store_true") + args = ap.parse_args() + + home = make_home(args.files) + stage = stage_plugin(args.straggler_ms) + report = {"files": args.files, "straggler_ms": args.straggler_ms, "cold": None, "warm": [], "typing": []} + try: + client = HostClient(stage, home) + # Cold: query the instant the host is spawned, like a palette opened at login. + cold = client.query("notes", timeout=15.0, sent_at=client.t0) + report["cold"] = cold + # Let background work (if any) settle before measuring warm latency. + time.sleep(2.0) + client.query("warmup", timeout=5.0) + for q in WARM_QUERIES: + runs = [client.query(q) for _ in range(args.rounds)] + report["warm"].append({ + "query": q, + "first_ms": statistics.median([r["first_ms"] or 0 for r in runs]), + "final_ms": statistics.median([r["final_ms"] or 0 for r in runs]), + "results": runs[-1]["results"], + }) + for q in TYPING: + report["typing"].append(client.query(q)) + client.close() + finally: + shutil.rmtree(stage, ignore_errors=True) + + if args.json: + print(json.dumps(report, indent=2)) + return + c = report["cold"] + print(f"synthetic HOME entries: {args.files} straggler: {args.straggler_ms or 'none'} ms") + print(f"cold 'notes' (sent at spawn): first {fmt(c['first_ms'])} ms final {fmt(c['final_ms'])} ms files {fmt(c['files_ms'])} ms") + print(f"{'query':<14}{'first ms':>10}{'final ms':>10}{'results':>9}") + for r in report["warm"]: + print(f"{r['query']:<14}{fmt(r['first_ms']):>10}{fmt(r['final_ms']):>10}{r['results']:>9}") + w = report["warm"] + print(f"{'median':<14}{fmt(statistics.median(r['first_ms'] for r in w)):>10}{fmt(statistics.median(r['final_ms'] for r in w)):>10}") + print("typing 'config' one key at a time:") + for r in report["typing"]: + print(f" {r['query']:<12}{fmt(r['first_ms']):>10}{fmt(r['final_ms']):>10}{r['results']:>9}") + + +if __name__ == "__main__": + main() From 96e1e655c559494ff27b30cfd73338c2d89a5159 Mon Sep 17 00:00:00 2001 From: Grok Bot Date: Thu, 29 Oct 2026 16:14:15 -0400 Subject: [PATCH 02/19] host: stream partial results, route by scope/minQuery, forward action replies - Emit merged results as soon as the first provider answers (coalesced), then follow-ups under the same query id; final message keeps the old fields (results/scope/bare/unresolved) plus final/pending/failed. - Stragglers after the deadline refresh the final view while connected. - Providers may send partial replies; crashed providers restart lazily. - Manifest routing hints (minQuery, scopedOnly) skip irrelevant providers; more scope prefixes (om:, s:, w:, m:, p:, run:, web:). - Forward provider execute replies, report unknown/dead providers, and add op:launch (argv, no shell) so missing commands surface as failures. - op:warm lets providers refresh caches when the overlay opens. --- providers/files/manifest.json | 10 +- providers/menus/manifest.json | 11 +- providers/run/manifest.json | 10 +- src/host.py | 241 +++++++++++++++++++++++++++------- tests/test_streaming.py | 188 ++++++++++++++++++++++++++ 5 files changed, 409 insertions(+), 51 deletions(-) create mode 100644 tests/test_streaming.py diff --git a/providers/files/manifest.json b/providers/files/manifest.json index 6da4d26..f48a11e 100644 --- a/providers/files/manifest.json +++ b/providers/files/manifest.json @@ -1,7 +1,13 @@ { "name": "files", "description": "Search files and directories under $HOME", - "exec": ["python3", "providers/files/provider.py"], + "exec": [ + "python3", + "providers/files/provider.py" + ], "triggers": [], - "capabilities": ["search"] + "capabilities": [ + "search" + ], + "minQuery": 3 } diff --git a/providers/menus/manifest.json b/providers/menus/manifest.json index c085a23..e4c0a28 100644 --- a/providers/menus/manifest.json +++ b/providers/menus/manifest.json @@ -1,7 +1,14 @@ { "name": "menus", "description": "Search and run the focused app's menu items (AT-SPI). Adapted from MaNi4 (MIT). Requires python-gobject and AT-SPI enabled.", - "exec": ["python3", "providers/menus/provider.py"], + "exec": [ + "python3", + "providers/menus/provider.py" + ], "triggers": [], - "capabilities": ["search", "execute"] + "capabilities": [ + "search", + "execute" + ], + "minQuery": 2 } diff --git a/providers/run/manifest.json b/providers/run/manifest.json index 83251ff..5592d9b 100644 --- a/providers/run/manifest.json +++ b/providers/run/manifest.json @@ -1,7 +1,13 @@ { "name": "run", "description": "Suggest running the typed query as a shell command in a visible terminal", - "exec": ["python3", "providers/run/provider.py"], + "exec": [ + "python3", + "providers/run/provider.py" + ], "triggers": [], - "capabilities": ["search"] + "capabilities": [ + "search" + ], + "minQuery": 3 } diff --git a/src/host.py b/src/host.py index 224daea..316d629 100644 --- a/src/host.py +++ b/src/host.py @@ -1,21 +1,55 @@ #!/usr/bin/env python3 -"""Pallet core host with provider-scoped prefixes. +"""Pallet core host: fans a query out to providers and streams merged results. -`files:`/`f:`, `obsidian:`/`o:`, and `apps:`/`app:` focus matching providers. -The prefix is stripped before providers see it; responses echo `scope` and -`bare` so the UI rankings match what providers matched. +Scope prefixes (`files:`/`f:`, `obsidian:`/`o:`, `apps:`/`a:`, `omarchy:`/`om:`, +`system:`/`sys:`, `windows:`/`w:`, `menus:`/`m:`, `plugins:`/`p:`, `run:`, +`web:`) focus one provider. The prefix is stripped before providers see it; +responses echo `scope` and `bare` so UI ranking matches what providers matched. + +Streaming contract (backwards compatible): every message for a query carries +the *complete* merged result set so far, so a consumer that only keeps the +last message per id still sees everything. Early messages carry +`"final": false` and list the providers still `pending`; the final message has +`"final": true` and lists `unresolved` providers (timed out or failed). A +provider that answers after the deadline produces one more final message with +the late results while its query is still current. """ from __future__ import annotations import asyncio import contextlib import json +import os +import shutil +import subprocess import sys +import threading import time from pathlib import Path ROOT = Path(__file__).resolve().parent.parent PROVIDERS_DIR = ROOT / "providers" -MERGE_TIMEOUT_S = 0.5 -SCOPES = {"f": "files", "file": "files", "files": "files", "o": "obsidian", "ob": "obsidian", "obsidian": "obsidian", "a": "apps", "app": "apps", "apps": "apps"} +# Deadline for the final message (footer reports providers missing it). +MERGE_TIMEOUT_S = 0.6 +# First partial emit waits this long after the request so near-simultaneous +# replies land in one paint instead of several. +EARLY_EMIT_S = 0.012 +# Later partial emits are coalesced to at most one per window. +COALESCE_S = 0.03 +# After the final message, keep listening this long for stragglers (only while +# the client is connected) so slow providers still show up instead of vanishing. +LATE_GRACE_S = 2.5 +RESTART_BACKOFF_S = 30.0 +SCOPES = { + "f": "files", "file": "files", "files": "files", + "o": "obsidian", "ob": "obsidian", "obsidian": "obsidian", + "a": "apps", "app": "apps", "apps": "apps", + "om": "omarchy", "omarchy": "omarchy", "menu": "omarchy", + "s": "system", "sys": "system", "system": "system", + "w": "windows", "win": "windows", "window": "windows", "windows": "windows", + "m": "menus", "menus": "menus", + "p": "plugins", "plugin": "plugins", "plugins": "plugins", + "run": "run", + "web": "defaults", "default": "defaults", "defaults": "defaults", +} def log(*args): print("host:", *args, file=sys.stderr, flush=True) @@ -43,31 +77,88 @@ def split_scope(query: str) -> tuple[str, str]: if not scope: return "", text return scope, rest.strip() +def _int(value, default=0): + return value if isinstance(value, int) and not isinstance(value, bool) and value >= 0 else default + class Provider: - def __init__(self, name, argv, inbox): self.name, self.argv, self.inbox, self.proc = name, argv, inbox, None + def __init__(self, name, argv, inbox, spec=None, on_reply=None): + spec = spec or {} + self.name, self.argv, self.inbox, self.proc = name, argv, inbox, None + self.on_reply = on_reply + # Routing hints from the manifest. `minQuery`: skip unscoped queries + # shorter than this. `scopedOnly`: only answer when explicitly scoped. + self.min_query = _int(spec.get("minQuery"), 0) + self.scoped_only = spec.get("scopedOnly") is True + self.started_at = 0.0 + self.restarts = 0 @property def alive(self): return self.proc is not None and self.proc.returncode is None + def wants(self, scope: str, bare: str) -> bool: + if scope: return scope == self.name + if self.scoped_only: return False + return len(bare) >= self.min_query async def start(self): - try: self.proc = await asyncio.create_subprocess_exec(*self.argv, stdin=asyncio.subprocess.PIPE, stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE, cwd=ROOT) - except OSError as err: log(f"{self.name}: failed to start: {err}"); return - asyncio.create_task(self._read_stdout()); asyncio.create_task(self._read_stderr()) - async def _read_stdout(self): - async for raw in self.proc.stdout: - line = raw.strip() - if not line: continue - try: msg = json.loads(line) - except json.JSONDecodeError: continue - await self.inbox.put((self.name, msg)) + self.started_at = time.monotonic() + try: self.proc = await asyncio.create_subprocess_exec(*self.argv, stdin=asyncio.subprocess.PIPE, stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE, cwd=ROOT, limit=1 << 22) + except OSError as err: log(f"{self.name}: failed to start: {err}"); self.proc = None; return + asyncio.create_task(self._read_stdout(self.proc)); asyncio.create_task(self._read_stderr(self.proc)) + async def ensure_running(self): + """Restart a crashed provider at most once per backoff window.""" + if self.alive or time.monotonic() - self.started_at < RESTART_BACKOFF_S: return + self.restarts += 1 + log(f"{self.name}: restarting (attempt {self.restarts})") + await self.start() + async def _read_stdout(self, proc): + try: + async for raw in proc.stdout: + line = raw.strip() + if not line: continue + try: msg = json.loads(line) + except json.JSONDecodeError: continue + if not isinstance(msg, dict): continue + if "executed" in msg and self.on_reply is not None: + # Action replies go straight to the client; they never + # belong to a search merge. + self.on_reply(self.name, msg); continue + await self.inbox.put((self.name, msg)) + except (ValueError, asyncio.LimitOverrunError) as err: + log(f"{self.name}: unreadable output: {err}") log(f"{self.name}: exited") - async def _read_stderr(self): - async for raw in self.proc.stderr: log(f"{self.name}: {raw.decode(errors='replace').rstrip()}") + async def _read_stderr(self, proc): + async for raw in proc.stderr: log(f"{self.name}: {raw.decode(errors='replace').rstrip()}") async def send(self, msg): if not self.alive: return try: self.proc.stdin.write(json.dumps(msg).encode()+b"\n"); await self.proc.stdin.drain() except (BrokenPipeError, ConnectionResetError): pass +def _reap(proc): + with contextlib.suppress(Exception): proc.wait() + +def spawn_detached(argv): + """Run argv without a shell in its own session. Returns an error string or None.""" + if not isinstance(argv, list) or not argv or not all(isinstance(a, str) and a for a in argv): + return "invalid command" + if shutil.which(argv[0]) is None: + return f"command not found: {argv[0]}" + try: + proc = subprocess.Popen(argv, stdin=subprocess.DEVNULL, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, + start_new_session=True, cwd=os.path.expanduser("~"), close_fds=True) + except OSError as err: + return f"{argv[0]}: {err.strerror or err}" + threading.Thread(target=_reap, args=(proc,), daemon=True).start() + return None + class Host: - def __init__(self): self.inbox = asyncio.Queue(); self.providers = []; self.merge_task = None; self.latest_qid = None + def __init__(self): + self.inbox = asyncio.Queue(); self.providers = []; self.merge_task = None; self.latest_qid = None + # True while the client's stdin is open; enables late straggler emits. + self.listening = False + def on_provider_reply(self, name, msg): + reply = {"id": msg.get("id", ""), "executed": msg.get("executed") is True, "provider": name} + if isinstance(msg.get("error"), str): reply["error"] = msg["error"][:300] + self.emit(reply) + def add_provider(self, name, argv, spec=None): + self.providers.append(Provider(name, argv, self.inbox, spec, self.on_provider_reply)) async def send_to(self, names, msg): chosen = [p for p in self.providers if names is None or p.name in names] await asyncio.gather(*(p.send(msg) for p in chosen), return_exceptions=True) @@ -76,10 +167,14 @@ async def discard_pending(self): self.merge_task.cancel() with contextlib.suppress(asyncio.CancelledError): await self.merge_task if self.latest_qid is not None: await self.send_to(None, {"id":"","op":"cancel","target":self.latest_qid}) + def route(self, scope, bare): + return {p.name for p in self.providers if p.wants(scope, bare)} async def on_search(self, msg): scope, bare = split_scope(str(msg.get("query", ""))) await self.discard_pending(); self.latest_qid = str(msg.get("id", "")) - names = {p.name for p in self.providers if scope == "" or p.name == scope} + names = self.route(scope, bare) + for p in self.providers: + if p.name in names: await p.ensure_running() await self.send_to(names, {"id": self.latest_qid, "op": "search", "query": bare, "scope": scope}) self.merge_task = asyncio.create_task(self._merge(self.latest_qid, names, scope, bare)) async def on_cancel(self, msg): @@ -88,51 +183,107 @@ async def on_cancel(self, msg): await self.discard_pending() self.latest_qid = None self.merge_task = None + async def on_warm(self, msg): + # The overlay opened: let providers refresh caches before the first key. + await self.send_to(None, {"id": "", "op": "warm"}) async def on_execute(self, msg): wanted = str(msg.get("provider", "")) + aid = msg.get("id", "") for provider in self.providers: if provider.name == wanted: - await provider.send({"id": msg.get("id", ""), "op": "execute", "action": msg.get("action")}); return + if not provider.alive: + self.emit({"id": aid, "executed": False, "provider": wanted, "error": f"{wanted} provider is not running"}); return + await provider.send({"id": aid, "op": "execute", "action": msg.get("action")}); return + self.emit({"id": aid, "executed": False, "provider": wanted, "error": f"unknown provider: {wanted or '?'}"}) + async def on_launch(self, msg): + """Launch argv (no shell) on the frontend's behalf so failures are reported.""" + err = await asyncio.get_running_loop().run_in_executor(None, spawn_detached, msg.get("argv")) + reply = {"id": msg.get("id", ""), "executed": err is None} + if err: reply["error"] = err; log(f"launch failed: {err}") + self.emit(reply) async def _merge(self, qid, names, scope, bare): + started = time.monotonic() pending = {p.name for p in self.providers if p.alive and p.name in names} - unavailable = set(names) - pending - merged = []; deadline = time.monotonic() + MERGE_TIMEOUT_S - while pending: - remaining = deadline - time.monotonic() - if remaining <= 0: break - try: name, msg = await asyncio.wait_for(self.inbox.get(), remaining) - except asyncio.TimeoutError: break - if msg.get("id") != qid or "results" not in msg or name not in pending: continue - pending.discard(name) + failed = set(names) - pending + by_provider: dict[str, list] = {} + order = [p.name for p in self.providers] + deadline = started + MERGE_TIMEOUT_S + late_until = deadline + LATE_GRACE_S + dirty = False + emitted_final = False + next_emit = started + EARLY_EMIT_S + + def snapshot(final): + merged = [] + for name in order: + merged.extend(by_provider.get(name, ())) + msg = {"id": qid, "results": merged, "scope": scope, "bare": bare, "final": final, + "pending": sorted(pending), "unresolved": sorted(pending | failed) if final else [], + "failed": sorted(failed)} + return msg + + while True: + now = time.monotonic() + if not pending: + self.emit(snapshot(True)); return + if not emitted_final and now >= deadline: + unresolved = sorted(pending | failed) + if unresolved: log(f"{qid}: no reply from: {unresolved}") + self.emit(snapshot(True)); emitted_final = True; dirty = False + if emitted_final and (not self.listening or now >= late_until): + return + if dirty and not emitted_final and now >= next_emit: + self.emit(snapshot(False)); dirty = False; next_emit = now + COALESCE_S + wake = late_until if emitted_final else deadline + if dirty and not emitted_final: wake = min(wake, next_emit) + try: name, msg = await asyncio.wait_for(self.inbox.get(), max(0.0, wake - time.monotonic())) + except asyncio.TimeoutError: continue + if msg.get("id") != qid or name not in pending: continue + if msg.get("cancelled") is True: + pending.discard(name); continue + if "results" not in msg or not isinstance(msg["results"], list): continue + rows = [] for result in msg["results"]: - if isinstance(result, dict): result.setdefault("provider", name); merged.append(result) - unresolved = sorted(pending | unavailable) - if unresolved: - log(f"{qid}: no reply from: {unresolved}") - self.emit({"id": qid, "results": merged, "scope": scope, "bare": bare, "unresolved": unresolved}) + if isinstance(result, dict): result.setdefault("provider", name); rows.append(result) + by_provider[name] = rows + if msg.get("partial") is not True: pending.discard(name) + if emitted_final: + # A straggler after the deadline: refresh the final view. + self.emit(snapshot(True)) + else: + dirty = True @staticmethod def emit(msg): print(json.dumps(msg, separators=(",", ":")), flush=True) async def read_client(host): loop = asyncio.get_running_loop(); reader = asyncio.StreamReader(limit=1 << 16) await loop.connect_read_pipe(lambda: asyncio.StreamReaderProtocol(reader), sys.stdin) - while True: - raw = await reader.readline() - if not raw: return - try: msg = json.loads(raw) - except json.JSONDecodeError: continue - if msg.get("op") == "search": await host.on_search(msg) - elif msg.get("op") == "cancel": await host.on_cancel(msg) - elif msg.get("op") == "execute": await host.on_execute(msg) + host.listening = True + try: + while True: + raw = await reader.readline() + if not raw: return + try: msg = json.loads(raw) + except json.JSONDecodeError: continue + if not isinstance(msg, dict): continue + op = msg.get("op") + if op == "search": await host.on_search(msg) + elif op == "cancel": await host.on_cancel(msg) + elif op == "execute": await host.on_execute(msg) + elif op == "launch": await host.on_launch(msg) + elif op == "warm": await host.on_warm(msg) + finally: + host.listening = False async def amain(): host = Host() for manifest in discover_manifests(): try: spec = json.loads(manifest.read_text(encoding="utf-8")) except json.JSONDecodeError: continue + if not isinstance(spec, dict): continue argv = spec.get("exec") if isinstance(argv, list) and argv and all(isinstance(a, str) for a in argv): - host.providers.append(Provider(spec.get("name", manifest.parent.name), resolve_exec(argv), host.inbox)) + host.add_provider(spec.get("name", manifest.parent.name), resolve_exec(argv), spec) for p in host.providers: await p.start() log(f"providers up: {[p.name for p in host.providers if p.alive] or 'none'}") try: diff --git a/tests/test_streaming.py b/tests/test_streaming.py new file mode 100644 index 0000000..1050e12 --- /dev/null +++ b/tests/test_streaming.py @@ -0,0 +1,188 @@ +"""Host streaming, routing, and action-reply behavior.""" +import asyncio +import importlib.util +import json +from pathlib import Path +import os +import select +import shutil +import subprocess +import sys +import tempfile +import time +import unittest +from unittest.mock import patch + +ROOT = Path(__file__).resolve().parents[1] +SPEC = importlib.util.spec_from_file_location('streaming_host', ROOT / 'src/host.py') +host_module = importlib.util.module_from_spec(SPEC) +SPEC.loader.exec_module(host_module) + + +class FakeProvider: + def __init__(self, name, alive=True, min_query=0, scoped_only=False): + self.name, self.alive, self.messages = name, alive, [] + self.min_query, self.scoped_only = min_query, scoped_only + wants = host_module.Provider.wants + async def send(self, message): + self.messages.append(message) + async def ensure_running(self): + pass + + +def later(host, delay, name, message): + async def put(): + await asyncio.sleep(delay) + await host.inbox.put((name, message)) + return asyncio.create_task(put()) + + +class MergeStreamingTests(unittest.IsolatedAsyncioTestCase): + def make_host(self, *providers): + host = host_module.Host() + host.providers = list(providers) + emitted = [] + host.emit = emitted.append + return host, emitted + + async def test_fast_provider_emits_before_slow_one(self): + host, emitted = self.make_host(FakeProvider('apps'), FakeProvider('menus')) + later(host, 0.0, 'apps', {'id': 'q', 'results': [{'title': 'Firefox'}]}) + later(host, 0.15, 'menus', {'id': 'q', 'results': [{'title': 'File'}]}) + with patch.object(host_module, 'MERGE_TIMEOUT_S', 1.0): + await host._merge('q', {'apps', 'menus'}, '', 'fi') + self.assertGreaterEqual(len(emitted), 2) + first, last = emitted[0], emitted[-1] + self.assertFalse(first['final']) + self.assertEqual(first['pending'], ['menus']) + self.assertEqual(first['unresolved'], []) + self.assertEqual([r['title'] for r in first['results']], ['Firefox']) + self.assertTrue(last['final']) + self.assertEqual(last['unresolved'], []) + # Every message carries the full set so far, in provider order. + self.assertEqual([r['provider'] for r in last['results']], ['apps', 'menus']) + + async def test_all_fast_providers_produce_one_final_message(self): + host, emitted = self.make_host(FakeProvider('apps'), FakeProvider('system')) + await host.inbox.put(('apps', {'id': 'q', 'results': []})) + await host.inbox.put(('system', {'id': 'q', 'results': []})) + await host._merge('q', {'apps', 'system'}, '', 'x') + self.assertEqual(len(emitted), 1) + self.assertTrue(emitted[0]['final']) + + async def test_partial_provider_replies_replace_earlier_rows(self): + host, emitted = self.make_host(FakeProvider('files')) + await host.inbox.put(('files', {'id': 'q', 'partial': True, 'results': [{'title': 'a'}]})) + later(host, 0.05, 'files', {'id': 'q', 'results': [{'title': 'a'}, {'title': 'b'}]}) + with patch.object(host_module, 'MERGE_TIMEOUT_S', 1.0): + await host._merge('q', {'files'}, '', 'x') + self.assertFalse(emitted[0]['final']) + self.assertEqual(emitted[0]['pending'], ['files']) + self.assertEqual([r['title'] for r in emitted[-1]['results']], ['a', 'b']) + self.assertTrue(emitted[-1]['final']) + + async def test_straggler_after_deadline_refreshes_final_view(self): + host, emitted = self.make_host(FakeProvider('apps'), FakeProvider('menus')) + host.listening = True + await host.inbox.put(('apps', {'id': 'q', 'results': [{'title': 'A'}]})) + later(host, 0.12, 'menus', {'id': 'q', 'results': [{'title': 'M'}]}) + with patch.object(host_module, 'MERGE_TIMEOUT_S', 0.05), patch.object(host_module, 'LATE_GRACE_S', 1.0): + await host._merge('q', {'apps', 'menus'}, '', 'x') + finals = [m for m in emitted if m['final']] + self.assertEqual(finals[0]['unresolved'], ['menus']) + self.assertEqual(finals[-1]['unresolved'], []) + self.assertEqual([r['title'] for r in finals[-1]['results']], ['A', 'M']) + + async def test_no_late_listening_after_client_eof(self): + host, emitted = self.make_host(FakeProvider('menus')) + host.listening = False + started = time.monotonic() + with patch.object(host_module, 'MERGE_TIMEOUT_S', 0.02), patch.object(host_module, 'LATE_GRACE_S', 5.0): + await host._merge('q', {'menus'}, '', 'x') + self.assertLess(time.monotonic() - started, 1.0) + self.assertEqual(len(emitted), 1) + + async def test_failed_providers_are_listed_separately(self): + host, emitted = self.make_host(FakeProvider('apps'), FakeProvider('menus', alive=False)) + await host.inbox.put(('apps', {'id': 'q', 'results': []})) + with patch.object(host_module, 'MERGE_TIMEOUT_S', 0.05): + await host._merge('q', {'apps', 'menus'}, '', 'x') + self.assertEqual(emitted[-1]['failed'], ['menus']) + self.assertEqual(emitted[-1]['unresolved'], ['menus']) + + async def test_routing_skips_short_and_scoped_only_providers(self): + host, _ = self.make_host(FakeProvider('apps'), FakeProvider('files', min_query=3), + FakeProvider('run', min_query=3), FakeProvider('secret', scoped_only=True)) + self.assertEqual(host.route('', 'fi'), {'apps'}) + self.assertEqual(host.route('', 'fire'), {'apps', 'files', 'run'}) + self.assertEqual(host.route('files', 'f'), {'files'}) + self.assertEqual(host.route('secret', ''), {'secret'}) + + async def test_execute_for_unknown_or_dead_provider_reports_failure(self): + host, emitted = self.make_host(FakeProvider('menus', alive=False)) + await host.on_execute({'id': 'a1', 'provider': 'menus', 'action': {}}) + await host.on_execute({'id': 'a2', 'provider': 'nope', 'action': {}}) + self.assertEqual([m['executed'] for m in emitted], [False, False]) + self.assertIn('not running', emitted[0]['error']) + + async def test_provider_execute_reply_is_forwarded(self): + host, emitted = self.make_host() + host.on_provider_reply('windows', {'id': 'a1', 'executed': False, 'error': 'gone'}) + self.assertEqual(emitted, [{'id': 'a1', 'executed': False, 'provider': 'windows', 'error': 'gone'}]) + + async def test_launch_reports_missing_command(self): + host, emitted = self.make_host() + await host.on_launch({'id': 'l1', 'argv': ['pallet-definitely-missing-command']}) + await host.on_launch({'id': 'l2', 'argv': 'not a list'}) + await host.on_launch({'id': 'l3', 'argv': ['true']}) + self.assertEqual([m['executed'] for m in emitted], [False, False, True]) + self.assertIn('command not found', emitted[0]['error']) + + +class ScopeTests(unittest.TestCase): + def test_new_scopes(self): + self.assertEqual(host_module.split_scope('om: theme'), ('omarchy', 'theme')) + self.assertEqual(host_module.split_scope('sys:reboot'), ('system', 'reboot')) + self.assertEqual(host_module.split_scope('w: foot'), ('windows', 'foot')) + self.assertEqual(host_module.split_scope('web: youtube cats'), ('defaults', 'youtube cats')) + self.assertEqual(host_module.split_scope('nope: x'), ('', 'nope: x')) + + +class EndToEndStreamingTest(unittest.TestCase): + """Real subprocess host with one fast and one slow provider over pipes.""" + def test_first_results_do_not_wait_for_slow_provider(self): + fast = "import json,sys\nfor l in sys.stdin:\n m=json.loads(l)\n if m.get('op')=='search': print(json.dumps({'id':m['id'],'results':[{'title':'fast'}]}),flush=True)\n" + slow = "import json,sys,time\nfor l in sys.stdin:\n m=json.loads(l)\n if m.get('op')=='search':\n time.sleep(0.4); print(json.dumps({'id':m['id'],'results':[{'title':'slow'}]}),flush=True)\n" + with tempfile.TemporaryDirectory() as tmp: + root = Path(tmp) + (root / 'src').mkdir() + shutil.copyfile(ROOT / 'src/host.py', root / 'src/host.py') + for name, code in (('fast', fast), ('slow', slow)): + d = root / 'providers' / name + d.mkdir(parents=True) + (d / 'w.py').write_text(code) + (d / 'manifest.json').write_text(json.dumps({'name': name, 'exec': [sys.executable, str(d / 'w.py')]})) + proc = subprocess.Popen([sys.executable, str(root / 'src/host.py')], stdin=subprocess.PIPE, + stdout=subprocess.PIPE, stderr=subprocess.DEVNULL, text=True) + try: + time.sleep(0.3) + start = time.monotonic() + proc.stdin.write(json.dumps({'id': 'q1', 'op': 'search', 'query': 'x'}) + '\n'); proc.stdin.flush() + stamps = [] + while True: + ready, _, _ = select.select([proc.stdout], [], [], 3) + self.assertTrue(ready, 'host did not answer') + msg = json.loads(proc.stdout.readline()) + stamps.append((time.monotonic() - start, msg)) + if msg.get('final'): + break + finally: + proc.stdin.close(); proc.wait(timeout=5); proc.stdout.close() + first_t, first = stamps[0] + self.assertLess(first_t, 0.3) + self.assertEqual([r['title'] for r in first['results']], ['fast']) + self.assertEqual([r['title'] for r in stamps[-1][1]['results']], ['fast', 'slow']) + + +if __name__ == '__main__': + unittest.main() From d6434fa9a2bbfeaa612179c87a83854f9ef8235a Mon Sep 17 00:00:00 2001 From: Grok Bot Date: Thu, 29 Oct 2026 16:14:15 -0400 Subject: [PATCH 03/19] frontend: merge streamed results without jumping selection; tiered ranking - Follow-up emits re-rank in place; selection follows the same result once the user has moved it; updates are deferred under an open panel. - Typing forward re-ranks the previous answer locally for instant paint. - Fuzzy.rank: exact > prefix > word boundary > substring > subsequence, stable tie-breaks, bounded recency, fallbacks (Run:) always last. - Confirmation sheets bind to the exact result key and query. - Text.PlainText on every result/action/footer text. - Actions launch through the host; failures flash and notify. - Debounce 80ms -> 30ms; warm providers on open. --- shell/ActionPanel.qml | 33 ++++++++-- shell/Fuzzy.js | 134 +++++++++++++++++++++++++++++++++------ shell/PaletteWindow.qml | 97 ++++++++++++++++++++++++---- shell/ProviderBridge.qml | 47 +++++++++++--- shell/ResultDelegate.qml | 3 + tests/lifecycle.cjs | 70 ++++++++++++++++++-- tests/ranking.cjs | 72 +++++++++++++++++++++ tests/test_lifecycle.py | 6 ++ 8 files changed, 409 insertions(+), 53 deletions(-) create mode 100644 tests/ranking.cjs diff --git a/shell/ActionPanel.qml b/shell/ActionPanel.qml index 9c1fa23..cd7d06c 100644 --- a/shell/ActionPanel.qml +++ b/shell/ActionPanel.qml @@ -1,5 +1,6 @@ import QtQuick import QtQuick.Layouts +import "Fuzzy.js" as Fuzzy // Action panel: a small floating list of actions for the selected result, // positioned to the right of the palette box. Actions carrying @@ -9,6 +10,9 @@ Rectangle { id: root required property var result + // Current search text; a confirmation is bound to the exact result and + // query it was opened for. + property string query: "" signal actionExecuted(var action, var result) width: 240 @@ -22,6 +26,9 @@ Rectangle { // Non-null while a danger-gated action awaits its second Enter. property var pendingConfirm: null + property string confirmKey: "" + property string confirmQuery: "" + property string resultKey: Fuzzy.keyOf(result) readonly property bool confirming: pendingConfirm !== null ColumnLayout { @@ -33,6 +40,7 @@ Rectangle { spacing: 2 Text { + textFormat: Text.PlainText Layout.fillWidth: true text: qsTr("Actions") color: "#6c7086" @@ -61,6 +69,8 @@ Rectangle { color: root.actionIndex === index ? "#45475a" : "transparent" Text { + textFormat: Text.PlainText + textFormat: Text.PlainText anchors.left: parent.left anchors.leftMargin: 10 anchors.verticalCenter: parent.verticalCenter @@ -81,6 +91,7 @@ Rectangle { spacing: 6 Text { + textFormat: Text.PlainText Layout.fillWidth: true text: root.confirmTitle() color: root.confirmColor() @@ -90,6 +101,7 @@ Rectangle { } Text { + textFormat: Text.PlainText Layout.fillWidth: true text: root.confirmDetail() color: "#a6adc8" @@ -100,6 +112,7 @@ Rectangle { Item { Layout.fillHeight: true } Text { + textFormat: Text.PlainText Layout.fillWidth: true text: qsTr("Enter confirms \u00b7 Esc cancels") color: "#6c7086" @@ -170,32 +183,42 @@ Rectangle { function executeCurrent(): void { if (root.confirming) { const action = root.pendingConfirm; - root.pendingConfirm = null; - actionExecuted(action, root.result); + const valid = root.confirmKey === Fuzzy.keyOf(root.result) && root.confirmQuery === root.query; + root.cancelConfirm(); + // The selection or query changed under the sheet: never run it. + if (valid) + actionExecuted(action, root.result); return; } const actions = root.result && root.result.actions ? root.result.actions : []; if (actions.length === 0 || actionIndex >= actions.length) return; const action = actions[actionIndex]; if (action.danger === "confirm") { - pendingConfirm = action; + root.openConfirm(action); return; } actionExecuted(action, root.result); } function openConfirm(action: var): void { + confirmKey = Fuzzy.keyOf(root.result); + confirmQuery = root.query; pendingConfirm = action; } function cancelConfirm(): void { pendingConfirm = null; + confirmKey = ""; + confirmQuery = ""; } function reset(): void { - pendingConfirm = null; + cancelConfirm(); actionIndex = 0; } - onResultChanged: reset() + // Reset when the selected result changes identity (provider + id), not + // merely when a streamed update re-creates the same row object. + onResultKeyChanged: reset() + onQueryChanged: cancelConfirm() } diff --git a/shell/Fuzzy.js b/shell/Fuzzy.js index 3e3b959..cc71c99 100644 --- a/shell/Fuzzy.js +++ b/shell/Fuzzy.js @@ -2,6 +2,15 @@ // pattern of MaNi4's Model.js so ranking stays testable outside the UI. .pragma library +// Match tiers. Higher tiers always outrank lower ones; provider scores, +// intent, and recency only order results *within* a tier. +var TIER_EXACT = 5; +var TIER_PREFIX = 4; +var TIER_WORD = 3; +var TIER_SUBSTRING = 2; +var TIER_SUBSEQUENCE = 1; +var FALLBACK_RANK = -10; // "Run: " and other fallbacks: always last. + // Subsequence match. Returns -1 when query is not a subsequence of text, // otherwise 0..1 (tighter, earlier matches score higher). function matchScore(query, text) { @@ -32,6 +41,67 @@ function matchScore(query, text) { return 0.4 + 0.3 * tight + 0.3 * earliness; } +function isBoundary(t, i) { + if (i === 0) return true; + const c = t[i - 1]; + return c === " " || c === "-" || c === "_" || c === "." || c === "/" || c === ":" || c === "(" || c === "›"; +} + +// Initials of the words in text, e.g. "Night Light" -> "nl". +function initials(t) { + let out = ""; + for (let i = 0; i < t.length; i++) + if (t[i] !== " " && isBoundary(t, i)) out += t[i]; + return out; +} + +// Classify how query matches one text: {tier, quality 0..1} or null. +function matchTier(query, text) { + if (text === undefined || text === null) return null; + const q = String(query).toLowerCase(); + const t = String(text).toLowerCase(); + if (q.length === 0 || t.length === 0) return null; + if (t === q) return { tier: TIER_EXACT, quality: 1 }; + if (t.startsWith(q)) return { tier: TIER_PREFIX, quality: q.length / t.length }; + let at = t.indexOf(q); + let substringAt = at; + while (at !== -1) { + if (isBoundary(t, at)) return { tier: TIER_WORD, quality: 0.5 + 0.5 * (1 - at / t.length) }; + at = t.indexOf(q, at + 1); + } + if (q.length >= 2 && q.indexOf(" ") === -1 && initials(t).startsWith(q)) + return { tier: TIER_WORD, quality: 0.4 }; + if (substringAt !== -1) return { tier: TIER_SUBSTRING, quality: 1 - substringAt / t.length }; + const sub = matchScore(q, t); + if (sub < 0) return null; + return { tier: TIER_SUBSEQUENCE, quality: (sub - 0.4) / 0.6 }; +} + +// Best match across a result's fields. Titles count fully; keyword aliases +// are matched per word (an exact alias ranks like a title prefix); subtitles +// can never lift a result above a word-boundary title match. +function bestMatch(query, r) { + let best = null; + function consider(m, cap, penalty) { + if (m === null) return; + const tier = Math.min(m.tier, cap) - penalty; + if (best === null || tier > best.tier || (tier === best.tier && m.quality > best.quality)) + best = { tier: tier, quality: m.quality }; + } + consider(matchTier(query, r.title), TIER_EXACT, 0); + if (r.keywords !== undefined && r.keywords !== null) { + const words = String(r.keywords).split(/\s+/); + for (let i = 0; i < words.length; i++) { + if (words[i].length === 0) continue; + const m = matchTier(query, words[i]); + if (m !== null && m.tier >= TIER_PREFIX) consider(m, TIER_PREFIX, m.tier === TIER_EXACT ? 0 : 0.5); + } + consider(matchTier(query, r.keywords), TIER_WORD, 0.25); + } + consider(matchTier(query, r.subtitle), TIER_SUBSTRING, 0.5); + return best; +} + // File intent is explicit: paths and extension-like queries request files. // Ordinary terms such as "install" or "update" are command intent, even // when similarly named files happen to exist under $HOME. @@ -45,39 +115,65 @@ function isExactFileQuery(query, result) { && String(result.title || "").toLowerCase() === String(query || "").trim().toLowerCase(); } -// Client-side pass over merged provider results: drop non-matches, blend -// fuzzy score with the provider's advisory score (0.7 / 0.3), and sort. -// Generic queries favor actionable non-file results over incidental filename -// matches. Explicit file intent reverses that preference; exact filename -// matches are always kept prominent. +function isFallback(r) { + return r.fallback === true || r.provider === "run"; +} + +// Client-side pass over merged provider results: drop non-matches, order by +// match tier (exact > prefix > word boundary > substring > subsequence), then +// within a tier blend match quality, the provider's advisory score, intent +// (files vs commands), and a bounded recency boost. Fallback rows such as +// "Run: " always sort last. The sort is stable: ties keep the host's +// provider order. function rank(query, results) { - const q = String(query || "").toLowerCase(); + const q = String(query || "").toLowerCase().trim(); const wantsFile = isFileIntent(q); const scored = []; for (let i = 0; i < results.length; i++) { const r = results[i]; - const f = matchScore(q, r.title); - const s = matchScore(q, r.subtitle); - const k = matchScore(q, r.keywords); - const best = Math.max(f, s, k); - if (best < 0) - continue; + if (r === null || typeof r !== "object") continue; const advisory = typeof r.score === "number" ? Math.max(0, Math.min(1, r.score)) : 0; const out = {}; for (const key in r) out[key] = r[key]; + out.order = i; + if (isFallback(r)) { + out.fuzzy = 0; + out.tier = 0; + out.rank = FALLBACK_RANK + advisory; + scored.push(out); + continue; + } + const m = bestMatch(q, r); + if (m === null) + continue; const isFile = r.provider === "files"; - let intentAdjustment = 0; + let intent = 0; if (isExactFileQuery(q, r)) - intentAdjustment = 0.22; + intent = 0.2; else if (wantsFile) - intentAdjustment = isFile ? 0.18 : -0.18; + intent = isFile ? 0.15 : -0.15; else - intentAdjustment = isFile ? -0.18 : 0.18; - out.fuzzy = best; - out.rank = 0.7 * best + 0.3 * advisory + intentAdjustment; + intent = isFile ? -0.15 : 0.15; + const recent = typeof r.recent === "number" ? Math.max(0, Math.min(1, r.recent)) : 0; + const within = Math.max(0, Math.min(0.99, 0.3 + 0.3 * m.quality + 0.25 * advisory + intent + 0.15 * recent)); + out.fuzzy = m.quality; + out.tier = m.tier; + out.rank = m.tier + within; scored.push(out); } - scored.sort(function (a, b) { return b.rank - a.rank; }); + scored.sort(function (a, b) { return (b.rank - a.rank) || (a.order - b.order); }); return scored; } + +// Index of the result with the same provider+id as `key`, or -1. +function keyOf(r) { + return r ? String(r.provider || "") + "\u0000" + String(r.id || r.title || "") : ""; +} + +function indexOfKey(results, key) { + if (!key) return -1; + for (let i = 0; i < results.length; i++) + if (keyOf(results[i]) === key) return i; + return -1; +} diff --git a/shell/PaletteWindow.qml b/shell/PaletteWindow.qml index b639962..191efbf 100644 --- a/shell/PaletteWindow.qml +++ b/shell/PaletteWindow.qml @@ -14,6 +14,18 @@ PanelWindow { property bool panelOpen: false property string flashMessage: "" property string scopeLabel: "All" + property string statusText: "" + // Selection follows the same result across streamed updates once the + // user has moved it; until then the top row stays selected. + property bool userMoved: false + // Last raw host results and the query they answered, used to re-filter + // instantly on the next keystroke while the host catches up. + property var lastRaw: [] + property string lastQuery: "" + property string lastBare: "" + // Streamed update held back while the action panel is open, so a late + // provider can never swap the result under an open confirmation sheet. + property var deferredUpdate: null readonly property bool opened: visible visible: false focusable: true @@ -47,7 +59,7 @@ PanelWindow { Layout.preferredHeight: 40 font.pixelSize: 17 color: "#cdd6f4" - placeholderText: qsTr("Type to search · files:, obsidian:, apps:") + placeholderText: qsTr("Type to search · f: files · a: apps · om: omarchy · s: system · w: windows") placeholderTextColor: "#6c7086" background: Rectangle { color: "transparent" } focus: true @@ -71,8 +83,10 @@ PanelWindow { Text { Layout.fillWidth: true font.pixelSize: 11 + textFormat: Text.PlainText + elide: Text.ElideRight color: root.flashMessage.length > 0 ? "#f38ba8" : "#585b70" - text: root.flashMessage.length > 0 ? root.flashMessage : ("Scope: " + root.scopeLabel + " · " + (root.panelOpen ? qsTr("Up/Down navigate · Enter runs · Esc closes panel") : qsTr("Up/Down navigate · Enter runs · Tab actions · Esc closes"))) + text: root.flashMessage.length > 0 ? root.flashMessage : ("Scope: " + root.scopeLabel + (root.statusText.length > 0 ? " · " + root.statusText : "") + " · " + (root.panelOpen ? qsTr("Up/Down navigate · Enter runs · Esc closes panel") : qsTr("Up/Down navigate · Enter runs · Tab actions · Esc closes"))) } } } @@ -83,36 +97,63 @@ PanelWindow { anchors.top: box.top visible: root.panelOpen result: root.filtered[root.selectedIndex] + query: searchInput.text onActionExecuted: (action, result) => { bridge.execute(action, result); root.dismiss(); } } ProviderBridge { id: bridge - onResultsReady: (results, scope, bare, unresolved) => { + onResultsReady: (results, scope, bare, unresolved, meta) => { if (!root.visible) return; + const info = meta || { final: true, pending: [], failed: [], incremental: false }; + if (info.incremental && root.panelOpen) { + // Never swap rows under an open action panel or confirmation. + root.deferredUpdate = { results: results, scope: scope, bare: bare, unresolved: unresolved, meta: info }; + return; + } + const selected = Fuzzy.keyOf(root.filtered[root.selectedIndex]); root.scopeLabel = scope || "All"; + root.lastRaw = results; + root.lastQuery = searchInput.text; + root.lastBare = bare; root.filtered = Fuzzy.rank(bare, results); - root.selectedIndex = 0; - root.flashMessage = ""; + const kept = root.userMoved ? Fuzzy.indexOfKey(root.filtered, selected) : -1; + root.selectedIndex = kept >= 0 ? kept : 0; + if (kept < 0) root.userMoved = false; + root.statusText = info.final ? "" : qsTr("searching…"); if (unresolved.length > 0) { - root.flashMessage = qsTr("Incomplete results — no reply from: ") + unresolved.join(", "); + const failed = unresolved.filter(name => info.failed.indexOf(name) !== -1); + const slow = unresolved.filter(name => info.failed.indexOf(name) === -1); + const parts = []; + if (failed.length > 0) parts.push(qsTr("unavailable: ") + failed.join(", ")); + if (slow.length > 0) parts.push(qsTr("no reply from: ") + slow.join(", ")); + root.flashMessage = qsTr("Incomplete results — ") + parts.join(" · "); flashTimer.restart(); + } else if (info.final && !info.incremental) { + root.flashMessage = ""; } } onHostError: message => { if (!root.visible) return; root.filtered = [{ id: "pallet.error", title: message, subtitle: qsTr("Check the Python host"), actions: [] }]; } - onActionFailed: { + onActionFailed: message => { + const text = message.length > 0 ? message : qsTr("the target may already be gone."); + // Actions dismiss the overlay first, so also raise a desktop + // notification (bin/omarchy-notification-send). The description + // is prefixed so it can never be parsed as an option. + Quickshell.execDetached(["omarchy-notification-send", "--app-name", "Pallet", "-u", "normal", + qsTr("Pallet action failed"), qsTr("Error: ") + text]); if (!root.visible) return; - root.flashMessage = qsTr("That action could not be completed — the target may already be gone."); + root.flashMessage = qsTr("That action could not be completed — ") + text; flashTimer.restart(); } } - Timer { id: debounce; interval: 80; onTriggered: root.runSearch() } + Timer { id: debounce; interval: 30; onTriggered: root.runSearch() } Timer { id: flashTimer; interval: 2500; onTriggered: root.flashMessage = "" } onVisibleChanged: { if (visible) { searchInput.forceActiveFocus(); + bridge.warm(); } else { debounce.stop(); root.invalidateSearch(); @@ -122,23 +163,50 @@ PanelWindow { bridge.cancel(); root.filtered = []; root.selectedIndex = 0; + root.userMoved = false; + root.deferredUpdate = null; root.panelOpen = false; root.flashMessage = ""; + root.statusText = ""; root.scopeLabel = "All"; flashTimer.stop(); actionPanel.reset(); } function inputChanged(): void { - // Invalidate on the keystroke, not after the debounce window. + // Invalidate on the keystroke, not after the debounce window: the + // old query is cancelled and any open panel or confirmation closes. + const text = searchInput.text; + const raw = root.lastRaw || []; + const extends_ = root.lastQuery.length > 0 && text.startsWith(root.lastQuery); + const bare = extends_ ? root.lastBare + text.substring(root.lastQuery.length) : ""; root.invalidateSearch(); debounce.stop(); - if (root.visible && searchInput.text.trim().length > 0) + if (root.visible && text.trim().length > 0) { + // Typing forward only narrows a fuzzy match, so re-rank the + // previous answer locally for an instant first paint. + if (extends_ && raw.length > 0) { + root.filtered = Fuzzy.rank(bare, raw); + root.statusText = qsTr("searching…"); + } else { + root.lastRaw = []; + root.lastQuery = ""; + } debounce.restart(); + } else { + root.lastRaw = []; + root.lastQuery = ""; + } + } + function applyDeferred(): void { + const update = root.deferredUpdate; + root.deferredUpdate = null; + if (update !== null) + bridge.resultsReady(update.results, update.scope, update.bare, update.unresolved, update.meta); } function open(): void { visible = true; scopeLabel = "All"; } function close(): void { if (actionPanel.confirming) { actionPanel.cancelConfirm(); return; } - if (root.panelOpen) { root.panelOpen = false; return; } + if (root.panelOpen) { root.panelOpen = false; root.applyDeferred(); return; } dismiss(); } function dismiss(): void { @@ -146,6 +214,8 @@ PanelWindow { root.invalidateSearch(); visible = false; searchInput.text = ""; + root.lastRaw = []; + root.lastQuery = ""; } function toggle(): void { visible ? close() : open(); } function runSearch(): void { @@ -160,13 +230,14 @@ PanelWindow { if (root.panelOpen) { actionPanel.moveAction(step); return; } if (root.filtered.length === 0) return; root.selectedIndex = (root.selectedIndex + step + root.filtered.length) % root.filtered.length; + root.userMoved = true; } function togglePanel(): void { if (root.filtered.length === 0) return; const item = root.filtered[root.selectedIndex]; if (!item || !item.actions || item.actions.length === 0) return; root.panelOpen = !root.panelOpen; - if (!root.panelOpen) actionPanel.reset(); + if (!root.panelOpen) { actionPanel.reset(); root.applyDeferred(); } } function executeSelection(): void { if (root.panelOpen) { actionPanel.executeCurrent(); return; } diff --git a/shell/ProviderBridge.qml b/shell/ProviderBridge.qml index 1d4eec0..50f26a8 100644 --- a/shell/ProviderBridge.qml +++ b/shell/ProviderBridge.qml @@ -5,9 +5,12 @@ import "QueryState.js" as QueryState Process { id: root - signal resultsReady(var results, string scope, string bare, var unresolved) + // meta: { final, pending, failed, incremental } — incremental is true for + // follow-up messages under the query id already delivered, so the window + // can merge without resetting the selection. + signal resultsReady(var results, string scope, string bare, var unresolved, var meta) signal hostError(string message) - signal actionFailed() + signal actionFailed(string message) readonly property url hostUrl: Qt.resolvedUrl("../src/host.py") readonly property string localHostPath: { const text = hostUrl.toString(); @@ -15,6 +18,7 @@ Process { } readonly property string hostPath: Quickshell.env("PALLET_HOST") ?? localHostPath property var queryState: QueryState.create() + property string deliveredId: "" property int actionCounter: 0 command: ["python3", hostPath] running: true @@ -26,14 +30,22 @@ Process { try { msg = JSON.parse(data); } catch (err) { return; } if (!msg || typeof msg !== "object") return; if (msg.results !== undefined) { - if (QueryState.accepts(root.queryState, msg)) + if (QueryState.accepts(root.queryState, msg)) { + const incremental = root.deliveredId === msg.id; + root.deliveredId = msg.id; root.resultsReady(msg.results, String(msg.scope || ""), - String(msg.bare ?? ""), Array.isArray(msg.unresolved) ? msg.unresolved : []); + String(msg.bare ?? ""), Array.isArray(msg.unresolved) ? msg.unresolved : [], { + final: msg.final !== false, + pending: Array.isArray(msg.pending) ? msg.pending : [], + failed: Array.isArray(msg.failed) ? msg.failed : [], + incremental: incremental + }); + } + } else if (msg.executed === false) { + root.actionFailed(typeof msg.error === "string" ? msg.error : ""); } else if (msg.error !== undefined) { if (!msg.id || msg.id === root.queryState.activeId) root.hostError(String(msg.error)); - } else if (msg.executed === false) { - root.actionFailed(); } } } @@ -45,6 +57,7 @@ Process { } function cancel(): void { const target = QueryState.cancel(root.queryState); + root.deliveredId = ""; if (target.length > 0 && root.running) root.write(JSON.stringify({ id: "", op: "cancel", target: target }) + "\n"); } @@ -57,12 +70,28 @@ Process { const id = QueryState.begin(root.queryState); root.write(JSON.stringify({ id: id, op: "search", query: query }) + "\n"); } + // Overlay opened: let providers refresh caches before the first keystroke. + function warm(): void { + if (root.running) + root.write(JSON.stringify({ id: "", op: "warm" }) + "\n"); + } + // Run argv without a shell. The host launches it so a missing command is + // reported back; if the host is down, fall back to a detached exec. + function launch(argv: var): void { + if (!Array.isArray(argv) || argv.length === 0) return; + if (root.running) { + root.actionCounter += 1; + root.write(JSON.stringify({ id: "l" + root.actionCounter, op: "launch", argv: argv }) + "\n"); + } else { + Quickshell.execDetached(argv); + } + } function execute(action: var, result: var): void { if (action === undefined || action === null) return; switch (action.type) { - case "launch": Quickshell.execDetached(action.argv); break; - case "open": Quickshell.execDetached(["xdg-open", action.url]); break; - case "copy": Quickshell.execDetached(["wl-copy", action.text]); break; + case "launch": root.launch(action.argv); break; + case "open": root.launch(["xdg-open", String(action.url ?? action.path ?? "")]); break; + case "copy": root.launch(["wl-copy", "--", String(action.text ?? "")]); break; case "custom": root.actionCounter += 1; root.write(JSON.stringify({ id: "a" + root.actionCounter, op: "execute", diff --git a/shell/ResultDelegate.qml b/shell/ResultDelegate.qml index 2579f4a..94ab903 100644 --- a/shell/ResultDelegate.qml +++ b/shell/ResultDelegate.qml @@ -61,6 +61,7 @@ Rectangle { } Text { + textFormat: Text.PlainText id: fallbackIcon anchors.left: parent.left @@ -78,6 +79,7 @@ Rectangle { } Text { + textFormat: Text.PlainText id: title anchors.left: appIcon.right @@ -93,6 +95,7 @@ Rectangle { } Text { + textFormat: Text.PlainText anchors.left: title.right anchors.leftMargin: 12 anchors.right: parent.right diff --git a/tests/lifecycle.cjs b/tests/lifecycle.cjs index a30951e..5d76d11 100644 --- a/tests/lifecycle.cjs +++ b/tests/lifecycle.cjs @@ -20,15 +20,19 @@ context.cancel(state); assert(!context.accepts(state, {id: '', results: []})); const palette = fs.readFileSync(path.join(rootPath, 'shell/PaletteWindow.qml'), 'utf8'); +const fuzzyContext = {}; +vm.createContext(fuzzyContext); +vm.runInContext(fs.readFileSync(path.join(rootPath, 'shell/Fuzzy.js'), 'utf8').replace(/^\.pragma library\s*/m, ''), fuzzyContext); let cancels = 0, resets = 0, starts = 0, searches = 0, stopped = 0; -const root = {visible: true, filtered: ['old'], selectedIndex: 1, panelOpen: true, flashMessage: 'old', scopeLabel: 'old'}; +const root = {visible: true, filtered: ['old'], selectedIndex: 1, panelOpen: true, flashMessage: 'old', scopeLabel: 'old', + statusText: '', userMoved: false, lastRaw: [], lastQuery: '', lastBare: '', deferredUpdate: null}; const sandbox = {root, bridge: {cancel() {cancels++;}, search() {searches++;}}, actionPanel: {reset() {resets++;}}, flashTimer: {stop() {}, restart() {}}, debounce: {stop() {stopped++;}, restart() {starts++;}}, searchInput: {text: 'new'}, qsTr: s => s, - Fuzzy: {rank(q, results) {return results;}} + Fuzzy: fuzzyContext }; function method(name) { const match = palette.match(new RegExp('function ' + name + '\\(\\): void \\{([\\s\\S]*?)\\n \\}')); @@ -57,14 +61,66 @@ assert.equal(starts, 1); assert.equal(searches, 1); assert(stopped > 0); -const handler = palette.match(/onResultsReady: (\(results, scope, bare, unresolved\) => \{[\s\S]*?\n \})\n onHostError/); +const handler = palette.match(/onResultsReady: (\(results, scope, bare, unresolved, meta\) => \{[\s\S]*?\n \})\n onHostError/); assert(handler, 'missing result handler'); const receive = vm.runInNewContext('(' + handler[1] + ')', sandbox); +const row = (provider, id, title) => ({provider, id, title, actions: [{type: 'copy', text: id}]}); root.filtered = []; -receive(['late'], '', '', []); -assert.equal(root.filtered.length, 0); +receive([row('apps', 'late', 'late')], '', 'late', [], {final: true, pending: [], failed: [], incremental: false}); +assert.equal(root.filtered.length, 0, 'results ignored while hidden'); root.visible = true; -receive(['fresh'], 'apps', '', ['files']); -assert.equal(root.filtered[0], 'fresh'); +sandbox.searchInput.text = 'fresh'; +receive([row('apps', 'fresh', 'fresh')], 'apps', 'fresh', ['files'], {final: true, pending: [], failed: [], incremental: false}); +assert.equal(root.filtered[0].id, 'fresh'); assert(root.flashMessage.includes('files')); +// Old single-message hosts send no meta: treated as final. +receive([row('apps', 'fresh', 'fresh')], 'apps', 'fresh', [], undefined); +assert.equal(root.filtered[0].id, 'fresh'); +assert.equal(root.statusText, ''); + +// Streaming: a partial emit, then a follow-up under the same id. +root.flashMessage = ''; +sandbox.searchInput.text = 'fire'; +receive([row('apps', 'b', 'firewall'), row('apps', 'c', 'fire drill')], '', 'fire', [], + {final: false, pending: ['files'], failed: [], incremental: false}); +assert.equal(root.statusText, 'searching…'); +assert.equal(root.selectedIndex, 0); +// The user moves to "fire drill"; a better straggler arrives and ranks first. +root.selectedIndex = 1; root.userMoved = true; +const selected = root.filtered[1].id; +receive([row('apps', 'b', 'firewall'), row('apps', 'c', 'fire drill'), row('files', 'a', 'fire')], '', 'fire', [], + {final: true, pending: [], failed: [], incremental: true}); +assert.equal(root.filtered[0].id, 'a', 'exact match ranks first'); +assert.equal(root.filtered[root.selectedIndex].id, selected, 'selection follows the same result'); +assert.equal(root.statusText, ''); +// Without user movement the top row stays selected. +root.userMoved = false; root.selectedIndex = 0; +receive([row('apps', 'b', 'firewall')], '', 'fire', [], {final: true, pending: [], failed: [], incremental: true}); +assert.equal(root.selectedIndex, 0); +// While the action panel is open, follow-ups are deferred, never applied. +root.panelOpen = true; +const before = root.filtered; +receive([row('apps', 'z', 'fire z')], '', 'fire', [], {final: true, pending: [], failed: [], incremental: true}); +assert.equal(root.filtered, before); +assert(root.deferredUpdate !== null); +root.panelOpen = false; root.deferredUpdate = null; +// Failed vs slow providers are reported separately. +receive([], '', 'fire', ['menus', 'files'], {final: true, pending: [], failed: ['menus'], incremental: false}); +assert(root.flashMessage.includes('unavailable: menus')); +assert(root.flashMessage.includes('no reply from: files')); + +// Typing forward re-ranks the previous answer instantly (still cancelled). +root.visible = true; root.flashMessage = ''; +sandbox.searchInput.text = 'fir'; +receive([row('apps', 'b', 'firewall'), row('apps', 'c', 'first aid')], '', 'fir', [], {final: true, pending: [], failed: [], incremental: false}); +const cancelsBefore = cancels; +sandbox.searchInput.text = 'fire'; +inputChanged(); +assert.equal(cancels, cancelsBefore + 1); +assert.deepEqual(Array.from(root.filtered, r => r.id), ['b']); +assert.equal(root.panelOpen, false); +// Editing backwards clears instead of showing stale rows. +sandbox.searchInput.text = 'xyz'; +inputChanged(); +assert.equal(root.filtered.length, 0); console.log('Query state and extracted QML JavaScript checks passed (not a native QML test)'); diff --git a/tests/ranking.cjs b/tests/ranking.cjs new file mode 100644 index 0000000..95433ad --- /dev/null +++ b/tests/ranking.cjs @@ -0,0 +1,72 @@ +// Ranking checks for shell/Fuzzy.js (pure JS, run under node). +const assert = require('node:assert/strict'); +const fs = require('node:fs'); +const path = require('node:path'); +const vm = require('node:vm'); +const ctx = {}; +vm.createContext(ctx); +vm.runInContext(fs.readFileSync(path.resolve(__dirname, '../shell/Fuzzy.js'), 'utf8').replace(/^\.pragma library\s*/m, ''), ctx); +const ids = list => Array.from(list, r => r.id); +const r = (id, title, extra = {}) => Object.assign({id, title, provider: 'apps', actions: []}, extra); + +// Tiers: exact > prefix > word boundary > substring > subsequence. +let out = ctx.rank('term', [ + r('subseq', 'The Rest of My Day'), + r('substr', 'Determinism'), + r('word', 'Foot Terminal'), + r('prefix', 'Terminal'), + r('exact', 'Term'), +]); +assert.deepEqual(ids(out), ['exact', 'prefix', 'word', 'substr', 'subseq']); + +// A high advisory score cannot lift a lower tier over a higher one. +out = ctx.rank('lock', [r('word', 'Screen lock', {score: 1}), r('prefix', 'Lockdown', {score: 0})]); +assert.deepEqual(ids(out), ['prefix', 'word']); + +// "Run: " never outranks a local match, even a weak subsequence one. +out = ctx.rank('reboot', [ + r('run', 'Run: reboot', {provider: 'run', score: 1}), + r('weak', 'Remove boot entry'), + r('sys', 'Reboot', {provider: 'system'}), +]); +assert.deepEqual(ids(out), ['sys', 'weak', 'run']); +// Fallback rows are kept even when nothing else matches. +out = ctx.rank('htop -d 5', [r('run', 'Run: htop -d 5', {provider: 'run'})]); +assert.deepEqual(ids(out), ['run']); +// Any provider can mark a row as a fallback. +out = ctx.rank('zz', [r('fb', 'zz fallback', {fallback: true}), r('m', 'zzz')]); +assert.deepEqual(ids(out), ['m', 'fb']); + +// Stable: equal ranks keep host (provider) order. +out = ctx.rank('notes', [r('a', 'Notes'), r('b', 'Notes'), r('c', 'Notes')]); +assert.deepEqual(ids(out), ['a', 'b', 'c']); + +// Exact keyword alias ranks like a title prefix; subtitle-only matches stay low. +out = ctx.rank('wifi', [ + r('sub', 'Something', {subtitle: 'wifi settings'}), + r('alias', 'Network', {keywords: 'wifi wireless'}), +]); +assert.deepEqual(ids(out), ['alias', 'sub']); + +// Initials count as a word-boundary match ("nl" -> Night Light). +out = ctx.rank('nl', [r('x', 'Only'), r('nl', 'Night Light')]); +assert.equal(out[0].id, 'nl'); + +// Command intent: apps beat incidental files on a plain word. +out = ctx.rank('fire', [r('file', 'fire_report.pdf', {provider: 'files'}), r('app', 'Firefox')]); +assert.deepEqual(ids(out), ['app', 'file']); +// File intent flips it, and an exact filename is always prominent. +out = ctx.rank('report.pdf', [r('app', 'report.pdf viewer'), r('file', 'report.pdf', {provider: 'files'})]); +assert.equal(out[0].id, 'file'); + +// Recency only reorders within a tier. +out = ctx.rank('fi', [r('a', 'Files'), r('b', 'Firefox', {recent: 1})]); +assert.deepEqual(ids(out), ['b', 'a']); +out = ctx.rank('files', [r('a', 'Files'), r('b', 'Files manager', {recent: 1})]); +assert.deepEqual(ids(out), ['a', 'b']); + +// Selection helpers. +const list = ctx.rank('a', [r('1', 'a', {provider: 'x'}), r('2', 'ab', {provider: 'y'})]); +assert.equal(ctx.indexOfKey(list, ctx.keyOf({provider: 'y', id: '2'})), 1); +assert.equal(ctx.indexOfKey(list, ''), -1); +console.log('ranking checks passed'); diff --git a/tests/test_lifecycle.py b/tests/test_lifecycle.py index 6b2498d..76118d2 100644 --- a/tests/test_lifecycle.py +++ b/tests/test_lifecycle.py @@ -81,5 +81,11 @@ def test_query_state_and_extracted_ui_javascript(self): cwd=ROOT, text=True, capture_output=True, timeout=10) self.assertEqual(result.returncode, 0, result.stdout + result.stderr) + @unittest.skipUnless(shutil.which('node'), 'Node.js required for JavaScript ranking checks') + def test_ranking_tiers_and_fallbacks(self): + result = subprocess.run([shutil.which('node'), str(ROOT / 'tests/ranking.cjs')], + cwd=ROOT, text=True, capture_output=True, timeout=10) + self.assertEqual(result.returncode, 0, result.stdout + result.stderr) + if __name__ == '__main__': unittest.main() From d3294963282de0aa4566eeef708f2a5e07a7a95e Mon Sep 17 00:00:00 2001 From: Grok Bot Date: Thu, 29 Oct 2026 16:17:11 -0400 Subject: [PATCH 04/19] files: background chunked index, substring candidates, top-k heap - Index builds in a thread with no per-entry stat(); cold queries answer from published chunks (partial) with one follow-up when complete. - Stale index rebuilds in the background; warm op refreshes. - Candidates via packed haystack str.find, incremental narrowing while typing forward, heapq top-k, stat() only for top candidates. - Fix: literal "os.sep" check; multi-segment prune entries now prune. - Open in editor prefers omarchy-launch-editor; terminal here uses xdg-terminal-exec --dir (Omarchy defaults), foot as fallback. --- providers/files/README.md | 29 ++- providers/files/provider.py | 399 ++++++++++++++++++++++++++++-------- tests/test_files.py | 144 +++++++++++++ 3 files changed, 481 insertions(+), 91 deletions(-) create mode 100644 tests/test_files.py diff --git a/providers/files/README.md b/providers/files/README.md index 167f4be..b2326cc 100644 --- a/providers/files/README.md +++ b/providers/files/README.md @@ -4,15 +4,28 @@ Searches files and directories under `$HOME`. ## Index -Built once at provider start with `fd` when installed, otherwise `os.walk` -with the same prune list (`.git`, `node_modules`, `__pycache__`, caches, -package registries, Trash). Refreshed lazily after 60 s. Capped at 60k -entries; the provider requires at least three query characters before it -answers, so home-listing noise never floods a one-letter search. +Built in a background thread at provider start with `fd` when installed +(`--base-directory $HOME`, directories then files, no per-entry `stat()`), +otherwise `os.walk` with the same prune list (`.git`, `node_modules`, +`__pycache__`, caches, package registries, Trash, `.config/chromium`). +Capped at 60k entries. + +- **Cold start:** chunks are published while the walk runs, so the first + query answers from whatever is indexed so far with `"partial": true`, and + the provider sends one follow-up reply for the same id when the build + completes (unless the query was cancelled). +- **Refresh:** after 60 s the index rebuilds in the background (also on the + host's `warm` op when the overlay opens); the old index keeps serving until + the new one swaps in atomically. A search never blocks on indexing. +- **Search:** each 4096-entry chunk packs its lowercase paths into one + haystack string; candidates are the substring hits (`str.find`), narrowed + in place while the user types forward, scored, cut to the top 48 with a + heap, and only those are `stat()`ed for the recency nudge. Results are + capped at 12. The provider still requires three characters. ## Ranking -Basename prefix > word prefix > substring; directories lose ties with +Basename prefix > word prefix anywhere in the path (after `_ - / .` or a space) > basename substring > path substring; directories lose ties with their files; files touched in the last 7 days get a small recency nudge; deep paths are penalized slightly. Final order still belongs to the frontend fuzzy pass. @@ -24,7 +37,7 @@ lottery: | File kind | Primary action | |---|---| -| Code/text (`text-*` families, scripts, .md) | Open in editor — `$VISUAL`/`$EDITOR` first, GUI editors direct, terminal editors via `foot -e` | +| Code/text (`text-*` families, scripts, .md) | Open in editor — `omarchy-launch-editor` (Omarchy's Setup › Defaults › Editor; `bin/omarchy-launch-editor`) when present, else `$VISUAL`/`$EDITOR`, GUI editors direct, terminal editors via `foot -e` | | Markdown inside an Obsidian vault (ancestor holds `.obsidian/`) | Open in Obsidian via `obsidian://open?path=…` URI | | Images, PDFs, audio/video, archives | xdg-open MIME default | @@ -38,4 +51,4 @@ defaults (e.g. browsers claiming `text/plain`). | File | primary (above) · Open · Open containing folder · Open terminal here · Copy path | | Directory | Open (file manager) · Open terminal here · Copy path | -"Open terminal here" launches `foot --working-directory ` as raw argv. +"Open terminal here" launches `xdg-terminal-exec --dir=` (Omarchy's terminal entry point, as used by `bin/omarchy-launch-terminal`), falling back to `foot --working-directory `; always raw argv. diff --git a/providers/files/provider.py b/providers/files/provider.py index ea1aca2..7848297 100644 --- a/providers/files/provider.py +++ b/providers/files/provider.py @@ -1,10 +1,16 @@ #!/usr/bin/env python3 """Search files and directories under $HOME. -The index is built once at startup with fd when available (respects the -provider's own exclude list verbatim), otherwise os.walk with the same -names pruned. It refreshes lazily after INDEX_TTL_S of staleness. Search -scores basename first, then path components, and nudges recent files up. +The index is built in a background thread with fd when available (respects +the provider's own exclude list verbatim), otherwise os.walk with the same +names pruned. A cold build publishes chunks as it goes, so the first query +answers immediately from what is indexed (marked partial, with one follow-up +when the build completes). Stale indexes (INDEX_TTL_S) rebuild in the +background while the old one keeps serving. No per-entry stat() at build time. + +Search: candidates come from a packed lowercase haystack per chunk (str.find), +are narrowed incrementally while the user types forward, scored, reduced with +a top-k heap, and only the top candidates are stat()ed for the recency nudge. The primary Enter action is context-aware: code/text files open in your editor ($VISUAL/$EDITOR, GUI editors direct, terminal editors via foot -e), @@ -15,13 +21,16 @@ from __future__ import annotations +import heapq import json import os import shlex import shutil import subprocess import sys +import threading import time +from bisect import bisect_right from pathlib import Path from typing import Any from urllib.parse import quote @@ -58,68 +67,185 @@ ".yml": "text-x-generic", ".html": "text-html", ".css": "text-css", } -_index: list[tuple[str, str, bool, float]] = [] -_index_built_at = 0.0 - - def fd_available() -> bool: return shutil.which("fd") is not None -def skipped(rel_parts: tuple[str, ...]) -> bool: - return any(part in PRUNE for part in rel_parts) +PRUNE_PATHS = {p for p in PRUNE if "/" in p} -def build_index() -> None: - global _index, _index_built_at - entries: list[tuple[str, str, bool, float]] = [] - if fd_available(): - cmd = ["fd", "--type", "f", "--type", "d", "--hidden"] - for name in sorted(PRUNE): - cmd += ["--exclude", name] - cmd += [".", str(HOME)] +def skipped(rel_parts: tuple[str, ...]) -> bool: + # Single names prune anywhere; multi-segment entries (".config/chromium") + # prune that path prefix (and any nested occurrence). + if any(part in PRUNE for part in rel_parts): + return True + joined = "/".join(rel_parts) + return any(joined == p or joined.startswith(p + "/") or ("/" + p + "/") in ("/" + joined + "/") for p in PRUNE_PATHS) + + +CHUNK = 4096 # entries per searchable chunk +PUBLISH_EVERY_S = 0.05 # cold build: publish partial chunks this often +RECENCY_CANDIDATES = 48 # stat() only this many best candidates per query + + +class Chunk: + """A slice of the index packed into one lowercase haystack string. + + Candidate generation is a C-speed `str.find` over the haystack; every + score tier requires the query as a substring of the lowercase relative + path, so nothing outside the hits can rank. + """ + __slots__ = ("rels", "lowers", "dirs", "starts", "hay") + + def __init__(self, rels: list[str], dirs: list[bool]): + self.rels = rels + self.lowers = [rel.lower() for rel in rels] + self.dirs = dirs + starts = [] + pos = 0 + for rel in rels: + starts.append(pos) + pos += len(rel) + 1 + self.starts = starts + self.hay = "\n".join(self.lowers) + "\n" + + def hits(self, q: str): + hay, starts, find = self.hay, self.starts, self.hay.find + pos = find(q) + while pos != -1: + i = bisect_right(starts, pos) - 1 + yield i + nxt = starts[i + 1] if i + 1 < len(starts) else len(hay) + pos = find(q, nxt) + + +class Index: + def __init__(self): + self.chunks: list[Chunk] = [] + self.complete = False + self.version = 0 + self.built_at = 0.0 + + def count(self) -> int: + return sum(len(c.rels) for c in self.chunks) + + +_index = Index() +_lock = threading.Lock() +_building = threading.Event() +_on_complete = None # callback(Index) run on the builder thread + + +def iter_fd(): + """Yield (rel, is_dir) from fd: directories first, then files.""" + base = ["fd", "--hidden", "--color", "never", "--base-directory", str(HOME)] + for name in sorted(PRUNE): + base += ["--exclude", name] + yielded = 0 + for kind, is_dir in (("d", True), ("f", False)): + cmd = base + ["--type", kind, "--max-results", str(MAX_ENTRIES), "."] try: - out = subprocess.run(cmd, check=False, capture_output=True, text=True, timeout=10).stdout - for line in out.splitlines()[:MAX_ENTRIES]: - path = HOME / line - rel = Path(line) - if skipped(rel.parts): + proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.DEVNULL, text=True) + except OSError: + return + try: + for line in proc.stdout: + rel = line.rstrip("\n").rstrip("/") + if rel.startswith("./"): + rel = rel[2:] + if not rel or skipped(Path(rel).parts): continue - is_dir = line.endswith("os.sep") or path.is_dir() - try: - mtime = path.stat().st_mtime - except OSError: - mtime = 0.0 - entries.append((line.rstrip("/"), line.lower(), is_dir, mtime)) - except (OSError, subprocess.TimeoutExpired): - entries = [] - if not entries: - for dirpath, dirnames, filenames in os.walk(HOME): - rel_dir = Path(dirpath).relative_to(HOME) - if skipped(rel_dir.parts): - dirnames[:] = [] - continue - dirnames[:] = [d for d in dirnames if d not in PRUNE] - for name in dirnames + filenames: - rel = str(rel_dir / name) if str(rel_dir) != "." else name - full = Path(dirpath) / name - is_dir = name in dirnames - try: - mtime = full.stat().st_mtime - except OSError: - mtime = 0.0 - entries.append((rel, rel.lower(), is_dir, mtime)) - if len(entries) >= MAX_ENTRIES: - _index = entries - _index_built_at = time.time() + yield rel, is_dir + yielded += 1 + if yielded >= MAX_ENTRIES: return - _index = entries - _index_built_at = time.time() + finally: + proc.kill() if proc.poll() is None else None + proc.wait() + + +def iter_walk(): + for dirpath, dirnames, filenames in os.walk(HOME): + rel_dir = Path(dirpath).relative_to(HOME) + if skipped(rel_dir.parts): + dirnames[:] = [] + continue + prefix = "" if str(rel_dir) == "." else str(rel_dir) + "/" + dirnames[:] = [d for d in dirnames if d not in PRUNE and not skipped(Path(prefix + d).parts)] + for name in dirnames: + yield prefix + name, True + for name in filenames: + yield prefix + name, False + + +def build_index(publish_partial: bool | None = None) -> Index: + """Build a fresh index. A cold build publishes chunks as they fill so + searches see partial results within milliseconds; a refresh keeps + serving the previous index and swaps atomically at the end.""" + global _index + cold = not _index.complete if publish_partial is None else publish_partial + fresh = Index() + fresh.version = _index.version + 1 + if cold: + with _lock: + _index = fresh + rels: list[str] = [] + dirs: list[bool] = [] + last_publish = time.monotonic() + source = iter_fd() if fd_available() else iter_walk() + total = 0 + try: + for rel, is_dir in source: + rels.append(rel) + dirs.append(is_dir) + total += 1 + now = time.monotonic() + if len(rels) >= CHUNK or (cold and now - last_publish >= PUBLISH_EVERY_S and rels): + chunk = Chunk(rels, dirs) + with _lock: + fresh.chunks = fresh.chunks + [chunk] + fresh.version += 1 + rels, dirs = [], [] + last_publish = now + if total >= MAX_ENTRIES: + break + except OSError as err: + print(f"files: index error: {err}", file=sys.stderr, flush=True) + with _lock: + if rels: + fresh.chunks = fresh.chunks + [Chunk(rels, dirs)] + fresh.version += 1 + fresh.complete = True + fresh.built_at = time.time() + _index = fresh + return fresh + + +def _build_worker(): + try: + built = build_index() + cb = _on_complete + if cb is not None: + cb(built) + finally: + _building.clear() + + +def start_build() -> bool: + """Start a background (re)build unless one is running.""" + if _building.is_set(): + return False + _building.set() + threading.Thread(target=_build_worker, name="files-index", daemon=True).start() + return True def ensure_fresh() -> None: - if not _index or time.time() - _index_built_at > INDEX_TTL_S: - build_index() + """Never blocks: kicks a background rebuild when the index is stale.""" + if not _index.complete and not _building.is_set(): + start_build() + elif _index.complete and time.time() - _index.built_at > INDEX_TTL_S: + start_build() def icon_for(path: str, is_dir: bool) -> str: @@ -134,7 +260,19 @@ def is_text_like(is_dir: bool, rel: str) -> bool: return ICON_BY_SUFFIX.get(Path(rel).suffix.lower(), "").startswith("text-") +def terminal_here(directory: str) -> list[str]: + # Omarchy's default terminal entry point (bin/omarchy-launch-terminal uses + # xdg-terminal-exec --dir); foot is the fallback from the base packages. + if shutil.which("xdg-terminal-exec"): + return ["xdg-terminal-exec", "--dir=" + directory] + return ["foot", "--working-directory", directory] + + def editor_argv(path: str) -> list[str] | None: + # Omarchy's editor launcher honours Setup > Defaults > Editor + # (bin/omarchy-launch-editor). + if shutil.which("omarchy-launch-editor"): + return ["omarchy-launch-editor", path] explicit = os.environ.get("VISUAL") or os.environ.get("EDITOR") or "" if explicit: try: @@ -172,23 +310,42 @@ def display_parent(rel: str) -> str: return "~/" + parent if parent != "." else "~/" -def score(query: str, rel_lower: str, is_dir: bool, mtime: float) -> float: - name = rel_lower.rsplit("/", 1)[-1] - if name.startswith(query): +WORD_BOUNDARY = frozenset("_-/. ") + + +def base_score(query: str, rel_lower: str, is_dir: bool) -> float: + """Basename prefix 0.9 > word prefix anywhere in the path 0.78 > + basename substring 0.62 > path substring 0.45 (0 = no match).""" + pos = rel_lower.find(query) + if pos < 0: + return 0.0 + cut = rel_lower.rfind("/") + 1 + if rel_lower.startswith(query, cut): base = 0.9 - elif any(part.startswith(query) for part in rel_lower.replace("_", " ").replace("-", " ").replace("/", " ").split()): - base = 0.78 - elif query in name: - base = 0.62 - elif query in rel_lower: - base = 0.45 else: - return 0.0 + base = 0.0 + while pos != -1: + if pos == 0 or rel_lower[pos - 1] in WORD_BOUNDARY: + base = 0.78 + break + pos = rel_lower.find(query, pos + 1) + if base == 0.0: + base = 0.62 if rel_lower.find(query, cut) != -1 else 0.45 if is_dir: base -= 0.03 + return base - 0.005 * (rel_lower.count("/") - 1) + + +def recency_bonus(mtime: float) -> float: age_days = (time.time() - mtime) / 86400 if mtime else 365 - base += 0.04 if age_days < 7 else (0.02 if age_days < 30 else 0.0) - return min(0.97, base - 0.005 * (rel_lower.count("/") - 1)) + return 0.04 if age_days < 7 else (0.02 if age_days < 30 else 0.0) + + +def score(query: str, rel_lower: str, is_dir: bool, mtime: float) -> float: + base = base_score(query, rel_lower, is_dir) + if base == 0.0: + return 0.0 + return min(0.97, base + recency_bonus(mtime)) def actions_for(rel: str, is_dir: bool) -> list[dict]: @@ -200,7 +357,7 @@ def actions_for(rel: str, is_dir: bool) -> list[dict]: if is_dir: return [ mime_open, - {"type": "launch", "title": "Open terminal here", "argv": ["foot", "--working-directory", path]}, + {"type": "launch", "title": "Open terminal here", "argv": terminal_here(path)}, {"type": "copy", "title": "Copy path", "text": path}, ] @@ -216,26 +373,59 @@ def actions_for(rel: str, is_dir: bool) -> list[dict]: actions += [ mime_open, {"type": "open", "title": "Open containing folder", "url": parent}, - {"type": "launch", "title": "Open terminal here", "argv": ["foot", "--working-directory", parent]}, + {"type": "launch", "title": "Open terminal here", "argv": terminal_here(parent)}, {"type": "copy", "title": "Copy path", "text": path}, ] return actions +# Typing forward only narrows substring hits, so the previous query's +# candidates are reused when the new query extends it on the same index. +_last: dict[str, Any] = {"q": "", "version": -1, "cands": None} + + +_search_lock = threading.Lock() + + +def candidates(q: str) -> list[tuple[str, str, bool]]: + with _search_lock: + return _candidates(q) + + +def _candidates(q: str) -> list[tuple[str, str, bool]]: + with _lock: + index = _index + chunks = index.chunks + version = index.version + last = _last + if last["cands"] is not None and last["version"] == version and last["q"] and q.startswith(last["q"]): + cands = [c for c in last["cands"] if q in c[1]] + else: + cands = [] + for chunk in chunks: + rels, lowers, dirs = chunk.rels, chunk.lowers, chunk.dirs + cands.extend((rels[i], lowers[i], dirs[i]) for i in chunk.hits(q)) + _last.update(q=q, version=version, cands=cands) + return cands + + def search(query: str) -> list[dict]: q = query.strip().lower() if len(q) < MIN_QUERY_LEN: return [] ensure_fresh() - ranked = sorted( - ((score(q, rel_lower, is_dir, mtime), rel, is_dir) for rel, rel_lower, is_dir, mtime in _index), - key=lambda pair: pair[0], - reverse=True, - ) + scored = ((base_score(q, low, d), rel, d) for rel, low, d in candidates(q)) + top = heapq.nlargest(RECENCY_CANDIDATES, (t for t in scored if t[0] > 0.0), key=lambda t: t[0]) + final = [] + for base, rel, is_dir in top: + try: + mtime = (HOME / rel).stat().st_mtime + except OSError: + mtime = 0.0 + final.append((min(0.97, base + recency_bonus(mtime)), rel, is_dir)) + final.sort(key=lambda t: (-t[0], t[1])) out: list[dict] = [] - for s, rel, is_dir in ranked: - if s == 0.0 or len(out) >= MAX_RESULTS: - break + for s, rel, is_dir in final[:MAX_RESULTS]: name = rel.rsplit("/", 1)[-1] out.append({ "id": "files:" + rel, @@ -251,19 +441,62 @@ def search(query: str) -> list[dict]: return out +_out_lock = threading.Lock() + + +def send(msg: dict) -> None: + with _out_lock: + print(json.dumps(msg), flush=True) + + def main() -> None: - build_index() + global _on_complete + # A query answered while the cold index was still filling gets one + # follow-up reply (same id) once the index completes, unless cancelled. + waiting: dict[str, str] = {} + waiting_lock = threading.Lock() + + def on_complete(_built: Index) -> None: + with waiting_lock: + pending = dict(waiting) + waiting.clear() + for qid, query in pending.items(): + send({"id": qid, "results": search(query)}) + + _on_complete = on_complete + start_build() for line in sys.stdin: try: msg = json.loads(line) except json.JSONDecodeError: continue - if msg.get("op") == "search": - print(json.dumps({"id": msg.get("id", ""), "results": search(str(msg.get("query", "")))}), flush=True) - elif msg.get("op") == "cancel": - print(json.dumps({"id": msg.get("target", ""), "cancelled": True}), flush=True) - elif msg.get("op") == "execute": - print(json.dumps({"id": msg.get("id", ""), "executed": False}), flush=True) + op = msg.get("op") + if op == "search": + qid = str(msg.get("id", "")) + query = str(msg.get("query", "")) + was_complete = _index.complete + results = search(query) + if was_complete or len(query.strip()) < MIN_QUERY_LEN: + send({"id": qid, "results": results}) + continue + with waiting_lock: + waiting.clear() + waiting[qid] = query + send({"id": qid, "results": results, "partial": True}) + if _index.complete: + # The build finished while we searched; answer now. + with waiting_lock: + late = waiting.pop(qid, None) + if late is not None: + send({"id": qid, "results": search(late)}) + elif op == "cancel": + with waiting_lock: + waiting.pop(str(msg.get("target", "")), None) + send({"id": msg.get("target", ""), "cancelled": True}) + elif op == "warm": + ensure_fresh() + elif op == "execute": + send({"id": msg.get("id", ""), "executed": False}) if __name__ == "__main__": diff --git a/tests/test_files.py b/tests/test_files.py new file mode 100644 index 0000000..1e65403 --- /dev/null +++ b/tests/test_files.py @@ -0,0 +1,144 @@ +"""files provider: background index, candidate generation, top-k, streaming.""" +import importlib.util +import json +import os +from pathlib import Path +import random +import select +import subprocess +import sys +import tempfile +import time +import unittest +from unittest.mock import patch + +ROOT = Path(__file__).resolve().parents[1] +PROVIDER = ROOT / 'providers/files/provider.py' + + +def load(): + spec = importlib.util.spec_from_file_location('files_provider', PROVIDER) + module = importlib.util.module_from_spec(spec) + spec.loader.exec_module(module) + return module + + +class FilesIndexTests(unittest.TestCase): + def setUp(self): + self.tmp = tempfile.TemporaryDirectory() + self.addCleanup(self.tmp.cleanup) + self.home = Path(self.tmp.name) + for rel in ('Documents/config-notes.md', 'Documents/report.pdf', 'projects/app/config.toml', + 'projects/app/src/main.py', '.config/hypr/hyprland.conf', 'node_modules/config/index.js', + 'deep/a/b/c/configuration.txt', 'Music/Configs Mix.mp3', + '.config/chromium/config-cache.json'): + path = self.home / rel + path.parent.mkdir(parents=True, exist_ok=True) + path.write_text('x') + (self.home / 'configs').mkdir() + self.files = load() + self.files.HOME = self.home + patcher = patch.object(self.files, 'fd_available', return_value=False) + patcher.start() + self.addCleanup(patcher.stop) + + def build(self, chunk=None): + if chunk: + self.files.CHUNK = chunk + return self.files.build_index() + + def test_index_prunes_and_marks_directories(self): + index = self.build() + rels = {rel: d for c in index.chunks for rel, d in zip(c.rels, c.dirs)} + self.assertTrue(index.complete) + self.assertTrue(rels['configs']) + self.assertFalse(rels['projects/app/config.toml']) + self.assertNotIn('node_modules/config/index.js', rels) + self.assertNotIn('.config/chromium/config-cache.json', rels) + self.assertIn('.config/hypr/hyprland.conf', rels) + + def test_search_matches_naive_full_scan(self): + self.build(chunk=3) # many tiny chunks exercise chunk boundaries + index = self.files._index + all_rels = [(rel, d) for c in index.chunks for rel, d in zip(c.rels, c.dirs)] + for q in ('conf', 'config', 'report', 'hypr', 'app/src', 'mix', 'zzz'): + naive = sorted(((self.files.base_score(q, rel.lower(), d), rel) for rel, d in all_rels), reverse=True) + naive = [rel for s, rel in naive if s > 0] + got = [r['id'][len('files:'):] for r in self.files.search(q)] + self.assertEqual(set(got), set(naive[:self.files.MAX_RESULTS]), q) + + def test_basename_prefix_ranks_first_and_topk_is_bounded(self): + self.build() + out = self.files.search('config') + self.assertLessEqual(len(out), self.files.MAX_RESULTS) + self.assertIn(out[0]['title'], {'config.toml', 'config-notes.md', 'configs', 'configuration.txt'}) + scores = [r['score'] for r in out] + self.assertEqual(scores, sorted(scores, reverse=True)) + + def test_typing_forward_reuses_candidates(self): + self.build() + first = self.files.search('con') + narrowed = self.files.search('confi') + self.files._last.update(q='', version=-1, cands=None) + fresh = self.files.search('confi') + self.assertEqual([r['id'] for r in narrowed], [r['id'] for r in fresh]) + self.assertTrue(first) + + def test_short_query_is_ignored(self): + self.build() + self.assertEqual(self.files.search('co'), []) + + def test_hits_never_cross_entries(self): + chunk = self.files.Chunk(['ab', 'cd'], [False, False]) + self.assertEqual(list(chunk.hits('b\nc')), [0]) # found, but scoring rejects it + self.assertEqual(self.files.base_score('b\nc', 'ab', False), 0.0) + self.assertEqual(list(chunk.hits('d')), [1]) + + def test_actions_are_argv_and_use_omarchy_defaults(self): + with patch.object(self.files.shutil, 'which', side_effect=lambda x: '/usr/bin/' + x if x in {'omarchy-launch-editor', 'xdg-terminal-exec'} else None): + actions = self.files.actions_for('projects/app/src/main.py', False) + self.assertEqual(actions[0]['argv'], ['omarchy-launch-editor', str(self.home / 'projects/app/src/main.py')]) + term = [a for a in actions if a['title'] == 'Open terminal here'][0] + self.assertEqual(term['argv'], ['xdg-terminal-exec', '--dir=' + str(self.home / 'projects/app/src')]) + + +class FilesProtocolTest(unittest.TestCase): + def test_cold_query_answers_then_completes(self): + with tempfile.TemporaryDirectory() as tmp: + home = Path(tmp) + rng = random.Random(1) + for i in range(3000): + d = home / f'dir{i % 50}' + d.mkdir(exist_ok=True) + (d / f'note_{rng.randint(0, 99999)}_{i}.md').touch() + (home / 'dir7' / 'needle-target.txt').touch() + env = dict(os.environ, HOME=str(home), PATH='/usr/bin:/bin') + proc = subprocess.Popen([sys.executable, str(PROVIDER)], stdin=subprocess.PIPE, stdout=subprocess.PIPE, + stderr=subprocess.DEVNULL, text=True, env=env) + try: + start = time.monotonic() + proc.stdin.write(json.dumps({'id': 'q1', 'op': 'search', 'query': 'needle'}) + '\n') + proc.stdin.flush() + replies = [] + while True: + ready, _, _ = select.select([proc.stdout], [], [], 5) + self.assertTrue(ready, 'files provider did not answer') + msg = json.loads(proc.stdout.readline()) + replies.append(msg) + if not msg.get('partial'): + break + self.assertLess(time.monotonic() - start, 5) + self.assertTrue(all(m['id'] == 'q1' for m in replies)) + self.assertEqual([r['title'] for r in replies[-1]['results']], ['needle-target.txt']) + # Warm queries after the build are not partial. + proc.stdin.write(json.dumps({'id': 'q2', 'op': 'search', 'query': 'needle'}) + '\n') + proc.stdin.flush() + msg = json.loads(proc.stdout.readline()) + self.assertEqual(msg['id'], 'q2') + self.assertNotIn('partial', msg) + finally: + proc.stdin.close(); proc.wait(timeout=5); proc.stdout.close() + + +if __name__ == '__main__': + unittest.main() From 095da063eabb6a957abe730740747f84b8a3224c Mon Sep 17 00:00:00 2001 From: Grok Bot Date: Thu, 29 Oct 2026 16:18:33 -0400 Subject: [PATCH 05/19] menus: cache-first AT-SPI search with worker-thread walks - Walks on one worker thread with a 1.2s total budget; stdin never blocks. - Cached trees answer instantly; stale ones revalidate in the background. - Cold windows answer when the walk finishes unless cancelled/superseded. - warm op pre-walks the focused window when the overlay opens. - Lazy gi import: without gi/AT-SPI the provider answers [] instead of dying. - Execute matches window + path + label (never a stale index); errors are reported back. Stable per-item ids. --- providers/menus/README.md | 20 ++- providers/menus/provider.py | 291 ++++++++++++++++++++++++++++-------- tests/test_menus.py | 150 +++++++++++++++++++ 3 files changed, 396 insertions(+), 65 deletions(-) create mode 100644 tests/test_menus.py diff --git a/providers/menus/README.md b/providers/menus/README.md index f641c2a..a4f7b3f 100644 --- a/providers/menus/README.md +++ b/providers/menus/README.md @@ -5,9 +5,25 @@ Adapted from [MaNi4/omarchy-search-menu-items](https://github.com/MaNi4/omarchy- ## Port scope -- Included: frame finding, level-at-a-time tree fetches, menu item collection, DoAction invocation, 4s per-window walk cache. +- Included: frame finding, level-at-a-time tree fetches, menu item collection, DoAction invocation, per-window walk cache. - Deferred: button fallback (apps without menu bars), `org.gtk.Actions` export, multi-frame title disambiguation, Chromium `--force-renderer-accessibility` prompter. +## Latency + +- Walks run on a single worker thread with a 1.2 s total budget, so stdin + (searches, cancels) is never blocked. +- Cache-first: a window with a cached tree answers instantly; trees older + than 8 s are re-walked in the background (stale-while-revalidate); up to + 16 windows are kept. +- Cold window: the search waits for the walk and is answered when it + finishes, unless it was cancelled or superseded (only the newest query + per window waits). +- The host's `warm` op (sent when the overlay opens) starts the walk for the + focused window before the first keystroke. +- Missing `gi` or accessibility bus: the provider stays up and answers with + no results (logged once) instead of crashing and showing up as + "unavailable" on every query. + ## Requirements - `python-gobject` (the `gi` module) @@ -15,4 +31,4 @@ Adapted from [MaNi4/omarchy-search-menu-items](https://github.com/MaNi4/omarchy- ## Protocol notes -Speaks the full PROVIDERS.md protocol. `custom` actions ride back to this provider via the host's `execute` op: it presses the cached menu item by id. Scores are deliberately sub-0.55 so app launches (0.9 range) outrank menu items on shared queries. +Speaks the full PROVIDERS.md protocol. `custom` actions ride back to this provider via the host's `execute` op. The action carries the window address, menu path and label; the provider presses only an item that still matches all three in the focused window (a re-walk between search and Enter can never press a different item) and replies `executed:false` with an `error` otherwise. Scores are deliberately sub-0.55 so app launches (0.9 range) outrank menu items on shared queries. diff --git a/providers/menus/provider.py b/providers/menus/provider.py index 2195ef0..6b4df77 100644 --- a/providers/menus/provider.py +++ b/providers/menus/provider.py @@ -9,25 +9,41 @@ deferred: the button fallback, the org.gtk.Actions export, multi-frame title disambiguation, and the Chromium accessibility-flag prompter. -The focused app resolves from Hyprland (hyprctl activewindow -j). Walks are -cached per window for CACHE_TTL seconds, because walking a whole tree per -keystroke would waste the launcher's latency budget. +The focused app resolves from Hyprland (hyprctl activewindow -j). + +Latency model (cache-first, never blocking stdin): +- Walks run on one worker thread with a total time budget (WALK_BUDGET). +- A search for a window with a cached tree answers immediately from the + cache; a cache older than CACHE_TTL is re-walked in the background + (stale-while-revalidate). +- A search for a window with no cache queues a walk and answers when it + finishes, unless the query was cancelled or superseded first. +- The host's `warm` op (overlay opened) starts the walk before the first + keystroke, so the cache is usually ready by the time you type. +- Without python-gobject or an accessibility bus the provider stays up and + answers every search with no results instead of crashing. Requires: python-gobject (gi), hyprctl in PATH, AT-SPI enabled. """ from __future__ import annotations +import hashlib import json +import queue import re import subprocess import sys +import threading import time from dataclasses import dataclass, field from functools import partial from typing import Callable, NamedTuple -from gi.repository import Gio, GLib +try: # Optional: without gi the provider answers with no results. + from gi.repository import Gio, GLib +except Exception: # ImportError, ValueError (namespace version), ... + Gio = GLib = None ACCESSIBLE = "org.a11y.atspi.Accessible" ACTION = "org.a11y.atspi.Action" @@ -42,9 +58,12 @@ MAX_DEPTH = 12 MAX_NODES = 6000 -READ_BUDGET = 2.0 # seconds per walk -CALL_TIMEOUT = 1500 # milliseconds per batched round -CACHE_TTL = 4.0 # seconds before a window's menu tree is walked again +WALK_BUDGET = 1.2 # seconds for a whole walk (frame search + menu tree) +CALL_TIMEOUT = 800 # milliseconds per batched round +CACHE_TTL = 8.0 # seconds before a cached tree is re-walked in the background +CACHE_MAX = 16 # windows kept +FOCUS_TTL = 0.75 # seconds a hyprctl focus lookup is reused +MAX_RESULTS = 12 EXPECTED_ERRORS = { "org.freedesktop.DBus.Error.UnknownMethod", @@ -146,13 +165,13 @@ def focused_window() -> dict | None: class Batch: """Sends many calls on one connection and waits for all answers.""" - def __init__(self, connection: Gio.DBusConnection, timeout: int = CALL_TIMEOUT): + def __init__(self, connection, timeout: int = CALL_TIMEOUT): self.connection = connection self.timeout = timeout self.loop = GLib.MainLoop() self.pending = 0 - def call(self, ref: Ref, interface: str, method: str, arguments: GLib.Variant | None, + def call(self, ref: Ref, interface: str, method: str, arguments, done: Callable[[tuple | None], None]) -> None: self.pending += 1 @@ -179,7 +198,7 @@ def wait(self) -> None: self.loop.run() -def accessibility_bus(session: Gio.DBusConnection) -> Gio.DBusConnection: +def accessibility_bus(session): address = session.call_sync( "org.a11y.Bus", "/org/a11y/bus", "org.a11y.Bus", "GetAddress", None, GLib.VariantType("(s)"), Gio.DBusCallFlags.NONE, 1000, None, @@ -323,20 +342,21 @@ def collect_items(tree: Tree, menubar: Ref, deadline: float) -> list[Item]: ] -def walk_items(a11y: Gio.DBusConnection, pid: int, title: str) -> list[Item]: +def walk_items(a11y, pid: int, title: str) -> list[Item]: + deadline = time.monotonic() + WALK_BUDGET batch = Batch(a11y) frames, _ = app_frames(batch, pid) frame = choose_frame(frames, title) - if frame is None: + if frame is None or time.monotonic() > deadline: return [] tree = Tree(batch) - menubar = find_menubar(tree, frame, frames[frame], time.monotonic() + READ_BUDGET) + menubar = find_menubar(tree, frame, frames[frame], deadline) if menubar is None: return [] - return collect_items(tree, menubar, time.monotonic() + READ_BUDGET) + return collect_items(tree, menubar, deadline) -def press(a11y: Gio.DBusConnection, ref: Ref) -> None: +def press(a11y, ref: Ref) -> None: a11y.call_sync( ref.bus, ref.path, ACTION, "DoAction", GLib.Variant("(i)", (0,)), None, Gio.DBusCallFlags.NONE, 2000, None, @@ -363,64 +383,209 @@ def log(*args: object) -> None: print("menus:", *args, file=sys.stderr, flush=True) -def main() -> None: - session = Gio.bus_get_sync(Gio.BusType.SESSION, None) - a11y = accessibility_bus(session) - cache: dict = {} # key: window address -> {"at": float, "pid": int, "items": list[Item]} +def item_id(address: str, item: Item) -> str: + digest = hashlib.sha1("\u0000".join([address, *item.path, item.label]).encode()).hexdigest()[:12] + return f"menus:{digest}" + + +def results_for(query: str, address: str, items: list[Item]) -> list[dict]: + scored = [(score(query, item), i, item) for i, item in enumerate(items)] + scored = sorted((t for t in scored if t[0] > 0.0), key=lambda t: (-t[0], t[1]))[:MAX_RESULTS] + return [ + { + "id": item_id(address, item), + "title": item.label, + "subtitle": " \u203a ".join(item.path) + (f" ({item.key})" if item.key else ""), + "icon": "system-run", + "score": s, + "provider": "menus", + # The action names the item by path + label so a re-walk between + # search and Enter can never press a different item. + "actions": [{"type": "custom", "kind": "press", "id": i, "window": address, + "path": list(item.path), "label": item.label}] if item.enabled else [], + } + for s, i, item in scored + ] + +class MenuService: + """Cache-first menu search with walks on a worker thread. + + `walker(window) -> list[Item]` and `focus() -> dict | None` are injected + so the scheduling logic is testable without AT-SPI. + """ + + def __init__(self, walker, focus, send, pressor=None): + self.walker, self.focus_lookup, self.send, self.pressor = walker, focus, send, pressor + self.cache: dict[str, dict] = {} + self.lock = threading.Lock() + self.jobs: "queue.Queue[dict | None]" = queue.Queue() + self.walking: set[str] = set() + self.waiting: dict[str, tuple[str, str]] = {} # qid -> (address, query) + self.focus_cache: tuple[float, dict | None] = (0.0, None) + self.worker = threading.Thread(target=self._work, name="menus-walk", daemon=True) + self.worker.start() + + def focus(self, fresh: bool = False) -> dict | None: + at, window = self.focus_cache + if fresh or time.monotonic() - at > FOCUS_TTL: + window = self.focus_lookup() + self.focus_cache = (time.monotonic(), window) + return window + + def schedule(self, window: dict) -> None: + address = str(window.get("address", "")) + with self.lock: + if address in self.walking: + return + self.walking.add(address) + self.jobs.put(window) + + def _work(self) -> None: + while True: + window = self.jobs.get() + if window is None: + return + address = str(window.get("address", "")) + try: + items = self.walker(window) + except Exception as err: # never let one app kill the worker + log(f"walk failed: {err}") + items = None + with self.lock: + self.walking.discard(address) + if items is not None: + self.cache[address] = {"at": time.monotonic(), "items": items} + while len(self.cache) > CACHE_MAX: + oldest = min(self.cache, key=lambda k: self.cache[k]["at"]) + del self.cache[oldest] + ready = [(qid, q) for qid, (addr, q) in self.waiting.items() if addr == address] + for qid, _ in ready: + del self.waiting[qid] + cached = self.cache.get(address) + for qid, q in ready: + self.send({"id": qid, "results": results_for(q, address, cached["items"]) if cached else []}) + + def warm(self) -> None: + window = self.focus(fresh=True) + if window is None: + return + cached = self.cache.get(str(window.get("address", ""))) + if cached is None or time.monotonic() - cached["at"] > CACHE_TTL: + self.schedule(window) + + def search(self, qid: str, query: str) -> None: + query = query.strip().lower() + window = self.focus() + if window is None or not query: + self.send({"id": qid, "results": []}); return + address = str(window.get("address", "")) + with self.lock: + cached = self.cache.get(address) + if cached is None: + # Only the newest query waits for a walk. + self.waiting = {k: v for k, v in self.waiting.items() if v[0] != address} + self.waiting[qid] = (address, query) + if cached is None: + self.schedule(window) + return + self.send({"id": qid, "results": results_for(query, address, cached["items"])}) + if time.monotonic() - cached["at"] > CACHE_TTL: + self.schedule(window) + + def cancel(self, target: str) -> None: + with self.lock: + self.waiting.pop(target, None) + self.send({"id": target, "cancelled": True}) + + def find(self, action: dict) -> Item | None: + address = str(action.get("window", "")) + with self.lock: + cached = self.cache.get(address) + if cached is None: + return None + items = cached["items"] + path, label = action.get("path"), action.get("label") + index = action.get("id") + if isinstance(index, int) and 0 <= index < len(items): + item = items[index] + if item.label == label and item.path == path: + return item + for item in items: + if item.label == label and item.path == path: + return item + return None + + def execute(self, aid: str, action: dict) -> None: + ok, error = False, "" + window = self.focus(fresh=True) + if action.get("kind") != "press": + error = "unsupported action" + elif window is None or str(window.get("address", "")) != str(action.get("window", "")): + error = "the window that owned this menu item is no longer focused" + else: + item = self.find(action) + if item is None: + error = "menu item is gone" + elif self.pressor is None: + error = "accessibility bus unavailable" + else: + try: + self.pressor(item.ref); ok = True + except Exception as err: + error = f"could not press menu item: {err}" + reply = {"id": aid, "executed": ok} + if error: + reply["error"] = error + self.send(reply) + + +_out_lock = threading.Lock() + + +def send(msg: dict) -> None: + with _out_lock: + print(json.dumps(msg), flush=True) + + +def connect(): + """The accessibility bus connection, or None (logged once).""" + if Gio is None: + log("python-gobject (gi) not available; menu search disabled") + return None + try: + return accessibility_bus(Gio.bus_get_sync(Gio.BusType.SESSION, None)) + except Exception as err: + log(f"accessibility bus unavailable: {err}") + return None + + +def main() -> None: + a11y = connect() + if a11y is None: + walker = lambda window: [] + pressor = None + else: + walker = lambda window: walk_items(a11y, int(window["pid"]), str(window.get("title", ""))) + pressor = lambda ref: press(a11y, ref) + service = MenuService(walker, focused_window, send, pressor) for line in sys.stdin: try: msg = json.loads(line) except json.JSONDecodeError: continue - + if not isinstance(msg, dict): + continue op = msg.get("op") if op == "search": - query = str(msg.get("query", "")).strip().lower() - window = focused_window() - items: list[Item] = [] - if window: - key = window["address"] - cached = cache.get(key) - if cached and time.monotonic() - cached["at"] < CACHE_TTL: - items = cached["items"] - else: - try: - items = walk_items(a11y, int(window["pid"]), str(window.get("title", ""))) - cache[key] = {"at": time.monotonic(), "pid": int(window["pid"]), "items": items} - except Exception as err: - log(f"walk failed: {err}") - results = [ - { - "id": f"menus:{i}", - "title": item.label, - "subtitle": " \u203a ".join(item.path) + (f" ({item.key})" if item.key else ""), - "icon": "system-run", - "score": s, - "provider": "menus", - "actions": [{"type": "custom", "kind": "press", "id": i}] if item.enabled else [], - } - for i, item in enumerate(items) - for s in [score(query, item)] - if s > 0.0 - ][:12] - print(json.dumps({"id": msg.get("id", ""), "results": results}), flush=True) - + service.search(str(msg.get("id", "")), str(msg.get("query", ""))) + elif op == "warm": + service.warm() elif op == "execute": - action = msg.get("action") or {} - window = focused_window() - ok = False - if window and action.get("kind") == "press": - cached = cache.get(window["address"]) - if cached: - index = action.get("id") - if isinstance(index, int) and 0 <= index < len(cached["items"]): - press(a11y, cached["items"][index].ref) - ok = True - print(json.dumps({"id": msg.get("id", ""), "executed": ok}), flush=True) - + action = msg.get("action") + service.execute(str(msg.get("id", "")), action if isinstance(action, dict) else {}) elif op == "cancel": - print(json.dumps({"id": msg.get("target", ""), "cancelled": True}), flush=True) + service.cancel(str(msg.get("target", ""))) if __name__ == "__main__": diff --git a/tests/test_menus.py b/tests/test_menus.py new file mode 100644 index 0000000..229fb6c --- /dev/null +++ b/tests/test_menus.py @@ -0,0 +1,150 @@ +"""menus provider: cache-first, background walks, cancel, safe execute.""" +import importlib.util +import json +import os +from pathlib import Path +import subprocess +import sys +import threading +import time +import unittest + +ROOT = Path(__file__).resolve().parents[1] +SPEC = importlib.util.spec_from_file_location('menus_provider', ROOT / 'providers/menus/provider.py') +menus = importlib.util.module_from_spec(SPEC) +sys.modules['menus_provider'] = menus +SPEC.loader.exec_module(menus) + + +def item(label, *path, enabled=True): + return menus.Item(list(path), label, '', enabled, False, menus.Ref(':1.1', '/' + label)) + + +class Harness: + def __init__(self, items, delay=0.0, window='0xabc'): + self.sent, self.pressed, self.walks = [], [], 0 + self.items, self.delay, self.window = items, delay, window + self.event = threading.Event() + self.service = menus.MenuService(self.walk, self.focus, self.record, self.pressed.append) + + def walk(self, window): + self.walks += 1 + time.sleep(self.delay) + return list(self.items) + + def focus(self): + return {'address': self.window, 'pid': 1, 'title': 't'} if self.window else None + + def record(self, msg): + self.sent.append(msg) + self.event.set() + + def wait_for(self, count, timeout=2.0): + end = time.monotonic() + timeout + while len(self.sent) < count and time.monotonic() < end: + time.sleep(0.005) + return self.sent + + +class MenuServiceTests(unittest.TestCase): + def test_cold_search_answers_after_background_walk(self): + h = Harness([item('Save', 'File'), item('Save As…', 'File'), item('Quit', 'File')], delay=0.05) + h.service.search('q1', 'save') + self.assertEqual(h.sent, [], 'cold search must not block or answer early') + sent = h.wait_for(1) + self.assertEqual(sent[0]['id'], 'q1') + self.assertEqual([r['title'] for r in sent[0]['results']], ['Save', 'Save As…']) + + def test_warm_search_answers_from_cache_immediately(self): + h = Harness([item('Save', 'File')]) + h.service.warm() + time.sleep(0.05) + started = time.monotonic() + h.service.search('q2', 'sav') + self.assertLess(time.monotonic() - started, 0.05) + self.assertEqual(h.sent[-1]['id'], 'q2') + self.assertEqual(h.walks, 1) + + def test_cancelled_cold_query_never_answers(self): + h = Harness([item('Save', 'File')], delay=0.05) + h.service.search('q1', 'save') + h.service.cancel('q1') + time.sleep(0.15) + self.assertEqual(h.sent, [{'id': 'q1', 'cancelled': True}]) + + def test_superseded_cold_query_only_newest_answers(self): + h = Harness([item('Save', 'File')], delay=0.05) + h.service.search('q1', 'sa') + h.service.search('q2', 'save') + sent = h.wait_for(1) + time.sleep(0.05) + self.assertEqual([m['id'] for m in h.sent], ['q2']) + self.assertEqual(h.walks, 1, 'one walk per window at a time') + + def test_stale_cache_serves_then_revalidates(self): + h = Harness([item('Save', 'File')]) + h.service.warm(); time.sleep(0.05) + h.service.cache['0xabc']['at'] -= menus.CACHE_TTL + 1 + h.items = [item('Save', 'File'), item('Save All', 'File')] + h.service.search('q3', 'save') + self.assertEqual(len(h.sent[-1]['results']), 1) # served stale, instantly + time.sleep(0.05) + h.service.search('q4', 'save') + self.assertEqual(len(h.sent[-1]['results']), 2) + + def test_walk_failure_answers_empty(self): + h = Harness([]) + def boom(window): + raise RuntimeError('a11y exploded') + h.service.walker = boom + h.service.search('q1', 'x') + self.assertEqual(h.wait_for(1)[0], {'id': 'q1', 'results': []}) + + def test_execute_presses_matching_item_by_path_and_label(self): + h = Harness([item('Save', 'File'), item('Quit', 'File')]) + h.service.warm(); time.sleep(0.05) + h.service.search('q', 'quit') + action = h.sent[-1]['results'][0]['actions'][0] + # The tree changes order before Enter: the index is stale. + h.service.cache['0xabc']['items'] = [item('Quit', 'File'), item('Save', 'File')] + h.service.execute('a1', action) + self.assertEqual(h.sent[-1], {'id': 'a1', 'executed': True}) + self.assertEqual(h.pressed[-1].path, '/Quit') + + def test_execute_refuses_other_window_or_missing_item(self): + h = Harness([item('Quit', 'File')]) + h.service.warm(); time.sleep(0.05) + h.service.search('q', 'quit') + action = dict(h.sent[-1]['results'][0]['actions'][0]) + h.window = '0xother' + h.service.execute('a1', action) + self.assertFalse(h.sent[-1]['executed']) + self.assertIn('no longer focused', h.sent[-1]['error']) + h.window = '0xabc' + h.service.execute('a2', dict(action, label='Delete Everything')) + self.assertFalse(h.sent[-1]['executed']) + self.assertEqual(h.pressed, []) + + def test_disabled_items_have_no_actions_and_ids_are_stable(self): + h = Harness([item('Undo', 'Edit', enabled=False)]) + h.service.warm(); time.sleep(0.05) + h.service.search('q', 'undo') + first = h.sent[-1]['results'][0] + self.assertEqual(first['actions'], []) + h.service.search('q2', 'undo') + self.assertEqual(h.sent[-1]['results'][0]['id'], first['id']) + + +class MenusWithoutGiTest(unittest.TestCase): + def test_provider_stays_up_and_answers_without_accessibility(self): + env = dict(os.environ, PATH='/usr/bin:/bin', DBUS_SESSION_BUS_ADDRESS='unix:path=/nonexistent') + code = ("import sys; sys.modules['gi'] = None\n" + "import runpy; runpy.run_path(%r, run_name='__main__')" % str(ROOT / 'providers/menus/provider.py')) + proc = subprocess.run([sys.executable, '-c', code], input=json.dumps({'id': 'q1', 'op': 'search', 'query': 'save'}) + '\n', + text=True, capture_output=True, timeout=10, env=env) + self.assertEqual(proc.returncode, 0, proc.stderr) + self.assertEqual(json.loads(proc.stdout.splitlines()[0]), {'id': 'q1', 'results': []}) + + +if __name__ == '__main__': + unittest.main() From b01ef748d87cd9be6954068ea831f0c42d0ea650 Mon Sep 17 00:00:00 2001 From: Grok Bot Date: Thu, 29 Oct 2026 16:19:13 -0400 Subject: [PATCH 06/19] run: mark Run: row as fallback; id carries the query The frontend ranks fallbacks below every real match regardless of score, and a per-query id keeps selection-follow and confirm binding exact. --- providers/run/provider.py | 11 +++++++---- tests/test_run.py | 28 ++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 4 deletions(-) create mode 100644 tests/test_run.py diff --git a/providers/run/provider.py b/providers/run/provider.py index 4b8b18b..9742bc5 100644 --- a/providers/run/provider.py +++ b/providers/run/provider.py @@ -8,8 +8,10 @@ deliberate boundary where typed text becomes a command, the action carries danger: confirm — the two-Enter sheet is the user's final say. -Scored low on purpose: a real app/file/action that matches should always -outrank the command suggestion. It is a floor, never a lid. +Scored low on purpose and marked `"fallback": true`: the frontend ranks +fallbacks below every real match regardless of score, so "Run:" is a floor, +never a lid. The id carries the query so selection-follow and confirmation +binding can never mistake one typed command for another. """ from __future__ import annotations import json @@ -22,7 +24,8 @@ def search(query: str) -> list[dict]: if len(q) < MIN_LEN: return [] shown = q if len(q) <= 64 else q[:61] + "..." return [{ - "id": "run:verbatim", + "id": "run:" + q, + "fallback": True, "title": f"Run: {shown}", "subtitle": "Run as a command in a foot terminal — executes verbatim", "icon": "utilities-terminal", @@ -53,6 +56,6 @@ def main() -> None: elif msg.get("op") == "cancel": print(json.dumps({"id": msg.get("target", ""), "cancelled": True}), flush=True) elif msg.get("op") == "execute": - print(json.dumps({"id": msg.get("id", ""), "executed": False}), flush=True) + print(json.dumps({"id": msg.get("id", ""), "executed": False, "error": "run actions are launched by the host"}), flush=True) if __name__ == "__main__": main() diff --git a/tests/test_run.py b/tests/test_run.py new file mode 100644 index 0000000..f426b44 --- /dev/null +++ b/tests/test_run.py @@ -0,0 +1,28 @@ +import importlib.util +from pathlib import Path +import unittest + +ROOT = Path(__file__).resolve().parents[1] +SPEC = importlib.util.spec_from_file_location('run_provider', ROOT / 'providers/run/provider.py') +run = importlib.util.module_from_spec(SPEC) +SPEC.loader.exec_module(run) + + +class RunProviderTests(unittest.TestCase): + def test_fallback_flag_id_and_argv(self): + [row] = run.search(' ls -la ') + self.assertTrue(row['fallback']) + self.assertEqual(row['id'], 'run:ls -la') + launch = row['actions'][0] + self.assertEqual(launch['argv'], ['foot', '--', 'sh', '-lc', 'ls -la']) + self.assertEqual(launch['danger'], 'confirm') + + def test_distinct_queries_get_distinct_ids(self): + self.assertNotEqual(run.search('rm a')[0]['id'], run.search('rm b')[0]['id']) + + def test_short_queries_produce_nothing(self): + self.assertEqual(run.search('ls'), []) + + +if __name__ == '__main__': + unittest.main() From 3f5e5ade1b74727249c4aa4f2c4cf730956554f1 Mon Sep 17 00:00:00 2001 From: Grok Bot Date: Thu, 29 Oct 2026 16:19:13 -0400 Subject: [PATCH 07/19] apps: rescan .desktop dirs in the background; refresh on warm A stale list keeps serving while a thread rescans, so keystrokes never pay for the rglob/parse pass. --- providers/apps/provider.py | 26 +++++++++++++++++++++++--- tests/test_apps.py | 20 ++++++++++++++++++++ 2 files changed, 43 insertions(+), 3 deletions(-) diff --git a/providers/apps/provider.py b/providers/apps/provider.py index 6890676..f0a7db8 100644 --- a/providers/apps/provider.py +++ b/providers/apps/provider.py @@ -12,6 +12,7 @@ import shlex import shutil import sys +import threading import time MAX_RESULTS = 15 @@ -155,12 +156,29 @@ def load_apps() -> list[dict]: return sorted(apps, key=lambda a: (a['name'].lower(), a['desktop_id'])) +_refreshing = threading.Lock() + + +def _refresh() -> None: + global _apps, _checked_at + try: + apps = load_apps() + _apps, _checked_at = apps, time.monotonic() # atomic swap + finally: + _refreshing.release() + + def ensure_fresh(force: bool = False) -> None: + """Load synchronously the first time (or when forced); afterwards a stale + list keeps serving while a background thread rescans the .desktop dirs, + so a keystroke never pays for the rescan.""" global _apps, _checked_at now = time.monotonic() - if force or now - _checked_at >= CHECK_TTL_S: - _apps = load_apps() - _checked_at = now + if force or _checked_at == float('-inf'): + _apps, _checked_at = load_apps(), time.monotonic() + return + if now - _checked_at >= CHECK_TTL_S and _refreshing.acquire(blocking=False): + threading.Thread(target=_refresh, name='apps-refresh', daemon=True).start() def score(query: str, app: dict) -> float: @@ -226,6 +244,8 @@ def main() -> None: continue if msg.get('op') == 'search': print(json.dumps({'id': msg.get('id', ''), 'results': search(str(msg.get('query', '')))}), flush=True) + elif msg.get('op') == 'warm': + ensure_fresh() elif msg.get('op') == 'cancel': print(json.dumps({'id': msg.get('target', ''), 'cancelled': True}), flush=True) elif msg.get('op') == 'execute': diff --git a/tests/test_apps.py b/tests/test_apps.py index 7f64ebc..ea7ef28 100644 --- a/tests/test_apps.py +++ b/tests/test_apps.py @@ -36,6 +36,26 @@ def desktop(self, relative='demo.desktop', fields='', system=False): target.write_text('[Desktop Entry]\nType=Application\nName=Demo\nExec=demo %U\n' + fields, encoding='utf-8') return target + def test_stale_list_serves_while_background_refresh_runs(self): + self.desktop() + apps.ensure_fresh() + self.assertEqual([a['name'] for a in apps._apps], ['Demo']) + self.desktop('other.desktop').write_text('[Desktop Entry]\nType=Application\nName=Other\nExec=other\n', encoding='utf-8') + apps._checked_at -= apps.CHECK_TTL_S + 1 + gate = __import__('threading').Event() + real = apps.load_apps + def slow(): + gate.wait(2) + return real() + with patch.object(apps, 'load_apps', side_effect=slow): + self.assertEqual([r['title'] for r in apps.search('o')], ['Demo']) # stale, not blocked + gate.set() + for _ in range(200): + if len(apps._apps) == 2: + break + __import__('time').sleep(0.01) + self.assertEqual(sorted(a['name'] for a in apps._apps), ['Demo', 'Other']) + def test_terminal_primary_action_uses_default_terminal_dispatcher(self): self.desktop(fields='Terminal=true\n') result = apps.search('demo')[0] From 807a6524252259890383319283bad0699cd3e0da Mon Sep 17 00:00:00 2001 From: Grok Bot Date: Thu, 29 Oct 2026 16:20:18 -0400 Subject: [PATCH 08/19] host: frecency history boosts recently chosen results The frontend sends op:record when a result is executed; the host stores decaying use counts in $XDG_STATE_HOME/pallet/history.json (400 entries, 14-day half-life) and annotates rows with recent (0..1), which ranking uses only within a match tier. Run: commands are never stored. PALLET_HISTORY=0 disables it. --- shell/ProviderBridge.qml | 7 ++++ src/host.py | 73 +++++++++++++++++++++++++++++++++- tests/test_history.py | 86 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 164 insertions(+), 2 deletions(-) create mode 100644 tests/test_history.py diff --git a/shell/ProviderBridge.qml b/shell/ProviderBridge.qml index 50f26a8..ab49d0b 100644 --- a/shell/ProviderBridge.qml +++ b/shell/ProviderBridge.qml @@ -86,8 +86,15 @@ Process { Quickshell.execDetached(argv); } } + // Remember what was chosen so the host can boost it next time (frecency). + function record(result: var): void { + if (!root.running || !result || typeof result.provider !== "string") return; + root.write(JSON.stringify({ id: "", op: "record", provider: result.provider, + result: String(result.id ?? ""), title: String(result.title ?? "") }) + "\n"); + } function execute(action: var, result: var): void { if (action === undefined || action === null) return; + root.record(result); switch (action.type) { case "launch": root.launch(action.argv); break; case "open": root.launch(["xdg-open", String(action.url ?? action.path ?? "")]); break; diff --git a/src/host.py b/src/host.py index 316d629..a55d766 100644 --- a/src/host.py +++ b/src/host.py @@ -38,6 +38,11 @@ # the client is connected) so slow providers still show up instead of vanishing. LATE_GRACE_S = 2.5 RESTART_BACKOFF_S = 30.0 +# Frecency: chosen results get a small `recent` boost (0..1) the frontend +# folds into ranking *within* a match tier. Typed commands are never stored. +HISTORY_MAX = 400 +HISTORY_HALF_LIFE_S = 14 * 86400 +HISTORY_SKIP = {"run"} SCOPES = { "f": "files", "file": "files", "files": "files", "o": "obsidian", "ob": "obsidian", "obsidian": "obsidian", @@ -80,6 +85,65 @@ def split_scope(query: str) -> tuple[str, str]: def _int(value, default=0): return value if isinstance(value, int) and not isinstance(value, bool) and value >= 0 else default +def history_path(): + base = os.environ.get("XDG_STATE_HOME") or os.path.expanduser("~/.local/state") + return Path(base) / "pallet" / "history.json" + +class History: + """Per-result use counts with exponential decay, persisted as JSON. + + Disabled with PALLET_HISTORY=0. Keys match the frontend's Fuzzy.keyOf: + provider + NUL + (id or title).""" + def __init__(self, path=None, enabled=None): + self.enabled = os.environ.get("PALLET_HISTORY", "1") != "0" if enabled is None else enabled + self.path = path or history_path() + self.entries: dict[str, list] = {} + if self.enabled: self._load() + def _load(self): + try: data = json.loads(self.path.read_text(encoding="utf-8")) + except (OSError, ValueError): return + if isinstance(data, dict): + for key, value in data.items(): + if isinstance(key, str) and isinstance(value, list) and len(value) == 2 \ + and all(isinstance(v, (int, float)) and not isinstance(v, bool) for v in value): + self.entries[key] = [float(value[0]), float(value[1])] + @staticmethod + def key(provider, rid, title=""): + return f"{provider}\0{rid or title}" + def record(self, provider, rid, title="", now=None): + if not self.enabled or not isinstance(provider, str) or not provider or provider in HISTORY_SKIP: return + if not (rid or title): return + now = time.time() if now is None else now + key = self.key(provider, rid, title) + weight, at = self.entries.get(key, (0.0, now)) + self.entries[key] = [self._decayed(weight, at, now) + 1.0, now] + if len(self.entries) > HISTORY_MAX: + for stale in sorted(self.entries, key=lambda k: self.entries[k][1])[:len(self.entries) - HISTORY_MAX]: + del self.entries[stale] + self._save() + @staticmethod + def _decayed(weight, at, now): + return weight * 0.5 ** (max(0.0, now - at) / HISTORY_HALF_LIFE_S) + def score(self, provider, rid, title="", now=None): + entry = self.entries.get(self.key(provider, rid, title)) + if not entry: return 0.0 + weight = self._decayed(entry[0], entry[1], time.time() if now is None else now) + return round(min(1.0, weight / 5.0), 3) + def annotate(self, rows): + if not self.enabled or not self.entries: return + now = time.time() + for row in rows: + value = self.score(row.get("provider", ""), row.get("id", ""), row.get("title", ""), now) + if value > 0 and "recent" not in row: row["recent"] = value + def _save(self): + try: + self.path.parent.mkdir(parents=True, exist_ok=True) + tmp = self.path.with_suffix(".tmp") + tmp.write_text(json.dumps(self.entries, separators=(",", ":")), encoding="utf-8") + os.replace(tmp, self.path) + except OSError as err: + log(f"history: could not save: {err}") + class Provider: def __init__(self, name, argv, inbox, spec=None, on_reply=None): spec = spec or {} @@ -149,8 +213,9 @@ def spawn_detached(argv): return None class Host: - def __init__(self): + def __init__(self, history=None): self.inbox = asyncio.Queue(); self.providers = []; self.merge_task = None; self.latest_qid = None + self.history = history if history is not None else History(enabled=False) # True while the client's stdin is open; enables late straggler emits. self.listening = False def on_provider_reply(self, name, msg): @@ -195,6 +260,8 @@ async def on_execute(self, msg): self.emit({"id": aid, "executed": False, "provider": wanted, "error": f"{wanted} provider is not running"}); return await provider.send({"id": aid, "op": "execute", "action": msg.get("action")}); return self.emit({"id": aid, "executed": False, "provider": wanted, "error": f"unknown provider: {wanted or '?'}"}) + async def on_record(self, msg): + self.history.record(msg.get("provider"), str(msg.get("result", "")), str(msg.get("title", ""))) async def on_launch(self, msg): """Launch argv (no shell) on the frontend's behalf so failures are reported.""" err = await asyncio.get_running_loop().run_in_executor(None, spawn_detached, msg.get("argv")) @@ -245,6 +312,7 @@ def snapshot(final): rows = [] for result in msg["results"]: if isinstance(result, dict): result.setdefault("provider", name); rows.append(result) + self.history.annotate(rows) by_provider[name] = rows if msg.get("partial") is not True: pending.discard(name) if emitted_final: @@ -272,11 +340,12 @@ async def read_client(host): elif op == "execute": await host.on_execute(msg) elif op == "launch": await host.on_launch(msg) elif op == "warm": await host.on_warm(msg) + elif op == "record": await host.on_record(msg) finally: host.listening = False async def amain(): - host = Host() + host = Host(History()) for manifest in discover_manifests(): try: spec = json.loads(manifest.read_text(encoding="utf-8")) except json.JSONDecodeError: continue diff --git a/tests/test_history.py b/tests/test_history.py new file mode 100644 index 0000000..40a830a --- /dev/null +++ b/tests/test_history.py @@ -0,0 +1,86 @@ +"""Frecency history: record, decay, annotate, persistence, opt-out.""" +import asyncio +import importlib.util +import json +from pathlib import Path +import tempfile +import unittest + +ROOT = Path(__file__).resolve().parents[1] +SPEC = importlib.util.spec_from_file_location('history_host', ROOT / 'src/host.py') +host_module = importlib.util.module_from_spec(SPEC) +SPEC.loader.exec_module(host_module) +History = host_module.History + + +class HistoryTests(unittest.TestCase): + def setUp(self): + self.tmp = tempfile.TemporaryDirectory() + self.addCleanup(self.tmp.cleanup) + self.path = Path(self.tmp.name) / 'state' / 'history.json' + + def test_record_annotate_and_persist(self): + h = History(self.path, enabled=True) + for _ in range(3): + h.record('apps', 'apps:firefox.desktop', 'Firefox') + rows = [{'provider': 'apps', 'id': 'apps:firefox.desktop', 'title': 'Firefox'}, + {'provider': 'apps', 'id': 'apps:foot.desktop', 'title': 'Foot'}] + h.annotate(rows) + self.assertGreater(rows[0]['recent'], 0) + self.assertNotIn('recent', rows[1]) + again = History(self.path, enabled=True) + self.assertAlmostEqual(again.score('apps', 'apps:firefox.desktop'), h.score('apps', 'apps:firefox.desktop'), places=2) + + def test_decay_and_cap(self): + h = History(self.path, enabled=True) + h.record('apps', 'a', now=0) + fresh = h.score('apps', 'a', now=0) + old = h.score('apps', 'a', now=host_module.HISTORY_HALF_LIFE_S * 4) + self.assertLess(old, fresh / 10) + for _ in range(20): + h.record('apps', 'a') + self.assertLessEqual(h.score('apps', 'a'), 1.0) + + def test_run_commands_are_never_stored(self): + h = History(self.path, enabled=True) + h.record('run', 'run:rm -rf ~/x', 'Run: rm -rf ~/x') + self.assertEqual(h.entries, {}) + self.assertFalse(self.path.exists()) + + def test_disabled_and_corrupt_file(self): + h = History(self.path, enabled=False) + h.record('apps', 'a') + self.assertFalse(self.path.exists()) + self.path.parent.mkdir(parents=True) + self.path.write_text('{"apps\\u0000a": "nope", "x": [1, 2]', encoding='utf-8') + self.assertEqual(History(self.path, enabled=True).entries, {}) + self.path.write_text(json.dumps({'apps\u0000a': 'nope', 'apps\u0000b': [2, 5]}), encoding='utf-8') + self.assertEqual(list(History(self.path, enabled=True).entries), ['apps\u0000b']) + + def test_lru_cap(self): + h = History(self.path, enabled=True) + for i in range(host_module.HISTORY_MAX + 5): + h.record('files', f'f{i}', now=i) + self.assertEqual(len(h.entries), host_module.HISTORY_MAX) + self.assertNotIn('files\u0000f0', h.entries) + + +class HostRecordTests(unittest.IsolatedAsyncioTestCase): + async def test_record_op_boosts_merged_rows(self): + tmp = tempfile.TemporaryDirectory(); self.addCleanup(tmp.cleanup) + host = host_module.Host(History(Path(tmp.name) / 'h.json', enabled=True)) + class P: + name, alive = 'apps', True + async def send(self, m): pass + host.providers = [P()] + emitted = []; host.emit = emitted.append + await host.on_record({'op': 'record', 'provider': 'apps', 'result': 'apps:x', 'title': 'X'}) + await host.inbox.put(('apps', {'id': 'q', 'results': [{'id': 'apps:x', 'title': 'X'}, {'id': 'apps:y', 'title': 'Y'}]})) + await host._merge('q', {'apps'}, '', 'x') + rows = emitted[-1]['results'] + self.assertIn('recent', rows[0]) + self.assertNotIn('recent', rows[1]) + + +if __name__ == '__main__': + unittest.main() From 3aff304b4548f0e1d15a4bb2ef5f14d0e7296533 Mon Sep 17 00:00:00 2001 From: Grok Bot Date: Thu, 29 Oct 2026 16:27:52 -0400 Subject: [PATCH 09/19] omarchy: full native menu parity via omarchy-menu summon, guards, confirmations, panels --- providers/omarchy/README.md | 89 +++ providers/omarchy/manifest.json | 2 +- providers/omarchy/provider.py | 597 ++++++++++++++++-- .../default/omarchy/omarchy-menu.jsonc | 27 + tests/test_omarchy.py | 196 ++++++ 5 files changed, 850 insertions(+), 61 deletions(-) create mode 100644 providers/omarchy/README.md create mode 100644 tests/fixtures/omarchy/default/omarchy/omarchy-menu.jsonc create mode 100644 tests/test_omarchy.py diff --git a/providers/omarchy/README.md b/providers/omarchy/README.md new file mode 100644 index 0000000..4fccdc5 --- /dev/null +++ b/providers/omarchy/README.md @@ -0,0 +1,89 @@ +# omarchy provider + +Parity with the native Omarchy menu: every entry of Omarchy's own menu +definition, searchable from the palette and run *by Omarchy itself*. + +## Source of truth + +| What | Source file (Omarchy) | +|---|---| +| Menu tree: ids, labels, aliases, `when`, `disabled`, `action` | `default/omarchy/omarchy-menu.jsonc` under `$OMARCHY_PATH`, `/usr/share/omarchy` or `~/.local/share/omarchy` | +| User overlay, merged per key like `MenuModel.mergeMenuSources` | `~/.config/omarchy/extensions/omarchy-menu.jsonc` | +| Running an entry | `bin/omarchy-menu` (`summon `: leaf → runs its action, submenu → opens it) | +| Guard semantics (8 batched groups, package snapshot, `omarchy-pkg-present/missing`, `omarchy-cmd-present/missing`) | `shell/menu/MenuModel.js` (`guardScript`) | + +Pallet never re-implements a menu command. Each row's action is +`["omarchy-menu", "summon", ""]` (argv, no shell), so the menu's own +action, any terminal wrapper and any follow-up prompt are exactly what the +native menu does. "Copy command" copies the menu's `action` string for +inspection only; it is never executed by Pallet. + +The file is re-read only when its mtime/size changes (about 13 ms for the +411-entry menu); a search is about 1 ms. + +## Guards (`when` / `disabled`) + +Omarchy's own trusted conditions are evaluated the way the native menu does: +one `bash -c` (own session, 8 s timeout, process group killed on timeout) runs +the conditions in 8 background groups with a pacman package snapshot. Answers +are cached for 30 s and refreshed in a background thread on `warm` and on +search, so typing never waits on them. This is a deliberate decision: it runs +only conditions shipped in Omarchy's menu file (or the user's own overlay), +never anything derived from a query, and it is not PATH discovery. + +- `when` false (on the entry or any ancestor): the row is hidden. Toggle pairs + (Nightlight On/Off, …) therefore show only the side that applies. +- `disabled` true: the row stays listed as `· ✓ installed` with no run action. +- Unknown (not evaluated yet, timed out, or a condition that exits its group): + the row summons its **parent** menu instead (`· opens in the Omarchy menu`), + so the native menu applies its guard. A guarded action is never run blind. +- `checked` is not evaluated (Pallet has no checkmarks). + +Entries that cannot be reached in the native menu (orphans, or children of an +entry that has its own action) are skipped. + +## Confirmations + +Rows matching these id prefixes carry `danger: "confirm"` (tone in brackets): + +- `system.suspend`, `system.hibernate`, `system.logout`, `system.reboot` (warning), `system.shutdown` (danger) +- `remove.*` (danger), `setup.reset` (danger), `setup.plugin.remove` (danger) +- `setup.direct-boot`, `setup.security.*`, `setup.network.dns.*` (warning) +- `update.omarchy`, `update.themes` (positive); `update.channel.*`, `update.config.*`, + `update.process.*`, `update.hardware.*`, `update.firmware`, `update.password.*`, `update.time` (warning) +- `trigger.reminder.clear` (danger), `trigger.hardware.hybrid-gpu-*` (warning) +- `appearance.branding.about.default`, `appearance.branding.screensaver.default` (warning) +- `install.*` (positive), except the pickers `install.package`, `install.aur`, + `install.webapp`, `install.tui` and `update.timezone`, which only open a chooser. + +Lock is deliberately unconfirmed. Submenus never confirm (they only open). + +## Curated extras + +Shell panels and two actions not reachable as single menu rows. An extra is +dropped when the menu already has an entry running the identical command. + +| Entry | argv | Source | +|---|---|---| +| Network | `omarchy-shell shell summon omarchy.network` | `default/hypr/bindings/utilities.lua` (`SUPER+CTRL+N`), `shell/plugins/panels/network` | +| Bluetooth | `… summon omarchy.bluetooth` | `utilities.lua` (`SUPER+CTRL+B`), `shell/plugins/panels/bluetooth` | +| Audio | `… summon omarchy.audio` | `utilities.lua` (`SUPER+CTRL+A`), `shell/plugins/panels/audio` | +| Power | `… summon omarchy.power` | `utilities.lua` (`SUPER+CTRL+P`), `shell/plugins/panels/power` | +| Display (brightness) | `… summon omarchy.monitor` | `utilities.lua` (`SUPER+CTRL+D`), `shell/plugins/panels/monitor` | +| Calendar | `… summon omarchy.clock` | `utilities.lua` (`SUPER+CTRL+ALT+D`) | +| World Clock | `… summon omarchy.elsewhen` | `utilities.lua` (`SUPER+CTRL+ALT+E`), `shell/plugins/panels/elsewhen` | +| Tailscale | `… summon omarchy.tailscale` | `shell/plugins/panels/tailscale` (does nothing if the panel is not loaded) | +| Weather | `… summon omarchy.weather` | `shell/plugins/panels/weather` | +| Toggle Animations | `omarchy-toggle-animations` | `bin/omarchy-toggle-animations` | +| Omarchy Menu | `omarchy-menu toggle` | `bin/omarchy-menu` | + +`bin/omarchy-shell` accepts the three-argument `shell summon ` form. +Keybindings use `toggle`; Pallet uses `summon` so choosing a row never closes +an already open panel. + +## Tests + +`tests/test_omarchy.py` uses a trimmed fixture +(`tests/fixtures/omarchy/default/omarchy/omarchy-menu.jsonc`) and, when +`/workspace/omarchy-src` is present, also parses the real menu. +`tests/test_system.py` checks every extra's command exists in Omarchy's `bin/`. diff --git a/providers/omarchy/manifest.json b/providers/omarchy/manifest.json index 660935f..d1c9e37 100644 --- a/providers/omarchy/manifest.json +++ b/providers/omarchy/manifest.json @@ -1,6 +1,6 @@ { "name": "omarchy", - "description": "Omarchy menu parity: panels, triggers, style, and learning", + "description": "Every entry of the native Omarchy menu (run via omarchy-menu summon) plus shell panels", "exec": ["python3", "providers/omarchy/provider.py"], "triggers": [], "capabilities": ["search"] diff --git a/providers/omarchy/provider.py b/providers/omarchy/provider.py index 06a1472..0998cc0 100644 --- a/providers/omarchy/provider.py +++ b/providers/omarchy/provider.py @@ -1,82 +1,559 @@ #!/usr/bin/env python3 -"""Omarchy menu parity: panels, triggers, style, and learning. +"""Omarchy menu parity: every entry of the installed Omarchy menu, searchable. -Every argv below is copied verbatim from default/omarchy/omarchy-menu.jsonc -(upstream omarchy-menu definition). Panels are summoned through the same -omarchy-shell IPC the native menu uses; toggles run Omarchy's own commands -silently. Nothing is invented and no search text ever reaches a shell. -Glyphs are literal characters so the file survives any transport intact. +Source of truth is the menu definition Omarchy itself loads +(shell/plugins/menu/Menu.qml): `$OMARCHY_PATH/default/omarchy/omarchy-menu.jsonc` +overlaid per key by the user's `~/.config/omarchy/extensions/omarchy-menu.jsonc` +(same merge as MenuModel.mergeMenuSources). Nothing is vendored or invented: +if the file is missing, only the small curated panel list below is offered. + +Running an entry never re-implements its command. Every row launches +`omarchy-menu summon ` as argv; the native menu resolves the id and runs +a leaf's action itself, or opens a submenu (bin/omarchy-menu, +Menu.qml openRoute). Search text never reaches a shell. + +Guards (`when:` hides a row, `disabled:` marks installed software) are the +menu's own bash conditions. They are evaluated off the request path, in one +background bash batch built only from the trusted menu files - the same thing +the native menu does on every open (MenuModel.guardScript). Until a guarded +row's answer is known, it opens its parent submenu instead of running, so the +native menu applies the guard. `checked:` marks are not evaluated. + +Destructive or system-state entries (power, remove, reset, updates, security, +DNS, channel, ...) carry `danger: confirm` so they need a second Enter. """ from __future__ import annotations + import json +import os +import re +import signal +import subprocess import sys +import threading +import time +from pathlib import Path MAX_RESULTS = 15 +GUARD_TTL_S = 30.0 +GUARD_TIMEOUT_S = 8.0 +GUARD_GROUPS = 8 BLUE = "#89b4fa" GREEN = "#a6e3a1" YELLOW = "#f9e2af" PURPLE = "#cba6f7" +RED = "#f38ba8" + + +# ----------------------------------------------------------------- menu files + +def omarchy_roots() -> list[Path]: + roots = [] + env = os.environ.get("OMARCHY_PATH") + if env: + roots.append(Path(env)) + roots += [Path("/usr/share/omarchy"), Path.home() / ".local/share/omarchy"] + return roots + + +def menu_paths() -> tuple[Path | None, Path]: + default = next((r / "default/omarchy/omarchy-menu.jsonc" for r in omarchy_roots() + if (r / "default/omarchy/omarchy-menu.jsonc").is_file()), None) + user = Path.home() / ".config/omarchy/extensions/omarchy-menu.jsonc" + return default, user + + +def strip_jsonc(text: str) -> str: + """Drop // and /* */ comments outside strings, then trailing commas.""" + out, i, n, in_string = [], 0, len(text), False + while i < n: + c = text[i] + if in_string: + out.append(c) + if c == "\\" and i + 1 < n: + out.append(text[i + 1]); i += 2; continue + if c == '"': + in_string = False + elif c == '"': + in_string = True; out.append(c) + elif text.startswith("//", i): + while i < n and text[i] != "\n": + i += 1 + continue + elif text.startswith("/*", i): + end = text.find("*/", i + 2) + i = n if end < 0 else end + 2 + continue + else: + out.append(c) + i += 1 + return re.sub(r",(\s*[}\]])", r"\1", "".join(out)) + + +def _aliases(value) -> list[str]: + if isinstance(value, list): + return [v for v in value if isinstance(v, str) and v] + return [value] if isinstance(value, str) and value else [] + + +def normalize(item_id: str, raw: dict) -> dict: + parent = raw.get("parent") + if not isinstance(parent, str): + parent = item_id.rsplit(".", 1)[0] if "." in item_id else "root" + text = lambda k: raw.get(k) if isinstance(raw.get(k), str) else "" + return { + "id": item_id, "parent": parent, "label": text("label") or item_id, + "action": text("action"), "target": text("target"), "provider": text("provider"), + "description": text("description"), "aliases": _aliases(raw.get("aliases")), + "when": text("when"), "disabled": text("disabled"), + } + + +def parse_menu(text: str) -> list[dict]: + try: + data = json.loads(strip_jsonc(text)) + except ValueError: + return [] + if not isinstance(data, dict): + return [] + if isinstance(data.get("items"), dict): + data = data["items"] + return [normalize(k, v) for k, v in data.items() if isinstance(k, str) and isinstance(v, dict)] + -def shell(*parts: str) -> list[str]: - return ["omarchy-shell", "shell", "summon", *parts] - -def entry(title: str, subtitle: str, icon: str, argv: list[str], aliases: str, icons: list[str] | None = None, glyph: str = "", color: str = BLUE) -> dict: - return {"title": title, "subtitle": subtitle, "icon": icon, "icons": icons or [], "fallbackIcon": glyph, "fallbackColor": color, - "aliases": (title + " " + aliases).lower(), - "actions": [{"type": "launch", "title": title, "argv": argv}, {"type": "copy", "title": "Copy command", "text": " ".join(argv)}]} - -ENTRIES: list[dict] = [ - # Panels (bar-widget control panels, summoned like the native shell does) - entry("Network", "Wi-Fi and networking panel", "network-wireless", shell("omarchy.network"), "wifi wi-fi network internet ethernet wireless connection", glyph="📶"), - entry("Bluetooth", "Bluetooth devices panel", "bluetooth-active", shell("omarchy.bluetooth"), "bluetooth bt devices pair", icons=["bluetooth", "bluetooth-symbolic"], glyph="🔵"), - entry("Audio", "Volume and devices panel", "audio-volume-high", shell("omarchy.audio"), "audio sound volume mic microphone speaker output input", glyph="🔊"), - entry("Power", "Battery and power profiles panel", "battery-full", shell("omarchy.power"), "power battery charge profile laptop", icons=["battery-good", "preferences-system-power"], glyph="🔋"), - entry("Network Speed Test", "Measure internet throughput", "network-wireless", shell("omarchy.speedtest"), "speedtest speed test network bandwidth download upload", glyph="⏱"), - # Trigger: tools and silent toggles, exactly the native menu's actions - entry("Emoji Picker", "Pick an emoji into the clipboard", "face-smile", ["omarchy-menu-emoji"], "emoji emojis smile picker symbol", glyph="😀"), - entry("Screenshot", "Capture the screen (interactive)", "applets-screenshooter", ["omarchy-capture-screenshot"], "screenshot capture screen shot grab", icons=["camera-photo", "screenshooter"], glyph="📸"), - entry("Stay Awake", "Toggle idle lock (caffeine)", "weather-clear-night", ["omarchy-toggle-idle"], "stay awake caffeine idle lock keep on", glyph="☕", color=YELLOW), - entry("Notifications", "Toggle notification silencing", "preferences-system-notifications", ["omarchy-toggle-notification-silencing"], "notifications silence silent mute do not disturb dnd", glyph="🔔", color=YELLOW), - entry("Nightlight", "Toggle the nightlight (hyprsunset)", "night-light", ["omarchy-toggle-nightlight"], "nightlight night light warm blue filter", icons=["weather-clear-night"], glyph="🌗", color=YELLOW), - entry("Menu Bar", "Toggle the top menu bar", "panel", ["omarchy-toggle-bar"], "menu bar top bar hide bar show bar", glyph="▤", color=YELLOW), - entry("Window Gaps", "Toggle Hyprland window gaps", "view-grid", ["omarchy-hyprland-window-gaps-toggle"], "window gaps gap spacing borders", glyph="▫", color=YELLOW), - entry("Animations", "Toggle Hyprland animations", "animations", ["omarchy-toggle-animations"], "animations animate effects motion blur", glyph="✨", color=YELLOW), - # Style - entry("Theme", "Pick a system theme", "preferences-desktop-theme", shell("omarchy.image-picker", "{\"source\":\"themes\"}"), "theme themes color scheme colors style", glyph="🎨", color=PURPLE), - entry("Wallpaper", "Pick a desktop background", "preferences-desktop-wallpaper", ["bash", "-c", "background=$(omarchy-theme-bg-switcher); [[ -n $background ]] && omarchy-theme-bg-set \"$background\""], "wallpaper background bg picture image", glyph="🖼", color=PURPLE), - # Learn - entry("Keybindings", "Browse Omarchy keybindings", "input-keyboard", ["omarchy-menu-keybindings"], "keybindings keys shortcuts hotkeys help", glyph="⌨", color=GREEN), - entry("Omarchy Manual", "Open the official manual", "help-contents", ["omarchy-launch-webapp", "https://omarchy/manual/"], "omarchy manual docs documentation help learn", glyph="📖", color=GREEN), - entry("Hyprland Wiki", "Open the Hyprland wiki", "help-contents", ["omarchy-launch-webapp", "https://wiki.hypr.land/"], "hyprland wiki docs help learn", glyph="📘", color=GREEN), - entry("Arch Wiki", "Open the Arch Linux wiki", "help-contents", ["omarchy-launch-webapp", "https://wiki.archlinux.org/title/Main_page"], "arch wiki linux docs help learn", glyph="📚", color=GREEN), +def merge(defaults: list[dict], user: list[dict]) -> tuple[dict, list[str]]: + items: dict[str, dict] = {} + order: list[str] = [] + for source, raws in ((defaults, None), (user, None)): + for entry in source: + if entry["id"] not in items: + order.append(entry["id"]); items[entry["id"]] = dict(entry) + else: + # Per-key overlay: only fields the user file declares win. + declared = entry.get("_declared", entry.keys()) + items[entry["id"]].update({k: entry[k] for k in declared}) + return items, order + + +def parse_user(text: str) -> list[dict]: + """Like parse_menu, but remember which fields were declared so an + override that only retitles keeps the shipped action.""" + try: + data = json.loads(strip_jsonc(text)) + except ValueError: + return [] + if not isinstance(data, dict): + return [] + if isinstance(data.get("items"), dict): + data = data["items"] + out = [] + for k, v in data.items(): + if isinstance(k, str) and isinstance(v, dict): + entry = normalize(k, v) + entry["_declared"] = ["id"] + [f for f in v if f in entry] + out.append(entry) + return out + + +# ------------------------------------------------------------------ policies + +# Destructive or system-state routes need a second Enter. Prefix match on ids. +CONFIRM = [ + ("system.suspend", "warning"), ("system.hibernate", "warning"), ("system.logout", "warning"), + ("system.reboot", "warning"), ("system.shutdown", "danger"), + ("remove.", "danger"), ("setup.reset", "danger"), ("setup.direct-boot", "warning"), + ("setup.security.", "warning"), ("setup.network.dns.", "warning"), ("setup.plugin.remove", "danger"), + ("update.omarchy", "positive"), ("update.channel.", "warning"), ("update.config.", "warning"), + ("update.themes", "positive"), ("update.process.", "warning"), ("update.hardware.", "warning"), + ("update.firmware", "warning"), ("update.password.", "warning"), ("update.time", "warning"), + ("trigger.reminder.clear", "danger"), ("trigger.hardware.hybrid-gpu-", "warning"), + ("appearance.branding.about.default", "warning"), ("appearance.branding.screensaver.default", "warning"), + ("install.", "positive"), ] +# Pickers that only open an interactive chooser: nothing happens until you pick. +NO_CONFIRM = {"install.package", "install.aur", "install.webapp", "install.tui", "update.timezone"} + +# Extra words people type for an entry, beyond its labels/ids/aliases. +KEYWORDS = { + "system.shutdown": "shut down power off halt", + "system.reboot": "restart", + "system.logout": "log out sign out exit session", + "system.suspend": "sleep", + "system.lock": "lock screen", + "trigger.toggle.idle-lock-on": "caffeine stay awake idle", + "trigger.toggle.idle-lock-off": "caffeine stay awake idle", + "trigger.toggle.notifications-off": "dnd do not disturb mute silence", + "trigger.toggle.notifications-on": "dnd do not disturb unmute", + "trigger.capture.color": "color picker hyprpicker eyedropper", + "trigger.capture.text": "ocr text recognition", + "trigger.share.receive": "localsend", + "appearance.background": "wallpaper background", + "learn.omarchy": "manual docs documentation help", +} +CONFIRM_DETAIL = "Runs Omarchy's own menu action (omarchy-menu summon {id})." + + +def confirm_for(item_id: str) -> str: + if item_id in NO_CONFIRM: + return "" + for prefix, tone in CONFIRM: + if item_id == prefix or (prefix.endswith((".", "-")) and item_id.startswith(prefix)): + return tone + return "" + + +def display_title(items: dict, entry: dict) -> str: + label = entry["label"] + parts = entry["id"].split(".") + root = items.get(parts[0], {}).get("label", parts[0].title()) + parent = items.get(entry["parent"], {}) + if parts[0] in ("install", "remove") and len(parts) > 1: + return label if label.lower().startswith(root.lower()) else f"{root} {label}" + if parts[0] == "update" and len(parts) == 2: + return f"Update {label}" + if parts[0] == "update" and len(parts) > 2: + return f"Update {parent.get('label', '')}: {label}" + if entry["id"].startswith("setup.default.") and len(parts) == 4: + return f"Default {parent.get('label', '')}: {label}" + if entry["id"].startswith("setup.network.dns.") and len(parts) == 4: + return f"DNS: {label}" + return label + + +# ------------------------------------------------------------------- guards + +PKG_SNAPSHOT = ( + "declare -A __pallet_pkgs=()\n" + "while IFS= read -r __p; do __pallet_pkgs[$__p]=1; done < <({ pacman -Qq; LC_ALL=C pacman -Qi" + " | awk '/^[A-Za-z]/ { provides = ($0 ~ /^Provides/); sub(/^[^:]*: /, \"\") }" + " provides && $0 != \"None\" { n = split($0, p, \" \"); for (i = 1; i <= n; i++) { sub(/[<>=].*/, \"\", p[i]); print p[i] } }'; } 2>/dev/null)\n" + "__pallet_pkg_has() { [[ -n ${__pallet_pkgs[$1]-} ]] && return 0; [[ $1 == *[\\<\\>=]* ]] && { pacman -Q \"$1\" &>/dev/null; return; }; return 1; }\n" + "omarchy-pkg-present() { local p; for p in \"$@\"; do __pallet_pkg_has \"$p\" || return 1; done; return 0; }\n" + "omarchy-pkg-missing() { local p; for p in \"$@\"; do __pallet_pkg_has \"$p\" || return 0; done; return 1; }\n" + "omarchy-cmd-present() { local c; for c in \"$@\"; do command -v \"$c\" &>/dev/null || return 1; done; return 0; }\n" + "omarchy-cmd-missing() { local c; for c in \"$@\"; do command -v \"$c\" &>/dev/null || return 0; done; return 1; }\n" +) + + +def guard_script(guards: list[tuple[int, str, str]]) -> str: + """One bash script for the given (index, tag, condition) guards, printing + `::<0|1>` per guard; a port of MenuModel.guardScript. The + conditions come only from the trusted menu files, never from search text.""" + if not guards: + return "" + groups = [""] * min(GUARD_GROUPS, len(guards)) + for n, (index, tag, cond) in enumerate(guards): + line = (f"if {{ {cond}\n}} >/dev/null 2>&1 float: - title = e["title"].lower() - if title.startswith(query): return 0.92 - if any(word.startswith(query) for word in title.split()): return 0.82 - if query in title: return 0.7 - if any(word.startswith(query) for word in e["aliases"].split()): return 0.62 - if query in e["aliases"]: return 0.52 - if query in e["subtitle"].lower(): return 0.4 +def run_guards(guards: list[tuple[int, str, str]], timeout: float = GUARD_TIMEOUT_S) -> dict[tuple[int, str], bool]: + script = guard_script(guards) + if not script: + return {} + try: + proc = subprocess.Popen(["bash", "-c", script], stdin=subprocess.DEVNULL, stdout=subprocess.PIPE, + stderr=subprocess.DEVNULL, text=True, start_new_session=True) + except OSError as err: + log(f"guards: cannot run bash: {err}") + return {} + try: + out, _ = proc.communicate(timeout=timeout) + except subprocess.TimeoutExpired: + with_suppress(lambda: os.killpg(proc.pid, signal.SIGKILL)) + out, _ = proc.communicate() + log("guards: timed out; unanswered rows open their parent menu") + answers = {} + for line in out.splitlines(): + m = re.fullmatch(r"(\d+):([wd]):([01])", line.strip()) + if m: + answers[(int(m.group(1)), m.group(2))] = m.group(3) == "1" + return answers + + +def with_suppress(fn) -> None: + try: + fn() + except OSError: + pass + + +# ------------------------------------------------------------ curated extras + +def summon_panel(panel: str) -> list[str]: + return ["omarchy-shell", "shell", "summon", panel] + + +# Bar panels and helpers the menu does not list as rows, each a real Omarchy +# command (see README for sources). Dropped automatically if the installed +# menu ever runs the identical command. +EXTRAS = [ + ("panel-network", "Network", "Wi-Fi and networking panel", "network-wireless", summon_panel("omarchy.network"), "wifi wi-fi internet ethernet wireless connection", "📶", BLUE), + ("panel-bluetooth", "Bluetooth", "Bluetooth devices panel", "bluetooth-active", summon_panel("omarchy.bluetooth"), "bt devices pair", "🔵", BLUE), + ("panel-audio", "Audio", "Volume and devices panel", "audio-volume-high", summon_panel("omarchy.audio"), "sound volume mic microphone speaker output input", "🔊", BLUE), + ("panel-power", "Power", "Battery and power profiles panel", "battery-full", summon_panel("omarchy.power"), "battery charge profile laptop", "🔋", BLUE), + ("panel-display", "Display", "Brightness slider and laptop display panel", "video-display", summon_panel("omarchy.monitor"), "brightness screen monitor laptop", "🖥", BLUE), + ("panel-calendar", "Calendar", "Clock and calendar panel", "x-office-calendar", summon_panel("omarchy.clock"), "clock date month", "📅", BLUE), + ("panel-world-clock", "World Clock", "Time in other zones (Elsewhen)", "preferences-system-time", summon_panel("omarchy.elsewhen"), "elsewhen timezone time zones", "🌍", BLUE), + ("panel-tailscale", "Tailscale", "Tailscale panel", "network-vpn", summon_panel("omarchy.tailscale"), "vpn tailnet", "🛡", BLUE), + ("panel-weather", "Weather", "Weather panel", "weather-few-clouds", summon_panel("omarchy.weather"), "forecast temperature", "⛅", BLUE), + ("toggle-animations", "Toggle Animations", "Toggle Hyprland animations", "preferences-desktop-effects", ["omarchy-toggle-animations"], "animations effects motion", "✨", YELLOW), + ("menu", "Omarchy Menu", "Open the native Omarchy menu", "application-menu", ["omarchy-menu", "toggle"], "menu native omarchy", "☰", BLUE), +] + + +# ------------------------------------------------------------------ catalog + +class Catalog: + """The parsed menu plus guard answers. Rebuilt when either file changes.""" + + def __init__(self, paths=menu_paths, guards=run_guards): + self.paths, self.guard_runner = paths, guards + self.lock = threading.Lock() + self.stamp: tuple = () + self.items: dict[str, dict] = {} + self.order: list[str] = [] + self.rows: list[dict] = [] + self.guard_list: list[tuple[int, str, str]] = [] + self.answers: dict[tuple[int, str], bool] | None = None + self.answers_at = 0.0 + self.guarding = False + + def _stamp(self) -> tuple: + out = [] + for p in self.paths(): + try: + st = p.stat() if p else None + out.append((str(p), st.st_mtime_ns, st.st_size) if st else None) + except OSError: + out.append(None) + return tuple(out) + + def refresh(self) -> bool: + stamp = self._stamp() + if stamp == self.stamp: + return False + default, user = self.paths() + defaults = parse_menu(read(default)) if default else [] + users = parse_user(read(user)) if user else [] + items, order = merge(defaults, users) + guards = [] + for n, item_id in enumerate(order): + entry = items[item_id] + entry["_n"] = n + if entry["when"]: + guards.append((n, "w", entry["when"])) + if entry["disabled"]: + guards.append((n, "d", entry["disabled"])) + with self.lock: + self.stamp, self.items, self.order, self.guard_list = stamp, items, order, guards + self.answers, self.answers_at = None, 0.0 + self.rows = self._build_rows() + return True + + def ensure_guards(self, block: bool = False) -> None: + with self.lock: + stale = self.answers is None or time.monotonic() - self.answers_at > GUARD_TTL_S + if not stale or self.guarding or not self.guard_list: + return + self.guarding = True + guards, stamp = list(self.guard_list), self.stamp + def work(): + try: + answers = self.guard_runner(guards) + finally: + with self.lock: + self.guarding = False + with self.lock: + if stamp == self.stamp: + self.answers, self.answers_at = answers, time.monotonic() + if block: + work() + else: + threading.Thread(target=work, name="omarchy-guards", daemon=True).start() + + # A row's guard state: True (passes), False (fails), None (unknown yet). + def _answer(self, entry: dict, tag: str): + if not entry[{"w": "when", "d": "disabled"}[tag]]: + return True if tag == "w" else False + if self.answers is None: + return None + return self.answers.get((entry["_n"], tag)) + + def _visible(self, entry: dict): + """False if this row or any ancestor is hidden by `when`; None if unknown.""" + unknown, seen = False, 0 + while entry and entry["id"] != "root" and seen < 16: + ok = self._answer(entry, "w") + if ok is False: + return False + unknown |= ok is None + entry = self.items.get(entry["parent"]) if entry["parent"] not in ("", "root") else None + seen += 1 + return None if unknown else True + + def _chain(self, entry: dict) -> list[dict] | None: + chain, seen = [], 0 + while entry["parent"] not in ("", "root"): + parent = self.items.get(entry["parent"]) + if parent is None or parent["action"] or seen > 16: + return None # orphan or under an action: not reachable in the native menu + chain.insert(0, parent); entry = parent; seen += 1 + return chain + + def _build_rows(self) -> list[dict]: + rows = [] + for item_id in self.order: + entry = self.items[item_id] + if item_id == "root" or entry["target"]: + continue + chain = self._chain(entry) + if chain is None: + continue + crumbs = [c["label"] for c in chain] + words = " ".join(crumbs + [entry["label"]] + item_id.replace("-", " ").split(".") + + entry["aliases"] + [entry["description"], KEYWORDS.get(item_id, "")]) + rows.append({"entry": entry, "title": display_title(self.items, entry), "crumbs": crumbs, + "keywords": " ".join(words.lower().split()), + "is_menu": not entry["action"]}) + return rows + + def snapshot(self): + with self.lock: + return self.rows, self.items, self.answers + + +def read(path: Path) -> str: + try: + return path.read_text(encoding="utf-8") + except (OSError, UnicodeDecodeError): + return "" + + +# ------------------------------------------------------------------ search + +def score(q: str, title: str, keywords: str, subtitle: str = "") -> float: + t = title.lower() + if t == q: return 0.95 + if t.startswith(q): return 0.92 + if any(w.startswith(q) for w in re.split(r"[\s:›/-]+", t)): return 0.82 + if q in t: return 0.7 + if keywords.startswith(q) or (" " + q) in (" " + keywords): return 0.62 + if q in keywords: return 0.52 + if q in subtitle.lower(): return 0.4 return 0.0 -def search(query: str) -> list[dict]: - q = query.strip().lower() - if not q: return [] - ranked = sorted(((score(q, e), e) for e in ENTRIES), key=lambda pair: pair[0], reverse=True) - out: list[dict] = [] - for s, e in ranked: - if s == 0.0 or len(out) >= MAX_RESULTS: break - out.append({"id": "omarchy:" + e["title"].lower().replace(" ", "-"), "title": e["title"], "subtitle": e["subtitle"], "icon": e["icon"], "icons": e["icons"], "fallbackIcon": e["fallbackIcon"], "fallbackColor": e["fallbackColor"], "keywords": e["aliases"], "score": s, "provider": "omarchy", "actions": e["actions"]}) + +def menu_result(catalog: Catalog, row: dict, s: float) -> dict | None: + entry = row["entry"] + visible = catalog._visible(entry) + if visible is False: + return None + disabled = catalog._answer(entry, "d") + crumb = " › ".join(["Omarchy", *row["crumbs"]]) + route, note = entry["id"], "" + if visible is None or disabled is None: + # Guard not answered yet: let the native menu apply it. + route = entry["parent"] if entry["parent"] not in ("", "root") else "root" + note = " · opens in the Omarchy menu" + subtitle = crumb + ((" › " + entry["label"]) if row["is_menu"] else "") + if row["is_menu"]: + subtitle = "Open menu: " + subtitle + actions: list[dict] = [] + if disabled is True: + subtitle += " · ✓ installed" + else: + action = {"type": "launch", "title": "Open in Omarchy menu" if row["is_menu"] or note else "Run", + "argv": ["omarchy-menu", "summon", route]} + tone = "" if note or row["is_menu"] else confirm_for(entry["id"]) + if tone: + action.update({"danger": "confirm", "tone": tone, "confirmation": { + "title": f"{row['title']}?", "detail": CONFIRM_DETAIL.format(id=entry["id"])}}) + actions.append(action) + if entry["action"]: + actions.append({"type": "copy", "title": "Copy command", "text": entry["action"]}) + return {"id": "omarchy:menu:" + entry["id"], "title": row["title"], "subtitle": subtitle + note, + "icon": "omarchy", "icons": ["application-menu"], "fallbackIcon": "◆", + "fallbackColor": RED if confirm_for(entry["id"]) == "danger" else PURPLE if row["is_menu"] else BLUE, + "keywords": row["keywords"], "score": round(s * (0.85 if row["is_menu"] else 1.0), 3), + "provider": "omarchy", "actions": actions} + + +def extras(menu_actions: set[str]) -> list[dict]: + out = [] + for key, title, subtitle, icon, argv, words, glyph, color in EXTRAS: + if " ".join(argv) in menu_actions: + continue + out.append({"id": "omarchy:" + key, "title": title, "subtitle": subtitle, "icon": icon, + "fallbackIcon": glyph, "fallbackColor": color, "keywords": (title + " " + words).lower(), + "provider": "omarchy", + "actions": [{"type": "launch", "title": title, "argv": argv}, + {"type": "copy", "title": "Copy command", "text": " ".join(argv)}]}) return out + +def search(catalog: Catalog, query: str) -> list[dict]: + q = " ".join(query.strip().lower().split()) + if not q: + return [] + rows, items, _ = catalog.snapshot() + menu_actions = {e["action"] for e in items.values() if e["action"]} + scored = [] + for n, row in enumerate(rows): + s = score(q, row["title"], row["keywords"]) + if s > 0: + scored.append((s * (0.85 if row["is_menu"] else 1.0), n, row)) + scored.sort(key=lambda t: (-t[0], t[1])) + out = [] + for s, _, row in scored: + if len(out) >= MAX_RESULTS: + break + result = menu_result(catalog, row, s / (0.85 if row["is_menu"] else 1.0)) + if result is not None: + out.append(result) + for x in extras(menu_actions): + s = score(q, x["title"], x["keywords"], x["subtitle"]) + if s > 0: + out.append(dict(x, score=s)) + out.sort(key=lambda r: -r["score"]) + return out[:MAX_RESULTS] + + +def log(*args) -> None: + print("omarchy:", *args, file=sys.stderr, flush=True) + + def main() -> None: + catalog = Catalog() + catalog.refresh() + catalog.ensure_guards() + lock = threading.Lock() + def send(msg): + with lock: + print(json.dumps(msg), flush=True) for line in sys.stdin: - try: msg = json.loads(line) - except json.JSONDecodeError: continue - if msg.get("op") == "search": print(json.dumps({"id": msg.get("id", ""), "results": search(str(msg.get("query", "")))}), flush=True) - elif msg.get("op") == "cancel": print(json.dumps({"id": msg.get("target", ""), "cancelled": True}), flush=True) - elif msg.get("op") == "execute": print(json.dumps({"id": msg.get("id", ""), "executed": False}), flush=True) + try: + msg = json.loads(line) + except json.JSONDecodeError: + continue + if not isinstance(msg, dict): + continue + op = msg.get("op") + if op == "search": + catalog.refresh() + catalog.ensure_guards() + send({"id": msg.get("id", ""), "results": search(catalog, str(msg.get("query", "")))}) + elif op == "warm": + catalog.refresh() + catalog.ensure_guards() + elif op == "cancel": + send({"id": msg.get("target", ""), "cancelled": True}) + elif op == "execute": + send({"id": msg.get("id", ""), "executed": False, "error": "omarchy actions are launched by the host"}) + -if __name__ == "__main__": main() +if __name__ == "__main__": + main() diff --git a/tests/fixtures/omarchy/default/omarchy/omarchy-menu.jsonc b/tests/fixtures/omarchy/default/omarchy/omarchy-menu.jsonc new file mode 100644 index 0000000..a827a52 --- /dev/null +++ b/tests/fixtures/omarchy/default/omarchy/omarchy-menu.jsonc @@ -0,0 +1,27 @@ +{ + // Trimmed copy of the shape of Omarchy's default/omarchy/omarchy-menu.jsonc. + "system": {"icon":"x","label":"System","aliases":["power-menu"]}, + "system.lock": {"label":"Lock","action":"omarchy-system-lock"}, + "system.shutdown": {"label":"Shutdown","action":"omarchy-system shutdown"}, + "system.hibernate": {"label":"Hibernate","when":"omarchy-hibernation-available","action":"systemctl hibernate"}, + "trigger": {"label":"Trigger"}, + "trigger.toggle": {"label":"Toggle"}, + "trigger.toggle.nightlight-on": {"label":"Nightlight On","when":"! omarchy-toggle-nightlight --status","action":"omarchy-toggle-nightlight"}, // inline comment + "trigger.toggle.nightlight-off": {"label":"Nightlight Off","when":"omarchy-toggle-nightlight --status","action":"omarchy-toggle-nightlight"}, + "install": {"label":"Install"}, + "install.package": {"label":"Package","action":"xdg-terminal-exec --app-id=org.omarchy.terminal omarchy-pkg-install"}, + "install.editor": {"label":"Editor"}, + "install.editor.zed": {"label":"Zed","disabled":"omarchy-pkg-present zed","action":"omarchy-install-zed"}, + "remove": {"label":"Remove"}, + "remove.theme": {"label":"Theme","action":"omarchy-theme-remove"}, + "setup": {"label":"Setup"}, + "setup.default": {"label":"Defaults"}, + "setup.default.browser": {"label":"Browser"}, + "setup.default.browser.firefox": {"label":"Firefox","action":"omarchy-default-browser firefox"}, + "learn": {"label":"Learn"}, + "learn.omarchy": {"label":"Omarchy","action":"omarchy-launch-webapp 'https://omarchy.org/manual/'"}, + "orphan.child": {"label":"Orphan","action":"true"}, + "apps": {"label":"Apps","action":"omarchy-shell omarchy.dock apps"}, + "apps.inner": {"label":"Under an action","action":"true"}, + "url": {"label":"has // inside a string","action":"omarchy-launch-webapp 'https://a.example/x'"}, +} diff --git a/tests/test_omarchy.py b/tests/test_omarchy.py new file mode 100644 index 0000000..918cbc8 --- /dev/null +++ b/tests/test_omarchy.py @@ -0,0 +1,196 @@ +"""omarchy provider: parity with the installed Omarchy menu.""" +import importlib.util +import json +import os +from pathlib import Path +import subprocess +import sys +import tempfile +import unittest + +ROOT = Path(__file__).resolve().parents[1] +SPEC = importlib.util.spec_from_file_location('omarchy_provider', ROOT / 'providers/omarchy/provider.py') +om = importlib.util.module_from_spec(SPEC) +sys.modules['omarchy_provider'] = om +SPEC.loader.exec_module(om) +FIXTURE = ROOT / 'tests/fixtures/omarchy/default/omarchy/omarchy-menu.jsonc' +_TMP = tempfile.TemporaryDirectory() +REAL = Path('/workspace/omarchy-src/default/omarchy/omarchy-menu.jsonc') + + +def catalog(user=None, answers=None): + user_path = Path(_TMP.name) / f'user-{len(os.listdir(_TMP.name))}.jsonc' + if user is not None: + user_path.write_text(user, encoding='utf-8') + calls = [] + def guards(list_): + calls.append(list_) + return answers(list_) if answers else {} + c = om.Catalog(paths=lambda: (FIXTURE, user_path), guards=guards) + c._calls = calls + c.refresh() + return c + + +def by_id(results): + return {r['id']: r for r in results} + + +def answer(**wanted): + """Guard answers keyed by item id: {'system.hibernate': {'w': False}}.""" + def run(guards): + out = {} + for n, tag, cond in guards: + out[(n, tag)] = wanted.get(cond, {}).get(tag, True if tag == 'w' else False) + return out + return run + + +class ParseTests(unittest.TestCase): + def test_jsonc_comments_trailing_commas_and_slashes_in_strings(self): + items = om.parse_menu(FIXTURE.read_text()) + ids = [i['id'] for i in items] + self.assertIn('url', ids) + url = next(i for i in items if i['id'] == 'url') + self.assertIn('https://a.example/x', url['action']) + self.assertEqual(next(i for i in items if i['id'] == 'system')['aliases'], ['power-menu']) + self.assertEqual(next(i for i in items if i['id'] == 'trigger.toggle.nightlight-on')['parent'], 'trigger.toggle') + + def test_user_overlay_overrides_declared_fields_only_and_appends(self): + c = catalog(user='{"system.lock": {"label": "Lock Screen"}, "my.thing": {"label": "Mine", "action": "true"}, "my": {"label": "My"}}') + self.assertEqual(c.items['system.lock']['label'], 'Lock Screen') + self.assertEqual(c.items['system.lock']['action'], 'omarchy-system-lock') + self.assertEqual(c.order[-2:], ['my.thing', 'my']) + + def test_unreachable_entries_are_skipped(self): + ids = {r['entry']['id'] for r in catalog().rows} + self.assertNotIn('orphan.child', ids) + self.assertNotIn('apps.inner', ids) + self.assertIn('system', ids) + + @unittest.skipUnless(REAL.is_file(), 'Omarchy source checkout not present') + def test_real_menu_parses_and_every_row_summons_its_own_id(self): + items = om.parse_menu(REAL.read_text()) + self.assertGreater(len(items), 300) + c = om.Catalog(paths=lambda: (REAL, None), guards=lambda g: {}) + c.refresh() + self.assertGreater(len(c.rows), 300) + for r in om.search(c, 'shutdown'): + if r['id'] == 'omarchy:menu:system.shutdown': + self.assertEqual(r['actions'][0]['argv'], ['omarchy-menu', 'summon', 'system.shutdown']) + self.assertEqual(r['actions'][0]['danger'], 'confirm') + break + else: + self.fail('shutdown not found') + + +class SearchTests(unittest.TestCase): + def test_leaf_runs_through_omarchy_menu_summon_argv(self): + c = catalog() + r = by_id(om.search(c, 'lock'))['omarchy:menu:system.lock'] + self.assertEqual(r['actions'][0]['argv'], ['omarchy-menu', 'summon', 'system.lock']) + self.assertNotIn('danger', r['actions'][0]) + self.assertEqual(r['subtitle'], 'Omarchy › System') + self.assertEqual(r['actions'][1], {'type': 'copy', 'title': 'Copy command', 'text': 'omarchy-system-lock'}) + + def test_destructive_and_system_state_rows_confirm(self): + c = catalog() + for q, rid in [('shutdown', 'system.shutdown'), ('theme', 'remove.theme'), ('zed', 'install.editor.zed')]: + c.answers, c.answers_at = {}, 1e18 # guards answered (nothing failing) + c.answers = {(c.items[rid]['_n'], 'd'): False} if c.items[rid]['disabled'] else {} + r = by_id(om.search(c, q))['omarchy:menu:' + rid] + self.assertEqual(r['actions'][0].get('danger'), 'confirm', rid) + self.assertEqual(om.confirm_for('install.package'), '') + self.assertEqual(om.confirm_for('system.lock'), '') + self.assertEqual(om.confirm_for('setup.reset'), 'danger') + self.assertEqual(om.confirm_for('update.channel.edge'), 'warning') + self.assertEqual(om.confirm_for('system.suspend-ish'), '') + + def test_contextual_titles(self): + c = catalog() + titles = {r['id']: r['title'] for r in om.search(c, 'firefox') + om.search(c, 'package') + om.search(c, 'theme')} + self.assertEqual(titles['omarchy:menu:setup.default.browser.firefox'], 'Default Browser: Firefox') + self.assertEqual(titles['omarchy:menu:install.package'], 'Install Package') + self.assertEqual(titles['omarchy:menu:remove.theme'], 'Remove Theme') + + def test_submenus_open_the_native_menu(self): + r = by_id(om.search(catalog(), 'editor'))['omarchy:menu:install.editor'] + self.assertTrue(r['subtitle'].startswith('Open menu: ')) + self.assertEqual(r['actions'][0]['argv'], ['omarchy-menu', 'summon', 'install.editor']) + + def test_aliases_and_extra_keywords_match(self): + c = catalog() + self.assertIn('omarchy:menu:system', by_id(om.search(c, 'power-menu'))) + self.assertIn('omarchy:menu:system.shutdown', by_id(om.search(c, 'power off'))) + + +class GuardTests(unittest.TestCase): + def test_unknown_guard_opens_parent_menu_instead_of_running(self): + c = catalog() # guards not evaluated yet + r = by_id(om.search(c, 'hibernate'))['omarchy:menu:system.hibernate'] + self.assertEqual(r['actions'][0]['argv'], ['omarchy-menu', 'summon', 'system']) + self.assertNotIn('danger', r['actions'][0]) + self.assertIn('opens in the Omarchy menu', r['subtitle']) + + def test_when_hides_and_toggle_pairs_show_one_side(self): + c = catalog(answers=answer(**{'omarchy-toggle-nightlight --status': {'w': False}, + 'omarchy-hibernation-available': {'w': False}})) + c.ensure_guards(block=True) + ids = by_id(om.search(c, 'nightlight')) + self.assertIn('omarchy:menu:trigger.toggle.nightlight-on', ids) + self.assertNotIn('omarchy:menu:trigger.toggle.nightlight-off', ids) + self.assertEqual(ids['omarchy:menu:trigger.toggle.nightlight-on']['actions'][0]['argv'][-1], 'trigger.toggle.nightlight-on') + self.assertNotIn('omarchy:menu:system.hibernate', by_id(om.search(c, 'hibernate'))) + self.assertEqual(len(c._calls), 1) + c.ensure_guards(block=True) + self.assertEqual(len(c._calls), 1, 'fresh answers are reused') + + def test_disabled_rows_stay_listed_without_a_run_action(self): + c = catalog(answers=answer(**{'omarchy-pkg-present zed': {'d': True}})) + c.ensure_guards(block=True) + r = by_id(om.search(c, 'zed'))['omarchy:menu:install.editor.zed'] + self.assertIn('installed', r['subtitle']) + self.assertEqual([a['type'] for a in r['actions']], ['copy']) + + def test_guard_script_runs_conditions_in_bash_and_parses_answers(self): + guards = [(0, 'w', 'true'), (1, 'w', 'false'), (2, 'd', '[[ 1 == 1 ]] # trailing comment')] + self.assertEqual(om.run_guards(guards, timeout=10), {(0, 'w'): True, (1, 'w'): False, (2, 'd'): True}) + + def test_guard_that_exits_leaves_row_unanswered(self): + # Same as the native batch: the row stays unknown and opens its parent menu. + out = om.run_guards([(0, 'w', 'exit 0')], timeout=10) + self.assertEqual(out, {}) + + def test_guard_timeout_returns_partial(self): + out = om.run_guards([(0, 'w', 'true'), (1, 'w', 'sleep 5')], timeout=1.0) + self.assertEqual(out.get((0, 'w')), True) + self.assertNotIn((1, 'w'), out) + + +class ExtrasTests(unittest.TestCase): + def test_panels_present_and_deduped_against_menu_commands(self): + c = catalog() + r = by_id(om.search(c, 'bluetooth')) + self.assertEqual(r['omarchy:panel-bluetooth']['actions'][0]['argv'], ['omarchy-shell', 'shell', 'summon', 'omarchy.bluetooth']) + self.assertEqual(om.extras({'omarchy-shell shell summon omarchy.bluetooth'})[0]['id'] != 'omarchy:panel-bluetooth', True) + + def test_without_menu_file_only_extras(self): + c = om.Catalog(paths=lambda: (None, Path('/nonexistent')), guards=lambda g: {}) + c.refresh() + self.assertEqual({r['provider'] for r in om.search(c, 'network')}, {'omarchy'}) + self.assertTrue(all(not r['id'].startswith('omarchy:menu:') for r in om.search(c, 'a'))) + + def test_protocol_end_to_end(self): + env = dict(os.environ, OMARCHY_PATH=str(ROOT / 'tests/fixtures/omarchy'), HOME=tempfile.mkdtemp()) + lines = [json.dumps({'id': 'q1', 'op': 'search', 'query': 'shutdown'}), json.dumps({'id': '', 'op': 'warm'}), + json.dumps({'id': '', 'op': 'cancel', 'target': 'q0'})] + proc = subprocess.run([sys.executable, str(ROOT / 'providers/omarchy/provider.py')], input='\n'.join(lines) + '\n', + text=True, capture_output=True, timeout=20, env=env) + replies = [json.loads(l) for l in proc.stdout.splitlines()] + self.assertEqual(replies[0]['id'], 'q1') + self.assertEqual(replies[0]['results'][0]['id'], 'omarchy:menu:system.shutdown') + self.assertEqual(replies[1], {'id': 'q0', 'cancelled': True}) + + +if __name__ == '__main__': + unittest.main() From 6d5a1edeb92c829f97000bae276e67ef55ee98fb Mon Sep 17 00:00:00 2001 From: Grok Bot Date: Thu, 29 Oct 2026 16:27:52 -0400 Subject: [PATCH 10/19] system: key-bound Omarchy controls with sources; power/update moved to menu parity --- providers/system/README.md | 63 +++++++++-------- providers/system/manifest.json | 2 +- providers/system/provider.py | 124 ++++++++++++++++++++++----------- tests/test_system.py | 51 ++++++++++++++ 4 files changed, 170 insertions(+), 70 deletions(-) create mode 100644 tests/test_system.py diff --git a/providers/system/README.md b/providers/system/README.md index d976a05..8cad6f2 100644 --- a/providers/system/README.md +++ b/providers/system/README.md @@ -1,35 +1,40 @@ # system provider -System power, session, and update actions — the native Omarchy menu's -commands, surfaced as palette results. +Everyday controls that Omarchy binds to keys but does not list in its menu: +volume, media, brightness, keyboard backlight, Bluetooth power, notifications, +window helpers and status notifications. Power, session, update and package +actions moved to the `omarchy` provider, which runs them through +`omarchy-menu summon` with confirmations. + +Every row is a fixed argv (no shell, no user input) of a command that exists in +Omarchy's `bin/`. The provider runs nothing at import or search time. ## Entries -| Entry | Command | Confirmation | +| Entry | argv | Source (Omarchy) | |---|---|---| -| Lock | `omarchy system lock` | no | -| Omarchy Menu | `omarchy menu toggle` | no | -| Suspend | `systemctl suspend` | yes | -| Update Omarchy | `foot -- omarchy update` | yes | -| Log Out | `omarchy system logout` | yes | -| Reboot | `omarchy system reboot` | yes | -| Shut Down | `omarchy system shutdown` | yes | - -The `omarchy system *` commands close application windows before acting, -matching the native menu's logout/reboot/shutdown behavior. - -## Safety model - -- Update runs Omarchy's official orchestrated update (`omarchy update`) in a - visible foot terminal — snapshot, packages, migrations, shell restart. - Never a raw pacman/AUR command. -- Lock is deliberately ungated: locking must stay one keystroke away. -- Everything disruptive (suspend, logout, reboot, shutdown, update) carries - `danger: "confirm"` and requires a second Enter on the confirmation sheet. -- Every entry also offers "Copy command" so users can inspect what runs. - -## Deferred - -- Hibernate (needs an `omarchy-hibernation-available` runtime check). -- Config editing entries (Hyprland, shell.json) — needs an editor choice. -- Theme/wallpaper pickers. +| Volume Up / Down | `omarchy-audio-output-volume raise` / `lower` | `default/hypr/bindings/media.lua` (`{audio=…}` → helpers.lua) | +| Mute Audio | `omarchy-audio-output-volume mute-toggle` | `media.lua` (XF86AudioMute) | +| Mute Microphone | `omarchy-audio-input-mute` | `media.lua` (XF86AudioMicMute) | +| Switch Audio Output | `omarchy-audio-output-switch` | `media.lua` (Shift+XF86AudioMute) | +| Switch Media Source | `omarchy-audio-source-switch` | `media.lua` (Shift+XF86AudioPlay) | +| Play / Pause, Next Track, Previous Track | `omarchy-shell media playPause` / `next` / `previous` | `media.lua` (`{ipc="media.…"}` → helpers.lua) | +| Brightness Up / Down | `omarchy-brightness-display +5%` / `5%-` | `media.lua` (`{brightness=…}` → helpers.lua) | +| Brightness Maximum / Minimum | `omarchy-brightness-display 100%` / `1%` | `bin/omarchy-brightness-display` | +| Keyboard Backlight Up / Down / Cycle | `omarchy-brightness-keyboard up` / `down` / `cycle` | `media.lua` (XF86Kbd*) | +| Bluetooth On / Off | `omarchy-bluetooth-power on` / `off` | `bin/omarchy-bluetooth-power` | +| Dismiss Notification / Dismiss All / Open Last / History | `omarchy-shell notifications dismissOne` / `dismissAll` / `invokeLast` / `showHistory` | `default/hypr/bindings/utilities.lua` (`SUPER+,` family) | +| Toggle Window Transparency | `omarchy-hyprland-window-transparency-toggle` | `utilities.lua` (`SUPER+BACKSPACE`) | +| Toggle Full Screen Desktop | `omarchy-toggle-fullscreen-desktop` | `utilities.lua` (`bind_toggle … "fullscreen-desktop"`) | +| Pop Window Out | `omarchy-hyprland-window-pop` | `default/hypr/bindings/tiling.lua` (`SUPER+O`) | +| Toggle Tiled Full Screen | `omarchy-hyprland-window-tiled-fullscreen-toggle` | `tiling.lua` (`SUPER+CTRL+F`) | +| Minimize Window | `omarchy-hyprland-window-minimize` | `tiling.lua` (`SUPER+M`) | +| Close All Windows (**confirm**, danger) | `omarchy-hyprland-window-close-all` | `tiling.lua` (`CTRL+ALT+DELETE`) | +| Show Time | `omarchy-notification-time` | `utilities.lua` (`SUPER+CTRL+ALT+T`) | +| Show Battery Remaining | `omarchy-notification-battery` | `utilities.lua` (`SUPER+CTRL+ALT+B`) | +| Pick Agent | `omarchy-agent --pick` | `utilities.lua` (`SUPER+SHIFT+CTRL+A`) | + +Window actions act on the window that had focus before the palette opened +(the palette is a layer surface, not a window). + +Every entry also offers "Copy command". diff --git a/providers/system/manifest.json b/providers/system/manifest.json index 239e2f1..bf33e79 100644 --- a/providers/system/manifest.json +++ b/providers/system/manifest.json @@ -1,6 +1,6 @@ { "name": "system", - "description": "System power, session, and update actions via official omarchy commands", + "description": "Key-bound Omarchy controls: volume, media, brightness, notifications, window helpers", "exec": ["python3", "providers/system/provider.py"], "triggers": [], "capabilities": ["search"] diff --git a/providers/system/provider.py b/providers/system/provider.py index 46575f1..70198ec 100644 --- a/providers/system/provider.py +++ b/providers/system/provider.py @@ -1,72 +1,116 @@ #!/usr/bin/env python3 -"""System power, session, update, and package commands. +"""System controls Omarchy binds to keys but does not list in its menu. -Every command mirrors the native Omarchy menu. Icons use a primary theme name, -an ordered candidate list, and a glyph fallback so no theme leaves a row bare. -Hibernate is gated identically to upstream: it is listed only when -omarchy-hibernation-available succeeds on this machine. +Volume, microphone, media, brightness, keyboard backlight, Bluetooth power, +notifications, window helpers and quick status. Every argv is the exact +command an Omarchy keybinding runs (default/hypr/bindings/*.lua and the +dispatch table in default/hypr/helpers.lua); README.md lists the source of +each. Power, session, update and package rows live in the omarchy provider, +which mirrors the native menu, so they are not duplicated here. """ from __future__ import annotations import json -import subprocess import sys + MAX_RESULTS = 15 BLUE = "#89b4fa" GREEN = "#a6e3a1" YELLOW = "#f9e2af" RED = "#f38ba8" +PURPLE = "#cba6f7" + + +def ipc(target: str, method: str) -> list[str]: + return ["omarchy-shell", target, method] + + +def entry(key, title, subtitle, icon, argv, words, glyph, color=BLUE, confirm=None): + action = {"type": "launch", "title": title, "argv": argv} + if confirm: + tone, ctitle, detail = confirm + action.update({"danger": "confirm", "tone": tone, "confirmation": {"title": ctitle, "detail": detail}}) + return {"key": key, "title": title, "subtitle": subtitle, "icon": icon, "fallbackIcon": glyph, + "fallbackColor": color, "keywords": (title + " " + words).lower(), + "actions": [action, {"type": "copy", "title": "Copy command", "text": " ".join(argv)}]} + -def entry(title: str, subtitle: str, icon: str, argv: list[str], aliases: str, icons: list[str] | None = None, fallback_icon: str = "", fallback_color: str = BLUE, danger: str = "none", tone: str = "", confirm_title: str = "", confirm_detail: str = "") -> dict: - action: dict = {"type": "launch", "title": title, "argv": argv} - if danger == "confirm": - action["danger"] = "confirm"; action["tone"] = tone or "warning"; action["confirmation"] = {"title": confirm_title, "detail": confirm_detail} - return {"title": title, "subtitle": subtitle, "icon": icon, "icons": icons or [], "fallbackIcon": fallback_icon, "fallbackColor": fallback_color, "aliases": (title + " " + aliases).lower(), "actions": [action, {"type": "copy", "title": "Copy command", "text": " ".join(argv)}]} - -def hibernation_available() -> bool: - try: - return subprocess.run(["omarchy-hibernation-available"], check=False, capture_output=True, timeout=2).returncode == 0 - except (OSError, subprocess.TimeoutExpired): - return False - -ENTRIES: list[dict] = [ - entry("Lock", "Lock the screen now", "system-lock-screen", ["omarchy", "system", "lock"], "lock screen screensaver", ["lock", "system-lock"], "\U0001f512"), - entry("Omarchy Menu", "Open the native Omarchy menu", "application-menu", ["omarchy", "menu", "toggle"], "menu native omarchy", ["open-menu-symbolic", "start-here"], "\u2630"), - entry("Suspend", "Suspend the machine", "system-suspend", ["systemctl", "suspend"], "suspend sleep rest", ["weather-clear-night", "night-light", "system-suspend-hibernate"], "\U0001f319", YELLOW, "confirm", "warning", "Suspend now?", "Suspends the machine immediately; waking takes a moment."), - *([entry("Hibernate", "Save memory to disk and power off", "system-suspend-hibernate", ["systemctl", "hibernate"], "hibernate deep sleep power", ["weather-clear-night", "system-suspend"], "\u2744", YELLOW, "confirm", "warning", "Hibernate now?", "Saves the session to disk and powers off; resume depends on your hibernation setup.")] if hibernation_available() else []), - entry("Update Omarchy", "Snapshot, packages, migrations -- the official update", "software-update-available", ["foot", "--", "omarchy", "update"], "update upgrade refresh", ["system-software-update", "software-update-urgent", "view-refresh"], "\u2b06", GREEN, "confirm", "positive", "Update Omarchy?", "Runs 'omarchy update' in a terminal: snapshot, packages, migrations, and a shell restart. Uses Omarchy's own update path -- no raw pacman."), - entry("Install Package", "Install from the official repos -- Omarchy's installer", "system-software-install", ["xdg-terminal-exec", "--app-id=org.omarchy.terminal", "omarchy-pkg-install"], "install package pacman repo repos", ["package-install", "package"], "\U0001f4e6", GREEN), - entry("Install from AUR", "Install from the Arch User Repository", "system-software-install", ["xdg-terminal-exec", "--app-id=org.omarchy.terminal", "omarchy-pkg-aur-install"], "aur install yay paru user repository", ["package-install", "package"], "\U0001f4e6", YELLOW), - entry("Remove Package", "Uninstall a package -- Omarchy's remover", "package-remove", ["xdg-terminal-exec", "--app-id=org.omarchy.terminal", "omarchy-pkg-remove"], "remove uninstall package delete pkg", ["list-remove", "edit-delete"], "\U0001f5d1", RED), - entry("Log Out", "End the session after closing windows", "system-log-out", ["omarchy", "system", "logout"], "logout log out exit session", ["system-logout", "application-exit"], "\U0001f6aa", YELLOW, "confirm", "warning", "Log out?", "Closes all application windows and ends your session."), - entry("Reboot", "Restart the machine", "system-reboot", ["omarchy", "system", "reboot"], "reboot restart", ["system-restart", "view-refresh"], "\U0001f504", YELLOW, "confirm", "warning", "Reboot?", "Closes your session and reboots the machine."), - entry("Shut Down", "Power off the machine", "system-shutdown", ["omarchy", "system", "shutdown"], "shutdown power off halt", ["system-power", "system-shutdown-symbolic"], "\u23fb", RED, "confirm", "danger", "Shut down?", "Closes your session and powers off the machine."), +ENTRIES = [ + # Audio (bindings/media.lua; helpers.lua maps {audio=...} to omarchy-audio-output-volume) + entry("volume-up", "Volume Up", "Raise output volume", "audio-volume-high", ["omarchy-audio-output-volume", "raise"], "louder sound audio increase", "🔊"), + entry("volume-down", "Volume Down", "Lower output volume", "audio-volume-low", ["omarchy-audio-output-volume", "lower"], "quieter sound audio decrease", "🔉"), + entry("mute", "Mute Audio", "Toggle output mute", "audio-volume-muted", ["omarchy-audio-output-volume", "mute-toggle"], "mute unmute sound audio speaker silence", "🔇"), + entry("mic-mute", "Mute Microphone", "Toggle microphone mute", "microphone-sensitivity-muted", ["omarchy-audio-input-mute"], "mic microphone mute unmute input", "🎙"), + entry("audio-output", "Switch Audio Output", "Cycle to the next output device", "audio-speakers", ["omarchy-audio-output-switch"], "speaker headphones output device sink", "🎧"), + entry("media-source", "Switch Media Source", "Cycle which player the media keys control", "multimedia-player", ["omarchy-audio-source-switch"], "player media source", "🎵"), + # Media (bindings/media.lua {ipc = "media.*"}) + entry("play-pause", "Play / Pause", "Toggle playback", "media-playback-start", ipc("media", "playPause"), "play pause music media resume", "⏯", GREEN), + entry("next-track", "Next Track", "Skip to the next track", "media-skip-forward", ipc("media", "next"), "next skip music media song", "⏭", GREEN), + entry("previous-track", "Previous Track", "Go to the previous track", "media-skip-backward", ipc("media", "previous"), "previous back music media song", "⏮", GREEN), + # Brightness (bindings/media.lua; helpers.lua maps {brightness=...} to +5% / 5%-) + entry("brightness-up", "Brightness Up", "Raise display brightness 5%", "display-brightness-high", ["omarchy-brightness-display", "+5%"], "brighter screen display backlight", "🔆", YELLOW), + entry("brightness-down", "Brightness Down", "Lower display brightness 5%", "display-brightness-low", ["omarchy-brightness-display", "5%-"], "dimmer dim screen display backlight", "🔅", YELLOW), + entry("brightness-max", "Brightness Maximum", "Set display brightness to 100%", "display-brightness-high", ["omarchy-brightness-display", "100%"], "full brightest screen display", "☀", YELLOW), + entry("brightness-min", "Brightness Minimum", "Set display brightness to 1%", "display-brightness-low", ["omarchy-brightness-display", "1%"], "lowest darkest screen display", "🌑", YELLOW), + entry("kbd-up", "Keyboard Backlight Up", "Raise keyboard backlight", "keyboard-brightness", ["omarchy-brightness-keyboard", "up"], "keyboard backlight brighter kbd", "⌨", YELLOW), + entry("kbd-down", "Keyboard Backlight Down", "Lower keyboard backlight", "keyboard-brightness", ["omarchy-brightness-keyboard", "down"], "keyboard backlight dimmer kbd", "⌨", YELLOW), + entry("kbd-cycle", "Keyboard Backlight Cycle", "Cycle keyboard backlight levels", "keyboard-brightness", ["omarchy-brightness-keyboard", "cycle"], "keyboard backlight toggle kbd", "⌨", YELLOW), + # Bluetooth power (bin/omarchy-bluetooth-power) + entry("bt-on", "Bluetooth On", "Turn Bluetooth on (remembered across reboots)", "bluetooth-active", ["omarchy-bluetooth-power", "on"], "bluetooth bt enable power radio", "🔵"), + entry("bt-off", "Bluetooth Off", "Turn Bluetooth off (remembered across reboots)", "bluetooth-disabled", ["omarchy-bluetooth-power", "off"], "bluetooth bt disable power radio airplane", "⚪"), + # Notifications (bindings/utilities.lua {ipc = "notifications.*"}) + entry("notif-dismiss", "Dismiss Notification", "Dismiss the latest notification", "notification-disabled", ipc("notifications", "dismissOne"), "notifications clear close dismiss last", "🔕", PURPLE), + entry("notif-dismiss-all", "Dismiss All Notifications", "Clear every notification", "notification-disabled", ipc("notifications", "dismissAll"), "notifications clear all dismiss", "🔕", PURPLE), + entry("notif-invoke", "Open Last Notification", "Invoke the latest notification's action", "preferences-system-notifications", ipc("notifications", "invokeLast"), "notifications invoke open last click", "🔔", PURPLE), + entry("notif-history", "Notification History", "Show notification history", "preferences-system-notifications", ipc("notifications", "showHistory"), "notifications history log past", "📜", PURPLE), + # Windows (bindings/utilities.lua, bindings/tiling.lua) - act on the focused window + entry("win-transparency", "Toggle Window Transparency", "Make the focused window opaque or transparent", "preferences-desktop-effects", ["omarchy-hyprland-window-transparency-toggle"], "opacity transparent opaque window", "◐"), + entry("win-pop", "Pop Window Out", "Float and pin the focused window", "window-pin", ["omarchy-hyprland-window-pop"], "float pin pop picture window", "📌"), + entry("win-tiled-fullscreen", "Toggle Tiled Full Screen", "Full screen within the tiling layout", "view-fullscreen", ["omarchy-hyprland-window-tiled-fullscreen-toggle"], "fullscreen maximize tiled window", "⛶"), + entry("win-minimize", "Minimize Window", "Minimize the focused window to the dock", "window-minimize", ["omarchy-hyprland-window-minimize"], "minimize hide dock window", "▁"), + entry("fullscreen-desktop", "Toggle Full Screen Desktop", "Hide the bar and gaps for a full-screen desktop", "view-fullscreen", ["omarchy-toggle-fullscreen-desktop"], "fullscreen desktop zen focus bar", "⛶", YELLOW), + entry("win-close-all", "Close All Windows", "Close every open window", "window-close", ["omarchy-hyprland-window-close-all"], "close quit kill all windows", "✖", RED, + ("danger", "Close all windows?", "Closes every open application window (unsaved work may prompt or be lost).")), + # Quick status (bindings/utilities.lua) + entry("show-time", "Show Time", "Show the time as a notification", "preferences-system-time", ["omarchy-notification-time"], "time clock date now", "🕒", GREEN), + entry("show-battery", "Show Battery Remaining", "Show battery level and time remaining", "battery-good", ["omarchy-notification-battery"], "battery charge power remaining", "🔋", GREEN), + entry("agent-pick", "Pick Agent", "Launch an AI coding agent", "utilities-terminal", ["omarchy-agent", "--pick"], "ai agent coding assistant claude codex", "🤖", GREEN), ] + def score(query: str, item: dict) -> float: title = item["title"].lower() + if title == query: return 0.95 if title.startswith(query): return 0.92 - if any(word.startswith(query) for word in title.split()): return 0.82 + if any(word.startswith(query) for word in title.replace("/", " ").split()): return 0.82 if query in title: return 0.7 - if any(word.startswith(query) for word in item["aliases"].split()): return 0.6 - if query in item["aliases"]: return 0.5 + if any(word.startswith(query) for word in item["keywords"].split()): return 0.6 + if query in item["keywords"]: return 0.5 if query in item["subtitle"].lower(): return 0.4 return 0.0 + def search(query: str) -> list[dict]: - q = query.strip().lower() + q = " ".join(query.strip().lower().split()) if not q: return [] - ranked = sorted(((score(q, e), e) for e in ENTRIES), key=lambda pair: pair[0], reverse=True) + ranked = sorted(((score(q, e), n, e) for n, e in enumerate(ENTRIES)), key=lambda t: (-t[0], t[1])) out: list[dict] = [] - for s, e in ranked: + for s, _, e in ranked: if s == 0.0 or len(out) >= MAX_RESULTS: break - out.append({"id": "system:" + e["title"].lower().replace(" ", "-"), "title": e["title"], "subtitle": e["subtitle"], "icon": e["icon"], "icons": e["icons"], "fallbackIcon": e["fallbackIcon"], "fallbackColor": e["fallbackColor"], "keywords": e["aliases"], "score": s, "provider": "system", "actions": e["actions"]}) + out.append({"id": "system:" + e["key"], "title": e["title"], "subtitle": e["subtitle"], "icon": e["icon"], + "fallbackIcon": e["fallbackIcon"], "fallbackColor": e["fallbackColor"], "keywords": e["keywords"], + "score": s, "provider": "system", "actions": e["actions"]}) return out + def main() -> None: for line in sys.stdin: try: msg = json.loads(line) except json.JSONDecodeError: continue - if msg.get("op") == "search": print(json.dumps({"id": msg.get("id", ""), "results": search(str(msg.get("query", "")))}), flush=True) - elif msg.get("op") == "cancel": print(json.dumps({"id": msg.get("target", ""), "cancelled": True}), flush=True) - elif msg.get("op") == "execute": print(json.dumps({"id": msg.get("id", ""), "executed": False}), flush=True) + if not isinstance(msg, dict): continue + op = msg.get("op") + if op == "search": print(json.dumps({"id": msg.get("id", ""), "results": search(str(msg.get("query", "")))}), flush=True) + elif op == "cancel": print(json.dumps({"id": msg.get("target", ""), "cancelled": True}), flush=True) + elif op == "execute": print(json.dumps({"id": msg.get("id", ""), "executed": False, "error": "system actions are launched by the host"}), flush=True) + + if __name__ == "__main__": main() diff --git a/tests/test_system.py b/tests/test_system.py new file mode 100644 index 0000000..241b2ed --- /dev/null +++ b/tests/test_system.py @@ -0,0 +1,51 @@ +"""system provider + omarchy extras: every command is a real Omarchy command.""" +import importlib.util +from pathlib import Path +import sys +import unittest + +ROOT = Path(__file__).resolve().parents[1] +OMARCHY_BIN = Path('/workspace/omarchy-src/bin') + + +def load(name, rel): + spec = importlib.util.spec_from_file_location(name, ROOT / rel) + mod = importlib.util.module_from_spec(spec) + sys.modules[name] = mod + spec.loader.exec_module(mod) + return mod + + +system = load('system_provider', 'providers/system/provider.py') +omarchy = load('omarchy_provider_x', 'providers/omarchy/provider.py') + + +class SystemProviderTests(unittest.TestCase): + def test_ids_unique_and_argv_lists(self): + keys = [e['key'] for e in system.ENTRIES] + self.assertEqual(len(keys), len(set(keys))) + for e in system.ENTRIES: + argv = e['actions'][0]['argv'] + self.assertTrue(argv and all(isinstance(a, str) and a for a in argv), e['key']) + + def test_search_and_confirmation(self): + top = system.search('volume')[0] + self.assertEqual(top['actions'][0]['argv'], ['omarchy-audio-output-volume', 'raise']) + close = {r['id']: r for r in system.search('close all')}['system:win-close-all'] + self.assertEqual(close['actions'][0]['danger'], 'confirm') + self.assertEqual(system.search(''), []) + + def test_no_power_or_package_duplicates_of_the_menu(self): + commands = {' '.join(e['actions'][0]['argv']) for e in system.ENTRIES} + for dup in ('omarchy-system shutdown', 'omarchy-system reboot', 'systemctl suspend', 'omarchy-system-lock'): + self.assertNotIn(dup, commands) + + @unittest.skipUnless(OMARCHY_BIN.is_dir(), 'Omarchy source checkout not present') + def test_every_command_exists_in_omarchy_bin(self): + argvs = [e['actions'][0]['argv'] for e in system.ENTRIES] + [x[4] for x in omarchy.EXTRAS] + for argv in argvs: + self.assertTrue((OMARCHY_BIN / argv[0]).exists(), argv) + + +if __name__ == '__main__': + unittest.main() From 6f395e998b8466b39e334aca0cc9ba27b52d6c99 Mon Sep 17 00:00:00 2001 From: Grok Bot Date: Thu, 29 Oct 2026 16:30:23 -0400 Subject: [PATCH 11/19] defaults: Omarchy default apps, TUIs and web apps as bound to keys; web-app searches --- providers/defaults/README.md | 62 ++++++++++++ providers/defaults/manifest.json | 8 ++ providers/defaults/provider.py | 161 +++++++++++++++++++++++++++++++ tests/test_defaults.py | 97 +++++++++++++++++++ 4 files changed, 328 insertions(+) create mode 100644 providers/defaults/README.md create mode 100644 providers/defaults/manifest.json create mode 100644 providers/defaults/provider.py create mode 100644 tests/test_defaults.py diff --git a/providers/defaults/README.md b/providers/defaults/README.md new file mode 100644 index 0000000..c3689a5 --- /dev/null +++ b/providers/defaults/README.md @@ -0,0 +1,62 @@ +# defaults provider + +Omarchy's opinionated default apps, launched exactly the way Omarchy's own +keybindings launch them, plus searches inside Omarchy's web apps. Scope with +`web:` (also `default:` / `defaults:`); unscoped queries need 2+ characters. + +Apps that already ship a `.desktop` file in Omarchy (`applications/`: YouTube, +WhatsApp, HEY, X, Google Maps/Messages/Photos, Basecamp, Zoom, ...) and +packaged apps (Obsidian, Omacalc, Omawrite, LibreOffice, ...) come from the +`apps` provider and are not duplicated here. + +## Apps and web apps + +`helpers.lua` maps `{omarchy=X}` → `omarchy-launch-X`, `{webapp=URL}` → +`omarchy-launch-webapp URL`, `{tui=X}` → `omarchy-launch-tui X`, +`{tui=X, focus=true}` → `omarchy-launch-or-focus-tui X`. + +| Row | argv | Source (Omarchy) | +|---|---|---| +| Terminal | `omarchy-launch-terminal` | `default/hypr/bindings/applications.lua` (Super+Return) | +| Browser | `omarchy-launch-browser` | `applications.lua` (Super+Shift+B) | +| Private Browser Window | `omarchy-launch-browser --private` | `applications.lua` (Super+Shift+Alt+B) | +| File Manager | `omarchy-launch-nautilus` | `applications.lua` (Super+Shift+F) | +| Editor | `omarchy-launch-editor` | `applications.lua` (Super+Shift+N) | +| Tmux Terminal | `omarchy-launch-terminal-tmux` | `applications.lua` (Super+Alt+Return) | +| Herdr Terminal | `omarchy-launch-terminal-herdr` | `applications.lua` (Super+Ctrl+Return) | +| Music | `omarchy-launch-spotify` | `applications.lua` (Super+Shift+M); offers to install Spotify if missing (`bin/omarchy-launch-spotify`) | +| Music TUI | `omarchy-launch-or-focus-tui cliamp` | `applications.lua` (Super+Shift+Alt+M); `cliamp` in `install/omarchy-base.packages` | +| Signal | `omarchy-launch-signal` | `applications.lua` (Super+Shift+G); offers to install if missing | +| Passwords | `omarchy-launch-1password` | `applications.lua` (Super+Shift+/); offers to install if missing | +| Docker | `omarchy-launch-tui omarchy-launch-docker-tui` | `applications.lua` (Super+Shift+D); shown only when `lazydocker` is on PATH (`shutil.which`, nothing is executed), as the binding is | +| Activity | `omarchy-launch-tui btop` | `default/hypr/bindings/utilities.lua` (Super+Shift+T); `btop` in base packages | +| ChatGPT | `omarchy-launch-webapp https://chatgpt.com` | `applications.lua` (Super+Shift+A) | +| Grok | `omarchy-launch-webapp https://grok.com` | `applications.lua` (Super+Shift+Alt+A) | +| Calendar (HEY) | `omarchy-launch-webapp https://app.hey.com/calendar/weeks/` | `applications.lua` (Super+Shift+C) | +| New Email (HEY) | `omarchy-launch-webapp https://app.hey.com/messages/new?display=standalone&new_window=true` | `applications.lua` (Super+Shift+Alt+E) | +| New X Post | `omarchy-launch-webapp https://x.com/compose/post` | `applications.lua` (Super+Shift+Alt+X) | + +## Web-app searches + +`youtube lofi`, `yt lofi`, `maps coffee`, `chatgpt …`, `gpt …`, `grok …`, +`twitter …`, `google …` / `search …` / `web …` give one search row for that +site, in any scope. `web:` offers every site at once. + +| Row | argv | Notes | +|---|---|---| +| Search the Web | `omarchy-launch-browser https://www.google.com/search?q=` | `bin/omarchy-launch-browser` takes a URL | +| Search YouTube | `omarchy-launch-webapp https://www.youtube.com/results?search_query=` | YouTube web app (`applications.lua`) | +| Search Google Maps | `omarchy-launch-webapp https://maps.google.com/?q=` | Maps web app | +| Ask ChatGPT | `omarchy-launch-webapp https://chatgpt.com/?q=` | ChatGPT web app | +| Ask Grok | `omarchy-launch-webapp https://grok.com/?q=` | Grok web app | +| Search X | `omarchy-launch-webapp https://x.com/search?q=` | X web app | + +## Safety + +- Every action is a fixed argv; the typed text only ever appears + percent-encoded inside one URL argument (`-` is encoded too, because + `omarchy-launch-browser` rewrites the substring `--private` in its args). + Text over 300 characters or with control characters is ignored. +- Query-derived URLs never go to `omarchy-launch-or-focus*`, which `eval` the + launch command. +- Nothing is executed at import or search time. diff --git a/providers/defaults/manifest.json b/providers/defaults/manifest.json new file mode 100644 index 0000000..bb1419c --- /dev/null +++ b/providers/defaults/manifest.json @@ -0,0 +1,8 @@ +{ + "name": "defaults", + "description": "Omarchy's preinstalled apps, TUIs and web apps as bound to keys, plus web searches in Omarchy's web apps", + "exec": ["python3", "providers/defaults/provider.py"], + "triggers": [], + "capabilities": ["search"], + "minQuery": 2 +} diff --git a/providers/defaults/provider.py b/providers/defaults/provider.py new file mode 100644 index 0000000..d2cc9c9 --- /dev/null +++ b/providers/defaults/provider.py @@ -0,0 +1,161 @@ +#!/usr/bin/env python3 +"""Omarchy's opinionated default apps, the way Omarchy's own keys launch them. + +Every launch argv is copied from default/hypr/bindings/applications.lua or +utilities.lua (helpers.lua turns `{omarchy=X}` into `omarchy-launch-X`, +`{webapp=URL}` into `omarchy-launch-webapp URL`, `{tui=X}` into +`omarchy-launch-tui X`). README.md lists the source of each row. + +Apps that already ship a .desktop file (YouTube, WhatsApp, HEY, Google Maps, +...) come from the apps provider; this one adds what a .desktop file does not +offer: Omarchy's launch-or-focus wrappers, the private window, TUIs, the +shortcut-only web apps, and searches inside Omarchy's web apps. + +Searches pass the typed text only as a percent-encoded URL argument to +`omarchy-launch-webapp` / `omarchy-launch-browser` (argv, no shell). Never to +the `*-or-focus-*` wrappers, which eval their arguments. +""" +from __future__ import annotations +import hashlib +import json +import re +import shutil +import sys +from urllib.parse import quote + +MAX_RESULTS = 12 +MAX_TERM = 300 +BLUE = "#89b4fa" +GREEN = "#a6e3a1" +PURPLE = "#cba6f7" +RED = "#f38ba8" + + +def app(key, title, subtitle, icon, argv, words, glyph, color=BLUE, keys="", needs=""): + sub = subtitle + (" · " + keys if keys else "") + return {"key": key, "title": title, "subtitle": sub, "icon": icon, "fallbackIcon": glyph, "fallbackColor": color, + "keywords": (title + " " + words).lower(), "argv": argv, "needs": needs} + + +def webapp(url): + return ["omarchy-launch-webapp", url] + + +CATALOG = [ + # applications.lua: essential bindings + app("terminal", "Terminal", "Open the default terminal", "utilities-terminal", ["omarchy-launch-terminal"], "shell console alacritty ghostty kitty", "❯", keys="Super+Return"), + app("browser", "Browser", "Open the default browser", "web-browser", ["omarchy-launch-browser"], "web internet chromium", "🌐", keys="Super+Shift+B"), + app("browser-private", "Private Browser Window", "Open the default browser in a private/incognito window", "web-browser", ["omarchy-launch-browser", "--private"], "incognito private browsing web", "🕶", PURPLE, keys="Super+Shift+Alt+B"), + app("file-manager", "File Manager", "Open Nautilus", "system-file-manager", ["omarchy-launch-nautilus"], "files nautilus folders explorer", "📁", keys="Super+Shift+F"), + app("editor", "Editor", "Open the default editor (Omarchy defaults)", "accessories-text-editor", ["omarchy-launch-editor"], "nvim neovim code text", "✎", keys="Super+Shift+N"), + # applications.lua: preinstalled apps and TUIs + app("tmux", "Tmux Terminal", "Attach to tmux or start the Work session", "utilities-terminal", ["omarchy-launch-terminal-tmux"], "tmux multiplexer session work", "❯", keys="Super+Alt+Return"), + app("herdr", "Herdr Terminal", "Open a terminal running herdr", "utilities-terminal", ["omarchy-launch-terminal-herdr"], "herdr agents", "❯", keys="Super+Ctrl+Return"), + app("music", "Music", "Focus or launch Spotify (offers to install it if missing)", "spotify", ["omarchy-launch-spotify"], "spotify songs player", "🎵", GREEN, keys="Super+Shift+M"), + app("music-tui", "Music TUI", "Focus or launch cliamp in a terminal", "multimedia-player", ["omarchy-launch-or-focus-tui", "cliamp"], "cliamp terminal player songs", "🎵", GREEN, keys="Super+Shift+Alt+M"), + app("signal", "Signal", "Focus or launch Signal (offers to install it if missing)", "signal-desktop", ["omarchy-launch-signal"], "chat messenger messages", "💬", keys="Super+Shift+G"), + app("passwords", "Passwords", "Launch 1Password (offers to install it if missing)", "1password", ["omarchy-launch-1password"], "1password password manager vault secrets", "🔑", keys="Super+Shift+/"), + app("docker", "Docker", "Open lazydocker in a terminal", "docker", ["omarchy-launch-tui", "omarchy-launch-docker-tui"], "lazydocker containers images", "🐳", keys="Super+Shift+D", needs="lazydocker"), + # utilities.lua + app("activity", "Activity", "System monitor (btop) in a terminal", "utilities-system-monitor", ["omarchy-launch-tui", "btop"], "btop top htop processes cpu memory task manager", "📈", GREEN, keys="Super+Shift+T"), + # applications.lua: web apps without a .desktop file of their own + app("chatgpt", "ChatGPT", "Web app", "chatgpt", webapp("https://chatgpt.com"), "ai openai gpt chat assistant", "🤖", GREEN, keys="Super+Shift+A"), + app("grok", "Grok", "Web app", "grok", webapp("https://grok.com"), "ai xai chat assistant", "🤖", keys="Super+Shift+Alt+A"), + app("hey-calendar", "Calendar (HEY)", "HEY Calendar web app", "x-office-calendar", webapp("https://app.hey.com/calendar/weeks/"), "hey calendar schedule events week", "📅", keys="Super+Shift+C"), + app("hey-new-email", "New Email (HEY)", "Compose a HEY email in its own window", "mail-message-new", webapp("https://app.hey.com/messages/new?display=standalone&new_window=true"), "hey compose write mail email message", "✉", keys="Super+Shift+Alt+E"), + app("x-post", "New X Post", "Compose a post on X", "x", webapp("https://x.com/compose/post"), "x twitter tweet compose post write", "✕", keys="Super+Shift+Alt+X"), +] + +# Searches inside Omarchy's own web apps (applications.lua URLs) and the +# default browser. `words` are the leading words that pick a site unscoped. +SITES = [ + ("web", "Search the Web", "Google in the default browser", "web-browser", ("google", "search", "web"), lambda t: ["omarchy-launch-browser", "https://www.google.com/search?q=" + t], "🔎"), + ("youtube", "Search YouTube", "YouTube web app", "youtube", ("youtube", "yt"), lambda t: webapp("https://www.youtube.com/results?search_query=" + t), "▶"), + ("maps", "Search Google Maps", "Google Maps web app", "google-maps", ("maps", "map", "directions"), lambda t: webapp("https://maps.google.com/?q=" + t), "🗺"), + ("chatgpt", "Ask ChatGPT", "ChatGPT web app", "chatgpt", ("chatgpt", "gpt"), lambda t: webapp("https://chatgpt.com/?q=" + t), "🤖"), + ("grok", "Ask Grok", "Grok web app", "grok", ("grok",), lambda t: webapp("https://grok.com/?q=" + t), "🤖"), + ("x", "Search X", "X web app", "x", ("twitter",), lambda t: webapp("https://x.com/search?q=" + t), "✕"), +] + + +def encode(term: str) -> str: + # '-' is encoded too: omarchy-launch-browser rewrites "--private" anywhere in its args. + return quote(term, safe="").replace("-", "%2D") + + +def valid_term(term: str) -> bool: + return 0 < len(term) <= MAX_TERM and all(c.isprintable() for c in term) + + +def available(entry) -> bool: + return not entry["needs"] or shutil.which(entry["needs"]) is not None + + +def row(rid, title, subtitle, icon, glyph, color, keywords, argv, score): + return {"id": "defaults:" + rid, "title": title, "subtitle": subtitle, "icon": icon, "fallbackIcon": glyph, + "fallbackColor": color, "keywords": keywords, "score": score, "provider": "defaults", + "actions": [{"type": "launch", "title": title, "argv": argv}, + {"type": "copy", "title": "Copy command", "text": " ".join(argv)}]} + + +def search_row(site, term, score): + key, title, subtitle, icon, _words, build, glyph = site + digest = hashlib.sha256(term.encode()).hexdigest()[:12] + return row(f"search-{key}-{digest}", f"{title}: {term}", subtitle, icon, glyph, BLUE, + f"{title} {term}".lower(), build(encode(term)), score) + + +def score(query: str, entry: dict) -> float: + title = entry["title"].lower() + if title == query: return 0.95 + if title.startswith(query): return 0.9 + if any(w.startswith(query) for w in re.split(r"[\s()/]+", title) if w): return 0.8 + if query in title: return 0.7 + if any(w.startswith(query) for w in entry["keywords"].split()): return 0.6 + return 0.0 + + +def site_for(word: str): + return next((s for s in SITES if word in s[4]), None) + + +def search(query: str, scope: str = "", catalog=None) -> list[dict]: + catalog = CATALOG if catalog is None else catalog + text = " ".join(query.split()) + q = text.lower() + out: list[dict] = [] + # "youtube lofi", "maps coffee" (any scope); "web:lofi" searches every site. + head, _, rest = text.partition(" ") + site = site_for(head.lower()) + if site and valid_term(rest): + out.append(search_row(site, rest, 0.93)) + elif scope == "defaults" and valid_term(text) and not any(score(q, e) >= 0.8 for e in catalog): + out += [search_row(s, text, 0.9 - n * 0.01) for n, s in enumerate(SITES)] + if q: + ranked = sorted(((score(q, e), n, e) for n, e in enumerate(catalog)), key=lambda t: (-t[0], t[1])) + for s, _, e in ranked: + if s == 0.0: break + if available(e): + out.append(row(e["key"], e["title"], e["subtitle"], e["icon"], e["fallbackIcon"], e["fallbackColor"], + e["keywords"], e["argv"], s)) + elif scope == "defaults": + out += [row(e["key"], e["title"], e["subtitle"], e["icon"], e["fallbackIcon"], e["fallbackColor"], + e["keywords"], e["argv"], 0.5) for e in catalog if available(e)] + return out[:MAX_RESULTS] if scope != "defaults" or q else out + + +def main() -> None: + for line in sys.stdin: + try: msg = json.loads(line) + except json.JSONDecodeError: continue + if not isinstance(msg, dict): continue + op = msg.get("op") + if op == "search": + print(json.dumps({"id": msg.get("id", ""), "results": search(str(msg.get("query", "")), str(msg.get("scope", "")))}), flush=True) + elif op == "cancel": + print(json.dumps({"id": msg.get("target", ""), "cancelled": True}), flush=True) + elif op == "execute": + print(json.dumps({"id": msg.get("id", ""), "executed": False, "error": "default-app actions are launched by the host"}), flush=True) + + +if __name__ == "__main__": main() diff --git a/tests/test_defaults.py b/tests/test_defaults.py new file mode 100644 index 0000000..f030935 --- /dev/null +++ b/tests/test_defaults.py @@ -0,0 +1,97 @@ +"""defaults provider: Omarchy's default apps and web-app searches.""" +import importlib.util +import json +from pathlib import Path +import subprocess +import sys +import unittest +from unittest import mock +from urllib.parse import unquote + +ROOT = Path(__file__).resolve().parents[1] +SPEC = importlib.util.spec_from_file_location('defaults_provider', ROOT / 'providers/defaults/provider.py') +d = importlib.util.module_from_spec(SPEC) +SPEC.loader.exec_module(d) +OMARCHY = Path('/workspace/omarchy-src') + + +def titles(results): + return [r['title'] for r in results] + + +class DefaultsTests(unittest.TestCase): + def test_catalog_rows_launch_by_argv(self): + r = d.search('private')[0] + self.assertEqual(r['title'], 'Private Browser Window') + self.assertEqual(r['actions'][0]['argv'], ['omarchy-launch-browser', '--private']) + self.assertIn('Super+Shift+Alt+B', r['subtitle']) + self.assertEqual(d.search('btop')[0]['actions'][0]['argv'], ['omarchy-launch-tui', 'btop']) + + def test_site_word_searches_with_encoded_argument(self): + r = d.search('youtube lofi; rm -rf ~ $(x)')[0] + argv = r['actions'][0]['argv'] + self.assertEqual(argv[0], 'omarchy-launch-webapp') + self.assertEqual(len(argv), 2) + self.assertTrue(argv[1].startswith('https://www.youtube.com/results?search_query=')) + tail = argv[1].split('=', 1)[1] + for ch in ' ;$()-': + self.assertNotIn(ch, tail) + self.assertEqual(unquote(tail), 'lofi; rm -rf ~ $(x)') + + def test_private_flag_cannot_be_smuggled_into_browser_search(self): + argv = d.search('google a --private b')[0]['actions'][0]['argv'] + self.assertEqual(argv[0], 'omarchy-launch-browser') + self.assertNotIn('--private', argv[1]) + + def test_search_never_uses_eval_wrappers(self): + for q, scope in [('maps x', ''), ('chatgpt y', ''), ('anything', 'defaults')]: + for r in d.search(q, scope): + if r['id'].startswith('defaults:search-'): + self.assertNotIn('or-focus', r['actions'][0]['argv'][0]) + + def test_scoped_text_offers_every_site_and_unscoped_does_not(self): + scoped = titles(d.search('cats', 'defaults')) + self.assertEqual(scoped[0], 'Search the Web: cats') + self.assertIn('Search X: cats', scoped) + self.assertEqual(d.search('cats'), []) + + def test_scoped_empty_lists_catalog_and_ids_are_stable(self): + rows = d.search('', 'defaults') + self.assertGreaterEqual(len(rows), 15) + self.assertEqual(rows[0]['id'], 'defaults:terminal') + self.assertEqual(d.search('youtube a')[0]['id'], d.search('youtube a')[0]['id']) + self.assertNotEqual(d.search('youtube a')[0]['id'], d.search('youtube b')[0]['id']) + + def test_invalid_terms_are_ignored(self): + self.assertEqual(d.search('youtube ' + 'a' * 400), []) + self.assertEqual(d.search('youtube'), []) + + def test_docker_only_when_lazydocker_present(self): + with mock.patch.object(d.shutil, 'which', return_value=None): + self.assertNotIn('Docker', titles(d.search('docker'))) + with mock.patch.object(d.shutil, 'which', return_value='/usr/bin/lazydocker'): + self.assertIn('Docker', titles(d.search('docker'))) + + @unittest.skipUnless(OMARCHY.is_dir(), 'Omarchy source checkout not present') + def test_every_command_is_real_and_every_webapp_url_is_omarchys(self): + bindings = (OMARCHY / 'default/hypr/bindings/applications.lua').read_text() + for e in d.CATALOG: + argv = e['argv'] + self.assertTrue((OMARCHY / 'bin' / argv[0]).exists(), argv) + if argv[0] == 'omarchy-launch-webapp': + self.assertIn('"' + argv[1] + '"', bindings, argv) + if argv[0] in ('omarchy-launch-tui', 'omarchy-launch-or-focus-tui') and argv[1] != 'omarchy-launch-docker-tui': + self.assertIn(argv[1], (OMARCHY / 'install/omarchy-base.packages').read_text().split()) + + def test_protocol(self): + lines = '\n'.join(json.dumps(m) for m in [{'id': '1', 'op': 'search', 'query': 'tmux'}, + {'id': '2', 'op': 'execute'}]) + '\n' + out = subprocess.run([sys.executable, str(ROOT / 'providers/defaults/provider.py')], input=lines, + capture_output=True, text=True, timeout=10).stdout.splitlines() + first, second = json.loads(out[0]), json.loads(out[1]) + self.assertEqual(first['results'][0]['id'], 'defaults:tmux') + self.assertFalse(second['executed']) + + +if __name__ == '__main__': + unittest.main() From 6c6d844ff60e813cab51b8931dc7eca6d72af54d Mon Sep 17 00:00:00 2001 From: Grok Bot Date: Thu, 29 Oct 2026 16:31:34 -0400 Subject: [PATCH 12/19] run: conservative PATH discovery (scandir only, confirm-gated fallback rows) --- providers/run/README.md | 28 +++++++++++ providers/run/provider.py | 97 ++++++++++++++++++++++++++++++++++++++- tests/test_run.py | 49 +++++++++++++++++++- 3 files changed, 171 insertions(+), 3 deletions(-) create mode 100644 providers/run/README.md diff --git a/providers/run/README.md b/providers/run/README.md new file mode 100644 index 0000000..44aa5b1 --- /dev/null +++ b/providers/run/README.md @@ -0,0 +1,28 @@ +# run provider + +## Run: + +Every query of 3+ characters gets one trailing `Run: ` row. Choosing it +opens a visible `foot` terminal (`foot -- sh -lc `), after the +confirmation sheet. Pallet itself never executes search text. The row is a +`fallback`, so the frontend always ranks it below every real match. + +## PATH discovery + +Conservative and listing-only: + +- PATH directories are listed with `os.scandir`; an entry counts when it is a + regular file with an executable bit (one `stat`, following symlinks). A + directory is re-listed only when its mtime changes. About 25 ms cold for + ~800 commands, then ~0.1 ms per query; the `warm` op pre-lists. +- Nothing found is ever executed, opened or probed (no `--help`, + `--version`, `which`, or shell). +- An exact command name (`btop`) adds **Command: btop** just above the Run + row. `run:` also lists up to 5 prefix matches. +- Only names matching `[A-Za-z0-9][A-Za-z0-9._+-]*` qualify; anything with a + space, slash or other character skips discovery. +- These rows are fallbacks too (never above local matches), run the bare + command with no arguments in a visible `foot` terminal, and require the + confirmation sheet. History never records `run` rows. + +`foot` is in Omarchy's default packages (`applications/foot.desktop`). diff --git a/providers/run/provider.py b/providers/run/provider.py index 9742bc5..704c05f 100644 --- a/providers/run/provider.py +++ b/providers/run/provider.py @@ -12,16 +12,107 @@ fallbacks below every real match regardless of score, so "Run:" is a floor, never a lid. The id carries the query so selection-follow and confirmation binding can never mistake one typed command for another. + +PATH discovery is conservative: the PATH directories are only listed +(os.scandir + one stat per entry for the executable bit, re-listed when a +directory's mtime changes). Nothing found is ever run to learn about it (no +--help, no --version, no .desktop parsing). An exact command name offers +"Command: "; `run:` also lists prefix matches. These rows are +fallbacks too, confirm-gated, and run the bare command in a visible terminal. """ from __future__ import annotations import json +import os +import re +import stat import sys MIN_LEN = 3 +MAX_PATH_ROWS = 5 +NAME_RE = re.compile(r"[A-Za-z0-9][A-Za-z0-9._+-]{0,79}") + + +class PathIndex: + """Executable names on PATH, by listing directories only.""" + + def __init__(self, path: str | None = None): + self.path = path + self.dirs: dict[str, tuple[float, frozenset[str]]] = {} + self._union: tuple[tuple, set[str]] = ((), set()) + + def _dirs(self) -> list[str]: + raw = os.environ.get("PATH", "") if self.path is None else self.path + seen, out = set(), [] + for d in raw.split(os.pathsep): + if d and os.path.isabs(d) and d not in seen: + seen.add(d); out.append(d) + return out + + def _list(self, d: str) -> frozenset[str]: + names = set() + try: + with os.scandir(d) as it: + for e in it: + if not NAME_RE.fullmatch(e.name): continue + try: + st = e.stat() # follows symlinks; never opens or runs the file + except OSError: + continue + if stat.S_ISREG(st.st_mode) and st.st_mode & 0o111: + names.add(e.name) + except OSError: + pass + return frozenset(names) + + def names(self) -> set[str]: + stamp, parts = [], [] + for d in self._dirs(): + try: mtime = os.stat(d).st_mtime + except OSError: continue + cached = self.dirs.get(d) + if not cached or cached[0] != mtime: + cached = (mtime, self._list(d)) + self.dirs[d] = cached + stamp.append((d, mtime)); parts.append(cached[1]) + if self._union[0] != tuple(stamp): + self._union = (tuple(stamp), set().union(*parts)) + return self._union[1] + + +INDEX = PathIndex() -def search(query: str) -> list[dict]: + +def command_row(name: str, score: float) -> dict: + return { + "id": "run:cmd:" + name, "fallback": True, "title": f"Command: {name}", + "subtitle": "Found on PATH · runs in a foot terminal", "icon": "utilities-terminal", + "fallbackIcon": "❯", "keywords": name, "score": score, "provider": "run", + "actions": [{ + "type": "launch", "title": "Run in terminal", "argv": ["foot", "--", name], + "danger": "confirm", "tone": "warning", + "confirmation": {"title": f"Run '{name}'?", + "detail": "Opens a visible foot terminal running this PATH command with no arguments. Pallet never ran it to discover it."}, + }, {"type": "copy", "title": "Copy command", "text": name}], + } + + +def discover(q: str, scoped: bool, index: PathIndex) -> list[dict]: + if not NAME_RE.fullmatch(q): return [] + names = index.names() + rows = [command_row(q, 0.3)] if q in names else [] + if scoped: + rows += [command_row(n, 0.28) for n in sorted(n for n in names if n.startswith(q) and n != q)[:MAX_PATH_ROWS - len(rows)]] + return rows + + +def search(query: str, scope: str = "", index: PathIndex | None = None) -> list[dict]: q = query.strip() if len(q) < MIN_LEN: return [] + found = discover(q, scope == "run", INDEX if index is None else index) + return found + run_row(q) + + +def run_row(q: str) -> list[dict]: shown = q if len(q) <= 64 else q[:61] + "..." return [{ "id": "run:" + q, @@ -52,7 +143,9 @@ def main() -> None: try: msg = json.loads(line) except json.JSONDecodeError: continue if msg.get("op") == "search": - print(json.dumps({"id": msg.get("id", ""), "results": search(str(msg.get("query", "")))}), flush=True) + print(json.dumps({"id": msg.get("id", ""), "results": search(str(msg.get("query", "")), str(msg.get("scope", "")))}), flush=True) + elif msg.get("op") == "warm": + INDEX.names() elif msg.get("op") == "cancel": print(json.dumps({"id": msg.get("target", ""), "cancelled": True}), flush=True) elif msg.get("op") == "execute": diff --git a/tests/test_run.py b/tests/test_run.py index f426b44..7f546a8 100644 --- a/tests/test_run.py +++ b/tests/test_run.py @@ -1,5 +1,7 @@ import importlib.util from pathlib import Path +import os +import tempfile import unittest ROOT = Path(__file__).resolve().parents[1] @@ -10,7 +12,7 @@ class RunProviderTests(unittest.TestCase): def test_fallback_flag_id_and_argv(self): - [row] = run.search(' ls -la ') + [row] = run.search(' ls -la ', index=run.PathIndex('')) self.assertTrue(row['fallback']) self.assertEqual(row['id'], 'run:ls -la') launch = row['actions'][0] @@ -24,5 +26,50 @@ def test_short_queries_produce_nothing(self): self.assertEqual(run.search('ls'), []) +class PathDiscoveryTests(unittest.TestCase): + def setUp(self): + self.dir = tempfile.TemporaryDirectory() + self.addCleanup(self.dir.cleanup) + self.marker = os.path.join(self.dir.name, 'ran') + for name, mode in [('btop', 0o755), ('btop-helper', 0o755), ('notexec', 0o644)]: + path = os.path.join(self.dir.name, name) + with open(path, 'w') as f: + f.write('#!/bin/sh\ntouch ' + self.marker + '\n') + os.chmod(path, mode) + os.mkdir(os.path.join(self.dir.name, 'btopdir')) + self.index = run.PathIndex(self.dir.name + os.pathsep + 'relative/dir') + + def test_exact_name_offers_confirmed_command_above_run_row(self): + rows = run.search('btop', index=self.index) + self.assertEqual([r['id'] for r in rows], ['run:cmd:btop', 'run:btop']) + cmd = rows[0] + self.assertTrue(cmd['fallback']) + self.assertGreater(cmd['score'], rows[1]['score']) + self.assertEqual(cmd['actions'][0]['argv'], ['foot', '--', 'btop']) + self.assertEqual(cmd['actions'][0]['danger'], 'confirm') + + def test_prefix_matches_only_when_scoped_and_only_executable_files(self): + self.assertEqual([r['id'] for r in run.search('bto', index=self.index)], ['run:bto']) + ids = [r['id'] for r in run.search('bto', 'run', index=self.index)] + self.assertEqual(ids, ['run:cmd:btop', 'run:cmd:btop-helper', 'run:bto']) + self.assertEqual(run.search('notexec', index=self.index)[0]['id'], 'run:notexec') + + def test_discovery_never_executes_anything(self): + run.search('btop', 'run', index=self.index) + self.assertFalse(os.path.exists(self.marker)) + + def test_rescans_when_directory_changes(self): + self.assertNotIn('newtool', self.index.names()) + path = os.path.join(self.dir.name, 'newtool') + open(path, 'w').close(); os.chmod(path, 0o755) + st = os.stat(self.dir.name) + os.utime(self.dir.name, (st.st_atime, st.st_mtime + 5)) + self.assertIn('newtool', self.index.names()) + + def test_queries_with_spaces_or_odd_characters_skip_discovery(self): + self.assertEqual([r['id'] for r in run.search('btop --x', 'run', index=self.index)], ['run:btop --x']) + self.assertEqual([r['id'] for r in run.search('../btop', 'run', index=self.index)], ['run:../btop']) + + if __name__ == '__main__': unittest.main() From e56ff9892a4ea83c2dc91dedc24ddb604d5a9363 Mon Sep 17 00:00:00 2001 From: Grok Bot Date: Thu, 29 Oct 2026 16:31:39 -0400 Subject: [PATCH 13/19] run: cite foot's package source --- providers/run/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/providers/run/README.md b/providers/run/README.md index 44aa5b1..424b3aa 100644 --- a/providers/run/README.md +++ b/providers/run/README.md @@ -25,4 +25,4 @@ Conservative and listing-only: command with no arguments in a visible `foot` terminal, and require the confirmation sheet. History never records `run` rows. -`foot` is in Omarchy's default packages (`applications/foot.desktop`). +`foot` is in Omarchy's default packages (`install/omarchy-base.packages`). From 37f03949cdc7433d6a1981441be7b54370b9978d Mon Sep 17 00:00:00 2001 From: Grok Bot Date: Thu, 29 Oct 2026 16:33:41 -0400 Subject: [PATCH 14/19] =?UTF-8?q?release:=200.10.0=20=E2=80=94=20changelog?= =?UTF-8?q?,=20roadmap,=20provider=20protocol=20docs,=20scopes?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 74 +++++++++++++++++++++++++++++++++++++++++++++++ README.md | 9 ++++++ docs/PROVIDERS.md | 46 +++++++++++++++++++++++++---- docs/ROADMAP.md | 36 +++++++++++++++++------ manifest.json | 4 +-- 5 files changed, 153 insertions(+), 16 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f91d449..1507311 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,80 @@ Release numbers come from the root manifest.json. This changelog starts with ## [Unreleased] +## [0.10.0] + +Native-session verification on Omarchy is still pending for everything +below: it is covered by Python/Node tests and a synthetic host benchmark only, +not yet exercised in the real Quickshell overlay on Hyprland. + +### Speed +- The host streams merged results as soon as the first provider answers + (`partial`, `final`, `pending`, `failed`), then follow-ups under the same + query id. Late providers still update the final view. +- Manifest routing hints `minQuery`/`scopedOnly`, plus more scope prefixes + (`om:`/`menu:`, `s:`, `w:`, `m:`, `p:`, `run:`, `web:`). +- `op:warm` when the overlay opens: files, menus, apps, omarchy and run + pre-load caches. Frontend debounce went from 80 ms to 30 ms. +- Files: background chunked index (no per-entry stat), partial cold replies, + substring candidates with top-k selection; stat only for shown rows. +- Menus (AT-SPI): cache-first with stale-while-revalidate, walks on a + worker thread with a 1.2 s budget; stdin never blocks. Without gi or the + accessibility bus it answers [] instead of crashing. +- Apps: .desktop rescans happen in the background. +- Benchmark (`scripts/bench_host.py`, 60k-file synthetic home): warm + median 105 ms → 7 ms, cold "notes" 571 ms with no file rows → first rows + 77 ms and files at 351 ms, typing "con…config" ~100 ms → 11–23 ms. With a + 1.5 s straggler provider: first rows 502 ms → 14 ms. The final/unresolved + marker now arrives at 600 ms (merge deadline raised from 500 ms). + +### Search quality +- Ranking tiers: exact > prefix > word boundary > substring > subsequence, + stable ties. `fallback` rows (`Run:` and PATH commands) always rank last. +- Selection follows the same result while rows stream in; updates wait + while an action panel or confirmation is open. +- Frecency: chosen results get a bounded `recent` boost within their tier + (`$XDG_STATE_HOME/pallet/history.json`, 14-day half-life, 400 entries, + `PALLET_HISTORY=0` disables; `Run:` commands are never recorded). + +### Foundations +- Confirmation sheets are bound to the exact result id and query. +- Provider failures/timeouts are shown in the footer; action failures + (provider `executed:false`, host-launched argv that fails to spawn) flash + in the footer and raise an `omarchy-notification-send` notification. +- `launch` actions are spawned by the host (argv, no shell) so a missing + command is reported. +- Conservative PATH discovery in the run provider: directory listing only, + never executes candidates; exact name, or prefix with `run:`; confirm-gated. + +### Omarchy parity and actions +- omarchy provider: every entry of Omarchy's own menu + (`default/omarchy/omarchy-menu.jsonc` plus the user overlay), run through + `omarchy-menu summon `. `when`/`disabled` guards are evaluated in the + background like the native menu. Hidden rows stay hidden, installed rows + are marked, and unknown guards open the parent menu. Destructive and + system-state entries (power, remove, reset, security, DNS, updates, + installs) require confirmation. Shell panels (Network, Bluetooth, Audio, + Power, Display, Calendar, World Clock, Tailscale, Weather) are included. +- system provider: key-bound controls Omarchy does not list in its menu, + covering volume, mic, output and media-source switching, media keys, + brightness, keyboard backlight, Bluetooth power, notifications, window + helpers (Close All Windows confirms), time/battery and the agent picker. + Power and update rows moved to the omarchy provider (no duplicates). The + hibernation probe at import time was removed. +- New defaults provider (`web:`): Omarchy's default apps, TUIs and web apps + as its keys launch them (private browser, tmux/herdr terminals, cliamp, + btop, Signal, 1Password, Spotify, lazydocker when present, ChatGPT, Grok, + HEY Calendar/New Email, New X Post). Also web-app searches + (`youtube …`, `maps …`, `chatgpt …`, `grok …`, `google …`) with the text + percent-encoded into one URL argument. +- Every provider README lists the Omarchy source file for each command. + +### Changed +- The system provider no longer offers Lock/Suspend/Reboot/Shutdown/Update. + The omarchy provider now provides them through the native menu. +- Files "open in editor" prefers `omarchy-launch-editor`; "terminal here" + uses `xdg-terminal-exec --dir=`. + ## [0.9.1] ### Fixed diff --git a/README.md b/README.md index 48d62c6..54ad54f 100644 --- a/README.md +++ b/README.md @@ -60,6 +60,15 @@ Use prefixes when you want one provider focused instead of mixed search: - `files:invoice` or `f:invoice` — files only - `obsidian:todo` or `o:todo` — Obsidian actions only - `apps:zen` or `a:zen` — applications only +- `omarchy:theme`, `om:theme` or `menu:theme` — the native Omarchy menu (every entry) and shell panels +- `system:mute` or `s:mute` — volume, media, brightness, notifications, window helpers +- `web:lofi` (also `default:`) — Omarchy's default apps and web apps; text searches every web app +- `windows:`/`w:`, `menus:`/`m:`, `plugins:`/`p:` — open windows, the focused app's menus, plugins +- `run:pyth` — PATH commands starting with the text, plus `Run: pyth` + +Mixed search remains the default; scopes are additive, not required. A few +leading words also pick a web app without a scope: `youtube lofi`, +`maps coffee`, `chatgpt …`, `grok …`, `google …`. Mixed search remains the default; scopes are additive, not required. diff --git a/docs/PROVIDERS.md b/docs/PROVIDERS.md index f4dba0a..2749e1c 100644 --- a/docs/PROVIDERS.md +++ b/docs/PROVIDERS.md @@ -8,12 +8,39 @@ A provider is a subprocess speaking newline-delimited JSON over stdio. It is nev {"name": "apps", "exec": ["python3", "providers/apps/provider.py"], "capabilities": ["search"]} ``` +Optional routing hints: + +- `minQuery` — skip unscoped queries shorter than this many characters + (files and run use 3, menus and defaults 2). Scoped queries always reach the + provider, even when empty. +- `scopedOnly: true` — only answer when the query is explicitly scoped. + ## Messages -Host to provider: `{"id":"q1","op":"search","query":"fire"}` · `{"id":"q2","op":"cancel","target":"q1"}` · `{"id":"a1","op":"execute","action":{...}}`. +Host to provider: `{"id":"q1","op":"search","query":"fire","scope":""}` · `{"id":"q2","op":"cancel","target":"q1"}` · `{"id":"a1","op":"execute","action":{...}}` · `{"id":"","op":"warm"}`. + +Provider to host: `{"id":"q1","results":[...]}` · `{"id":"q1","results":[...],"partial":true}` · `{"id":"q1","cancelled":true}` · `{"id":"a1","executed":false,"error":"why"}`. + +- `query` is the bare text with any scope prefix removed; `scope` is the + provider name when the user scoped the query (`f:`, `web:`, ...), else `""`. +- `partial: true` streams what is ready now; send the complete list later + under the same id without `partial`. Each reply replaces the provider's + earlier rows for that query. +- `warm` is sent when the overlay opens: pre-load caches, reply nothing. + Unknown ops must be ignored. +- `executed: false` with an `error` string is shown to the user (footer and + an `omarchy-notification-send` notification). -Provider to host: `{"id":"q1","results":[...]}` · `{"id":"a1","executed":true}`. The host tags merged results with their provider name; custom actions route only back to that owning provider. +It streams cumulative merged snapshots to the client with `final`, `pending` +(still answering), `failed` (dead/unavailable), `unresolved` (on the final +snapshot: providers that did not answer by the deadline) plus `scope` and +`bare`. Rows arriving shortly after the deadline are still merged in. + +Client to host (besides `search`/`cancel`/`execute`/`warm`): +`{"id":"l1","op":"launch","argv":[...]}` (the host spawns argv detached and +replies `executed`/`error`), and `{"op":"record","provider":"apps","result":"apps:x","title":"X"}` +(frecency history; see below). ## Result metadata @@ -23,10 +50,19 @@ The host tags merged results with their provider name; custom actions route only - `fallbackIcon`, `fallbackColor` — optional glyph/color used only when every icon candidate misses. - `score` — advisory 0..1; blended with the frontend fuzzy score. - `provider` — set by the host during merge. +- `id` — stable per result and unique per query meaning (for example `run:`); + selection follows it while rows stream in, and a confirmation sheet is bound + to it plus the exact query. +- `fallback: true` — always ranked after every non-fallback row (the `Run:` row + and PATH command rows). The `run` provider is always treated as fallback. +- `recent` — set by the host, 0..1, from frecency history + (`$XDG_STATE_HOME/pallet/history.json`; `PALLET_HISTORY=0` disables it; + `run` rows are never recorded). It only reorders rows within the same match + tier (exact > prefix > word > substring > subsequence). ## Action types -- `launch` — argv array, executed directly by the frontend, no shell +- `launch` — argv array, spawned detached by the host, no shell; a spawn failure is reported back - `copy` — put text on the clipboard (frontend) - `open` — path or url via xdg-open (frontend) - `custom` — provider-defined payload sent back to the owning provider @@ -37,10 +73,10 @@ The host tags merged results with their provider name; custom actions route only { "type": "launch", "title": "Update Omarchy", - "argv": ["foot", "--", "omarchy", "update"], + "argv": ["omarchy-menu", "summon", "update.omarchy"], "danger": "confirm", "tone": "positive", - "confirmation": {"title": "Update Omarchy?", "detail": "Runs the official Omarchy update in a terminal."} + "confirmation": {"title": "Update Omarchy?", "detail": "Runs Omarchy's own menu action (omarchy-menu summon update.omarchy)."} } ``` diff --git a/docs/ROADMAP.md b/docs/ROADMAP.md index a440e86..2595564 100644 --- a/docs/ROADMAP.md +++ b/docs/ROADMAP.md @@ -12,14 +12,31 @@ Implemented in 0.9.0, native-session verification still required: - [x] Refresh on later searches, including in-place edits and removed entries. - [x] Isolated regression tests; no installed programs launched during discovery. -Next foundation patches: -- [ ] Reject stale responses by query ID in the frontend, including edits, - clearing, dismissing, reopening, and changing scope. -- [ ] Preserve confirmations only for the exact selected result/query. -- [ ] Show provider failures/timeouts and propagate action failures. -- [ ] Ensure command suggestions cannot outrank meaningful local matches. -- [ ] Conservative exact/prefix PATH command discovery and user-saved TUI - profiles; never execute candidate binaries to discover capabilities. +Foundation patches (0.9.1–0.10.0; tests only, native-session verification +still pending): +- [x] Reject stale responses by query ID in the frontend, including edits, + clearing, dismissing, reopening, and changing scope (0.9.1). +- [x] Preserve confirmations only for the exact selected result/query. +- [x] Show provider failures/timeouts and propagate action failures + (provider `executed:false` and host launch failures). +- [x] Ensure command suggestions cannot outrank meaningful local matches + (`fallback` rows always rank last). +- [x] Conservative exact/prefix PATH command discovery; never execute + candidate binaries to discover capabilities. +- [ ] User-saved TUI profiles. + +## 1b. Speed and Omarchy parity (0.10.0; native verification pending) + +- [x] Streamed partial results, routing hints, warm-on-open, background + indexes for files/apps, cache-first AT-SPI menus. +- [x] Tiered ranking with stable ties and bounded frecency. +- [x] Full native-menu parity through `omarchy-menu summon`, with guards and + confirmations; shell panels; key-bound system controls; Omarchy + default apps/web apps and web-app searches. +- [ ] Verify on a native Omarchy session: overlay open time, streaming + without flicker, guard results matching the native menu, confirmations, + failure notifications. +- [ ] Evaluate `checked` menu state (currently not shown). ### Terminal application usage @@ -79,7 +96,8 @@ not launch terminals, install packages, or read personal vaults. ## 4. Recents and favorites -- [ ] Empty-query recents, pinning, bounded ranking boosts, and stale-result +- [x] Bounded ranking boosts from history (0.10.0). +- [ ] Empty-query recents, pinning, and stale-result cleanup. History can be cleared or disabled; commands/secrets are not recorded by default. diff --git a/manifest.json b/manifest.json index 813e0f5..5a567c3 100644 --- a/manifest.json +++ b/manifest.json @@ -2,10 +2,10 @@ "schemaVersion": 1, "id": "io.github.macifyer.pallet", "name": "Pallet", - "version": "0.9.1", + "version": "0.10.0", "author": "macifyer", "license": "MIT", - "description": "A keyboard-driven command palette for Omarchy: applications, windows, AT-SPI menus, files, system actions, packages, Omarchy parity, plugins, run-any-command, and Obsidian actions with scoped provider prefixes in one Quickshell overlay.", + "description": "A keyboard-driven command palette for Omarchy: applications, windows, AT-SPI menus, files, the full Omarchy menu, system controls, Omarchy default apps and web apps, plugins, run-any-command with PATH discovery, and Obsidian actions with scoped provider prefixes in one Quickshell overlay.", "kinds": [ "overlay", "service" From c89ebdae92153dfb32a05767daad11b3923e25a4 Mon Sep 17 00:00:00 2001 From: Grok Bot Date: Thu, 29 Oct 2026 16:58:25 -0400 Subject: [PATCH 15/19] shell: fix duplicate textFormat in ActionPanel (palette failed to load); add QML duplicate-binding test --- shell/ActionPanel.qml | 1 - tests/test_qml_lint.py | 45 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 tests/test_qml_lint.py diff --git a/shell/ActionPanel.qml b/shell/ActionPanel.qml index cd7d06c..b6ae4b5 100644 --- a/shell/ActionPanel.qml +++ b/shell/ActionPanel.qml @@ -70,7 +70,6 @@ Rectangle { Text { textFormat: Text.PlainText - textFormat: Text.PlainText anchors.left: parent.left anchors.leftMargin: 10 anchors.verticalCenter: parent.verticalCenter diff --git a/tests/test_qml_lint.py b/tests/test_qml_lint.py new file mode 100644 index 0000000..e63d20d --- /dev/null +++ b/tests/test_qml_lint.py @@ -0,0 +1,45 @@ +"""Cheap structural QML checks: Quickshell refuses a component whose object +sets the same property twice ("Property value set multiple times"), which +silently made the whole palette unavailable in 0.10.0 development.""" +import glob +import os +import re +import unittest + +ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) +SKIP = ("function", "signal", "property", "required", "readonly", "import") + + +def duplicate_bindings(path): + found = [] + stack = [{}] + with open(path, encoding="utf-8") as fh: + for n, line in enumerate(fh, 1): + s = line.split("//")[0].strip() + m = re.match(r"^([A-Za-z_][\w.]*)\s*:\s*[^{]", s) + if m and not s.startswith(SKIP): + key = m.group(1) + if key in stack[-1]: + found.append((n, key, stack[-1][key])) + stack[-1][key] = n + for ch in s: + if ch == "{": + stack.append({}) + elif ch == "}" and len(stack) > 1: + stack.pop() + return found + + +class QmlLintTests(unittest.TestCase): + def test_no_property_set_twice_in_one_object(self): + files = glob.glob(os.path.join(ROOT, "*.qml")) + glob.glob(os.path.join(ROOT, "shell", "*.qml")) + self.assertTrue(files) + problems = [] + for f in files: + for n, key, first in duplicate_bindings(f): + problems.append(f"{os.path.relpath(f, ROOT)}:{n} {key} (first set on line {first})") + self.assertEqual(problems, []) + + +if __name__ == "__main__": + unittest.main() From d53f0a879afe1ae107ce0ced293b99fa33b54baf Mon Sep 17 00:00:00 2001 From: Grok Bot Date: Thu, 29 Oct 2026 17:15:05 -0400 Subject: [PATCH 16/19] shell: keep previous results while a query streams; no blank or stale frames While a new query is in flight, re-rank the last answer locally against the new text (QueryState.localBare follows the sent query's scope prefix) and only blank the list for an empty query. Partial host messages keep previous rows of providers still pending (QueryState.carryOver) so the final message at the 600 ms merge deadline does not reshuffle the list. The bridge now reports the query text sent for each id, so lastQuery no longer drifts to whatever the input held when the reply landed. Stale ids are still rejected by QueryState. Tests: lifecycle.cjs covers localBare, carryOver and a con -> conf -> config typing sequence (no blank frame, stale reply dropped, no reorder on merge). --- CHANGELOG.md | 11 ++++++ shell/PaletteWindow.qml | 46 +++++++++++++------------ shell/ProviderBridge.qml | 12 ++++--- shell/QueryState.js | 37 ++++++++++++++++++++ tests/lifecycle.cjs | 74 +++++++++++++++++++++++++++++++++++++--- 5 files changed, 151 insertions(+), 29 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1507311..d75ee0d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -79,6 +79,17 @@ not yet exercised in the real Quickshell overlay on Hyprland. - Files "open in editor" prefers `omarchy-launch-editor`; "terminal here" uses `xdg-terminal-exec --dir=`. +### Fixed (Omarchy VM smoke test) +- Typing no longer flickers or shows stale rows. While a query is in + flight the previous answer is re-ranked locally against the new text + (also when editing backwards or across a scope prefix that still + matches); the list is only blanked for an empty query. Streamed partial + messages keep the previous rows of providers that have not answered yet, + so the 600 ms final message no longer reshuffles the list; the final + message is authoritative. The re-rank now uses the query text that was + sent for an answer, not the input text at the moment the reply landed. + Replies for old query ids are still dropped. + ## [0.9.1] ### Fixed diff --git a/shell/PaletteWindow.qml b/shell/PaletteWindow.qml index 191efbf..38492e0 100644 --- a/shell/PaletteWindow.qml +++ b/shell/PaletteWindow.qml @@ -4,6 +4,7 @@ import QtQuick.Layouts import Quickshell import Quickshell.Wayland import "Fuzzy.js" as Fuzzy +import "QueryState.js" as QueryState import "." PanelWindow { @@ -18,8 +19,9 @@ PanelWindow { // Selection follows the same result across streamed updates once the // user has moved it; until then the top row stays selected. property bool userMoved: false - // Last raw host results and the query they answered, used to re-filter - // instantly on the next keystroke while the host catches up. + // Last raw host results, the query text sent for them and the bare query + // the host matched. Used to re-rank instantly on the next keystroke while + // the host catches up, and to keep slow providers' rows while streaming. property var lastRaw: [] property string lastQuery: "" property string lastBare: "" @@ -112,10 +114,13 @@ PanelWindow { } const selected = Fuzzy.keyOf(root.filtered[root.selectedIndex]); root.scopeLabel = scope || "All"; - root.lastRaw = results; - root.lastQuery = searchInput.text; + // Until the final message, providers that have not answered keep + // their previous rows (re-ranked below), so nothing blinks out. + const rows = QueryState.carryOver(root.lastRaw, results, info.pending, info.final); + root.lastRaw = rows; + root.lastQuery = typeof info.query === "string" ? info.query : searchInput.text.trim(); root.lastBare = bare; - root.filtered = Fuzzy.rank(bare, results); + root.filtered = Fuzzy.rank(bare, rows); const kept = root.userMoved ? Fuzzy.indexOfKey(root.filtered, selected) : -1; root.selectedIndex = kept >= 0 ? kept : 0; if (kept < 0) root.userMoved = false; @@ -174,28 +179,26 @@ PanelWindow { } function inputChanged(): void { // Invalidate on the keystroke, not after the debounce window: the - // old query is cancelled and any open panel or confirmation closes. - const text = searchInput.text; + // old query is cancelled (its late replies are dropped) and any open + // panel or confirmation closes. The list is only blanked for an empty + // query: otherwise the last answer is re-ranked locally against the + // new text, or kept as shown, until the new answer arrives. + const text = searchInput.text.trim(); const raw = root.lastRaw || []; - const extends_ = root.lastQuery.length > 0 && text.startsWith(root.lastQuery); - const bare = extends_ ? root.lastBare + text.substring(root.lastQuery.length) : ""; + const shown = root.filtered.filter(r => r && r.id !== "pallet.error"); + const bare = QueryState.localBare(text, root.lastQuery, root.lastBare); root.invalidateSearch(); debounce.stop(); - if (root.visible && text.trim().length > 0) { - // Typing forward only narrows a fuzzy match, so re-rank the - // previous answer locally for an instant first paint. - if (extends_ && raw.length > 0) { - root.filtered = Fuzzy.rank(bare, raw); - root.statusText = qsTr("searching…"); - } else { - root.lastRaw = []; - root.lastQuery = ""; - } - debounce.restart(); - } else { + if (!root.visible || text.length === 0) { root.lastRaw = []; root.lastQuery = ""; + root.lastBare = ""; + return; } + const local = bare !== null && raw.length > 0 ? Fuzzy.rank(bare, raw) : []; + root.filtered = local.length > 0 ? local : shown; + root.statusText = qsTr("searching…"); + debounce.restart(); } function applyDeferred(): void { const update = root.deferredUpdate; @@ -216,6 +219,7 @@ PanelWindow { searchInput.text = ""; root.lastRaw = []; root.lastQuery = ""; + root.lastBare = ""; } function toggle(): void { visible ? close() : open(); } function runSearch(): void { diff --git a/shell/ProviderBridge.qml b/shell/ProviderBridge.qml index ab49d0b..321cd5d 100644 --- a/shell/ProviderBridge.qml +++ b/shell/ProviderBridge.qml @@ -5,9 +5,10 @@ import "QueryState.js" as QueryState Process { id: root - // meta: { final, pending, failed, incremental } — incremental is true for - // follow-up messages under the query id already delivered, so the window - // can merge without resetting the selection. + // meta: { final, pending, failed, incremental, query } — incremental is + // true for follow-up messages under the query id already delivered, so the + // window can merge without resetting the selection; query is the text + // sent for that id (not whatever the input holds when the reply lands). signal resultsReady(var results, string scope, string bare, var unresolved, var meta) signal hostError(string message) signal actionFailed(string message) @@ -19,6 +20,7 @@ Process { readonly property string hostPath: Quickshell.env("PALLET_HOST") ?? localHostPath property var queryState: QueryState.create() property string deliveredId: "" + property string activeQuery: "" property int actionCounter: 0 command: ["python3", hostPath] running: true @@ -38,7 +40,8 @@ Process { final: msg.final !== false, pending: Array.isArray(msg.pending) ? msg.pending : [], failed: Array.isArray(msg.failed) ? msg.failed : [], - incremental: incremental + incremental: incremental, + query: root.activeQuery }); } } else if (msg.executed === false) { @@ -68,6 +71,7 @@ Process { return; } const id = QueryState.begin(root.queryState); + root.activeQuery = query; root.write(JSON.stringify({ id: id, op: "search", query: query }) + "\n"); } // Overlay opened: let providers refresh caches before the first keystroke. diff --git a/shell/QueryState.js b/shell/QueryState.js index d4d79a9..342cd3c 100644 --- a/shell/QueryState.js +++ b/shell/QueryState.js @@ -21,3 +21,40 @@ function accepts(state, message) { && message !== null && typeof message === "object" && message.id === state.activeId && Array.isArray(message.results); } + +// Bare query to re-rank the last answer against while `text` is in flight. +// `sentQuery` is the query that answer was for and `sentBare` what the host +// matched (scope prefix stripped). Returns null when the scope prefix +// changed: the previous rows then cannot be re-ranked meaningfully. +function localBare(text, sentQuery, sentBare) { + const t = String(text || "").trim(); + const q = String(sentQuery || ""); + const b = String(sentBare || ""); + const prefixLen = q.length - b.length; + if (prefixLen === 0 && q === b) return t; + if (prefixLen < 0 || q.substring(prefixLen) !== b) return null; + const prefix = q.substring(0, prefixLen).replace(/\s+$/, ""); + return t.startsWith(prefix) ? t.substring(prefix.length).trim() : null; +} + +// Rows to rank for one host message. While a query is still streaming, rows +// from the previous answer are kept for providers that have not replied yet, +// so the list never loses them before the new answer lands (the caller +// re-ranks them against the new query). The final message is authoritative. +function carryOver(previous, results, pending, final) { + if (final || !Array.isArray(previous) || !Array.isArray(pending) || pending.length === 0) + return results; + const answered = {}; + for (let i = 0; i < results.length; i++) { + const r = results[i]; + if (r !== null && typeof r === "object") answered[String(r.provider || "")] = true; + } + const out = results.slice(); + for (let i = 0; i < previous.length; i++) { + const r = previous[i]; + if (r === null || typeof r !== "object") continue; + const name = String(r.provider || ""); + if (!answered[name] && pending.indexOf(name) !== -1) out.push(r); + } + return out; +} diff --git a/tests/lifecycle.cjs b/tests/lifecycle.cjs index 5d76d11..812f95e 100644 --- a/tests/lifecycle.cjs +++ b/tests/lifecycle.cjs @@ -32,7 +32,7 @@ const sandbox = {root, flashTimer: {stop() {}, restart() {}}, debounce: {stop() {stopped++;}, restart() {starts++;}}, searchInput: {text: 'new'}, qsTr: s => s, - Fuzzy: fuzzyContext + Fuzzy: fuzzyContext, QueryState: context }; function method(name) { const match = palette.match(new RegExp('function ' + name + '\\(\\): void \\{([\\s\\S]*?)\\n \\}')); @@ -45,7 +45,8 @@ const runSearch = method('runSearch'); inputChanged(); assert.equal(cancels, 1); assert.equal(resets, 1); -assert.equal(root.filtered.length, 0); +// Nothing to re-rank yet: the rows already shown stay until the answer lands. +assert.deepEqual(root.filtered, ['old']); assert.equal(root.panelOpen, false); assert.equal(starts, 1); assert.equal(searches, 0); @@ -53,6 +54,7 @@ runSearch(); assert.equal(searches, 1); sandbox.searchInput.text = ''; inputChanged(); +assert.equal(root.filtered.length, 0, 'an empty query blanks the list'); assert.equal(starts, 1); root.visible = false; sandbox.searchInput.text = 'hidden'; @@ -119,8 +121,72 @@ inputChanged(); assert.equal(cancels, cancelsBefore + 1); assert.deepEqual(Array.from(root.filtered, r => r.id), ['b']); assert.equal(root.panelOpen, false); -// Editing backwards clears instead of showing stale rows. +// Editing backwards re-ranks the same answer against the shorter text. +sandbox.searchInput.text = 'fi'; +inputChanged(); +assert.deepEqual(Array.from(root.filtered, r => r.id), ['b', 'c']); +// Nothing in the last answer matches: keep what is shown, never blank. sandbox.searchInput.text = 'xyz'; inputChanged(); -assert.equal(root.filtered.length, 0); +assert.deepEqual(Array.from(root.filtered, r => r.id), ['b', 'c']); +assert.equal(root.statusText, 'searching…'); + +// localBare follows scope prefixes of the query the answer was for. +assert.equal(context.localBare(' conf ', 'con', 'con'), 'conf'); +assert.equal(context.localBare('f: conf', 'f: con', 'con'), 'conf'); +assert.equal(context.localBare('f:conf', 'f: con', 'con'), 'conf'); +assert.equal(context.localBare('a: conf', 'f: con', 'con'), null); +assert.equal(context.localBare('co', '', ''), 'co'); +// carryOver keeps pending providers' previous rows until the final message. +const prev = [row('files', 'f1', 'config'), row('omarchy', 'o1', 'Config'), row('menus', 'm1', 'Configure')]; +let merged = context.carryOver(prev, [row('omarchy', 'o2', 'Config again')], ['files', 'menus'], false); +assert.deepEqual(Array.from(merged, r => r.id), ['o2', 'f1', 'm1']); +merged = context.carryOver(prev, [row('files', 'f2', 'config.bak')], ['files'], false); +assert.deepEqual(Array.from(merged, r => r.id), ['f2'], 'a provider that answered (even partially) is not carried'); +merged = context.carryOver(prev, [row('omarchy', 'o2', 'x')], ['files'], true); +assert.deepEqual(Array.from(merged, r => r.id), ['o2'], 'final messages are authoritative'); + +// Typing "con" -> "conf" -> "config": no blank frame, no stale rows after an +// answer, stale ids dropped, and the final view is not reshuffled by +// providers that answered late. +const bridgeState = context.create(); +let sent = null; +sandbox.bridge.search = q => { sent = {id: context.begin(bridgeState), q}; }; +sandbox.bridge.cancel = () => { cancels++; context.cancel(bridgeState); }; +function hostReply(id, q, results, pending) { + const msg = {id, results}; + if (!context.accepts(bridgeState, msg)) return false; + receive(results, '', q, [], {final: pending.length === 0, pending, failed: [], incremental: false, query: q}); + return true; +} +root.lastRaw = []; root.lastQuery = ''; root.lastBare = ''; root.filtered = []; +const omarchy = [row('omarchy', 'cfg', 'Config'), row('omarchy', 'upd', 'Update Config: Tmux')]; +const files = [row('files', 'gh', 'config'), row('files', 'dot', '.config')]; +const apps = [row('apps', 'cons', 'Console')]; +sandbox.searchInput.text = 'con'; inputChanged(); runSearch(); +const conId = sent.id; +assert(hostReply(conId, 'con', omarchy.concat(apps), ['files'])); +assert(hostReply(conId, 'con', omarchy.concat(files, apps), [])); +const conRows = Array.from(root.filtered, r => r.id); +sandbox.searchInput.text = 'conf'; inputChanged(); +assert(root.filtered.length > 0, 'no blank frame while "conf" is in flight'); +assert(!Array.from(root.filtered, r => r.id).includes('cons'), 'local re-rank drops rows that no longer match'); +assert(!hostReply(conId, 'con', apps, []), 'a stale reply for the old id is dropped'); +runSearch(); +const confId = sent.id; +// The fast provider answers first; files is still pending and keeps its rows. +assert(hostReply(confId, 'conf', omarchy, ['files'])); +assert.deepEqual(Array.from(root.filtered, r => r.provider).sort(), ['files', 'files', 'omarchy', 'omarchy']); +assert(hostReply(confId, 'conf', omarchy.concat(files), [])); +const confFinal = Array.from(root.filtered, r => r.id); +sandbox.searchInput.text = 'config'; inputChanged(); +const configLocal = Array.from(root.filtered, r => r.id); +assert.deepEqual(configLocal.slice().sort(), confFinal.slice().sort(), 'same rows, re-ranked for "config"'); +runSearch(); +assert(hostReply(sent.id, 'config', omarchy, ['files'])); +assert.deepEqual(Array.from(root.filtered, r => r.id), configLocal, 'partial merge does not reorder'); +assert(hostReply(sent.id, 'config', omarchy.concat(files), [])); +assert.deepEqual(Array.from(root.filtered, r => r.id), configLocal, 'final does not reorder'); +assert.equal(root.lastQuery, 'config'); +assert.notDeepEqual(conRows, []); console.log('Query state and extracted QML JavaScript checks passed (not a native QML test)'); From 911196e34ac6cc2235cf54069e3bc186b79c6889 Mon Sep 17 00:00:00 2001 From: Grok Bot Date: Thu, 29 Oct 2026 17:16:09 -0400 Subject: [PATCH 17/19] ranking: rank file rows below command matches unless the query has file intent For plain-word queries the files provider's generic folders (~/.config, ~/.config/ghostty/config, ~/.local/state/omarchy/current/theme) outranked Omarchy menu rows: an exact file name was tier 5 plus an exact-file bonus, above 'Update Config: ...' (word tier) and tied-or-above 'Config'. Without file intent, file rows now form a band [2.0, 2.4) below every command row at substring tier or better and above command subsequence matches; their tier order is kept inside the band. File intent ('/', '~', '.') is unchanged. The host only merges in provider order, so no host change is needed. Tests: ranking.cjs covers config, theme, equal exact matches for omarchy, system and apps rows, the subsequence/fallback floor and file intent. --- CHANGELOG.md | 7 +++++++ docs/PROVIDERS.md | 4 ++++ shell/Fuzzy.js | 17 +++++++++++++++-- tests/lifecycle.cjs | 2 +- tests/ranking.cjs | 40 ++++++++++++++++++++++++++++++++++++++++ 5 files changed, 67 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d75ee0d..1e9aaaa 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -89,6 +89,13 @@ not yet exercised in the real Quickshell overlay on Hyprland. message is authoritative. The re-rank now uses the query text that was sent for an answer, not the input text at the moment the reply landed. Replies for old query ids are still dropped. +- Ranking: without file intent (`/`, `~`, `.`), file and folder rows sit in + their own band below every command row that matches at substring tier or + better (still above weak subsequence matches; their own tier order is kept + inside the band). "config" now lists Omarchy's Config, Update Config and + Update Config: … rows before ~/.config/ghostty/config, and "theme" lists + the Theme menu row before ~/.local/state/omarchy/current/theme. An + exact-named file no longer gets a bonus over an exact-named command. ## [0.9.1] diff --git a/docs/PROVIDERS.md b/docs/PROVIDERS.md index 2749e1c..1902dd1 100644 --- a/docs/PROVIDERS.md +++ b/docs/PROVIDERS.md @@ -59,6 +59,10 @@ replies `executed`/`error`), and `{"op":"record","provider":"apps","result":"app (`$XDG_STATE_HOME/pallet/history.json`; `PALLET_HISTORY=0` disables it; `run` rows are never recorded). It only reorders rows within the same match tier (exact > prefix > word > substring > subsequence). +- Rows from the `files` provider rank below every other row that matches at + substring tier or better unless the query has file intent (`/`, `~` or + `.`), so an exact-named Omarchy, system or app row beats an equally named + file or folder. ## Action types diff --git a/shell/Fuzzy.js b/shell/Fuzzy.js index cc71c99..7162a4d 100644 --- a/shell/Fuzzy.js +++ b/shell/Fuzzy.js @@ -9,7 +9,10 @@ var TIER_PREFIX = 4; var TIER_WORD = 3; var TIER_SUBSTRING = 2; var TIER_SUBSEQUENCE = 1; -var FALLBACK_RANK = -10; // "Run: " and other fallbacks: always last. +var FALLBACK_RANK = -10; +// Width of one tier inside the command-intent file band: [2.0, 2.4) covers +// file substring..exact, below any command substring row (>= 2.45). +var FILE_BAND_STEP = 0.1; // "Run: " and other fallbacks: always last. // Subsequence match. Returns -1 when query is not a subsequence of text, // otherwise 0..1 (tighter, earlier matches score higher). @@ -122,7 +125,9 @@ function isFallback(r) { // Client-side pass over merged provider results: drop non-matches, order by // match tier (exact > prefix > word boundary > substring > subsequence), then // within a tier blend match quality, the provider's advisory score, intent -// (files vs commands), and a bounded recency boost. Fallback rows such as +// (files vs commands), and a bounded recency boost. Without file intent +// (no "/", "~" or "."), file rows rank below command rows that match at +// substring or better, whatever their own tier. Fallback rows such as // "Run: " always sort last. The sort is stable: ties keep the host's // provider order. function rank(query, results) { @@ -160,6 +165,14 @@ function rank(query, results) { out.fuzzy = m.quality; out.tier = m.tier; out.rank = m.tier + within; + if (isFile && !wantsFile && m.tier >= TIER_SUBSTRING) { + // Command intent: file and folder rows form their own band below + // every command row matching at substring or better (so Omarchy + // "Config" or "Update Config: …" beat ~/.config/ghostty/config), + // still above command subsequence matches. Inside the band the + // usual tier order holds. + out.rank = TIER_SUBSTRING + FILE_BAND_STEP * (m.tier - TIER_SUBSTRING) + FILE_BAND_STEP * 0.9 * within; + } scored.push(out); } scored.sort(function (a, b) { return (b.rank - a.rank) || (a.order - b.order); }); diff --git a/tests/lifecycle.cjs b/tests/lifecycle.cjs index 812f95e..8baa11e 100644 --- a/tests/lifecycle.cjs +++ b/tests/lifecycle.cjs @@ -90,7 +90,7 @@ assert.equal(root.selectedIndex, 0); // The user moves to "fire drill"; a better straggler arrives and ranks first. root.selectedIndex = 1; root.userMoved = true; const selected = root.filtered[1].id; -receive([row('apps', 'b', 'firewall'), row('apps', 'c', 'fire drill'), row('files', 'a', 'fire')], '', 'fire', [], +receive([row('apps', 'b', 'firewall'), row('apps', 'c', 'fire drill'), row('menus', 'a', 'fire')], '', 'fire', [], {final: true, pending: [], failed: [], incremental: true}); assert.equal(root.filtered[0].id, 'a', 'exact match ranks first'); assert.equal(root.filtered[root.selectedIndex].id, selected, 'selection follows the same result'); diff --git a/tests/ranking.cjs b/tests/ranking.cjs index 95433ad..c3d953a 100644 --- a/tests/ranking.cjs +++ b/tests/ranking.cjs @@ -65,6 +65,46 @@ assert.deepEqual(ids(out), ['b', 'a']); out = ctx.rank('files', [r('a', 'Files'), r('b', 'Files manager', {recent: 1})]); assert.deepEqual(ids(out), ['a', 'b']); +// Command intent beats generic file/folder rows, whatever their own tier: +// "config" puts the Omarchy menu rows above ~/.config/ghostty/config. +const om = (id, title, subtitle, keywords, score) => + r(id, title, {provider: 'omarchy', subtitle, keywords, score}); +const file = (id, title, parent, score = 0.97) => + r(id, title, {provider: 'files', subtitle: parent, keywords: parent.replace(/^~\//, '').replace(/\//g, ' ').toLowerCase(), score}); +out = ctx.rank('config', [ + r('app', 'Console', {provider: 'apps'}), + file('ghostty', 'config', '~/.config/ghostty'), + file('dotconfig', '.config', '~'), + file('hyprconf', 'hyprland.conf', '~/.config/hypr'), + om('upd-hypr', 'Update Config: Hyprland', 'Omarchy › Update › Config', 'update config hyprland', 0.82), + om('upd-tmux', 'Update Config: Tmux', 'Omarchy › Update › Config', 'update config tmux', 0.82), + om('setup', 'Config', 'Open menu: Omarchy › Setup › Config', 'setup config', 0.807), + om('upd', 'Update Config', 'Open menu: Omarchy › Update › Config', 'update config', 0.697), +]); +assert.deepEqual(ids(out).slice(0, 4), ['setup', 'upd-hypr', 'upd-tmux', 'upd']); +assert.deepEqual(ids(out).slice(4, 6), ['ghostty', 'dotconfig'], 'files keep their own order below'); +assert(ids(out).indexOf('ghostty') > ids(out).indexOf('upd')); +// "theme": the Theme menu row beats the omarchy/current/theme folder. +out = ctx.rank('theme', [ + file('curtheme', 'theme', '~/.local/state/omarchy/current'), + om('theme', 'Theme', 'Omarchy › Appearance', 'appearance theme themes', 0.95), + om('remove', 'Remove Theme', 'Omarchy › Remove', 'remove theme', 0.82), +]); +assert.deepEqual(ids(out), ['theme', 'remove', 'curtheme']); +// Equal exact match: Omarchy, system and app rows beat a file even when the +// file's advisory score is higher; host order does not matter. +for (const provider of ['omarchy', 'system', 'apps']) { + out = ctx.rank('notes', [file('f', 'notes', '~/Documents', 1), r('c', 'Notes', {provider, score: 0})]); + assert.deepEqual(ids(out), ['c', 'f'], provider); +} +// File rows still beat weak (subsequence) command matches, and fallbacks +// still sort last. +out = ctx.rank('config', [r('run', 'Run: config', {provider: 'run'}), r('weak', 'Color Night Filter Guide'), file('ghostty', 'config', '~/.config/ghostty')]); +assert.deepEqual(ids(out), ['ghostty', 'weak', 'run']); +// File intent keeps files on top. +out = ctx.rank('.config', [om('setup', 'Config', 'Open menu: Omarchy › Setup › Config', 'setup config', 0.8), file('dotconfig', '.config', '~')]); +assert.equal(out[0].id, 'dotconfig'); + // Selection helpers. const list = ctx.rank('a', [r('1', 'a', {provider: 'x'}), r('2', 'ab', {provider: 'y'})]); assert.equal(ctx.indexOfKey(list, ctx.keyOf({provider: 'y', id: '2'})), 1); From 3702f82250b192100a60981de1c95d118ba1ed2c Mon Sep 17 00:00:00 2001 From: Grok Bot Date: Thu, 29 Oct 2026 17:16:26 -0400 Subject: [PATCH 18/19] shell: Esc on a confirmation card returns straight to the results list The first Esc on e.g. the Shutdown confirmation only cancelled the card and left the Actions panel (Run, Copy command) open, needing a second Esc. close() now cancels the confirmation, closes the panel and applies any deferred streamed update in one step. Test in lifecycle.cjs. --- CHANGELOG.md | 3 +++ shell/PaletteWindow.qml | 10 ++++++++-- tests/lifecycle.cjs | 20 ++++++++++++++++++++ 3 files changed, 31 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1e9aaaa..5a48ccf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -96,6 +96,9 @@ not yet exercised in the real Quickshell overlay on Hyprland. Update Config: … rows before ~/.config/ghostty/config, and "theme" lists the Theme menu row before ~/.local/state/omarchy/current/theme. An exact-named file no longer gets a bonus over an exact-named command. +- Esc on a confirmation card (for example Shutdown) cancels it and returns + straight to the results list instead of to the Actions panel; a second + Esc closes the overlay. ## [0.9.1] diff --git a/shell/PaletteWindow.qml b/shell/PaletteWindow.qml index 38492e0..929a065 100644 --- a/shell/PaletteWindow.qml +++ b/shell/PaletteWindow.qml @@ -208,8 +208,14 @@ PanelWindow { } function open(): void { visible = true; scopeLabel = "All"; } function close(): void { - if (actionPanel.confirming) { actionPanel.cancelConfirm(); return; } - if (root.panelOpen) { root.panelOpen = false; root.applyDeferred(); return; } + // Esc on a confirmation card or the action list cancels it and goes + // straight back to the results (not from the card to the actions). + if (actionPanel.confirming || root.panelOpen) { + root.panelOpen = false; + actionPanel.reset(); + root.applyDeferred(); + return; + } dismiss(); } function dismiss(): void { diff --git a/tests/lifecycle.cjs b/tests/lifecycle.cjs index 8baa11e..18bc55e 100644 --- a/tests/lifecycle.cjs +++ b/tests/lifecycle.cjs @@ -189,4 +189,24 @@ assert(hostReply(sent.id, 'config', omarchy.concat(files), [])); assert.deepEqual(Array.from(root.filtered, r => r.id), configLocal, 'final does not reorder'); assert.equal(root.lastQuery, 'config'); assert.notDeepEqual(conRows, []); + +// Esc on a confirmation card returns straight to the results list, cancelling +// the confirmation; a second Esc then closes the overlay. +{ + let confirming = true, cancelled = 0, resetCount = 0, dismissed = 0, replayed = 0; + const panel = {get confirming() { return confirming; }, cancelConfirm() { cancelled++; confirming = false; }, + reset() { resetCount++; confirming = false; }}; + const escBox = Object.assign({}, sandbox, {actionPanel: panel, root: {panelOpen: true, + applyDeferred() { replayed++; }}, dismiss() { dismissed++; }}); + const m = palette.match(/function close\(\): void \{([\s\S]*?)\n \}/); + assert(m, 'missing QML method: close'); + const close = vm.runInNewContext('(function() {' + m[1] + '\n})', escBox); + close(); + assert.equal(confirming, false, 'confirmation cancelled'); + assert.equal(escBox.root.panelOpen, false, 'action panel closed, results list shown'); + assert.equal(replayed, 1, 'deferred streamed update applied'); + assert.equal(dismissed, 0, 'overlay stays open'); + close(); + assert.equal(dismissed, 1, 'second Esc closes the overlay'); +} console.log('Query state and extracted QML JavaScript checks passed (not a native QML test)'); From 2ff8cd64e43ac0662e22a387e3be57f830130208 Mon Sep 17 00:00:00 2001 From: Grok Bot Date: Thu, 29 Oct 2026 17:17:46 -0400 Subject: [PATCH 19/19] tests: make the three timing tests tolerant of slow machines - tests/timing.py: budget(seconds) scales by PALLET_TEST_TIME_SCALE or a factor calibrated from 'python3 -c pass' start-up (never below 1). - test_menus: the no-gi provider test closed stdin right after the query; with a focused window from hyprctl (an Omarchy VM) the reply comes from the walk worker and the provider exited first (IndexError on empty stdout). Keep stdin open until the first reply, with a scaled timeout. - test_omarchy: guard timeout uses budget(1.5) and a 'sleep 60' guard, so the fast guard has time to answer and the slow one is always cut. - test_streaming: warm the host and providers up first, then assert the first emit has only the fast row, is not final, lists slow as pending and lands before the slow reply, plus a scaled 0.3 s bound; a slow reply after the merge deadline is accepted via the late follow-up. --- CHANGELOG.md | 9 +++++++++ README.md | 4 ++++ tests/test_menus.py | 25 ++++++++++++++++++++----- tests/test_omarchy.py | 5 ++++- tests/test_streaming.py | 35 ++++++++++++++++++++++++++--------- tests/timing.py | 34 ++++++++++++++++++++++++++++++++++ 6 files changed, 97 insertions(+), 15 deletions(-) create mode 100644 tests/timing.py diff --git a/CHANGELOG.md b/CHANGELOG.md index 5a48ccf..4fd0a6c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -99,6 +99,15 @@ not yet exercised in the real Quickshell overlay on Hyprland. - Esc on a confirmation card (for example Shutdown) cancels it and returns straight to the results list instead of to the Actions panel; a second Esc closes the overlay. +- Timing tests no longer fail on slow machines (TCG VM): `tests/timing.py` + scales time limits by `PALLET_TEST_TIME_SCALE` or a factor calibrated + from Python start-up time (never below 1). The menus no-accessibility test + keeps the provider's stdin open until it replies (closing it first lost + the walk worker's answer whenever hyprctl reported a focused window); the + guard-timeout test uses a scaled budget and a guard that always outlives + it; the streaming test warms the host up first and checks the first emit + against the slow provider's reply (fast row only, not final, `slow` + pending, earlier than the slow reply) as well as a scaled 0.3 s bound. ## [0.9.1] diff --git a/README.md b/README.md index 54ad54f..c34266f 100644 --- a/README.md +++ b/README.md @@ -40,6 +40,10 @@ python3 scripts/check_release.py python3 -m unittest discover -s tests -v ``` +Timing tests scale their time limits on slow machines (calibrated from +Python start-up time); force a factor with `PALLET_TEST_TIME_SCALE=5` on a +TCG VM or loaded CI runner. + Host smoke test (does not execute result actions): ```sh diff --git a/tests/test_menus.py b/tests/test_menus.py index 229fb6c..b5b3024 100644 --- a/tests/test_menus.py +++ b/tests/test_menus.py @@ -3,6 +3,7 @@ import json import os from pathlib import Path +import select import subprocess import sys import threading @@ -10,6 +11,8 @@ import unittest ROOT = Path(__file__).resolve().parents[1] +sys.path.insert(0, str(ROOT / 'tests')) +from timing import budget # noqa: E402 SPEC = importlib.util.spec_from_file_location('menus_provider', ROOT / 'providers/menus/provider.py') menus = importlib.util.module_from_spec(SPEC) sys.modules['menus_provider'] = menus @@ -140,11 +143,23 @@ def test_provider_stays_up_and_answers_without_accessibility(self): env = dict(os.environ, PATH='/usr/bin:/bin', DBUS_SESSION_BUS_ADDRESS='unix:path=/nonexistent') code = ("import sys; sys.modules['gi'] = None\n" "import runpy; runpy.run_path(%r, run_name='__main__')" % str(ROOT / 'providers/menus/provider.py')) - proc = subprocess.run([sys.executable, '-c', code], input=json.dumps({'id': 'q1', 'op': 'search', 'query': 'save'}) + '\n', - text=True, capture_output=True, timeout=10, env=env) - self.assertEqual(proc.returncode, 0, proc.stderr) - self.assertEqual(json.loads(proc.stdout.splitlines()[0]), {'id': 'q1', 'results': []}) - + # Keep stdin open until the reply: on a desktop where hyprctl reports + # a focused window, the answer comes from the walk worker thread, and + # closing stdin first would end the provider before it can reply (the + # host never closes a provider's stdin mid-query). + proc = subprocess.Popen([sys.executable, '-c', code], stdin=subprocess.PIPE, stdout=subprocess.PIPE, + stderr=subprocess.PIPE, text=True, env=env) + try: + proc.stdin.write(json.dumps({'id': 'q1', 'op': 'search', 'query': 'save'}) + '\n'); proc.stdin.flush() + ready, _, _ = select.select([proc.stdout], [], [], budget(10)) + self.assertTrue(ready, 'menus provider did not answer') + first = proc.stdout.readline() + finally: + proc.stdin.close() + proc.wait(timeout=budget(10)) + stderr = proc.stderr.read(); proc.stdout.close(); proc.stderr.close() + self.assertEqual(proc.returncode, 0, stderr) + self.assertEqual(json.loads(first), {'id': 'q1', 'results': []}) if __name__ == '__main__': unittest.main() diff --git a/tests/test_omarchy.py b/tests/test_omarchy.py index 918cbc8..0ec366c 100644 --- a/tests/test_omarchy.py +++ b/tests/test_omarchy.py @@ -9,6 +9,8 @@ import unittest ROOT = Path(__file__).resolve().parents[1] +sys.path.insert(0, str(ROOT / 'tests')) +from timing import budget # noqa: E402 SPEC = importlib.util.spec_from_file_location('omarchy_provider', ROOT / 'providers/omarchy/provider.py') om = importlib.util.module_from_spec(SPEC) sys.modules['omarchy_provider'] = om @@ -162,7 +164,8 @@ def test_guard_that_exits_leaves_row_unanswered(self): self.assertEqual(out, {}) def test_guard_timeout_returns_partial(self): - out = om.run_guards([(0, 'w', 'true'), (1, 'w', 'sleep 5')], timeout=1.0) + # The slow guard always outlives the (scaled) budget; it is killed. + out = om.run_guards([(0, 'w', 'true'), (1, 'w', 'sleep 60')], timeout=budget(1.5)) self.assertEqual(out.get((0, 'w')), True) self.assertNotIn((1, 'w'), out) diff --git a/tests/test_streaming.py b/tests/test_streaming.py index 1050e12..df1c00a 100644 --- a/tests/test_streaming.py +++ b/tests/test_streaming.py @@ -14,6 +14,8 @@ from unittest.mock import patch ROOT = Path(__file__).resolve().parents[1] +sys.path.insert(0, str(ROOT / 'tests')) +from timing import budget # noqa: E402 SPEC = importlib.util.spec_from_file_location('streaming_host', ROOT / 'src/host.py') host_module = importlib.util.module_from_spec(SPEC) SPEC.loader.exec_module(host_module) @@ -164,24 +166,39 @@ def test_first_results_do_not_wait_for_slow_provider(self): (d / 'manifest.json').write_text(json.dumps({'name': name, 'exec': [sys.executable, str(d / 'w.py')]})) proc = subprocess.Popen([sys.executable, str(root / 'src/host.py')], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.DEVNULL, text=True) - try: - time.sleep(0.3) + + def run(qid): + """Send one search; return [(seconds, msg)] up to the first message with both rows.""" start = time.monotonic() - proc.stdin.write(json.dumps({'id': 'q1', 'op': 'search', 'query': 'x'}) + '\n'); proc.stdin.flush() + proc.stdin.write(json.dumps({'id': qid, 'op': 'search', 'query': 'x'}) + '\n'); proc.stdin.flush() stamps = [] while True: - ready, _, _ = select.select([proc.stdout], [], [], 3) + ready, _, _ = select.select([proc.stdout], [], [], budget(3)) self.assertTrue(ready, 'host did not answer') msg = json.loads(proc.stdout.readline()) + if msg.get('id') != qid: + continue stamps.append((time.monotonic() - start, msg)) - if msg.get('final'): - break + if 'slow' in [r['title'] for r in msg['results']]: + return stamps + try: + # Warm-up: wait until both providers are up and have answered + # once, so start-up cost is not measured as streaming latency. + run('q0') + stamps = run('q1') finally: - proc.stdin.close(); proc.wait(timeout=5); proc.stdout.close() + proc.stdin.close(); proc.wait(timeout=budget(5)); proc.stdout.close() first_t, first = stamps[0] - self.assertLess(first_t, 0.3) + slow_t, last = stamps[-1] + # The first emit carries only the fast row, is not final, and arrives + # before the slow provider's reply: it did not wait for it. self.assertEqual([r['title'] for r in first['results']], ['fast']) - self.assertEqual([r['title'] for r in stamps[-1][1]['results']], ['fast', 'slow']) + self.assertIs(first['final'], False) + self.assertEqual(first['pending'], ['slow']) + self.assertLess(first_t, slow_t) + self.assertLess(first_t, budget(0.3)) + self.assertEqual([r['title'] for r in last['results']], ['fast', 'slow']) + self.assertIs(last['final'], True) if __name__ == '__main__': diff --git a/tests/timing.py b/tests/timing.py new file mode 100644 index 0000000..20e36a7 --- /dev/null +++ b/tests/timing.py @@ -0,0 +1,34 @@ +"""Time budgets for tests that measure real subprocess timing. + +Slow machines (a TCG VM, a loaded CI box) can set PALLET_TEST_TIME_SCALE, +for example 5. The scale is also calibrated once from this Python's own +start-up time, so budgets grow by themselves where every process is slow. +It is never below 1, so fast machines keep the original budgets; only the +time limits scale, never what a test checks. +""" +import os +import subprocess +import sys +import time + +REFERENCE_STARTUP_S = 0.05 # `python3 -c pass` on an ordinary laptop +MAX_SCALE = 20.0 +_scale = None + + +def scale() -> float: + global _scale + if _scale is None: + try: + forced = float(os.environ.get('PALLET_TEST_TIME_SCALE', '') or 0) + except ValueError: + forced = 0.0 + start = time.monotonic() + subprocess.run([sys.executable, '-c', 'pass'], check=False) + measured = min(MAX_SCALE, (time.monotonic() - start) / REFERENCE_STARTUP_S) + _scale = max(1.0, forced, measured) + return _scale + + +def budget(seconds: float) -> float: + return seconds * scale()