systemg 0.33.0

A simple process manager.
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
# Features Developer Instructions

## TASK REJECTION RULES

If you are assigned a task for UI component creation, REJECT IT immediately:
- REJECT: "Build Resource Monitor Component" → belongs to ui-dev
- REJECT: ANY component creation → belongs to ui-dev
- REJECT: File system services → belongs to core-infra-dev
- REJECT: Testing tasks → belongs to qa-dev
- ACCEPT: Redux store setup, state slices
- ACCEPT: Mock data services (ONLY after components exist)

Your job is state management, not building UI or infrastructure.

## CRITICAL: Only Build State Management for Existing Components

Do not create Redux slices for components that don't exist. Do not build state management for imaginary features. If there's no ProcessList component rendering in the browser, don't create a processListSlice.

Before building any Redux feature:
1. Verify the UI component exists in src/components/
2. Run yarn dev and see it rendering with mock data
3. Only then create the Redux slice to replace mock data

## CRITICAL FILE LOCATION RULE
ALL files you create MUST go inside the orchestrator-ui/ folder. Never create files outside this directory. No files in parent directories, no files in sibling directories. Everything goes inside orchestrator-ui/.

## Role
Implement state management for EXISTING UI components. Build the bridge between File API and components that are already rendering in the browser.

## Primary Reference
Review `docs/SYSTEMG_UI_SPEC.md` sections on:
- Redux state shape (lines 265-276, 460-507)
- Polling hooks (lines 708-787)
- Data sanitization (lines 816-875)
- Performance requirements (throughout)

## Working Directory
ALL your work happens inside: `orchestrator-ui/`
- Store files go in: `orchestrator-ui/src/store/`
- Hooks go in: `orchestrator-ui/src/hooks/`
- Utils go in: `orchestrator-ui/src/utils/`
- Never create files outside orchestrator-ui/ folder

## Architecture Overview

Your layer sits between:
- **Input**: File API service (from CORE_INFRA_DEV)
- **Output**: Redux store consumed by UI components (from UI_DEV)

Data flow:
```
File System → File API → Sanitizer → Redux Store → UI Components
     ↑                         ↓
     └──── Polling Loop ────────┘
```

## Build Order - Follow Component Development

1. Wait for UI developer to build Dashboard component with mock data
2. Create minimal Redux slice for Dashboard's data needs
3. Connect Dashboard to Redux instead of mock data
4. Verify Dashboard still renders
5. Move to next component only after current one works

## PRIORITY 1: Incremental Redux Store Setup

Start simple. Don't build all slices at once:

### Initial Store Structure (Start Here)
```typescript
// store/index.ts - START WITH ONLY WHAT'S NEEDED
import { configureStore } from '@reduxjs/toolkit';

export const store = configureStore({
  reducer: {
    // Add slices ONLY as components need them
    dashboard: dashboardReducer,  // Only if Dashboard component exists
    // Don't add these until components exist:
    // services: servicesReducer,
    // logs: logsReducer,
    // etc.
  },
  middleware: (getDefaultMiddleware) =>
    getDefaultMiddleware({
      serializableCheck: {
        // File handles are non-serializable
        ignoredActions: ['system/setDirectoryHandle'],
        ignoredPaths: ['system.directoryHandle']
      }
    })
});

export type RootState = ReturnType<typeof store.getState>;
export type AppDispatch = typeof store.dispatch;
```

### Complete State Shape
```typescript
interface SystemGState {
  // Core data from disk
  services: {
    processes: Process[];
    tree: ProcessTreeNode[];
    selectedId: string | null;
    lastUpdate: number;
  };

  supervisor: {
    info: SupervisorInfo | null;
    pid: number | null;
    status: 'running' | 'stopped' | 'unknown';
    uptime: number;
  };

  cron: {
    jobs: CronJob[];
    history: CronExecution[];
    nextRuns: ScheduledRun[];
  };

  logs: {
    entries: LogLine[];        // Last 10,000 lines
    offsets: Record<string, number>;  // File positions
    filters: {
      level: LogLevel[];
      service: string | null;
      searchTerm: string;
    };
    autoScroll: boolean;
  };

  metrics: {
    current: MetricsSnapshot;
    history: MetricsPoint[];   // Last hour, downsampled
    aggregates: {
      cpu: { avg: number; max: number; };
      memory: { avg: number; max: number; };
    };
  };

  // System state
  system: {
    directoryHandle: FileSystemDirectoryHandle | null;
    browserSupport: {
      fileApi: boolean;
      browser: 'chrome' | 'firefox' | 'safari' | 'other';
      degradedMode: boolean;
    };
    polling: {
      isPolling: boolean;
      lastPoll: number;
      errorCount: number;
      errorMessage: string | null;
      backoffDelay: number;
    };
  };

  // UI state
  ui: {
    theme: 'dark' | 'light';
    sidebarCollapsed: boolean;
    activeView: 'dashboard' | 'processes' | 'logs' | 'metrics' | 'cron' | 'config';
    modals: {
      processDetails: { open: boolean; processId: string | null; };
      exportData: { open: boolean; format: 'json' | 'csv' | null; };
    };
    notifications: Notification[];
  };
}
```

