p2pmesh 0.5.2__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.
- mesh/__init__.py +95 -0
- mesh/address.py +211 -0
- mesh/agent.py +701 -0
- mesh/auth.py +167 -0
- mesh/capability.py +398 -0
- mesh/cli.py +1037 -0
- mesh/delta.py +521 -0
- mesh/hub.py +1287 -0
- mesh/queue.py +1041 -0
- mesh/task.py +529 -0
- mesh/wire.py +453 -0
- p2pmesh-0.5.2.dist-info/METADATA +552 -0
- p2pmesh-0.5.2.dist-info/RECORD +17 -0
- p2pmesh-0.5.2.dist-info/WHEEL +5 -0
- p2pmesh-0.5.2.dist-info/entry_points.txt +2 -0
- p2pmesh-0.5.2.dist-info/licenses/LICENSE +21 -0
- p2pmesh-0.5.2.dist-info/top_level.txt +1 -0
mesh/__init__.py
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
"""Mesh —— 个人 AI 网格操作系统。
|
|
2
|
+
|
|
3
|
+
把三种资源统一到一张网里:
|
|
4
|
+
|
|
5
|
+
* **文件系统** —— 每个节点暴露若干命名 root(来自 linux-file-bridge 的路径围栏)
|
|
6
|
+
* **能力** —— 每个节点可查询的 CPU/内存/工具/模型/root 清单(全新)
|
|
7
|
+
* **算力** —— 每个节点若干可调度的槽位,跑 DAG 任务(来自 ai-swarm 的调度器)
|
|
8
|
+
|
|
9
|
+
统一寻址 ``mesh://node/env:root/path``,数据按块级增量传输,全程零第三方依赖。
|
|
10
|
+
|
|
11
|
+
模块分工:
|
|
12
|
+
|
|
13
|
+
============ ==========================================================
|
|
14
|
+
``address`` 统一寻址:解析、规范化、通配匹配
|
|
15
|
+
``delta`` 块级增量传输:Adler-32 滚动校验 + sha256,rsync 式差分
|
|
16
|
+
``capability``能力注册表:本机探测、调度打分、节点清单
|
|
17
|
+
``queue`` 持久作业队列:SQLite WAL + 租约 + 崩溃恢复 + 补送表
|
|
18
|
+
``task`` 任务图:DAG 校验、批次、写作用域
|
|
19
|
+
``wire`` 零依赖 HTTP + JSON-RPC(客户端与服务端)
|
|
20
|
+
``agent`` 节点 agent:路径围栏、只读 root、作用域强制
|
|
21
|
+
``hub`` 控制面:心跳、派发、失败重试、产物回收与分发、面板
|
|
22
|
+
``auth`` 令牌与权限:项目隔离、只读/可提交/可管理
|
|
23
|
+
``cli`` 命令行:node / hub / submit / nodes / token / status / sync / doctor
|
|
24
|
+
============ ==========================================================
|
|
25
|
+
|
|
26
|
+
最省事的起法(两台机器,同一个局域网)::
|
|
27
|
+
|
|
28
|
+
# 手机(Termux + proot):把工作目录暴露出去
|
|
29
|
+
mesh node --id 手机 --root work=~/proj --token 123 --port 8765
|
|
30
|
+
|
|
31
|
+
# 任意一台常开的机器:登记节点、发一把只能提交 blog 项目的令牌
|
|
32
|
+
mesh nodes add 手机 http://手机IP:8765 --token 123 --tags llm --db mesh.db
|
|
33
|
+
mesh token add --perms submit,read --project blog --db mesh.db
|
|
34
|
+
|
|
35
|
+
# 开始调度;手机浏览器打开面板看全程
|
|
36
|
+
mesh hub --db mesh.db --mirror ./产物 --port 8788 --token 123
|
|
37
|
+
"""
|
|
38
|
+
|
|
39
|
+
from __future__ import annotations
|
|
40
|
+
|
|
41
|
+
from . import address, agent, auth, capability, delta, hub, queue, task, wire
|
|
42
|
+
|
|
43
|
+
__version__ = "0.4.0"
|
|
44
|
+
PROTO = 2 # 0.3 加了 rm(分发时同步删除);旧节点会缺这一条
|
|
45
|
+
NAME = "mesh"
|
|
46
|
+
|
|
47
|
+
__all__ = [
|
|
48
|
+
"__version__", "PROTO", "NAME",
|
|
49
|
+
"address", "agent", "auth", "capability", "delta", "hub", "queue", "task", "wire",
|
|
50
|
+
"MeshAddress", "parse", "matches",
|
|
51
|
+
"Capability", "Registry", "probe", "score",
|
|
52
|
+
"Delta", "diff", "patch",
|
|
53
|
+
"Job", "Queue",
|
|
54
|
+
"TaskSpec", "Plan", "check_scope",
|
|
55
|
+
"Node", "serve", "jail",
|
|
56
|
+
"Hub", "HubError", "NodeRef",
|
|
57
|
+
"Client", "Server", "WireError",
|
|
58
|
+
"Auth", "Grant",
|
|
59
|
+
]
|
|
60
|
+
|
|
61
|
+
# 常用名字提到顶层,方便 `from mesh import parse, diff, Hub`
|
|
62
|
+
MeshAddress = address.MeshAddress
|
|
63
|
+
parse = address.parse
|
|
64
|
+
matches = address.matches
|
|
65
|
+
|
|
66
|
+
Capability = capability.Capability
|
|
67
|
+
Registry = capability.Registry
|
|
68
|
+
probe = capability.probe
|
|
69
|
+
score = capability.score
|
|
70
|
+
|
|
71
|
+
Delta = delta.Delta
|
|
72
|
+
diff = delta.diff
|
|
73
|
+
patch = delta.patch
|
|
74
|
+
|
|
75
|
+
Job = queue.Job
|
|
76
|
+
Queue = queue.Queue
|
|
77
|
+
|
|
78
|
+
TaskSpec = task.TaskSpec
|
|
79
|
+
Plan = task.Plan
|
|
80
|
+
check_scope = task.check_scope
|
|
81
|
+
|
|
82
|
+
Node = agent.Node
|
|
83
|
+
serve = agent.serve
|
|
84
|
+
jail = agent.jail
|
|
85
|
+
|
|
86
|
+
Hub = hub.Hub
|
|
87
|
+
HubError = hub.HubError
|
|
88
|
+
NodeRef = hub.NodeRef
|
|
89
|
+
|
|
90
|
+
Client = wire.Client
|
|
91
|
+
Server = wire.Server
|
|
92
|
+
WireError = wire.WireError
|
|
93
|
+
|
|
94
|
+
Auth = auth.Auth
|
|
95
|
+
Grant = auth.Grant
|
mesh/address.py
ADDED
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
"""统一寻址:``mesh://<节点>/<环境>:<根>/<路径>``。
|
|
2
|
+
|
|
3
|
+
为什么需要它
|
|
4
|
+
------------
|
|
5
|
+
两个地基仓库各有一套坐标,互不相通:
|
|
6
|
+
|
|
7
|
+
* linux-file-bridge 用 ``env:root/path`` —— 有"环境"和"根",没有"节点"。
|
|
8
|
+
* ai-swarm 用 ``(project, task)`` —— 有任务,没有路径。
|
|
9
|
+
|
|
10
|
+
Mesh 里一个节点同时是**文件系统 + 能力清单 + 算力槽位**,所以需要一个三维坐标:
|
|
11
|
+
哪个节点、哪个环境、哪个根、哪条路径。
|
|
12
|
+
|
|
13
|
+
语法
|
|
14
|
+
----
|
|
15
|
+
::
|
|
16
|
+
|
|
17
|
+
mesh://node-a/ubuntu:project/src/main.py # 完整形式
|
|
18
|
+
mesh://node-a/project/src/main.py # 省略环境 → 默认环境
|
|
19
|
+
mesh://node-a/project # 根本身
|
|
20
|
+
mesh://node-a # 节点本身
|
|
21
|
+
mesh://*/ubuntu:project/README.md # 通配:所有节点
|
|
22
|
+
|
|
23
|
+
``local`` 是保留节点���,指"本进程所在的那台机器"。
|
|
24
|
+
"""
|
|
25
|
+
|
|
26
|
+
from __future__ import annotations
|
|
27
|
+
|
|
28
|
+
import re
|
|
29
|
+
|
|
30
|
+
__all__ = [
|
|
31
|
+
"AddressError", "MeshAddress", "LOCAL_NODE", "WILDCARD_NODE",
|
|
32
|
+
"parse", "normalize_path", "is_valid_name", "matches",
|
|
33
|
+
]
|
|
34
|
+
|
|
35
|
+
SCHEME = "mesh://"
|
|
36
|
+
LOCAL_NODE = "local"
|
|
37
|
+
WILDCARD_NODE = "*"
|
|
38
|
+
|
|
39
|
+
# 名字会出现在 mesh:// 地址、镜像目录名和库里。允许 Unicode 词字符 ——
|
|
40
|
+
# 中文节点名("手机"、"笔记本")是最自然的用法,把它们挡在外面纯属自找麻烦。
|
|
41
|
+
# 前半段限制仍然有效:不能以 . 或 - 开头,不能含 / : 这些会破坏地址结构的字符。
|
|
42
|
+
_NAME_RE = re.compile(r"^[\w][\w.-]{0,63}$", re.UNICODE)
|
|
43
|
+
MAX_PATH = 4096
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
class AddressError(Exception):
|
|
47
|
+
"""地址不合法。"""
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
def is_valid_name(value):
|
|
51
|
+
"""节点/环境/根的名字:词字符开头,可含 ``._-``,最长 64(支持中文)。"""
|
|
52
|
+
return bool(value) and bool(_NAME_RE.match(value))
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
def _check_name(value, what):
|
|
56
|
+
if value == WILDCARD_NODE and what == "节点":
|
|
57
|
+
return value
|
|
58
|
+
if not is_valid_name(value):
|
|
59
|
+
raise AddressError(
|
|
60
|
+
"%s名不合法:%r(需字母、数字或汉字开头,可含 . _ -,最长 64 字符)"
|
|
61
|
+
% (what, value))
|
|
62
|
+
return value
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
def normalize_path(path):
|
|
66
|
+
"""规范化相对路径:去掉多余斜杠���拒绝逃逸。
|
|
67
|
+
|
|
68
|
+
拒绝:绝对路径、``..`` 段、NUL、反斜杠、超长。
|
|
69
|
+
"""
|
|
70
|
+
if path is None:
|
|
71
|
+
return ""
|
|
72
|
+
if not isinstance(path, str):
|
|
73
|
+
raise AddressError("路径必须是字符串,收到 %r" % (type(path).__name__,))
|
|
74
|
+
if "\x00" in path:
|
|
75
|
+
raise AddressError("路径含 NUL 字节")
|
|
76
|
+
if "\\" in path:
|
|
77
|
+
raise AddressError("路径含反斜杠(请用 /):%r" % path)
|
|
78
|
+
if len(path) > MAX_PATH:
|
|
79
|
+
raise AddressError("路径过长(%d > %d)" % (len(path), MAX_PATH))
|
|
80
|
+
|
|
81
|
+
parts = []
|
|
82
|
+
for seg in path.split("/"):
|
|
83
|
+
if seg in ("", "."):
|
|
84
|
+
continue
|
|
85
|
+
if seg == "..":
|
|
86
|
+
raise AddressError("路径含 .. 段,拒绝逃逸:%r" % path)
|
|
87
|
+
parts.append(seg)
|
|
88
|
+
return "/".join(parts)
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
class MeshAddress(object):
|
|
92
|
+
"""一个网格坐标。不可变(用 ``_replace`` 派生新对象)。"""
|
|
93
|
+
|
|
94
|
+
__slots__ = ("node", "env", "root", "path")
|
|
95
|
+
|
|
96
|
+
def __init__(self, node, env="", root="", path=""):
|
|
97
|
+
self.node = _check_name(node, "节点")
|
|
98
|
+
self.env = _check_name(env, "环境") if env else ""
|
|
99
|
+
self.root = _check_name(root, "根") if root else ""
|
|
100
|
+
self.path = normalize_path(path)
|
|
101
|
+
|
|
102
|
+
# -- 构造 ------------------------------------------------------------
|
|
103
|
+
def _replace(self, **kw):
|
|
104
|
+
return MeshAddress(
|
|
105
|
+
kw.get("node", self.node),
|
|
106
|
+
kw.get("env", self.env),
|
|
107
|
+
kw.get("root", self.root),
|
|
108
|
+
kw.get("path", self.path),
|
|
109
|
+
)
|
|
110
|
+
|
|
111
|
+
@property
|
|
112
|
+
def is_wildcard(self):
|
|
113
|
+
return self.node == WILDCARD_NODE
|
|
114
|
+
|
|
115
|
+
@property
|
|
116
|
+
def is_local(self):
|
|
117
|
+
return self.node == LOCAL_NODE
|
|
118
|
+
|
|
119
|
+
@property
|
|
120
|
+
def has_root(self):
|
|
121
|
+
return bool(self.root)
|
|
122
|
+
|
|
123
|
+
# -- 序列化 ----------------------------------------------------------
|
|
124
|
+
def __str__(self):
|
|
125
|
+
out = SCHEME + self.node
|
|
126
|
+
if self.env:
|
|
127
|
+
out += "/" + self.env + ":" + self.root
|
|
128
|
+
elif self.root:
|
|
129
|
+
out += "/" + self.root
|
|
130
|
+
if self.path:
|
|
131
|
+
out += "/" + self.path
|
|
132
|
+
return out
|
|
133
|
+
|
|
134
|
+
def __repr__(self):
|
|
135
|
+
return "MeshAddress(%r)" % str(self)
|
|
136
|
+
|
|
137
|
+
def to_dict(self):
|
|
138
|
+
return {"node": self.node, "env": self.env, "root": self.root, "path": self.path}
|
|
139
|
+
|
|
140
|
+
@classmethod
|
|
141
|
+
def from_dict(cls, obj):
|
|
142
|
+
if not isinstance(obj, dict):
|
|
143
|
+
raise AddressError("地址必须是对象")
|
|
144
|
+
return cls(obj.get("node") or "", obj.get("env") or "",
|
|
145
|
+
obj.get("root") or "", obj.get("path") or "")
|
|
146
|
+
|
|
147
|
+
def __eq__(self, other):
|
|
148
|
+
if not isinstance(other, MeshAddress):
|
|
149
|
+
return NotImplemented
|
|
150
|
+
return (self.node, self.env, self.root, self.path) == \
|
|
151
|
+
(other.node, other.env, other.root, other.path)
|
|
152
|
+
|
|
153
|
+
def __hash__(self):
|
|
154
|
+
return hash((self.node, self.env, self.root, self.path))
|
|
155
|
+
|
|
156
|
+
|
|
157
|
+
def parse(text):
|
|
158
|
+
"""解析地址字符串。``mesh://`` 前缀可省略。"""
|
|
159
|
+
if not isinstance(text, str):
|
|
160
|
+
raise AddressError("地址必须是字符串")
|
|
161
|
+
raw = text.strip()
|
|
162
|
+
if not raw:
|
|
163
|
+
raise AddressError("地址为空")
|
|
164
|
+
if raw.lower().startswith(SCHEME):
|
|
165
|
+
raw = raw[len(SCHEME):]
|
|
166
|
+
elif "://" in raw:
|
|
167
|
+
raise AddressError("只支持 mesh:// 协议:%r" % text)
|
|
168
|
+
|
|
169
|
+
raw = raw.rstrip("/")
|
|
170
|
+
if not raw:
|
|
171
|
+
raise AddressError("地址缺少节点名:%r" % text)
|
|
172
|
+
|
|
173
|
+
node, _, rest = raw.partition("/")
|
|
174
|
+
if not node:
|
|
175
|
+
raise AddressError("地址缺少节点名:%r" % text)
|
|
176
|
+
|
|
177
|
+
env = ""
|
|
178
|
+
root = ""
|
|
179
|
+
path = ""
|
|
180
|
+
if rest:
|
|
181
|
+
head, _, tail = rest.partition("/")
|
|
182
|
+
if ":" in head:
|
|
183
|
+
env, _, root = head.partition(":")
|
|
184
|
+
if not env:
|
|
185
|
+
raise AddressError("环境名为空:%r" % text)
|
|
186
|
+
if not root:
|
|
187
|
+
raise AddressError("根名为空:%r" % text)
|
|
188
|
+
else:
|
|
189
|
+
root = head
|
|
190
|
+
path = tail
|
|
191
|
+
return MeshAddress(node, env, root, path)
|
|
192
|
+
|
|
193
|
+
|
|
194
|
+
def matches(pattern, addr):
|
|
195
|
+
"""通配匹配:``pattern`` 的节点位可以是 ``*``。
|
|
196
|
+
|
|
197
|
+
其余三段(环境/根/路径)必须完全相等;``pattern`` 中留空的段视为"不限"。
|
|
198
|
+
"""
|
|
199
|
+
if isinstance(pattern, str):
|
|
200
|
+
pattern = parse(pattern)
|
|
201
|
+
if isinstance(addr, str):
|
|
202
|
+
addr = parse(addr)
|
|
203
|
+
if pattern.node != WILDCARD_NODE and pattern.node != addr.node:
|
|
204
|
+
return False
|
|
205
|
+
if pattern.env and pattern.env != addr.env:
|
|
206
|
+
return False
|
|
207
|
+
if pattern.root and pattern.root != addr.root:
|
|
208
|
+
return False
|
|
209
|
+
if pattern.path and pattern.path != addr.path:
|
|
210
|
+
return False
|
|
211
|
+
return True
|