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
|
// module-world-model.js — Global World Model: system self-awareness synthesis engine
// Aggregates from memory graph, neural VFS, skills index, telemetry
// Commands: world-model, world-model-summary, world-model-ask, world-model-refresh, world-model-config
class WorldModelModule {
constructor() {
this.commands = {};
this.terminal = null;
this._model = null;
this._lastRefresh = null;
this._REFRESH_INTERVAL = 300000; // 5 minutes
this._MAX_PROJECTS = 10;
this._refreshTimer = null;
}
getHelpInfo() {
return {
'world-model': 'world-model — Show full world model as structured output',
'world-model-summary': 'world-model-summary — One-line system self-awareness statement',
'world-model-ask': 'world-model-ask <question> — Query the model (e.g. "what changed since yesterday")',
'world-model-refresh': 'world-model-refresh — Force re-synthesis from all subsystems',
'world-model-config': 'world-model-config <key> <value> — Tune: refresh-interval, max-projects',
};
}
registerCommands() {
this.commands['world-model'] = () => this.showModel();
this.commands['world-model-summary'] = () => this.showSummary();
this.commands['world-model-ask'] = (args) => this.ask(args.join(' '));
this.commands['world-model-refresh'] = () => this.refresh();
this.commands['world-model-config'] = (args) => this.setConfig(args);
}
init(terminal) {
this.terminal = terminal;
this._loadFromVFS();
// Auto-refresh on a timer
this._refreshTimer = setInterval(() => {
const age = Date.now() - (this._lastRefresh ? new Date(this._lastRefresh).getTime() : 0);
if (age > this._REFRESH_INTERVAL) this._synthesize();
}, this._REFRESH_INTERVAL);
// Initial synthesis
this._synthesize();
this.terminal.print('🌐 World Model loaded — system self-awareness active', 'info');
}
// ─── Synthesis Engine ───
_synthesize() {
try {
const fs = this.terminal.fs;
if (!fs) return;
const model = {
version: 'v4.4',
synthesized: new Date().toISOString(),
system: this._gatherSystemInfo(fs),
modules: this._gatherModuleInfo(),
files: this._gatherFileInfo(fs),
skills: this._gatherSkillInfo(fs),
memory: this._gatherMemoryInfo(),
agents: this._gatherAgentInfo(),
telemetry: this._gatherTelemetry(fs),
projects: this._gatherProjects(fs),
recent: this._gatherRecent(fs)
};
this._model = model;
this._lastRefresh = model.synthesized;
this._saveToVFS(fs);
// Log synthesis summary
const mCount = model.modules?.loaded || '?';
const sCount = model.skills?.count || '?';
const fCount = model.files?.total || '?';
console.log(`[WorldModel] Synthesis complete: ${mCount} modules, ${sCount} skills, ${fCount} files`);
} catch (e) {
console.error('[WorldModel] Synthesis error:', e.message);
}
}
_gatherSystemInfo(fs) {
const healthData = fs.fsReadJSON('/system/metrics/health.json');
return {
version: 'newOS4 v4.4',
modules: Object.keys(this.terminal?.moduleManager?.modules || {}).length || 0,
vfsSize: JSON.stringify(fs?.root || {}).length,
online: typeof navigator !== 'undefined' ? navigator.onLine : true,
memory: typeof performance !== 'undefined' && performance.memory
? `${(performance.memory.usedJSHeapSize / 1048576).toFixed(1)}MB`
: 'N/A',
health: healthData?.status || 'OK',
lastHealthCheck: healthData?.timestamp || null
};
}
_gatherModuleInfo() {
const mm = this.terminal?.moduleManager;
if (!mm) return { loaded: 0, modified: [] };
const modules = Object.keys(mm.modules || {}).map(name => {
const mod = mm.modules[name];
const clsName = mod?.constructor?.name || name;
return { name: clsName, hasCommands: !!mod?.commands };
});
// Check for local modifications
const modsDir = this.terminal.fs?.readDir('/_mods/');
const modified = modsDir
? modsDir.filter(e => e.type === 'file' && e.name.endsWith('.js') && !e.name.endsWith('.orig.js'))
.map(e => e.name.replace('.js', ''))
: [];
return {
loaded: modules.length,
total: 49,
modules,
modified,
modifiedCount: modified.length
};
}
_gatherFileInfo(fs) {
if (!fs) return { total: 0, size: '0KB' };
let fileCount = 0, dirCount = 0;
const walk = (path) => {
const entries = fs.ls(path);
if (!entries) return;
for (const e of entries) {
if (e.type === 'file') fileCount++;
else { dirCount++; walk(path === '/' ? '/' + e.name : path + '/' + e.name); }
}
};
walk('/');
return {
total: fileCount,
directories: dirCount,
size: `${(JSON.stringify(fs.root).length / 1024).toFixed(1)}KB`
};
}
_gatherSkillInfo(fs) {
try {
const idx = fs.fsReadJSON('/skills/index.json');
const count = idx ? Object.keys(idx).length : 0;
return { count, skills: idx ? Object.keys(idx) : [] };
} catch (e) {
return { count: 0, skills: [] };
}
}
_gatherMemoryInfo() {
const mg = this.terminal?.moduleManager?.modules?.['MemoryGraphModule'];
if (mg && mg._nodes) {
const nodes = Object.keys(mg._nodes).length;
const edges = (mg._edges || []).length;
return { nodes, edges, active: true };
}
// Fallback: count memory files
try {
const memIndex = this.terminal.fs?.fsReadJSON('/memory/index.json');
return { nodes: memIndex ? Object.keys(memIndex).length : 0, edges: 0, active: false };
} catch (e) {
return { nodes: 0, edges: 0, active: false };
}
}
_gatherAgentInfo() {
const orch = this.terminal?.moduleManager?.modules?.['OrchestratorModule'];
if (!orch || !orch._agents) return { agents: 0, active: 0 };
const agents = Object.keys(orch._agents).length;
const active = Object.values(orch._agents).filter(a => a.status === 'busy').length;
return { agents, active, totalLeases: Object.keys(orch._a2aMessages || {}).length };
}
_gatherTelemetry(fs) {
try {
const latency = fs.fsReadJSON('/system/metrics/tool-latency.json');
const errors = fs.fsReadJSON('/system/metrics/error-log.json');
return {
toolLatencyDataPoints: latency ? Object.keys(latency).length : 0,
recentErrors: errors ? (Array.isArray(errors) ? errors.length : Object.keys(errors).length) : 0,
};
} catch (e) {
return { toolLatencyDataPoints: 0, recentErrors: 0 };
}
}
_gatherProjects(fs) {
try {
const mg = this.terminal?.moduleManager?.modules?.['MemoryGraphModule'];
if (mg && mg._nodes) {
// Find task clusters as "projects"
const tasks = Object.values(mg._nodes).filter(n => n.type === 'task');
const recentFiles = Object.values(mg._nodes)
.filter(n => n.type === 'file')
.sort((a, b) => b.access_count - a.access_count)
.slice(0, this._MAX_PROJECTS);
return {
totalTasks: tasks.length,
activeProjects: recentFiles.slice(0, 5).map(f => ({
name: f.label,
accesses: f.access_count,
lastModified: f.updated
}))
};
}
return { totalTasks: 0, activeProjects: [] };
} catch (e) {
return { totalTasks: 0, activeProjects: [] };
}
}
_gatherRecent(fs) {
const recent = [];
try {
// Check modified modules
const modsDir = fs.readDir('/_mods/');
if (modsDir) {
for (const e of modsDir) {
if (e.type === 'file' && e.name.endsWith('.js') && !e.name.endsWith('.orig.js') && !e.name.endsWith('.meta.json')) {
recent.push(`Modified module: ${e.name.replace('.js', '')}`);
}
}
}
// Check skills activity
const skills = fs.ls('/skills/');
if (skills && skills.length > 0) {
recent.push(`Skills directory: ${skills.filter(s => s.type === 'file').length} files`);
}
} catch (e) {}
return recent;
}
// ─── Commands ───
showModel() {
if (!this._model) return this.terminal.print('No model yet — run world-model-refresh', 'warning');
const m = this._model;
this.terminal.print('🌐 World Model', 'header');
this.terminal.print(` Version: ${m.version} (last synthsized: ${new Date(m.synthesized).toLocaleTimeString()})`, 'info');
this.terminal.print(` ⚙️ System — ${m.system.modules} modules loaded, ${m.system.vfsSize} VFS, ${m.system.memory}`, 'info');
this.terminal.print(` 🧩 Modules — ${m.modules.loaded} loaded (${m.modules.modifiedCount} modified)`, 'info');
this.terminal.print(` 📁 Files — ${m.files.total} files, ${m.files.directories} dirs, ${m.files.size}`, 'info');
this.terminal.print(` 📚 Skills — ${m.skills.count} installed`, 'info');
this.terminal.print(` 🧠 Memory Graph — ${m.memory.nodes} nodes, ${m.memory.edges} edges${m.memory.active ? ' ✅' : ''}`, 'info');
this.terminal.print(` 🤖 Agents — ${m.agents.agents} registered (${m.agents.active} active)`, 'info');
this.terminal.print(` 📊 Telemetry — ${m.telemetry.toolLatencyDataPoints} tool latency points, ${m.telemetry.recentErrors} recent errors`, 'info');
if (m.projects.activeProjects.length > 0) {
this.terminal.print(' Active context:', 'header');
for (const p of m.projects.activeProjects.slice(0, 3)) {
this.terminal.print(` 📌 ${p.name} (${p.accesses}x accesses)`, 'info');
}
}
if (m.recent.length > 0) {
this.terminal.print(' Recent changes:', 'header');
for (const r of m.recent.slice(0, 3)) {
this.terminal.print(` ${r}`, 'info');
}
}
}
showSummary() {
if (!this._model) return this.terminal.print('No model yet', 'warning');
const m = this._model;
const summary = `newOS4 ${m.version} · ${m.modules.loaded}/${m.modules.total} modules · ${m.skills.count} skills · ${m.files.total} files · ${m.agents.agents} agents · ${m.memory.nodes} graph nodes · ${m.telemetry.recentErrors > 0 ? `⚠️ ${m.telemetry.recentErrors} errors` : '✅ OK'}`;
this.terminal.print(summary, 'info');
return summary;
}
ask(question) {
if (!question) return this.terminal.print('Usage: world-model-ask <question>', 'error');
if (!this._model) return this.terminal.print('No model — run world-model-refresh first', 'warning');
const q = question.toLowerCase();
const m = this._model;
let answer = '';
if (q.includes('module') || q.includes('loaded')) {
answer = `Loaded ${m.modules.loaded} modules (${m.modules.modifiedCount} modified: ${m.modules.modified.join(', ') || 'none'})`;
} else if (q.includes('skill')) {
answer = `Installed ${m.skills.count} skills: ${m.skills.skills.join(', ') || 'none'}`;
} else if (q.includes('file') || q.includes('vfs')) {
answer = `${m.files.total} files in ${m.files.directories} directories, total ${m.files.size}`;
} else if (q.includes('agent') || q.includes('orchestrat')) {
answer = `${m.agents.agents} agents (${m.agents.active} active)`;
} else if (q.includes('health') || q.includes('status')) {
answer = `Health: ${m.system.health} · Memory: ${m.system.memory} · ${m.telemetry.recentErrors > 0 ? `${m.telemetry.recentErrors} recent errors` : 'No errors'}`;
} else if (q.includes('memory') || q.includes('graph')) {
answer = `Memory Graph: ${m.memory.nodes} nodes, ${m.memory.edges} edges (${m.memory.active ? 'active' : 'inactive'})`;
} else if (q.includes('change') || q.includes('yesterday') || q.includes('recent')) {
const recent = m.recent;
answer = recent.length > 0 ? `Recent changes: ${recent.join('; ')}` : 'No recent changes detected';
} else if (q.includes('project') || q.includes('work')) {
const projects = m.projects.activeProjects;
answer = projects.length > 0
? `Active projects: ${projects.map(p => `${p.name} (${p.accesses}x)`).join(', ')}`
: 'No active projects detected';
} else if (q.includes('telemetry') || q.includes('latency')) {
answer = `${m.telemetry.toolLatencyDataPoints} tool latency data points tracked`;
} else if (q.includes('summary') || q.includes('overview') || q.includes('all')) {
answer = this.showSummary();
return;
} else {
answer = `I'm not sure how to answer "${question}". Try: modules, skills, files, agents, health, memory, changes, projects, telemetry, or summary.`;
}
this.terminal.print(`❓ ${question}`, 'info');
this.terminal.print(`💡 ${answer}`, 'success');
}
refresh() {
this._synthesize();
this.terminal.print(`✅ World Model refreshed at ${new Date().toLocaleTimeString()}`, 'success');
}
setConfig(args) {
if (args.length < 2) return this.terminal.print('Usage: world-model-config <key> <value>', 'error');
const key = args[0];
const val = args[1];
const configMap = {
'refresh-interval': () => {
const n = parseInt(val) * 1000;
if (isNaN(n) || n < 10000) return 'Must be >= 10 seconds';
this._REFRESH_INTERVAL = n;
if (this._refreshTimer) clearInterval(this._refreshTimer);
this._refreshTimer = setInterval(() => this._synthesize(), n);
return `refresh-interval set to ${val}s`;
},
'max-projects': () => {
const n = parseInt(val);
if (isNaN(n) || n < 1) return 'Must be >= 1';
this._MAX_PROJECTS = n;
return `max-projects set to ${n}`;
},
};
if (!configMap[key]) return this.terminal.print(`Unknown: ${key}. Valid: ${Object.keys(configMap).join(', ')}`, 'error');
const msg = configMap[key]();
this.terminal.print(`✅ ${msg}`, 'success');
}
// ─── Context Injection ───
getContextForPrompt() {
if (!this._model) return '';
const m = this._model;
return `[World Model Context]\\nSystem: newOS4 ${m.version} · ${m.modules.loaded} modules · ${m.skills.count} skills · ${m.files.total} files · ${m.agents.agents} agents${m.telemetry.recentErrors > 0 ? ` · ⚠️ ${m.telemetry.recentErrors} errors` : ' · ✅ OK'}${m.modules.modifiedCount > 0 ? `\\nModified modules: ${m.modules.modified.slice(0, 3).join(', ')}${m.modules.modifiedCount > 3 ? ` (+${m.modules.modifiedCount - 3} more)` : ''}` : ''}`;
}
// ─── Persistence ───
_loadFromVFS() {
try {
const fs = this.terminal.fs;
if (!fs) return;
const data = fs.fsReadJSON('/system/world-model.json');
if (data) {
this._model = data;
this._lastRefresh = data.synthesized;
}
} catch (e) {
console.warn('[WorldModel] Load error (fresh start):', e.message);
}
}
_saveToVFS(fs) {
try {
if (!fs.readDir('/system')) fs.mkdir('system');
fs.fsWriteJSON('/system/world-model.json', this._model);
} catch (e) {
console.error('[WorldModel] Save error:', e.message);
}
}
}
// Required for module system
if (typeof window !== 'undefined') {
window.WorldModelModule = WorldModelModule;
}
|