Update backend/agents/reflection_agent.py

#3
by munals - opened
Files changed (1) hide show
  1. backend/agents/reflection_agent.py +8 -12
backend/agents/reflection_agent.py CHANGED
@@ -51,7 +51,6 @@ class ReflectionAgent:
51
  passing it downstream to OperationsAgent.
52
  """
53
 
54
- # ── Step 1: Observe ──────────────────────────────────────────
55
  self._history.append({
56
  'risk_level': rr.risk_level,
57
  'risk_score': rr.risk_score,
@@ -64,8 +63,6 @@ class ReflectionAgent:
64
  corrected_level = rr.risk_level
65
  corrected_score = rr.risk_score
66
 
67
- # ── Step 2 & 3: Critique + Correct ───────────────────────────
68
-
69
  # Bias 1: Chronic LOW under-reporting
70
  if len(self._history) >= self.BIAS_WINDOW:
71
  all_low = all(h['risk_level'] == 'LOW' for h in self._history)
@@ -73,7 +70,7 @@ class ReflectionAgent:
73
  if all_low and avg_crowd > self.CROWD_LOW_THRESH:
74
  bias_detected = True
75
  corrected_level = 'MEDIUM'
76
- corrected_score = max(rr.risk_score, 0.42)
77
  critique.append(
78
  f'CHRONIC_LOW_BIAS: {self.BIAS_WINDOW} consecutive LOW '
79
  f'with avg crowd={avg_crowd:.0f} persons. Upgraded to MEDIUM.'
@@ -83,7 +80,7 @@ class ReflectionAgent:
83
  if rr.trend == 'rising' and rr.risk_level == 'LOW' and fr.person_count > 20:
84
  bias_detected = True
85
  corrected_level = 'MEDIUM'
86
- corrected_score = max(corrected_score, 0.38)
87
  critique.append(
88
  f'RISING_TREND_IGNORED: trend=rising, persons={fr.person_count}, '
89
  f'but risk=LOW. Upgraded to MEDIUM.'
@@ -91,20 +88,20 @@ class ReflectionAgent:
91
 
92
  # Bias 3: Count-risk mismatch (enhanced with critical threshold)
93
  if fr.person_count >= self.CRITICAL_CROWD_THRESH and rr.risk_level == 'LOW':
94
- # 100+ persons with LOW risk β†’ CRITICAL inconsistency β†’ upgrade to HIGH
95
  bias_detected = True
96
  corrected_level = 'HIGH'
97
- corrected_score = max(corrected_score, 0.68) # Ensure it's clearly HIGH
98
  critique.append(
99
  f'CRITICAL_COUNT_RISK_MISMATCH: {fr.person_count} persons but risk=LOW. '
100
- f'This is a severe inconsistency β€” 100+ persons should never be LOW. '
101
  f'Upgraded to HIGH (corrected_score={corrected_score:.3f}).'
102
  )
103
  elif fr.person_count > self.HIGH_CROWD_THRESH and rr.risk_level == 'LOW':
104
- # 80-99 persons with LOW risk β†’ upgrade to MEDIUM
105
  bias_detected = True
106
  corrected_level = 'MEDIUM'
107
- corrected_score = max(corrected_score, 0.40)
108
  critique.append(
109
  f'COUNT_RISK_MISMATCH: {fr.person_count} persons but risk=LOW. '
110
  f'Thresholds may need recalibration. Upgraded to MEDIUM.'
@@ -114,7 +111,7 @@ class ReflectionAgent:
114
  if rr.risk_level == 'HIGH' and fr.person_count < config.REFLECTION_OVER_EST_THRESH:
115
  bias_detected = True
116
  corrected_level = 'MEDIUM'
117
- corrected_score = min(corrected_score, 0.62)
118
  critique.append(
119
  f'OVER_ESTIMATION: HIGH risk but only {fr.person_count} persons. '
120
  f'Downgraded to MEDIUM β€” possible empty-frame or detection artifact.'
@@ -123,7 +120,6 @@ class ReflectionAgent:
123
  if not critique:
124
  critique.append('OK: assessment consistent, no bias detected.')
125
 
126
- # ── Step 4: Log ──────────────────────────────────────────────
127
  reflection = {
128
  'frame_id': rr.frame_id,
129
  'original_level': rr.risk_level,
 
51
  passing it downstream to OperationsAgent.
52
  """
