chimericmesh 0.2.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.
Files changed (147) hide show
  1. chimericmesh/__init__.py +3 -0
  2. chimericmesh/__main__.py +119 -0
  3. chimericmesh/agent/__init__.py +21 -0
  4. chimericmesh/agent/codesearch.py +131 -0
  5. chimericmesh/agent/digest.py +131 -0
  6. chimericmesh/agent/errors.py +38 -0
  7. chimericmesh/agent/feed.py +47 -0
  8. chimericmesh/agent/limits.py +98 -0
  9. chimericmesh/agent/ports.py +32 -0
  10. chimericmesh/agent/routes.py +92 -0
  11. chimericmesh/agent/service.py +116 -0
  12. chimericmesh/agent/snippet.py +135 -0
  13. chimericmesh/agent/verify.py +215 -0
  14. chimericmesh/agent/watch.py +156 -0
  15. chimericmesh/api.py +626 -0
  16. chimericmesh/approvals/__init__.py +25 -0
  17. chimericmesh/approvals/errors.py +72 -0
  18. chimericmesh/approvals/format.py +76 -0
  19. chimericmesh/approvals/irc.py +54 -0
  20. chimericmesh/approvals/ledger.py +149 -0
  21. chimericmesh/approvals/limits.py +87 -0
  22. chimericmesh/approvals/manager.py +935 -0
  23. chimericmesh/approvals/matching.py +107 -0
  24. chimericmesh/approvals/models.py +251 -0
  25. chimericmesh/approvals/ports.py +36 -0
  26. chimericmesh/approvals/routes.py +220 -0
  27. chimericmesh/approvals/rulebook.py +337 -0
  28. chimericmesh/approvals/schema.py +64 -0
  29. chimericmesh/approvals/scope.py +104 -0
  30. chimericmesh/approvals/validate.py +215 -0
  31. chimericmesh/catalog/demo_apps.json +41 -0
  32. chimericmesh/catalog/legacy_apps.json +612 -0
  33. chimericmesh/commands.py +223 -0
  34. chimericmesh/connectors/__init__.py +1 -0
  35. chimericmesh/connectors/config.py +55 -0
  36. chimericmesh/connectors/framing.py +132 -0
  37. chimericmesh/connectors/limiter.py +42 -0
  38. chimericmesh/connectors/manager.py +252 -0
  39. chimericmesh/connectors/offsets.py +157 -0
  40. chimericmesh/connectors/schema.py +31 -0
  41. chimericmesh/connectors/submit.py +26 -0
  42. chimericmesh/connectors/syslog_tcp.py +141 -0
  43. chimericmesh/connectors/tailer.py +236 -0
  44. chimericmesh/connectors/tailread.py +288 -0
  45. chimericmesh/demo/__init__.py +1 -0
  46. chimericmesh/demo/agent.py +32 -0
  47. chimericmesh/demo/apps/__init__.py +1 -0
  48. chimericmesh/demo/apps/__main__.py +83 -0
  49. chimericmesh/demo/apps/common.py +163 -0
  50. chimericmesh/demo/apps/mail_bridge.py +48 -0
  51. chimericmesh/demo/apps/orders_api.py +31 -0
  52. chimericmesh/demo/apps/spooler.py +28 -0
  53. chimericmesh/demo/config.py +38 -0
  54. chimericmesh/demo/engine.py +68 -0
  55. chimericmesh/demo/env.py +46 -0
  56. chimericmesh/demo/hub.py +172 -0
  57. chimericmesh/demo/launcher.py +165 -0
  58. chimericmesh/demo/ports.py +44 -0
  59. chimericmesh/demo/routes.py +81 -0
  60. chimericmesh/demo/samples.py +32 -0
  61. chimericmesh/demo/scenario.py +95 -0
  62. chimericmesh/demo/supervisor.py +267 -0
  63. chimericmesh/demo/watch.py +46 -0
  64. chimericmesh/diagnostics.py +253 -0
  65. chimericmesh/discovery/__init__.py +17 -0
  66. chimericmesh/discovery/accept.py +131 -0
  67. chimericmesh/discovery/catalog.py +313 -0
  68. chimericmesh/discovery/collect.py +131 -0
  69. chimericmesh/discovery/dismissals.py +46 -0
  70. chimericmesh/discovery/engine.py +41 -0
  71. chimericmesh/discovery/errors.py +34 -0
  72. chimericmesh/discovery/expand.py +151 -0
  73. chimericmesh/discovery/manager.py +220 -0
  74. chimericmesh/discovery/match.py +177 -0
  75. chimericmesh/discovery/models.py +174 -0
  76. chimericmesh/discovery/parse.py +166 -0
  77. chimericmesh/discovery/probe.py +69 -0
  78. chimericmesh/discovery/routes.py +129 -0
  79. chimericmesh/discovery/runner.py +147 -0
  80. chimericmesh/discovery/schema.py +25 -0
  81. chimericmesh/discovery/text.py +53 -0
  82. chimericmesh/events.py +74 -0
  83. chimericmesh/fsperms.py +72 -0
  84. chimericmesh/hub.py +757 -0
  85. chimericmesh/listeners.py +162 -0
  86. chimericmesh/mcp_server.py +199 -0
  87. chimericmesh/mute/__init__.py +10 -0
  88. chimericmesh/mute/models.py +14 -0
  89. chimericmesh/mute/mutelist.py +51 -0
  90. chimericmesh/mute/routes.py +49 -0
  91. chimericmesh/mute/schema.py +29 -0
  92. chimericmesh/mute/store.py +94 -0
  93. chimericmesh/notify.py +47 -0
  94. chimericmesh/parsers/__init__.py +12 -0
  95. chimericmesh/parsers/formats.py +23 -0
  96. chimericmesh/parsers/irc.py +91 -0
  97. chimericmesh/parsers/logctx.py +143 -0
  98. chimericmesh/parsers/ncsa.py +57 -0
  99. chimericmesh/parsers/plain.py +17 -0
  100. chimericmesh/parsers/syslog.py +82 -0
  101. chimericmesh/parsers/syslogtext.py +62 -0
  102. chimericmesh/parsers/telnet.py +56 -0
  103. chimericmesh/parsers/w3c.py +58 -0
  104. chimericmesh/parsers/xferlog.py +31 -0
  105. chimericmesh/replay.py +108 -0
  106. chimericmesh/replay_irc.py +202 -0
  107. chimericmesh/services/__init__.py +39 -0
  108. chimericmesh/services/access.py +224 -0
  109. chimericmesh/services/asgi.py +107 -0
  110. chimericmesh/services/deps.py +42 -0
  111. chimericmesh/services/errors.py +49 -0
  112. chimericmesh/services/limiter.py +130 -0
  113. chimericmesh/services/models.py +160 -0
  114. chimericmesh/services/paths.py +139 -0
  115. chimericmesh/services/registry.py +638 -0
  116. chimericmesh/services/routes.py +161 -0
  117. chimericmesh/services/schema.py +48 -0
  118. chimericmesh/services/status.py +239 -0
  119. chimericmesh/services/tagging.py +63 -0
  120. chimericmesh/services/token.py +129 -0
  121. chimericmesh/services/validate.py +184 -0
  122. chimericmesh/store/__init__.py +43 -0
  123. chimericmesh/store/audit.py +89 -0
  124. chimericmesh/store/config.py +88 -0
  125. chimericmesh/store/connection.py +98 -0
  126. chimericmesh/store/errors.py +65 -0
  127. chimericmesh/store/events.py +430 -0
  128. chimericmesh/store/incidents.py +292 -0
  129. chimericmesh/store/migrations.py +109 -0
  130. chimericmesh/store/models.py +49 -0
  131. chimericmesh/store/ports.py +61 -0
  132. chimericmesh/store/proposals.py +215 -0
  133. chimericmesh/store/schema.py +102 -0
  134. chimericmesh/store/store.py +173 -0
  135. chimericmesh/store/tx.py +143 -0
  136. chimericmesh/store/writer.py +315 -0
  137. chimericmesh/web/__init__.py +1 -0
  138. chimericmesh/web/feed.py +212 -0
  139. chimericmesh/web/index.html +3775 -0
  140. chimericmesh/web/sse.py +74 -0
  141. chimericmesh/web/stream.py +64 -0
  142. chimericmesh-0.2.2.dist-info/METADATA +537 -0
  143. chimericmesh-0.2.2.dist-info/RECORD +147 -0
  144. chimericmesh-0.2.2.dist-info/WHEEL +5 -0
  145. chimericmesh-0.2.2.dist-info/entry_points.txt +2 -0
  146. chimericmesh-0.2.2.dist-info/licenses/LICENSE +21 -0
  147. chimericmesh-0.2.2.dist-info/top_level.txt +1 -0
