trusty-mpm 1.0.2

trusty-mpm: unified multi-agent orchestration platform (core, daemon, CLI, TUI, Telegram)
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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
# Framework-Specific Performance Patterns

Optimization strategies tailored to popular web frameworks and build tools.

---

## Table of Contents

1. [Next.js Optimization]#nextjs-optimization
2. [React Optimization]#react-optimization
3. [Vue Optimization]#vue-optimization
4. [Vite Optimization]#vite-optimization
5. [Astro Optimization]#astro-optimization
6. [SvelteKit Optimization]#sveltekit-optimization

---

## Next.js Optimization

**Version:** Next.js 14+ (App Router and Pages Router)

### Image Optimization

Next.js Image component provides automatic optimization:

```jsx
import Image from 'next/image';

// ❌ BAD: Standard img tag
<img src="/hero.jpg" alt="Hero" />

// ✅ GOOD: Next.js Image component
<Image
    src="/hero.jpg"
    alt="Hero"
    width={1200}
    height={600}
    priority  // Preload LCP image
/>

// ✅ BETTER: Responsive images
<Image
    src="/hero.jpg"
    alt="Hero"
    fill
    sizes="(max-width: 768px) 100vw, (max-width: 1200px) 50vw, 33vw"
    priority
/>
```

**next.config.js optimization:**

```javascript
module.exports = {
    images: {
        formats: ['image/avif', 'image/webp'],  // Modern formats
        deviceSizes: [640, 750, 828, 1080, 1200, 1920],
        imageSizes: [16, 32, 48, 64, 96, 128, 256, 384],
        minimumCacheTTL: 60,
        remotePatterns: [
            {
                protocol: 'https',
                hostname: 'cdn.example.com',
            },
        ],
    },
};
```

**Impact:** Automatic WebP/AVIF, responsive sizes, lazy loading

---

### Font Optimization

**App Router (`app/layout.tsx`):**

```tsx
import { Inter, Roboto_Mono } from 'next/font/google';

// Load Google Fonts with automatic optimization
const inter = Inter({
    subsets: ['latin'],
    display: 'swap',  // Prevent FOIT
    variable: '--font-inter',
});

const robotoMono = Roboto_Mono({
    subsets: ['latin'],
    display: 'swap',
    variable: '--font-roboto-mono',
});

export default function RootLayout({ children }: { children: React.ReactNode }) {
    return (
        <html lang="en" className={`${inter.variable} ${robotoMono.variable}`}>
            <body>{children}</body>
        </html>
    );
}
```

**Custom fonts (local):**

```tsx
import localFont from 'next/font/local';

const myFont = localFont({
    src: './fonts/my-font.woff2',
    display: 'swap',
    variable: '--font-custom',
});
```

**Impact:** Zero CLS, automatic preloading, self-hosted fonts

---

### Static Generation vs SSR vs ISR

**Performance trade-offs:**

| Strategy | TTFB | Build Time | Use Case |
|----------|------|------------|----------|
| **Static (SSG)** | <100ms | Long | Blog posts, marketing pages |
| **ISR** | <200ms | Medium | E-commerce, dynamic content with caching |
| **SSR** | 500-2000ms | Fast | User-specific pages, real-time data |
| **Client-side** | <100ms (HTML) | Fast | SPAs, dashboards |

**Static Generation (fastest TTFB):**

```tsx
// app/blog/[slug]/page.tsx
export async function generateStaticParams() {
    const posts = await getPosts();
    return posts.map((post) => ({ slug: post.slug }));
}

export default async function BlogPost({ params }: { params: { slug: string } }) {
    const post = await getPost(params.slug);
    return <article>{post.content}</article>;
}
```

**ISR (balanced):**

```tsx
// Revalidate every 60 seconds
export const revalidate = 60;

export default async function ProductPage({ params }: { params: { id: string } }) {
    const product = await getProduct(params.id);
    return <div>{product.name}</div>;
}
```

**SSR (dynamic data):**

```tsx
// Force dynamic rendering
export const dynamic = 'force-dynamic';

export default async function UserDashboard() {
    const session = await getSession();
    const userData = await getUserData(session.userId);
    return <Dashboard data={userData} />;
}
```

---

### Route Prefetching

Next.js automatically prefetches routes on `<Link>`:

```tsx
import Link from 'next/link';

// ✅ GOOD: Automatic prefetching
<Link href="/about" prefetch>About</Link>

// Disable prefetching for less important routes
<Link href="/legal" prefetch={false}>Legal</Link>

// Programmatic prefetching
import { useRouter } from 'next/navigation';

const router = useRouter();
router.prefetch('/products');
```

