Repository navigation
Expand file tree
/
Copy pathCulturalCompatibilityChecker.ts
More file actions
457 lines (405 loc) · 15.4 KB
/
Copy pathCulturalCompatibilityChecker.ts
File metadata and controls
457 lines (405 loc) · 15.4 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
/**
* CulturalCompatibilityChecker — Compile-Time Cultural Profile Validation
*
* Analyzes agent compositions for cultural compatibility before runtime.
* Integrates with TraitComposer and TraitDependencyGraph to detect
* incompatible agent team compositions at compile time.
*
* Checks four dimensions of cultural compatibility:
* 1. **Cooperation Index** — flags agent pairs with cooperation_index delta > threshold
* 2. **Cultural Family** — uses compatibility matrix to detect known conflicts
* 3. **Prompt Dialect** — warns when agents use different communication styles
* 4. **Norm Set** — detects contradictory norms within a composition
*
* Design:
* - Pure data: no runtime/DOM dependencies (safe for Worker threads).
* - Returns structured diagnostics (errors + warnings) for the compiler pipeline.
* - Integrates with TraitDependencyGraph by registering cultural_profile as a trait.
* - Called by TraitComposer during composition validation.
*
* @module CulturalCompatibilityChecker
* @version 1.0.0
*/
import type {
CulturalProfileTrait,
CulturalFamily,
PromptDialect,
CompatibilityRating,
} from '../traits/CultureTraits';
import {
getFamilyCompatibility,
getAllContradictoryNorms,
BUILTIN_NORMS,
} from '../traits/CultureTraits';
// =============================================================================
// TYPES
// =============================================================================
/**
* Severity level for cultural compatibility diagnostics.
* - 'error' — composition is invalid and should not proceed
* - 'warning' — composition may work but has known friction points
* - 'info' — informational observation about the composition
*/
export type DiagnosticSeverity = 'error' | 'warning' | 'info';
/**
* Category of the compatibility issue detected.
*/
export type DiagnosticCategory =
| 'cooperation_mismatch'
| 'family_incompatible'
| 'family_cautious'
| 'dialect_mismatch'
| 'norm_contradiction'
| 'norm_unknown'
| 'cooperation_out_of_range';
/**
* A single compatibility diagnostic emitted by the checker.
*/
export interface CulturalDiagnostic {
/** Unique diagnostic code (e.g., 'CULT001') */
code: string;
/** Severity level */
severity: DiagnosticSeverity;
/** Diagnostic category */
category: DiagnosticCategory;
/** Human-readable diagnostic message */
message: string;
/** The two agents involved (by name/identifier) */
agents: [string, string];
/** Suggestion for resolving the issue */
suggestion?: string;
}
/**
* An agent entry with its name and cultural profile.
* Passed into the checker for validation.
*/
export interface AgentCulturalEntry {
/** Agent name or identifier */
name: string;
/** The agent's cultural_profile trait configuration */
profile: CulturalProfileTrait;
}
/**
* Configuration for the compatibility checker.
*/
export interface CulturalCheckerConfig {
/**
* Maximum cooperation_index delta before flagging an error.
* Default: 0.5 (agents with >50% cooperation difference are incompatible).
*/
cooperationThreshold: number;
/**
* Whether to emit warnings for dialect mismatches.
* Default: true.
*/
checkDialects: boolean;
/**
* Whether to validate that norm_set entries reference known norms.
* Default: true.
*/
validateNormReferences: boolean;
/**
* Whether family 'cautious' ratings produce warnings or are silenced.
* Default: true (produces warnings).
*/
warnOnCautious: boolean;
}
/**
* Complete result of cultural compatibility analysis.
*/
export interface CulturalCompatibilityResult {
/** Whether the composition is valid (no errors) */
compatible: boolean;
/** All diagnostics emitted */
diagnostics: CulturalDiagnostic[];
/** Error-level diagnostics only */
errors: CulturalDiagnostic[];
/** Warning-level diagnostics only */
warnings: CulturalDiagnostic[];
/** Number of agent pairs checked */
pairsChecked: number;
}
// =============================================================================
// DEFAULT CONFIG
// =============================================================================
const DEFAULT_CONFIG: CulturalCheckerConfig = {
cooperationThreshold: 0.5,
checkDialects: true,
validateNormReferences: true,
warnOnCautious: true,
};
// =============================================================================
// DIAGNOSTIC CODES
// =============================================================================
const CODES = {
COOPERATION_OUT_OF_RANGE: 'CULT001',
COOPERATION_MISMATCH: 'CULT002',
FAMILY_INCOMPATIBLE: 'CULT003',
FAMILY_CAUTIOUS: 'CULT004',
DIALECT_MISMATCH: 'CULT005',
NORM_CONTRADICTION: 'CULT006',
NORM_UNKNOWN: 'CULT007',
} as const;
// =============================================================================
// CULTURAL COMPATIBILITY CHECKER
// =============================================================================
export class CulturalCompatibilityChecker {
private config: CulturalCheckerConfig;
constructor(config?: Partial<CulturalCheckerConfig>) {
this.config = { ...DEFAULT_CONFIG, ...config };
}
/**
* Update checker configuration.
*/
setConfig(config: Partial<CulturalCheckerConfig>): void {
this.config = { ...this.config, ...config };
}
/**
* Get current checker configuration.
*/
getConfig(): Readonly<CulturalCheckerConfig> {
return { ...this.config };
}
/**
* Check cultural compatibility of a set of agents in a composition.
*
* Performs pairwise checks across all four dimensions:
* 1. Validates individual profiles (cooperation_index range, norm references)
* 2. Checks cooperation_index deltas between all pairs
* 3. Checks cultural_family compatibility between all pairs
* 4. Checks prompt_dialect compatibility between all pairs
* 5. Checks for contradictory norms across the full composition
*
* @param agents Array of agent entries with their cultural profiles
* @returns Complete compatibility result with diagnostics
*/
check(agents: AgentCulturalEntry[]): CulturalCompatibilityResult {
const diagnostics: CulturalDiagnostic[] = [];
// Phase 1: Validate individual profiles
for (const agent of agents) {
diagnostics.push(...this.validateProfile(agent));
}
// Phase 2: Pairwise compatibility checks
let pairsChecked = 0;
for (let i = 0; i < agents.length; i++) {
for (let j = i + 1; j < agents.length; j++) {
const a = agents[i];
const b = agents[j];
pairsChecked++;
diagnostics.push(...this.checkCooperationIndex(a, b));
diagnostics.push(...this.checkCulturalFamily(a, b));
if (this.config.checkDialects) {
diagnostics.push(...this.checkPromptDialect(a, b));
}
}
}
// Phase 3: Composition-wide norm contradiction check
diagnostics.push(...this.checkNormContradictions(agents));
// Partition results
const errors = diagnostics.filter((d) => d.severity === 'error');
const warnings = diagnostics.filter((d) => d.severity === 'warning');
return {
compatible: errors.length === 0,
diagnostics,
errors,
warnings,
pairsChecked,
};
}
/**
* Quick check — returns true if the composition is compatible (no errors).
* Use `check()` for full diagnostics.
*/
isCompatible(agents: AgentCulturalEntry[]): boolean {
return this.check(agents).compatible;
}
// ===========================================================================
// INDIVIDUAL PROFILE VALIDATION
// ===========================================================================
private validateProfile(agent: AgentCulturalEntry): CulturalDiagnostic[] {
const diagnostics: CulturalDiagnostic[] = [];
const { profile, name } = agent;
// Validate cooperation_index range
if (profile.cooperation_index < 0 || profile.cooperation_index > 1) {
diagnostics.push({
code: CODES.COOPERATION_OUT_OF_RANGE,
severity: 'error',
category: 'cooperation_out_of_range',
message: `Agent "${name}" has cooperation_index ${profile.cooperation_index} which is outside the valid range [0, 1].`,
agents: [name, name],
suggestion: 'Set cooperation_index to a value between 0.0 and 1.0.',
});
}
// Validate norm references (if enabled)
if (this.config.validateNormReferences) {
const builtinIds = new Set(BUILTIN_NORMS.map((n) => n.id));
for (const normId of profile.norm_set) {
if (!builtinIds.has(normId)) {
diagnostics.push({
code: CODES.NORM_UNKNOWN,
severity: 'warning',
category: 'norm_unknown',
message: `Agent "${name}" references unknown norm "${normId}". It may be a custom norm not yet registered.`,
agents: [name, name],
suggestion: `Register the norm "${normId}" via BUILTIN_NORMS or ensure it exists in your custom norm registry.`,
});
}
}
}
return diagnostics;
}
// ===========================================================================
// COOPERATION INDEX CHECK
// ===========================================================================
private checkCooperationIndex(
a: AgentCulturalEntry,
b: AgentCulturalEntry
): CulturalDiagnostic[] {
const delta = Math.abs(a.profile.cooperation_index - b.profile.cooperation_index);
if (delta > this.config.cooperationThreshold) {
return [
{
code: CODES.COOPERATION_MISMATCH,
severity: 'error',
category: 'cooperation_mismatch',
message:
`Agents "${a.name}" (cooperation_index: ${a.profile.cooperation_index}) and ` +
`"${b.name}" (cooperation_index: ${b.profile.cooperation_index}) have a cooperation delta ` +
`of ${delta.toFixed(2)} which exceeds the threshold of ${this.config.cooperationThreshold}. ` +
`These agents are unlikely to cooperate effectively.`,
agents: [a.name, b.name],
suggestion:
'Adjust cooperation_index values to be within ' +
`${this.config.cooperationThreshold} of each other, or add a mediator agent with a middle cooperation_index.`,
},
];
}
return [];
}
// ===========================================================================
// CULTURAL FAMILY CHECK
// ===========================================================================
private checkCulturalFamily(a: AgentCulturalEntry, b: AgentCulturalEntry): CulturalDiagnostic[] {
const { rating, reason } = getFamilyCompatibility(
a.profile.cultural_family,
b.profile.cultural_family
);
if (rating === 'incompatible') {
return [
{
code: CODES.FAMILY_INCOMPATIBLE,
severity: 'error',
category: 'family_incompatible',
message:
`Agents "${a.name}" (family: ${a.profile.cultural_family}) and ` +
`"${b.name}" (family: ${b.profile.cultural_family}) belong to incompatible cultural families. ` +
reason,
agents: [a.name, b.name],
suggestion:
`Change one agent's cultural_family to a compatible type, or separate them ` +
`into different compositions/zones.`,
},
];
}
if (rating === 'cautious' && this.config.warnOnCautious) {
return [
{
code: CODES.FAMILY_CAUTIOUS,
severity: 'warning',
category: 'family_cautious',
message:
`Agents "${a.name}" (family: ${a.profile.cultural_family}) and ` +
`"${b.name}" (family: ${b.profile.cultural_family}) have cautious cultural compatibility. ` +
reason,
agents: [a.name, b.name],
suggestion:
'Consider adding a mediator agent or defining explicit interaction protocols.',
},
];
}
return [];
}
// ===========================================================================
// PROMPT DIALECT CHECK
// ===========================================================================
private checkPromptDialect(a: AgentCulturalEntry, b: AgentCulturalEntry): CulturalDiagnostic[] {
if (a.profile.prompt_dialect !== b.profile.prompt_dialect) {
return [
{
code: CODES.DIALECT_MISMATCH,
severity: 'warning',
category: 'dialect_mismatch',
message:
`Agents "${a.name}" (dialect: ${a.profile.prompt_dialect}) and ` +
`"${b.name}" (dialect: ${b.profile.prompt_dialect}) use different prompt dialects. ` +
`Inter-agent communication may be less efficient.`,
agents: [a.name, b.name],
suggestion:
'Align prompt_dialect values for agents that need frequent communication, ' +
'or add a translator agent that bridges both dialects.',
},
];
}
return [];
}
// ===========================================================================
// NORM CONTRADICTION CHECK
// ===========================================================================
/**
* Check for contradictory norms across the entire composition.
* This is an N-way check (not just pairwise) because norms from
* different agents can contradict each other.
*/
private checkNormContradictions(agents: AgentCulturalEntry[]): CulturalDiagnostic[] {
const diagnostics: CulturalDiagnostic[] = [];
const contradictions = getAllContradictoryNorms();
// Build a map: normId -> list of agent names that subscribe to it
const normAgentMap = new Map<string, string[]>();
for (const agent of agents) {
for (const normId of agent.profile.norm_set) {
if (!normAgentMap.has(normId)) {
normAgentMap.set(normId, []);
}
normAgentMap.get(normId)!.push(agent.name);
}
}
// Check each contradictory pair
for (const { normA, normB, reason } of contradictions) {
const agentsWithA = normAgentMap.get(normA) || [];
const agentsWithB = normAgentMap.get(normB) || [];
if (agentsWithA.length > 0 && agentsWithB.length > 0) {
// Find the specific agents that create the contradiction
for (const agentA of agentsWithA) {
for (const agentB of agentsWithB) {
if (agentA !== agentB) {
diagnostics.push({
code: CODES.NORM_CONTRADICTION,
severity: 'error',
category: 'norm_contradiction',
message:
`Norm contradiction: agent "${agentA}" subscribes to "${normA}" ` +
`and agent "${agentB}" subscribes to "${normB}". ${reason}`,
agents: [agentA, agentB],
suggestion:
`Remove one of the contradictory norms, or separate agents ` +
`"${agentA}" and "${agentB}" into different compositions.`,
});
}
// Also check if the same agent subscribes to both contradictory norms
if (agentA === agentB) {
diagnostics.push({
code: CODES.NORM_CONTRADICTION,
severity: 'error',
category: 'norm_contradiction',
message: `Agent "${agentA}" subscribes to contradictory norms "${normA}" and "${normB}". ${reason}`,
agents: [agentA, agentA],
suggestion: `Remove one of the contradictory norms from agent "${agentA}".`,
});
}
}
}
}
}
return diagnostics;
}
}