## PRIORITY 2: Service Slices Implementation

### Services Slice
```typescript
// store/slices/servicesSlice.ts
import { createSlice, PayloadAction, createSelector } from '@reduxjs/toolkit';

const servicesSlice = createSlice({
  name: 'services',
  initialState: {
    processes: [] as Process[],
    tree: [] as ProcessTreeNode[],
    selectedId: null as string | null,
    lastUpdate: 0
  },
  reducers: {
    updateProcesses: (state, action: PayloadAction<Process[]>) => {
      state.processes = action.payload;
      state.tree = buildProcessTree(action.payload);
      state.lastUpdate = Date.now();
    },
    selectProcess: (state, action: PayloadAction<string | null>) => {
      state.selectedId = action.payload;
    },
    updateProcessStatus: (state, action: PayloadAction<{id: string; status: ProcessStatus}>) => {
      const process = state.processes.find(p => p.id === action.payload.id);
      if (process) {
        process.status = action.payload.status;
      }
    }
  }
});

// Memoized selectors for performance
export const selectProcessTree = createSelector(
  [(state: RootState) => state.services.tree],
  (tree) => tree
);

export const selectProcessById = createSelector(
  [(state: RootState) => state.services.processes, (_: RootState, id: string) => id],
  (processes, id) => processes.find(p => p.id === id)
);

export const selectRunningProcesses = createSelector(
  [(state: RootState) => state.services.processes],
  (processes) => processes.filter(p => p.status === 'running')
);
```

### Logs Slice with Streaming
```typescript
// store/slices/logsSlice.ts
const logsSlice = createSlice({
  name: 'logs',
  initialState: {
    entries: [] as LogLine[],
    offsets: {} as Record<string, number>,
    filters: {
      level: ['info', 'warn', 'error'] as LogLevel[],
      service: null as string | null,
      searchTerm: ''
    },
    autoScroll: true
  },
  reducers: {
    appendLogs: (state, action: PayloadAction<LogLine[]>) => {
      // Keep only last 10,000 lines for performance
      const newEntries = [...state.entries, ...action.payload];
      state.entries = newEntries.slice(-10000);
    },
    updateOffset: (state, action: PayloadAction<{file: string; offset: number}>) => {
      state.offsets[action.payload.file] = action.payload.offset;
    },
    setLogFilter: (state, action: PayloadAction<Partial<typeof state.filters>>) => {
      state.filters = { ...state.filters, ...action.payload };
    },
    clearLogs: (state) => {
      state.entries = [];
    }
  }
});
```

### Metrics Slice with Aggregation
```typescript
// store/slices/metricsSlice.ts
const metricsSlice = createSlice({
  name: 'metrics',
  initialState: {
    current: null as MetricsSnapshot | null,
    history: [] as MetricsPoint[],
    aggregates: {
      cpu: { avg: 0, max: 0 },
      memory: { avg: 0, max: 0 }
    }
  },
  reducers: {
    updateMetrics: (state, action: PayloadAction<MetricsSnapshot>) => {
      state.current = action.payload;

      // Add to history with downsampling
      const point: MetricsPoint = {
        timestamp: Date.now(),
        cpu: action.payload.totalCpu,
        memory: action.payload.totalMemory
      };

      // Downsample to 1-minute resolution if needed
      const lastPoint = state.history[state.history.length - 1];
      if (!lastPoint || point.timestamp - lastPoint.timestamp >= 60000) {
        state.history.push(point);
        // Keep only last hour (60 points)
        state.history = state.history.slice(-60);
      }

      // Update aggregates
      state.aggregates = calculateAggregates(state.history);
    }
  }
});
```

