Something went wrong. Try again.
small gleam coding and persistent agent daemon with a detachable cli and a canvas ui forked from okami.mom/albedo
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116"""run() jobs get the .envrc direnv would load for the kernel's folder, andonly when direnv allows it, as in the person's shell.
A blocked file does not load and the cell says so without telling the modelto allow it; once allowed, the next job starts in its environment (PATH_addincluded, so its tools are found by name); an edit blocks it again; leavingthe folder unloads it; a denied file stays quiet. Runs the real direnv, withits allow list in the test's own XDG directories."""
import jsonimport osimport shutilimport subprocessimport unittest
from harness import Albedo, Provider, exclusive, python, text
ENVRC = "export ALBEDO_DIRENV_PROBE=from-envrc\nPATH_add bin\n"PROBE = '#!/bin/sh\necho "probe:${ALBEDO_DIRENV_PROBE-unset}"\n'
@exclusive@unittest.skipUnless(shutil.which("direnv"), "needs direnv")class DirenvTests(unittest.TestCase): def setUp(self): self.cells = []
def script(request): if request["messages"][-1].get("role") == "user" and self.cells: return python(self.cells.pop(0)) return text("ok")
def prepare(app): # direnv's allow list and config live here, never the developer's for name in ("XDG_DATA_HOME", "XDG_CONFIG_HOME", "XDG_CACHE_HOME"): app.env[name] = str(app.root / name.lower())
self.provider = Provider(script) self.addCleanup(self.provider.close) self.app = Albedo(self.provider, prepare=prepare) self.app.__enter__() self.addCleanup(self.app.__exit__, None, None, None)
def direnv(self, *args): subprocess.run( ["direnv", *args], env=self.app.env, check=True, capture_output=True )
def cell(self, session, code): self.cells.append(code) self.app.prompt(session, "run it").close() self.app.idle(session) results = [ json.loads(part["value"]) for entry in self.app.history(session)["items"] if entry["kind"] == "tool_result" and entry["tool"]["name"] == "python" for part in entry["content"] if part["kind"] == "json" and part["field"] == "result" ] self.assertEqual(results[-1]["status"], "ok", results[-1]) return results[-1]["output"].strip()
def job(self, session, program): return self.cell( session, f"job = run({program!r})\nawait job\nprint(job.tail().strip())" )
def test_jobs_load_the_envrc_direnv_allows(self): workspace = self.app.root / "project" (workspace / "bin").mkdir(parents=True) envrc = workspace / ".envrc" envrc.write_text(ENVRC) probe = workspace / "bin" / "probe" probe.write_text(PROBE) probe.chmod(0o755) session = self.app.session(workspace)
output = self.job(session, str(probe)) self.assertIn("is not allowed, so jobs start without it", output) self.assertNotIn("direnv allow", output) self.assertTrue(output.endswith("probe:unset"), output)
self.direnv("allow", str(envrc)) output = self.job(session, "probe") self.assertIn("direnv: loading", output) self.assertTrue(output.endswith("probe:from-envrc"), output) # unchanged, the environment stays and direnv has nothing to say self.assertEqual(self.job(session, "probe"), "probe:from-envrc")
# direnv stores watched mtimes at one-second resolution. Ensure this # edit is observable rather than accidentally reusing the prior tick. watched_mtime = envrc.stat().st_mtime envrc.write_text(ENVRC + "export SECOND=1\n") os.utime(envrc, (watched_mtime + 2, watched_mtime + 2)) output = self.job(session, str(probe)) self.assertIn("is not allowed", output) self.assertTrue(output.endswith("probe:unset"), output)
self.direnv("allow", str(envrc)) self.assertTrue(self.job(session, "probe").endswith("probe:from-envrc")) elsewhere = self.app.root / "elsewhere" elsewhere.mkdir() output = self.cell( session, f"os.chdir({str(elsewhere)!r})\njob = run({str(probe)!r})\nawait job\nprint(job.tail().strip())", ) self.assertEqual(output, "direnv: unloading\nprobe:unset")
self.direnv("deny", str(envrc)) output = self.cell( session, f"os.chdir({str(workspace)!r})\njob = run({str(probe)!r})\nawait job\nprint(job.tail().strip())", ) self.assertEqual(output, "probe:unset")