@johnhenry/browsermesh-apps 0.0.0

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.
@@ -0,0 +1,255 @@
1
+ /**
2
+ // STATUS: INTEGRATED — wired into ClawserPod lifecycle, proven via E2E testing
3
+ * clawser-mesh-devtools.js -- Mesh DevTools Inspector.
4
+ *
5
+ * Provides diagnostic snapshot and health checking for the mesh subsystem.
6
+ * Exposed as a BrowserTool so the AI agent can query mesh state.
7
+ *
8
+ * No browser-only imports at module level.
9
+ *
10
+ * Run tests:
11
+ * node --import ./web/test/_setup-globals.mjs --test web/test/clawser-mesh-devtools.test.mjs
12
+ */
13
+
14
+ const BrowserTool = globalThis.BrowserTool || class { constructor() {} }
15
+
16
+ /**
17
+ * Diagnostic inspector for mesh subsystem state.
18
+ */
19
+ export class MeshInspector {
20
+ #state
21
+
22
+ /**
23
+ * @param {object} state - The global state object containing mesh subsystem references
24
+ */
25
+ constructor(state) {
26
+ if (!state) throw new Error('state is required')
27
+ this.#state = state
28
+ }
29
+
30
+ /**
31
+ * Take a complete snapshot of mesh subsystem state.
32
+ * @returns {object}
33
+ */
34
+ snapshot() {
35
+ const s = this.#state
36
+ return {
37
+ pod: {
38
+ podId: s.pod?.podId || null,
39
+ state: s.pod?.state || 'unknown',
40
+ },
41
+ peerNode: {
42
+ podId: s.peerNode?.podId || null,
43
+ state: s.peerNode?.state || 'unknown',
44
+ peerCount: s.peerNode?.peerCount ?? 0,
45
+ },
46
+ swarm: {
47
+ active: !!s.swarmCoordinator,
48
+ swarmCount: s.swarmCoordinator?.listSwarms?.()?.length ?? 0,
49
+ },
50
+ discovery: {
51
+ active: !!s.discoveryManager,
52
+ },
53
+ transport: {
54
+ active: !!s.transportNegotiator,
55
+ },
56
+ audit: {
57
+ active: !!s.auditChain,
58
+ entryCount: s.auditChain?.length ?? s.auditChain?.size ?? 0,
59
+ },
60
+ streams: {
61
+ active: !!s.streamMultiplexer,
62
+ },
63
+ files: {
64
+ active: !!s.fileTransfer,
65
+ },
66
+ services: {
67
+ active: !!s.serviceDirectory,
68
+ },
69
+ sync: {
70
+ active: !!s.syncEngine,
71
+ },
72
+ resources: {
73
+ active: !!s.resourceRegistry,
74
+ count: s.resourceRegistry?.listAll?.()?.length ?? 0,
75
+ },
76
+ marketplace: {
77
+ active: !!s.meshMarketplace,
78
+ stats: s.meshMarketplace?.getStats?.() || null,
79
+ },
80
+ quotas: {
81
+ managerActive: !!s.quotaManager,
82
+ enforcerActive: !!s.quotaEnforcer,
83
+ },
84
+ payments: {
85
+ active: !!s.paymentRouter,
86
+ },
87
+ consensus: {
88
+ active: !!s.consensusManager,
89
+ proposalCount: s.consensusManager?.size ?? 0,
90
+ },
91
+ relay: {
92
+ active: !!s.relayClient,
93
+ },
94
+ naming: {
95
+ active: !!s.nameResolver,
96
+ },
97
+ apps: {
98
+ registryActive: !!s.appRegistry,
99
+ storeActive: !!s.appStore,
100
+ stats: s.appRegistry?.getStats?.() || null,
101
+ },
102
+ orchestrator: {
103
+ active: !!s.orchestrator,
104
+ peerCount: s.orchestrator?.peerCount ?? 0,
105
+ },
106
+ connectivity: {
107
+ active: !!s.webrtcMeshManager,
108
+ connectionCount: s.webrtcMeshManager?.connectionCount ?? 0,
109
+ // Synchronous read of the last getAllConnectionStats() call — the
110
+ // Tracer/health-metrics poller refreshes this, snapshot() can't
111
+ // await a live RTCPeerConnection.getStats() call.
112
+ stats: s.webrtcMeshManager?.lastStats ?? [],
113
+ },
114
+ }
115
+ }
116
+
117
+ /**
118
+ * Run health checks on all subsystems.
119
+ * @returns {{ overall: 'healthy'|'degraded'|'unhealthy', checks: Array<{ name: string, status: string, detail?: string }> }}
120
+ */
121
+ healthCheck() {
122
+ const checks = []
123
+ const s = this.#state
124
+
125
+ // Pod check
126
+ checks.push({
127
+ name: 'pod',
128
+ status: s.pod ? 'ok' : 'missing',
129
+ detail: s.pod ? `podId: ${s.pod.podId || 'unknown'}` : 'Pod not initialized',
130
+ })
131
+
132
+ // PeerNode check
133
+ checks.push({
134
+ name: 'peerNode',
135
+ status: s.peerNode ? 'ok' : 'missing',
136
+ })
137
+
138
+ // Core subsystems
139
+ const coreNames = [
140
+ ['swarmCoordinator', 'swarm'],
141
+ ['discoveryManager', 'discovery'],
142
+ ['transportNegotiator', 'transport'],
143
+ ['auditChain', 'audit'],
144
+ ['streamMultiplexer', 'streams'],
145
+ ['fileTransfer', 'files'],
146
+ ['serviceDirectory', 'services'],
147
+ ['syncEngine', 'sync'],
148
+ ]
149
+ for (const [key, name] of coreNames) {
150
+ checks.push({ name, status: s[key] ? 'ok' : 'missing' })
151
+ }
152
+
153
+ // Extended subsystems
154
+ const extNames = [
155
+ ['resourceRegistry', 'resources'],
156
+ ['meshMarketplace', 'marketplace'],
157
+ ['quotaManager', 'quotas'],
158
+ ['paymentRouter', 'payments'],
159
+ ['consensusManager', 'consensus'],
160
+ ['relayClient', 'relay'],
161
+ ['nameResolver', 'naming'],
162
+ ['appRegistry', 'apps'],
163
+ ['orchestrator', 'orchestrator'],
164
+ ]
165
+ for (const [key, name] of extNames) {
166
+ checks.push({ name, status: s[key] ? 'ok' : 'missing' })
167
+ }
168
+
169
+ // Determine overall health
170
+ const missingCount = checks.filter(c => c.status === 'missing').length
171
+ let overall = 'healthy'
172
+ if (missingCount > 0 && missingCount <= 5) overall = 'degraded'
173
+ if (missingCount > 5) overall = 'unhealthy'
174
+ // If pod or peerNode missing, always unhealthy
175
+ if (!s.pod || !s.peerNode) overall = 'unhealthy'
176
+
177
+ return { overall, checks }
178
+ }
179
+
180
+ /**
181
+ * Generate a markdown report of mesh state.
182
+ * @returns {string}
183
+ */
184
+ toMarkdownReport() {
185
+ const snap = this.snapshot()
186
+ const health = this.healthCheck()
187
+
188
+ let md = `# Mesh Inspector Report\n\n`
189
+ md += `**Overall Health:** ${health.overall}\n\n`
190
+ md += `## Pod\n- ID: ${snap.pod.podId || 'N/A'}\n- State: ${snap.pod.state}\n\n`
191
+ md += `## Peer Node\n- ID: ${snap.peerNode.podId || 'N/A'}\n- Peers: ${snap.peerNode.peerCount}\n\n`
192
+ md += `## Health Checks\n`
193
+ for (const check of health.checks) {
194
+ const icon = check.status === 'ok' ? 'OK' : 'MISSING'
195
+ md += `- ${check.name}: ${icon}${check.detail ? ` (${check.detail})` : ''}\n`
196
+ }
197
+ return md
198
+ }
199
+ }
200
+
201
+ /**
202
+ * BrowserTool exposing mesh inspection to the AI agent.
203
+ */
204
+ export class MeshInspectTool extends BrowserTool {
205
+ #inspector
206
+
207
+ constructor(state) {
208
+ super()
209
+ this.#inspector = new MeshInspector(state)
210
+ }
211
+
212
+ get name() { return 'mesh_inspect' }
213
+ get description() { return 'Inspect mesh subsystem state, health, and diagnostics' }
214
+ get parameters() {
215
+ return {
216
+ type: 'object',
217
+ properties: {
218
+ section: {
219
+ type: 'string',
220
+ description: 'Optional: specific section to inspect (pod, peers, health, full). Default: full',
221
+ },
222
+ },
223
+ }
224
+ }
225
+ get permission() { return 'read' }
226
+
227
+ async execute({ section } = {}) {
228
+ try {
229
+ switch (section) {
230
+ case 'health': {
231
+ const health = this.#inspector.healthCheck()
232
+ return { success: true, output: JSON.stringify(health, null, 2) }
233
+ }
234
+ case 'pod': {
235
+ const snap = this.#inspector.snapshot()
236
+ return { success: true, output: JSON.stringify(snap.pod, null, 2) }
237
+ }
238
+ case 'peers': {
239
+ const snap = this.#inspector.snapshot()
240
+ return { success: true, output: JSON.stringify(snap.peerNode, null, 2) }
241
+ }
242
+ case 'report':
243
+ case 'markdown': {
244
+ return { success: true, output: this.#inspector.toMarkdownReport() }
245
+ }
246
+ default: {
247
+ const snap = this.#inspector.snapshot()
248
+ return { success: true, output: JSON.stringify(snap, null, 2) }
249
+ }
250
+ }
251
+ } catch (err) {
252
+ return { success: false, output: '', error: `Inspection failed: ${err.message}` }
253
+ }
254
+ }
255
+ }