Deduplicate session environment exports
ci / test (push) Canceled after 0s

Append each session export once under the file lock, refusing links and unsafe files. Keep the product at 1948 lines.
This commit is contained in:
swaits committed 2026-11-25 11:46:13 -07:00
1 parent 712cc75c12
commit 8fe62f6ad9
3 files changed
+36 -8

No files matched your search

+1 -1
View File
@@ -119,7 +119,7 @@ def tick(p):
worker, free = worker or bool(s.get("worker")), s.get("free_bytes")
env = os.environ.get("CLAUDE_ENV_FILE")
if env and re.fullmatch(r"[A-Za-z0-9_-]+", str(sid)):
store.append(env, "export SISI_SESSION_ID=%s" % sid) # so `sisi retro` knows its session
store.append(env, "export SISI_SESSION_ID=%s" % sid, once=True) # so `sisi retro` knows its session
if not os.path.exists(os.path.join(store.DATA, "generated", "core.md")):
rules.build_rulebook()
text = "\n".join(x for x in (rules.project_text(cwd), rules.due(cwd, claim=True), models.stale(), "" if worker else _faults(cwd, False, free)) if x)
+7 -7
View File
@@ -17,7 +17,6 @@ def _where(own, xdg, fallback):
base = os.environ.get(xdg)
return os.environ.get(own) or (os.path.join(base, "sisi") if base else os.path.join(HOME, *fallback, "sisi"))
DATA = _where("SISI_DATA", "XDG_DATA_HOME", (".local", "share"))
STATE = _where("SISI_STATE", "XDG_STATE_HOME", (".local", "state"))
LIB = os.environ.get("SISI_LIB") or os.path.join(HOME, ".local", "lib", "sisi")
@@ -27,7 +26,6 @@ DEFAULTS = {"gate": True, "meter": True, "direct_calls": 8, "direct_extension":
"rules_lines": 30, "rules_chars": 4000, "organize_after": 8, "research_days": 7, "shell_workers": False,
"worker_minutes": 15, "retro_minutes": 5, "disk_free_percent": 20, "disk_drop_percent": 5, "checkouts": 8}
def config():
cfg = dict(DEFAULTS)
try:
@@ -79,11 +77,13 @@ def write(path, text):
raise
def append(path, line):
os.makedirs(os.path.dirname(path), exist_ok=True)
with open(path, "a", encoding="utf-8") as f:
fcntl.flock(f, fcntl.LOCK_EX)
f.write(line.rstrip("\n") + "\n")
def append(path, line, once=False):
line = line.rstrip("\n").encode() + b"\n"
with contextlib.suppress(OSError) if once else contextlib.nullcontext():
os.makedirs(os.path.dirname(path), exist_ok=True)
with open(path, "a+b", opener=lambda p, f: os.open(p, f | os.O_NOFOLLOW)) as f:
fcntl.flock(f, fcntl.LOCK_EX); f.seek(0); raw = f.read(64001)
(not once or len(raw) <= 64000 and b"\0" not in raw and line[:-1] not in raw.splitlines()) and f.write(line)
def git(args, cwd, timeout=15):
+28
View File
@@ -2091,6 +2091,34 @@ cli.main(["tick"])
self.assertEqual(self.sisi(name, stdin="not json")[0], 0, name)
self.assertEqual(self.sisi(name, stdin=json.dumps({"session_id": "s1", "cwd": "/no/such/place"}))[0], 0, name)
def test_session_start_exports_each_session_once(self):
env = self.write(os.path.join(self.tmp, "env"), "")
self.env["CLAUDE_ENV_FILE"] = env
for _ in range(3):
self.assertEqual(self.hook("tick", sid="s1")[0], 0)
for _ in range(3):
self.assertEqual(self.hook("tick", sid="s2")[0], 0)
self.assertEqual(self.read(env), "export SISI_SESSION_ID=s1\nexport SISI_SESSION_ID=s2\n")
def test_session_start_leaves_unsafe_environment_files_alone(self):
missing = os.path.join(self.tmp, "new-env")
self.env["CLAUDE_ENV_FILE"] = missing
self.assertEqual(self.hook("tick")[0], 0)
self.assertEqual(self.read(missing), "export SISI_SESSION_ID=s1\n")
unreadable = self.write(os.path.join(self.tmp, "unreadable-env"), "keep\n")
os.chmod(unreadable, 0)
self.env["CLAUDE_ENV_FILE"] = unreadable
self.assertEqual(self.hook("tick", sid="unreadable")[0], 0)
os.chmod(unreadable, 0o600)
self.assertEqual(self.read(unreadable), "keep\n")
target = self.write(os.path.join(self.tmp, "target-env"), "keep\n")
link = os.path.join(self.tmp, "linked-env")
os.symlink(target, link)
self.env["CLAUDE_ENV_FILE"] = link
self.assertEqual(self.hook("tick", sid="linked")[0], 0)
self.assertEqual(self.read(target), "keep\n")
self.assertTrue(os.path.islink(link))
# any agent in any harness
def watch(self, fields, sid="s1"):