**Impact:** Instant navigation for prefetched routes

---

### Bundle Analysis

```bash
# Install bundle analyzer
npm install --save-dev @next/bundle-analyzer

# next.config.js
const withBundleAnalyzer = require('@next/bundle-analyzer')({
    enabled: process.env.ANALYZE === 'true',
});

module.exports = withBundleAnalyzer({
    // ... config
});

# Run analysis
ANALYZE=true npm run build
```

---

### Edge Runtime (App Router)

**Deploy performance-critical routes to edge:**

```tsx
// app/api/data/route.ts
export const runtime = 'edge';  // Run on Edge, not Node.js

export async function GET() {
    const data = await fetch('https://api.example.com/data');
    return Response.json(data);
}
```

**Impact:** 50-200ms faster TTFB (served from edge locations)

---

### Caching Strategies

**App Router caching:**

```tsx
// Aggressive caching (static data)
const data = await fetch('https://api.example.com/static', {
    cache: 'force-cache',
});

// Revalidate every 60 seconds
const data = await fetch('https://api.example.com/products', {
    next: { revalidate: 60 },
});

// No caching (always fresh)
const data = await fetch('https://api.example.com/user', {
    cache: 'no-store',
});
```

---

## React Optimization

### Component Memoization

**Prevent unnecessary re-renders:**

```jsx
import { memo, useMemo, useCallback } from 'react';

// ❌ BAD: Re-renders on every parent update
function ExpensiveComponent({ data }) {
    const processed = processData(data);  // Expensive computation runs every render
    return <div>{processed}</div>;
}

// ✅ GOOD: Memoize component
const ExpensiveComponent = memo(function ExpensiveComponent({ data }) {
    const processed = useMemo(() => processData(data), [data]);
    return <div>{processed}</div>;
});

// ✅ GOOD: Memoize callbacks
function Parent() {
    const [count, setCount] = useState(0);

    // ❌ BAD: New function every render
    const handleClick = () => setCount(count + 1);

    // ✅ GOOD: Memoized callback
    const handleClick = useCallback(() => setCount(c => c + 1), []);

    return <Child onClick={handleClick} />;
}
```

**When to memoize:**
- Components that render frequently with same props
- Expensive computations
- Callbacks passed to memoized child components

**When NOT to memoize:**
- Simple components (overhead not worth it)
- Props change frequently
- Premature optimization

---

### Code Splitting and Lazy Loading

```jsx
import { lazy, Suspense } from 'react';

// ❌ BAD: Load everything upfront
import HeavyChart from './HeavyChart';
import Dashboard from './Dashboard';

// ✅ GOOD: Lazy load heavy components
const HeavyChart = lazy(() => import('./HeavyChart'));
const Dashboard = lazy(() => import('./Dashboard'));

function App() {
    return (
        <Suspense fallback={<LoadingSpinner />}>
            <Dashboard />
            <HeavyChart />
        </Suspense>
    );
}
```

**Route-based code splitting:**

```jsx
import { lazy, Suspense } from 'react';
import { BrowserRouter, Routes, Route } from 'react-router-dom';

const Home = lazy(() => import('./routes/Home'));
const About = lazy(() => import('./routes/About'));
const Contact = lazy(() => import('./routes/Contact'));

function App() {
    return (
        <BrowserRouter>
            <Suspense fallback={<div>Loading...</div>}>
                <Routes>
                    <Route path="/" element={<Home />} />
                    <Route path="/about" element={<About />} />
                    <Route path="/contact" element={<Contact />} />
                </Routes>
            </Suspense>
        </BrowserRouter>
    );
}
```

**Impact:** 30-50% smaller initial bundle

---

### Virtual Scrolling

**For long lists (1000+ items):**

```jsx
import { FixedSizeList } from 'react-window';

// ❌ BAD: Render all 10,000 items (slow!)
function List({ items }) {
    return (
        <div>
            {items.map(item => (
                <div key={item.id}>{item.name}</div>
            ))}
        </div>
    );
}

// ✅ GOOD: Virtual scrolling (only render visible items)
function VirtualizedList({ items }) {
    const Row = ({ index, style }) => (
        <div style={style}>{items[index].name}</div>
    );

    return (
        <FixedSizeList
            height={600}
            itemCount={items.length}
            itemSize={50}
            width="100%"
        >
            {Row}
        </FixedSizeList>
    );
}
```

**Impact:** 10-100x faster for long lists

---

### React Profiler

