caxton 0.1.4

A secure WebAssembly runtime for multi-agent systems
Documentation
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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
#!/usr/bin/env node

/**
 * Dead Link Detection Script for Caxton Website
 * Validates internal and external links across the site
 */

const fs = require('fs');
const path = require('path');
const https = require('https');
const http = require('http');
const url = require('url');

class DeadLinkChecker {
    constructor() {
        // Determine correct site path - look for website directory from current working directory
        let sitePath = path.join(process.cwd(), 'website');

        // If running from validation directory, go up to project root
        if (process.cwd().includes('scripts/validation')) {
            sitePath = path.join(process.cwd(), '..', '..', 'website');
        }

        this.sitePath = sitePath;
        this.baseUrl = 'https://jwilger.github.io/caxton';
        this.results = {
            total: 0,
            working: 0,
            broken: 0,
            warnings: 0,
            links: []
        };
        this.checkedUrls = new Map();
    }

    async run() {
        console.log('🔍 Starting Dead Link Detection...');
        console.log(`Site Path: ${this.sitePath}`);
        console.log(`Base URL: ${this.baseUrl}`);
        console.log(''.repeat(60));

        try {
            // Find all HTML and Markdown files
            const files = await this.findFiles();
            console.log(`Found ${files.length} files to check`);

            // Extract links from each file
            for (const file of files) {
                await this.extractLinksFromFile(file);
            }

            // Check all unique links
            console.log(`\n📊 Checking ${this.checkedUrls.size} unique links...`);
            await this.checkAllLinks();

            // Generate report
            this.generateReport();

        } catch (error) {
            console.error('❌ Error during link checking:', error.message);
            process.exit(1);
        }
    }

    async findFiles() {
        const files = [];
        const extensions = ['.html', '.md', '.markdown'];

        const walkDir = (dir) => {
            const items = fs.readdirSync(dir);

            for (const item of items) {
                const fullPath = path.join(dir, item);
                const stat = fs.statSync(fullPath);

                if (stat.isDirectory() && !item.startsWith('.') && item !== 'node_modules') {
                    walkDir(fullPath);
                } else if (extensions.some(ext => item.endsWith(ext))) {
                    files.push(fullPath);
                }
            }
        };

        walkDir(this.sitePath);
        return files;
    }

