Repository navigation
Expand file tree
/
Copy pathcell.py
More file actions
1420 lines (1224 loc) · 51.5 KB
/
Copy pathcell.py
File metadata and controls
1420 lines (1224 loc) · 51.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
Cell — a unit node in the MOSS communication network, addressed and projected into the Matrix.
This module defines the Cell abstraction (``Cell``, ``CellAddress``, ``CellProtocol``),
the node manifest that declares a cell (``NodeManifest``), and the entry points that
build and tear down cells (``build_cell_from_node``, ``enter_cell_lifecycle``).
Cell 是在 Moss 的网络 (Matrix) 中运行的进程单元. 可以将之想象为一个数字城市中的一个房间, 用来提供不同的功能.
Cell 可以用来控制机器人, 创建图形界面, 运行独立的思考或 Agent. 可以把它理解为手机里的 App.
Cell 运行时, 通过映射进入 Matrix 网络, Cell 之间的通讯可以通过文件或者 Matrix.session 协议.
每个 Cell 都有自己的地址 - address, 类似于电话; 关于 cell 的通讯基本都从 address 出发.
当 Cell 通过 Matrix 提供 Channel 等能力时, 将能够让 Moss 在运行时控制它. 一个通过 moss 启动的 Ghost 可以用 Channel 控制它看到的 Cell.
各种各样的 Cell 运行时组织成 Matrix 的网络, 通过 Host 节点提供给 Ghost 一个可用的操作系统.
"""
import contextlib
import os
import secrets
import string
import sys
import time
from enum import IntEnum
from pathlib import Path
from typing import Callable, ClassVar, Iterable, Literal
from typing_extensions import Self
from abc import ABC, abstractmethod
import fnmatch
import frontmatter
import shlex
from pydantic import BaseModel, Field, AwareDatetime
from ghoshell_moss.core.concepts.channel import Channel, ChannelProvider, ChannelProxy
from ghoshell_moss.contracts.subprocesses import CaptureSpec, ManagedProcess
from .environment import Environment
import datetime
import dateutil
import dataclasses
import psutil
import asyncio
__all__ = [
'CellAddress',
'CellRole',
'CellProtocol',
'CellEventLevel',
'HOST_ROLE',
'NODE_ROLE',
'normalize',
'make_address',
'parse_address',
'build_cell_from_node',
'build_host_cell',
'discover_this_node',
'ExecSpec',
'NodeManifest',
'CellRuntimeInfo',
'Cell',
'CellEvent',
'CellPresence',
'CellNetwork',
'AutoAcceptPolicy',
'NodeManager',
'DuplicatedError',
'CellName',
'CellNamePattern',
'AbsolutePath',
'ProjectRelativePath',
'MatchPattern',
'NodeLauncher',
'enter_cell_lifecycle',
'CellAddressCodec', 'NodeProbeError',
]
CellRole = Literal['host', 'node']
"""
Role of a cell in the Matrix network topology.
- 'host': the network's central node — organizes all capabilities for a body or agent to drive.
- 'node': a functional node — may control a body, provide a GUI, run an independent app, etc.
"""
HOST_ROLE: CellRole = 'host'
NODE_ROLE: CellRole = 'node'
ROLES = frozenset({HOST_ROLE, NODE_ROLE})
class CellEventLevel(IntEnum):
"""Perception level of a cell lifecycle event — aligned to logging levels.
Perception follows logging filter semantics:
event_level >= INFO (threshold) -> send_signal (perceivable)
event_level < INFO -> no send_signal (zero-cost); kept in event_buffer (pullable)
Mapping: DEBUG->drop, INFO->BACKGROUND, WARNING->WARNING, ERROR->ERROR, CRITICAL->CRITICAL.
"""
DEBUG = 10
INFO = 20
WARNING = 30
ERROR = 40
CRITICAL = 50
@classmethod
def resolve(cls, level: 'CellEventLevel | None') -> 'CellEventLevel':
"""Normalize None (system default) to INFO — the single entry point for the perception check."""
return level if level is not None else cls.INFO
@classmethod
def is_perceivable(cls, level: 'CellEventLevel | None') -> bool:
"""Whether this level produces a ghost signal (below the INFO threshold it does not)."""
return cls.resolve(level) >= cls.INFO
CellProtocol = Literal['channel']
"""
A MOSS duplex protocol a cell provides. Closed set — each value maps to a Provider/Proxy
role pair + event vocabulary in duplex/. Adding a protocol means adding that implementation
and appending the value here; it is not a free string.
"""
CellAddress = str
"""
The cell's unique network address: CellRole / name / uid
"""
ProjectRelativePath = str
AbsolutePath = str
MatchPattern = str
"""Wildcard pattern: group/name, group/*, *, */*, */name"""
CellName = str
# 不硬约束 -/. : from_script 从文件名 (moss-ghost 等) 导出的 name 会反复炸.
CellNamePattern = r"^[a-zA-Z0-9_.-]+$"
"""A cell name is a governance-domain path segment, allowing ``-`` ``.``.
``make_address`` normalizes ``-``/``.`` to ``_`` for identifier safety; ``Cell.name``
keeps the original value.
"""
def short_uid() -> str:
"""Generate a 6-char short random uid — the address uniqueness source.
First char a letter, the rest letter/digit (no underscore), satisfying
``ChannelNamePattern`` so the uid is safe as a channel path segment.
"""
return secrets.choice(string.ascii_lowercase) + ''.join(
secrets.choice(string.ascii_lowercase + string.digits) for _ in range(5)
)
class Cell(BaseModel):
"""
The declaration of a **running** node in the Matrix network — a room in the building
that is Matrix. Usually one process.
"""
role: CellRole = Field(
description="cell role"
)
name: str = Field(
description="cell name",
pattern=CellNamePattern,
)
uid: str = Field(
default_factory=short_uid,
description="Short random uid — the address uniqueness source.",
)
singleton: bool = Field(
default=False,
description="Whether this cell keeps a single instance within the governance domain. "
"True: a live same-name cell rejects a duplicate launch (DuplicatedError) — "
"for hardware-exclusive (mic/camera/robot) or state-exclusive (database) cases. "
"False (default): multiple instances run in parallel, each with its own uid.",
)
category: Literal['ghost', 'shell', 'script'] | str = Field(
default='',
pattern=r"^[a-zA-Z0-9_]*$",
description="The cell's category.",
)
event_level: CellEventLevel | None = Field(
default=None,
description="Perception level of this cell's lifecycle events for listeners. "
"None = system default (resident node -> INFO perceivable, one-shot -> DEBUG silent). "
"Below the INFO threshold no ghost signal is produced — the event stays "
"in event_buffer (pullable) but does not enter attention.",
)
description: str = Field(
default='',
description="cell description",
)
project_id: str = Field(
default='',
description="Governance-domain (project) id of this cell; used to tell local from foreign cells.",
)
project_name: str = Field(
default='',
description="Name of the project this cell belongs to.",
)
providing: list[CellProtocol] = Field(
default_factory=list,
description="The MOSS duplex protocols this cell currently provides. "
"Marks protocol names only — content is pulled through each Provider/Proxy bridge. "
"Value domain is closed by CellProtocol; adding one requires landing the role pair in duplex/.",
)
updated: AwareDatetime = Field(
default_factory=lambda: datetime.datetime.now(dateutil.tz.gettz()),
description="Timestamp of this cell's last update.",
)
home: str = Field(
description="Process working directory, absolute path.",
)
persist: bool = Field(
default=False,
)
parent_address: str = Field(
default='',
description="The parent node running this cell.",
)
def update(self) -> None:
self.updated = datetime.datetime.now(dateutil.tz.gettz())
@property
def address(self) -> CellAddress:
return make_address(self.role, self.name, self.uid)
@property
def address_codec(self) -> 'CellAddressCodec':
return CellAddressCodec(self.address)
@property
def fullname(self) -> str:
if self.category:
return '_'.join([self.category, normalize(self.name)])
return normalize(self.name)
@property
def is_host(self) -> bool:
"""Whether this cell is the network's host."""
return self.role == HOST_ROLE
def is_local(self, env: Environment) -> bool:
return self.project_id == env.project_id
class ExecSpec(BaseModel):
"""
Declaration of a process to run (usually a Node).
"""
command: Literal['python'] | str = Field(
default='python',
description="When non-empty, argv[0] of the launch command. "
"A bare 'python' resolves to the spawner's own interpreter "
"(sys.executable); any other value is a path relative to cwd.",
)
args: str = Field(
default='main.py',
description="Argument list of the launch command.",
)
env: dict[str, str] = Field(
default_factory=dict,
description="Extra environment variables to inject. The launch script may also load its own via dotenv.",
)
timeout: float | None = Field(
default=None,
description="Process timeout in seconds. Currently consumed only by the NodeManifest.check "
"probe: on timeout it is judged broken and its process group is terminated. "
"None = the system default probe ceiling — a probe never waits without bound. "
"(Not consumed by the main exec path yet.)",
)
@property
def arguments(self) -> list[str]:
return shlex.split(self.args)
NodeScriptCategory = 'script'
class NodeManifest(BaseModel):
"""
Declaration of a Node-type cell — defined through a declaration file, and also
supporting script-launch scenarios without one.
"""
MANIFEST_FILENAME: ClassVar[str] = 'NODE.md'
"""The conventional declaration file — think of it as a Windows shortcut. """
INSTALL_FILENAME: ClassVar[str] = 'INSTALL.md'
"""The file describing how to install. When present, it pairs with INSTALLED_FILE to
mark install state — a node with its own project dependencies declares its steps here."""
INSTALLED_FILE: ClassVar[str] = '.installed'
"""A file marker recording whether a node has completed installation."""
name: str = Field(
description="The node's name — its identity anchor within the governance domain.",
pattern=CellNamePattern,
)
description: str = Field(
default='',
description="One-line description of the node.",
)
category: str = Field(
default='',
description="A free-form category label (e.g. sensors / bodies / scripts / tools) that drives no mechanism.",
pattern=r"^[a-zA-Z0-9_]*$",
)
singleton: bool = Field(
default=True,
description="Whether this cell keeps a single instance within the governance domain. "
"True (default): a live same-name cell rejects a duplicate launch — "
"for hardware-exclusive (mic/camera/robot) or state-exclusive (database) cases. "
"False: multiple instances run in parallel, each with its own uid.",
)
persist: bool = Field(
default=True,
description="Whether this node is resident. "
"True (default): resident node cell — provides a channel long-running, "
"lifecycle events are perceivable (event_level INFO by convention). "
"False: one-shot run-to-completion — events silent (event_level=DEBUG), "
"no channel provided, results read via a blocking nodes:run for stdout/stderr/exitcode.",
)
exec: 'ExecSpec' = Field(
default_factory=ExecSpec,
description="Default launch entry (frontmatter `run:` declaration). "
"A cell without one can only be launched by an explicit script path.",
)
check: 'ExecSpec | None' = Field(
default=None,
description="Pre-launch probe (frontmatter `check:` declaration), an independent process "
"requiring zero cooperation from the target. "
"exit 0 -> pass, launch the main script; nonzero + stderr -> broken reason, do not launch. "
"Verifies whether the environment can run now (real dependency import / smoke call). "
"Omitted -> probe skipped.",
)
instruction: str = Field(
default='',
description="Detailed usage instructions for the node.",
)
installed: bool = Field(
default=True,
description="Whether installation is complete. An uninstalled cell is discoverable but "
"refuses to launch; the error points at the INSTALL.md path. Derived from the "
"filesystem, not from frontmatter.",
)
file: AbsolutePath = Field(
default='',
description="Absolute path of the file this NodeManifest was generated from.",
)
@property
def cwd(self) -> Path:
if self.file:
return Path(self.file).parent.resolve()
return Path.cwd()
@classmethod
def read_from_file(cls, file: Path) -> 'NodeManifest':
"""Read the declaration from a NODE.md file. The body is the instruction; the
frontmatter is the fields."""
content = file.read_text(encoding='utf-8')
post = frontmatter.loads(content)
data = dict(post.metadata)
data['instruction'] = post.content.strip()
data['file'] = str(file.absolute())
directory = file.parent
# installed 由文件系统推导:
# 有 INSTALL.md → 需要额外安装步骤, 靠 .installed 标记是否完成
# 无 INSTALL.md → 无额外依赖, 天然视为已安装
if directory.joinpath(cls.INSTALL_FILENAME).exists():
data['installed'] = directory.joinpath(cls.INSTALLED_FILE).exists()
else:
data['installed'] = True
return cls(**data)
@classmethod
def read_from_directory(cls, directory: Path) -> 'NodeManifest | None':
"""Read the manifest from a directory."""
file = directory.joinpath(cls.MANIFEST_FILENAME)
if file.is_file():
return cls.read_from_file(file)
return None
def save(self) -> None:
file = Path(self.file).absolute()
self.write_file(file)
def write_file(self, directory: Path, filename: str = '') -> None:
"""Write the declaration to NODE.md."""
filename = filename or self.MANIFEST_FILENAME
data = self.model_dump(
exclude_none=True,
exclude={'instruction', 'installed', 'file'},
)
post = frontmatter.Post(content=self.instruction, **data)
frontmatter.dump(post, directory.joinpath(filename).resolve())
@classmethod
def find_upward(cls, start: Path) -> 'NodeManifest | None':
"""Walk up from ``start`` to the nearest NODE.md (stops at the first hit)."""
directory = start if start.is_dir() else start.parent
home = Path.home()
for candidate in [directory, *directory.parents]:
manifest = cls.read_from_directory(candidate)
if manifest is not None:
return manifest
if candidate == home:
break
return None
@classmethod
def from_script(cls, script: Path, *, exec_spec: ExecSpec | None = None) -> 'NodeManifest':
"""
Build a Manifest from a script entry point: adopt the nearest NODE.md found by
walking upward. When none is found, degrade to an ad-hoc identity rather than
refusing to run.
"""
script = script.resolve()
found = cls.find_upward(script)
exec_spec = exec_spec or ExecSpec(command=sys.executable, args=str(script))
if found is None:
manifest = cls(
name=script.stem,
category=NodeScriptCategory,
singleton=True,
persist=False,
description=f'ad-hoc node from {script}',
file=str(script.absolute()),
)
else:
manifest = found
manifest.exec = exec_spec
return manifest
@classmethod
def from_proc(cls) -> 'NodeManifest':
"""Build identity from the current process: walk up from the __main__ script; degrade
to a temporary identity when none is found."""
from importlib import import_module
import inspect
main = import_module('__main__')
script_file = Path(inspect.getfile(main))
exec_spec = ExecSpec(command=sys.executable, args=' '.join(sys.argv))
return cls.from_script(script_file, exec_spec=exec_spec)
@classmethod
def new(
cls,
name: str,
*,
description: str = '',
category: str = '',
) -> 'NodeManifest':
"""Create a node manifest in the current process."""
manifest = cls.from_proc()
manifest.name = name
manifest.description = description
manifest.category = category
return manifest
class CellRuntimeInfo(BaseModel):
"""
Runtime data MOSS Project keeps for a cell process — the operational surface: only the
side that can act directly on the process (owner / local CLI) should consume these fields.
"""
# -- 运行时文件命名约定 -- #
RUNTIME_SUBDIR: ClassVar[str] = 'runtime'
SUFFIX_JSON: ClassVar[str] = '.json'
SUFFIX_STDOUT: ClassVar[str] = '.stdout.log'
SUFFIX_STDERR: ClassVar[str] = '.stderr.log'
address: CellAddress = Field(
description="The cell's network address.",
)
pid: int = Field(
default=0,
description="Process id; 0 means not yet started.",
)
pgid: int = Field(
default=0,
description="Process group id (the process's own group after start_new_session); the target of killpg.",
)
start_time: float = Field(
default_factory=time.time,
description="Process start timestamp; combined with pid as a guard against pid reuse.",
)
cell: Cell = Field(
description="The cell's runtime data, used to rebuild and broadcast identity.",
)
alias: str | None = Field(
default=None,
description="cell 启动时赋予的别名",
)
@classmethod
def from_cell(cls, cell: Cell) -> 'CellRuntimeInfo':
return cls(address=cell.address, cell=cell)
@classmethod
def filename(cls, address: CellAddress, *, suffix: str = SUFFIX_JSON) -> str:
return normalize(address) + suffix
@classmethod
def default_stdout_log(cls, cell_home: Path, address: CellAddress) -> Path:
filename = cls.filename(address, suffix=cls.SUFFIX_STDOUT)
return cell_home.joinpath(filename).resolve()
@classmethod
def default_stderr_log(cls, cell_home: Path, address: CellAddress) -> Path:
filename = cls.filename(address, suffix=cls.SUFFIX_STDERR)
return cell_home.joinpath(filename).resolve()
@classmethod
def get_normalized_address_from_file(cls, file: Path) -> str:
if not file.is_file():
raise FileNotFoundError(f'{file} is not a valid file')
return file.stem
@classmethod
def filepath(cls, runtime_dir: Path, address: CellAddress) -> Path:
filename = cls.filename(address)
return runtime_dir.joinpath(filename).resolve()
def write_to_runtime_dir(self, runtime_dir: Path) -> None:
content = self.model_dump_json(indent=0, exclude_defaults=True, exclude_none=True, ensure_ascii=True)
self.filepath(runtime_dir, self.address).write_text(content)
def delete_invalid(self, runtime_dir: Path) -> None:
file = self.filepath(runtime_dir, self.address)
if file.exists():
file.unlink()
@classmethod
def read_from_runtime_dir(
cls,
runtime_dir: Path, address: CellAddress, *, delete_invalid: bool = True,
) -> 'CellRuntimeInfo | None':
filepath = cls.filepath(runtime_dir, address)
return cls.read_from_file(filepath, delete_invalid=delete_invalid)
@classmethod
def read_from_file(
cls,
filepath: Path,
*,
delete_invalid: bool = True,
) -> 'CellRuntimeInfo | None':
if filepath.exists():
data = filepath.read_text()
try:
info = cls.model_validate_json(data)
return info
except Exception:
if delete_invalid:
filepath.unlink()
return None
@classmethod
def iter_runtime_info(cls, runtime_dir: Path) -> 'Iterable[CellRuntimeInfo]':
for file in runtime_dir.glob(f'*{cls.SUFFIX_JSON}'):
found = cls.read_from_file(file, delete_invalid=False)
if found is not None:
yield found
@classmethod
def clear_dead_runtimes(cls, runtime_dir: Path) -> int:
"""Scan all ledgers in runtime_dir; clear dead processes together with their log files. Returns the cleared count."""
cleaned = 0
for info in cls.iter_runtime_info(runtime_dir):
if info.is_alive():
continue
info.delete_invalid(runtime_dir)
for suffix in (cls.SUFFIX_STDOUT, cls.SUFFIX_STDERR):
f = runtime_dir / cls.filename(info.address, suffix=suffix)
if f.exists():
f.unlink()
cleaned += 1
return cleaned
def is_alive(self) -> bool:
return psutil.pid_exists(self.pid)
def locker_name(self) -> str:
"""
This cell's lock name — the single authority for the singleton exclusion mechanism.
The lock's carrier is the file lock env.workspace.lock(locker_name()). The name is
the fullname (category_name or name); within a governance domain fullname uniqueness
is lock uniqueness. The uid is deliberately not in the lock name — same-name cells
across different uids are the ones that must exclude each other.
"""
return normalize(self.cell.fullname)
class CellEvent(BaseModel):
"""
An on-change notification on the network: the change hint a cell broadcasts (the push
side of push-pull).
The event itself is a cheap push ("I changed"); the concrete content is always pulled
on demand by the consumer:
refetch=True -> consumer refetches the Cell to update its cache (the pull)
refetch=False -> consumer only records the event, cache untouched (pure signal/debug)
Two consumption surfaces (two subscription points on the Watcher):
structural change -> Watcher.on_change (Cell snapshot consumers: cache view / CLI display)
attention candidate -> Watcher.on_event (nucleus consumers: turn into Signal for mindflow)
"""
address: CellAddress = Field(
description="Address of the cell the event came from.",
)
content: str = Field(
default='',
description="Free-text hint of the event; may be empty. A reference for the consumer, not a scheduling criterion.",
)
created: AwareDatetime = Field(
default_factory=lambda: datetime.datetime.now(dateutil.tz.gettz()),
description="Time the event was issued.",
)
refetch: bool = Field(
default=True,
description="True -> the consumer should refetch the Cell to update its cache "
"(cell state / membrane type may have changed); "
"False -> only append the event buffer, cache untouched (pure signal/debug).",
)
event_level: CellEventLevel | None = Field(
default=None,
description="Perception level (CellEventLevel) of the source cell. "
"The listener decides whether to produce a ghost signal: "
"below the INFO threshold no send_signal, kept pullable.",
)
@property
def address_codec(self) -> 'CellAddressCodec':
return CellAddressCodec(self.address)
@dataclasses.dataclass
class NodeLauncher:
"""
The launch parameters of a node cell, packaged.
Describes only "how to start a process", not "who starts it". The spawner (usually
Subprocesses.execute) consumes this dataclass, starts the process with
start_new_session=True, then backfills runtime.pid / runtime.pgid and writes to
the runtime dir.
"""
cwd: Path
env: dict[str, str]
run: list[str]
runtime: CellRuntimeInfo
@classmethod
def from_manifest(
cls,
env: Environment,
manifest: NodeManifest,
*,
uid: str | None = None,
) -> 'NodeLauncher':
"""Prepare the launch of a node cell."""
cell = build_cell_from_node(env, manifest, uid=uid)
cwd = manifest.cwd
# pid/pgid 留 0, 由 spawner 起进程后回填.
runtime_info = CellRuntimeInfo(address=cell.address, cell=cell)
env_data = env.dump_cell_env(cell_address=cell.address, parent_cell_address=env.this_cell_address)
run = []
if manifest.exec.command:
command = manifest.exec.command
if command == 'python':
command = sys.executable
run.append(command)
run.extend(manifest.exec.arguments)
return cls(
cwd=cwd,
env=env_data,
run=run,
runtime=runtime_info,
)
def make_address(role: CellRole, name: CellName, uid: str) -> str:
"""
Build an address from three segments.
:param role: address[0] — a reserved word, one of the CellRole values.
:param name: address[1] — the governance-domain path.
:param uid: address[-1] — the uniqueness source, a short random string.
"""
# name 是治理域路径, -/. 归一化为 _ 保持 address 标识符安全;
# 原始值留在 Cell.name, 此处是 address 生成的唯一落点.
name = name.replace('-', '_').replace('.', '_')
return CellAddressCodec.make(role, name, uid).address
def parse_address(address: CellAddress) -> tuple[CellRole, CellName, str]:
"""
Split an address into three slices: (role, name, uid).
:raise ValueError: fewer than 3 segments, or role not in CellRole.
"""
return CellAddressCodec.parse(address)
def normalize(name_or_address: str) -> str:
"""Normalize a name or address into a filename / python-identifier-safe form."""
return CellAddressCodec.normalize(name_or_address)
class CellAddressCodec:
"""Form conversion and validation for CellAddress (str).
The address stays a str representation (type alias); this class is the single entry
point for converting, displaying and matching it. Types that hold an address
(Cell / CellEvent) expose an instance via ``.address_codec`` instead of parsing the
address string themselves.
"""
def __init__(self, address: CellAddress, *, validate: bool = True) -> None:
self.address: CellAddress = address
self._parts: tuple[CellRole, CellName, str] | None = None
if validate:
self._parts = self.parse(address)
@property
def parts(self) -> tuple[CellRole, CellName, str]:
if self._parts is None:
self._parts = self.parse(self.address)
return self._parts
@property
def role(self) -> CellRole:
return self.parts[0]
@property
def name(self) -> CellName:
return self.parts[1]
@property
def uid(self) -> str:
return self.parts[2]
# -- 别名 -------------------------------------------------
@property
def dot_address(self) -> str:
"""Dot-separated form: ``role.name.uid``."""
return self.address.replace('/', '.')
@classmethod
def from_dot_address(cls, dot_address: str) -> 'CellAddressCodec':
"""Rebuild from the dot-separated form (best effort)."""
return cls(dot_address.replace('.', '/'), validate=True)
@property
def normalized(self) -> str:
"""Filesystem-safe form: ``/`` ``.`` ``-`` replaced with ``__``."""
return self.normalize(self.address)
@classmethod
def from_normalized(cls, normalized: str) -> 'CellAddressCodec':
"""Rebuild from normalize output (best effort); a validate failure raises ValueError."""
return cls(normalized.replace('__', '/'), validate=True)
# -- from / to ---------------------------------------------
@classmethod
def make(cls, role: CellRole, name: CellName, uid: str) -> 'CellAddressCodec':
"""Build an address from three segments (role/name/uid)."""
if not name:
raise ValueError(
f'address must have at least one middle segment (kind={role!r}, uid={uid!r})'
)
elif not uid:
raise ValueError(f'address uid must be non-empty (kind={role!r})')
elif role not in ROLES:
raise ValueError(f'address role must be in ROLES {ROLES}')
for seg in (role, name, uid):
if '/' in seg:
raise ValueError(f'address segment must not contain "/": {seg!r}')
return cls('/'.join([role, name, uid]))
@classmethod
def parse(cls, address: CellAddress) -> tuple[CellRole, CellName, str]:
"""Parse the three segments (role, name, uid)."""
parts = address.split('/')
if len(parts) != 3:
raise ValueError(
f'address must have at least 3 segments (kind/middle+/uid), got {address!r}'
)
role, name, uid = parts
if role not in ROLES:
raise ValueError(
f'address[0] must be in CellRole {ROLES}, got {role!r}'
)
elif not name or not uid:
raise ValueError(f'address {address} parts should not be empty')
return role, name, uid # type: ignore[return-value]
@classmethod
def normalize(cls, name_or_address: str) -> str:
"""Normalize into a filesystem-safe name: ``/`` ``.`` ``-`` -> ``__``."""
return (name_or_address.replace('/', '__').replace('\\', '__').
replace('.', '__').replace('-', '__'))
def __str__(self) -> str:
return self.address
def __repr__(self) -> str:
return f'CellAddressCodec({self.address})'
# -- 匹配 -------------------------------------------------
def match(self, query: str) -> bool:
"""Whether ``query`` matches this address.
Five paths, by priority: exact full address -> exact normalized address -> exact
name segment -> uid prefix (>= 3 chars) -> address prefix (>= 3 chars). An empty /
one-two char query never matches — a semantic threshold against false matches.
"""
if not query:
return False
if query == self.address:
return True
if query == self.normalized:
return True
if query == self.name:
return True
if len(query) >= 3:
if self.uid.startswith(query):
return True
if self.address.startswith(query):
return True
return False
@classmethod
def suggest(
cls,
query: str,
candidates: Iterable[CellAddress],
*,
limit: int = 3,
) -> list[CellAddress]:
"""did you want? — collect approximate hits from candidates (name prefix/substring,
uid prefix).
A fallback hint for a parse failure or ambiguity, turning fuzzy input into a
correctable dialog. Returns the full addresses of the candidates.
"""
if not query:
return []
q = query.lower()
scored: list[tuple[int, CellAddress]] = []
for addr in candidates:
try:
_, name, uid = parse_address(addr)
except ValueError:
continue
nl, ul = name.lower(), uid.lower()
score = 0
if ul.startswith(q):
score += 3
if nl == q:
score += 4
elif nl.startswith(q):
score += 2
elif q in nl:
score += 1
if score:
scored.append((score, addr))
scored.sort(key=lambda t: (-t[0], t[1]))
return [addr for _, addr in scored[:limit]]
def build_cell_from_node(
env: Environment,
manifest: 'NodeManifest',
*,
uid: str | None = None,
) -> 'Cell':
"""
Build a Cell instance from a Node declaration.
:param env: the environment carrier.
:param manifest: this cell's NodeManifest.
:param uid: the cell uid; a short random one is generated when None.
"""
uid = uid or short_uid()
cell_name = manifest.name
if manifest.file:
# 以发现 node 声明文件的位置作为 cell 的 workspace.
home = Path(manifest.file).parent.resolve()
else:
# 在 workspace 内部为 cell 创建一个临时的 workspace.
# fullname 表达与 Cell.fullname property 同源 — 二者变则同变.
if manifest.category:
fullname = '_'.join([manifest.category, normalize(cell_name)])
else:
fullname = normalize(cell_name)
home = env.cell_runtimes_dir.joinpath(fullname).resolve()
# NodeManifest 暂未暴露 event_level; 显式值优先, persist 仅作未声明时的兜底.
event_level = getattr(manifest, 'event_level', None)
if event_level is None:
event_level = None if manifest.persist else CellEventLevel.DEBUG
return Cell(
role=NODE_ROLE,
name=cell_name,
category=manifest.category,
persist=manifest.persist,
uid=uid,
singleton=manifest.singleton,
event_level=event_level,
project_id=env.project_id,
project_name=env.project_name,
home=str(home.absolute()),
)
def build_host_cell(
env: Environment,
) -> 'Cell':
"""
Build a host-type cell.
host address = host / {moss_name} / {project_id}
- moss_name comes from MOSS.md.name (the workspace static declaration).
- project_id acts as the uid, unique within one project.
- singleton=True: a project can run at most one host.
"""
return Cell(
role=HOST_ROLE,
name=env.moss_meta.name,
uid=env.project_id,
category='',
persist=True,
singleton=True,
project_id=env.project_id,
project_name=env.project_name,
home=str(env.workspace_path.absolute())
)
def discover_this_node(
env: Environment,
) -> CellRuntimeInfo:
"""Discover the running cell runtime info from the current runtime.
Paths:
1. env.this_cell_address set -> read the file the parent wrote into the runtime dir (spawn path).
2. runtime file missing or corrupt -> degrade to self-description from the current process (from_proc + build).
3. env.this_cell_address empty -> go straight to the from_proc branch (bare script run).
Finally, override runtime_info.pid with the current process pid.
"""
address = env.this_cell_address
cell_runtime_info: CellRuntimeInfo | None = None
if address:
cell_runtime_info = CellRuntimeInfo.read_from_runtime_dir(env.cell_runtimes_dir, address)
if cell_runtime_info is None:
manifest = NodeManifest.from_proc()
cell = build_cell_from_node(env, manifest)
cell_runtime_info = CellRuntimeInfo(
address=address or cell.address,
pid=env.pid,
cell=cell,
)
cell_runtime_info.pid = env.pid
return cell_runtime_info
def clear_cell_runtimes(
env: Environment,
kill: Callable[[CellRuntimeInfo], None],
*,
throw: bool = False
):
for found in CellRuntimeInfo.iter_runtime_info(env.cell_runtimes_dir):
try:
if found.is_alive():
kill(found)
found.delete_invalid(env.cell_runtimes_dir)
except Exception as e:
if throw:
raise e
def enter_cell_lifecycle(
stack: contextlib.ExitStack,
env: Environment,
runtime_info: CellRuntimeInfo,
kill: Callable[[CellRuntimeInfo], None],
):
if runtime_info.cell.singleton:
# 单写者纪律 (§UU-6): singleton cell 的进程锁由 cell 自身争抢, 且必须