**Identify performance bottlenecks:**

```jsx
import { Profiler } from 'react';

function onRenderCallback(
    id,
    phase,  // "mount" or "update"
    actualDuration,  // Time spent rendering
    baseDuration,  // Estimated time without memoization
    startTime,
    commitTime
) {
    console.log(`${id} (${phase}) took ${actualDuration}ms`);
}

<Profiler id="Dashboard" onRender={onRenderCallback}>
    <Dashboard />
</Profiler>
```

---

## Vue Optimization

### Component Lazy Loading

```vue
<script setup>
import { defineAsyncComponent } from 'vue';

// ❌ BAD: Load everything upfront
import HeavyChart from './HeavyChart.vue';

// ✅ GOOD: Lazy load heavy components
const HeavyChart = defineAsyncComponent(() => import('./HeavyChart.vue'));
</script>

<template>
    <Suspense>
        <HeavyChart />
        <template #fallback>
            <LoadingSpinner />
        </template>
    </Suspense>
</template>
```

---

### Computed Properties vs Methods

```vue
<script setup>
import { ref, computed } from 'vue';

const items = ref([1, 2, 3, 4, 5]);

// ❌ BAD: Method runs on every render
function filteredItems() {
    return items.value.filter(i => i > 2);
}

// ✅ GOOD: Computed property cached
const filteredItems = computed(() => {
    return items.value.filter(i => i > 2);
});
</script>
```

---

### v-once for Static Content

```vue
<template>
    <!-- Render once, never update -->
    <div v-once>
        <h1>{{ staticTitle }}</h1>
        <p>{{ staticContent }}</p>
    </div>

    <!-- Render once per item -->
    <div v-for="item in items" :key="item.id">
        <div v-once>{{ item.staticData }}</div>
        <div>{{ item.dynamicData }}</div>
    </div>
</template>
```

**Impact:** 50-80% faster re-renders for static content

---

### Virtual Scrolling (Vue)

```vue
<script setup>
import { VirtualScroller } from 'vue-virtual-scroller';
import 'vue-virtual-scroller/dist/vue-virtual-scroller.css';

const items = ref([...Array(10000).keys()]);
</script>

<template>
    <VirtualScroller
        :items="items"
        :item-size="50"
        class="scroller"
    >
        <template #default="{ item }">
            <div class="item">{{ item }}</div>
        </template>
    </VirtualScroller>
</template>
```

---

## Vite Optimization

### Build Optimization

```javascript
// vite.config.js
import { defineConfig } from 'vite';

export default defineConfig({
    build: {
        // Chunk splitting strategy
        rollupOptions: {
            output: {
                manualChunks: {
                    // Vendor chunk
                    vendor: ['react', 'react-dom'],
                    // UI library chunk
                    ui: ['@mui/material'],
                },
            },
        },
        // Production optimizations
        minify: 'terser',
        terserOptions: {
            compress: {
                drop_console: true,  // Remove console.log
                drop_debugger: true,
            },
        },
        // Source map (disable in production)
        sourcemap: false,
        // Chunk size warnings
        chunkSizeWarningLimit: 500,
    },
    // CSS code splitting
    css: {
        devSourcemap: false,
    },
});
```

---

### Legacy Browser Support

```javascript
// vite.config.js
import legacy from '@vitejs/plugin-legacy';

export default defineConfig({
    plugins: [
        legacy({
            targets: ['defaults', 'not IE 11'],
            modernPolyfills: true,
            renderLegacyChunks: false,
        }),
    ],
});
```

---

### Preview Mode (Production Testing)

```bash
# Build for production
npm run build

# Preview production build locally
npm run preview
```

---

## Astro Optimization

### Partial Hydration (Component Islands)

**Astro's killer feature: Zero JS by default**

```astro
---
// src/pages/index.astro
import HeavyInteractive from '../components/HeavyInteractive.jsx';
import StaticComponent from '../components/StaticComponent.astro';
---

<html>
    <body>
        <!-- Static component: Zero JS shipped -->
        <StaticComponent />

        <!-- Interactive component: Only load when visible -->
        <HeavyInteractive client:visible />

        <!-- Interactive component: Load immediately -->
        <ChatWidget client:load />

        <!-- Interactive component: Load when idle -->
        <Analytics client:idle />
    </body>
</html>
```

**Client directives:**
- `client:load` - Load immediately (critical interactivity)
- `client:idle` - Load when browser idle (non-critical)
- `client:visible` - Load when scrolled into view (below fold)
- `client:media` - Load based on media query
- `client:only` - Only render on client (skip SSR)

