forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrecommendations.py
More file actions
1216 lines (1118 loc) · 47.8 KB
/
Copy pathrecommendations.py
File metadata and controls
1216 lines (1118 loc) · 47.8 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
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
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""Facts → filters → one judgment → trace for What Matters Now."""
import hashlib
import json
from collections import defaultdict, deque
from dataclasses import dataclass
from datetime import datetime, timedelta, timezone
from typing import Any, Optional, Protocol
from pydantic import BaseModel, ConfigDict, Field
import database.candidates as candidates_db
import database.task_recommendations as recommendation_db
import database.workstreams as workstreams_db
from models.action_item import EvidenceKind, EvidenceRef, EvidenceScope, TaskChangePayload, TaskOwner, TaskStatus
from models.candidate import (
CandidateAction,
CandidateCreate,
CandidateStatus,
CandidateSubjectKind,
TaskCompleteCandidate,
)
from models.goal import GoalStatus
from models.task_intelligence import TaskIntelligenceFeedbackAction, TaskIntelligenceFeedbackReason
from models.task_recommendation import (
ContextMatchSignal,
DecisionDebugProjection,
DecisionRecord,
DeterministicFacts,
EvaluationRequest,
FeedbackCreate,
FeedbackRecord,
FeedbackSubjectKind,
InterventionCreate,
InterventionRecord,
NormalizedContextSnapshot,
OpenLoopSnapshot,
OpenLoopStatus,
OutcomeCreate,
OutcomeRecord,
Recommendation,
RecommendationSubjectKind,
ShortlistEligibility,
SnapshotReceipt,
WhatMattersNowProjection,
)
from utils.metrics import TASK_INTELLIGENCE_ATTRIBUTION_TOTAL
from utils.task_intelligence.capture_policy import MINIMUM_CAPTURE_CONFIDENCE
MAX_SHORTLIST_SIZE = 20
MAX_RECOMMENDATIONS = 3
ATTENTION_TIER_RESERVED_CAPACITY = {0: 10, 1: 8, 2: 2}
PROJECTION_TTL = timedelta(minutes=30)
MAX_LOCAL_SNAPSHOT_TTL = timedelta(hours=1)
DEFAULT_LATER_TTL = timedelta(days=1)
DISMISS_TTL = timedelta(days=30)
PROMPT_VERSION = 'what-matters-now.v2'
POLICY_VERSION = 'ranking.v2'
FACT_DEFINITION_VERSION = 'facts.v2'
class SnapshotValidationError(ValueError):
pass
class JudgmentSelection(BaseModel):
model_config = ConfigDict(extra='forbid')
subject_kind: RecommendationSubjectKind
subject_id: str = Field(min_length=1, max_length=128)
why_now: str = Field(min_length=1, max_length=1024)
recommended_action: str = Field(min_length=1, max_length=128)
alternative_action: Optional[str] = Field(default=None, max_length=128)
@dataclass(frozen=True)
class EvaluationSubject:
kind: RecommendationSubjectKind
subject_id: str
feedback_subject_kind: FeedbackSubjectKind
feedback_subject_id: str
destination_task_id: Optional[str]
destination_workstream_id: Optional[str]
headline: str
label: Optional[str]
evidence_preview: str
evidence_refs: tuple[EvidenceRef, ...]
facts: DeterministicFacts
eligibility: ShortlistEligibility
material_token: str
explicit_user_intent: bool = False
class RecommendationJudgment(Protocol):
model_version: str
def judge(self, subjects: list[EvaluationSubject]) -> list[JudgmentSelection]: ...
def _stable_id(prefix: str, *parts: object) -> str:
encoded = '\x1f'.join(str(part) for part in parts).encode('utf-8')
return f'{prefix}_{hashlib.sha256(encoded).hexdigest()[:32]}'
def _recommendation_dedupe_key(subject: EvaluationSubject) -> str:
# Suggested and What Matters Now are two presentation surfaces for the same
# pending Candidate. Keep one bounded key so feedback on either surface
# suppresses the equivalent intervention everywhere.
if subject.kind == RecommendationSubjectKind.candidate:
return candidate_recommendation_dedupe_key(subject.subject_id)
return _stable_id('recommendation', subject.kind.value, subject.subject_id, subject.material_token)
def candidate_recommendation_dedupe_key(candidate_id: str) -> str:
"""Return the cross-surface Candidate attention identity."""
return _stable_id('candidate', candidate_id)
def _as_aware(value: Any) -> Optional[datetime]:
if not isinstance(value, datetime):
return None
return value if value.tzinfo is not None else value.replace(tzinfo=timezone.utc)
def _iso_token(value: Any) -> str:
timestamp = _as_aware(value)
return timestamp.isoformat() if timestamp is not None else str(value or '')
def _valid_evidence(raw: Any, *, device_id: Optional[str] = None) -> tuple[EvidenceRef, ...]:
if not isinstance(raw, list):
return ()
records: list[EvidenceRef] = []
for item in raw[:50]:
try:
evidence = EvidenceRef.model_validate(item)
except (TypeError, ValueError):
continue
if evidence.scope == EvidenceScope.device_local and evidence.device_id != device_id:
continue
records.append(evidence)
return tuple(records)
def _context_signals(
kind: RecommendationSubjectKind,
subject_id: str,
snapshot: Optional[NormalizedContextSnapshot],
) -> list[ContextMatchSignal]:
if snapshot is None:
return []
signals: set[ContextMatchSignal] = set()
for match in snapshot.matches:
if match.subject_kind == kind and match.subject_id == subject_id:
signals.update(match.signals)
return sorted(signals, key=lambda signal: signal.value)
def _days_to_due(due_at: Any, now: datetime) -> Optional[float]:
due = _as_aware(due_at)
if due is None:
return None
# Whole-day fact buckets avoid turning clock drift into material state churn.
seconds = (due - now).total_seconds()
return float(int(seconds // 86400) if seconds >= 0 else -int((-seconds) // 86400))
def _stored_confidence(value: Any) -> float:
"""Coerce a stored confidence to a float, treating anything malformed as 0.0.
Mirrors ``database.candidates._stored_confidence``, which already reads these very
fields off ``action_items`` documents. ``dict.get(key, default)`` returns ``None``
when the key is present with a null value, so the default never fires and a bare
``float(...)`` raises TypeError; booleans must not be promoted to 1.0/0.0 either.
"""
if isinstance(value, (int, float)) and not isinstance(value, bool):
return float(value)
return 0.0
def _recent(updated_at: Any, now: datetime) -> bool:
updated = _as_aware(updated_at)
return updated is not None and now - updated <= timedelta(days=7)
def _eligibility(
*,
is_open: bool,
unexpired: bool,
facts: DeterministicFacts,
recent_material_activity: bool,
has_evidence: bool = True,
quality_eligible: bool = True,
) -> ShortlistEligibility:
passes = (
is_open
and unexpired
and facts.capture_confidence >= MINIMUM_CAPTURE_CONFIDENCE
and facts.has_concrete_next_action
and has_evidence
and quality_eligible
)
return ShortlistEligibility(
open=is_open,
unexpired=unexpired,
passes_recommendation_gates=passes,
recent_material_activity=recent_material_activity,
inside_due_window=facts.days_to_due is not None and -1 <= facts.days_to_due <= 7,
)
def _canonical_evidence_preview(facts: DeterministicFacts, evidence: tuple[EvidenceRef, ...]) -> str:
if facts.days_to_due is not None:
if facts.days_to_due < 0:
return 'Due date has passed.'
if facts.days_to_due <= 7:
days = max(0, round(facts.days_to_due))
return f'Due in {days} day' + ('' if days == 1 else 's') + '.'
if facts.context_match_signals:
return 'Relevant context is active: ' + ', '.join(signal.value for signal in facts.context_match_signals) + '.'
if facts.someone_blocked:
return 'Progress is waiting on input.'
if evidence:
return f'Linked to {len(evidence)} evidence source' + ('' if len(evidence) == 1 else 's') + '.'
return 'Canonical state has a concrete next action.'
def _subject(
*,
kind: RecommendationSubjectKind,
subject_id: str,
feedback_subject_kind: Optional[FeedbackSubjectKind] = None,
feedback_subject_id: Optional[str] = None,
destination_task_id: Optional[str] = None,
destination_workstream_id: Optional[str] = None,
headline: str,
label: Optional[str],
evidence: tuple[EvidenceRef, ...],
facts: DeterministicFacts,
is_open: bool,
unexpired: bool,
recent_material_activity: bool,
material_token: str,
quality_eligible: bool = True,
evidence_preview: Optional[str] = None,
explicit_user_intent: bool = False,
) -> EvaluationSubject:
eligibility = _eligibility(
is_open=is_open,
unexpired=unexpired,
facts=facts,
recent_material_activity=recent_material_activity,
has_evidence=bool(evidence),
quality_eligible=quality_eligible,
)
resolved_feedback_kind = feedback_subject_kind or FeedbackSubjectKind(kind.value)
return EvaluationSubject(
kind=kind,
subject_id=subject_id,
feedback_subject_kind=resolved_feedback_kind,
feedback_subject_id=feedback_subject_id or subject_id,
destination_task_id=destination_task_id,
destination_workstream_id=destination_workstream_id,
headline=headline[:256] or 'Untitled work',
label=label[:256] if label else None,
evidence_preview=evidence_preview or _canonical_evidence_preview(facts, evidence),
evidence_refs=evidence,
facts=facts,
eligibility=eligibility,
material_token=material_token,
explicit_user_intent=explicit_user_intent,
)
def valid_evidence(raw: Any, *, device_id: Optional[str] = None) -> tuple[EvidenceRef, ...]:
"""Public evidence normalization for fixture and live-eval harnesses."""
return _valid_evidence(raw, device_id=device_id)
def build_evaluation_subject(
*,
kind: RecommendationSubjectKind,
subject_id: str,
feedback_subject_kind: Optional[FeedbackSubjectKind] = None,
feedback_subject_id: Optional[str] = None,
destination_task_id: Optional[str] = None,
destination_workstream_id: Optional[str] = None,
headline: str,
label: Optional[str],
evidence: tuple[EvidenceRef, ...],
facts: DeterministicFacts,
is_open: bool,
unexpired: bool,
recent_material_activity: bool,
material_token: str,
quality_eligible: bool = True,
evidence_preview: Optional[str] = None,
explicit_user_intent: bool = False,
) -> EvaluationSubject:
"""Public EvaluationSubject builder for fixture and live-eval harnesses."""
return _subject(
kind=kind,
subject_id=subject_id,
feedback_subject_kind=feedback_subject_kind,
feedback_subject_id=feedback_subject_id,
destination_task_id=destination_task_id,
destination_workstream_id=destination_workstream_id,
headline=headline,
label=label,
evidence=evidence,
facts=facts,
is_open=is_open,
unexpired=unexpired,
recent_material_activity=recent_material_activity,
material_token=material_token,
quality_eligible=quality_eligible,
evidence_preview=evidence_preview,
explicit_user_intent=explicit_user_intent,
)
def _build_subjects(
state: dict[str, list[dict[str, Any]]],
*,
context: Optional[NormalizedContextSnapshot],
open_loop_snapshots: list[OpenLoopSnapshot],
now: datetime,
) -> list[EvaluationSubject]:
goals = {str(goal.get('goal_id') or goal.get('id')): goal for goal in state['goals']}
focused_goal_ids = {
goal_id
for goal_id, goal in goals.items()
if goal.get('status') == GoalStatus.focused.value and goal.get('is_active', True)
}
workstreams = {str(record.get('workstream_id') or record.get('id')): record for record in state['workstreams']}
subjects: list[EvaluationSubject] = []
for task in state['tasks']:
subject_id = str(task.get('task_id') or task.get('id') or '')
if not subject_id:
continue
kind = RecommendationSubjectKind.task
goal_id = str(task.get('goal_id') or '')
workstream_id = str(task.get('workstream_id') or '')
workstream = workstreams.get(workstream_id)
label = str((workstream or {}).get('title') or goals.get(goal_id, {}).get('title') or '') or None
status = str(
task.get('status') or (TaskStatus.completed.value if task.get('completed') else TaskStatus.active.value)
)
signals = _context_signals(kind, subject_id, context)
raw_owner = task.get('owner')
owner = raw_owner.value if isinstance(raw_owner, TaskOwner) else str(raw_owner or '')
trusted_manual_task = str(task.get('source') or '') == 'manual' and owner == TaskOwner.user.value
capture_confidence = 1.0 if trusted_manual_task else _stored_confidence(task.get('capture_confidence'))
facts = DeterministicFacts(
days_to_due=_days_to_due(task.get('due_at'), now),
someone_blocked=False,
has_concrete_next_action=bool(str(task.get('description') or '').strip()),
focused_goal_linked=goal_id in focused_goal_ids,
context_match_signals=signals,
capture_confidence=capture_confidence,
)
evidence = _valid_evidence(task.get('provenance'), device_id=context.device_id if context else None)
if trusted_manual_task and not evidence:
evidence = (EvidenceRef(kind=EvidenceKind.external, id=subject_id, scope=EvidenceScope.canonical),)
recent_material_activity = _recent(
task.get('created_at') if trusted_manual_task else task.get('updated_at') or task.get('created_at'),
now,
)
subjects.append(
_subject(
kind=kind,
subject_id=subject_id,
destination_task_id=subject_id,
destination_workstream_id=workstream_id or None,
headline=str(task.get('description') or ''),
label=label,
evidence=evidence,
facts=facts,
is_open=status == TaskStatus.active.value and not task.get('deleted', False),
unexpired=True,
recent_material_activity=recent_material_activity,
material_token=':'.join((status, _iso_token(task.get('updated_at')), _iso_token(task.get('due_at')))),
evidence_preview='Created directly by you.' if trusted_manual_task else None,
explicit_user_intent=trusted_manual_task,
)
)
for candidate in state['candidates']:
subject_id = str(candidate.get('candidate_id') or candidate.get('id') or '')
if not subject_id:
continue
subject_kind = str(candidate.get('subject_kind') or CandidateSubjectKind.task.value)
proposed_action = str(candidate.get('proposed_action') or CandidateAction.create.value)
if subject_kind == CandidateSubjectKind.task.value and proposed_action != CandidateAction.create.value:
# Task mutations have no Suggested renderer yet; emitting one here creates a dead-end WMN card.
continue
kind = RecommendationSubjectKind.candidate
raw_task_change = candidate.get('task_change')
task_change: dict[str, Any] = raw_task_change if isinstance(raw_task_change, dict) else {}
raw_proposal = candidate.get('workstream_proposal')
proposal: dict[str, Any] = raw_proposal if isinstance(raw_proposal, dict) else {}
headline = str(task_change.get('description') or proposal.get('title') or 'Review suggested work')
goal_id = str(candidate.get('goal_id') or '')
confidence = _stored_confidence(candidate.get('capture_confidence'))
ownership_confidence = _stored_confidence(candidate.get('ownership_confidence'))
facts = DeterministicFacts(
days_to_due=_days_to_due(task_change.get('due_at'), now),
has_concrete_next_action=bool(headline.strip()),
focused_goal_linked=goal_id in focused_goal_ids,
context_match_signals=_context_signals(kind, subject_id, context),
capture_confidence=confidence,
)
evidence = _valid_evidence(candidate.get('evidence_refs'), device_id=context.device_id if context else None)
candidate_status = str(candidate.get('status') or CandidateStatus.pending.value)
# A suggestion the user did not act on expires; the Suggested surface stops
# showing it. Recommending it here would resurrect it on a second surface --
# the same dead end the task-mutation skip above avoids.
unexpired = not candidates_db.stored_candidate_has_lapsed(candidate, now=now)
subjects.append(
_subject(
kind=kind,
subject_id=subject_id,
destination_task_id=str(candidate.get('task_id') or '') or None,
destination_workstream_id=str(candidate.get('workstream_id') or '') or None,
headline=headline,
label=str(goals.get(goal_id, {}).get('title') or '') or None,
evidence=evidence,
facts=facts,
is_open=candidate_status == CandidateStatus.pending.value,
unexpired=unexpired,
recent_material_activity=_recent(candidate.get('created_at'), now),
material_token=':'.join((candidate_status, _iso_token(candidate.get('created_at')))),
quality_eligible=ownership_confidence >= MINIMUM_CAPTURE_CONFIDENCE,
)
)
for workstream_id, workstream in workstreams.items():
if not workstream_id:
continue
kind = RecommendationSubjectKind.workstream
goal_id = str(workstream.get('goal_id') or '')
headline = str(workstream.get('title') or workstream.get('objective') or '')
signals = _context_signals(kind, workstream_id, context)
days_to_review = _days_to_due(workstream.get('next_review_at'), now)
event_evidence: list[EvidenceRef] = []
for event in state.get('workstream_events', []):
if str(event.get('workstream_id') or '') != workstream_id:
continue
event_id = str(event.get('event_id') or event.get('id') or '')
if event_id:
event_evidence.append(
EvidenceRef(
kind=EvidenceKind.workstream_event,
id=event_id,
scope=EvidenceScope.canonical,
)
)
event_evidence.extend(_valid_evidence(event.get('evidence_refs')))
if len(event_evidence) >= 50:
break
facts = DeterministicFacts(
days_to_due=days_to_review,
has_concrete_next_action=(days_to_review is not None and days_to_review <= 7) or bool(signals),
focused_goal_linked=goal_id in focused_goal_ids,
context_match_signals=signals,
capture_confidence=1,
)
subjects.append(
_subject(
kind=kind,
subject_id=workstream_id,
destination_workstream_id=workstream_id,
headline=headline,
label=str(goals.get(goal_id, {}).get('title') or '') or None,
evidence=tuple(event_evidence[:50]),
facts=facts,
is_open=workstream.get('status') == 'open',
unexpired=True,
recent_material_activity=_recent(workstream.get('updated_at'), now),
material_token=':'.join(
(
str(workstream.get('status')),
_iso_token(workstream.get('updated_at')),
_iso_token(workstream.get('next_review_at')),
','.join(evidence.id for evidence in event_evidence[:50]),
)
),
)
)
for artifact in state['artifacts']:
subject_id = str(artifact.get('artifact_id') or artifact.get('id') or '')
if not subject_id:
continue
kind = RecommendationSubjectKind.artifact
workstream_id = str(artifact.get('workstream_id') or '')
workstream = workstreams.get(workstream_id, {})
goal_id = str(workstream.get('goal_id') or '')
status = str(artifact.get('status') or '')
facts = DeterministicFacts(
has_concrete_next_action=status == 'awaiting_review',
focused_goal_linked=goal_id in focused_goal_ids,
context_match_signals=_context_signals(kind, subject_id, context),
capture_confidence=1,
)
evidence = _valid_evidence(artifact.get('evidence_refs'), device_id=context.device_id if context else None)
subjects.append(
_subject(
kind=kind,
subject_id=subject_id,
destination_workstream_id=workstream_id or None,
headline=f"Review {str(artifact.get('kind') or 'artifact').replace('_', ' ')}",
label=str(workstream.get('title') or '') or None,
evidence=evidence,
facts=facts,
is_open=status == 'awaiting_review',
unexpired=True,
recent_material_activity=_recent(artifact.get('created_at'), now),
material_token=':'.join(
(status, str(artifact.get('version') or ''), str(artifact.get('content_hash') or ''))
),
)
)
for snapshot in open_loop_snapshots:
workstream = workstreams.get(snapshot.workstream_id, {})
if not workstream or workstream.get('status') != 'open':
continue
goal_id = str(workstream.get('goal_id') or '')
for loop in snapshot.open_loop_snapshot:
kind = (
RecommendationSubjectKind.decision
if loop.kind.value == 'decision'
else RecommendationSubjectKind.agent_open_loop
)
recommendation_subject_id = loop.subject_id if kind == RecommendationSubjectKind.decision else loop.loop_id
signals = _context_signals(kind, recommendation_subject_id, context)
user_actionable = loop.status in {OpenLoopStatus.open, OpenLoopStatus.blocked, OpenLoopStatus.awaiting_user}
facts = DeterministicFacts(
someone_blocked=loop.status in {OpenLoopStatus.blocked, OpenLoopStatus.awaiting_user},
has_concrete_next_action=user_actionable and bool(loop.next_action_code),
focused_goal_linked=goal_id in focused_goal_ids,
context_match_signals=signals,
capture_confidence=1,
)
evidence = (
EvidenceRef(
kind=EvidenceKind.external,
id=loop.loop_id,
version=snapshot.context_packet_version,
scope=EvidenceScope.device_local,
device_id=snapshot.device_id,
),
)
feedback_kind = {
'task': FeedbackSubjectKind.task,
'artifact': FeedbackSubjectKind.artifact,
'decision': FeedbackSubjectKind.decision,
'approval': FeedbackSubjectKind.artifact,
'external_wait': FeedbackSubjectKind.workstream,
}[loop.kind.value]
feedback_id = snapshot.workstream_id if feedback_kind == FeedbackSubjectKind.workstream else loop.subject_id
subjects.append(
_subject(
kind=kind,
subject_id=recommendation_subject_id,
feedback_subject_kind=feedback_kind,
feedback_subject_id=feedback_id,
destination_task_id=loop.subject_id if loop.kind.value == 'task' else None,
destination_workstream_id=snapshot.workstream_id,
headline=(
'Decision needed'
if kind == RecommendationSubjectKind.decision
else (
'Omi needs your input'
if loop.status == OpenLoopStatus.awaiting_user
else 'Continue agent work'
)
),
label=str(workstream.get('title') or '') or None,
evidence=evidence,
facts=facts,
is_open=user_actionable,
unexpired=snapshot.expires_at > now,
recent_material_activity=_recent(loop.updated_at, now),
material_token=':'.join((loop.status.value, loop.next_action_code, loop.updated_at.isoformat())),
)
)
# Canonical ID order is normalization for cache stability, not a relevance rank.
subjects.sort(key=lambda subject: (subject.kind.value, subject.subject_id))
return subjects
def _attention_tier(subject: EvaluationSubject) -> Optional[int]:
"""Return a deterministic trigger tier, never a relevance score.
Recency only establishes freshness. It cannot, by itself, earn attention.
The holistic judgment remains the sole relevance ordering step.
"""
days_to_due = subject.facts.days_to_due
if subject.facts.someone_blocked or (days_to_due is not None and days_to_due < 0):
return 0
if (days_to_due is not None and 0 <= days_to_due <= 7) or bool(subject.facts.context_match_signals):
return 1
if subject.eligibility.recent_material_activity and (
subject.kind
in {
RecommendationSubjectKind.artifact,
RecommendationSubjectKind.decision,
RecommendationSubjectKind.agent_open_loop,
}
or (
subject.kind == RecommendationSubjectKind.task
and (subject.facts.focused_goal_linked or subject.explicit_user_intent)
)
):
return 2
return None
def _round_robin(groups: dict[str, list[EvaluationSubject]]) -> list[EvaluationSubject]:
queues = {key: deque(values) for key, values in sorted(groups.items()) if values}
result: list[EvaluationSubject] = []
while queues:
for key in list(queues):
queue = queues[key]
result.append(queue.popleft())
if not queue:
del queues[key]
return result
def _balanced_tier(subjects: list[EvaluationSubject]) -> list[EvaluationSubject]:
by_kind_and_workstream: dict[str, dict[str, list[EvaluationSubject]]] = defaultdict(lambda: defaultdict(list))
for subject in sorted(subjects, key=lambda item: (item.kind.value, item.subject_id)):
workstream_bucket = subject.destination_workstream_id or 'unlinked'
by_kind_and_workstream[subject.kind.value][workstream_bucket].append(subject)
by_kind = {
kind: _round_robin(dict(workstream_groups)) for kind, workstream_groups in by_kind_and_workstream.items()
}
return _round_robin(by_kind)
def filter_shortlist(subjects: list[EvaluationSubject], suppressed_dedupe_keys: set[str]) -> list[EvaluationSubject]:
"""Apply typed attention gates, then build a stable kind/workstream-balanced recall set."""
by_tier: dict[int, list[EvaluationSubject]] = defaultdict(list)
for subject in subjects:
dedupe_key = _recommendation_dedupe_key(subject)
tier = _attention_tier(subject)
if (
subject.eligibility.passes_recommendation_gates
and tier is not None
and dedupe_key not in suppressed_dedupe_keys
):
by_tier[tier].append(subject)
ordered_by_tier = {tier: _balanced_tier(by_tier[tier]) for tier in sorted(by_tier)}
shortlist: list[EvaluationSubject] = []
consumed: dict[int, int] = {}
# Reserve typed recall across trigger classes so an overdue flood cannot erase
# due-today, active-context, or fresh review loops. Unused capacity is then
# redistributed in urgency order; this is a bounded policy, not a score.
for tier, ordered in ordered_by_tier.items():
reserved = min(len(ordered), ATTENTION_TIER_RESERVED_CAPACITY.get(tier, 0))
shortlist.extend(ordered[:reserved])
consumed[tier] = reserved
for tier, ordered in ordered_by_tier.items():
if len(shortlist) == MAX_SHORTLIST_SIZE:
break
start = consumed[tier]
available = MAX_SHORTLIST_SIZE - len(shortlist)
shortlist.extend(ordered[start : start + available])
return shortlist
def _material_version(
subjects: list[EvaluationSubject],
*,
suppressed_dedupe_keys: set[str],
context: Optional[NormalizedContextSnapshot],
open_loops: list[OpenLoopSnapshot],
model_version: str,
) -> str:
payload = {
'subjects': [
{
'kind': subject.kind.value,
'id': subject.subject_id,
'material': subject.material_token,
'facts': subject.facts.model_dump(mode='json'),
'eligibility': subject.eligibility.model_dump(mode='json'),
'explicit_user_intent': subject.explicit_user_intent,
'headline': subject.headline,
'label': subject.label,
'evidence_preview': subject.evidence_preview,
'evidence_refs': [
evidence.model_dump(mode='json', exclude_none=True) for evidence in subject.evidence_refs
],
}
for subject in subjects
],
'suppressed': sorted(suppressed_dedupe_keys),
'context': (
sorted(
(
match.subject_kind.value,
match.subject_id,
tuple(sorted(signal.value for signal in match.signals)),
)
for match in context.matches
)
if context is not None
else None
),
'open_loops': sorted(
(
snapshot.workstream_id,
snapshot.runtime_id,
snapshot.context_packet_version,
snapshot.checkpoint_ref or '',
tuple(
sorted(
(
loop.loop_id,
loop.kind.value,
loop.subject_id,
loop.status.value,
loop.next_action_code,
loop.blocking_on_id or '',
loop.updated_at.isoformat(),
)
for loop in snapshot.open_loop_snapshot
)
),
)
for snapshot in open_loops
),
'judgment_contract': {
'prompt': PROMPT_VERSION,
'policy': POLICY_VERSION,
'facts': FACT_DEFINITION_VERSION,
'model': model_version,
},
}
return _stable_id('material', json.dumps(payload, sort_keys=True, separators=(',', ':')))
def evaluate(
uid: str,
request: EvaluationRequest,
*,
judgment: RecommendationJudgment,
account_generation: int = 0,
now: Optional[datetime] = None,
firestore_client: Any = None,
) -> WhatMattersNowProjection:
evaluated_at = now or datetime.now(timezone.utc)
device_scope = request.device_id or 'global'
context = (
recommendation_db.get_context_snapshot(
uid,
request.device_id,
now=evaluated_at,
account_generation=account_generation,
firestore_client=firestore_client,
)
if request.device_id is not None
else None
)
open_loops = (
recommendation_db.list_open_loop_snapshots(
uid,
device_id=request.device_id,
now=evaluated_at,
account_generation=account_generation,
firestore_client=firestore_client,
)
if request.device_id is not None
else []
)
state = recommendation_db.load_canonical_product_state(
uid, account_generation=account_generation, firestore_client=firestore_client
)
subjects = _build_subjects(state, context=context, open_loop_snapshots=open_loops, now=evaluated_at)
suppressed = recommendation_db.list_active_override_dedupe_keys(
uid, now=evaluated_at, account_generation=account_generation, firestore_client=firestore_client
)
material_version = _material_version(
subjects,
suppressed_dedupe_keys=suppressed,
context=context,
open_loops=open_loops,
model_version=judgment.model_version,
)
cached = recommendation_db.get_projection(
uid,
device_scope=device_scope,
now=evaluated_at,
include_expired=True,
account_generation=account_generation,
firestore_client=firestore_client,
)
if cached is not None and cached.material_version == material_version:
if cached.expires_at > evaluated_at:
return cached
refreshed_expiry = evaluated_at + PROJECTION_TTL
refreshed = cached.model_copy(
update={
'generated_at': evaluated_at,
'expires_at': refreshed_expiry,
'recommendations': [
item.model_copy(update={'expires_at': refreshed_expiry}) for item in cached.recommendations
],
}
)
prior_decisions = recommendation_db.get_decisions(
uid,
cached.evaluation_id,
device_scope=device_scope,
account_generation=account_generation,
firestore_client=firestore_client,
)
published = recommendation_db.save_projection(
uid,
device_scope=device_scope,
projection=refreshed,
decisions=[decision.model_copy(update={'expires_at': refreshed_expiry}) for decision in prior_decisions],
account_generation=account_generation,
firestore_client=firestore_client,
)
return published
shortlist = filter_shortlist(subjects, suppressed)
raw_selections = judgment.judge(shortlist)
shortlist_by_key = {(subject.kind, subject.subject_id): subject for subject in shortlist}
selected: list[tuple[EvaluationSubject, JudgmentSelection]] = []
selected_keys: set[tuple[RecommendationSubjectKind, str]] = set()
for selection in raw_selections:
selection_key = (selection.subject_kind, selection.subject_id)
subject = shortlist_by_key.get(selection_key)
if subject is None or selection_key in selected_keys:
continue
selected.append((subject, selection))
selected_keys.add(selection_key)
if len(selected) == MAX_RECOMMENDATIONS:
break
expires_at = evaluated_at + PROJECTION_TTL
evaluation_id = _stable_id('evaluation', uid, account_generation, device_scope, material_version)
output_version = _stable_id(
'output', evaluation_id, *((subject.kind.value, subject.subject_id) for subject, _ in selected)
)
recommendations: list[Recommendation] = []
for subject, selection in selected:
dedupe_key = _recommendation_dedupe_key(subject)
intervention_id = _stable_id('intervention', uid, account_generation, output_version, dedupe_key)
recommendations.append(
Recommendation(
intervention_id=intervention_id,
output_version=output_version,
subject_kind=subject.kind,
subject_id=subject.subject_id,
feedback_subject_kind=subject.feedback_subject_kind,
feedback_subject_id=subject.feedback_subject_id,
destination_task_id=subject.destination_task_id,
destination_workstream_id=subject.destination_workstream_id,
headline=subject.headline,
why_now=selection.why_now,
goal_or_workstream_label=subject.label,
recommended_action=selection.recommended_action,
alternative_action=selection.alternative_action,
evidence_preview=subject.evidence_preview,
evidence_refs=list(subject.evidence_refs),
dedupe_key=dedupe_key,
expires_at=expires_at,
)
)
projection = WhatMattersNowProjection(
evaluation_id=evaluation_id,
output_version=output_version,
material_version=material_version,
generated_at=evaluated_at,
expires_at=expires_at,
recommendations=recommendations,
)
shortlist_ids = [_stable_id('subject', subject.kind.value, subject.subject_id) for subject in shortlist]
shortlist_keys = {(subject.kind, subject.subject_id) for subject in shortlist}
def disposition(subject: EvaluationSubject) -> tuple[str, str]:
key = (subject.kind, subject.subject_id)
if key in selected_keys:
return 'Selected by holistic judgment.', 'selected'
if key in shortlist_keys:
return 'Not selected by holistic judgment.', 'not_selected'
if not subject.eligibility.passes_recommendation_gates:
return 'Removed by deterministic recommendation gates.', 'ineligible'
if _attention_tier(subject) is None:
return 'No current attention trigger.', 'no_attention_trigger'
if _recommendation_dedupe_key(subject) in suppressed:
return 'Suppressed by an active attention override.', 'suppressed'
return 'Excluded from the bounded balanced shortlist.', 'shortlist_capacity'
traced_subjects: list[EvaluationSubject] = []
traced_keys: set[tuple[RecommendationSubjectKind, str]] = set()
def append_trace(subject: EvaluationSubject) -> None:
key = (subject.kind, subject.subject_id)
if key in traced_keys or len(traced_subjects) == MAX_SHORTLIST_SIZE:
return
traced_keys.add(key)
traced_subjects.append(subject)
for subject in shortlist:
if (subject.kind, subject.subject_id) in selected_keys:
append_trace(subject)
for reason_code in ('suppressed', 'ineligible', 'no_attention_trigger', 'shortlist_capacity'):
representative = next((subject for subject in subjects if disposition(subject)[1] == reason_code), None)
if representative is not None:
append_trace(representative)
for subject in shortlist:
append_trace(subject)
for subject in subjects:
append_trace(subject)
decisions = [
DecisionRecord(
evaluation_id=evaluation_id,
subject_kind=subject.kind,
subject_id=subject.subject_id,
shortlist_ids=shortlist_ids,
facts_snapshot=subject.facts,
eligibility=subject.eligibility,
prompt_version=PROMPT_VERSION,
policy_version=POLICY_VERSION,
fact_definition_version=FACT_DEFINITION_VERSION,
model_version=judgment.model_version,
decision_summary=disposition(subject)[0],
reason_codes=[disposition(subject)[1]],
evidence_refs=list(subject.evidence_refs),
final_output_ref=output_version,
evaluated_at=evaluated_at,
expires_at=expires_at,
)
for subject in traced_subjects
]
published = recommendation_db.save_projection(
uid,
device_scope=device_scope,
projection=projection,
decisions=decisions,
account_generation=account_generation,
firestore_client=firestore_client,
)
if published != projection:
return published
for recommendation in recommendations:
TASK_INTELLIGENCE_ATTRIBUTION_TOTAL.labels(
event='intervention', subject_kind=recommendation.feedback_subject_kind.value, code='what_matters_now'
).inc()
return projection
def get_debug_projection(
uid: str,
evaluation_id: str,
*,
device_id: Optional[str],
account_generation: int = 0,
now: Optional[datetime] = None,
firestore_client: Any = None,
) -> Optional[DecisionDebugProjection]:
checked_at = now or datetime.now(timezone.utc)
projection = recommendation_db.get_evaluation_projection(
uid,
evaluation_id,
device_scope=device_id or 'global',
now=checked_at,
account_generation=account_generation,
firestore_client=firestore_client,
)
if projection is None:
return None
decisions = recommendation_db.get_decisions(
uid,
evaluation_id,
device_scope=device_id or 'global',
account_generation=account_generation,
firestore_client=firestore_client,
)
return DecisionDebugProjection(projection=projection, decisions=decisions)