phiward 0.2.0b1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (53) hide show
  1. phiward/__init__.py +8 -0
  2. phiward/__main__.py +5 -0
  3. phiward/_toml.py +211 -0
  4. phiward/adapters/__init__.py +105 -0
  5. phiward/adapters/claude.py +429 -0
  6. phiward/broker.py +831 -0
  7. phiward/carryover.py +1613 -0
  8. phiward/claude_settings.py +1307 -0
  9. phiward/cli.py +110 -0
  10. phiward/config.py +936 -0
  11. phiward/confine.py +476 -0
  12. phiward/copying.py +533 -0
  13. phiward/core.py +105 -0
  14. phiward/data/agents-section.md +43 -0
  15. phiward/doctor.py +1132 -0
  16. phiward/fasthook.py +70 -0
  17. phiward/fsinfo.py +241 -0
  18. phiward/gateway.py +159 -0
  19. phiward/githooks.py +248 -0
  20. phiward/guards/__init__.py +0 -0
  21. phiward/guards/protected_paths.py +1138 -0
  22. phiward/hidden.py +281 -0
  23. phiward/init.py +155 -0
  24. phiward/install.py +828 -0
  25. phiward/inventory.py +173 -0
  26. phiward/mcp.py +186 -0
  27. phiward/mcp_config.py +1736 -0
  28. phiward/names.py +5 -0
  29. phiward/packs/dates.toml +17 -0
  30. phiward/packs/genomics.toml +51 -0
  31. phiward/packs/msk.toml +32 -0
  32. phiward/pathid.py +403 -0
  33. phiward/pathmatch.py +98 -0
  34. phiward/paths.py +942 -0
  35. phiward/pin.py +214 -0
  36. phiward/procinfo.py +73 -0
  37. phiward/remote.py +448 -0
  38. phiward/run.py +788 -0
  39. phiward/safeio.py +84 -0
  40. phiward/sanitize.py +691 -0
  41. phiward/selfcheck.py +1506 -0
  42. phiward/service.py +321 -0
  43. phiward/session_note.py +86 -0
  44. phiward/setup_cmd.py +886 -0
  45. phiward/shell.py +504 -0
  46. phiward/wholetool.py +1463 -0
  47. phiward-0.2.0b1.dist-info/METADATA +147 -0
  48. phiward-0.2.0b1.dist-info/RECORD +53 -0
  49. phiward-0.2.0b1.dist-info/WHEEL +5 -0
  50. phiward-0.2.0b1.dist-info/entry_points.txt +3 -0
  51. phiward-0.2.0b1.dist-info/licenses/LICENSE +662 -0
  52. phiward-0.2.0b1.dist-info/licenses/NOTICE +16 -0
  53. phiward-0.2.0b1.dist-info/top_level.txt +1 -0
phiward/safeio.py ADDED
@@ -0,0 +1,84 @@
1
+ """Reading a file that the agent may have placed or replaced.
2
+
3
+ The hook and the broker run outside every sandbox, so a file they read must not be a way in:
4
+ `read_text` opens it once, without blocking (a FIFO or a device never stalls the reader), as a
5
+ regular file of bounded size, and, unless `follow` is set, without following a link in its last
6
+ part. The caller checks where it may be.
7
+ """
8
+
9
+ import os
10
+ import stat
11
+
12
+ LIMIT = 16 * 1024 * 1024
13
+
14
+
15
+ class UnsafeFile(OSError):
16
+ pass
17
+
18
+
19
+ def read_text(path: str, limit: int = LIMIT, follow: bool = False) -> str:
20
+ flags = os.O_RDONLY | os.O_NONBLOCK | (0 if follow else os.O_NOFOLLOW)
21
+ try:
22
+ fd = os.open(path, flags)
23
+ except OSError as exc:
24
+ if not follow and os.path.islink(path):
25
+ raise UnsafeFile("it is a link") from exc
26
+ raise
27
+ try:
28
+ st = os.fstat(fd)
29
+ if not stat.S_ISREG(st.st_mode):
30
+ raise UnsafeFile("it is not a regular file")
31
+ if st.st_size > limit:
32
+ raise UnsafeFile("it is too large")
33
+ chunks = []
34
+ size = 0
35
+ while True:
36
+ b = os.read(fd, 1 << 20)
37
+ if not b:
38
+ break
39
+ size += len(b)
40
+ if size > limit:
41
+ raise UnsafeFile("it is too large")
42
+ chunks.append(b)
43
+ finally:
44
+ os.close(fd)
45
+ return b"".join(chunks).decode("utf-8")
46
+
47
+
48
+ def write_new(path: str, text: str, mode: int = 0o600) -> None:
49
+ """Write `text` to `path` in place of what is there: through a fresh name that is never
50
+ followed as a link, and only when the file moved into place is the one written. On any
51
+ failure the fresh name is removed (a caller does not know it). For the operator's own
52
+ folders: a folder the agent can write needs dir_fd-based writes (broker.Spool)."""
53
+ import secrets
54
+ # a hidden name beside the file: a walk that skips dot names never takes it for a file
55
+ folder, name = os.path.split(path)
56
+ tmp = os.path.join(folder, f".{name}.{secrets.token_hex(8)}.tmp")
57
+ fd = os.open(tmp, os.O_WRONLY | os.O_CREAT | os.O_EXCL | os.O_NOFOLLOW, mode)
58
+ try:
59
+ with os.fdopen(fd, "w", encoding="utf-8") as fh:
60
+ fh.write(text)
61
+ fh.flush()
62
+ mine = os.fstat(fh.fileno())
63
+ now = os.lstat(tmp)
64
+ if (now.st_dev, now.st_ino) != (mine.st_dev, mine.st_ino):
65
+ raise OSError("the new file was replaced before it was moved into place")
66
+ os.replace(tmp, path)
67
+ except BaseException:
68
+ try:
69
+ os.unlink(tmp)
70
+ except OSError:
71
+ pass
72
+ raise
73
+
74
+
75
+ def remove_tree(path: str) -> bool:
76
+ """Remove `path` (a folder of any depth, or a file), never through a link: carryover's remover,
77
+ which goes down by descriptor without recursion (shutil.rmtree recurses before Python 3.12,
78
+ and its ignore_errors does not catch RecursionError). False when something stayed."""
79
+ from phiward import carryover
80
+ try:
81
+ carryover._remove(path)
82
+ except (OSError, RecursionError, carryover.CarryoverError):
83
+ return False
84
+ return True