**Impact:** 90-99% less JavaScript shipped

---

### Zero-JS Pages

```astro
---
// Completely static page - ZERO JavaScript
const posts = await getPosts();
---

<html>
    <head>
        <title>Blog</title>
    </head>
    <body>
        {posts.map(post => (
            <article>
                <h2>{post.title}</h2>
                <p>{post.excerpt}</p>
            </article>
        ))}
    </body>
</html>
```

**Impact:** LCP <1s, perfect Lighthouse scores

---

### Build Optimization

```javascript
// astro.config.mjs
export default defineConfig({
    output: 'static',  // or 'server' for SSR
    build: {
        inlineStylesheets: 'auto',
    },
    vite: {
        build: {
            cssCodeSplit: true,
        },
    },
});
```

---

## SvelteKit Optimization

### Prerendering (SSG)

```javascript
// src/routes/blog/[slug]/+page.js
export const prerender = true;

export async function load({ params }) {
    const post = await getPost(params.slug);
    return { post };
}
```

---

### Server-Side Rendering

```javascript
// src/routes/+page.server.js
export async function load() {
    const data = await fetchData();
    return { data };
}
```

---

### Hydration Strategies

```svelte
<!-- +page.svelte -->
<script>
    // Only runs on client after hydration
    import { browser } from '$app/environment';

    if (browser) {
        // Client-only code
        initializeAnalytics();
    }
</script>
```

---

### Build Optimization

```javascript
// svelte.config.js
import adapter from '@sveltejs/adapter-auto';

export default {
    kit: {
        adapter: adapter(),
        prerender: {
            crawl: true,
            entries: ['*'],
        },
    },
    compilerOptions: {
        immutable: true,  // Performance optimization
    },
};
```

---

## Framework Comparison

| Framework | Bundle Size | TTFB | Hydration | Best For |
|-----------|-------------|------|-----------|----------|
| **Next.js** | Medium | Fast (SSG/ISR) | Full | Full-stack apps, SEO |
| **React** | Large | Slow (CSR) | Full | SPAs, dashboards |
| **Vue** | Medium | Medium | Full | Progressive enhancement |
| **Vite** | Small | Fast | Full | Modern build tool |
| **Astro** | Tiny | Very Fast | Partial | Content sites, blogs |
| **SvelteKit** | Small | Fast | Full | High-performance apps |

---

## Framework-Agnostic Tips

### Bundle Size Budgets

```json
{
    "budgets": [
        {
            "type": "bundle",
            "name": "main",
            "baseline": "150kb",
            "warning": "200kb",
            "error": "250kb"
        }
    ]
}
```

---

### Performance Monitoring

**All frameworks can use web-vitals:**

```javascript
import { onCLS, onFCP, onINP, onLCP, onTTFB } from 'web-vitals';

function sendToAnalytics(metric) {
    // Send to your analytics provider
    console.log(metric.name, metric.value);
}

onCLS(sendToAnalytics);
onFCP(sendToAnalytics);
onINP(sendToAnalytics);
onLCP(sendToAnalytics);
onTTFB(sendToAnalytics);
```

---

## Summary Checklist

### Next.js
- [ ] Use Image component with `priority` for LCP images
- [ ] Optimize fonts with `next/font`
- [ ] Choose appropriate rendering strategy (SSG/ISR/SSR)
- [ ] Enable bundle analyzer
- [ ] Use Edge runtime for API routes

### React
- [ ] Memoize expensive components
- [ ] Code split by route
- [ ] Use virtual scrolling for long lists
- [ ] Profile with React DevTools
- [ ] Implement lazy loading

### Vue
- [ ] Use computed properties instead of methods
- [ ] Lazy load heavy components
- [ ] Use `v-once` for static content
- [ ] Implement virtual scrolling
- [ ] Code split by route

### Vite
- [ ] Configure chunk splitting
- [ ] Enable minification
- [ ] Disable source maps in production
- [ ] Use legacy plugin if needed
- [ ] Preview production builds locally

### Astro
- [ ] Use partial hydration (client directives)
- [ ] Prefer zero-JS pages
- [ ] Prerender static routes
- [ ] Inline critical CSS
- [ ] Use component islands pattern

### SvelteKit
- [ ] Prerender static routes
- [ ] Use server-side data loading
- [ ] Optimize hydration strategy
- [ ] Enable compiler optimizations
- [ ] Configure adapter for deployment

---

**Next:** See [core-web-vitals.md](core-web-vitals.md) for metric-specific optimization strategies.