-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtruth_engine.js
More file actions
478 lines (408 loc) · 16.1 KB
/
Copy pathtruth_engine.js
File metadata and controls
478 lines (408 loc) · 16.1 KB
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
/**
* ============================================================
* TRUTH ENGINE v0.1
* Human Survival Algorithm — Epistemic Health Module
*
* Purpose: Protect the information ecosystem from manipulation,
* misinformation, and epistemic corruption — while preserving
* genuine diversity of thought and opinion.
*
* Philosophy: Truth is not just factual accuracy.
* It includes intellectual honesty, epistemic humility,
* transparent reasoning, and the absence of manipulation.
* A factually accurate post can still be deeply dishonest
* in how it frames reality.
* ============================================================
*/
// ── EPISTEMIC HEALTH DIMENSIONS ──────────────────────────────
// Truth is multidimensional. We score all of these.
const EPISTEMIC_DIMENSIONS = {
// FACTUAL ACCURACY: Are the claims verifiable?
factual_accuracy: {
weight: 0.25,
description: 'Claims match verifiable reality',
},
// INTELLECTUAL HONESTY: Does it acknowledge uncertainty?
intellectual_honesty: {
weight: 0.20,
description: 'Acknowledges limits of own knowledge',
},
// REASONING QUALITY: Is the logic sound?
reasoning_quality: {
weight: 0.20,
description: 'Conclusions follow from evidence',
},
// SOURCE TRANSPARENCY: Are sources shown?
source_transparency: {
weight: 0.15,
description: 'Sources are cited and checkable',
},
// MANIPULATION ABSENCE: No psychological tricks?
manipulation_absence: {
weight: 0.20,
description: 'No dark patterns or emotional exploitation',
},
};
// ── MANIPULATION TACTICS LIBRARY ─────────────────────────────
// A taxonomy of known manipulation patterns.
// Each has a severity score (0.0-1.0) and detection signals.
const MANIPULATION_TACTICS = {
// COGNITIVE EXPLOITS
false_urgency: {
severity: 0.75,
description: 'Creates artificial time pressure',
signals: ['act now', 'before it\'s too late', 'they don\'t want you to know'],
},
false_consensus: {
severity: 0.70,
description: 'Implies everyone agrees when they do not',
signals: ['everyone knows', 'it\'s obvious that', 'nobody believes'],
},
false_dichotomy: {
severity: 0.65,
description: 'Presents only two options when more exist',
signals: ['either you', 'if you\'re not with us', 'there are only two sides'],
},
slippery_slope: {
severity: 0.60,
description: 'Chains unlikely consequences without evidence',
signals: [], // Detected by reasoning analysis, not keywords
},
strawman: {
severity: 0.65,
description: 'Misrepresents opposing view to attack it',
signals: [], // Detected by argument structure analysis
},
// EMOTIONAL EXPLOITS
fear_amplification: {
severity: 0.85,
description: 'Deliberately inflates perceived threat',
signals: ['dangerous', 'they\'re coming', 'won\'t be safe', 'under attack'],
},
outrage_bait: {
severity: 0.90,
description: 'Designed to trigger anger with no constructive path',
signals: [], // Detected by emotional analysis + resolution absence
},
shame_induction: {
severity: 0.80,
description: 'Uses shame to control behavior',
signals: ['how could you', 'you should be ashamed', 'real people'],
},
envy_engineering: {
severity: 0.75,
description: 'Content designed to make you feel inadequate',
signals: [], // Detected by comparison framing analysis
},
// IDENTITY EXPLOITS
tribal_activation: {
severity: 0.85,
description: 'Activates us-vs-them tribalism',
signals: ['those people', 'they want to destroy', 'our way of life'],
},
identity_threat: {
severity: 0.90,
description: 'Frames content as attack on core identity',
signals: [], // Detected by identity + threat co-occurrence
},
in_group_flattery: {
severity: 0.60,
description: 'Praises in-group to trigger tribal loyalty',
signals: ['real Americans', 'true believers', 'people like us'],
},
// EPISTEMIC EXPLOITS
gish_gallop: {
severity: 0.70,
description: 'Overwhelming with many weak arguments',
signals: [], // Detected by argument density analysis
},
moving_goalposts: {
severity: 0.65,
description: 'Changes standards of evidence mid-argument',
signals: [],
},
firehose_of_falsehood: {
severity: 0.95,
description: 'Rapid, high-volume false claims to overwhelm fact-checking',
signals: [],
},
};
// ── TRUTH ENGINE MAIN ANALYZER ───────────────────────────────
/**
* Main entry point. Analyzes content for epistemic health.
*
* @param {Object} content - Content to analyze
* @param {Object} knowledgeDB - External fact-check database
* @returns {Object} Full truth analysis
*/
function analyzeEpistemicHealth(content, knowledgeDB = {}) {
const factualAnalysis = analyzeFactualAccuracy(content, knowledgeDB);
const honestyAnalysis = analyzeIntellectualHonesty(content);
const reasoningAnalysis = analyzeReasoningQuality(content);
const sourceAnalysis = analyzeSourceTransparency(content);
const manipulationAnalysis = detectManipulationTactics(content);
// Compute composite epistemic health score
const epistemicScore = computeEpistemicScore(
factualAnalysis,
honestyAnalysis,
reasoningAnalysis,
sourceAnalysis,
manipulationAnalysis,
);
return {
epistemicScore, // 0.0 - 1.0
factualAnalysis,
honestyAnalysis,
reasoningAnalysis,
sourceAnalysis,
manipulationAnalysis,
isMisinformation: factualAnalysis.misinformationRisk > 0.7,
isManipulative: manipulationAnalysis.overallManipulationScore > 0.6,
requiresHumanReview: epistemicScore < 0.3,
label: generateEpistemicLabel(epistemicScore),
};
}
// ── FACTUAL ACCURACY ANALYZER ────────────────────────────────
/**
* Checks claims against verified knowledge base.
* In production: connects to fact-checking APIs,
* scientific consensus databases, and verified news sources.
*/
function analyzeFactualAccuracy(content, knowledgeDB) {
const { historicalSignals = {}, claims = [] } = content;
// Pull from pre-computed fact-check results
const factCheckResults = historicalSignals.factCheckResults || [];
let verifiedCount = 0;
let unverifiedCount = 0;
let contradictedCount = 0;
for (const result of factCheckResults) {
if (result.status === 'verified') verifiedCount++;
if (result.status === 'unverified') unverifiedCount++;
if (result.status === 'contradicted') contradictedCount++;
}
const total = factCheckResults.length || 1;
// Contradicted claims carry double penalty
const accuracyScore = Math.max(0, Math.min(1,
(verifiedCount - contradictedCount * 2) / total
));
// Misinformation risk increases with contradicted claims
const misinformationRisk = contradictedCount / total;
return {
accuracyScore: Math.round(accuracyScore * 1000) / 1000,
misinformationRisk: Math.round(misinformationRisk * 1000) / 1000,
verifiedClaims: verifiedCount,
unverifiedClaims: unverifiedCount,
contradictedClaims: contradictedCount,
totalClaims: total,
confidence: factCheckResults.length > 0 ? 'checked' : 'unchecked',
};
}
// ── INTELLECTUAL HONESTY ANALYZER ────────────────────────────
/**
* Evaluates whether content acknowledges its own uncertainty,
* limitations, and the existence of other valid perspectives.
* Certainty without evidence is a red flag.
*/
function analyzeIntellectualHonesty(content) {
const { historicalSignals = {} } = content;
const honestyMarkers = historicalSignals.honestyMarkers || {};
const positiveSignals = [
'acknowledges_uncertainty', // "We don't fully know..."
'cites_opposing_view', // "Some argue that..."
'qualifies_claims', // "This may be..."
'admits_bias', // "I should note I believe..."
'invites_disagreement', // "What do you think?"
'updates_on_evidence', // Shows willingness to change view
];
const negativeSignals = [
'absolute_certainty', // "100% proven" on complex topics
'dismisses_opposing_view', // "Only idiots believe..."
'hides_conflicts_of_interest', // Undisclosed motivations
'selective_evidence', // Ignores contradicting evidence
];
let honestyScore = 0.5; // Start neutral
for (const signal of positiveSignals) {
if (honestyMarkers[signal] > 0.5) honestyScore += 0.08;
}
for (const signal of negativeSignals) {
if (honestyMarkers[signal] > 0.5) honestyScore -= 0.12;
}
return {
honestyScore: Math.max(0, Math.min(1, Math.round(honestyScore * 1000) / 1000)),
positiveSignals: positiveSignals.filter(s => honestyMarkers[s] > 0.5),
negativeSignals: negativeSignals.filter(s => honestyMarkers[s] > 0.5),
};
}
// ── REASONING QUALITY ANALYZER ───────────────────────────────
/**
* Evaluates the logical structure of the content's argument.
* In production: uses a fine-tuned argument mining model.
*/
function analyzeReasoningQuality(content) {
const { historicalSignals = {} } = content;
const reasoningSignals = historicalSignals.reasoningSignals || {};
const qualityIndicators = {
clear_claim: reasoningSignals.clear_claim || 0.5,
supported_by_evidence: reasoningSignals.evidence_present || 0.5,
logical_structure: reasoningSignals.logical_coherence || 0.5,
addresses_counterarg: reasoningSignals.counterargument || 0.3,
proportionate_conclusion: reasoningSignals.proportionality || 0.5,
};
const reasoningScore = Object.values(qualityIndicators)
.reduce((sum, v) => sum + v, 0) / Object.keys(qualityIndicators).length;
return {
reasoningScore: Math.round(reasoningScore * 1000) / 1000,
qualityIndicators,
};
}
// ── SOURCE TRANSPARENCY ANALYZER ─────────────────────────────
/**
* Evaluates whether the content is transparent about its
* sources, author, and potential conflicts of interest.
*/
function analyzeSourceTransparency(content) {
const { historicalSignals = {}, metadata = {} } = content;
const hasSources = (historicalSignals.sourcesCount || 0) > 0;
const sourcesVerifiable = historicalSignals.sourcesVerifiable || false;
const authorKnown = Boolean(metadata.authorId);
const noConflicts = !historicalSignals.conflictsOfInterest;
const dateVisible = Boolean(metadata.publishDate);
const transparencyScore = [
hasSources,
sourcesVerifiable,
authorKnown,
noConflicts,
dateVisible,
].filter(Boolean).length / 5;
return {
transparencyScore: Math.round(transparencyScore * 100) / 100,
hasSources,
sourcesVerifiable,
authorKnown,
noConflicts,
dateVisible,
};
}
// ── MANIPULATION DETECTION ENGINE ────────────────────────────
/**
* Core manipulation detector.
* Scans for known psychological exploitation tactics.
* This is the most important defense in the truth engine.
*/
function detectManipulationTactics(content) {
const { historicalSignals = {}, text = '' } = content;
const detectedTactics = [];
let totalManipulationScore = 0;
for (const [tacticName, tactic] of Object.entries(MANIPULATION_TACTICS)) {
const detected = checkForTactic(text, tactic, historicalSignals, tacticName);
if (detected.present) {
detectedTactics.push({
tactic: tacticName,
severity: tactic.severity,
confidence: detected.confidence,
description: tactic.description,
});
totalManipulationScore += tactic.severity * detected.confidence;
}
}
// Normalize — cap at 1.0
const overallManipulationScore = Math.min(1,
totalManipulationScore / Math.max(1, detectedTactics.length)
);
return {
overallManipulationScore: Math.round(overallManipulationScore * 1000) / 1000,
detectedTactics,
tacticCount: detectedTactics.length,
mostSevereTactic: detectedTactics.sort((a, b) => b.severity - a.severity)[0] || null,
isHighRisk: overallManipulationScore > 0.7,
};
}
/**
* Checks for a specific manipulation tactic.
* In production: each tactic has its own fine-tuned classifier.
*/
function checkForTactic(text, tactic, historicalSignals, tacticName) {
// Check historical ML signal first
const mlSignal = historicalSignals[`tactic_${tacticName}`];
if (mlSignal !== undefined) {
return { present: mlSignal > 0.4, confidence: mlSignal };
}
// Fallback: simple keyword check for tactics with known signals
if (tactic.signals && tactic.signals.length > 0) {
const lowerText = text.toLowerCase();
const matches = tactic.signals.filter(s => lowerText.includes(s));
if (matches.length > 0) {
return { present: true, confidence: Math.min(0.6, matches.length * 0.2) };
}
}
return { present: false, confidence: 0 };
}
// ── COMPOSITE SCORE ──────────────────────────────────────────
function computeEpistemicScore(
factualAnalysis,
honestyAnalysis,
reasoningAnalysis,
sourceAnalysis,
manipulationAnalysis,
) {
let score = 0;
score += factualAnalysis.accuracyScore * EPISTEMIC_DIMENSIONS.factual_accuracy.weight;
score += honestyAnalysis.honestyScore * EPISTEMIC_DIMENSIONS.intellectual_honesty.weight;
score += reasoningAnalysis.reasoningScore * EPISTEMIC_DIMENSIONS.reasoning_quality.weight;
score += sourceAnalysis.transparencyScore * EPISTEMIC_DIMENSIONS.source_transparency.weight;
// Manipulation absence: invert manipulation score
const manipulationAbsence = 1 - manipulationAnalysis.overallManipulationScore;
score += manipulationAbsence * EPISTEMIC_DIMENSIONS.manipulation_absence.weight;
// Hard penalty for confirmed misinformation
if (factualAnalysis.misinformationRisk > 0.7) score *= 0.3;
// Hard penalty for high-risk manipulation
if (manipulationAnalysis.isHighRisk) score *= 0.4;
return Math.max(0, Math.min(1, Math.round(score * 1000) / 1000));
}
// ── EPISTEMIC LABEL GENERATOR ────────────────────────────────
function generateEpistemicLabel(score) {
if (score > 0.85) return 'epistemically healthy';
if (score > 0.70) return 'generally reliable';
if (score > 0.55) return 'mixed signals — verify';
if (score > 0.35) return 'low epistemic quality';
if (score > 0.20) return 'likely misleading';
return 'manipulation detected';
}
// ── COMMUNITY FACT-CHECK INTEGRATOR ──────────────────────────
/**
* Integrates human fact-checker corrections into the system.
* Community notes, professional fact-checkers, and AI signals
* are weighted by trust score of the source.
*/
function integrateCommunityFactCheck(contentId, factCheck) {
const {
checkerTrustScore = 0.5, // 0.0 (untrusted) to 1.0 (expert)
verdict, // 'true' | 'false' | 'misleading' | 'unverified'
explanation,
sources = [],
} = factCheck;
// Weight the correction by checker trust
const weightedVerdict = {
contentId,
verdict,
weight: checkerTrustScore,
explanation,
sources,
timestamp: Date.now(),
};
// High-trust fact-checks (> 0.8) trigger immediate re-scoring
const triggerRescoring = checkerTrustScore > 0.8;
return { weightedVerdict, triggerRescoring };
}
// ── EXPORTS ──────────────────────────────────────────────────
module.exports = {
analyzeEpistemicHealth,
analyzeFactualAccuracy,
analyzeIntellectualHonesty,
analyzeReasoningQuality,
analyzeSourceTransparency,
detectManipulationTactics,
integrateCommunityFactCheck,
MANIPULATION_TACTICS,
EPISTEMIC_DIMENSIONS,
};