From 70d105fee42e0186131ee330c559cdc4dd9d0508 Mon Sep 17 00:00:00 2001 From: Drake Stapleton Date: Sun, 25 Oct 2026 17:14:39 -0500 Subject: [PATCH 1/2] test(tkad): fixture for directory replacement after open A6 asked for the one cross-process case the suites did not cover: the directory holding the store is renamed away and a fresh directory takes its path while the daemon is running. The daemon opens the directory once, checks it, takes the lock on it and chdirs into it, so every later syscall resolves through the held descriptor. The fixture pins that end to end: rename the store directory, create a replacement of the same name, then serve and commit through the live daemon. The commit must land beside the opened descriptor, the replacement directory must stay empty, the lock must still exclude a second store on the inode whatever its path is now called, and a resume in the replacement directory must refuse rather than serve a fresh revision as if it were this store. Restarting on the moved directory must still report the committed revision. It also records an observation the frame work has to answer: the store carries no identity word, so the replacement path accepts an independent empty store with its own Revision 0 while the opened one is still live. Nothing in the current design can tell a reconnecting client that its path now names a different store. (cherry picked from commit 82cee30062e2827756c3f3aba129c29bec7803fc) --- cmd/tkad/test/directory.py | 140 +++++++++++++++++++++++++++++++++++++ cmd/tkad/test/run.sh | 1 + 2 files changed, 141 insertions(+) create mode 100755 cmd/tkad/test/directory.py diff --git a/cmd/tkad/test/directory.py b/cmd/tkad/test/directory.py new file mode 100755 index 0000000..915bd61 --- /dev/null +++ b/cmd/tkad/test/directory.py @@ -0,0 +1,140 @@ +#!/usr/bin/env python3 +# SPDX-License-Identifier: Apache-2.0 +"""Directory replacement after open: the held descriptor, not the path, is the store.""" +import json +import os +import pathlib +import socket +import struct +import subprocess +import sys +import tempfile +import time + +BIN = pathlib.Path(sys.argv[1]).resolve() +FIX = json.loads(pathlib.Path(__file__).with_name("fixture.json").read_text()) +checks = 0 + + +def check(value, message): + global checks + assert value, message + checks += 1 + + +def exact(sock, count): + result = b"" + while len(result) < count: + part = sock.recv(count - len(result)) + if not part: + raise AssertionError("truncated daemon reply") + result += part + return result + + +class Daemon: + def __init__(self, directory, initialize=False): + self.directory = pathlib.Path(directory) + self.p = subprocess.Popen([str(BIN), str(self.directory)] + (["--init"] if initialize else []), + stdout=subprocess.PIPE, stderr=subprocess.PIPE) + deadline = time.monotonic() + 30 + while time.monotonic() < deadline: + if self.p.poll() is not None: + raise AssertionError(f"startup rc={self.p.returncode}: {self.p.communicate()}") + try: + with socket.socket(socket.AF_UNIX) as s: + s.settimeout(.2) + s.connect(str(self.directory / "tkad.sock")) + return + except (FileNotFoundError, ConnectionRefusedError, TimeoutError): + time.sleep(.02) + raise AssertionError("socket readiness timeout") + + def stop(self): + if self.p.poll() is None: + self.p.kill() + return self.p.communicate(timeout=10) + + def rpc(self, route, body=None, node=1, admin=False, expected=200): + payload = json.dumps(body or {}, separators=(",", ":")).encode() + frame = struct.pack("<4sBBHQI", b"TKA\x01", route, int(admin), 0, node, len(payload)) + payload + with socket.socket(socket.AF_UNIX) as s: + s.settimeout(30) + s.connect(str(self.directory / "tkad.sock")) + s.sendall(frame) + magic, status, reserved, size = struct.unpack("<4sHHI", exact(s, 12)) + check(magic == b"TKA\x01" and reserved == 0 and size <= 65536, "reply header") + answer = json.loads(exact(s, size)) + check(status == expected, f"route {route}: expected {expected}, got {status}: {answer}") + check(self.p.poll() is None, "daemon trapped/exited") + return answer + + def register(self, index): + return self.rpc(9, dict(NodeID=index + 1, NodeKey=FIX["nodes"][index], RotationPubkey=FIX["rotations"][index]), node=0, admin=True) + + def revision(self): + return self.rpc(11, node=0, admin=True)["Revision"] + + +def files(directory): + return sorted(p.name for p in pathlib.Path(directory).iterdir()) + + +def refused(directory): + run = subprocess.run([str(BIN), str(directory)], capture_output=True, timeout=20) + return run.returncode + + +with tempfile.TemporaryDirectory(prefix="tkad-replaced-") as top: + live = pathlib.Path(top) / "store" + live.mkdir(mode=0o700) + moved = pathlib.Path(top) / "store-moved" # where the opened directory went + daemon = Daemon(live, initialize=True) + try: + daemon.register(0) + check(daemon.revision() == 1, "first append committed") + inode = os.stat(live).st_ino + check("snapshot" in files(live), "snapshot on disk before replacement") + + # An unprivileged racer replaces the path with a fresh directory of the + # same name while the daemon keeps its descriptor open. + os.rename(live, moved) + live.mkdir(mode=0o700) + check(os.stat(live).st_ino != inode and os.stat(moved).st_ino == inode, "path now names a different directory") + daemon.directory = moved + check(os.path.realpath(f"/proc/{daemon.p.pid}/cwd") == os.path.realpath(moved), "daemon cwd is the opened directory") + + check(daemon.revision() == 1, "reads still served from the opened directory") + daemon.register(1) + check(daemon.revision() == 2, "append after replacement committed") + check(files(live) == [], f"replacement directory untouched: {files(live)}") + check("snapshot" in files(moved), "snapshot written beside the opened descriptor") + + # The held directory still excludes a second store on the inode, whatever + # its path is now called. + check(refused(moved) == 73, "exclusion follows the inode, not the path") + # The replacement directory holds no store, so a resume there refuses + # instead of serving a fresh empty revision as if it were this store. + check(refused(live) == 73, "resume in the replacement directory refused") + check(files(live) == [], "refused resume wrote nothing into the replacement") + + # Observed, not endorsed: the store carries no identity word, so the same + # path can host an unrelated store with its own Revision 0 while the + # opened one is still live. Nothing here tells a reconnecting client that + # the path it follows now names a different store; see the R5 frame. + other = Daemon(live, initialize=True) + try: + check(other.revision() == 0, "replacement path accepted an independent empty store") + finally: + other.stop() + finally: + daemon.stop() + + # Nothing was lost by the rename: the committed revision is still there. + reopened = Daemon(moved) + try: + check(reopened.revision() == 2, "committed revision survives restart") + check(files(moved).count("snapshot") == 1, "single snapshot after restart") + finally: + reopened.stop() +print(f"PASS tkad directory replacement: {checks} checks, commits land beside the opened descriptor, replacement stays empty and refuses resume") diff --git a/cmd/tkad/test/run.sh b/cmd/tkad/test/run.sh index 730c23b..4d409a8 100755 --- a/cmd/tkad/test/run.sh +++ b/cmd/tkad/test/run.sh @@ -9,4 +9,5 @@ bin=$(sh "$root/tools/build-tkad.sh") python3 "$root/cmd/tkad/test/smoke.py" "$bin" python3 "$root/cmd/tkad/test/mutant.py" "$root" "$bin" python3 "$root/cmd/tkad/test/rollover.py" "$root" "$bin" +python3 "$root/cmd/tkad/test/directory.py" "$bin" python3 "$root/cmd/tkad/test/faults.py" "$root" "$bin" From 7697b04dc052f6b8877a26187cceab7ffdd32f1b Mon Sep 17 00:00:00 2001 From: Drake Stapleton Date: Sun, 25 Oct 2026 23:59:36 -0500 Subject: [PATCH 2/2] docs(tkad): record the directory-replacement fixture (#10) The fixture added in 82cee30 proves the held descriptor, not the path, is the store: after init, replacing the directory at the same path does not redirect commits, the replacement stays empty and refuses resume. Verified on the current mainline: PASS tkad directory replacement, 35 checks. Co-Authored-By: Claude Fable 5.1 Co-Authored-By: Drake Stapleton --- cmd/tkad/README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/cmd/tkad/README.md b/cmd/tkad/README.md index 43e2028..4d56d5d 100644 --- a/cmd/tkad/README.md +++ b/cmd/tkad/README.md @@ -179,6 +179,9 @@ Test-only syscall-boundary variants prove one-shot begin/commit failure recovery without process restart and fatal handling of post-rename directory-fsync failure. A two-dispatch epoch variant checks queued connections, stable PID/listener, retained writer exclusion and missing/swapped resume-descriptor refusal. +A directory-replacement fixture proves the held descriptor, not the path, is the +store: after init, replacing the directory at the same path does not redirect +commits, and the replacement stays empty and refuses resume. A completed-response admin-bypass mutant must be detected; a trapping mutant must be rejected as an invalid test completion. Tests use isolated temporary state directories and no live services. These are host/socket regression tests,