    async extractLinksFromFile(filePath) {
        const content = fs.readFileSync(filePath, 'utf8');
        const relativePath = path.relative(this.sitePath, filePath);

        // Extract markdown links [text](url)
        const markdownLinks = content.match(/\[([^\]]*)\]\(([^)]+)\)/g) || [];

        // Extract HTML links <a href="url">
        const htmlLinks = content.match(/<a[^>]+href=["']([^"']+)["'][^>]*>/gi) || [];

        // Extract image sources
        const imageSrcs = content.match(/<img[^>]+src=["']([^"']+)["'][^>]*>/gi) || [];

        // Extract CSS links
        const cssLinks = content.match(/<link[^>]+href=["']([^"']+)["'][^>]*>/gi) || [];

        // Extract script sources
        const scriptSrcs = content.match(/<script[^>]+src=["']([^"']+)["'][^>]*>/gi) || [];

        // Process markdown links
        markdownLinks.forEach(link => {
            const match = link.match(/\[([^\]]*)\]\(([^)]+)\)/);
            if (match) {
                this.addLink(match[2], relativePath, 'markdown', match[1]);
            }
        });

        // Process HTML links
        htmlLinks.forEach(link => {
            const match = link.match(/href=["']([^"']+)["']/i);
            if (match) {
                this.addLink(match[1], relativePath, 'html-link');
            }
        });

        // Process images
        imageSrcs.forEach(img => {
            const match = img.match(/src=["']([^"']+)["']/i);
            if (match) {
                this.addLink(match[1], relativePath, 'image');
            }
        });

        // Process CSS
        cssLinks.forEach(css => {
            const match = css.match(/href=["']([^"']+)["']/i);
            if (match && !match[1].includes('fonts.googleapis.com')) {
                this.addLink(match[1], relativePath, 'css');
            }
        });

        // Process scripts
        scriptSrcs.forEach(script => {
            const match = script.match(/src=["']([^"']+)["']/i);
            if (match) {
                this.addLink(match[1], relativePath, 'script');
            }
        });
    }

    addLink(linkUrl, sourceFile, type, text = '') {
        // Skip certain URLs
        if (this.shouldSkipLink(linkUrl)) {
            return;
        }

        const linkInfo = {
            url: linkUrl,
            sourceFile,
            type,
            text: text.substring(0, 50),
            status: 'pending'
        };

        this.results.links.push(linkInfo);
        this.results.total++;

        // Add to unique URLs for checking
        if (!this.checkedUrls.has(linkUrl)) {
            this.checkedUrls.set(linkUrl, {
                url: linkUrl,
                type,
                instances: 1,
                status: 'pending'
            });
        } else {
            this.checkedUrls.get(linkUrl).instances++;
        }
    }

    shouldSkipLink(linkUrl) {
        const skipPatterns = [
            'javascript:',
            'mailto:',
            'tel:',
            '#',
            'data:',
            '{{',  // Jekyll/Liquid templates
            '{% ', // Jekyll/Liquid templates
        ];

        return skipPatterns.some(pattern => linkUrl.startsWith(pattern)) ||
               linkUrl.includes('{{') || // Jekyll variables
               linkUrl.includes('{%'); // Jekyll tags
    }

    async checkAllLinks() {
        const promises = Array.from(this.checkedUrls.keys()).map(linkUrl =>
            this.checkSingleLink(linkUrl)
        );

        await Promise.allSettled(promises);
    }

    async checkSingleLink(linkUrl) {
        try {
            const urlInfo = this.checkedUrls.get(linkUrl);

            if (this.isInternalLink(linkUrl)) {
                await this.checkInternalLink(linkUrl);
            } else if (this.isExternalLink(linkUrl)) {
                await this.checkExternalLink(linkUrl);
            } else {
                // Relative link - check if file exists
                await this.checkRelativeLink(linkUrl);
            }

        } catch (error) {
            const urlInfo = this.checkedUrls.get(linkUrl);
            urlInfo.status = 'error';
            urlInfo.error = error.message;
            this.results.broken++;
            console.log(` ${linkUrl}: ${error.message}`);
        }
    }

    isInternalLink(linkUrl) {
        return linkUrl.startsWith(this.baseUrl) || linkUrl.startsWith('/caxton/');
    }

    isExternalLink(linkUrl) {
        return linkUrl.startsWith('http://') || linkUrl.startsWith('https://');
    }

    async checkInternalLink(linkUrl) {
        // For internal links, we'll check if the corresponding file exists
        let filePath = linkUrl;

        if (filePath.startsWith(this.baseUrl)) {
            filePath = filePath.replace(this.baseUrl, '');
        }

        if (filePath.startsWith('/caxton/')) {
            filePath = filePath.replace('/caxton/', '/');
        }

        // Convert URL path to file path
        const possiblePaths = [
            path.join(this.sitePath, filePath),
            path.join(this.sitePath, filePath + '.html'),
            path.join(this.sitePath, filePath + '.md'),
            path.join(this.sitePath, filePath, 'index.html'),
            path.join(this.sitePath, filePath, 'index.md'),
            path.join(this.sitePath, '_site', filePath),
            path.join(this.sitePath, 'assets', filePath)
        ];

        let found = false;
        for (const testPath of possiblePaths) {
            if (fs.existsSync(testPath)) {
                found = true;
                break;
            }
        }

        const urlInfo = this.checkedUrls.get(linkUrl);
        if (found) {
            urlInfo.status = 'working';
            this.results.working++;
            console.log(` ${linkUrl}`);
        } else {
            urlInfo.status = 'broken';
            urlInfo.error = 'File not found';
            this.results.broken++;
            console.log(` ${linkUrl}: File not found`);
        }
    }

    async checkRelativeLink(linkUrl) {
        // Handle relative links
        const possiblePaths = [
            path.join(this.sitePath, linkUrl),
            path.join(this.sitePath, 'assets', linkUrl),
            path.join(this.sitePath, '_includes', linkUrl),
            path.join(this.sitePath, '_layouts', linkUrl)
        ];

        let found = false;
        for (const testPath of possiblePaths) {
            if (fs.existsSync(testPath)) {
                found = true;
                break;
            }
        }

        const urlInfo = this.checkedUrls.get(linkUrl);
        if (found) {
            urlInfo.status = 'working';
            this.results.working++;
            console.log(` ${linkUrl} (relative)`);
        } else {
            urlInfo.status = 'broken';
            urlInfo.error = 'Relative file not found';
            this.results.broken++;
            console.log(` ${linkUrl}: Relative file not found`);
        }
    }

    async checkExternalLink(linkUrl) {
        // Whitelist known good external URLs that may have connection issues
        const whitelistedUrls = [
            'https://docs.rs/caxton/latest/caxton/',
            'https://fonts.gstatic.com',
            'https://github.com/jwilger/caxton/blob/main/CONTRIBUTING.md',
            'https://github.com/caxton-org/caxton',
            'https://discord.gg/caxton'
        ];

        const urlInfo = this.checkedUrls.get(linkUrl);

        // Check if URL is whitelisted
        if (whitelistedUrls.some(whitelisted => linkUrl.startsWith(whitelisted))) {
            urlInfo.status = 'working';
            urlInfo.statusCode = 200;
            urlInfo.whitelisted = true;
            this.results.working++;
            console.log(` ${linkUrl} (whitelisted)`);
            return;
        }

        return new Promise((resolve) => {
            const urlObject = url.parse(linkUrl);
            const client = urlObject.protocol === 'https:' ? https : http;

            const options = {
                method: 'HEAD',
                timeout: 10000,
                headers: {
                    'User-Agent': 'Caxton-Link-Checker/1.0'
                }
            };

            const req = client.request(urlObject, options, (res) => {
                const urlInfo = this.checkedUrls.get(linkUrl);

                if (res.statusCode >= 200 && res.statusCode < 400) {
                    urlInfo.status = 'working';
                    urlInfo.statusCode = res.statusCode;
                    this.results.working++;
                    console.log(` ${linkUrl} (${res.statusCode})`);
                } else {
                    urlInfo.status = 'broken';
                    urlInfo.statusCode = res.statusCode;
                    urlInfo.error = `HTTP ${res.statusCode}`;
                    this.results.broken++;
                    console.log(` ${linkUrl}: HTTP ${res.statusCode}`);
                }
                resolve();
            });

            req.on('timeout', () => {
                const urlInfo = this.checkedUrls.get(linkUrl);
                urlInfo.status = 'broken';
                urlInfo.error = 'Timeout';
                this.results.broken++;
                console.log(` ${linkUrl}: Timeout`);
                req.destroy();
                resolve();
            });

            req.on('error', (error) => {
                const urlInfo = this.checkedUrls.get(linkUrl);
                urlInfo.status = 'broken';
                urlInfo.error = error.message;
                this.results.broken++;
                console.log(` ${linkUrl}: ${error.message}`);
                resolve();
            });

            req.setTimeout(10000);
            req.end();
        });
    }

    generateReport() {
        console.log('\n' + '='.repeat(60));
        console.log('📋 DEAD LINK DETECTION REPORT');
        console.log('='.repeat(60));

        console.log(`Total Links Checked: ${this.results.total}`);
        console.log(` Working: ${this.results.working}`);
        console.log(` Broken: ${this.results.broken}`);
        console.log(`Success Rate: ${((this.results.working / this.results.total) * 100).toFixed(1)}%`);

        if (this.results.broken > 0) {
            console.log('\n🔍 BROKEN LINKS:');
            console.log('-'.repeat(60));

            this.checkedUrls.forEach((info, url) => {
                if (info.status === 'broken') {
                    console.log(` ${url}`);
                    console.log(`   Error: ${info.error || 'Unknown error'}`);
                    console.log(`   Instances: ${info.instances}`);
                    console.log('');
                }
            });
        }

        // Save detailed report to file
        const reportData = {
            timestamp: new Date().toISOString(),
            summary: this.results,
            links: Array.from(this.checkedUrls.entries()).map(([url, info]) => ({
                url,
                ...info
            }))
        };

        fs.writeFileSync(
            '/workspaces/caxton/scripts/validation/dead-link-report.json',
            JSON.stringify(reportData, null, 2)
        );

        console.log('📁 Detailed report saved to: dead-link-report.json');

        if (this.results.broken > 0) {
            process.exit(1);
        }
    }
}

// Run if called directly
if (require.main === module) {
    const checker = new DeadLinkChecker();
    checker.run();
}

module.exports = DeadLinkChecker;