@@ -0,0 +1,156 @@
1
+ """The watch queue: what the agent should look at now, and the long poll that waits for it (R4.3, R4.4).
2
+ ``evaluate_work`` is pure and total. Sub-criteria 4.3a.1 to 4.3a.6 and 4.3b follow the design (review L8).
3
+ """
4
+ from __future__ import annotations
5
+ import asyncio
6
+ from collections.abc import Awaitable, Callable, Mapping, Sequence
7
+ from dataclasses import dataclass
8
+ from chimericmesh.diagnostics import Incident
9
+ from chimericmesh.store.models import Proposal
10
+ from .errors import TooManyWaiters
11
+ from .limits import MAX_WAITERS, TICK, WAIT_MAX, WAIT_MIN, clean_mapping, clean_text
12
+ from .ports import AgentPorts
13
+ UNASSIGNED = "unassigned"
14
+ MAX_WORK = 5
15
+ SAMPLE_LIMIT = 200
16
+ @dataclass(frozen=True, slots=True)
17
+ class WorkResult:
18
+ incidents: tuple[dict, ...]
19
+ approved_proposals: tuple[dict, ...]
20
+ @property
21
+ def has_work(self) -> bool:
22
+ return bool(self.incidents or self.approved_proposals)
23
+ def _service_ok(sid: str | None, wanted: str | None, registered: Callable[[str], bool]) -> bool:
24
+ """4.3a.2 and 4.3a.6 (and the same filter for proposals in 4.3b)."""
25
+ if sid is None:
26
+ return wanted == UNASSIGNED
27
+ if wanted is not None and wanted != UNASSIGNED and sid != wanted:
28
+ return False
29
+ if wanted == UNASSIGNED:
30
+ return False
31
+ try:
32
+ return bool(registered(sid))
33
+ except Exception: # noqa: BLE001 - total by contract
34
+ return False
35
+ def _incident_view(inc: Incident) -> dict:
36
+ return {"id": clean_text(inc.id), "service_id": None if inc.service_id is None else clean_text(inc.service_id),
37
+ "host": clean_text(inc.host), "category": clean_text(inc.category), "severity": inc.severity,
38
+ "count": inc.count, "opening": inc.opening, "first_seen": inc.first_seen,
39
+ "last_seen": inc.last_seen, "sample": clean_text(inc.sample, SAMPLE_LIMIT)}
40
+ def _proposal_view(prop: Proposal, approval_of: Callable[[Proposal], dict | None]) -> dict:
41
+ try:
42
+ approval = clean_mapping(approval_of(prop))
43
+ except Exception: # noqa: BLE001
44
+ approval = None
45
+ files = prop.files if isinstance(prop.files, list) else None
46
+ return {"id": clean_text(prop.id), "incident_id": clean_text(prop.incident_id),
47
+ "service_id": None if prop.service_id is None else clean_text(prop.service_id),
48
+ "action": clean_text(prop.action),
49
+ "files": None if files is None else [clean_text(f) for f in files[:200]],
50
+ "estimated_lines": prop.estimated_lines,
51
+ "verification": None if prop.verification is None else clean_text(prop.verification),
52
+ "approval": approval}
53
+ def _proposal_order(prop: Proposal) -> tuple:
54
+ return (prop.created, len(prop.id), prop.id)
55
+ def evaluate_work(incidents: Sequence[Incident], proposals: Sequence[Proposal], *, service_id: str | None,
56
+ exclude: frozenset[str], registered: Callable[[str], bool],
57
+ approval_of: Callable[[Proposal], dict | None], limit: int = MAX_WORK) -> WorkResult:
58
+ """The incidents (4.3a) and approved proposals (4.3b) the agent should act on. At most ``limit`` of each."""
59
+ blocked: set[str] = set() # 4.3a.3: an active proposal
60
+ denied_openings: set[tuple[str, int | None]] = set() # 4.3a.5: a user denial, per opening
61
+ approved: list[Proposal] = []
62
+ for prop in proposals:
63
+ if prop.status in ("pending", "approved"):
64
+ blocked.add(prop.incident_id)
65
+ elif prop.status == "denied" and prop.decision_origin == "user":
66
+ denied_openings.add((prop.incident_id, prop.incident_opening))
67
+ if prop.status == "approved" and prop.id not in exclude and _service_ok(prop.service_id, service_id, registered):
68
+ approved.append(prop)
69
+ work: list[Incident] = []
70
+ for inc in incidents:
71
+ if inc.resolved or inc.id in exclude or inc.id in blocked: # 4.3a.1, 4.3a.4, 4.3a.3
72
+ continue
73
+ if (inc.id, inc.opening) in denied_openings: # 4.3a.5
74
+ continue
75
+ if not _service_ok(inc.service_id, service_id, registered): # 4.3a.2, 4.3a.6
76
+ continue
77
+ work.append(inc)
78
+ work.sort(key=lambda i: (i.severity, -i.count, i.id))
79
+ approved.sort(key=_proposal_order)
80
+ cap = max(0, limit)
81
+ return WorkResult(tuple(_incident_view(i) for i in work[:cap]),
82
+ tuple(_proposal_view(p, approval_of) for p in approved[:cap]))
83
+
84
+
85
+ def _timeout() -> dict:
86
+ return {"status": "timeout", "incidents": [], "approved_proposals": []}
87
+
88
+
89
+ class WatchQueue:
90
+ """The long poll. Evaluate, return at once when there is work, otherwise wait on a per-waiter event for
91
+ one tick and evaluate again. No thread, no blocking sleep: a held waiter is a coroutine (G4)."""
92
+
93
+ def __init__(self, ports: AgentPorts, *, wait_min: float = WAIT_MIN, wait_max: float = WAIT_MAX,
94
+ tick: float = TICK, max_waiters: int = MAX_WAITERS) -> None:
95
+ self._ports = ports
96
+ self.wait_min, self.wait_max = wait_min, wait_max
97
+ self._tick = tick
98
+ self._max = max_waiters
99
+ self._waiters: set[asyncio.Event] = set()
100
+ self._closed = False
101
+ self.waiting = 0
102
+
103
+ def _registered(self, sid: str) -> bool:
104
+ return self._ports.registry.get(sid) is not None
105
+
106
+ def _approval_of(self, prop: Proposal) -> dict | None:
107
+ reader = self._ports.approvals
108
+ return None if reader is None else reader.approval_of(prop)
109
+
110
+ def _evaluate(self, service_id: str | None, exclude: frozenset[str]) -> WorkResult:
111
+ state = self._ports.state
112
+ return evaluate_work(state.incidents(status="open"), state.list_proposals(None), service_id=service_id,
113
+ exclude=exclude, registered=self._registered, approval_of=self._approval_of)
114
+
115
+ def kick(self) -> None:
116
+ """Wake every waiter now (a future hub listener, and tests)."""
117
+ for waiter in tuple(self._waiters):
118
+ waiter.set()
119
+
120
+ def close(self) -> None:
121
+ """Shutdown: every held waiter returns ``timeout`` at once, and so does any later call."""
122
+ self._closed = True
123
+ self.kick()
124
+
125
+ async def next(self, *, timeout: float, service_id: str | None, exclude: frozenset[str],
126
+ is_disconnected: Callable[[], Awaitable[bool]] | None = None) -> dict:
127
+ """Return work as soon as there is some, or ``timeout`` after ``timeout`` seconds. Raises
128
+ ``TooManyWaiters`` when every slot is held. The slot is freed in ``finally``."""
129
+ if self._closed:
130
+ return _timeout()
131
+ if self.waiting >= self._max:
132
+ raise TooManyWaiters(retry_after=5)
133
+ self.waiting += 1
134
+ waiter = asyncio.Event()
135
+ self._waiters.add(waiter)
136
+ try:
137
+ loop = asyncio.get_running_loop()
138
+ deadline = loop.time() + timeout
139
+ while True:
140
+ waiter.clear() # before reading: no lost kick
141
+ result = self._evaluate(service_id, exclude) # synchronous: a consistent snapshot
142
+ if result.has_work:
143
+ return {"status": "work", "incidents": list(result.incidents),
144
+ "approved_proposals": list(result.approved_proposals)}
145
+ left = deadline - loop.time()
146
+ if left <= 0 or self._closed:
147
+ return _timeout()
148
+ if is_disconnected is not None and await is_disconnected(): # once per tick (R1-M1)
149
+ return _timeout()
150
+ try:
151
+ await asyncio.wait_for(waiter.wait(), min(self._tick, left))
152
+ except asyncio.TimeoutError:
153
+ pass
154
+ finally:
155
+ self.waiting -= 1
156
+ self._waiters.discard(waiter)