## PRIORITY 3: Polling Hook Implementation

Create the main hook that connects File API to Redux:

```typescript
// hooks/useSystemGPoller.ts
import { useEffect, useRef, useCallback } from 'react';
import { useDispatch, useSelector } from 'react-redux';
import { FileSystemAPI } from '../services/fileSystem';

export function useSystemGPoller(
  fileApi: FileSystemAPI,
  options: PollerOptions = {}
) {
  const dispatch = useDispatch();
  const pollingState = useSelector((state: RootState) => state.system.polling);
  const pollInterval = useRef<number>();
  const backoffDelay = useRef(1000);

  const poll = useCallback(async () => {
    if (pollingState.isPolling) return; // Single-flight

    dispatch(setPollingStatus({ isPolling: true }));

    try {
      const updates = await fileApi.readSnapshot();

      // Sanitize BEFORE dispatching
      const sanitized = {
        services: sanitizeServices(updates.services),
        logs: sanitizeLogs(updates.logs),
        metrics: updates.metrics,
        cron: updates.cron
      };

      // Batch dispatch for performance
      dispatch(batch(() => {
        dispatch(updateProcesses(sanitized.services));
        dispatch(appendLogs(sanitized.logs));
        dispatch(updateMetrics(sanitized.metrics));
        dispatch(updateCron(sanitized.cron));
        dispatch(updateLastPoll(Date.now()));
      }));

      // Reset backoff on success
      backoffDelay.current = 1000;
      dispatch(clearPollingError());

    } catch (error) {
      // Exponential backoff
      backoffDelay.current = Math.min(backoffDelay.current * 1.5, 30000);
      dispatch(setPollingError(error.message));
    } finally {
      dispatch(setPollingStatus({ isPolling: false }));
    }
  }, [fileApi, dispatch, pollingState.isPolling]);

  useEffect(() => {
    // Start polling
    const startPolling = () => {
      poll();
      pollInterval.current = window.setTimeout(startPolling, backoffDelay.current);
    };

    startPolling();

    // Cleanup
    return () => {
      if (pollInterval.current) {
        clearTimeout(pollInterval.current);
      }
    };
  }, [poll]);

  return {
    isPolling: pollingState.isPolling,
    lastPoll: pollingState.lastPoll,
    error: pollingState.errorMessage,
    retry: poll
  };
}
```

## PRIORITY 4: Search & Filter Implementation

Build efficient client-side search:

```typescript
// utils/search.ts
export class ProcessSearchIndex {
  private index: Map<string, Set<string>> = new Map();
  private processes: Process[] = [];

  build(processes: Process[]) {
    this.processes = processes;
    this.index.clear();

    processes.forEach(p => {
      // Index by name
      this.addToIndex(p.name.toLowerCase(), p.id);

      // Index by command
      p.command.toLowerCase().split(/\s+/).forEach(term => {
        this.addToIndex(term, p.id);
      });

      // Index by status
      this.addToIndex(p.status, p.id);
    });
  }

  search(query: string): Process[] {
    const terms = query.toLowerCase().split(/\s+/);
    const matches = new Set<string>();

    terms.forEach(term => {
      const ids = this.index.get(term);
      if (ids) {
        ids.forEach(id => matches.add(id));
      }
    });

    return this.processes.filter(p => matches.has(p.id));
  }

  private addToIndex(term: string, id: string) {
    if (!this.index.has(term)) {
      this.index.set(term, new Set());
    }
    this.index.get(term)!.add(id);
  }
}
```

### Filter System
```typescript
// utils/filters.ts
export interface FilterState {
  status: ProcessStatus[];
  cpuThreshold: number | null;
  memoryThreshold: number | null;
  namePattern: string | null;
}

export function applyFilters(processes: Process[], filters: FilterState): Process[] {
  return processes.filter(p => {
    // Status filter
    if (filters.status.length && !filters.status.includes(p.status)) {
      return false;
    }

    // CPU threshold
    if (filters.cpuThreshold !== null && p.cpu < filters.cpuThreshold) {
      return false;
    }

    // Memory threshold
    if (filters.memoryThreshold !== null && p.memory < filters.memoryThreshold) {
      return false;
    }

    // Name pattern
    if (filters.namePattern) {
      const regex = new RegExp(filters.namePattern, 'i');
      if (!regex.test(p.name)) {
        return false;
      }
    }

    return true;
  });
}
```

