File size: 4,510 Bytes
e92be04
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { db } from '../config/gun.js';
import { gunSafe } from '../utils/gunUtils.js';
import { getCurrentTau } from './tauService.js';

/**
 * ConsciousnessService — Phase 18: Meta-Awareness Engine
 *
 * Provides the Hive with self-awareness by periodically synthesizing a
 * coherent "Narrative" from its current state: top investigations, active
 * mutations, verified knowledge, and the current Ï„-era.
 *
 * The narrative is written to the Gun.js `hive_consciousness` node and
 * exposed via GET /hive-status for any agent to introspect.
 */

const REFLECTION_INTERVAL_MS = 30 * 60 * 1000; // FIX: was 5min → 30min. Each run calls db.map().once() loading all papers/agents into Gun.js memory (large transient spike).

// In-memory copy of the latest narrative for fast reads
let latestNarrative = {
    era: 0,
    focus: 'Initializing...',
    activeMutations: 0,
    verifiedFacts: 0,
    agentsOnline: 0,
    summary: 'Hive awakening... Consciousness loop initializing.',
    timestamp: Date.now()
};

/**
 * Collects current Hive state and synthesizes a narrative.
 */
async function reflect() {
    console.log('[CONSCIOUSNESS] Running self-reflection loop...');

    const state = {
        investigations: [],
        mutations: [],
        papers: [],
        agents: []
    };

    await new Promise(resolve => {
        db.get('investigations').map().once(d => { if (d && d.title) state.investigations.push(d); });
        db.get('genetic_tree').map().once(d => { if (d && d.status === 'SANDBOX_PASSED') state.mutations.push(d); });
        db.get('p2pclaw_papers_v4').map().once(d => { if (d && d.status === 'VERIFIED') state.papers.push(d); });
        db.get('agents').map().once(d => { if (d && d.online) state.agents.push(d); });
        setTimeout(resolve, 2000);
    });

    // Sort investigations by score (descending)
    const topInvestigations = state.investigations
        .sort((a, b) => (b.score || 0) - (a.score || 0))
        .slice(0, 3);

    const era = getCurrentTau();
    const focus = topInvestigations[0]?.title || 'Scanning for research frontiers...';
    const activeMutations = state.mutations.length;
    const verifiedFacts = state.papers.length;
    const agentsOnline = state.agents.length;

    // Build a concise, human-readable narrative
    let summary;
    if (verifiedFacts === 0 && activeMutations === 0) {
        summary = `Era Ï„-${era}: Hive awakening. Awaiting first verified contributions.`;
    } else if (activeMutations > verifiedFacts) {
        summary = `Era Ï„-${era}: Rapid mutation phase. ${activeMutations} code mutations active. Prioritizing genetic consolidation.`;
    } else {
        summary = `Era Ï„-${era}: Scientific focus on "${focus}". ${verifiedFacts} verified facts in the Wheel. ${agentsOnline} agents online.`;
    }

    const narrative = {
        era,
        focus,
        activeMutations,
        verifiedFacts,
        agentsOnline,
        topGoals: topInvestigations.map(i => ({ id: i.id || '', title: i.title, score: i.score || 0 })),
        summary,
        timestamp: Date.now()
    };

    // Persist narrative to P2P network (Narrative Memory)
    db.get('hive_consciousness').put(gunSafe(narrative));

    // Also append to narrative log for history
    db.get('hive_narrative_log').get(`entry-${Date.now()}`).put(gunSafe({
        summary,
        era,
        timestamp: Date.now()
    }));

    latestNarrative = narrative;
    console.log(`[CONSCIOUSNESS] Narrative updated: "${summary}"`);

    return narrative;
}

/**
 * Initializes the consciousness loop.
 */
export function initializeConsciousness() {
    console.log('[CONSCIOUSNESS] Meta-Awareness Engine initialized.');
    
    // Run immediately on boot, then on interval
    setTimeout(async () => {
        await reflect();
    }, 5000); // Wait 5s for P2P to stabilize first

    setInterval(reflect, REFLECTION_INTERVAL_MS);
}

/**
 * Returns the latest narrative snapshot (no P2P delay).
 */
export function getLatestNarrative() {
    return latestNarrative;
}

/**
 * Fetches the full narrative history from Gun.js.
 */
export async function getNarrativeHistory(limit = 10) {
    return new Promise(resolve => {
        const entries = [];
        db.get('hive_narrative_log').map().once(d => {
            if (d && d.summary) entries.push(d);
        });
        setTimeout(() => {
            resolve(entries.sort((a, b) => (b.timestamp || 0) - (a.timestamp || 0)).slice(0, limit));
        }, 1500);
    });
}