53
 
 
54
  self._history.append({
55
  'risk_level': rr.risk_level,
56
  'risk_score': rr.risk_score,
 
63
  corrected_level = rr.risk_level
64
  corrected_score = rr.risk_score
65
 
 
 
66
  # Bias 1: Chronic LOW under-reporting
67
  if len(self._history) >= self.BIAS_WINDOW:
68
  all_low = all(h['risk_level'] == 'LOW' for h in self._history)
 
70
  if all_low and avg_crowd > self.CROWD_LOW_THRESH:
71
  bias_detected = True
72
  corrected_level = 'MEDIUM'
73
+ corrected_score = max(rr.risk_score, 0.30) # push into MEDIUM zone (>0.20)
74
  critique.append(
75
  f'CHRONIC_LOW_BIAS: {self.BIAS_WINDOW} consecutive LOW '
76
  f'with avg crowd={avg_crowd:.0f} persons. Upgraded to MEDIUM.'
 
80
  if rr.trend == 'rising' and rr.risk_level == 'LOW' and fr.person_count > 20:
81
  bias_detected = True
82
  corrected_level = 'MEDIUM'
83
+ corrected_score = max(corrected_score, 0.25) # push into MEDIUM zone (>0.20)
84
  critique.append(
85
  f'RISING_TREND_IGNORED: trend=rising, persons={fr.person_count}, '
86
  f'but risk=LOW. Upgraded to MEDIUM.'
 
88
 
89
  # Bias 3: Count-risk mismatch (enhanced with critical threshold)
90
  if fr.person_count >= self.CRITICAL_CROWD_THRESH and rr.risk_level == 'LOW':
91
+ # 70+ persons with LOW risk β†’ CRITICAL inconsistency β†’ upgrade to HIGH
92
  bias_detected = True
93
  corrected_level = 'HIGH'
94
+ corrected_score = max(corrected_score, 0.85) # push into HIGH zone (>0.80)
95
  critique.append(
96
  f'CRITICAL_COUNT_RISK_MISMATCH: {fr.person_count} persons but risk=LOW. '
97
+ f'This is a severe inconsistency. '
98
  f'Upgraded to HIGH (corrected_score={corrected_score:.3f}).'
99
  )
100
  elif fr.person_count > self.HIGH_CROWD_THRESH and rr.risk_level == 'LOW':
101
+ # 50+ persons with LOW risk β†’ upgrade to MEDIUM
102
  bias_detected = True
103
  corrected_level = 'MEDIUM'
104
+ corrected_score = max(corrected_score, 0.30) # push into MEDIUM zone (>0.20)
105
  critique.append(
106
  f'COUNT_RISK_MISMATCH: {fr.person_count} persons but risk=LOW. '
107
  f'Thresholds may need recalibration. Upgraded to MEDIUM.'
 
111
  if rr.risk_level == 'HIGH' and fr.person_count < config.REFLECTION_OVER_EST_THRESH:
112
  bias_detected = True
113
  corrected_level = 'MEDIUM'
114
+ corrected_score = min(corrected_score, 0.50) # pull down to MEDIUM zone
115
  critique.append(
116
  f'OVER_ESTIMATION: HIGH risk but only {fr.person_count} persons. '
117
  f'Downgraded to MEDIUM β€” possible empty-frame or detection artifact.'
 
120
  if not critique:
121
  critique.append('OK: assessment consistent, no bias detected.')
122
 
 
123
  reflection = {
124
  'frame_id': rr.frame_id,
125
  'original_level': rr.risk_level,