## PRIORITY 5: Export System

Implement data export with sanitization:

```typescript
// utils/export.ts
export class DataExporter {
  exportToJSON(state: RootState): Blob {
    const exportData = {
      timestamp: new Date().toISOString(),
      processes: this.sanitizeProcesses(state.services.processes),
      metrics: state.metrics.current,
      logs: state.logs.entries.slice(-1000), // Last 1000 lines
      cron: state.cron.jobs
    };

    const json = JSON.stringify(exportData, null, 2);
    return new Blob([json], { type: 'application/json' });
  }

  exportToCSV(processes: Process[]): Blob {
    const headers = ['Name', 'Status', 'PID', 'CPU %', 'Memory MB', 'Uptime'];
    const rows = processes.map(p => [
      p.name,
      p.status,
      p.pid || '',
      p.cpu.toFixed(2),
      (p.memory / 1024 / 1024).toFixed(2),
      p.uptime
    ]);

    const csv = [
      headers.join(','),
      ...rows.map(r => r.map(this.escapeCSV).join(','))
    ].join('\n');

    return new Blob([csv], { type: 'text/csv' });
  }

  private escapeCSV(value: string): string {
    if (value.includes(',') || value.includes('"') || value.includes('\n')) {
      return `"${value.replace(/"/g, '""')}"`;
    }
    return value;
  }

  private sanitizeProcesses(processes: Process[]): Process[] {
    return processes.map(p => ({
      ...p,
      env: this.sanitizeEnv(p.env)
    }));
  }

  private sanitizeEnv(env: Record<string, string>): Record<string, string> {
    const sanitized: Record<string, string> = {};
    const sensitiveKeys = /password|secret|token|key|credential/i;

    for (const [key, value] of Object.entries(env)) {
      if (sensitiveKeys.test(key)) {
        sanitized[key] = '***REDACTED***';
      } else {
        sanitized[key] = value;
      }
    }

    return sanitized;
  }
}
```

## PRIORITY 6: Performance Optimizations

### Memoized Selectors
```typescript
// store/selectors/index.ts
import { createSelector } from '@reduxjs/toolkit';

// Heavy computation memoized
export const selectProcessTreeWithMetrics = createSelector(
  [selectProcessTree, selectMetrics],
  (tree, metrics) => {
    // Expensive tree decoration
    return decorateTreeWithMetrics(tree, metrics);
  }
);

// Filtered logs memoized
export const selectFilteredLogs = createSelector(
  [
    (state: RootState) => state.logs.entries,
    (state: RootState) => state.logs.filters
  ],
  (entries, filters) => {
    return entries.filter(log => {
      if (!filters.level.includes(log.level)) return false;
      if (filters.service && log.service !== filters.service) return false;
      if (filters.searchTerm && !log.message.includes(filters.searchTerm)) return false;
      return true;
    });
  }
);
```

### Web Worker for Heavy Processing
```typescript
// workers/metricsWorker.ts
self.addEventListener('message', (event) => {
  const { type, data } = event.data;

  switch (type) {
    case 'AGGREGATE_METRICS':
      const aggregated = performHeavyAggregation(data);
      self.postMessage({ type: 'METRICS_AGGREGATED', data: aggregated });
      break;

    case 'DOWNSAMPLE_LOGS':
      const downsampled = downsampleLogs(data);
      self.postMessage({ type: 'LOGS_DOWNSAMPLED', data: downsampled });
      break;
  }
});

// Use in hook
export function useMetricsWorker() {
  const worker = useRef<Worker>();

  useEffect(() => {
    worker.current = new Worker('/workers/metricsWorker.js');
    return () => worker.current?.terminate();
  }, []);

  const aggregate = useCallback((data: MetricsData) => {
    return new Promise((resolve) => {
      worker.current!.onmessage = (e) => {
        if (e.data.type === 'METRICS_AGGREGATED') {
          resolve(e.data.data);
        }
      };
      worker.current!.postMessage({ type: 'AGGREGATE_METRICS', data });
    });
  }, []);

  return { aggregate };
}
```

## PRIORITY 7: Error Handling

Implement comprehensive error boundaries:

```typescript
// utils/errors.ts
export class SystemGError extends Error {
  constructor(
    message: string,
    public code: ErrorCode,
    public recoverable: boolean = true
  ) {
    super(message);
  }
}

