diff options
Diffstat (limited to 'modules')
| -rw-r--r-- | modules/core/module-aiassist.js | 10 | ||||
| -rw-r--r-- | modules/core/module-loader.js | 4 | ||||
| -rw-r--r-- | modules/core/module-orchestrator.js | 660 | ||||
| -rw-r--r-- | modules/core/module-system.js | 2 | ||||
| -rw-r--r-- | modules/core/terminal.js | 6 | ||||
| -rw-r--r-- | modules/core/virtual-fs.js | 109 | ||||
| -rw-r--r-- | modules/ext/alias.js | 26 | ||||
| -rw-r--r-- | modules/ext/floating.js | 331 | ||||
| -rw-r--r-- | modules/ext/shell.js | 2 | ||||
| -rw-r--r-- | modules/ext/skill-pack-core.js | 36 | ||||
| -rw-r--r-- | modules/test/test-suite.js | 54 |
11 files changed, 1095 insertions, 145 deletions
diff --git a/modules/core/module-aiassist.js b/modules/core/module-aiassist.js index ed76a8a..7f99386 100644 --- a/modules/core/module-aiassist.js +++ b/modules/core/module-aiassist.js @@ -93,6 +93,15 @@ SKILLS (PERSISTENT PROCEDURAL MEMORY): - create_skill(name, description, content, tags, category) — Save a new stored procedure. Call after completing complex multi-step tasks (3+ tool calls). - patch_skill(name, content) — Update an existing skill when the approach changes +FLOATING UI (PERSISTENT UI ELEMENTS): Use 'run_command' tool with these terminal commands: +- 'floating-add btn <id> <label> <command>' — Create a floating button that runs a terminal command when clicked +- 'floating-add menu <id> <label> <btn1>,<btn2>...' — Create a collapsible floating menu (buttons: label:command pairs) +- 'floating-add panel <id> <title> <content>' — Create a floating text/status panel +- 'floating-remove <id>' — Remove a floating element by ID +- 'floating-toggle <id>' — Show/hide a floating element without deleting +- 'floating-list' — List all floating elements +- 'floating-clear' — Remove all floating elements +- NOTE: These are terminal commands, run via 'run_command'. Use SPACES not hyphens between subcommands. MODULE SELF-IMPROVEMENT: - view_module(name) — View source code of any loaded module - edit_module(name) — Copy module into editor. Opens editor for modifications. @@ -1733,6 +1742,7 @@ RULES: const orch = this.terminal.moduleManager.modules['OrchestratorModule']; if (!orch) return JSON.stringify({ error: 'Orchestrator not available' }); const agent = orch.spawnAgent(role, caps); + if (!agent) return JSON.stringify({ error: 'Agent spawn failed — limit reached or concurrent spawn conflict' }); return JSON.stringify({ message: `Agent spawned: ${agent.id}`, agent }); } case 'load_skills': { diff --git a/modules/core/module-loader.js b/modules/core/module-loader.js index 1d1cbda..96fee00 100644 --- a/modules/core/module-loader.js +++ b/modules/core/module-loader.js @@ -105,7 +105,7 @@ window.__moduleLoaderReload = reloadWithSelection; -const LATEST_VER = '2026-06-06-v4.5.12'; +const LATEST_VER = '2026-06-08-v4.5.22'; const CACHE = '?v=' + LATEST_VER; const stored = localStorage.getItem('webx11_cache_ver'); if (stored !== LATEST_VER) { @@ -164,7 +164,7 @@ const CACHE = '?v=' + LATEST_VER; // Phase 4: Ext modules (filtered) if (typeof webx11._loadExtModules === 'function') { - const extKeys = ['calc', 'voice', 'cron', 'theme', 'wpm', 'mindmap', 'webdav', 'skill-pack-core', 'editor', 'diff', 'alias', 'finder', 'filemgr', 'cryptogzip', 'tutorial', 'shell', 'busybox']; + const extKeys = ['calc', 'voice', 'cron', 'theme', 'wpm', 'mindmap', 'webdav', 'skill-pack-core', 'editor', 'diff', 'alias', 'finder', 'filemgr', 'cryptogzip', 'tutorial', 'shell', 'busybox', 'floating']; const extSelected = selected ? extKeys.filter(k => isSelected(k + '.js', selected)) : extKeys; diff --git a/modules/core/module-orchestrator.js b/modules/core/module-orchestrator.js index 05e1598..bcbd00d 100644 --- a/modules/core/module-orchestrator.js +++ b/modules/core/module-orchestrator.js @@ -23,10 +23,18 @@ class OrchestratorModule { this._restartBackoffs = {}; this._eventBus = {}; this._idleSweepInterval = null; + this._spawnLock = false; + this._CONSENSUS_SIZE = 3; + this._CONSENSUS_STRATEGY = 'majority'; + this._CONSENSUS_TIMEOUT = 60000; + this._consensusRounds = []; + this._activeConsensus = null; + this._pipelineHistory = []; + this._PIPELINE_CONFIG = { autoFix: false, stageTimeout: 30000 }; this._loadConfig(); } _loadConfig() { - try { const c = localStorage.getItem('webx11_orchestrator_config'); if (c) { const p = JSON.parse(c); if (p.spawnLimit) this._SPAWN_LIMIT = p.spawnLimit; if (p.heartbeatTimeout) this._HEARTBEAT_TIMEOUT = p.heartbeatTimeout; if (p.idleTimeout) this._IDLE_TIMEOUT = p.idleTimeout; if (p.restartPolicy) this._RESTART_POLICY = p.restartPolicy; if (p.a2aPollInterval) this._A2A_POLL_INTERVAL = p.a2aPollInterval; if (p.heartbeatCheck) this._HEARTBEAT_CHECK = p.heartbeatCheck; if (p.idleSweepInterval) this._IDLE_SWEEP_INTERVAL = p.idleSweepInterval; if (p.deadPurgeTimeout) this._DEAD_PURGE_TIMEOUT = p.deadPurgeTimeout; } } catch (e) {} + try { const c = localStorage.getItem('webx11_orchestrator_config'); if (c) { const p = JSON.parse(c); if (p.spawnLimit) this._SPAWN_LIMIT = p.spawnLimit; if (p.heartbeatTimeout) this._HEARTBEAT_TIMEOUT = p.heartbeatTimeout; if (p.idleTimeout) this._IDLE_TIMEOUT = p.idleTimeout; if (p.restartPolicy) this._RESTART_POLICY = p.restartPolicy; if (p.a2aPollInterval) this._A2A_POLL_INTERVAL = p.a2aPollInterval; if (p.heartbeatCheck) this._HEARTBEAT_CHECK = p.heartbeatCheck; if (p.idleSweepInterval) this._IDLE_SWEEP_INTERVAL = p.idleSweepInterval; if (p.deadPurgeTimeout) this._DEAD_PURGE_TIMEOUT = p.deadPurgeTimeout; if (p.consensusSize) this._CONSENSUS_SIZE = p.consensusSize; if (p.consensusStrategy) this._CONSENSUS_STRATEGY = p.consensusStrategy; if (p.consensusTimeout) this._CONSENSUS_TIMEOUT = p.consensusTimeout; if (p.pipelineAutoFix !== undefined) this._PIPELINE_CONFIG.autoFix = p.pipelineAutoFix; if (p.pipelineStageTimeout) this._PIPELINE_CONFIG.stageTimeout = p.pipelineStageTimeout; } } catch (e) {} } _saveConfig() { localStorage.setItem('webx11_orchestrator_config', JSON.stringify({ @@ -37,7 +45,12 @@ class OrchestratorModule { a2aPollInterval: this._A2A_POLL_INTERVAL, heartbeatCheck: this._HEARTBEAT_CHECK, idleSweepInterval: this._IDLE_SWEEP_INTERVAL, - deadPurgeTimeout: this._DEAD_PURGE_TIMEOUT + deadPurgeTimeout: this._DEAD_PURGE_TIMEOUT, + consensusSize: this._CONSENSUS_SIZE, + consensusStrategy: this._CONSENSUS_STRATEGY, + consensusTimeout: this._CONSENSUS_TIMEOUT, + pipelineAutoFix: this._PIPELINE_CONFIG.autoFix, + pipelineStageTimeout: this._PIPELINE_CONFIG.stageTimeout })); } registerCommands() { @@ -53,10 +66,75 @@ class OrchestratorModule { this.commands['orchestrator-restart-policy'] = (args) => this.setRestartPolicy(args); this.commands['a2a-send'] = (args) => this.sendA2AMessage(args); this.commands['a2a-list'] = () => this.listA2AMessages(); + this.commands['orchestrate-parallel'] = (args) => this.delegateTask(args.join(' '), { parallel: true }); + this.commands['orchestrate-decompose'] = (args) => this.showDecomposition(args.join(' ')); + this.commands['orchestrate-result'] = () => this.showParallelResult(); + this.commands['consensus'] = (args) => this.handleConsensus(args); + this.commands['consensus-status'] = () => this.consensusStatus(); + this.commands['consensus-history'] = () => this.consensusHistory(); + this.commands['pipeline'] = (args) => this.runPipeline(args); + } + // ─── Task Decomposition Engine ─── + _decomposeGoal(goal) { + if (!goal || typeof goal !== 'string') return [{ goal }]; + const lower = goal; + // Heuristic: only split at conjunctions when both sides have distinct action verbs + // Action verb patterns from _deriveCapabilities + const ACTION_VERBS = [ + 'search', 'find', 'research', 'lookup', 'query', + 'write', 'create', 'generate', 'compose', 'draft', 'save', + 'delete', 'remove', 'trash', 'rm', + 'read', 'view', 'cat', 'show', 'display', 'inspect', 'preview', + 'edit', 'modify', 'update', 'patch', 'change', 'fix', + 'install', 'package', 'setup', + 'list', 'status', 'health', 'audit', 'report', 'check', + 'fetch', 'scrape', 'download', + 'build', 'develop', 'compile', + 'analyze', 'analyse', 'summarize', 'review', 'investigate', + 'deploy', 'publish', 'upload', + 'monitor', 'watch', 'poll', 'track', 'observe', + 'test', 'verify', 'cmp', 'diff', 'compare' + ]; + const hasActionVerb = (s) => ACTION_VERBS.some(v => { + const re = new RegExp('\\b' + v + '\\b', 'i'); + return re.test(s); + }); + // Try splitting on " and " / " then " / " also " / "; " / ", then" + const separators = [ + /\s+and\s+/i, + /\s+then\s+/i, + /\s+also\s+/i, + /;\s+/, + /,\s+then\s+/i, + /\s+followed by\s+/i, + ]; + let bestSplit = null; + for (const sep of separators) { + const parts = goal.split(sep); + if (parts.length >= 2 && parts.every(p => hasActionVerb(p.trim()))) { + bestSplit = parts.map(p => p.trim()).filter(p => p.length > 3); + break; // first matching separator wins + } + } + if (!bestSplit || bestSplit.length < 2) { + return [{ goal }]; // atomic + } + return bestSplit.map(sub => ({ + goal: sub, + role: this._deriveRole(sub), + caps: this._deriveCapabilities(sub) + })); } getHelpInfo() { return { 'orchestrate': 'orchestrate <task> — Delegate a task to an available agent (auto-spawns if needed)', + 'orchestrate-parallel': 'orchestrate-parallel <task> — Decompose and delegate sub-tasks in parallel (auto-splits on "and"/"then")', + 'orchestrate-decompose': 'orchestrate-decompose <task> — Preview how a task would be decomposed into sub-tasks', + 'orchestrate-result': 'orchestrate-result — Show the last parallel batch results (stored in VFS)', + 'consensus': 'consensus <question> — Start a consensus round: spawn 3 agents, debate the question, resolve by majority', + 'consensus-status': 'consensus-status — Show current/active consensus round status', + 'consensus-history': 'consensus-history — Show past consensus rounds and their resolutions', + 'pipeline': 'pipeline <test|deploy|verify|history|config> — Autonomous CI/CD: test agents/queue/modules, deploy (test→auto-fix→verify), history (show runs), config (show config)', 'orchestrator-status': 'orchestrator-status — Show agent pool status and config', 'orchestrator-spawn': 'orchestrator-spawn [role] [capabilities...] — Manually spawn a new agent', 'orchestrator-kill': 'orchestrator-kill <agent-id> — Terminate an agent and revoke leases', @@ -234,35 +312,44 @@ class OrchestratorModule { return (name || 'worker').replace(/[^a-zA-Z0-9_-]/g, '-').replace(/-+/g, '-').replace(/^-|-$/g, '').substring(0, 32) || 'worker'; } spawnAgent(role, capabilities) { - const safeRole = this._sanitizeId(role); - const id = `agent-${Object.keys(this._agents).length + 1}-${safeRole}`; - if (Object.keys(this._agents).length >= this._SPAWN_LIMIT) { - this.terminal.print(`❌ Agent spawn limit (${this._SPAWN_LIMIT}) reached`, 'error'); + if (this._spawnLock) { + this.terminal.print('⚠️ Spawn in progress — queued', 'warning'); return null; } - const agent = { - id, - role: role || 'worker', - capabilities: capabilities.length ? capabilities : ['list_files', 'read_file', 'web_search'], - createdAt: new Date().toISOString(), - lastHeartbeat: Date.now(), - status: 'idle', - currentTask: null, - leaseId: null, - restartPolicy: this._RESTART_POLICY, - failureCount: 0, - restartCount: 0, - nextRestartAt: null - }; - this._agents[id] = agent; - // Create VFS agent directory with status - if (!this.terminal.fs.readFile(`/agents/${id}/`)) this.terminal.fs.mkdir(`agents/${id}`); - this.terminal.fs.writeFile(`agents/${id}/status.json`, JSON.stringify(agent)); - // Register in agent registry - const registry = this.terminal.moduleManager.modules['AgentRegistryModule']; - if (registry) registry.registerAgent(['register', id, ...agent.capabilities]); - this.terminal.print(`🤖 Agent '${id}' spawned (role: ${role || 'worker'}, ${agent.capabilities.length} caps)`, 'success'); - return agent; + this._spawnLock = true; + try { + const safeRole = this._sanitizeId(role); + const id = `agent-${Date.now().toString(36)}-${Math.random().toString(36).substr(2, 4)}-${safeRole}`; + if (Object.keys(this._agents).length >= this._SPAWN_LIMIT) { + this.terminal.print(`❌ Agent spawn limit (${this._SPAWN_LIMIT}) reached`, 'error'); + return null; + } + const agent = { + id, + role: role || 'worker', + capabilities: capabilities.length ? capabilities : ['list_files', 'read_file', 'web_search'], + createdAt: new Date().toISOString(), + lastHeartbeat: Date.now(), + status: 'idle', + currentTask: null, + leaseId: null, + restartPolicy: this._RESTART_POLICY, + failureCount: 0, + restartCount: 0, + nextRestartAt: null + }; + this._agents[id] = agent; + // Create VFS agent directory with status + if (!this.terminal.fs.readFile(`/agents/${id}/`)) this.terminal.fs.mkdir(`agents/${id}`); + this.terminal.fs.writeFile(`agents/${id}/status.json`, JSON.stringify(agent)); + // Register in agent registry + const registry = this.terminal.moduleManager.modules['AgentRegistryModule']; + if (registry) registry.registerAgent(['register', id, ...agent.capabilities]); + this.terminal.print(`🤖 Agent '${id}' spawned (role: ${role || 'worker'}, ${agent.capabilities.length} caps)`, 'success'); + return agent; + } finally { + this._spawnLock = false; + } } killAgent(id) { @@ -460,7 +547,12 @@ class OrchestratorModule { 'a2a-poll-interval': () => { const n = parseInt(val); if (isNaN(n) || n < 50 || n > 30000) return 'Must be 50-30000ms'; clearInterval(this._a2aPollTimer); this._a2aPollTimer = null; this._A2A_POLL_INTERVAL = n; if (this._A2A_POLLING) { this._a2aPollTimer = setInterval(() => this._pollAgentMailboxes(), n); } return `a2a-poll-interval set to ${n}ms`; }, 'heartbeat-check': () => { const n = parseInt(val); if (isNaN(n) || n < 500 || n > 60000) return 'Must be 500-60000ms'; clearInterval(this._heartbeatInterval); this._HEARTBEAT_CHECK = n; this._heartbeatInterval = setInterval(() => this._checkHeartbeats(), n); return `heartbeat-check set to ${n}ms`; }, 'idle-sweep-interval': () => { const n = parseInt(val); if (isNaN(n) || n < 5000 || n > 600000) return 'Must be 5000-600000ms'; clearInterval(this._idleSweepInterval); this._IDLE_SWEEP_INTERVAL = n; this._idleSweepInterval = setInterval(() => this._sweepIdle(), n); return `idle-sweep-interval set to ${n}ms`; }, - 'dead-purge-timeout': () => { const n = parseInt(val); if (isNaN(n) || n < 5000 || n > 600000) return 'Must be 5000-600000ms'; this._DEAD_PURGE_TIMEOUT = n; return `dead-purge-timeout set to ${n}ms`; } + 'dead-purge-timeout': () => { const n = parseInt(val); if (isNaN(n) || n < 5000 || n > 600000) return 'Must be 5000-600000ms'; this._DEAD_PURGE_TIMEOUT = n; return `dead-purge-timeout set to ${n}ms`; }, + 'consensus-size': () => { const n = parseInt(val); if (isNaN(n) || n < 2 || n > 10) return 'Must be 2-10'; this._CONSENSUS_SIZE = n; return `consensus-size set to ${n}`; }, + 'consensus-strategy': () => { if (!['majority', 'unanimous', 'weighted'].includes(val)) return 'Must be majority, unanimous, or weighted'; this._CONSENSUS_STRATEGY = val; return `consensus-strategy set to ${val}`; }, + 'consensus-timeout': () => { const n = parseInt(val); if (isNaN(n) || n < 5000 || n > 300000) return 'Must be 5000-300000ms'; this._CONSENSUS_TIMEOUT = n; return `consensus-timeout set to ${n}ms`; }, + 'pipeline-auto-fix': () => { this._PIPELINE_CONFIG.autoFix = val === 'on' || val === 'true'; return `auto-fix ${this._PIPELINE_CONFIG.autoFix ? 'on' : 'off'}`; }, + 'pipeline-stage-timeout': () => { const n = parseInt(val); if (isNaN(n) || n < 5000 || n > 120000) return 'Must be 5000-120000ms'; this._PIPELINE_CONFIG.stageTimeout = n; return `stage-timeout set to ${n}ms`; } }; if (!configMap[key]) { @@ -471,25 +563,514 @@ class OrchestratorModule { this._saveConfig(); } + // ─── Show Decomposition Preview ─── + + showDecomposition(goal) { + if (!goal) return this.terminal.print('Usage: orchestrate-decompose <goal>', 'error'); + const parts = this._decomposeGoal(goal); + this.terminal.print('🔍 Decomposition of "' + goal.substring(0, 60) + '":', 'header'); + if (parts.length === 1 && parts[0].goal === goal) { + this.terminal.print(' → Atomic (no split detected)', 'info'); + return; + } + for (let i = 0; i < parts.length; i++) { + const p = parts[i]; + this.terminal.print(' [' + (i + 1) + '] ' + p.goal.substring(0, 50), 'success'); + this.terminal.print(' Role: ' + p.role + ' | Caps: ' + (p.caps || []).join(', '), 'text-muted'); + } + this.terminal.print('→ ' + parts.length + ' sub-task(s) — run with: orchestrate-parallel "' + goal + '"', 'info'); + } + + // ─── Show Last Parallel Result ─── + + showParallelResult() { + const data = this.terminal.fs.fsReadJSON('orchestrator/parallel-results.json'); + if (!data) return this.terminal.print('No parallel results saved yet', 'info'); + this.terminal.print('📊 Last Parallel Batch Results', 'header'); + this.terminal.print(' Task: ' + (data.parent_task || '').substring(0, 60), 'info'); + this.terminal.print(' ' + data.successes + '/' + data.total + ' succeeded' + (data.failures > 0 ? ', ' + data.failures + ' failed' : ''), 'info'); + this.terminal.print(' Timestamp: ' + data.timestamp, 'text-muted'); + for (const s of (data.summaries || [])) { + const icon = s.status === 'ok' ? '✅' : '❌'; + const snippet = s.result ? s.result.replace(/\n/g, ' ').substring(0, 80) : (s.error || ''); + this.terminal.print(' ' + icon + ' ' + s.task.substring(0, 40) + ' → ' + snippet, 'info'); + } + } + + // ─── Consensus Protocol ─── + + async handleConsensus(args) { + if (!args || args.length === 0) return this.terminal.print('Usage: consensus <question>', 'error'); + const question = args.join(' '); + if (this._activeConsensus) return this.terminal.print('❌ A consensus round is already in progress — wait for it to resolve', 'error'); + + const roundId = `consensus-${Date.now().toString(36)}-${Math.random().toString(36).substr(2, 4)}`; + const size = this._CONSENSUS_SIZE; + this.terminal.print(`🤝 Starting consensus round ${roundId} (${size} agents, strategy: ${this._CONSENSUS_STRATEGY})`, 'header'); + this.terminal.print(` Question: ${question.substring(0, 80)}`, 'info'); + + this._activeConsensus = { + id: roundId, + question, + strategy: this._CONSENSUS_STRATEGY, + size, + votes: [], + startedAt: new Date().toISOString(), + resolved: false, + result: null + }; + + // Spawn N agents with same question + const tasks = []; + for (let i = 0; i < size; i++) { + tasks.push(`analyze and answer: ${question}. Provide your reasoning then your final answer as "VOTE: <answer>"`); + } + + const timeout = setTimeout(() => { + if (this._activeConsensus && !this._activeConsensus.resolved) { + this._resolveConsensus('timeout'); + } + }, this._CONSENSUS_TIMEOUT); + + try { + const results = await Promise.allSettled(tasks.map(t => this.delegateTask(t))); + + for (let i = 0; i < results.length; i++) { + const r = results[i]; + const voteText = r.status === 'fulfilled' ? (r.value || '') : 'FAILED'; + // Extract VOTE: marker or use full text + let answer = 'no vote'; + const voteMatch = voteText.match(/VOTE:\s*(.+?)(?:\n|$)/i); + if (voteMatch) answer = voteMatch[1].trim(); + else if (voteText.length > 0 && voteText !== 'FAILED') answer = voteText.substring(0, 60).trim(); + + this._activeConsensus.votes.push({ + agentIdx: i, + answer, + raw: voteText.substring(0, 200), + status: r.status === 'fulfilled' ? 'voted' : 'failed' + }); + } + } catch (e) { + this.terminal.print(`⚠️ Consensus error: ${e.message}`, 'warning'); + } finally { + clearTimeout(timeout); + } + + this._resolveConsensus('complete'); + return this._activeConsensus.result; + } + + _resolveConsensus(reason) { + const round = this._activeConsensus; + if (!round || round.resolved) return; + round.resolved = true; + round.resolvedAt = new Date().toISOString(); + round.resolvedBy = reason; + + // Count votes + const voteCounts = {}; + for (const v of round.votes) { + if (v.status === 'failed') continue; + voteCounts[v.answer] = (voteCounts[v.answer] || 0) + 1; + } + + let winner = null; + let maxVotes = 0; + let totalVoted = 0; + for (const [ans, count] of Object.entries(voteCounts)) { + totalVoted += count; + if (count > maxVotes) { + maxVotes = count; + winner = ans; + } + } + + if (this._CONSENSUS_STRATEGY === 'majority') { + const needed = Math.ceil(round.size / 2); + if (maxVotes >= needed) { + round.result = { consensus: 'reached', answer: winner, votes: maxVotes, total: round.size }; + } else { + round.result = { consensus: 'hung', votes: voteCounts, total: round.size }; + } + } else if (this._CONSENSUS_STRATEGY === 'unanimous') { + if (totalVoted === round.size && Object.keys(voteCounts).length === 1) { + round.result = { consensus: 'reached', answer: winner, votes: maxVotes, total: round.size }; + } else { + round.result = { consensus: 'hung', votes: voteCounts, total: round.size }; + } + } else { + // weighted — fallback to majority + const needed = Math.ceil(round.size / 2); + round.result = maxVotes >= needed + ? { consensus: 'reached', answer: winner, votes: maxVotes, total: round.size } + : { consensus: 'hung', votes: voteCounts, total: round.size }; + } + + // Store the round + this._consensusRounds.push(round); + if (!this.terminal.fs.readDir('orchestrator')) this.terminal.fs.mkdir('orchestrator'); + this.terminal.fs.writeFile(`orchestrator/consensus-round-${round.id}.json`, JSON.stringify(round, null, 2)); + + // Print result + const r = round.result; + if (r.consensus === 'reached') { + this.terminal.print(`✅ Consensus reached: "${r.answer}" (${r.votes}/${r.total} votes, ${round.strategy})`, 'success'); + } else { + this.terminal.print(`⚖️ Consensus hung — no clear winner. Votes: ${JSON.stringify(r.votes)}`, 'warning'); + } + + this._activeConsensus = null; + } + + consensusStatus() { + if (this._activeConsensus) { + const r = this._activeConsensus; + this.terminal.print('🤝 Active Consensus Round', 'header'); + this.terminal.print(` ID: ${r.id}`, 'info'); + this.terminal.print(` Question: ${r.question.substring(0, 60)}`, 'info'); + this.terminal.print(` Strategy: ${r.strategy} | Size: ${r.size}`, 'info'); + this.terminal.print(` Votes collected: ${r.votes.length}/${r.size}`, 'info'); + for (const v of r.votes) { + this.terminal.print(` ${v.status === 'voted' ? '✅' : '❌'} Agent ${v.agentIdx}: ${v.answer}`, 'info'); + } + } else { + this.terminal.print('No active consensus round', 'info'); + } + } + + consensusHistory() { + const rounds = this._consensusRounds; + if (rounds.length === 0) { + this.terminal.print('No consensus rounds in memory — checking VFS...', 'info'); + // Try VFS + const dir = this.terminal.fs.readDir('orchestrator/'); + if (dir) { + const files = dir.filter(e => e.name && e.name.startsWith('consensus-round-')); + if (files.length > 0) { + this.terminal.print(`📜 ${files.length} round(s) in VFS`, 'header'); + for (const f of files.slice(-5)) { + const data = this.terminal.fs.fsReadJSON(`orchestrator/${f.name}`); + if (data) { + const r = data.result; + const status = r?.consensus === 'reached' ? '✅' : '⚖️'; + this.terminal.print(` ${status} ${data.id}: "${data.question.substring(0, 40)}" → ${r?.answer || 'no result'}`, 'info'); + } + } + return; + } + } + this.terminal.print('No past consensus rounds found', 'info'); + return; + } + this.terminal.print('📜 Consensus History', 'header'); + for (const round of rounds.slice(-10)) { + const r = round.result; + const status = r?.consensus === 'reached' ? '✅' : '⚖️'; + this.terminal.print(` ${status} ${round.id}: "${round.question.substring(0, 40)}"`, 'info'); + this.terminal.print(` Strategy: ${round.strategy} | Votes: ${round.votes.length}/${round.size} | Result: ${r?.answer || 'hung'}`, 'text-muted'); + } + } + + // ─── Autonomous CI/CD Pipeline ─── + + async runPipeline(args) { + if (!args || args.length === 0) return this.terminal.print('Usage: pipeline <command> [options]', 'error'); + const cmd = args[0].toLowerCase(); + const opts = args.slice(1); + + const run = { + id: `pipeline-${Date.now().toString(36)}-${Math.random().toString(36).substr(2, 4)}`, + command: cmd, + startedAt: new Date().toISOString(), + stages: [], + status: 'running' + }; + + try { + if (cmd === 'test') { + await this._pipelineStage(run, 'agent-health', () => this._testAgentHealth()); + await this._pipelineStage(run, 'a2a-mailbox', () => this._testA2AMailbox()); + await this._pipelineStage(run, 'module-check', () => this._testModules()); + run.status = 'passed'; + this.terminal.print('✅ All pipeline tests passed', 'success'); + } else if (cmd === 'deploy') { + const deployTask = opts.join(' ') || 'run all checks'; + await this._pipelineStage(run, 'test', () => this.runPipeline(['test'])); + if (run.stages.find(s => s.name === 'test' && s.status === 'failed')) { + if (this._PIPELINE_CONFIG.autoFix) { + await this._pipelineStage(run, 'auto-fix', () => this._autoFix()); + } else { + run.status = 'blocked'; + this.terminal.print('❌ Pipeline blocked — tests failed. Set pipeline-auto-fix on to auto-repair', 'error'); + return; + } + } + await this._pipelineStage(run, 'verify', () => this._pipelineVerify()); + run.status = 'passed'; + this.terminal.print('✅ Pipeline deploy passed all stages', 'success'); + } else if (cmd === 'verify') { + await this._pipelineStage(run, 'verify', () => this._pipelineVerify()); + run.status = 'passed'; + } else if (cmd === 'history') { + this._pipelineHistory.slice(-10).forEach(p => { + const icon = p.status === 'passed' ? '✅' : (p.status === 'blocked' ? '🚫' : '❌'); + this.terminal.print(` ${icon} ${p.id}: ${p.command} (${p.startedAt.slice(11, 19)})`, 'info'); + p.stages.forEach(s => { + this.terminal.print(` ${s.status === 'passed' ? '✅' : '❌'} ${s.name} (${(s.duration || 0).toFixed(0)}ms)`, 'text-muted'); + }); + }); + if (this._pipelineHistory.length === 0) this.terminal.print('No pipeline runs yet', 'info'); + return; + } else if (cmd === 'config') { + this.terminal.print('Pipeline Config:', 'header'); + this.terminal.print(` auto-fix: ${this._PIPELINE_CONFIG.autoFix ? 'on' : 'off'}`, 'info'); + this.terminal.print(` stage-timeout: ${this._PIPELINE_CONFIG.stageTimeout}ms`, 'info'); + return; + } else { + return this.terminal.print('Unknown pipeline command. Usage: test, deploy, verify, history, config', 'error'); + } + } catch (e) { + run.status = 'failed'; + run.error = e.message; + this.terminal.print(`❌ Pipeline failed: ${e.message}`, 'error'); + } + + run.completedAt = new Date().toISOString(); + this._pipelineHistory.push(run); + if (!this.terminal.fs.readDir('orchestrator')) this.terminal.fs.mkdir('orchestrator'); + this.terminal.fs.writeFile(`orchestrator/pipeline-${run.id}.json`, JSON.stringify(run, null, 2)); + return run; + } + + async _pipelineStage(run, name, fn) { + const stage = { name, status: 'running', startedAt: Date.now() }; + run.stages.push(stage); + this.terminal.print(`⏳ Pipeline stage: ${name}...`, 'info'); + + try { + const timeout = this._PIPELINE_CONFIG.stageTimeout; + const result = await Promise.race([ + fn(), + new Promise((_, reject) => setTimeout(() => reject(new Error(`${name} timed out (${timeout}ms)`)), timeout)) + ]); + stage.status = 'passed'; + stage.duration = Date.now() - stage.startedAt; + stage.result = typeof result === 'string' ? result.substring(0, 200) : 'ok'; + this.terminal.print(`✅ Stage ${name} passed (${stage.duration}ms)`, 'success'); + return result; + } catch (e) { + stage.status = 'failed'; + stage.duration = Date.now() - stage.startedAt; + stage.error = e.message; + this.terminal.print(`❌ Stage ${name} failed: ${e.message}`, 'error'); + throw e; + } + } + + _testAgentHealth() { + const total = Object.keys(this._agents).length; + const busy = Object.values(this._agents).filter(a => a.status === 'busy').length; + const failed = Object.values(this._agents).filter(a => a.status === 'failed').length; + + const issues = []; + if (failed > 0) issues.push(`${failed} agent(s) in failed state`); + if (total > 10) issues.push(`high agent count: ${total}`); + if (issues.length > 0) throw new Error(issues.join('; ')); + this.terminal.print(` Agents: ${total} (${busy} busy, ${failed} failed) — healthy`, 'info'); + return `agents_ok:${total}:${busy}:${failed}`; + } + + _testA2AMailbox() { + // Verify A2A polling is running + if (!this._A2A_POLLING) throw new Error('A2A polling is disabled'); + if (!this._a2aPollTimer) throw new Error('A2A polling timer not running'); + // Verify each agent has a VFS directory + for (const [id, agent] of Object.entries(this._agents)) { + const dir = this.terminal.fs.readDir(`/agents/${id}/`); + if (!dir) throw new Error(`Agent ${id} missing VFS directory`); + } + this.terminal.print(` A2A: ${Object.keys(this._agents).length} agent mailboxes OK`, 'info'); + return `a2a_ok:${Object.keys(this._agents).length}`; + } + + _testModules() { + const mm = this.terminal.moduleManager; + if (!mm || !mm.modules) throw new Error('ModuleManager not available'); + const required = ['OrchestratorModule', 'AgentRegistryModule', 'LeaseModule', 'MemoryGraphModule']; + const missing = required.filter(n => !mm.modules[n]); + if (missing.length > 0) throw new Error(`Missing modules: ${missing.join(', ')}`); + // Check orchestrator is in registry + const registry = mm.modules['AgentRegistryModule']; + if (registry && !registry.agents?.find(a => a.id === 'orchestrator-0')) { + this.terminal.print(' ⚠️ orchestrator-0 not in registry (will register on next init)', 'warning'); + } + this.terminal.print(` Modules: ${required.length}/${required.length} required loaded`, 'info'); + return `modules_ok:${required.length}`; + } + + _pipelineVerify() { + // Verify running state — internal checks + const issues = []; + + // Check orchestrator status + const total = Object.keys(this._agents).length; + const failed = Object.values(this._agents).filter(a => a.status === 'failed').length; + if (failed > 0) issues.push(`${failed} failed agents`); + if (total + failed > 0 && failed === total) issues.push('all agents failed'); + + // Verify last parallel result exists + const lastResult = this.terminal.fs.fsReadJSON('orchestrator/parallel-results.json'); + if (!lastResult) this.terminal.print(' ℹ️ No parallel results yet (first deploy)', 'info'); + + // Verify consensus history + const consensusDir = this.terminal.fs.readDir('orchestrator/'); + if (consensusDir) { + const rounds = consensusDir.filter(e => e.name && e.name.startsWith('consensus-round-')); + this.terminal.print(` VFS: ${rounds.length} consensus rounds archived`, 'info'); + } + + // Check memory graph + const memgraph = this.terminal.moduleManager?.modules['MemoryGraphModule']; + if (memgraph && typeof memgraph.getStats === 'function') { + const stats = memgraph.getStats(); + this.terminal.print(` Memory graph: ${stats?.edges || 0} edges, ${stats?.nodes || 0} nodes`, 'info'); + } + + if (issues.length > 0) throw new Error(issues.join('; ')); + return `verify_ok:${total} agents, ${issues.length} issues`; + } + + async _autoFix() { + this.terminal.print('🔄 Auto-fix running...', 'warning'); + let fixed = 0; + + // Fix 1: Purge failed agents + const failedIds = Object.entries(this._agents) + .filter(([, a]) => a.status === 'failed') + .map(([id]) => id); + for (const id of failedIds) { + this.killAgent(id); + fixed++; + } + if (fixed > 0) this.terminal.print(` Purged ${fixed} failed agent(s)`, 'warning'); + + // Fix 2: Restart A2A poller if stopped + if (!this._A2A_POLLING) { + this._A2A_POLLING = true; + this._a2aPollTimer = setInterval(() => this._pollAgentMailboxes(), this._A2A_POLL_INTERVAL); + this.terminal.print(' Restarted A2A polling', 'warning'); + fixed++; + } + + // Fix 3: Ensure VFS orchestrator dir exists + if (!this.terminal.fs.readDir('orchestrator')) { + this.terminal.fs.mkdir('orchestrator'); + fixed++; + } + + if (fixed === 0) this.terminal.print(' No auto-fix actions needed', 'info'); + return `fixed ${fixed} issues`; + } + // ─── Task Delegation ─── - async delegateTask(task) { + async delegateTask(task, opts = {}) { if (!task) return this.terminal.print('Usage: orchestrate <task description>', 'error'); - // Find an idle agent + // Parallel fan-out: decompose and run sub-tasks concurrently + if (opts.parallel) { + const parts = this._decomposeGoal(task); + if (parts.length > 1) { + this.terminal.print('🧩 Decomposed into ' + parts.length + ' sub-tasks — running in parallel', 'info'); + + // Auto-scale: ensure enough idle agents for N sub-tasks + const idleCount = Object.values(this._agents).filter(a => a.status === 'idle').length; + const totalSlots = this._SPAWN_LIMIT; + const usedSlots = Object.keys(this._agents).length; + const availableSlots = totalSlots - usedSlots; + const needed = parts.length - idleCount; + if (needed > 0 && availableSlots > 0) { + const toSpawn = Math.min(needed, availableSlots); + this.terminal.print('📈 Auto-scaling: spawning ' + toSpawn + ' pre-warm agent(s)', 'info'); + for (let i = 0; i < toSpawn; i++) { + this.spawnAgent('worker', ['list_files', 'read_file', 'write_file', 'web_search']); + } + } + + const results = await Promise.allSettled(parts.map(sub => this.delegateTask(sub.goal))); + const successes = results.filter(r => r.status === 'fulfilled').length; + const failures = results.filter(r => r.status === 'rejected').length; + + // Result synthesis: collect outbox results from each sub-task's agent + const summaries = []; + for (let i = 0; i < parts.length; i++) { + const sub = parts[i]; + const r = results[i]; + if (r.status === 'fulfilled') { + // Find the agent that handled this sub-task + const agent = Object.values(this._agents).find(a => a.currentTask === sub.goal || (a.lastTask === sub.goal)); + if (agent) { + const outbox = this.terminal.fs.fsReadJSON(`/agents/${agent.id}/outbox.json`); + const snippet = outbox?.result ? outbox.result.substring(0, 200) : 'completed (no outbox)'; + summaries.push({ task: sub.goal, agent: agent.id, status: 'ok', result: snippet }); + } else { + summaries.push({ task: sub.goal, status: 'ok', result: 'completed (agent recycled)' }); + } + } else { + summaries.push({ task: sub.goal, status: 'failed', error: r.reason?.message || 'unknown error' }); + } + } + + // Store merged result to VFS for persistence + const merged = { + type: 'parallel_result', + parent_task: task, + timestamp: new Date().toISOString(), + total: parts.length, + successes, + failures, + summaries + }; + if (!this.terminal.fs.readDir('orchestrator')) this.terminal.fs.mkdir('orchestrator'); + this.terminal.fs.writeFile('orchestrator/parallel-results.json', JSON.stringify(merged, null, 2)); + + // Print synthesis to terminal + this.terminal.print('📊 Parallel Batch Results:', 'header'); + for (const s of summaries) { + const icon = s.status === 'ok' ? '✅' : '❌'; + const agentTag = s.agent ? ' [' + s.agent.substring(0, 20) + ']' : ''; + const snippet = s.result ? s.result.replace(/\n/g, ' ').substring(0, 60) : ''; + this.terminal.print(' ' + icon + ' ' + s.task.substring(0, 35) + agentTag + ' → ' + snippet, 'info'); + } + this.terminal.print('✅ Parallel batch: ' + successes + '/' + parts.length + ' succeeded' + (failures > 0 ? ', ' + failures + ' failed' : ''), 'header'); + return merged; + } + // Single sub-task — fall through to normal path + } + + // Find an idle agent and immediately stamp it busy (atomic claim) let agent = Object.values(this._agents).find(a => a.status === 'idle'); + if (agent) { + agent.status = 'busy'; + } // Auto-spawn with role-derived capabilities if (!agent) { if (Object.keys(this._agents).length >= this._SPAWN_LIMIT) { // Try finding any non-failed agent to reuse - agent = Object.values(this._agents).find(a => a.status !== 'failed' && a.status !== 'sleeping'); + const reuse = Object.values(this._agents).find(a => a.status !== 'failed' && a.status !== 'sleeping'); + if (reuse) { + reuse.status = 'busy'; + agent = reuse; + } } if (!agent) { const role = this._deriveRole(task); const caps = this._deriveCapabilities(task); - this.spawnAgent(role, caps); - agent = Object.values(this._agents).find(a => a.status === 'idle'); + agent = this.spawnAgent(role, caps); + if (!agent) return this.terminal.print('❌ No available agents — spawn limit reached', 'error'); + agent.status = 'busy'; // claim immediately after spawn } } if (!agent) return this.terminal.print('❌ No available agents', 'error'); @@ -548,8 +1129,12 @@ class OrchestratorModule { this.terminal.print(`✅ ${agent.id} completed task`, 'success'); + // Return result for callers that await delegateTask + const outboxResult = typeof result === 'string' ? result : (result?.content || 'completed'); + // Check restart policy — if always, set back to idle for next task if (agent.restartPolicy === 'always') { + agent.lastTask = agent.currentTask; agent.status = 'idle'; agent.currentTask = null; agent.lastHeartbeat = Date.now(); @@ -564,11 +1149,14 @@ class OrchestratorModule { // Return to idle pool (unless failed/restarting) if (agent.status === 'failed') { // handled by _handleAgentFailure + throw new Error(agent.lastError || 'agent failed'); } else if (agent.status !== 'busy') { - agent.status = 'idle'; + agent.lastTask = agent.currentTask; agent.currentTask = null; + agent.status = 'idle'; agent.lastHeartbeat = Date.now(); } + return outboxResult || 'completed'; } // ─── Monitoring ─── diff --git a/modules/core/module-system.js b/modules/core/module-system.js index c2115c1..77525c7 100644 --- a/modules/core/module-system.js +++ b/modules/core/module-system.js @@ -284,7 +284,7 @@ search: ['search','s','ddg','duck','fetch','web','extract'], tools: ['calc','cron-every','cron-list','cron-stop','cron-clear','wpm','mindmap','theme','voice','alias','unalias', 'which','env','date','cal','yes','seq','base64','printf','cut','uniq','hostname','whoami','basename','dirname','sleep','expand','tutorial','gzip','gunzip','encrypt','decrypt','files','session-save','session-load','session-list','session-delete','session-export','session-import'], - system: ['help','clear','cls','echo','mem','status','font-size','setfont','smart-prompt','logs','preview','factory-reset','reset-vfs','export','unset','script','set','test','logrotate','health-check','memory-map-export','commands','check','checkpoint','checkpoint-list','rollback','checkpoint-del'], + system: ['help','clear','cls','echo','mem','status','font-size','setfont','smart-prompt','logs','preview','factory-reset','reset-vfs','export','unset','script','sh','set','test','logrotate','health-check','memory-map-export','commands','check','checkpoint','checkpoint-list','rollback','checkpoint-del'], net: ['server','servers','server-check','server-remove','monitor'], mods: ['modules','listmods','loadmod','removemod','load-local','loadlocal','startup-modules'], ollama: ['ollama','ollamaconnect','ollamadisconnect','ollamamodels','ollamasetmodel','ollamasetproxy','models','model-info','model-pull','model-delete','model-run'], diff --git a/modules/core/terminal.js b/modules/core/terminal.js index 2825340..1fb44aa 100644 --- a/modules/core/terminal.js +++ b/modules/core/terminal.js @@ -1160,7 +1160,7 @@ } catch (e) {} } async _loadExtModules(filterKeys) { - const autoLoadKeys = filterKeys || ['calc', 'voice', 'cron', 'theme', 'wpm', 'mindmap', 'webdav', 'skill-pack-core', 'editor', 'diff', 'alias', 'finder', 'filemgr', 'cryptogzip', 'shell', 'tutorial', 'busybox']; + const autoLoadKeys = filterKeys || ['calc', 'voice', 'cron', 'theme', 'wpm', 'mindmap', 'webdav', 'skill-pack-core', 'editor', 'diff', 'alias', 'finder', 'filemgr', 'cryptogzip', 'shell', 'tutorial', 'busybox', 'floating']; const cacheVer = localStorage.getItem('webx11_cache_ver') || '2026-05-27-14'; try { const res = await fetch('packages.json?v=' + cacheVer); @@ -1179,7 +1179,7 @@ } async reloadPersistedModules() { const registry = this.moduleManager.moduleRegistry; - const skipList = ['calc', 'voice', 'cron', 'theme', 'wpm', 'mindmap', 'webdav', 'skill-pack-core', 'editor', 'diff', 'alias', 'finder', 'filemgr', 'cryptogzip', 'shell', 'tutorial', 'busybox']; + const skipList = ['calc', 'voice', 'cron', 'theme', 'wpm', 'mindmap', 'webdav', 'skill-pack-core', 'editor', 'diff', 'alias', 'finder', 'filemgr', 'cryptogzip', 'shell', 'tutorial', 'busybox', 'floating']; for (const mod of registry) { if (mod.enabled && mod.url) { // Skip ext modules — they load via _loadExtModules @@ -2055,7 +2055,7 @@ 'module-fetcher.js','module-skills.js','module-chatstudio.js' ]; const extFiles = [ - 'calc.js','voice.js','cron.js','theme.js','wpm.js','mindmap.js','webdav.js','skill-pack-core.js','editor.js','diff.js','alias.js','finder.js','filemgr.js','cryptogzip.js','tutorial.js','shell.js','busybox.js' + 'calc.js','voice.js','cron.js','theme.js','wpm.js','mindmap.js','webdav.js','skill-pack-core.js','editor.js','diff.js','alias.js','finder.js','filemgr.js','cryptogzip.js','tutorial.js','shell.js','busybox.js','floating.js' ]; const addSection = (title) => { diff --git a/modules/core/virtual-fs.js b/modules/core/virtual-fs.js index 95b031e..cb8020a 100644 --- a/modules/core/virtual-fs.js +++ b/modules/core/virtual-fs.js @@ -3,7 +3,6 @@ class VirtualFileSystem { this.root = { type: 'dir', content: {} }; this.currentPath = []; this._ready = this.loadFromStorage(); - this._conceptCache = null; } _u8ToBase64(u8) { let s = ''; @@ -66,6 +65,17 @@ class VirtualFileSystem { } return false; } + _emitChange(path) { + // Notify NeuralVFS via window event — zero coupling + // Skip internal index/system files to prevent re-trigger loops + if (!path) return; + const normalized = path.startsWith('/') ? path : '/' + path; + const sysPaths = ['/.semantic_index', '/.memory_graph', '/.trash', '/_snapshots', '/_sessions', '/logs', '/memory', '/system', '/_mods', '/backups']; + if (sysPaths.some(p => normalized.startsWith(p))) return; + if (typeof window !== 'undefined' && window.__nvfsMarkDirty) { + window.__nvfsMarkDirty(path); + } + } saveToStorage() { if (this._storageSavePending) return; this._storageSavePending = true; @@ -83,38 +93,6 @@ class VirtualFileSystem { } catch (e) { console.error('FS save error:', e); } }, 2000); } - _emitChange(path) { - // Notify NeuralVFS via window event — zero coupling - // Skip internal index/system files to prevent re-trigger loops - if (!path) return; - const normalized = path.startsWith('/') ? path : '/' + path; - const sysPaths = ['/.semantic_index', '/.memory_graph', '/.trash', '/_snapshots', '/_sessions', '/logs', '/memory', '/system', '/_mods', '/backups']; - if (sysPaths.some(p => normalized.startsWith(p))) return; - if (typeof window !== 'undefined' && window.__nvfsMarkDirty) { - window.__nvfsMarkDirty(path); - } - } - getCurrentPath() { - return '/' + this.currentPath.join('/'); - } - setCurrentPath(path) { - const segs = this._toAbsoluteSegments(path); - if (segs === null) return false; - this.currentPath = segs; - return true; - } - _loadConceptIndex() { - if (this._conceptCache) return this._conceptCache; - try { - const raw = this.readFile('/.semantic_index/map.json'); - if (raw) { - this._conceptCache = JSON.parse(raw); - return this._conceptCache; - } - } catch (e) {} - return null; - } - _clearConceptCache() { this._conceptCache = null; } getCurrentDir() { let dir = this.root; for (let part of this.currentPath) { @@ -163,18 +141,6 @@ class VirtualFileSystem { return dir; } ls(path) { - // concept:// listing - if (path && path.startsWith('concept://')) { - const concept = path.replace('concept://', '').replace(/\/.*$/, '').toLowerCase(); - const index = this._loadConceptIndex(); - if (!index) return []; - let matches = index[concept] || []; - if (matches.length === 0) { - const fuzzy = Object.keys(index).filter(k => k.includes(concept) || concept.includes(k)); - if (fuzzy.length > 0) matches = index[fuzzy[0]]; - } - return (matches || []).map(m => ({ name: m.path.split('/').pop(), type: 'file', path: m.path, size: 0, concept: true })); - } let dir; if (path) { const resolved = this._resolvePath(path); @@ -207,24 +173,6 @@ class VirtualFileSystem { return true; } readFile(path) { - // concept:// URI resolution - if (path && path.startsWith('concept://')) { - const concept = path.replace('concept://', '').replace(/\/.*$/, '').toLowerCase(); - try { - const index = this._loadConceptIndex(); - if (!index) return 'No semantic index found. Run nvfs-index first.'; - let matches = index[concept]; - if (!matches) { - const fuzzy = Object.keys(index).filter(k => k.includes(concept) || concept.includes(k)); - if (fuzzy.length > 0) matches = index[fuzzy[0]]; - } - if (!matches || matches.length === 0) return `No files found for concept "${concept}"`; - const first = matches[0]; - const content = this.readFile(first.path); - if (content) return `--- ${first.path} (concept://${concept}) ---\n${content}`; - return `Concept "${concept}" → ${matches.length} file(s):\n${matches.map(m => ` ${m.path} — ${m.summary || ''}`).join('\n')}`; - } catch (e) { return `concept:// error: ${e.message}`; } - } const resolved = this._resolvePath(path); if (!resolved || !resolved.name) return null; const node = resolved.dir.content[resolved.name]; @@ -291,6 +239,9 @@ class VirtualFileSystem { } return false; } + deleteDirRecursive(path) { + return this.trashDir(path); + } mkdirRecursive(path) { const segs = this._toAbsoluteSegments(path); if (!segs) return false; @@ -539,25 +490,15 @@ class VirtualFileSystem { checkpoint(name) { if (!name) name = new Date().toISOString(); const createdAt = new Date().toISOString(); - // Strip system dirs to avoid fractal bloat — snapshots/sessions/trash - // don't belong inside a checkpoint of user data - const root = JSON.parse(JSON.stringify(this.root)); - if (root && root.content) { - delete root.content['_snapshots']; - delete root.content['_sessions']; - delete root.content['.trash']; - } const data = { version: 1, createdAt, - root, + root: JSON.parse(JSON.stringify(this.root)), currentPath: [...this.currentPath] }; if (!this.readDir('/_snapshots')) { this.mkdirRecursive('/_snapshots'); } - // Strip _snapshots from the serialized data too (redundant safety) - data.root = root; this.fsWriteJSON('/_snapshots/checkpoint-' + name + '.json', data); this.saveToStorage(); // Auto-manage: keep max 10 checkpoints, delete oldest @@ -597,26 +538,6 @@ class VirtualFileSystem { } return false; } - // Reset VFS — wipes everything, removes localStorage keys - reset() { - this.root = { type: 'dir', content: {} }; - this.currentPath = []; - this._conceptCache = null; - try { - localStorage.removeItem('webx11_filesystem_v2'); - localStorage.removeItem('webx11_filesystem'); - } catch (e) {} - return true; - } - // Empty trash — permanently delete all trashed files - emptyTrash() { - if (this.root.content['.trash']) { - delete this.root.content['.trash']; - this.saveToStorage(); - return true; - } - return false; - } // Old-compat aliases for agent tools and SkillsModule read(path) { return this.readFile(path); } write(path, content) { return this.writeFile(path, content); } diff --git a/modules/ext/alias.js b/modules/ext/alias.js index 80663ac..06a5d47 100644 --- a/modules/ext/alias.js +++ b/modules/ext/alias.js @@ -3,8 +3,10 @@ class AliasModule { this.commands = {}; this.terminal = null; this.key = 'webx11_aliases'; - this.defaults = { 'll': 'ls', 'h': 'help', 'cls': 'clear' }; + this.defaults = { 'll': 'ls', 'h': 'help', 'cls': 'clear', 'sh': 'script' }; this.aliases = this.load(); + this._aliasDepth = 0; + this._aliasMaxDepth = 10; } getHelpInfo() { @@ -37,7 +39,10 @@ class AliasModule { if (name === 'alias' || name === 'unalias') return; const target = this.aliases[name]; this.commands[name] = (args) => { - this.terminal.executeCommand(target + (args.length ? ' ' + args.join(' ') : '')); + if (this._aliasDepth >= this._aliasMaxDepth) { this.terminal.print(`alias: max depth (${this._aliasMaxDepth}) reached`, 'error'); return; } + this._aliasDepth++; + try { this.terminal.executeCommand(target + (args.length ? ' ' + args.join(' ') : '')); } + finally { this._aliasDepth--; } }; }); } @@ -64,7 +69,8 @@ class AliasModule { // Show specific alias value const name = args[0]; if (this.aliases[name]) { - this.terminal.print(`${name}=${this.aliases[name]}`, 'info'); + const mark = this.defaults[name] ? ' (built-in)' : ''; + this.terminal.print(`${name}=${this.aliases[name]}${mark}`, 'info'); } else { this.terminal.print(`Alias '${name}' not found`, 'error'); } @@ -77,15 +83,19 @@ class AliasModule { if (name === 'alias' || name === 'unalias') { return this.terminal.print('Cannot alias reserved command name', 'error'); } + if (this.defaults[name]) { + return this.terminal.print(`Cannot alias built-in alias '${name}'`, 'warning'); + } this.aliases[name] = value; this.save(); // Register the new alias as a command immediately - if (!this.defaults[name]) { - const target = value; - this.commands[name] = (args) => { - this.terminal.executeCommand(target + (args.length ? ' ' + args.join(' ') : '')); + const target = value; + this.commands[name] = (args) => { + if (this._aliasDepth >= this._aliasMaxDepth) { this.terminal.print(`alias: max depth (${this._aliasMaxDepth}) reached`, 'error'); return; } + this._aliasDepth++; + try { this.terminal.executeCommand(target + (args.length ? ' ' + args.join(' ') : '')); } + finally { this._aliasDepth--; } }; - } this.terminal.print(`Alias set: ${name}=${value}`, 'success'); } handleUnalias(args) { diff --git a/modules/ext/floating.js b/modules/ext/floating.js new file mode 100644 index 0000000..b6f5a93 --- /dev/null +++ b/modules/ext/floating.js @@ -0,0 +1,331 @@ +// FloatingModule — Remixable floating UI elements (buttons, menus, panels) +// Persisted in localStorage. Created/modified/removed via terminal commands. +// Accessible to AI via normal run_command path. + +class FloatingModule { + constructor() { + this.commands = {}; + this.terminal = null; + this._elements = {}; + this._storageKey = 'webx11_floating_elements'; + } + + requirements() { return { met: true, missing: [] }; } + + getHelpInfo() { + return { + 'floating-add': 'floating-add btn|menu|panel <id> <label> [command|buttons...] — Add a floating UI element', + 'floating-remove': 'floating-remove <id> — Remove a floating element', + 'floating-list': 'floating-list — Show all floating elements', + 'floating-pos': 'floating-pos <id> <x|auto> <y|auto> — Reposition a floating element', + 'floating-clear': 'floating-clear — Remove all floating elements', + 'floating-toggle': 'floating-toggle <id> — Show/hide a floating element', + }; + } + + registerCommands() { + this.commands['floating-add'] = (args) => this._cmdAdd(args); + this.commands['floating-remove'] = (args) => this._cmdRemove(args); + this.commands['floating-list'] = () => this._cmdList(); + this.commands['floating-pos'] = (args) => this._cmdPos(args); + this.commands['floating-clear'] = () => this._cmdClear(); + this.commands['floating-toggle'] = (args) => this._cmdToggle(args); + } + + seed() { + this._restore(); + } + + // ── Persistence ── + + _save() { + const data = { version: 1, elements: {} }; + for (const [id, el] of Object.entries(this._elements)) { + data.elements[id] = { + type: el.type, + label: el.label, + command: el.command, + buttons: el.buttons, + content: el.content, + x: el.x, + y: el.y, + visible: el.visible !== false, + collapsed: el.collapsed !== false + }; + } + try { + localStorage.setItem(this._storageKey, JSON.stringify(data)); + } catch (e) { + console.warn('FloatingModule: save failed', e); + } + } + + _restore() { + try { + const raw = localStorage.getItem(this._storageKey); + if (!raw) return; + const data = JSON.parse(raw); + if (!data.elements) return; + for (const [id, cfg] of Object.entries(data.elements)) { + this._elements[id] = { + id, + type: cfg.type, + label: cfg.label, + command: cfg.command || '', + buttons: cfg.buttons || [], + content: cfg.content || '', + x: cfg.x != null ? cfg.x : null, + y: cfg.y != null ? cfg.y : null, + visible: cfg.visible !== false, + collapsed: cfg.collapsed !== false + }; + } + } catch (e) { + console.warn('FloatingModule: failed to restore', e); + } + this._renderAll(); + } + + // ── Rendering ── + + _getContainer() { + let container = document.getElementById('floating-container'); + if (!container) { + container = document.createElement('div'); + container.id = 'floating-container'; + const ref = document.getElementById('ai-confirm-area') || document.getElementById('input-area'); + const parent = document.body; + if (ref && ref.parentNode) { + parent.insertBefore(container, ref); + } else { + parent.appendChild(container); + } + } + return container; + } + + _renderAll() { + const container = this._getContainer(); + container.innerHTML = ''; + for (const [id, el] of Object.entries(this._elements)) { + if (!el.visible) continue; + const node = this._renderElement(el); + if (!node) continue; + node.dataset.floatingId = id; + if (el.x != null && el.y != null) { + node.style.position = 'fixed'; + node.style.left = el.x + 'px'; + node.style.top = el.y + 'px'; + node.style.bottom = 'auto'; + node.style.right = 'auto'; + } + container.appendChild(node); + } + } + + _renderElement(el) { + switch (el.type) { + case 'button': return this._renderButton(el); + case 'menu': return this._renderMenu(el); + case 'panel': return this._renderPanel(el); + default: return null; + } + } + + _renderButton(el) { + const btn = document.createElement('button'); + btn.className = 'floating-btn'; + btn.innerHTML = el.label; + btn.onclick = (e) => { + e.stopPropagation(); + if (el.command && this.terminal) { + this.terminal.executeCommand(el.command); + } + }; + return btn; + } + + _renderMenu(el) { + const wrapper = document.createElement('div'); + wrapper.className = 'floating-menu'; + + const header = document.createElement('div'); + header.className = 'floating-menu-header'; + header.innerHTML = `<span class="arrow ${el.collapsed ? 'collapsed' : ''}">▼</span> ${el.label}`; + + const body = document.createElement('div'); + body.className = 'floating-menu-body' + (el.collapsed ? '' : ' expanded'); + + (el.buttons || []).forEach(sub => { + const subBtn = document.createElement('button'); + subBtn.className = 'floating-menu-btn'; + subBtn.textContent = sub.label; + subBtn.onclick = (e) => { + e.stopPropagation(); + if (sub.command && this.terminal) { + this.terminal.executeCommand(sub.command); + } + }; + body.appendChild(subBtn); + }); + + header.onclick = () => { + el.collapsed = !el.collapsed; + const arrow = header.querySelector('.arrow'); + if (arrow) arrow.classList.toggle('collapsed', el.collapsed); + body.classList.toggle('expanded', !el.collapsed); + this._save(); + }; + + wrapper.appendChild(header); + wrapper.appendChild(body); + return wrapper; + } + + _renderPanel(el) { + const panel = document.createElement('div'); + panel.className = 'floating-panel'; + panel.innerHTML = ` + <div class="floating-panel-title">${this._escapeHtml(el.label)}</div> + <div class="floating-panel-content">${this._escapeHtml(el.content)}</div> + `; + return panel; + } + + _escapeHtml(str) { + if (!str) return ''; + return str.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>') + .replace(/"/g, '"').replace(/'/g, '''); + } + + // ── Commands ── + + _cmdAdd(args) { + if (args.length < 3) { + this.terminal.print('Usage: floating-add btn|menu|panel <id> <label> [command|btn:Label|cmd...]', 'warning'); + return; + } + const type = args[0]; + const id = args[1]; + const label = args[2]; + + if (this._elements[id]) { + this.terminal.print(`Element '${id}' already exists`, 'warning'); + return; + } + + const el = { id, type, label, x: null, y: null, visible: true }; + + switch (type) { + case 'btn': + case 'button': + if (args.length < 4) { + this.terminal.print('Usage: floating-add btn <id> <label> <command>', 'warning'); + return; + } + el.type = 'button'; + el.command = args.slice(3).join(' '); + break; + case 'menu': + el.type = 'menu'; + el.collapsed = true; + el.buttons = []; + for (let i = 3; i < args.length; i++) { + const m = args[i].match(/^btn:(.+?)\|(.+)$/); + if (m) { + el.buttons.push({ label: m[1], command: m[2] }); + } + } + if (el.buttons.length === 0) { + this.terminal.print('No valid sub-buttons. Use btn:Label|command format', 'warning'); + return; + } + break; + case 'panel': + el.type = 'panel'; + el.content = args.slice(3).join(' ') || ''; + break; + default: + this.terminal.print(`Unknown type '${type}'. Use: btn, menu, panel`, 'warning'); + return; + } + + this._elements[id] = el; + this._save(); + this._renderAll(); + this.terminal.print(`Floating '${type}' '${label}' created (id: ${id})`, 'success'); + } + + _cmdRemove(args) { + if (!args[0]) { + this.terminal.print('Usage: floating-remove <id>', 'warning'); + return; + } + if (!this._elements[args[0]]) { + this.terminal.print(`Element '${args[0]}' not found`, 'warning'); + return; + } + delete this._elements[args[0]]; + this._save(); + this._renderAll(); + this.terminal.print(`Element '${args[0]}' removed`, 'success'); + } + + _cmdList() { + const ids = Object.keys(this._elements); + if (ids.length === 0) { + this.terminal.print('No floating elements', 'info'); + return; + } + this.terminal.print(`Floating elements (${ids.length}):`, 'info'); + for (const id of ids) { + const el = this._elements[id]; + const pos = (el.x != null) ? `${el.x},${el.y}` : 'auto'; + const vis = el.visible ? 'visible' : 'hidden'; + this.terminal.print(` ${id}: ${el.type} "${el.label}" @${pos} [${vis}]`, 'text-muted'); + if (el.type === 'menu' && el.buttons) { + el.buttons.forEach(b => this.terminal.print(` → btn:${b.label}|${b.command}`, 'text-muted')); + } + } + } + + _cmdPos(args) { + if (args.length < 3) { + this.terminal.print('Usage: floating-pos <id> <x|auto> <y|auto>', 'warning'); + return; + } + const id = args[0]; + if (!this._elements[id]) { + this.terminal.print(`Element '${id}' not found`, 'warning'); + return; + } + this._elements[id].x = args[1] === 'auto' ? null : parseInt(args[1]); + this._elements[id].y = args[2] === 'auto' ? null : parseInt(args[2]); + this._save(); + this._renderAll(); + this.terminal.print(`Element '${id}' repositioned`, 'success'); + } + + _cmdToggle(args) { + if (!args[0]) { + this.terminal.print('Usage: floating-toggle <id>', 'warning'); + return; + } + const id = args[0]; + if (!this._elements[id]) { + this.terminal.print(`Element '${id}' not found`, 'warning'); + return; + } + this._elements[id].visible = !this._elements[id].visible; + this._save(); + this._renderAll(); + this.terminal.print(`Element '${id}' now ${this._elements[id].visible ? 'visible' : 'hidden'}`, 'success'); + } + + _cmdClear() { + this._elements = {}; + this._save(); + this._renderAll(); + this.terminal.print('All floating elements cleared', 'success'); + } +}
\ No newline at end of file diff --git a/modules/ext/shell.js b/modules/ext/shell.js index e87c37c..55d66de 100644 --- a/modules/ext/shell.js +++ b/modules/ext/shell.js @@ -23,6 +23,7 @@ class ShellModule { 'unset': 'unset <var> — Remove a shell variable', 'script': 'script <path.sh> — Run a shell script from VFS', '$': '$ <command> — Execute with shell parsing (default: auto-detected)', + 'sh': 'sh <path.sh> — Run a shell script from VFS (alias for script)', 'set': 'set — List all shell variables', }; } @@ -31,6 +32,7 @@ class ShellModule { this.commands['export'] = (args) => this._cmdExport(args); this.commands['unset'] = (args) => this._cmdUnset(args); this.commands['script'] = (args) => this._cmdScript(args); + this.commands['sh'] = (args) => this._cmdScript(args); this.commands['set'] = () => this._cmdSet(); } diff --git a/modules/ext/skill-pack-core.js b/modules/ext/skill-pack-core.js index 6f7ad4c..abbc7ab 100644 --- a/modules/ext/skill-pack-core.js +++ b/modules/ext/skill-pack-core.js @@ -471,7 +471,41 @@ class SkillPackCoreModule { ## When to use - User wants to brainstorm, organize ideas, plan features - User says "map out", "diagram", "brainstorm", "organize thoughts" -- Visualizing project structure or task decomposition`) +- Visualizing project structure or task decomposition`), + + sk('floating-ui', 'How to create, manage, and remove floating UI elements — buttons, collapsible menus, and status panels that persist across page reloads', ['floating', 'ui', 'buttons', 'menu', 'gui'], 'ui', +`## Commands + +1. **Create a floating button:** + - floating-add btn <id> <label> <command> + - Example: floating-add btn files "📁 Files" ls + - Button appears at bottom-right, persists across reloads + +2. **Create a collapsible menu:** + - floating-add menu <id> <label> btn:SubLabel|command btn:Sub2|cmd2 ... + - Example: floating-add menu tools "🔧 Tools" btn:Help|help btn:Clear|clear + - Click header to expand/collapse. State persists. + +3. **Create a text panel:** + - floating-add panel <id> <title> <content> + - Example: floating-add panel status "📊 Status" "CPU: 2%" + +4. **Manage elements:** + - floating-list — show all elements with IDs and positions + - floating-remove <id> — delete by ID + - floating-toggle <id> — show/hide without deleting + - floating-pos <id> x|auto y|auto — reposition or reset to auto + - floating-clear — remove everything + +5. **Persistence:** + - All elements auto-save to localStorage (webx11_floating_elements) + - Survive page reload, browser restart + - Run floating-clear to start fresh + +## When to use +- User wants quick-access buttons instead of typing commands +- User says "add a button for X", "make a shortcut", "floating menu" +- Creating a dashboard with status panels and action buttons`) ]; } registerCommands() { diff --git a/modules/test/test-suite.js b/modules/test/test-suite.js index 760259d..925a83e 100644 --- a/modules/test/test-suite.js +++ b/modules/test/test-suite.js @@ -242,6 +242,60 @@ class TestSuiteModule { destructive: false }, + // ── Aliases ── + 'alias-list': { + desc: 'alias shows built-in defaults', + cmd: 'alias', + expect: (out) => out.includes('ll=ls') && out.includes('h=help') && out.includes('cls=clear') && out.includes('sh=script'), + destructive: false + }, + 'alias-show-specific': { + desc: 'alias <name> shows a specific alias value', + cmd: 'alias ll', + expect: (out) => out.includes('ll=ls') && out.includes('built-in'), + destructive: false + }, + 'alias-missing-unknown': { + desc: 'alias <unknown> shows not found', + cmd: 'alias non_existent_alias_xyz', + expect: (out) => out.includes('not found'), + destructive: false + }, + 'unalias-builtin-refused': { + desc: 'unalias refuses to remove built-in alias', + cmd: 'unalias ll', + expect: (out) => out.includes('Cannot unalias built-in'), + destructive: false + }, + 'alias-create-and-run': { + desc: 'create custom alias and execute it', + cmd_parts: [ + { cmd: 'alias greet=echo hello from alias' }, + { cmd: 'greet', expect: (out) => out.includes('hello from alias') }, + { cmd: 'unalias greet' }, + ], + destructive: true + }, + 'alias-overwrite': { + desc: 'create, then overwrite, execute updated alias', + cmd_parts: [ + { cmd: 'alias greet2=echo first' }, + { cmd: 'alias greet2=echo second' }, + { cmd: 'greet2', expect: (out) => out.includes('second') }, + { cmd: 'unalias greet2' }, + ], + destructive: true + }, + 'unalias-custom': { + desc: 'unalias removes a custom alias', + cmd_parts: [ + { cmd: 'alias tempcmd=echo temp' }, + { cmd: 'unalias tempcmd' }, + { cmd: 'alias tempcmd', expect: (out) => out.includes('not found') }, + ], + destructive: true + }, + // ── Modules ── 'commands-help': { desc: 'help lists multiple command categories', |