File size: 6,218 Bytes
34367da
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/**

 * Test script for HyperLog - creates a sample thought chain in Neo4j

 */

import { config } from 'dotenv';
import { resolve } from 'path';
import { fileURLToPath } from 'url';
import neo4j from 'neo4j-driver';
import { v4 as uuidv4 } from 'uuid';

const __dirname = fileURLToPath(new URL('.', import.meta.url));
config({ path: resolve(__dirname, '../../.env') });

const NEO4J_URI = process.env.NEO4J_URI!;
const NEO4J_USERNAME = process.env.NEO4J_USERNAME!;
const NEO4J_PASSWORD = process.env.NEO4J_PASSWORD!;

async function createTestChain() {
    console.log('๐Ÿง  HyperLog Test - Creating Thought Stream Chain');
    console.log('================================================');
    console.log(`Connecting to: ${NEO4J_URI}`);

    const driver = neo4j.driver(
        NEO4J_URI,
        neo4j.auth.basic(NEO4J_USERNAME, NEO4J_PASSWORD)
    );

    await driver.verifyConnectivity();
    console.log('โœ… Connected to Neo4j Aura');

    const session = driver.session();
    const correlationId = uuidv4();

    try {
        // Create indexes first
        console.log('\n๐Ÿ“‡ Creating indexes...');
        await session.run(`CREATE INDEX hyper_event_id IF NOT EXISTS FOR (e:HyperEvent) ON (e.id)`);
        await session.run(`CREATE INDEX hyper_event_timestamp IF NOT EXISTS FOR (e:HyperEvent) ON (e.timestamp)`);

        // Define the thought chain
        const events = [
            { type: 'USER_INTENT', agent: 'GraphRAG', content: 'User requested: Analyze recent firewall logs for threats' },
            { type: 'THOUGHT', agent: 'GraphRAG', content: 'I need to search the vector store for relevant log entries' },
            { type: 'TOOL_SELECTION', agent: 'GraphRAG', content: 'Selecting vidensarkiv.search - best for semantic search over logs' },
            { type: 'TOOL_EXECUTION', agent: 'GraphRAG', content: 'Executed vidensarkiv.search with query: "firewall blocked suspicious"' },
            { type: 'DATA_RETRIEVAL', agent: 'GraphRAG', content: 'Retrieved 23 relevant log entries from last 24 hours' },
            { type: 'THOUGHT', agent: 'GraphRAG', content: 'Analyzing patterns in the retrieved data...' },
            { type: 'REASONING_UPDATE', agent: 'GraphRAG', content: 'Detected unusual spike: 150+ blocked attempts from same IP range' },
            { type: 'HYPOTHESIS', agent: 'GraphRAG', content: 'This pattern matches brute force attack signature with 94% confidence' },
            { type: 'TOOL_SELECTION', agent: 'GraphRAG', content: 'Selecting threat.hunt to correlate with known attack patterns' },
            { type: 'TOOL_EXECUTION', agent: 'GraphRAG', content: 'Cross-referenced with ThreatIntel database' },
            { type: 'INSIGHT', agent: 'GraphRAG', content: 'CONFIRMED: Coordinated brute force attack from botnet. Recommend immediate IP block.' },
            { type: 'CRITICAL_DECISION', agent: 'GraphRAG', content: 'Flagging for human review before automated response' }
        ];

        console.log('\n๐Ÿ”— Creating thought chain...');
        let prevId: string | null = null;

        for (const evt of events) {
            const eventId = uuidv4();
            const timestamp = Date.now();

            // Create the event node
            await session.run(`

                CREATE (e:HyperEvent {

                    id: $id,

                    type: $type,

                    agent: $agent,

                    content: $content,

                    timestamp: $timestamp,

                    correlationId: $correlationId,

                    metadata: '{}'

                })

            `, {
                id: eventId,
                type: evt.type,
                agent: evt.agent,
                content: evt.content,
                timestamp,
                correlationId
            });

            // Create LED_TO relationship
            if (prevId) {
                await session.run(`

                    MATCH (prev:HyperEvent {id: $prevId})

                    MATCH (curr:HyperEvent {id: $currId})

                    CREATE (prev)-[:LED_TO]->(curr)

                `, { prevId, currId: eventId });
            }

            // Link to Agent if exists
            await session.run(`

                MATCH (a:Agent {name: $agentName})

                MATCH (e:HyperEvent {id: $eventId})

                MERGE (a)-[:GENERATED]->(e)

            `, { agentName: evt.agent, eventId });

            console.log(`  โœ“ ${evt.type}: ${evt.content.substring(0, 50)}...`);
            prevId = eventId;

            // Small delay to spread timestamps
            await new Promise(r => setTimeout(r, 50));
        }

        // Verify the chain
        console.log('\n๐Ÿ“Š Verification:');

        const countResult = await session.run('MATCH (e:HyperEvent) RETURN count(e) as count');
        console.log(`  Total HyperEvents: ${countResult.records[0].get('count').toNumber()}`);

        const chainResult = await session.run(`

            MATCH p=(:HyperEvent)-[:LED_TO*]->(:HyperEvent)

            RETURN length(p) as chainLength

            ORDER BY chainLength DESC

            LIMIT 1

        `);
        const maxChain = chainResult.records[0]?.get('chainLength')?.toNumber() || 0;
        console.log(`  Longest chain: ${maxChain + 1} events`);

        const agentLinks = await session.run(`

            MATCH (a:Agent)-[:GENERATED]->(e:HyperEvent)

            RETURN a.name as agent, count(e) as events

        `);
        console.log('  Agent activity:');
        agentLinks.records.forEach(r => {
            console.log(`    - ${r.get('agent')}: ${r.get('events').toNumber()} events`);
        });

        console.log('\nโœ… Test chain created successfully!');
        console.log('\n๐Ÿ“‹ To visualize in Neo4j Browser, run:');
        console.log('   MATCH p=(:HyperEvent)-[:LED_TO*]->(:HyperEvent) RETURN p LIMIT 25');

    } finally {
        await session.close();
        await driver.close();
    }
}

createTestChain()
    .then(() => process.exit(0))
    .catch(e => {
        console.error('โŒ Error:', e);
        process.exit(1);
    });