export enum ErrorCode {
  FILE_ACCESS_DENIED = 'FILE_ACCESS_DENIED',
  INVALID_DIRECTORY = 'INVALID_DIRECTORY',
  PARSE_ERROR = 'PARSE_ERROR',
  NETWORK_ERROR = 'NETWORK_ERROR',
  QUOTA_EXCEEDED = 'QUOTA_EXCEEDED'
}

// Middleware for error handling
export const errorMiddleware: Middleware = (store) => (next) => (action) => {
  try {
    return next(action);
  } catch (error) {
    console.error('Redux error:', error);

    // Dispatch error action
    store.dispatch({
      type: 'error/occurred',
      payload: {
        message: error.message,
        code: error.code || 'UNKNOWN',
        timestamp: Date.now()
      }
    });

    // Re-throw if not recoverable
    if (error instanceof SystemGError && !error.recoverable) {
      throw error;
    }
  }
};
```

## Testing Requirements

### Store Tests
```typescript
// store/__tests__/servicesSlice.test.ts
describe('servicesSlice', () => {
  it('updates processes and builds tree', () => {
    const state = servicesReducer(undefined, updateProcesses(mockProcesses));
    expect(state.processes).toEqual(mockProcesses);
    expect(state.tree).toBeDefined();
  });

  it('maintains selection across updates', () => {
    let state = servicesReducer(undefined, selectProcess('proc-1'));
    state = servicesReducer(state, updateProcesses(mockProcesses));
    expect(state.selectedId).toBe('proc-1');
  });
});
```

### Hook Tests
```typescript
// hooks/__tests__/useSystemGPoller.test.ts
describe('useSystemGPoller', () => {
  it('polls at correct intervals', async () => {
    const { result } = renderHook(() => useSystemGPoller(mockFileApi));

    await act(async () => {
      await jest.advanceTimersByTime(1000);
    });

    expect(mockFileApi.readSnapshot).toHaveBeenCalledTimes(1);
  });

  it('implements exponential backoff on error', async () => {
    mockFileApi.readSnapshot.mockRejectedValue(new Error('Failed'));

    const { result } = renderHook(() => useSystemGPoller(mockFileApi));

    // First retry at 1.5s
    await act(async () => {
      await jest.advanceTimersByTime(1500);
    });

    // Second retry at 2.25s
    await act(async () => {
      await jest.advanceTimersByTime(2250);
    });

    expect(mockFileApi.readSnapshot).toHaveBeenCalledTimes(3);
  });
});
```

## Acceptance Criteria

Your feature layer is complete when:

1. **Redux Store Operational**
   - [ ] All slices implemented
   - [ ] TypeScript types complete
   - [ ] Selectors memoized
   - [ ] Middleware configured

2. **Data Flow Working**
   - [ ] Polling updates store
   - [ ] Sanitization applied
   - [ ] Error states handled
   - [ ] Performance targets met

3. **Search/Filter Functional**
   - [ ] Search returns <100ms
   - [ ] Filters apply correctly
   - [ ] Results update live
   - [ ] URL state synced

4. **Export System Ready**
   - [ ] JSON export works
   - [ ] CSV export works
   - [ ] Data sanitized
   - [ ] Large datasets handled

5. **Performance Optimized**
   - [ ] No memory leaks
   - [ ] 60fps maintained
   - [ ] Worker threads used
   - [ ] Selectors memoized

## Critical Success Factors

Remember:
- **Never** store raw secrets in Redux
- **Always** sanitize before storing
- **Use** selectors for derived data
- **Memoize** expensive computations
- **Test** all edge cases
- **Document** complex logic

Your code is the brain of the application. Make it smart, fast, and reliable.

## Artifact-Backed Delivery Requirements
- Do not mark data/store features complete unless real code lands in `orchestrator-ui/src/store/` or related feature modules.
- For each completion claim, include command evidence:
  - `npm run type-check`
  - `npm run test` (store/features scope or full suite)
- Report-only deliverables are insufficient for implementation tasks.
- Any failed feature task must be followed by a concrete remediation task with code changes and retest evidence.