Barazo AppView backend
barazo.forum
1import type { Logger } from '../lib/logger.js'
2import type { TrustGraphService, TrustComputationResult } from '../services/trust-graph.js'
3import type { SybilDetectorService, DetectionResult } from '../services/sybil-detector.js'
4import type {
5 BehavioralHeuristicsService,
6 BehavioralFlag,
7} from '../services/behavioral-heuristics.js'
8
9// ---------------------------------------------------------------------------
10// Types
11// ---------------------------------------------------------------------------
12
13export interface JobResult {
14 trustComputation: TrustComputationResult
15 behavioralFlags: BehavioralFlag[]
16 sybilDetection: DetectionResult
17 durationMs: number
18}
19
20export type JobState = 'idle' | 'running' | 'completed' | 'failed'
21
22export interface JobStatus {
23 state: JobState
24 lastComputedAt: Date | null
25 lastDurationMs: number | null
26 lastError: string | null
27}
28
29export interface TrustGraphJob {
30 run(communityId: string | null): Promise<JobResult>
31 getStatus(): JobStatus
32}
33
34// ---------------------------------------------------------------------------
35// Factory
36// ---------------------------------------------------------------------------
37
38export function createTrustGraphJob(
39 trustGraphService: TrustGraphService,
40 sybilDetectorService: SybilDetectorService,
41 behavioralHeuristicsService: BehavioralHeuristicsService,
42 logger: Logger
43): TrustGraphJob {
44 let state: JobState = 'idle'
45 let lastComputedAt: Date | null = null
46 let lastDurationMs: number | null = null
47 let lastError: string | null = null
48
49 async function run(communityId: string | null): Promise<JobResult> {
50 const start = Date.now()
51 state = 'running'
52
53 logger.info({ communityId }, 'Starting trust graph computation job')
54
55 try {
56 // Step 1: Compute trust scores (EigenTrust)
57 const trustComputation = await trustGraphService.computeTrustScores(communityId)
58
59 logger.info(
60 {
61 communityId,
62 nodes: trustComputation.totalNodes,
63 edges: trustComputation.totalEdges,
64 converged: trustComputation.converged,
65 iterations: trustComputation.iterations,
66 },
67 'Trust computation phase completed'
68 )
69
70 // Step 2: Run behavioral heuristics
71 const behavioralFlags = await behavioralHeuristicsService.runAll(communityId)
72
73 logger.info(
74 {
75 communityId,
76 flagsDetected: behavioralFlags.length,
77 },
78 'Behavioral heuristics phase completed'
79 )
80
81 // Step 3: Detect sybil clusters
82 const sybilDetection = await sybilDetectorService.detectClusters(communityId)
83
84 logger.info(
85 {
86 communityId,
87 clustersDetected: sybilDetection.clustersDetected,
88 lowTrustDids: sybilDetection.totalLowTrustDids,
89 },
90 'Sybil detection phase completed'
91 )
92
93 const durationMs = Date.now() - start
94 state = 'completed'
95 lastComputedAt = new Date()
96 lastDurationMs = durationMs
97 lastError = null
98
99 logger.info({ communityId, durationMs }, 'Trust graph computation job completed')
100
101 return { trustComputation, behavioralFlags, sybilDetection, durationMs }
102 } catch (err) {
103 state = 'failed'
104 const errorMessage = err instanceof Error ? err.message : 'Unknown error'
105 lastError = errorMessage
106
107 logger.error({ communityId, err }, 'Trust graph computation job failed')
108
109 throw err
110 }
111 }
112
113 function getStatus(): JobStatus {
114 return {
115 state,
116 lastComputedAt,
117 lastDurationMs,
118 lastError,
119 }
120 }
121
122 return { run, getStatus }
123}