infinity-context 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (1053) hide show
  1. infinity_context-0.1.0.dist-info/METADATA +239 -0
  2. infinity_context-0.1.0.dist-info/RECORD +1053 -0
  3. infinity_context-0.1.0.dist-info/WHEEL +5 -0
  4. infinity_context-0.1.0.dist-info/entry_points.txt +9 -0
  5. infinity_context-0.1.0.dist-info/licenses/LICENSE +201 -0
  6. infinity_context-0.1.0.dist-info/top_level.txt +8 -0
  7. infinity_context_adapters/__init__.py +1 -0
  8. infinity_context_adapters/cognee/__init__.py +5 -0
  9. infinity_context_adapters/cognee/adapter.py +364 -0
  10. infinity_context_adapters/embeddings/__init__.py +6 -0
  11. infinity_context_adapters/embeddings/noop_adapter.py +5 -0
  12. infinity_context_adapters/embeddings/openai_adapter.py +104 -0
  13. infinity_context_adapters/extraction/__init__.py +41 -0
  14. infinity_context_adapters/extraction/content.py +828 -0
  15. infinity_context_adapters/extraction/docling_engine.py +255 -0
  16. infinity_context_adapters/extraction/document_normalization.py +434 -0
  17. infinity_context_adapters/extraction/factory.py +69 -0
  18. infinity_context_adapters/extraction/fallback_context.py +137 -0
  19. infinity_context_adapters/extraction/file_detection.py +378 -0
  20. infinity_context_adapters/extraction/image_evidence.py +319 -0
  21. infinity_context_adapters/extraction/media_tools.py +579 -0
  22. infinity_context_adapters/extraction/openai_json.py +356 -0
  23. infinity_context_adapters/extraction/openai_vision.py +722 -0
  24. infinity_context_adapters/extraction/prompts/__init__.py +1 -0
  25. infinity_context_adapters/extraction/prompts/semantic_memory_v1.md +20 -0
  26. infinity_context_adapters/extraction/schemas/__init__.py +1 -0
  27. infinity_context_adapters/extraction/schemas/semantic_memory_v1.json +62 -0
  28. infinity_context_adapters/extraction/transcription/__init__.py +11 -0
  29. infinity_context_adapters/extraction/transcription/openai_adapter.py +400 -0
  30. infinity_context_adapters/extraction/transcription_engine.py +544 -0
  31. infinity_context_adapters/extraction/video_evidence.py +129 -0
  32. infinity_context_adapters/extraction/whisper_engine.py +386 -0
  33. infinity_context_adapters/features/__init__.py +7 -0
  34. infinity_context_adapters/features/context_building/__init__.py +53 -0
  35. infinity_context_adapters/features/context_building/candidate_provider_chain.py +68 -0
  36. infinity_context_adapters/features/context_building/graphiti_candidate_provider.py +44 -0
  37. infinity_context_adapters/features/context_building/in_memory_candidate_provider.py +62 -0
  38. infinity_context_adapters/features/context_building/postgres_candidate_provider.py +44 -0
  39. infinity_context_adapters/features/context_building/qdrant_candidate_provider.py +44 -0
  40. infinity_context_adapters/features/context_building/query_request.py +99 -0
  41. infinity_context_adapters/features/context_building/records.py +113 -0
  42. infinity_context_adapters/features/document_ingestion/__init__.py +74 -0
  43. infinity_context_adapters/features/document_ingestion/composition.py +69 -0
  44. infinity_context_adapters/features/document_ingestion/extraction_adapter.py +114 -0
  45. infinity_context_adapters/features/document_ingestion/in_memory_chunk_index.py +67 -0
  46. infinity_context_adapters/features/document_ingestion/in_memory_document_store.py +176 -0
  47. infinity_context_adapters/features/document_ingestion/postgres_document_store.py +110 -0
  48. infinity_context_adapters/features/document_ingestion/qdrant_chunk_index.py +49 -0
  49. infinity_context_adapters/features/document_ingestion/records.py +88 -0
  50. infinity_context_adapters/features/memory_facts/__init__.py +53 -0
  51. infinity_context_adapters/features/memory_facts/graphiti_fact_projection.py +111 -0
  52. infinity_context_adapters/features/memory_facts/in_memory_fact_store.py +200 -0
  53. infinity_context_adapters/features/memory_facts/postgres_fact_store.py +126 -0
  54. infinity_context_adapters/features/memory_facts/qdrant_fact_projection.py +100 -0
  55. infinity_context_adapters/features/memory_scopes/__init__.py +44 -0
  56. infinity_context_adapters/features/memory_scopes/in_memory_scope_store.py +189 -0
  57. infinity_context_adapters/features/memory_scopes/postgres_scope_store.py +121 -0
  58. infinity_context_adapters/features/memory_scopes/records.py +86 -0
  59. infinity_context_adapters/graphiti/__init__.py +5 -0
  60. infinity_context_adapters/graphiti/adapter.py +613 -0
  61. infinity_context_adapters/graphiti/identity_evidence.py +711 -0
  62. infinity_context_adapters/graphiti/scope_identity.py +31 -0
  63. infinity_context_adapters/local_blob.py +101 -0
  64. infinity_context_adapters/noop/__init__.py +16 -0
  65. infinity_context_adapters/noop/adapters.py +156 -0
  66. infinity_context_adapters/noop/runtime.py +21 -0
  67. infinity_context_adapters/postgres/__init__.py +19 -0
  68. infinity_context_adapters/postgres/asset_mappers.py +348 -0
  69. infinity_context_adapters/postgres/asset_repositories.py +865 -0
  70. infinity_context_adapters/postgres/benchmark_projection_schema.py +241 -0
  71. infinity_context_adapters/postgres/benchmark_run_completion.py +645 -0
  72. infinity_context_adapters/postgres/benchmark_run_repositories.py +908 -0
  73. infinity_context_adapters/postgres/benchmark_writer_fence.py +139 -0
  74. infinity_context_adapters/postgres/canonical_keyword_trigram.py +31 -0
  75. infinity_context_adapters/postgres/canonical_retrieval_batching.py +388 -0
  76. infinity_context_adapters/postgres/fact_repositories.py +766 -0
  77. infinity_context_adapters/postgres/mappers.py +745 -0
  78. infinity_context_adapters/postgres/migrations/0001_core_facts.sql +95 -0
  79. infinity_context_adapters/postgres/migrations/0002_threads_documents_chunks.sql +97 -0
  80. infinity_context_adapters/postgres/migrations/0003_suggestions.sql +24 -0
  81. infinity_context_adapters/postgres/migrations/0004_service_tokens.sql +16 -0
  82. infinity_context_adapters/postgres/migrations/0005_data_classification_lite.sql +8 -0
  83. infinity_context_adapters/postgres/migrations/0006_service_token_usage_metadata.sql +5 -0
  84. infinity_context_adapters/postgres/migrations/0007_service_token_permissions.sql +2 -0
  85. infinity_context_adapters/postgres/migrations/0008_service_token_memory_scope.sql +2 -0
  86. infinity_context_adapters/postgres/migrations/0009_outbox_lifecycle_hardening.sql +15 -0
  87. infinity_context_adapters/postgres/migrations/0010_fact_taxonomy.sql +14 -0
  88. infinity_context_adapters/postgres/migrations/0011_asset_extractions.sql +66 -0
  89. infinity_context_adapters/postgres/migrations/0012_asset_extraction_execution_hardening.sql +20 -0
  90. infinity_context_adapters/postgres/migrations/0012_usage_governance.sql +33 -0
  91. infinity_context_adapters/postgres/migrations/0013_context_link_suggestions.sql +44 -0
  92. infinity_context_adapters/postgres/migrations/0014_memory_anchors.sql +26 -0
  93. infinity_context_adapters/postgres/migrations/0015_users_acl.sql +36 -0
  94. infinity_context_adapters/postgres/migrations/0016_managed_benchmark_runs.sql +27 -0
  95. infinity_context_adapters/postgres/migrations/0017_managed_benchmark_writer_fence.sql +104 -0
  96. infinity_context_adapters/postgres/migrations/0018_benchmark_projection_manifest.sql +51 -0
  97. infinity_context_adapters/postgres/migrations/0019_managed_benchmark_sealed_fence.sql +131 -0
  98. infinity_context_adapters/postgres/migrations/0020_benchmark_cleanup_completion.sql +219 -0
  99. infinity_context_adapters/postgres/migrations/0021_benchmark_unsealed_abort.sql +64 -0
  100. infinity_context_adapters/postgres/migrations/0022_canonical_keyword_trigram.sql +5 -0
  101. infinity_context_adapters/postgres/models.py +878 -0
  102. infinity_context_adapters/postgres/projection_fence.py +80 -0
  103. infinity_context_adapters/postgres/repositories.py +906 -0
  104. infinity_context_adapters/postgres/repository_helpers.py +115 -0
  105. infinity_context_adapters/postgres/scope_repositories.py +540 -0
  106. infinity_context_adapters/postgres/unit_of_work.py +785 -0
  107. infinity_context_adapters/postgres/usage_repositories.py +128 -0
  108. infinity_context_adapters/postgres/user_repositories.py +164 -0
  109. infinity_context_adapters/provider_errors.py +73 -0
  110. infinity_context_adapters/qdrant/__init__.py +5 -0
  111. infinity_context_adapters/qdrant/identity_evidence.py +504 -0
  112. infinity_context_adapters/qdrant/vector_adapter.py +718 -0
  113. infinity_context_adapters/s3_blob.py +242 -0
  114. infinity_context_cli/__init__.py +5 -0
  115. infinity_context_cli/__main__.py +6 -0
  116. infinity_context_cli/cli.py +964 -0
  117. infinity_context_cli/config.py +184 -0
  118. infinity_context_cli/doctor.py +397 -0
  119. infinity_context_cli/local_experience.py +250 -0
  120. infinity_context_cli/mcp_config.py +124 -0
  121. infinity_context_cli/onboarding.py +542 -0
  122. infinity_context_cli/runtime.py +179 -0
  123. infinity_context_contracts/__init__.py +16 -0
  124. infinity_context_contracts/_json.py +32 -0
  125. infinity_context_contracts/capabilities.py +67 -0
  126. infinity_context_contracts/common.py +54 -0
  127. infinity_context_contracts/features/__init__.py +12 -0
  128. infinity_context_contracts/features/context_building.py +248 -0
  129. infinity_context_contracts/features/document_ingestion.py +277 -0
  130. infinity_context_contracts/features/memory_facts.py +236 -0
  131. infinity_context_contracts/features/memory_scopes.py +329 -0
  132. infinity_context_contracts/health.py +23 -0
  133. infinity_context_core/__init__.py +76 -0
  134. infinity_context_core/agent_behavior_contract.py +72 -0
  135. infinity_context_core/application/__init__.py +580 -0
  136. infinity_context_core/application/anchor_creative_work_filters.py +148 -0
  137. infinity_context_core/application/anchor_event_extraction.py +1571 -0
  138. infinity_context_core/application/anchor_extraction.py +1144 -0
  139. infinity_context_core/application/anchor_identity_normalization.py +90 -0
  140. infinity_context_core/application/anchor_relation_projection.py +203 -0
  141. infinity_context_core/application/anchor_temporal_window.py +172 -0
  142. infinity_context_core/application/asset_extraction_mapping.py +236 -0
  143. infinity_context_core/application/asset_upload_policy.py +952 -0
  144. infinity_context_core/application/auto_apply.py +72 -0
  145. infinity_context_core/application/auto_memory.py +527 -0
  146. infinity_context_core/application/capture_policy.py +61 -0
  147. infinity_context_core/application/chunker.py +93 -0
  148. infinity_context_core/application/context_action_role_evidence.py +890 -0
  149. infinity_context_core/application/context_action_role_labels.py +176 -0
  150. infinity_context_core/application/context_action_roles.py +2365 -0
  151. infinity_context_core/application/context_action_subject_policy.py +71 -0
  152. infinity_context_core/application/context_activity_companion.py +120 -0
  153. infinity_context_core/application/context_activity_duration_rerank.py +118 -0
  154. infinity_context_core/application/context_activity_participant_grounding.py +87 -0
  155. infinity_context_core/application/context_aggregation_answer_slots.py +586 -0
  156. infinity_context_core/application/context_aggregation_evidence_text.py +409 -0
  157. infinity_context_core/application/context_aggregation_rerank.py +145 -0
  158. infinity_context_core/application/context_aggregation_subject.py +137 -0
  159. infinity_context_core/application/context_allergy_condition_inference.py +146 -0
  160. infinity_context_core/application/context_anchor_identity.py +157 -0
  161. infinity_context_core/application/context_anchor_intent_ranking.py +84 -0
  162. infinity_context_core/application/context_anchor_relations.py +67 -0
  163. infinity_context_core/application/context_anchors.py +393 -0
  164. infinity_context_core/application/context_answer_evidence_types.py +14 -0
  165. infinity_context_core/application/context_answer_unit_shapes.py +207 -0
  166. infinity_context_core/application/context_artifact_evidence.py +893 -0
  167. infinity_context_core/application/context_attribution_lexicon.py +254 -0
  168. infinity_context_core/application/context_awareness_cause_rerank.py +109 -0
  169. infinity_context_core/application/context_baby_birth_evidence.py +91 -0
  170. infinity_context_core/application/context_birdwatching_schedule_rerank.py +130 -0
  171. infinity_context_core/application/context_bm25_ranking.py +450 -0
  172. infinity_context_core/application/context_book_author_preference_exact_turns.py +110 -0
  173. infinity_context_core/application/context_building_legacy_candidate_adapter.py +530 -0
  174. infinity_context_core/application/context_canonical_fact_retrieval.py +409 -0
  175. infinity_context_core/application/context_causal_reason_rerank.py +145 -0
  176. infinity_context_core/application/context_choice_reason_rerank.py +109 -0
  177. infinity_context_core/application/context_collectors.py +931 -0
  178. infinity_context_core/application/context_commonality_rerank.py +428 -0
  179. infinity_context_core/application/context_community_membership_inference.py +139 -0
  180. infinity_context_core/application/context_competition_count_exact_turns.py +383 -0
  181. infinity_context_core/application/context_conflicting_evidence_rerank.py +282 -0
  182. infinity_context_core/application/context_conversation_counterparty.py +260 -0
  183. infinity_context_core/application/context_count_cardinality.py +164 -0
  184. infinity_context_core/application/context_creative_work_count_exact_turns.py +517 -0
  185. infinity_context_core/application/context_diagnostics.py +55 -0
  186. infinity_context_core/application/context_diagnostics_bundle_keys.py +433 -0
  187. infinity_context_core/application/context_diagnostics_bundle_projection.py +711 -0
  188. infinity_context_core/application/context_diagnostics_item_projection.py +410 -0
  189. infinity_context_core/application/context_diagnostics_primitives.py +131 -0
  190. infinity_context_core/application/context_diagnostics_ranking.py +143 -0
  191. infinity_context_core/application/context_diagnostics_signals.py +209 -0
  192. infinity_context_core/application/context_diagnostics_sources.py +82 -0
  193. infinity_context_core/application/context_dialogue_authority.py +382 -0
  194. infinity_context_core/application/context_distinct_set_evidence.py +994 -0
  195. infinity_context_core/application/context_distinct_set_selection.py +335 -0
  196. infinity_context_core/application/context_domain_rerank_apply.py +310 -0
  197. infinity_context_core/application/context_domain_rerank_signals.py +2391 -0
  198. infinity_context_core/application/context_domain_rerank_types.py +14 -0
  199. infinity_context_core/application/context_english_activity_displacement_answer_support.py +128 -0
  200. infinity_context_core/application/context_english_lifestyle_inference.py +122 -0
  201. infinity_context_core/application/context_english_lifestyle_inference_answer_support.py +291 -0
  202. infinity_context_core/application/context_english_temporal_dates.py +53 -0
  203. infinity_context_core/application/context_evidence_priority.py +262 -0
  204. infinity_context_core/application/context_evidence_reservation_safety.py +208 -0
  205. infinity_context_core/application/context_exercise_performance_rerank.py +71 -0
  206. infinity_context_core/application/context_food_inventory_exact_turns.py +583 -0
  207. infinity_context_core/application/context_frequency_rerank.py +109 -0
  208. infinity_context_core/application/context_future_plan_timing_rerank.py +110 -0
  209. infinity_context_core/application/context_generic_behavior_inference.py +348 -0
  210. infinity_context_core/application/context_geo_residence_aliases.py +149 -0
  211. infinity_context_core/application/context_hydration.py +329 -0
  212. infinity_context_core/application/context_identity_fact_rerank.py +362 -0
  213. infinity_context_core/application/context_identity_rerank.py +76 -0
  214. infinity_context_core/application/context_identity_terms.py +15 -0
  215. infinity_context_core/application/context_inference_evidence.py +484 -0
  216. infinity_context_core/application/context_inference_evidence_terms.py +744 -0
  217. infinity_context_core/application/context_internal_diagnostics.py +61 -0
  218. infinity_context_core/application/context_item_evidence.py +108 -0
  219. infinity_context_core/application/context_item_purchase_evidence.py +49 -0
  220. infinity_context_core/application/context_keyword_aggregation_prefilter.py +63 -0
  221. infinity_context_core/application/context_keyword_ranking.py +178 -0
  222. infinity_context_core/application/context_lexical.py +750 -0
  223. infinity_context_core/application/context_lifecycle_rerank.py +502 -0
  224. infinity_context_core/application/context_link_candidate_policy.py +864 -0
  225. infinity_context_core/application/context_link_expansion.py +727 -0
  226. infinity_context_core/application/context_link_expansion_items.py +441 -0
  227. infinity_context_core/application/context_link_policy.py +451 -0
  228. infinity_context_core/application/context_link_source_enrichment.py +120 -0
  229. infinity_context_core/application/context_media_time.py +368 -0
  230. infinity_context_core/application/context_museum_visit_policy.py +66 -0
  231. infinity_context_core/application/context_named_person_preference.py +313 -0
  232. infinity_context_core/application/context_national_park_inference.py +93 -0
  233. infinity_context_core/application/context_object_attribute_rerank.py +416 -0
  234. infinity_context_core/application/context_object_mismatch.py +63 -0
  235. infinity_context_core/application/context_occupational_role_identity.py +961 -0
  236. infinity_context_core/application/context_ordinal_sequence.py +134 -0
  237. infinity_context_core/application/context_owner_grounding.py +408 -0
  238. infinity_context_core/application/context_packer.py +858 -0
  239. infinity_context_core/application/context_packer_answer_slots.py +733 -0
  240. infinity_context_core/application/context_packer_answer_support.py +3101 -0
  241. infinity_context_core/application/context_packer_answer_support_patterns.py +1267 -0
  242. infinity_context_core/application/context_packer_answer_support_slots.py +1209 -0
  243. infinity_context_core/application/context_packer_answer_support_utils.py +473 -0
  244. infinity_context_core/application/context_packer_diagnostics.py +48 -0
  245. infinity_context_core/application/context_packer_exact_literal_turns.py +697 -0
  246. infinity_context_core/application/context_packer_exact_prepasses.py +758 -0
  247. infinity_context_core/application/context_packer_exact_turn_utils.py +133 -0
  248. infinity_context_core/application/context_packer_identity_policy.py +155 -0
  249. infinity_context_core/application/context_packer_inference_reservation.py +113 -0
  250. infinity_context_core/application/context_packer_inventory_prepasses.py +359 -0
  251. infinity_context_core/application/context_packer_inventory_slots.py +771 -0
  252. infinity_context_core/application/context_packer_marker_coverage.py +409 -0
  253. infinity_context_core/application/context_packer_rendering.py +338 -0
  254. infinity_context_core/application/context_packer_selection.py +487 -0
  255. infinity_context_core/application/context_packer_source_policy.py +123 -0
  256. infinity_context_core/application/context_paired_evidence_requirements.py +78 -0
  257. infinity_context_core/application/context_paired_evidence_roles.py +64 -0
  258. infinity_context_core/application/context_person_activity_exact_turns.py +375 -0
  259. infinity_context_core/application/context_person_aliases.py +49 -0
  260. infinity_context_core/application/context_person_kinship.py +296 -0
  261. infinity_context_core/application/context_person_occupation.py +240 -0
  262. infinity_context_core/application/context_person_relation_inventory.py +755 -0
  263. infinity_context_core/application/context_person_residence.py +398 -0
  264. infinity_context_core/application/context_person_team_membership.py +305 -0
  265. infinity_context_core/application/context_pet_ownership.py +223 -0
  266. infinity_context_core/application/context_polarity_rerank.py +338 -0
  267. infinity_context_core/application/context_policy.py +107 -0
  268. infinity_context_core/application/context_political_inference.py +128 -0
  269. infinity_context_core/application/context_possession_source.py +161 -0
  270. infinity_context_core/application/context_precise_temporal_evidence.py +156 -0
  271. infinity_context_core/application/context_preference_goal_rerank.py +428 -0
  272. infinity_context_core/application/context_preference_inference.py +335 -0
  273. infinity_context_core/application/context_property_interaction_evidence.py +659 -0
  274. infinity_context_core/application/context_provider_retrieval_terms.py +95 -0
  275. infinity_context_core/application/context_quality.py +488 -0
  276. infinity_context_core/application/context_quantity_evidence_patterns.py +254 -0
  277. infinity_context_core/application/context_quantity_evidence_slots.py +750 -0
  278. infinity_context_core/application/context_quantity_pending_clothing.py +200 -0
  279. infinity_context_core/application/context_query_artifact_inventory_expansions.py +76 -0
  280. infinity_context_core/application/context_query_decomposition.py +879 -0
  281. infinity_context_core/application/context_query_decomposition_answer_policy.py +707 -0
  282. infinity_context_core/application/context_query_decomposition_contracts.py +28 -0
  283. infinity_context_core/application/context_query_decomposition_event_policy.py +535 -0
  284. infinity_context_core/application/context_query_decomposition_inventory_policy.py +767 -0
  285. infinity_context_core/application/context_query_decomposition_shared.py +300 -0
  286. infinity_context_core/application/context_query_duration.py +171 -0
  287. infinity_context_core/application/context_query_entity_relation_expansions.py +212 -0
  288. infinity_context_core/application/context_query_event_summary_expansions.py +80 -0
  289. infinity_context_core/application/context_query_expansion.py +200 -0
  290. infinity_context_core/application/context_query_expansion_rule_catalog_part1.py +1279 -0
  291. infinity_context_core/application/context_query_expansion_rule_catalog_part2.py +770 -0
  292. infinity_context_core/application/context_query_expansion_rule_catalog_part3.py +982 -0
  293. infinity_context_core/application/context_query_expansion_rule_catalog_part4.py +1334 -0
  294. infinity_context_core/application/context_query_expansion_rule_catalog_part5.py +517 -0
  295. infinity_context_core/application/context_query_expansion_rule_terms.py +269 -0
  296. infinity_context_core/application/context_query_expansion_rules.py +66 -0
  297. infinity_context_core/application/context_query_expansion_selection.py +892 -0
  298. infinity_context_core/application/context_query_financial_resources_inference.py +92 -0
  299. infinity_context_core/application/context_query_food_expansions.py +89 -0
  300. infinity_context_core/application/context_query_frequency.py +194 -0
  301. infinity_context_core/application/context_query_game_expansions.py +141 -0
  302. infinity_context_core/application/context_query_health_lifestyle_expansions.py +178 -0
  303. infinity_context_core/application/context_query_identity_terms.py +81 -0
  304. infinity_context_core/application/context_query_intent.py +33 -0
  305. infinity_context_core/application/context_query_intent_common.py +29 -0
  306. infinity_context_core/application/context_query_intent_contracts.py +92 -0
  307. infinity_context_core/application/context_query_intent_extraction.py +614 -0
  308. infinity_context_core/application/context_query_intent_keys.py +499 -0
  309. infinity_context_core/application/context_query_intent_matching.py +407 -0
  310. infinity_context_core/application/context_query_organization_summary_expansions.py +75 -0
  311. infinity_context_core/application/context_query_personal_fact_expansions.py +314 -0
  312. infinity_context_core/application/context_query_pet_activity_expansions.py +329 -0
  313. infinity_context_core/application/context_query_preference_expansions.py +97 -0
  314. infinity_context_core/application/context_query_project_summary_expansions.py +60 -0
  315. infinity_context_core/application/context_query_relevance_ranking.py +76 -0
  316. infinity_context_core/application/context_query_sports_expansions.py +48 -0
  317. infinity_context_core/application/context_query_state_transition.py +53 -0
  318. infinity_context_core/application/context_query_support_role.py +165 -0
  319. infinity_context_core/application/context_query_temporal_expansions.py +18 -0
  320. infinity_context_core/application/context_query_travel_expansions.py +134 -0
  321. infinity_context_core/application/context_query_vehicle_expansions.py +79 -0
  322. infinity_context_core/application/context_query_workflow_expansions.py +233 -0
  323. infinity_context_core/application/context_query_workflow_intent.py +65 -0
  324. infinity_context_core/application/context_quote_scanner.py +342 -0
  325. infinity_context_core/application/context_rank_dedupe.py +171 -0
  326. infinity_context_core/application/context_rank_fusion.py +191 -0
  327. infinity_context_core/application/context_ranked_activity_reservation.py +586 -0
  328. infinity_context_core/application/context_ranked_evidence_coverage_reservation.py +427 -0
  329. infinity_context_core/application/context_ranked_evidence_priority_reservation.py +37 -0
  330. infinity_context_core/application/context_ranked_evidence_selection.py +260 -0
  331. infinity_context_core/application/context_ranking.py +3681 -0
  332. infinity_context_core/application/context_ranking_reason_policy.py +31 -0
  333. infinity_context_core/application/context_ranking_reason_priorities.py +859 -0
  334. infinity_context_core/application/context_ranking_reason_scores.py +519 -0
  335. infinity_context_core/application/context_recommendation_answer_support.py +358 -0
  336. infinity_context_core/application/context_recommendation_exact_turns.py +360 -0
  337. infinity_context_core/application/context_relation_requirement.py +498 -0
  338. infinity_context_core/application/context_relationship_status_evidence.py +83 -0
  339. infinity_context_core/application/context_relative_duration_evidence.py +61 -0
  340. infinity_context_core/application/context_relevance.py +430 -0
  341. infinity_context_core/application/context_reported_obligation_attribution.py +416 -0
  342. infinity_context_core/application/context_reporting_frames.py +338 -0
  343. infinity_context_core/application/context_requirement_coverage.py +767 -0
  344. infinity_context_core/application/context_requirement_guard.py +503 -0
  345. infinity_context_core/application/context_requirement_patterns.py +943 -0
  346. infinity_context_core/application/context_requirement_ranking.py +181 -0
  347. infinity_context_core/application/context_requirement_signals.py +101 -0
  348. infinity_context_core/application/context_rerank_domain_heuristics.py +433 -0
  349. infinity_context_core/application/context_rerank_evidence_signals.py +400 -0
  350. infinity_context_core/application/context_rerank_relevance.py +131 -0
  351. infinity_context_core/application/context_retrieval_performance.py +153 -0
  352. infinity_context_core/application/context_review_items.py +338 -0
  353. infinity_context_core/application/context_score_signal_rerank.py +21 -0
  354. infinity_context_core/application/context_short_story_progress_evidence.py +119 -0
  355. infinity_context_core/application/context_snippets.py +545 -0
  356. infinity_context_core/application/context_social_education_inference.py +265 -0
  357. infinity_context_core/application/context_social_inventory_rerank.py +329 -0
  358. infinity_context_core/application/context_source_family.py +100 -0
  359. infinity_context_core/application/context_source_grounding.py +277 -0
  360. infinity_context_core/application/context_source_sibling_answer_evidence_repair.py +1983 -0
  361. infinity_context_core/application/context_source_sibling_contracts.py +35 -0
  362. infinity_context_core/application/context_source_sibling_evidence.py +405 -0
  363. infinity_context_core/application/context_source_sibling_evidence_rules.py +798 -0
  364. infinity_context_core/application/context_source_sibling_evidence_shared.py +449 -0
  365. infinity_context_core/application/context_source_sibling_identity.py +114 -0
  366. infinity_context_core/application/context_source_sibling_inventory_evidence.py +28 -0
  367. infinity_context_core/application/context_source_sibling_patterns.py +946 -0
  368. infinity_context_core/application/context_source_sibling_place_evidence.py +561 -0
  369. infinity_context_core/application/context_source_sibling_policy.py +988 -0
  370. infinity_context_core/application/context_source_sibling_scoring.py +721 -0
  371. infinity_context_core/application/context_source_siblings.py +363 -0
  372. infinity_context_core/application/context_speaker_attribution.py +574 -0
  373. infinity_context_core/application/context_speaker_disambiguation.py +108 -0
  374. infinity_context_core/application/context_stage_diagnostics.py +111 -0
  375. infinity_context_core/application/context_state_evidence.py +196 -0
  376. infinity_context_core/application/context_state_residence_inference.py +163 -0
  377. infinity_context_core/application/context_state_transition_pairing.py +401 -0
  378. infinity_context_core/application/context_temporal_answer_grounding.py +187 -0
  379. infinity_context_core/application/context_temporal_hints.py +485 -0
  380. infinity_context_core/application/context_temporal_intent_policy.py +146 -0
  381. infinity_context_core/application/context_temporal_interval_requirements.py +107 -0
  382. infinity_context_core/application/context_temporal_metadata.py +29 -0
  383. infinity_context_core/application/context_temporal_query.py +1358 -0
  384. infinity_context_core/application/context_temporal_ranges.py +471 -0
  385. infinity_context_core/application/context_temporal_session_order.py +439 -0
  386. infinity_context_core/application/context_temporal_source_identity.py +426 -0
  387. infinity_context_core/application/context_temporal_source_turn.py +1290 -0
  388. infinity_context_core/application/context_temporal_source_turn_labels.py +265 -0
  389. infinity_context_core/application/context_temporal_source_turn_patterns.py +186 -0
  390. infinity_context_core/application/context_temporal_source_turn_types.py +62 -0
  391. infinity_context_core/application/context_travel_hobby_writing_evidence.py +176 -0
  392. infinity_context_core/application/context_travel_place_evidence.py +23 -0
  393. infinity_context_core/application/document_fragments.py +206 -0
  394. infinity_context_core/application/document_text.py +203 -0
  395. infinity_context_core/application/dto.py +311 -0
  396. infinity_context_core/application/dto_anchors.py +143 -0
  397. infinity_context_core/application/dto_assets.py +234 -0
  398. infinity_context_core/application/dto_benchmark_runs.py +114 -0
  399. infinity_context_core/application/dto_context.py +176 -0
  400. infinity_context_core/application/dto_context_links.py +170 -0
  401. infinity_context_core/application/dto_facts.py +275 -0
  402. infinity_context_core/application/dto_suggestions_capture.py +199 -0
  403. infinity_context_core/application/dto_workspace.py +101 -0
  404. infinity_context_core/application/extraction_coordinates.py +56 -0
  405. infinity_context_core/application/extraction_resource_policy.py +796 -0
  406. infinity_context_core/application/extractor.py +147 -0
  407. infinity_context_core/application/memory_digest_renderer.py +215 -0
  408. infinity_context_core/application/multimodal_manifest.py +418 -0
  409. infinity_context_core/application/normalize.py +29 -0
  410. infinity_context_core/application/observed_anchor_resolution.py +243 -0
  411. infinity_context_core/application/related_facts.py +144 -0
  412. infinity_context_core/application/review_payloads.py +104 -0
  413. infinity_context_core/application/safe_payload.py +86 -0
  414. infinity_context_core/application/semantic_dedupe.py +943 -0
  415. infinity_context_core/application/sensitive_text.py +39 -0
  416. infinity_context_core/application/source_refs.py +263 -0
  417. infinity_context_core/application/storage_key_safety.py +13 -0
  418. infinity_context_core/application/temporal_validity.py +31 -0
  419. infinity_context_core/application/use_cases/__init__.py +1 -0
  420. infinity_context_core/application/use_cases/anchors.py +1165 -0
  421. infinity_context_core/application/use_cases/asset_extraction_support.py +273 -0
  422. infinity_context_core/application/use_cases/asset_extractions.py +1454 -0
  423. infinity_context_core/application/use_cases/assets.py +463 -0
  424. infinity_context_core/application/use_cases/benchmark_runs.py +877 -0
  425. infinity_context_core/application/use_cases/benchmark_unsealed_abort.py +147 -0
  426. infinity_context_core/application/use_cases/blob_storage_cleanup.py +289 -0
  427. infinity_context_core/application/use_cases/blob_storage_integrity.py +351 -0
  428. infinity_context_core/application/use_cases/build_context.py +995 -0
  429. infinity_context_core/application/use_cases/build_context_final_packing.py +59 -0
  430. infinity_context_core/application/use_cases/build_context_item_projection.py +839 -0
  431. infinity_context_core/application/use_cases/build_context_keyword_aggregation.py +908 -0
  432. infinity_context_core/application/use_cases/build_context_keyword_aggregation_seeds.py +132 -0
  433. infinity_context_core/application/use_cases/build_context_keyword_aggregation_selection.py +280 -0
  434. infinity_context_core/application/use_cases/build_context_keyword_aggregation_support.py +161 -0
  435. infinity_context_core/application/use_cases/build_context_requirement_guard.py +156 -0
  436. infinity_context_core/application/use_cases/build_context_source_selection.py +935 -0
  437. infinity_context_core/application/use_cases/build_memory_digest.py +203 -0
  438. infinity_context_core/application/use_cases/build_memory_insights.py +979 -0
  439. infinity_context_core/application/use_cases/consolidate_capture.py +806 -0
  440. infinity_context_core/application/use_cases/context_link_crud.py +196 -0
  441. infinity_context_core/application/use_cases/context_link_reviews.py +441 -0
  442. infinity_context_core/application/use_cases/context_link_suggestions.py +1060 -0
  443. infinity_context_core/application/use_cases/context_link_visibility.py +106 -0
  444. infinity_context_core/application/use_cases/context_links.py +33 -0
  445. infinity_context_core/application/use_cases/delete_document.py +71 -0
  446. infinity_context_core/application/use_cases/delete_thread_memory.py +55 -0
  447. infinity_context_core/application/use_cases/duplicate_merge_resolution.py +307 -0
  448. infinity_context_core/application/use_cases/ensure_scope.py +29 -0
  449. infinity_context_core/application/use_cases/expire_suggestions.py +36 -0
  450. infinity_context_core/application/use_cases/export_graph.py +684 -0
  451. infinity_context_core/application/use_cases/fact_relations.py +204 -0
  452. infinity_context_core/application/use_cases/forget_fact.py +43 -0
  453. infinity_context_core/application/use_cases/get_capabilities.py +213 -0
  454. infinity_context_core/application/use_cases/get_capture.py +16 -0
  455. infinity_context_core/application/use_cases/get_session_status.py +23 -0
  456. infinity_context_core/application/use_cases/ingest_document.py +376 -0
  457. infinity_context_core/application/use_cases/ingest_episode.py +263 -0
  458. infinity_context_core/application/use_cases/list_captures.py +25 -0
  459. infinity_context_core/application/use_cases/memory_browser.py +414 -0
  460. infinity_context_core/application/use_cases/operations_console.py +293 -0
  461. infinity_context_core/application/use_cases/process_document.py +83 -0
  462. infinity_context_core/application/use_cases/purge_capture.py +34 -0
  463. infinity_context_core/application/use_cases/query_documents.py +42 -0
  464. infinity_context_core/application/use_cases/query_facts.py +57 -0
  465. infinity_context_core/application/use_cases/receive_capture.py +238 -0
  466. infinity_context_core/application/use_cases/related_facts.py +47 -0
  467. infinity_context_core/application/use_cases/remember_fact.py +145 -0
  468. infinity_context_core/application/use_cases/spaces_memory_scopes.py +134 -0
  469. infinity_context_core/application/use_cases/suggestions.py +804 -0
  470. infinity_context_core/application/use_cases/update_fact.py +43 -0
  471. infinity_context_core/application/use_cases/usage.py +49 -0
  472. infinity_context_core/application/use_cases/users.py +138 -0
  473. infinity_context_core/domain/__init__.py +1 -0
  474. infinity_context_core/domain/aggregation_admission.py +200 -0
  475. infinity_context_core/domain/anchor_entities.py +316 -0
  476. infinity_context_core/domain/assets.py +654 -0
  477. infinity_context_core/domain/capture.py +321 -0
  478. infinity_context_core/domain/distinct_set_membership.py +69 -0
  479. infinity_context_core/domain/entities.py +103 -0
  480. infinity_context_core/domain/entity_policies.py +119 -0
  481. infinity_context_core/domain/entity_types.py +150 -0
  482. infinity_context_core/domain/errors.py +53 -0
  483. infinity_context_core/domain/events.py +14 -0
  484. infinity_context_core/domain/extraction.py +529 -0
  485. infinity_context_core/domain/idempotency.py +12 -0
  486. infinity_context_core/domain/memory_item_entities.py +426 -0
  487. infinity_context_core/domain/source_refs.py +91 -0
  488. infinity_context_core/domain/suggestion_entities.py +236 -0
  489. infinity_context_core/domain/taxonomy.py +121 -0
  490. infinity_context_core/domain/usage.py +221 -0
  491. infinity_context_core/domain/workspace_entities.py +269 -0
  492. infinity_context_core/features/__init__.py +7 -0
  493. infinity_context_core/features/context_building/__init__.py +3 -0
  494. infinity_context_core/features/context_building/application/__init__.py +92 -0
  495. infinity_context_core/features/context_building/application/candidate_fusion.py +136 -0
  496. infinity_context_core/features/context_building/application/coverage_reservation_selector.py +212 -0
  497. infinity_context_core/features/context_building/application/handlers.py +155 -0
  498. infinity_context_core/features/context_building/application/inference_evidence_reservation.py +592 -0
  499. infinity_context_core/features/context_building/application/provider_pipeline.py +61 -0
  500. infinity_context_core/features/context_building/application/queries.py +107 -0
  501. infinity_context_core/features/context_building/application/use_cases.py +78 -0
  502. infinity_context_core/features/context_building/domain/__init__.py +95 -0
  503. infinity_context_core/features/context_building/domain/budget.py +97 -0
  504. infinity_context_core/features/context_building/domain/context.py +157 -0
  505. infinity_context_core/features/context_building/domain/evidence_obligations.py +60 -0
  506. infinity_context_core/features/context_building/domain/feature.py +15 -0
  507. infinity_context_core/features/context_building/domain/prompt_sections.py +150 -0
  508. infinity_context_core/features/context_building/domain/query_pipeline.py +276 -0
  509. infinity_context_core/features/context_building/domain/rendering.py +102 -0
  510. infinity_context_core/features/context_building/ports/__init__.py +8 -0
  511. infinity_context_core/features/context_building/ports/candidates.py +36 -0
  512. infinity_context_core/features/context_building/public.py +165 -0
  513. infinity_context_core/features/context_building/tests/test_application_ports_public_skeleton.py +575 -0
  514. infinity_context_core/features/context_building/tests/test_domain_context_building_skeleton.py +402 -0
  515. infinity_context_core/features/document_ingestion/__init__.py +3 -0
  516. infinity_context_core/features/document_ingestion/application/__init__.py +31 -0
  517. infinity_context_core/features/document_ingestion/application/commands.py +62 -0
  518. infinity_context_core/features/document_ingestion/application/use_cases.py +273 -0
  519. infinity_context_core/features/document_ingestion/domain/__init__.py +61 -0
  520. infinity_context_core/features/document_ingestion/domain/chunking.py +297 -0
  521. infinity_context_core/features/document_ingestion/domain/errors.py +22 -0
  522. infinity_context_core/features/document_ingestion/domain/feature.py +18 -0
  523. infinity_context_core/features/document_ingestion/domain/source_document.py +228 -0
  524. infinity_context_core/features/document_ingestion/ports/__init__.py +21 -0
  525. infinity_context_core/features/document_ingestion/ports/indexing.py +51 -0
  526. infinity_context_core/features/document_ingestion/ports/repositories.py +56 -0
  527. infinity_context_core/features/document_ingestion/public.py +95 -0
  528. infinity_context_core/features/document_ingestion/tests/test_application_handlers.py +226 -0
  529. infinity_context_core/features/document_ingestion/tests/test_application_ports_public_skeleton.py +334 -0
  530. infinity_context_core/features/document_ingestion/tests/test_domain_chunking_skeleton.py +79 -0
  531. infinity_context_core/features/document_ingestion/tests/test_domain_source_document_skeleton.py +101 -0
  532. infinity_context_core/features/memory_facts/__init__.py +3 -0
  533. infinity_context_core/features/memory_facts/application/__init__.py +37 -0
  534. infinity_context_core/features/memory_facts/application/commands.py +89 -0
  535. infinity_context_core/features/memory_facts/application/handlers.py +241 -0
  536. infinity_context_core/features/memory_facts/application/use_cases.py +47 -0
  537. infinity_context_core/features/memory_facts/domain/__init__.py +35 -0
  538. infinity_context_core/features/memory_facts/domain/fact.py +98 -0
  539. infinity_context_core/features/memory_facts/domain/feature.py +15 -0
  540. infinity_context_core/features/memory_facts/ports/__init__.py +31 -0
  541. infinity_context_core/features/memory_facts/ports/clock.py +14 -0
  542. infinity_context_core/features/memory_facts/ports/ids.py +19 -0
  543. infinity_context_core/features/memory_facts/ports/outbox.py +26 -0
  544. infinity_context_core/features/memory_facts/ports/repositories.py +30 -0
  545. infinity_context_core/features/memory_facts/ports/unit_of_work.py +43 -0
  546. infinity_context_core/features/memory_facts/public.py +79 -0
  547. infinity_context_core/features/memory_facts/tests/test_application_handlers.py +332 -0
  548. infinity_context_core/features/memory_facts/tests/test_application_ports_public_skeleton.py +318 -0
  549. infinity_context_core/features/memory_facts/tests/test_domain_fact_skeleton.py +222 -0
  550. infinity_context_core/features/memory_scopes/__init__.py +3 -0
  551. infinity_context_core/features/memory_scopes/application/__init__.py +55 -0
  552. infinity_context_core/features/memory_scopes/application/commands.py +102 -0
  553. infinity_context_core/features/memory_scopes/application/errors.py +27 -0
  554. infinity_context_core/features/memory_scopes/application/handlers.py +226 -0
  555. infinity_context_core/features/memory_scopes/application/use_cases.py +68 -0
  556. infinity_context_core/features/memory_scopes/domain/__init__.py +59 -0
  557. infinity_context_core/features/memory_scopes/domain/errors.py +22 -0
  558. infinity_context_core/features/memory_scopes/domain/feature.py +18 -0
  559. infinity_context_core/features/memory_scopes/domain/policies.py +197 -0
  560. infinity_context_core/features/memory_scopes/domain/scope.py +178 -0
  561. infinity_context_core/features/memory_scopes/ports/__init__.py +25 -0
  562. infinity_context_core/features/memory_scopes/ports/clock.py +14 -0
  563. infinity_context_core/features/memory_scopes/ports/ids.py +13 -0
  564. infinity_context_core/features/memory_scopes/ports/repositories.py +40 -0
  565. infinity_context_core/features/memory_scopes/ports/unit_of_work.py +39 -0
  566. infinity_context_core/features/memory_scopes/public.py +111 -0
  567. infinity_context_core/features/memory_scopes/tests/test_application_ports_public_skeleton.py +824 -0
  568. infinity_context_core/features/memory_scopes/tests/test_domain_scope_skeleton.py +428 -0
  569. infinity_context_core/memory_scope_snapshot_preview.py +863 -0
  570. infinity_context_core/memory_scope_snapshots.py +206 -0
  571. infinity_context_core/ports/__init__.py +164 -0
  572. infinity_context_core/ports/adapters.py +217 -0
  573. infinity_context_core/ports/assets.py +270 -0
  574. infinity_context_core/ports/auth.py +68 -0
  575. infinity_context_core/ports/auto_memory.py +71 -0
  576. infinity_context_core/ports/benchmark_runs.py +205 -0
  577. infinity_context_core/ports/capabilities.py +324 -0
  578. infinity_context_core/ports/captures.py +53 -0
  579. infinity_context_core/ports/clock.py +11 -0
  580. infinity_context_core/ports/derived_projection_policy.py +72 -0
  581. infinity_context_core/ports/extraction.py +201 -0
  582. infinity_context_core/ports/graph_evidence.py +170 -0
  583. infinity_context_core/ports/ids.py +11 -0
  584. infinity_context_core/ports/projection_fence.py +27 -0
  585. infinity_context_core/ports/repositories.py +563 -0
  586. infinity_context_core/ports/transcription.py +63 -0
  587. infinity_context_core/ports/unit_of_work.py +77 -0
  588. infinity_context_core/ports/usage.py +36 -0
  589. infinity_context_core/ports/vector_projection_evidence.py +230 -0
  590. infinity_context_core/ports/vision.py +36 -0
  591. infinity_context_core/processes/__init__.py +3 -0
  592. infinity_context_core/processes/document_memory_pipeline/__init__.py +3 -0
  593. infinity_context_core/processes/document_memory_pipeline/contracts.py +44 -0
  594. infinity_context_core/processes/document_memory_pipeline/process.py +72 -0
  595. infinity_context_core/processes/document_memory_pipeline/public.py +21 -0
  596. infinity_context_core/reporting.py +105 -0
  597. infinity_context_mcp/__init__.py +5 -0
  598. infinity_context_mcp/__main__.py +6 -0
  599. infinity_context_mcp/adapters/__init__.py +1 -0
  600. infinity_context_mcp/adapters/http_gateway.py +940 -0
  601. infinity_context_mcp/agent_behavior_bench.py +1731 -0
  602. infinity_context_mcp/agent_behavior_bench_metrics.py +419 -0
  603. infinity_context_mcp/agent_behavior_bench_redaction.py +149 -0
  604. infinity_context_mcp/agent_behavior_bench_types.py +127 -0
  605. infinity_context_mcp/agent_behavior_scenarios.py +35 -0
  606. infinity_context_mcp/agent_behavior_scenarios_core.py +484 -0
  607. infinity_context_mcp/agent_behavior_scenarios_realistic.py +473 -0
  608. infinity_context_mcp/agent_behavior_scenarios_transcript.py +196 -0
  609. infinity_context_mcp/agent_behavior_transcripts.py +231 -0
  610. infinity_context_mcp/agent_behavior_types.py +66 -0
  611. infinity_context_mcp/agent_behavior_utils.py +29 -0
  612. infinity_context_mcp/application/__init__.py +1 -0
  613. infinity_context_mcp/application/context_link_review.py +109 -0
  614. infinity_context_mcp/application/local_runtime.py +473 -0
  615. infinity_context_mcp/application/normalization.py +36 -0
  616. infinity_context_mcp/application/obsidian.py +568 -0
  617. infinity_context_mcp/application/policy.py +160 -0
  618. infinity_context_mcp/application/ports.py +324 -0
  619. infinity_context_mcp/application/prepare.py +254 -0
  620. infinity_context_mcp/application/readiness.py +71 -0
  621. infinity_context_mcp/application/review_batch.py +62 -0
  622. infinity_context_mcp/application/service.py +701 -0
  623. infinity_context_mcp/application/service_base.py +29 -0
  624. infinity_context_mcp/application/service_constants.py +89 -0
  625. infinity_context_mcp/application/service_context_links.py +224 -0
  626. infinity_context_mcp/application/service_duplicates.py +62 -0
  627. infinity_context_mcp/application/service_facts.py +461 -0
  628. infinity_context_mcp/application/service_helpers.py +179 -0
  629. infinity_context_mcp/application/service_lifecycle.py +348 -0
  630. infinity_context_mcp/application/service_policy.py +74 -0
  631. infinity_context_mcp/application/service_query.py +384 -0
  632. infinity_context_mcp/application/service_resources.py +192 -0
  633. infinity_context_mcp/application/service_response.py +95 -0
  634. infinity_context_mcp/application/service_scope.py +132 -0
  635. infinity_context_mcp/application/service_suggestions.py +820 -0
  636. infinity_context_mcp/application/suggest_batch.py +71 -0
  637. infinity_context_mcp/application/usage_guide.py +74 -0
  638. infinity_context_mcp/bench.py +279 -0
  639. infinity_context_mcp/config.py +213 -0
  640. infinity_context_mcp/domain/__init__.py +1 -0
  641. infinity_context_mcp/domain/context_links.py +117 -0
  642. infinity_context_mcp/domain/local_runtime_models.py +56 -0
  643. infinity_context_mcp/domain/memory_browser.py +35 -0
  644. infinity_context_mcp/domain/models.py +1036 -0
  645. infinity_context_mcp/domain/obsidian_models.py +67 -0
  646. infinity_context_mcp/domain/policy.py +112 -0
  647. infinity_context_mcp/domain/prepare_models.py +24 -0
  648. infinity_context_mcp/plugin_hook.py +873 -0
  649. infinity_context_mcp/plugin_hook_settings.py +253 -0
  650. infinity_context_mcp/server.py +133 -0
  651. infinity_context_mcp/server_context_tools.py +404 -0
  652. infinity_context_mcp/server_fact_tools.py +928 -0
  653. infinity_context_mcp/server_hardening.py +54 -0
  654. infinity_context_mcp/server_local_runtime_tools.py +164 -0
  655. infinity_context_mcp/server_obsidian_tools.py +380 -0
  656. infinity_context_mcp/server_query_tools.py +626 -0
  657. infinity_context_mcp/server_request_mapping.py +73 -0
  658. infinity_context_mcp/server_resources.py +187 -0
  659. infinity_context_mcp/server_response.py +26 -0
  660. infinity_context_obsidian/__init__.py +5 -0
  661. infinity_context_obsidian/cli.py +594 -0
  662. infinity_context_obsidian/conflicts.py +110 -0
  663. infinity_context_obsidian/doctor.py +319 -0
  664. infinity_context_obsidian/domain.py +92 -0
  665. infinity_context_obsidian/frontmatter.py +68 -0
  666. infinity_context_obsidian/gateway.py +66 -0
  667. infinity_context_obsidian/layout.py +179 -0
  668. infinity_context_obsidian/note_format.py +159 -0
  669. infinity_context_obsidian/plugin_bundle/__init__.py +1 -0
  670. infinity_context_obsidian/plugin_bundle/main.js +5 -0
  671. infinity_context_obsidian/plugin_bundle/manifest.json +10 -0
  672. infinity_context_obsidian/plugin_bundle/styles.css +123 -0
  673. infinity_context_obsidian/plugin_install.py +139 -0
  674. infinity_context_obsidian/ports.py +72 -0
  675. infinity_context_obsidian/setup.py +202 -0
  676. infinity_context_obsidian/state.py +85 -0
  677. infinity_context_obsidian/sync.py +741 -0
  678. infinity_context_obsidian/vault.py +61 -0
  679. infinity_context_sdk/__init__.py +1049 -0
  680. infinity_context_sdk/_payloads.py +401 -0
  681. infinity_context_sdk/_redaction.py +31 -0
  682. infinity_context_sdk/anchors.py +249 -0
  683. infinity_context_sdk/assets.py +554 -0
  684. infinity_context_sdk/capabilities.py +199 -0
  685. infinity_context_sdk/context.py +540 -0
  686. infinity_context_sdk/context_bundle_diagnostics.py +905 -0
  687. infinity_context_sdk/context_payload_utils.py +219 -0
  688. infinity_context_sdk/export.py +123 -0
  689. infinity_context_sdk/scopes.py +74 -0
  690. infinity_context_sdk/thread_memory.py +55 -0
  691. infinity_context_server/__init__.py +5 -0
  692. infinity_context_server/__main__.py +19 -0
  693. infinity_context_server/admin.py +832 -0
  694. infinity_context_server/admin_invariants.py +388 -0
  695. infinity_context_server/admin_outbox.py +130 -0
  696. infinity_context_server/admin_projection_repair.py +374 -0
  697. infinity_context_server/api/__init__.py +1 -0
  698. infinity_context_server/api/auth.py +432 -0
  699. infinity_context_server/api/dependencies.py +9 -0
  700. infinity_context_server/api/errors.py +94 -0
  701. infinity_context_server/api/legacy_client.py +356 -0
  702. infinity_context_server/api/policy.py +35 -0
  703. infinity_context_server/api/public_payload.py +27 -0
  704. infinity_context_server/api/v1/__init__.py +54 -0
  705. infinity_context_server/api/v1/anchors.py +469 -0
  706. infinity_context_server/api/v1/assets.py +409 -0
  707. infinity_context_server/api/v1/capabilities.py +144 -0
  708. infinity_context_server/api/v1/captures.py +329 -0
  709. infinity_context_server/api/v1/context.py +362 -0
  710. infinity_context_server/api/v1/context_links.py +611 -0
  711. infinity_context_server/api/v1/diagnostics.py +333 -0
  712. infinity_context_server/api/v1/digest.py +107 -0
  713. infinity_context_server/api/v1/documents.py +164 -0
  714. infinity_context_server/api/v1/episodes.py +146 -0
  715. infinity_context_server/api/v1/export.py +184 -0
  716. infinity_context_server/api/v1/facts.py +265 -0
  717. infinity_context_server/api/v1/health.py +24 -0
  718. infinity_context_server/api/v1/insights.py +100 -0
  719. infinity_context_server/api/v1/internal_memory_comparison_runs.py +406 -0
  720. infinity_context_server/api/v1/memory_browser.py +183 -0
  721. infinity_context_server/api/v1/operations.py +81 -0
  722. infinity_context_server/api/v1/scope_resolution.py +389 -0
  723. infinity_context_server/api/v1/source_refs.py +111 -0
  724. infinity_context_server/api/v1/spaces_memory_scopes.py +141 -0
  725. infinity_context_server/api/v1/suggestions.py +237 -0
  726. infinity_context_server/api/v1/thread_memory.py +74 -0
  727. infinity_context_server/api/v1/usage.py +75 -0
  728. infinity_context_server/api/v1/users.py +164 -0
  729. infinity_context_server/auth_scope.py +492 -0
  730. infinity_context_server/auth_tokens.py +283 -0
  731. infinity_context_server/backpressure.py +47 -0
  732. infinity_context_server/benchmark_projection_absence.py +155 -0
  733. infinity_context_server/benchmark_projection_delete_outbox.py +162 -0
  734. infinity_context_server/composition.py +897 -0
  735. infinity_context_server/config.py +223 -0
  736. infinity_context_server/db.py +39 -0
  737. infinity_context_server/deployment_readiness.py +205 -0
  738. infinity_context_server/derived_identity_evidence.py +987 -0
  739. infinity_context_server/derived_identity_target.py +39 -0
  740. infinity_context_server/derived_projection_policy.py +85 -0
  741. infinity_context_server/diagnostics.py +536 -0
  742. infinity_context_server/doctor.py +71 -0
  743. infinity_context_server/eval.py +3066 -0
  744. infinity_context_server/eval_auto_memory.py +1735 -0
  745. infinity_context_server/eval_auto_memory_metrics.py +224 -0
  746. infinity_context_server/eval_auto_memory_types.py +75 -0
  747. infinity_context_server/eval_case_catalog.py +1590 -0
  748. infinity_context_server/eval_case_runner.py +1100 -0
  749. infinity_context_server/eval_common.py +336 -0
  750. infinity_context_server/eval_constants.py +310 -0
  751. infinity_context_server/eval_fixtures.py +150 -0
  752. infinity_context_server/eval_graph.py +92 -0
  753. infinity_context_server/eval_hybrid.py +99 -0
  754. infinity_context_server/eval_multimodal_offline.py +697 -0
  755. infinity_context_server/eval_prompt_contract.py +402 -0
  756. infinity_context_server/eval_scorecard.py +2627 -0
  757. infinity_context_server/eval_semantic_linking.py +1526 -0
  758. infinity_context_server/eval_semantic_retrieval_api_canary.py +405 -0
  759. infinity_context_server/eval_semantic_retrieval_canary.py +359 -0
  760. infinity_context_server/eval_types.py +88 -0
  761. infinity_context_server/evidence_bundle.py +523 -0
  762. infinity_context_server/extraction_capabilities.py +1234 -0
  763. infinity_context_server/features/__init__.py +3 -0
  764. infinity_context_server/features/context_building/__init__.py +5 -0
  765. infinity_context_server/features/context_building/benchmark_budget.py +29 -0
  766. infinity_context_server/features/context_building/composition.py +49 -0
  767. infinity_context_server/features/context_building/context_requests.py +151 -0
  768. infinity_context_server/features/context_building/contracts.py +96 -0
  769. infinity_context_server/features/context_building/digest_api_responses.py +225 -0
  770. infinity_context_server/features/context_building/digest_requests.py +65 -0
  771. infinity_context_server/features/context_building/insights_api_responses.py +132 -0
  772. infinity_context_server/features/context_building/legacy_api_responses.py +875 -0
  773. infinity_context_server/features/context_building/mappers.py +205 -0
  774. infinity_context_server/features/context_building/public.py +66 -0
  775. infinity_context_server/features/context_building/routes.py +44 -0
  776. infinity_context_server/features/document_ingestion/__init__.py +5 -0
  777. infinity_context_server/features/document_ingestion/asset_requests.py +45 -0
  778. infinity_context_server/features/document_ingestion/asset_responses.py +365 -0
  779. infinity_context_server/features/document_ingestion/composition.py +49 -0
  780. infinity_context_server/features/document_ingestion/contracts.py +104 -0
  781. infinity_context_server/features/document_ingestion/mappers.py +386 -0
  782. infinity_context_server/features/document_ingestion/public.py +58 -0
  783. infinity_context_server/features/document_ingestion/routes.py +44 -0
  784. infinity_context_server/features/memory_facts/__init__.py +7 -0
  785. infinity_context_server/features/memory_facts/compatibility.py +204 -0
  786. infinity_context_server/features/memory_facts/composition.py +72 -0
  787. infinity_context_server/features/memory_facts/contracts.py +187 -0
  788. infinity_context_server/features/memory_facts/mappers.py +620 -0
  789. infinity_context_server/features/memory_facts/public.py +168 -0
  790. infinity_context_server/features/memory_facts/routes.py +118 -0
  791. infinity_context_server/features/memory_facts/suggestion_requests.py +283 -0
  792. infinity_context_server/features/memory_facts/suggestion_responses.py +382 -0
  793. infinity_context_server/features/memory_scopes/__init__.py +5 -0
  794. infinity_context_server/features/memory_scopes/compatibility.py +231 -0
  795. infinity_context_server/features/memory_scopes/composition.py +49 -0
  796. infinity_context_server/features/memory_scopes/contracts.py +103 -0
  797. infinity_context_server/features/memory_scopes/graph_export_responses.py +93 -0
  798. infinity_context_server/features/memory_scopes/mappers.py +290 -0
  799. infinity_context_server/features/memory_scopes/public.py +136 -0
  800. infinity_context_server/features/memory_scopes/routes.py +185 -0
  801. infinity_context_server/features/memory_scopes/snapshot_compatibility.py +146 -0
  802. infinity_context_server/features/memory_scopes/thread_memory_requests.py +127 -0
  803. infinity_context_server/llm_provider_readiness.py +267 -0
  804. infinity_context_server/longmemeval_session_identity.py +167 -0
  805. infinity_context_server/main.py +68 -0
  806. infinity_context_server/mem0_benchmark_auth_challenge.py +160 -0
  807. infinity_context_server/memora_comparison.py +658 -0
  808. infinity_context_server/memory_comparison_answer_context.py +2442 -0
  809. infinity_context_server/memory_comparison_answer_context_backfill.py +1027 -0
  810. infinity_context_server/memory_comparison_answer_context_diagnostics.py +694 -0
  811. infinity_context_server/memory_comparison_answer_context_inspection.py +103 -0
  812. infinity_context_server/memory_comparison_answer_context_retrieval_slice.py +175 -0
  813. infinity_context_server/memory_comparison_answer_context_risks.py +223 -0
  814. infinity_context_server/memory_comparison_backend_target.py +41 -0
  815. infinity_context_server/memory_comparison_benchmark.py +651 -0
  816. infinity_context_server/memory_comparison_benchmark_compact.py +387 -0
  817. infinity_context_server/memory_comparison_benchmark_compact_fast_gate.py +387 -0
  818. infinity_context_server/memory_comparison_benchmark_compact_samples.py +931 -0
  819. infinity_context_server/memory_comparison_benchmark_config.py +105 -0
  820. infinity_context_server/memory_comparison_benchmark_control.py +215 -0
  821. infinity_context_server/memory_comparison_benchmark_execution.py +238 -0
  822. infinity_context_server/memory_comparison_benchmark_identity.py +65 -0
  823. infinity_context_server/memory_comparison_benchmark_metrics.py +686 -0
  824. infinity_context_server/memory_comparison_benchmark_replay.py +400 -0
  825. infinity_context_server/memory_comparison_benchmark_shared.py +284 -0
  826. infinity_context_server/memory_comparison_bounded_provider.py +295 -0
  827. infinity_context_server/memory_comparison_bundle_partial_support.py +107 -0
  828. infinity_context_server/memory_comparison_bundle_planner.py +3521 -0
  829. infinity_context_server/memory_comparison_bundle_source_windows.py +247 -0
  830. infinity_context_server/memory_comparison_candidate_features.py +2014 -0
  831. infinity_context_server/memory_comparison_candidate_fusion.py +893 -0
  832. infinity_context_server/memory_comparison_candidate_risks.py +124 -0
  833. infinity_context_server/memory_comparison_canonical_source_hash.py +109 -0
  834. infinity_context_server/memory_comparison_case_identity.py +100 -0
  835. infinity_context_server/memory_comparison_case_loader.py +123 -0
  836. infinity_context_server/memory_comparison_chat_completions.py +402 -0
  837. infinity_context_server/memory_comparison_clean_state.py +607 -0
  838. infinity_context_server/memory_comparison_clean_state_http.py +120 -0
  839. infinity_context_server/memory_comparison_clean_state_runner.py +103 -0
  840. infinity_context_server/memory_comparison_cli.py +295 -0
  841. infinity_context_server/memory_comparison_codex_canary.py +315 -0
  842. infinity_context_server/memory_comparison_codex_llm.py +345 -0
  843. infinity_context_server/memory_comparison_compact_gap_report.py +289 -0
  844. infinity_context_server/memory_comparison_comparative_preference.py +71 -0
  845. infinity_context_server/memory_comparison_conversation_ingestion.py +198 -0
  846. infinity_context_server/memory_comparison_evidence.py +877 -0
  847. infinity_context_server/memory_comparison_evidence_commitment.py +20 -0
  848. infinity_context_server/memory_comparison_failure_diagnostics.py +809 -0
  849. infinity_context_server/memory_comparison_full_canonical_evidence.py +746 -0
  850. infinity_context_server/memory_comparison_full_canonical_source_evidence_trust.py +450 -0
  851. infinity_context_server/memory_comparison_full_delete_evidence.py +670 -0
  852. infinity_context_server/memory_comparison_full_delete_evidence_trust.py +441 -0
  853. infinity_context_server/memory_comparison_full_delete_evidence_witnesses.py +308 -0
  854. infinity_context_server/memory_comparison_full_execution_validation.py +697 -0
  855. infinity_context_server/memory_comparison_full_execution_validation_slots.py +655 -0
  856. infinity_context_server/memory_comparison_full_methodology.py +556 -0
  857. infinity_context_server/memory_comparison_full_policy_component_validation.py +899 -0
  858. infinity_context_server/memory_comparison_full_profiles.py +398 -0
  859. infinity_context_server/memory_comparison_full_run_component_sets.py +304 -0
  860. infinity_context_server/memory_comparison_full_run_components.py +787 -0
  861. infinity_context_server/memory_comparison_full_run_evidence.py +759 -0
  862. infinity_context_server/memory_comparison_full_scope.py +244 -0
  863. infinity_context_server/memory_comparison_full_source_evidence.py +866 -0
  864. infinity_context_server/memory_comparison_full_verdict.py +252 -0
  865. infinity_context_server/memory_comparison_gold_blind.py +654 -0
  866. infinity_context_server/memory_comparison_gold_blind_answer_contract.py +492 -0
  867. infinity_context_server/memory_comparison_gold_blind_contract.py +760 -0
  868. infinity_context_server/memory_comparison_gold_blind_dispatch_models.py +34 -0
  869. infinity_context_server/memory_comparison_gold_blind_execution_validation.py +170 -0
  870. infinity_context_server/memory_comparison_gold_blind_judge_binding.py +167 -0
  871. infinity_context_server/memory_comparison_gold_blind_judge_capability.py +387 -0
  872. infinity_context_server/memory_comparison_gold_blind_retrieval_contract.py +413 -0
  873. infinity_context_server/memory_comparison_gold_blind_run_proof.py +880 -0
  874. infinity_context_server/memory_comparison_gold_blind_run_validation.py +109 -0
  875. infinity_context_server/memory_comparison_gold_blind_validation.py +319 -0
  876. infinity_context_server/memory_comparison_http.py +921 -0
  877. infinity_context_server/memory_comparison_http_ingest_observation.py +710 -0
  878. infinity_context_server/memory_comparison_http_ingest_request.py +204 -0
  879. infinity_context_server/memory_comparison_intent.py +2925 -0
  880. infinity_context_server/memory_comparison_latency.py +455 -0
  881. infinity_context_server/memory_comparison_llm.py +473 -0
  882. infinity_context_server/memory_comparison_location_roles.py +169 -0
  883. infinity_context_server/memory_comparison_locomo_cases.py +328 -0
  884. infinity_context_server/memory_comparison_locomo_expected_turn.py +297 -0
  885. infinity_context_server/memory_comparison_locomo_transport.py +873 -0
  886. infinity_context_server/memory_comparison_longmemeval_cases.py +339 -0
  887. infinity_context_server/memory_comparison_managed_attestation.py +771 -0
  888. infinity_context_server/memory_comparison_managed_benchmark_registry_abort_http.py +120 -0
  889. infinity_context_server/memory_comparison_managed_benchmark_registry_contracts.py +715 -0
  890. infinity_context_server/memory_comparison_managed_benchmark_registry_http.py +855 -0
  891. infinity_context_server/memory_comparison_managed_benchmark_registry_recovery_http.py +164 -0
  892. infinity_context_server/memory_comparison_managed_benchmark_registry_wire.py +890 -0
  893. infinity_context_server/memory_comparison_managed_composite_assembler.py +524 -0
  894. infinity_context_server/memory_comparison_managed_corpus_projection.py +653 -0
  895. infinity_context_server/memory_comparison_managed_execution_receipts.py +991 -0
  896. infinity_context_server/memory_comparison_managed_http_derived_evidence.py +724 -0
  897. infinity_context_server/memory_comparison_managed_http_exact_cleanup.py +476 -0
  898. infinity_context_server/memory_comparison_managed_http_execution.py +673 -0
  899. infinity_context_server/memory_comparison_managed_http_lifecycle.py +905 -0
  900. infinity_context_server/memory_comparison_managed_http_lifecycle_credentials.py +83 -0
  901. infinity_context_server/memory_comparison_managed_http_lifecycle_evidence.py +366 -0
  902. infinity_context_server/memory_comparison_managed_http_lifecycle_registration.py +106 -0
  903. infinity_context_server/memory_comparison_managed_http_policy_lifecycle.py +894 -0
  904. infinity_context_server/memory_comparison_managed_http_policy_material_projection.py +315 -0
  905. infinity_context_server/memory_comparison_managed_http_policy_receipts.py +219 -0
  906. infinity_context_server/memory_comparison_managed_http_policy_registry_evidence.py +177 -0
  907. infinity_context_server/memory_comparison_managed_http_policy_requirements.py +824 -0
  908. infinity_context_server/memory_comparison_managed_http_policy_support.py +108 -0
  909. infinity_context_server/memory_comparison_managed_http_policy_validation.py +762 -0
  910. infinity_context_server/memory_comparison_managed_ingest_manifest.py +449 -0
  911. infinity_context_server/memory_comparison_managed_live_admission.py +886 -0
  912. infinity_context_server/memory_comparison_managed_live_cli.py +536 -0
  913. infinity_context_server/memory_comparison_managed_live_composition.py +636 -0
  914. infinity_context_server/memory_comparison_managed_llm_execution.py +850 -0
  915. infinity_context_server/memory_comparison_managed_llm_execution_dispatch.py +269 -0
  916. infinity_context_server/memory_comparison_managed_llm_execution_ports.py +225 -0
  917. infinity_context_server/memory_comparison_managed_mem0_auth.py +65 -0
  918. infinity_context_server/memory_comparison_managed_mem0_runtime_authority.py +241 -0
  919. infinity_context_server/memory_comparison_managed_mem0_runtime_http.py +541 -0
  920. infinity_context_server/memory_comparison_managed_plan_builder.py +685 -0
  921. infinity_context_server/memory_comparison_managed_preflight.py +907 -0
  922. infinity_context_server/memory_comparison_managed_preflight_cli.py +431 -0
  923. infinity_context_server/memory_comparison_managed_production_cli.py +325 -0
  924. infinity_context_server/memory_comparison_managed_production_composition.py +140 -0
  925. infinity_context_server/memory_comparison_managed_production_ports.py +194 -0
  926. infinity_context_server/memory_comparison_managed_production_runner.py +450 -0
  927. infinity_context_server/memory_comparison_managed_projection_manifest.py +438 -0
  928. infinity_context_server/memory_comparison_managed_provider_calls.py +450 -0
  929. infinity_context_server/memory_comparison_managed_quality_projection.py +347 -0
  930. infinity_context_server/memory_comparison_managed_registry_policy_lifecycle.py +677 -0
  931. infinity_context_server/memory_comparison_managed_run.py +892 -0
  932. infinity_context_server/memory_comparison_managed_run_contract.py +454 -0
  933. infinity_context_server/memory_comparison_managed_run_ports.py +79 -0
  934. infinity_context_server/memory_comparison_managed_runtime_credentials.py +916 -0
  935. infinity_context_server/memory_comparison_managed_runtime_credentials_capability.py +361 -0
  936. infinity_context_server/memory_comparison_managed_runtime_credentials_context.py +132 -0
  937. infinity_context_server/memory_comparison_managed_runtime_credentials_integrity.py +213 -0
  938. infinity_context_server/memory_comparison_managed_runtime_credentials_models.py +81 -0
  939. infinity_context_server/memory_comparison_managed_runtime_credentials_targets.py +28 -0
  940. infinity_context_server/memory_comparison_mem0_contract.py +491 -0
  941. infinity_context_server/memory_comparison_mem0_http_observation.py +296 -0
  942. infinity_context_server/memory_comparison_mem0_locomo_prompts.py +328 -0
  943. infinity_context_server/memory_comparison_mem0_longmemeval_prompts.py +365 -0
  944. infinity_context_server/memory_comparison_mem0_official_chat.py +504 -0
  945. infinity_context_server/memory_comparison_mem0_official_prompt_renderer.py +307 -0
  946. infinity_context_server/memory_comparison_mem0_oss_contract.py +456 -0
  947. infinity_context_server/memory_comparison_mem0_oss_manifest.py +563 -0
  948. infinity_context_server/memory_comparison_mem0_platform_contract.py +467 -0
  949. infinity_context_server/memory_comparison_mem0_runtime_attestation.py +861 -0
  950. infinity_context_server/memory_comparison_models.py +398 -0
  951. infinity_context_server/memory_comparison_openai_official_transport.py +525 -0
  952. infinity_context_server/memory_comparison_preflight.py +1147 -0
  953. infinity_context_server/memory_comparison_probe_transport.py +154 -0
  954. infinity_context_server/memory_comparison_prompt_policy.py +603 -0
  955. infinity_context_server/memory_comparison_provider_provenance.py +392 -0
  956. infinity_context_server/memory_comparison_quality_accessors.py +339 -0
  957. infinity_context_server/memory_comparison_quality_actionable_gaps.py +2038 -0
  958. infinity_context_server/memory_comparison_quality_answer_context_gaps.py +450 -0
  959. infinity_context_server/memory_comparison_quality_answer_context_risks.py +75 -0
  960. infinity_context_server/memory_comparison_quality_bundle_gaps.py +860 -0
  961. infinity_context_server/memory_comparison_quality_bundle_table.py +805 -0
  962. infinity_context_server/memory_comparison_quality_diagnostics.py +2798 -0
  963. infinity_context_server/memory_comparison_quality_fusion.py +241 -0
  964. infinity_context_server/memory_comparison_quality_query_plan_gaps.py +533 -0
  965. infinity_context_server/memory_comparison_quality_query_role_gaps.py +845 -0
  966. infinity_context_server/memory_comparison_quality_query_roles.py +815 -0
  967. infinity_context_server/memory_comparison_quality_rerank_gaps.py +492 -0
  968. infinity_context_server/memory_comparison_quality_selected_weakness.py +544 -0
  969. infinity_context_server/memory_comparison_quality_support.py +921 -0
  970. infinity_context_server/memory_comparison_query_integrity.py +301 -0
  971. infinity_context_server/memory_comparison_query_plan.py +524 -0
  972. infinity_context_server/memory_comparison_query_terms.py +2055 -0
  973. infinity_context_server/memory_comparison_relation_support.py +2100 -0
  974. infinity_context_server/memory_comparison_rerank.py +3209 -0
  975. infinity_context_server/memory_comparison_rerank_intents.py +405 -0
  976. infinity_context_server/memory_comparison_rerank_policies.py +1726 -0
  977. infinity_context_server/memory_comparison_rerank_policy.py +675 -0
  978. infinity_context_server/memory_comparison_rerank_shapes.py +152 -0
  979. infinity_context_server/memory_comparison_rerank_terms.py +973 -0
  980. infinity_context_server/memory_comparison_rerank_text.py +722 -0
  981. infinity_context_server/memory_comparison_retrieval_policy.py +108 -0
  982. infinity_context_server/memory_comparison_retrieval_width.py +383 -0
  983. infinity_context_server/memory_comparison_service_probe.py +278 -0
  984. infinity_context_server/memory_comparison_service_probes.py +547 -0
  985. infinity_context_server/memory_comparison_session_identity_contract.py +328 -0
  986. infinity_context_server/memory_comparison_source_identity.py +1655 -0
  987. infinity_context_server/memory_comparison_subscription_chat.py +717 -0
  988. infinity_context_server/memory_comparison_subscription_live_probe.py +323 -0
  989. infinity_context_server/memory_comparison_subscription_probe.py +368 -0
  990. infinity_context_server/memory_comparison_target_identity.py +42 -0
  991. infinity_context_server/memory_comparison_temporal_grounding.py +771 -0
  992. infinity_context_server/memory_comparison_token_budget_contract.py +665 -0
  993. infinity_context_server/memory_comparison_token_latency.py +136 -0
  994. infinity_context_server/memory_scope_transfer.py +933 -0
  995. infinity_context_server/memory_scope_transfer_assets.py +181 -0
  996. infinity_context_server/memory_scope_transfer_conflicts.py +565 -0
  997. infinity_context_server/memory_scope_transfer_context.py +295 -0
  998. infinity_context_server/memory_scope_transfer_extractions.py +390 -0
  999. infinity_context_server/memory_scope_transfer_facts.py +65 -0
  1000. infinity_context_server/memory_scope_transfer_records.py +630 -0
  1001. infinity_context_server/memory_scope_transfer_relations.py +84 -0
  1002. infinity_context_server/memory_scope_transfer_remap.py +710 -0
  1003. infinity_context_server/memory_scope_transfer_scope.py +267 -0
  1004. infinity_context_server/memory_scope_transfer_support.py +108 -0
  1005. infinity_context_server/memory_scope_transfer_temporal.py +24 -0
  1006. infinity_context_server/metrics.py +264 -0
  1007. infinity_context_server/official_public_benchmark.py +979 -0
  1008. infinity_context_server/pagination.py +60 -0
  1009. infinity_context_server/processes/__init__.py +38 -0
  1010. infinity_context_server/processes/extractions.py +40 -0
  1011. infinity_context_server/processes/outbox.py +47 -0
  1012. infinity_context_server/processes/projections.py +372 -0
  1013. infinity_context_server/provider_budget.py +59 -0
  1014. infinity_context_server/provider_circuit.py +333 -0
  1015. infinity_context_server/public_benchmark.py +2532 -0
  1016. infinity_context_server/public_benchmark_artifacts.py +91 -0
  1017. infinity_context_server/public_benchmark_case_diagnostics.py +231 -0
  1018. infinity_context_server/public_benchmark_checkpoint.py +751 -0
  1019. infinity_context_server/public_benchmark_environment.py +102 -0
  1020. infinity_context_server/public_benchmark_execution.py +445 -0
  1021. infinity_context_server/public_benchmark_http.py +39 -0
  1022. infinity_context_server/public_benchmark_manifest.py +129 -0
  1023. infinity_context_server/public_benchmark_metrics.py +645 -0
  1024. infinity_context_server/public_benchmark_metrics_policy.py +117 -0
  1025. infinity_context_server/public_benchmark_models.py +112 -0
  1026. infinity_context_server/public_benchmark_progress.py +224 -0
  1027. infinity_context_server/public_benchmark_results.py +76 -0
  1028. infinity_context_server/public_benchmark_run_diagnostics.py +163 -0
  1029. infinity_context_server/public_benchmark_selection.py +379 -0
  1030. infinity_context_server/public_benchmark_transport.py +62 -0
  1031. infinity_context_server/public_benchmark_unsupported.py +228 -0
  1032. infinity_context_server/ranked_evidence_answer_support.py +665 -0
  1033. infinity_context_server/ranked_evidence_document_seed_contract.py +30 -0
  1034. infinity_context_server/ranked_evidence_evaluator_helpers.py +135 -0
  1035. infinity_context_server/ranked_evidence_retrieval_request.py +36 -0
  1036. infinity_context_server/ranked_evidence_seed_case.py +109 -0
  1037. infinity_context_server/ranked_evidence_semantic_gate.py +980 -0
  1038. infinity_context_server/ranked_evidence_semantic_gate_cli.py +137 -0
  1039. infinity_context_server/ranked_evidence_semantic_gate_decision.py +163 -0
  1040. infinity_context_server/ranked_evidence_semantic_metrics.py +375 -0
  1041. infinity_context_server/ranked_evidence_session_identity.py +100 -0
  1042. infinity_context_server/storage_maintenance.py +187 -0
  1043. infinity_context_server/top_evidence_dataset_profile.py +442 -0
  1044. infinity_context_server/top_evidence_policy.py +274 -0
  1045. infinity_context_server/top_evidence_preflight.py +693 -0
  1046. infinity_context_server/web/assets/memory-browser-capture.js +252 -0
  1047. infinity_context_server/web/assets/memory-browser-operations.js +143 -0
  1048. infinity_context_server/web/assets/memory-browser-review.js +687 -0
  1049. infinity_context_server/web/assets/memory-browser.css +854 -0
  1050. infinity_context_server/web/assets/memory-browser.js +3299 -0
  1051. infinity_context_server/web/index.html +261 -0
  1052. infinity_context_server/web_ui.py +29 -0
  1053. infinity_context_server/worker.py +380 -0
@@ -0,0 +1,2365 @@
1
+ """Role-sensitive action matching for deterministic memory reranking."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import re
6
+ from dataclasses import dataclass
7
+
8
+ from infinity_context_core.application.context_activity_participant_grounding import (
9
+ generic_activity_mention_without_participant_role,
10
+ local_action_segment,
11
+ )
12
+
13
+ _LABEL_RE = r"[A-ZА-ЯЁ][A-Za-zА-Яа-яЁё._-]{1,39}"
14
+ _QUERY_LABEL_RE = r"[A-Za-zА-Яа-яЁё][A-Za-zА-Яа-яЁё._-]{1,39}"
15
+ _ACTION_VERB_RE = (
16
+ r"recommend(?:ed|s|ing)?|suggest(?:ed|s|ing)?|"
17
+ r"promise(?:d|s|ing)?|decid(?:e|ed|es|ing)|"
18
+ r"ask(?:ed|s|ing)?|assign(?:ed|s|ing)?|approv(?:e|ed|es|ing)|"
19
+ r"call(?:ed|s|ing)?|message(?:d|s|ing)?|text(?:ed|s|ing)?|"
20
+ r"help(?:ed|s|ing)?|assist(?:ed|s|ing)?|support(?:ed|s|ing)?|"
21
+ r"attend(?:ed|s|ing)?|participat(?:e|ed|es|ing)|"
22
+ r"join(?:ed|s|ing)?|meet|met|visit(?:ed|s|ing)?|"
23
+ r"introduc(?:e|ed|es|ing)|send|sent|give|gave|lend|lent|tell|told|"
24
+ r"рекомендовал(?:а)?|порекомендовал(?:а)?|посоветовал(?:а)?|"
25
+ r"пообещал(?:а)?|обещал(?:а)?|решил(?:а)?|"
26
+ r"спросил(?:а)?|назначил(?:а)?|одобрил(?:а)?|сказал(?:а)?|"
27
+ r"познакомил(?:а|и)?|представил(?:а|и)?|"
28
+ r"помог(?:ла|ли)?|поддержал(?:а|и)?"
29
+ )
30
+ _DIRECT_RECIPIENT_ACTION_VERB_RE = (
31
+ r"promise(?:d|s|ing)?|ask(?:ed|s|ing)?|assign(?:ed|s|ing)?|"
32
+ r"call(?:ed|s|ing)?|message(?:d|s|ing)?|text(?:ed|s|ing)?|"
33
+ r"help(?:ed|s|ing)?|assist(?:ed|s|ing)?|support(?:ed|s|ing)?|"
34
+ r"join(?:ed|s|ing)?|meet|met|visit(?:ed|s|ing)?|"
35
+ r"introduc(?:e|ed|es|ing)|send|sent|give|gave|lend|lent|tell|told|"
36
+ r"пообещал(?:а)?|обещал(?:а)?|спросил(?:а)?|назначил(?:а)?|"
37
+ r"сказал(?:а)?|познакомил(?:а|и)?|представил(?:а|и)?|"
38
+ r"помог(?:ла|ли)?|поддержал(?:а|и)?"
39
+ )
40
+ _PASSIVE_ACTION_VERB_RE = (
41
+ r"recommended|suggested|promised|asked|assigned|approved|"
42
+ r"called|messaged|texted|helped|assisted|supported|joined|met|visited|"
43
+ r"introduced|sent|given|lent|told"
44
+ )
45
+ _INFO_SOURCE_VERB_RE = (
46
+ r"hear(?:d|s|ing)?|learn(?:ed|s|ing)?|find\s+out|found\s+out|"
47
+ r"узнал(?:а|и)?|услышал(?:а|и)?"
48
+ )
49
+ _BORROW_SOURCE_VERB_RE = r"borrow(?:ed|s|ing)?"
50
+ _QUESTION_ACTION_RE = re.compile(
51
+ rf"\b(?:what\s+did\s+|did\s+|what\s+has\s+|has\s+)?"
52
+ rf"(?P<actor>{_QUERY_LABEL_RE})\s+"
53
+ rf"(?P<verb>{_ACTION_VERB_RE})\b",
54
+ re.IGNORECASE,
55
+ )
56
+ _WHO_TO_ACTION_RE = re.compile(
57
+ rf"\bwho\s+(?P<verb>{_ACTION_VERB_RE})\b"
58
+ rf"(?P<object>.{{0,120}}?)\b(?P<prep>to|for)\s+"
59
+ rf"(?P<recipient>{_QUERY_LABEL_RE})\b",
60
+ re.IGNORECASE,
61
+ )
62
+ _RU_WHO_OBJECT_TO_ACTION_RE = re.compile(
63
+ rf"\bкто\s+(?P<verb>{_ACTION_VERB_RE})\s+"
64
+ rf"(?P<object>{_QUERY_LABEL_RE})\s+\b(?:с|со)\s+"
65
+ rf"(?P<recipient>{_QUERY_LABEL_RE})\b",
66
+ re.IGNORECASE,
67
+ )
68
+ _WHO_DIRECT_RECIPIENT_ACTION_QUERY_RE = re.compile(
69
+ rf"\bwho\s+(?P<verb>{_DIRECT_RECIPIENT_ACTION_VERB_RE})\s+"
70
+ rf"(?P<recipient>{_QUERY_LABEL_RE})\b",
71
+ re.IGNORECASE | re.DOTALL,
72
+ )
73
+ _RU_WHO_DIRECT_RECIPIENT_ACTION_QUERY_RE = re.compile(
74
+ rf"\bкто\s+(?P<verb>{_DIRECT_RECIPIENT_ACTION_VERB_RE})\s+"
75
+ rf"(?P<recipient>{_QUERY_LABEL_RE})\b",
76
+ re.IGNORECASE | re.DOTALL,
77
+ )
78
+ _WHO_DID_ACTOR_ACTION_TO_QUERY_RE = re.compile(
79
+ rf"\b(?:who|whom)\s+did\s+(?P<actor>{_QUERY_LABEL_RE})\s+"
80
+ rf"(?P<verb>{_ACTION_VERB_RE})\b"
81
+ rf"(?P<object>.{{0,120}}?)\b(?:to|for)\b"
82
+ rf"(?=\s*(?:\?|$|in\b|during\b|after\b|before\b|on\b|at\b))",
83
+ re.IGNORECASE | re.DOTALL,
84
+ )
85
+ _WHO_DID_ACTOR_DIRECT_RECIPIENT_QUERY_RE = re.compile(
86
+ rf"\b(?:who|whom)\s+did\s+(?P<actor>{_QUERY_LABEL_RE})\s+"
87
+ rf"(?P<verb>{_DIRECT_RECIPIENT_ACTION_VERB_RE})\b"
88
+ rf"(?P<object>.{{0,120}}?)"
89
+ rf"(?=\?|$|\b(?:about|regarding|during|after|before|on|at|in|when|because)\b)",
90
+ re.IGNORECASE | re.DOTALL,
91
+ )
92
+ _WHO_DID_ACTOR_ACTION_WITH_QUERY_RE = re.compile(
93
+ rf"\b(?:who|whom)\s+did\s+(?P<actor>{_QUERY_LABEL_RE})\s+"
94
+ rf"(?P<verb>{_ACTION_VERB_RE})\b"
95
+ rf"(?P<object>.{{0,120}}?)\bwith\b"
96
+ rf"(?=\s*(?:\?|$|in\b|during\b|after\b|before\b|on\b|at\b))",
97
+ re.IGNORECASE | re.DOTALL,
98
+ )
99
+ _WHO_ACTION_WITH_RECIPIENT_QUERY_RE = re.compile(
100
+ rf"\bwho\s+(?P<verb>{_ACTION_VERB_RE})\b"
101
+ rf"(?P<object>.{{0,120}}?)\bwith\s+(?P<recipient>{_QUERY_LABEL_RE})\b",
102
+ re.IGNORECASE | re.DOTALL,
103
+ )
104
+ _WHO_ACTOR_ACTION_QUERY_RE = re.compile(
105
+ rf"\bwho\s+(?P<verb>{_ACTION_VERB_RE})\b(?P<object>.{{0,160}})",
106
+ re.IGNORECASE | re.DOTALL,
107
+ )
108
+ _TO_WHOM_DID_ACTOR_ACTION_QUERY_RE = re.compile(
109
+ rf"\b(?:to|for)\s+whom\s+did\s+(?P<actor>{_QUERY_LABEL_RE})\s+"
110
+ rf"(?P<verb>{_ACTION_VERB_RE})\b",
111
+ re.IGNORECASE | re.DOTALL,
112
+ )
113
+ _WHO_WAS_ACTIONED_BY_ACTOR_QUERY_RE = re.compile(
114
+ rf"\bwho\s+(?:was|were)\s+(?P<verb>{_PASSIVE_ACTION_VERB_RE})\b"
115
+ rf"(?P<object>.{{0,140}}?)\bby\s+(?P<actor>{_QUERY_LABEL_RE})\b",
116
+ re.IGNORECASE | re.DOTALL,
117
+ )
118
+ _WHO_WAS_RECIPIENT_ACTIONED_BY_QUERY_RE = re.compile(
119
+ rf"\bwho\s+(?:was|were)\s+(?P<recipient>{_QUERY_LABEL_RE})\s+"
120
+ rf"(?P<verb>{_PASSIVE_ACTION_VERB_RE})\b"
121
+ rf"(?P<object>.{{0,140}}?)\bby\b",
122
+ re.IGNORECASE | re.DOTALL,
123
+ )
124
+ _WHO_DID_ACTOR_INFO_SOURCE_QUERY_RE = re.compile(
125
+ rf"\b(?:who|whom)\s+did\s+(?P<actor>{_QUERY_LABEL_RE})\s+"
126
+ rf"(?P<verb>{_INFO_SOURCE_VERB_RE})\b"
127
+ rf"(?P<object>.{{0,160}}?)\bfrom\b"
128
+ rf"(?=\s*(?:\?|$|in\b|during\b|after\b|before\b|on\b|at\b))",
129
+ re.IGNORECASE | re.DOTALL,
130
+ )
131
+ _FROM_WHOM_DID_ACTOR_INFO_SOURCE_QUERY_RE = re.compile(
132
+ rf"\bfrom\s+whom\s+did\s+(?P<actor>{_QUERY_LABEL_RE})\s+"
133
+ rf"(?P<verb>{_INFO_SOURCE_VERB_RE})\b",
134
+ re.IGNORECASE | re.DOTALL,
135
+ )
136
+ _RU_FROM_WHOM_ACTOR_INFO_SOURCE_QUERY_RE = re.compile(
137
+ rf"\bот\s+кого\s+(?P<actor>{_QUERY_LABEL_RE})\s+"
138
+ rf"(?P<verb>{_INFO_SOURCE_VERB_RE})\b",
139
+ re.IGNORECASE | re.DOTALL,
140
+ )
141
+ _WHO_DID_ACTOR_BORROW_FROM_QUERY_RE = re.compile(
142
+ rf"\b(?:who|whom)\s+did\s+(?P<actor>{_QUERY_LABEL_RE})\s+"
143
+ rf"(?P<verb>{_BORROW_SOURCE_VERB_RE})\b"
144
+ rf"(?P<object>.{{0,160}}?)\bfrom\b"
145
+ rf"(?=\s*(?:\?|$|in\b|during\b|after\b|before\b|on\b|at\b))",
146
+ re.IGNORECASE | re.DOTALL,
147
+ )
148
+ _FROM_WHOM_DID_ACTOR_BORROW_QUERY_RE = re.compile(
149
+ rf"\bfrom\s+whom\s+did\s+(?P<actor>{_QUERY_LABEL_RE})\s+"
150
+ rf"(?P<verb>{_BORROW_SOURCE_VERB_RE})\b",
151
+ re.IGNORECASE | re.DOTALL,
152
+ )
153
+ _WHO_WAS_THERE_FOR_QUERY_RE = re.compile(
154
+ rf"\bwho\s+(?:is|was|were|has\s+been|have\s+been|'s)\s+"
155
+ rf"there\s+for\s+(?P<recipient>{_QUERY_LABEL_RE})\b",
156
+ re.IGNORECASE | re.DOTALL,
157
+ )
158
+ _RU_WHO_WAS_THERE_FOR_QUERY_RE = re.compile(
159
+ rf"\bкто\s+(?:(?:был|была|были)\s+)?рядом\s+с\s+"
160
+ rf"(?P<recipient>{_QUERY_LABEL_RE})\b",
161
+ re.IGNORECASE | re.DOTALL,
162
+ )
163
+ _NOMINAL_ACTION_QUERY_RE = re.compile(
164
+ r"\b(?:what|which)\s+"
165
+ r"(?P<noun>decision|promise|recommendation|suggestion)\s+"
166
+ rf"did\s+(?P<actor>{_QUERY_LABEL_RE})\s+(?:make|give|offer)\b"
167
+ r"(?P<tail>.{0,120})",
168
+ re.IGNORECASE | re.DOTALL,
169
+ )
170
+ _OWNER_RESPONSIBILITY_QUERY_RE = re.compile(
171
+ rf"\b(?:(?:who\s+(?:is|was|'s)\s+(?:responsible|(?:the\s+)?owner)|who\s+owns)"
172
+ rf"|(?:is|was)\s+(?P<owner_after>{_QUERY_LABEL_RE})\s+responsible"
173
+ rf"|(?P<owner_before>{_QUERY_LABEL_RE})\s+"
174
+ rf"(?:is|was|'s)\s+(?:responsible|(?:the\s+)?owner)"
175
+ rf"|(?P<owner_owns>{_QUERY_LABEL_RE})\s+owns?)\b",
176
+ re.IGNORECASE,
177
+ )
178
+ _SUGGESTION_SOURCE_QUERY_RE = re.compile(
179
+ rf"\b(?P<recipient>{_QUERY_LABEL_RE})\s+"
180
+ r"(?:read|watched|tried|bought|used|visited|listened|started|played|made|ate)\b"
181
+ r".{0,120}\b(?:from|based\s+on|because\s+of|after)\s+"
182
+ rf"(?P<actor>{_QUERY_LABEL_RE})(?:'s|s')?\s+"
183
+ r"(?:suggestion|recommendation|advice)\b",
184
+ re.IGNORECASE | re.DOTALL,
185
+ )
186
+ _RECIPIENT_FOLLOWUP_ACTION_RE = (
187
+ r"read|watch(?:ed)?|try|tried|buy|bought|use|used|visit(?:ed)?|"
188
+ r"listen(?:ed)?|start(?:ed)?|play(?:ed)?|make|made|eat|ate"
189
+ )
190
+ _WHO_RECOMMENDED_THAT_RECIPIENT_QUERY_RE = re.compile(
191
+ r"\bwho\s+"
192
+ r"(?P<verb>recommend(?:ed|s|ing)?|suggest(?:ed|s|ing)?)\s+"
193
+ rf"(?:that\s+)?(?P<recipient>{_QUERY_LABEL_RE})\s+"
194
+ rf"(?:{_RECIPIENT_FOLLOWUP_ACTION_RE})\b",
195
+ re.IGNORECASE | re.DOTALL,
196
+ )
197
+ _WHAT_DID_ACTOR_RECOMMEND_RECIPIENT_ACTION_QUERY_RE = re.compile(
198
+ r"\b(?:what|which)(?:\s+\w+){0,4}\s+did\s+"
199
+ rf"(?P<actor>{_QUERY_LABEL_RE})\s+"
200
+ r"(?P<verb>recommend(?:ed|s|ing)?|suggest(?:ed|s|ing)?)\s+"
201
+ rf"(?:that\s+)?(?P<recipient>{_QUERY_LABEL_RE})\s+"
202
+ rf"(?:{_RECIPIENT_FOLLOWUP_ACTION_RE})\b",
203
+ re.IGNORECASE | re.DOTALL,
204
+ )
205
+ _RECIPIENT_AFTER_ACTOR_RECOMMENDATION_QUERY_RE = re.compile(
206
+ rf"\b(?P<recipient>{_QUERY_LABEL_RE})\s+"
207
+ r"(?:read|watched|tried|bought|used|visited|listened|started|played|made|ate)\b"
208
+ r".{0,120}\b(?:after|because|since|when)\s+"
209
+ rf"(?P<actor>{_QUERY_LABEL_RE})\s+"
210
+ r"(?P<verb>recommend(?:ed|s|ing)?|suggest(?:ed|s|ing)?)\b",
211
+ re.IGNORECASE | re.DOTALL,
212
+ )
213
+ _WHOSE_SUGGESTION_QUERY_RE = re.compile(
214
+ r"\bwhose\s+(?:suggestion|recommendation|advice)\s+did\s+"
215
+ rf"(?P<recipient>{_QUERY_LABEL_RE})\s+"
216
+ r"(?:follow|take|use|read|watch|try|buy|visit|listen|start|play|make|eat)\b",
217
+ re.IGNORECASE,
218
+ )
219
+ _WHO_GAVE_SUGGESTION_TO_QUERY_RE = re.compile(
220
+ r"\bwho\s+(?:gave|offered|made)\b"
221
+ r".{0,80}\b(?:suggestion|recommendation|advice)\b"
222
+ rf".{{0,80}}\b(?:to|for)\s+(?P<recipient>{_QUERY_LABEL_RE})\b",
223
+ re.IGNORECASE | re.DOTALL,
224
+ )
225
+ _WHO_GAVE_DIRECT_SUGGESTION_QUERY_RE = re.compile(
226
+ r"\bwho\s+(?:gave|offered|made)\s+"
227
+ rf"(?P<recipient>{_QUERY_LABEL_RE})\b"
228
+ r".{0,80}\b(?:suggestion|recommendation|advice)\b",
229
+ re.IGNORECASE | re.DOTALL,
230
+ )
231
+ _RU_WHO_DIRECT_RECOMMENDATION_QUERY_RE = re.compile(
232
+ r"\bкто\s+"
233
+ r"(?P<verb>рекомендовал(?:а)?|порекомендовал(?:а)?|посоветовал(?:а)?)\s+"
234
+ rf"(?P<recipient>{_QUERY_LABEL_RE})\b",
235
+ re.IGNORECASE | re.DOTALL,
236
+ )
237
+ _RU_WHOSE_SUGGESTION_QUERY_RE = re.compile(
238
+ r"\bпо\s+чь(?:ему|ей|им)\s+"
239
+ r"(?:совет\w*|рекомендац\w*)\s+"
240
+ rf"(?P<recipient>{_QUERY_LABEL_RE})\s+"
241
+ r"(?:прочитал(?:а|и)?|посмотрел(?:а|и)?|попробовал(?:а|и)?|"
242
+ r"использовал(?:а|и)?|купил(?:а|и)?|посетил(?:а|и)?|начал(?:а|и)?)\b",
243
+ re.IGNORECASE | re.DOTALL,
244
+ )
245
+ _RU_TO_WHOM_ACTOR_ACTION_QUERY_RE = re.compile(
246
+ rf"\bкому\s+(?P<actor>{_QUERY_LABEL_RE})\s+"
247
+ rf"(?P<verb>{_ACTION_VERB_RE})\b",
248
+ re.IGNORECASE | re.DOTALL,
249
+ )
250
+ _REPORTED_SUBJECT_SHIFT_RE = re.compile(
251
+ rf"\b(?:heard|learned|mentioned|reported|said|told|wrote)\b"
252
+ rf".{{0,80}}\b{_LABEL_RE}\W*$",
253
+ re.IGNORECASE | re.DOTALL,
254
+ )
255
+ _NEGATED_ACTION_GAP_RE = re.compile(
256
+ r"\b(?:did\s+not|didn't|does\s+not|doesn't|never|not|"
257
+ r"cannot|can\s+not|can't|could\s+not|couldn't|would\s+not|wouldn't|"
258
+ r"no\s+longer)\b|"
259
+ r"\b(?:не|никогда\s+не)\b",
260
+ re.IGNORECASE | re.DOTALL,
261
+ )
262
+ _NEGATION_CANCEL_RE = re.compile(
263
+ r"\bnot\s+only\b|\bне\s+только\b",
264
+ re.IGNORECASE | re.DOTALL,
265
+ )
266
+ _QUERY_LABEL_STOP_WORDS = frozenset(
267
+ {
268
+ "did",
269
+ "has",
270
+ "what",
271
+ "who",
272
+ "whom",
273
+ "whose",
274
+ "anybody",
275
+ "anyone",
276
+ "about",
277
+ "buy",
278
+ "people",
279
+ "person",
280
+ "eat",
281
+ "follow",
282
+ "for",
283
+ "in",
284
+ "listen",
285
+ "last",
286
+ "latest",
287
+ "make",
288
+ "newest",
289
+ "play",
290
+ "read",
291
+ "recent",
292
+ "somebody",
293
+ "someone",
294
+ "start",
295
+ "take",
296
+ "that",
297
+ "the",
298
+ "to",
299
+ "try",
300
+ "use",
301
+ "visit",
302
+ "watch",
303
+ "with",
304
+ "что",
305
+ "кто",
306
+ "кого",
307
+ "кому",
308
+ }
309
+ )
310
+ _TEXT_LABEL_STOP_WORDS = _QUERY_LABEL_STOP_WORDS.union(
311
+ {
312
+ "project",
313
+ "transcript",
314
+ }
315
+ )
316
+ _ACTION_ROLE_MATCH_BOOST = 0.024
317
+ _ACTION_ROLE_REQUESTED_RECIPIENT_EVIDENCE_BOOST = 0.028
318
+ _ACTION_ROLE_ACTOR_MATCH_BOOST = 0.018
319
+ _ACTION_ROLE_RECIPIENT_MATCH_BOOST = 0.016
320
+ _ACTION_ROLE_OWNER_EVIDENCE_BOOST = 0.014
321
+ _ACTION_ROLE_MISMATCH_PENALTY = 0.034
322
+ _ACTION_ROLE_RECIPIENT_MISMATCH_PENALTY = 0.028
323
+ _ACTION_ROLE_REQUESTED_RECIPIENT_MISSING_PENALTY = 0.024
324
+ _DIRECT_RECIPIENT_VERBS = frozenset(
325
+ {
326
+ "ask",
327
+ "assign",
328
+ "call",
329
+ "give",
330
+ "help",
331
+ "introduce",
332
+ "join",
333
+ "lend",
334
+ "meet",
335
+ "message",
336
+ "promise",
337
+ "send",
338
+ "tell",
339
+ "visit",
340
+ }
341
+ )
342
+ _COMPANION_ACTION_VERBS = frozenset({"attend", "help", "join", "meet", "visit"})
343
+ _DIRECT_RECIPIENT_OBJECT_STOP_WORDS = frozenset(
344
+ {
345
+ "a",
346
+ "an",
347
+ "badge",
348
+ "book",
349
+ "camera",
350
+ "car",
351
+ "charger",
352
+ "decision",
353
+ "doc",
354
+ "document",
355
+ "email",
356
+ "file",
357
+ "image",
358
+ "invoice",
359
+ "link",
360
+ "laptop",
361
+ "message",
362
+ "note",
363
+ "notes",
364
+ "photo",
365
+ "plan",
366
+ "report",
367
+ "screenshot",
368
+ "story",
369
+ "task",
370
+ "the",
371
+ "ticket",
372
+ "to",
373
+ "update",
374
+ "wallet",
375
+ }
376
+ )
377
+ _ACTION_CONTEXT_STOP_WORDS = frozenset(
378
+ {
379
+ "a",
380
+ "about",
381
+ "after",
382
+ "an",
383
+ "and",
384
+ "at",
385
+ "before",
386
+ "did",
387
+ "during",
388
+ "for",
389
+ "from",
390
+ "in",
391
+ "on",
392
+ "the",
393
+ "to",
394
+ "with",
395
+ "who",
396
+ "whom",
397
+ "что",
398
+ "кто",
399
+ "кого",
400
+ "кому",
401
+ "на",
402
+ "о",
403
+ "об",
404
+ "от",
405
+ "по",
406
+ "после",
407
+ "про",
408
+ "с",
409
+ "со",
410
+ }
411
+ )
412
+ _NON_RECIPIENT_ACTION_OBJECT_WORDS = frozenset(
413
+ {
414
+ "advice",
415
+ "approval",
416
+ "assistance",
417
+ "book",
418
+ "bring",
419
+ "buy",
420
+ "call",
421
+ "email",
422
+ "feedback",
423
+ "file",
424
+ "help",
425
+ "join",
426
+ "make",
427
+ "permission",
428
+ "review",
429
+ "schedule",
430
+ "send",
431
+ "share",
432
+ "submit",
433
+ "support",
434
+ "write",
435
+ }
436
+ )
437
+
438
+
439
+ @dataclass(frozen=True)
440
+ class ActionRoleRerankSignal:
441
+ boost: float = 0.0
442
+ penalty: float = 0.0
443
+ reason: str = ""
444
+
445
+
446
+ @dataclass(frozen=True)
447
+ class _ActionRoleQuery:
448
+ verb_key: str
449
+ actor_label: str = ""
450
+ recipient_label: str = ""
451
+ object_label: str = ""
452
+ context_terms: tuple[str, ...] = ()
453
+ owner_label: str = ""
454
+ owner_requested: bool = False
455
+ recipient_requested: bool = False
456
+ source_requested: bool = False
457
+ companion_requested: bool = False
458
+ actor_requested: bool = False
459
+
460
+
461
+ def action_role_rerank_signal(*, query: str, text: str) -> ActionRoleRerankSignal:
462
+ """Return bounded role-order signal for action questions.
463
+
464
+ This intentionally treats provider/output text as evidence only: it adjusts ranking,
465
+ but never rewrites or asserts canonical facts.
466
+ """
467
+
468
+ role_query = _action_role_query(query)
469
+ if role_query is None:
470
+ return ActionRoleRerankSignal()
471
+ if role_query.source_requested:
472
+ if role_query.verb_key == "borrow":
473
+ return _borrow_source_signal(role_query, text)
474
+ return _information_source_signal(role_query, text)
475
+ if role_query.companion_requested:
476
+ return _companion_action_signal(role_query, text)
477
+ if role_query.owner_requested:
478
+ return _owner_responsibility_signal(role_query, text)
479
+ if role_query.actor_requested:
480
+ return _actor_evidence_signal(role_query, text)
481
+ if role_query.actor_label:
482
+ return _actor_action_signal(role_query, text)
483
+ if role_query.recipient_label:
484
+ return _recipient_action_signal(role_query, text)
485
+ return ActionRoleRerankSignal()
486
+
487
+
488
+ def _action_role_query(query: str) -> _ActionRoleQuery | None:
489
+ owner_match = _OWNER_RESPONSIBILITY_QUERY_RE.search(query)
490
+ if owner_match is not None:
491
+ owner = _clean_label(
492
+ owner_match.group("owner_after")
493
+ or owner_match.group("owner_before")
494
+ or owner_match.group("owner_owns")
495
+ or ""
496
+ )
497
+ return _ActionRoleQuery(
498
+ verb_key="owner",
499
+ owner_label=owner,
500
+ owner_requested=True,
501
+ )
502
+
503
+ for pattern in (_WHO_WAS_THERE_FOR_QUERY_RE, _RU_WHO_WAS_THERE_FOR_QUERY_RE):
504
+ match = pattern.search(query)
505
+ if match is not None:
506
+ recipient = _clean_label(match.group("recipient"))
507
+ if recipient:
508
+ return _ActionRoleQuery(
509
+ verb_key="support_presence",
510
+ recipient_label=recipient,
511
+ )
512
+
513
+ match = _WHO_ACTION_WITH_RECIPIENT_QUERY_RE.search(query)
514
+ if match is not None:
515
+ recipient = _clean_label(match.group("recipient"))
516
+ verb_key = _canonical_verb_key(match.group("verb"))
517
+ context_terms = _action_context_terms(
518
+ match.groupdict().get("object") or "",
519
+ verb_key=verb_key,
520
+ )
521
+ if recipient and verb_key in _COMPANION_ACTION_VERBS:
522
+ return _ActionRoleQuery(
523
+ verb_key=verb_key,
524
+ recipient_label=recipient,
525
+ context_terms=context_terms,
526
+ companion_requested=True,
527
+ )
528
+
529
+ for pattern in (_WHO_TO_ACTION_RE, _RU_WHO_OBJECT_TO_ACTION_RE):
530
+ match = pattern.search(query)
531
+ if match is not None:
532
+ verb_key = _canonical_verb_key(match.group("verb"))
533
+ if pattern is _RU_WHO_OBJECT_TO_ACTION_RE and verb_key != "introduce":
534
+ continue
535
+ if (
536
+ pattern is _WHO_TO_ACTION_RE
537
+ and _who_to_action_match_targets_non_recipient(
538
+ match,
539
+ verb_key=verb_key,
540
+ )
541
+ ):
542
+ continue
543
+ recipient = _clean_label(match.group("recipient"))
544
+ object_label = _object_label_in_text(match.group("object") or "")
545
+ if verb_key and recipient:
546
+ return _ActionRoleQuery(
547
+ verb_key=verb_key,
548
+ recipient_label=recipient,
549
+ object_label=object_label if verb_key == "introduce" else "",
550
+ )
551
+
552
+ match = _WHOSE_SUGGESTION_QUERY_RE.search(query)
553
+ if match is not None:
554
+ recipient = _clean_label(match.group("recipient"))
555
+ if recipient:
556
+ return _ActionRoleQuery(
557
+ verb_key="recommend",
558
+ recipient_label=recipient,
559
+ )
560
+
561
+ for pattern in (
562
+ _WHO_GAVE_SUGGESTION_TO_QUERY_RE,
563
+ _WHO_GAVE_DIRECT_SUGGESTION_QUERY_RE,
564
+ ):
565
+ match = pattern.search(query)
566
+ if match is not None:
567
+ recipient = _clean_label(match.group("recipient"))
568
+ if recipient:
569
+ return _ActionRoleQuery(
570
+ verb_key="recommend",
571
+ recipient_label=recipient,
572
+ )
573
+
574
+ match = _WHO_RECOMMENDED_THAT_RECIPIENT_QUERY_RE.search(query)
575
+ if match is not None:
576
+ recipient = _clean_label(match.group("recipient"))
577
+ if recipient:
578
+ return _ActionRoleQuery(
579
+ verb_key="recommend",
580
+ recipient_label=recipient,
581
+ )
582
+
583
+ match = _WHAT_DID_ACTOR_RECOMMEND_RECIPIENT_ACTION_QUERY_RE.search(query)
584
+ if match is not None:
585
+ actor = _clean_label(match.group("actor"))
586
+ recipient = _clean_label(match.group("recipient"))
587
+ if actor and recipient:
588
+ return _ActionRoleQuery(
589
+ verb_key="recommend",
590
+ actor_label=actor,
591
+ recipient_label=recipient,
592
+ )
593
+
594
+ match = _RU_WHO_DIRECT_RECOMMENDATION_QUERY_RE.search(query)
595
+ if match is not None:
596
+ recipient = _clean_label(match.group("recipient"))
597
+ if recipient:
598
+ return _ActionRoleQuery(
599
+ verb_key="recommend",
600
+ recipient_label=recipient,
601
+ )
602
+
603
+ match = _RU_WHOSE_SUGGESTION_QUERY_RE.search(query)
604
+ if match is not None:
605
+ recipient = _clean_label(match.group("recipient"))
606
+ if recipient:
607
+ return _ActionRoleQuery(
608
+ verb_key="recommend",
609
+ recipient_label=recipient,
610
+ )
611
+
612
+ match = _WHO_DID_ACTOR_ACTION_WITH_QUERY_RE.search(query)
613
+ if match is not None:
614
+ actor = _clean_label(match.group("actor"))
615
+ verb_key = _canonical_verb_key(match.group("verb"))
616
+ context_terms = _action_context_terms(
617
+ match.groupdict().get("object") or "",
618
+ verb_key=verb_key,
619
+ )
620
+ if actor and verb_key in _COMPANION_ACTION_VERBS:
621
+ return _ActionRoleQuery(
622
+ verb_key=verb_key,
623
+ actor_label=actor,
624
+ context_terms=context_terms,
625
+ companion_requested=True,
626
+ )
627
+
628
+ for pattern in (
629
+ _WHO_DIRECT_RECIPIENT_ACTION_QUERY_RE,
630
+ _RU_WHO_DIRECT_RECIPIENT_ACTION_QUERY_RE,
631
+ ):
632
+ match = pattern.search(query)
633
+ if match is not None:
634
+ verb_key = _canonical_verb_key(match.group("verb"))
635
+ recipient = _clean_label(match.group("recipient"))
636
+ if verb_key and recipient:
637
+ return _ActionRoleQuery(verb_key=verb_key, recipient_label=recipient)
638
+
639
+ match = _WHO_ACTOR_ACTION_QUERY_RE.search(query)
640
+ if match is not None:
641
+ verb_key = _canonical_verb_key(match.group("verb"))
642
+ context_terms = _action_context_terms(
643
+ match.groupdict().get("object") or "",
644
+ verb_key=verb_key,
645
+ )
646
+ if verb_key and context_terms:
647
+ return _ActionRoleQuery(
648
+ verb_key=verb_key,
649
+ context_terms=context_terms,
650
+ actor_requested=True,
651
+ )
652
+
653
+ for pattern in (
654
+ _SUGGESTION_SOURCE_QUERY_RE,
655
+ _RECIPIENT_AFTER_ACTOR_RECOMMENDATION_QUERY_RE,
656
+ ):
657
+ match = pattern.search(query)
658
+ if match is not None:
659
+ actor = _clean_label(match.group("actor"))
660
+ recipient = _clean_label(match.group("recipient"))
661
+ if actor and recipient:
662
+ return _ActionRoleQuery(
663
+ verb_key="recommend",
664
+ actor_label=actor,
665
+ recipient_label=recipient,
666
+ )
667
+
668
+ match = _WHO_WAS_ACTIONED_BY_ACTOR_QUERY_RE.search(query)
669
+ if match is not None:
670
+ actor = _clean_label(match.group("actor"))
671
+ verb_key = _canonical_verb_key(match.group("verb"))
672
+ context_terms = _action_context_terms(
673
+ match.groupdict().get("object") or "",
674
+ verb_key=verb_key,
675
+ )
676
+ if actor and verb_key:
677
+ return _ActionRoleQuery(
678
+ verb_key=verb_key,
679
+ actor_label=actor,
680
+ context_terms=context_terms,
681
+ recipient_requested=True,
682
+ )
683
+
684
+ match = _WHO_WAS_RECIPIENT_ACTIONED_BY_QUERY_RE.search(query)
685
+ if match is not None:
686
+ recipient = _clean_label(match.group("recipient"))
687
+ verb_key = _canonical_verb_key(match.group("verb"))
688
+ if recipient and verb_key:
689
+ return _ActionRoleQuery(
690
+ verb_key=verb_key,
691
+ recipient_label=recipient,
692
+ )
693
+
694
+ for pattern in (
695
+ _WHO_DID_ACTOR_INFO_SOURCE_QUERY_RE,
696
+ _FROM_WHOM_DID_ACTOR_INFO_SOURCE_QUERY_RE,
697
+ _RU_FROM_WHOM_ACTOR_INFO_SOURCE_QUERY_RE,
698
+ ):
699
+ match = pattern.search(query)
700
+ if match is not None:
701
+ actor = _clean_label(match.group("actor"))
702
+ if actor:
703
+ return _ActionRoleQuery(
704
+ verb_key="information_source",
705
+ actor_label=actor,
706
+ source_requested=True,
707
+ )
708
+
709
+ for pattern in (
710
+ _WHO_DID_ACTOR_BORROW_FROM_QUERY_RE,
711
+ _FROM_WHOM_DID_ACTOR_BORROW_QUERY_RE,
712
+ ):
713
+ match = pattern.search(query)
714
+ if match is not None:
715
+ actor = _clean_label(match.group("actor"))
716
+ context_terms = _action_context_terms(
717
+ match.groupdict().get("object") or "",
718
+ verb_key="borrow",
719
+ )
720
+ if actor:
721
+ return _ActionRoleQuery(
722
+ verb_key="borrow",
723
+ actor_label=actor,
724
+ context_terms=context_terms,
725
+ source_requested=True,
726
+ )
727
+
728
+ for pattern in (
729
+ _WHO_DID_ACTOR_ACTION_TO_QUERY_RE,
730
+ _WHO_DID_ACTOR_DIRECT_RECIPIENT_QUERY_RE,
731
+ _TO_WHOM_DID_ACTOR_ACTION_QUERY_RE,
732
+ _RU_TO_WHOM_ACTOR_ACTION_QUERY_RE,
733
+ ):
734
+ match = pattern.search(query)
735
+ if match is not None:
736
+ actor = _clean_label(match.group("actor"))
737
+ verb_key = _canonical_verb_key(match.group("verb"))
738
+ object_label = _object_label_in_text(match.groupdict().get("object") or "")
739
+ context_terms = _action_context_terms(
740
+ match.groupdict().get("object") or "",
741
+ verb_key=verb_key,
742
+ )
743
+ if actor and verb_key:
744
+ return _ActionRoleQuery(
745
+ verb_key=verb_key,
746
+ actor_label=actor,
747
+ object_label=object_label if verb_key == "introduce" else "",
748
+ context_terms=() if verb_key == "introduce" else context_terms,
749
+ recipient_requested=True,
750
+ )
751
+
752
+ match = _NOMINAL_ACTION_QUERY_RE.search(query)
753
+ if match is not None:
754
+ actor = _clean_label(match.group("actor"))
755
+ verb_key = _canonical_nominal_action(match.group("noun"))
756
+ recipient = _recipient_in_tail(match.group("tail") or "")
757
+ if actor and verb_key:
758
+ return _ActionRoleQuery(
759
+ verb_key=verb_key,
760
+ actor_label=actor,
761
+ recipient_label=recipient,
762
+ )
763
+
764
+ match = _QUESTION_ACTION_RE.search(query)
765
+ if match is None:
766
+ return None
767
+ actor = _clean_label(match.group("actor"))
768
+ verb_key = _canonical_verb_key(match.group("verb"))
769
+ if not actor or not verb_key:
770
+ return None
771
+ recipient = _recipient_after_action(
772
+ query,
773
+ match_end=match.end(),
774
+ verb_key=verb_key,
775
+ )
776
+ return _ActionRoleQuery(
777
+ verb_key=verb_key,
778
+ actor_label=actor,
779
+ recipient_label=recipient,
780
+ )
781
+
782
+
783
+ def _who_to_action_match_targets_non_recipient(
784
+ match: re.Match[str],
785
+ *,
786
+ verb_key: str,
787
+ ) -> bool:
788
+ if verb_key not in _DIRECT_RECIPIENT_VERBS:
789
+ return False
790
+ if (match.groupdict().get("prep") or "").casefold() not in {"to", "for"}:
791
+ return False
792
+ recipient = (match.group("recipient") or "").casefold()
793
+ return recipient in _NON_RECIPIENT_ACTION_OBJECT_WORDS
794
+
795
+
796
+ def _actor_action_signal(
797
+ role_query: _ActionRoleQuery,
798
+ text: str,
799
+ ) -> ActionRoleRerankSignal:
800
+ actor = role_query.actor_label
801
+ recipient = role_query.recipient_label
802
+ verb_key = role_query.verb_key
803
+ object_label = role_query.object_label
804
+ context_terms = role_query.context_terms
805
+ if (
806
+ verb_key == "meet"
807
+ and role_query.recipient_requested
808
+ and (
809
+ _has_actor_action_to_any_recipient(
810
+ text,
811
+ actor=actor,
812
+ verb_key=verb_key,
813
+ )
814
+ or _has_action_to_recipient(text, recipient=actor, verb_key=verb_key)
815
+ )
816
+ ):
817
+ return ActionRoleRerankSignal(
818
+ boost=_ACTION_ROLE_REQUESTED_RECIPIENT_EVIDENCE_BOOST,
819
+ reason="action_role_actor_to_recipient_evidence",
820
+ )
821
+ if _has_negated_actor_action(
822
+ text,
823
+ actor=actor,
824
+ verb_key=verb_key,
825
+ target=recipient or object_label,
826
+ recipient_requested=role_query.recipient_requested,
827
+ context_terms=context_terms,
828
+ ):
829
+ return ActionRoleRerankSignal(
830
+ penalty=_ACTION_ROLE_MISMATCH_PENALTY,
831
+ reason="action_role_negated_evidence",
832
+ )
833
+ if (
834
+ recipient
835
+ and object_label
836
+ and _has_ordered_action_object_to_recipient(
837
+ text,
838
+ verb_key=verb_key,
839
+ object_label=object_label,
840
+ recipient=recipient,
841
+ )
842
+ ):
843
+ return ActionRoleRerankSignal(
844
+ boost=_ACTION_ROLE_MATCH_BOOST,
845
+ reason="action_role_actor_recipient_match",
846
+ )
847
+ if (
848
+ recipient
849
+ and object_label
850
+ and _has_ordered_action_object_to_recipient(
851
+ text,
852
+ verb_key=verb_key,
853
+ object_label=recipient,
854
+ recipient=object_label,
855
+ )
856
+ ):
857
+ return ActionRoleRerankSignal(
858
+ penalty=_ACTION_ROLE_MISMATCH_PENALTY,
859
+ reason="action_role_actor_recipient_reversed",
860
+ )
861
+ if recipient and _has_ordered_action(text, actor=actor, verb_key=verb_key, target=recipient):
862
+ return ActionRoleRerankSignal(
863
+ boost=_ACTION_ROLE_MATCH_BOOST,
864
+ reason="action_role_actor_recipient_match",
865
+ )
866
+ if recipient and _has_ordered_action(text, actor=recipient, verb_key=verb_key, target=actor):
867
+ return ActionRoleRerankSignal(
868
+ penalty=_ACTION_ROLE_MISMATCH_PENALTY,
869
+ reason="action_role_actor_recipient_reversed",
870
+ )
871
+ if object_label and role_query.recipient_requested:
872
+ if _has_actor_action_object_to_any_recipient(
873
+ text,
874
+ actor=actor,
875
+ verb_key=verb_key,
876
+ object_label=object_label,
877
+ ):
878
+ return ActionRoleRerankSignal(
879
+ boost=_ACTION_ROLE_REQUESTED_RECIPIENT_EVIDENCE_BOOST,
880
+ reason="action_role_actor_to_recipient_evidence",
881
+ )
882
+ if _has_actor_action(text, actor=actor, verb_key=verb_key):
883
+ return ActionRoleRerankSignal(
884
+ penalty=_ACTION_ROLE_REQUESTED_RECIPIENT_MISSING_PENALTY,
885
+ reason="action_role_requested_recipient_missing",
886
+ )
887
+ if context_terms and role_query.recipient_requested:
888
+ if _has_actor_action_to_any_recipient_with_context(
889
+ text,
890
+ actor=actor,
891
+ verb_key=verb_key,
892
+ context_terms=context_terms,
893
+ ):
894
+ return ActionRoleRerankSignal(
895
+ boost=_ACTION_ROLE_REQUESTED_RECIPIENT_EVIDENCE_BOOST,
896
+ reason="action_role_actor_to_recipient_evidence",
897
+ )
898
+ if _has_actor_action_to_any_recipient(
899
+ text,
900
+ actor=actor,
901
+ verb_key=verb_key,
902
+ ):
903
+ return ActionRoleRerankSignal(
904
+ penalty=_ACTION_ROLE_REQUESTED_RECIPIENT_MISSING_PENALTY,
905
+ reason="action_role_requested_context_mismatch",
906
+ )
907
+ if role_query.recipient_requested and _has_actor_action_to_any_recipient(
908
+ text,
909
+ actor=actor,
910
+ verb_key=verb_key,
911
+ ):
912
+ return ActionRoleRerankSignal(
913
+ boost=_ACTION_ROLE_REQUESTED_RECIPIENT_EVIDENCE_BOOST,
914
+ reason="action_role_actor_to_recipient_evidence",
915
+ )
916
+ if _has_actor_action(text, actor=actor, verb_key=verb_key):
917
+ if role_query.recipient_requested:
918
+ return ActionRoleRerankSignal(
919
+ penalty=_ACTION_ROLE_REQUESTED_RECIPIENT_MISSING_PENALTY,
920
+ reason="action_role_requested_recipient_missing",
921
+ )
922
+ return ActionRoleRerankSignal(
923
+ boost=_ACTION_ROLE_ACTOR_MATCH_BOOST,
924
+ reason="action_role_actor_match",
925
+ )
926
+ if _has_non_actor_action(text, expected_actor=actor, verb_key=verb_key):
927
+ return ActionRoleRerankSignal(
928
+ penalty=_ACTION_ROLE_MISMATCH_PENALTY,
929
+ reason="action_role_actor_mismatch",
930
+ )
931
+ return ActionRoleRerankSignal()
932
+
933
+
934
+ def _recipient_action_signal(
935
+ role_query: _ActionRoleQuery,
936
+ text: str,
937
+ ) -> ActionRoleRerankSignal:
938
+ recipient = role_query.recipient_label
939
+ verb_key = role_query.verb_key
940
+ object_label = role_query.object_label
941
+ if verb_key == "support_presence":
942
+ return _support_presence_signal(recipient=recipient, text=text)
943
+ if _has_negated_action_to_recipient(
944
+ text,
945
+ recipient=recipient,
946
+ verb_key=verb_key,
947
+ object_label=object_label,
948
+ ):
949
+ return ActionRoleRerankSignal(
950
+ penalty=_ACTION_ROLE_RECIPIENT_MISMATCH_PENALTY,
951
+ reason="action_role_negated_evidence",
952
+ )
953
+ if object_label and _has_ordered_action_object_to_recipient(
954
+ text,
955
+ verb_key=verb_key,
956
+ object_label=object_label,
957
+ recipient=recipient,
958
+ ):
959
+ return ActionRoleRerankSignal(
960
+ boost=_ACTION_ROLE_RECIPIENT_MATCH_BOOST,
961
+ reason="action_role_recipient_match",
962
+ )
963
+ if object_label and _has_ordered_action_object_to_recipient(
964
+ text,
965
+ verb_key=verb_key,
966
+ object_label=recipient,
967
+ recipient=object_label,
968
+ ):
969
+ return ActionRoleRerankSignal(
970
+ penalty=_ACTION_ROLE_RECIPIENT_MISMATCH_PENALTY,
971
+ reason="action_role_recipient_mismatch",
972
+ )
973
+ if object_label and _has_action_to_recipient(
974
+ text,
975
+ recipient=recipient,
976
+ verb_key=verb_key,
977
+ ):
978
+ return ActionRoleRerankSignal(
979
+ penalty=_ACTION_ROLE_REQUESTED_RECIPIENT_MISSING_PENALTY,
980
+ reason="action_role_requested_context_mismatch",
981
+ )
982
+ if _has_action_to_recipient(text, recipient=recipient, verb_key=verb_key):
983
+ return ActionRoleRerankSignal(
984
+ boost=_ACTION_ROLE_RECIPIENT_MATCH_BOOST,
985
+ reason="action_role_recipient_match",
986
+ )
987
+ if verb_key == "meet" and _recipient_acts_to_other(
988
+ text,
989
+ recipient=recipient,
990
+ verb_key=verb_key,
991
+ ):
992
+ return ActionRoleRerankSignal(
993
+ boost=_ACTION_ROLE_RECIPIENT_MATCH_BOOST,
994
+ reason="action_role_recipient_match",
995
+ )
996
+ if _recipient_acts_to_other(text, recipient=recipient, verb_key=verb_key):
997
+ return ActionRoleRerankSignal(
998
+ penalty=_ACTION_ROLE_RECIPIENT_MISMATCH_PENALTY,
999
+ reason="action_role_recipient_mismatch",
1000
+ )
1001
+ return ActionRoleRerankSignal()
1002
+
1003
+
1004
+ def _companion_action_signal(
1005
+ role_query: _ActionRoleQuery,
1006
+ text: str,
1007
+ ) -> ActionRoleRerankSignal:
1008
+ participant = role_query.actor_label or role_query.recipient_label
1009
+ if not participant:
1010
+ return ActionRoleRerankSignal()
1011
+ if _has_participant_action_with_companion(
1012
+ text,
1013
+ participant=participant,
1014
+ verb_key=role_query.verb_key,
1015
+ context_terms=role_query.context_terms,
1016
+ ):
1017
+ return ActionRoleRerankSignal(
1018
+ boost=_ACTION_ROLE_MATCH_BOOST,
1019
+ reason="action_role_companion_match",
1020
+ )
1021
+ if _has_participant_action_without_companion(
1022
+ text,
1023
+ participant=participant,
1024
+ verb_key=role_query.verb_key,
1025
+ context_terms=role_query.context_terms,
1026
+ ):
1027
+ return ActionRoleRerankSignal(
1028
+ penalty=_ACTION_ROLE_REQUESTED_RECIPIENT_MISSING_PENALTY,
1029
+ reason="action_role_companion_missing",
1030
+ )
1031
+ if generic_activity_mention_without_participant_role(
1032
+ text,
1033
+ participant=participant,
1034
+ verb_key=role_query.verb_key,
1035
+ context_terms=role_query.context_terms,
1036
+ ):
1037
+ return ActionRoleRerankSignal(
1038
+ penalty=_ACTION_ROLE_REQUESTED_RECIPIENT_MISSING_PENALTY,
1039
+ reason="action_role_participant_role_missing",
1040
+ )
1041
+ return ActionRoleRerankSignal()
1042
+
1043
+
1044
+ def _support_presence_signal(*, recipient: str, text: str) -> ActionRoleRerankSignal:
1045
+ if _has_negated_support_presence_for_recipient(text, recipient=recipient):
1046
+ return ActionRoleRerankSignal(
1047
+ penalty=_ACTION_ROLE_RECIPIENT_MISMATCH_PENALTY,
1048
+ reason="action_role_negated_evidence",
1049
+ )
1050
+ if _has_support_presence_for_recipient(text, recipient=recipient):
1051
+ return ActionRoleRerankSignal(
1052
+ boost=_ACTION_ROLE_RECIPIENT_MATCH_BOOST,
1053
+ reason="action_role_recipient_match",
1054
+ )
1055
+ if _recipient_present_for_other(text, recipient=recipient):
1056
+ return ActionRoleRerankSignal(
1057
+ penalty=_ACTION_ROLE_RECIPIENT_MISMATCH_PENALTY,
1058
+ reason="action_role_recipient_mismatch",
1059
+ )
1060
+ return ActionRoleRerankSignal()
1061
+
1062
+
1063
+ def _actor_evidence_signal(
1064
+ role_query: _ActionRoleQuery,
1065
+ text: str,
1066
+ ) -> ActionRoleRerankSignal:
1067
+ if _has_any_actor_action_with_context(
1068
+ text,
1069
+ verb_key=role_query.verb_key,
1070
+ context_terms=role_query.context_terms,
1071
+ ):
1072
+ return ActionRoleRerankSignal(
1073
+ boost=_ACTION_ROLE_REQUESTED_RECIPIENT_EVIDENCE_BOOST,
1074
+ reason="action_role_actor_evidence",
1075
+ )
1076
+ if generic_activity_mention_without_participant_role(
1077
+ text,
1078
+ participant="",
1079
+ verb_key=role_query.verb_key,
1080
+ context_terms=role_query.context_terms,
1081
+ ):
1082
+ return ActionRoleRerankSignal(
1083
+ penalty=_ACTION_ROLE_REQUESTED_RECIPIENT_MISSING_PENALTY,
1084
+ reason="action_role_participant_role_missing",
1085
+ )
1086
+ return ActionRoleRerankSignal()
1087
+
1088
+
1089
+ def _information_source_signal(
1090
+ role_query: _ActionRoleQuery,
1091
+ text: str,
1092
+ ) -> ActionRoleRerankSignal:
1093
+ actor = role_query.actor_label
1094
+ if _has_actor_info_from_any_source(text, actor=actor) or _has_source_told_actor(
1095
+ text,
1096
+ actor=actor,
1097
+ ):
1098
+ return ActionRoleRerankSignal(
1099
+ boost=_ACTION_ROLE_REQUESTED_RECIPIENT_EVIDENCE_BOOST,
1100
+ reason="action_role_information_source_evidence",
1101
+ )
1102
+ if _actor_tells_other(text, actor=actor) or _has_other_actor_info_from_source(
1103
+ text,
1104
+ expected_actor=actor,
1105
+ ):
1106
+ return ActionRoleRerankSignal(
1107
+ penalty=_ACTION_ROLE_MISMATCH_PENALTY,
1108
+ reason="action_role_information_source_reversed",
1109
+ )
1110
+ if _has_info_actor_without_source(text, actor=actor):
1111
+ return ActionRoleRerankSignal(
1112
+ penalty=_ACTION_ROLE_REQUESTED_RECIPIENT_MISSING_PENALTY,
1113
+ reason="action_role_information_source_missing",
1114
+ )
1115
+ return ActionRoleRerankSignal()
1116
+
1117
+
1118
+ def _borrow_source_signal(
1119
+ role_query: _ActionRoleQuery,
1120
+ text: str,
1121
+ ) -> ActionRoleRerankSignal:
1122
+ actor = role_query.actor_label
1123
+ context_terms = role_query.context_terms
1124
+ if _has_actor_borrow_from_source(
1125
+ text,
1126
+ actor=actor,
1127
+ context_terms=context_terms,
1128
+ ) or _has_source_lent_to_actor(
1129
+ text,
1130
+ actor=actor,
1131
+ context_terms=context_terms,
1132
+ ):
1133
+ return ActionRoleRerankSignal(
1134
+ boost=_ACTION_ROLE_REQUESTED_RECIPIENT_EVIDENCE_BOOST,
1135
+ reason="action_role_transfer_source_evidence",
1136
+ )
1137
+ if _actor_lent_to_other(
1138
+ text,
1139
+ actor=actor,
1140
+ context_terms=context_terms,
1141
+ ) or _other_borrowed_from_actor(
1142
+ text,
1143
+ actor=actor,
1144
+ context_terms=context_terms,
1145
+ ):
1146
+ return ActionRoleRerankSignal(
1147
+ penalty=_ACTION_ROLE_MISMATCH_PENALTY,
1148
+ reason="action_role_transfer_source_reversed",
1149
+ )
1150
+ if _has_actor_borrow_without_source(
1151
+ text,
1152
+ actor=actor,
1153
+ context_terms=context_terms,
1154
+ ):
1155
+ return ActionRoleRerankSignal(
1156
+ penalty=_ACTION_ROLE_REQUESTED_RECIPIENT_MISSING_PENALTY,
1157
+ reason="action_role_transfer_source_missing",
1158
+ )
1159
+ return ActionRoleRerankSignal()
1160
+
1161
+
1162
+ def _owner_responsibility_signal(
1163
+ role_query: _ActionRoleQuery,
1164
+ text: str,
1165
+ ) -> ActionRoleRerankSignal:
1166
+ owner = role_query.owner_label
1167
+ if not owner:
1168
+ if _owner_labels_in_text(text):
1169
+ return ActionRoleRerankSignal(
1170
+ boost=_ACTION_ROLE_OWNER_EVIDENCE_BOOST,
1171
+ reason="action_role_owner_evidence",
1172
+ )
1173
+ return ActionRoleRerankSignal()
1174
+ owner_key = _normalized_label(owner)
1175
+ owner_labels = {_normalized_label(label) for label in _owner_labels_in_text(text)}
1176
+ if owner_key in owner_labels:
1177
+ return ActionRoleRerankSignal(
1178
+ boost=_ACTION_ROLE_ACTOR_MATCH_BOOST,
1179
+ reason="action_role_owner_match",
1180
+ )
1181
+ if owner_labels:
1182
+ return ActionRoleRerankSignal(
1183
+ penalty=_ACTION_ROLE_MISMATCH_PENALTY,
1184
+ reason="action_role_owner_mismatch",
1185
+ )
1186
+ return ActionRoleRerankSignal()
1187
+
1188
+
1189
+ def _owner_labels_in_text(text: str) -> frozenset[str]:
1190
+ labels: set[str] = set()
1191
+ for match in re.finditer(
1192
+ rf"(?=(?P<label>{_LABEL_RE})\b(?P<gap>.{{0,80}}?)\b"
1193
+ rf"(?:owns?|owner|responsible)\b)",
1194
+ text,
1195
+ flags=re.IGNORECASE | re.DOTALL,
1196
+ ):
1197
+ label = _clean_label(match.group("label"))
1198
+ if label and not _owner_gap_crosses_person_or_speaker(match.group("gap")):
1199
+ labels.add(label)
1200
+ for pattern in (
1201
+ rf"\b(?:owner|responsible)\s+(?P<label>{_LABEL_RE})\b",
1202
+ rf"\b(?:owner|responsible)\b.{{0,80}}\b(?:is|was|:|-|to)\s+(?P<label>{_LABEL_RE})\b",
1203
+ rf"\bassigned\s+to\s+(?P<label>{_LABEL_RE})\b",
1204
+ ):
1205
+ for match in re.finditer(pattern, text, flags=re.IGNORECASE | re.DOTALL):
1206
+ label = _clean_label(match.group("label"))
1207
+ if label:
1208
+ labels.add(label)
1209
+ return frozenset(labels)
1210
+
1211
+
1212
+ def _owner_gap_crosses_person_or_speaker(gap: str) -> bool:
1213
+ return ":" in gap or bool(re.search(rf"\b{_LABEL_RE}\b", gap))
1214
+
1215
+
1216
+ def _has_support_presence_for_recipient(text: str, *, recipient: str) -> bool:
1217
+ recipient_pattern = _role_label_pattern(recipient)
1218
+ return bool(
1219
+ re.search(
1220
+ rf"\b{_LABEL_RE}\b.{{0,80}}\b"
1221
+ rf"(?:is|was|were|are|has\s+been|have\s+been|'s)\s+"
1222
+ rf"there\s+for\s+{recipient_pattern}\b",
1223
+ text,
1224
+ flags=re.IGNORECASE | re.DOTALL,
1225
+ )
1226
+ or re.search(
1227
+ rf"\b{_LABEL_RE}\b.{{0,80}}\b"
1228
+ rf"(?:был|была|были)\s+рядом\s+с\s+{recipient_pattern}\b",
1229
+ text,
1230
+ flags=re.IGNORECASE | re.DOTALL,
1231
+ )
1232
+ )
1233
+
1234
+
1235
+ def _has_negated_support_presence_for_recipient(text: str, *, recipient: str) -> bool:
1236
+ recipient_pattern = _role_label_pattern(recipient)
1237
+ return bool(
1238
+ re.search(
1239
+ rf"\b{_LABEL_RE}\b.{{0,80}}\b"
1240
+ rf"(?:was|were|is|are|has\s+been|have\s+been)\s+not\s+"
1241
+ rf"there\s+for\s+{recipient_pattern}\b",
1242
+ text,
1243
+ flags=re.IGNORECASE | re.DOTALL,
1244
+ )
1245
+ or re.search(
1246
+ rf"\b{_LABEL_RE}\b.{{0,80}}\bне\s+"
1247
+ rf"(?:был|была|были)\s+рядом\s+с\s+{recipient_pattern}\b",
1248
+ text,
1249
+ flags=re.IGNORECASE | re.DOTALL,
1250
+ )
1251
+ )
1252
+
1253
+
1254
+ def _recipient_present_for_other(text: str, *, recipient: str) -> bool:
1255
+ recipient_pattern = _role_label_pattern(recipient)
1256
+ return bool(
1257
+ re.search(
1258
+ rf"{recipient_pattern}.{{0,80}}\b"
1259
+ rf"(?:is|was|were|are|has\s+been|have\s+been|'s)\s+"
1260
+ rf"there\s+for\s+{_LABEL_RE}\b",
1261
+ text,
1262
+ flags=re.IGNORECASE | re.DOTALL,
1263
+ )
1264
+ or re.search(
1265
+ rf"{recipient_pattern}.{{0,80}}\b"
1266
+ rf"(?:был|была|были)\s+рядом\s+с\s+{_LABEL_RE}\b",
1267
+ text,
1268
+ flags=re.IGNORECASE | re.DOTALL,
1269
+ )
1270
+ )
1271
+
1272
+
1273
+ def _has_ordered_action(
1274
+ text: str,
1275
+ *,
1276
+ actor: str,
1277
+ verb_key: str,
1278
+ target: str,
1279
+ ) -> bool:
1280
+ return bool(
1281
+ _ordered_action_match_iter(
1282
+ text,
1283
+ actor=actor,
1284
+ verb_key=verb_key,
1285
+ tail_pattern=rf".{{0,100}}{_label_pattern(target)}",
1286
+ )
1287
+ )
1288
+
1289
+
1290
+ def _has_ordered_action_object_to_recipient(
1291
+ text: str,
1292
+ *,
1293
+ verb_key: str,
1294
+ object_label: str,
1295
+ recipient: str,
1296
+ ) -> bool:
1297
+ return bool(
1298
+ re.search(
1299
+ rf"\b(?:{_verb_forms(verb_key)})\b.{{0,80}}"
1300
+ rf"{_role_label_pattern(object_label)}.{{0,80}}\b"
1301
+ rf"(?:{_recipient_preposition_forms(verb_key)})\s+"
1302
+ rf"{_role_label_pattern(recipient)}",
1303
+ text,
1304
+ flags=re.IGNORECASE | re.DOTALL,
1305
+ )
1306
+ )
1307
+
1308
+
1309
+ def _has_actor_action_object_to_any_recipient(
1310
+ text: str,
1311
+ *,
1312
+ actor: str,
1313
+ verb_key: str,
1314
+ object_label: str,
1315
+ ) -> bool:
1316
+ pattern = re.compile(
1317
+ rf"{_label_pattern(actor)}(?P<gap>.{{0,80}}?)\b"
1318
+ rf"(?:{_verb_forms(verb_key)})\b.{{0,80}}"
1319
+ rf"{_role_label_pattern(object_label)}.{{0,80}}\b"
1320
+ rf"(?:{_recipient_preposition_forms(verb_key)})\s+"
1321
+ rf"(?P<recipient>{_LABEL_RE})\b",
1322
+ re.IGNORECASE | re.DOTALL,
1323
+ )
1324
+ for match in pattern.finditer(text):
1325
+ if _action_gap_blocks_positive_match(match.group("gap")):
1326
+ continue
1327
+ recipient = _clean_label(match.group("recipient"))
1328
+ if recipient and _looks_like_text_recipient(recipient):
1329
+ return True
1330
+ return False
1331
+
1332
+
1333
+ def _ordered_action_match_iter(
1334
+ text: str,
1335
+ *,
1336
+ actor: str,
1337
+ verb_key: str,
1338
+ tail_pattern: str,
1339
+ ) -> bool:
1340
+ pattern = re.compile(
1341
+ rf"{_label_pattern(actor)}(?P<gap>.{{0,80}}?)\b(?:{_verb_forms(verb_key)})\b"
1342
+ rf"{tail_pattern}",
1343
+ flags=re.IGNORECASE | re.DOTALL,
1344
+ )
1345
+ return any(
1346
+ not _action_gap_blocks_positive_match(match.group("gap"))
1347
+ for match in pattern.finditer(text)
1348
+ )
1349
+
1350
+
1351
+ def _has_actor_action(text: str, *, actor: str, verb_key: str) -> bool:
1352
+ return _ordered_action_match_iter(
1353
+ text,
1354
+ actor=actor,
1355
+ verb_key=verb_key,
1356
+ tail_pattern="",
1357
+ )
1358
+
1359
+
1360
+ def _has_participant_action_with_companion(
1361
+ text: str,
1362
+ *,
1363
+ participant: str,
1364
+ verb_key: str,
1365
+ context_terms: tuple[str, ...],
1366
+ ) -> bool:
1367
+ participant_key = _normalized_label(participant)
1368
+ pattern = re.compile(
1369
+ rf"(?P<actor>{_LABEL_RE})(?P<gap>.{{0,80}}?)\b"
1370
+ rf"(?:{_verb_forms(verb_key)})\b(?P<body>.{{0,220}})",
1371
+ re.IGNORECASE | re.DOTALL,
1372
+ )
1373
+ for match in pattern.finditer(text):
1374
+ if _action_gap_blocks_positive_match(match.group("gap")):
1375
+ continue
1376
+ actor = _clean_label(match.group("actor"))
1377
+ body = local_action_segment(match.group("body"))
1378
+ if context_terms and not _context_terms_match(body, context_terms):
1379
+ continue
1380
+ if _normalized_label(actor) == participant_key and _body_has_companion(body):
1381
+ return True
1382
+ if _body_has_companion_label(body, participant=participant):
1383
+ return True
1384
+ paired_pattern = re.compile(
1385
+ rf"(?:{_label_pattern(participant)}\s+and\s+{_LABEL_RE}\b|"
1386
+ rf"\b{_LABEL_RE}\s+and\s+{_label_pattern(participant)}\b)"
1387
+ rf"(?P<body>.{{0,180}}\b(?:{_verb_forms(verb_key)})\b.{{0,140}})",
1388
+ re.IGNORECASE | re.DOTALL,
1389
+ )
1390
+ return any(
1391
+ not context_terms
1392
+ or _context_terms_match(
1393
+ local_action_segment(match.group("body")),
1394
+ context_terms,
1395
+ )
1396
+ for match in paired_pattern.finditer(text)
1397
+ )
1398
+
1399
+
1400
+ def _has_participant_action_without_companion(
1401
+ text: str,
1402
+ *,
1403
+ participant: str,
1404
+ verb_key: str,
1405
+ context_terms: tuple[str, ...],
1406
+ ) -> bool:
1407
+ pattern = re.compile(
1408
+ rf"{_label_pattern(participant)}(?P<gap>.{{0,80}}?)\b"
1409
+ rf"(?:{_verb_forms(verb_key)})\b(?P<body>.{{0,180}})",
1410
+ re.IGNORECASE | re.DOTALL,
1411
+ )
1412
+ for match in pattern.finditer(text):
1413
+ if _action_gap_blocks_positive_match(match.group("gap")):
1414
+ continue
1415
+ body = local_action_segment(match.group("body"))
1416
+ if context_terms and not _context_terms_match(body, context_terms):
1417
+ continue
1418
+ if not _body_has_companion(body):
1419
+ return True
1420
+ return False
1421
+
1422
+
1423
+ def _body_has_companion(body: str) -> bool:
1424
+ return bool(
1425
+ re.search(
1426
+ rf"\b(?:with|alongside|together\s+with|joined\s+by|accompanied\s+by)\s+"
1427
+ rf"(?P<companion>{_LABEL_RE})\b",
1428
+ body,
1429
+ flags=re.IGNORECASE | re.DOTALL,
1430
+ )
1431
+ )
1432
+
1433
+
1434
+ def _has_any_actor_action_with_context(
1435
+ text: str,
1436
+ *,
1437
+ verb_key: str,
1438
+ context_terms: tuple[str, ...],
1439
+ ) -> bool:
1440
+ if not context_terms:
1441
+ return False
1442
+ pattern = re.compile(
1443
+ rf"\b(?P<actor>{_LABEL_RE})\b(?P<gap>"
1444
+ rf"\s+(?:\w+\s+){{0,3}}|:\s+(?:I|we)\s+(?:\w+\s+){{0,3}})"
1445
+ rf"\b(?:{_verb_forms(verb_key)})\b(?P<body>.{{0,220}})",
1446
+ re.IGNORECASE | re.DOTALL,
1447
+ )
1448
+ for match in pattern.finditer(text):
1449
+ actor = _clean_label(match.group("actor"))
1450
+ if not actor:
1451
+ continue
1452
+ if _action_gap_blocks_positive_match(match.group("gap")):
1453
+ continue
1454
+ if _context_terms_match(
1455
+ local_action_segment(match.group("body")),
1456
+ context_terms,
1457
+ ):
1458
+ return True
1459
+ return False
1460
+
1461
+
1462
+ def _body_has_companion_label(body: str, *, participant: str) -> bool:
1463
+ return bool(
1464
+ re.search(
1465
+ rf"\b(?:with|alongside|together\s+with|joined\s+by|accompanied\s+by)\s+"
1466
+ rf"{_role_label_pattern(participant)}\b",
1467
+ body,
1468
+ flags=re.IGNORECASE | re.DOTALL,
1469
+ )
1470
+ )
1471
+
1472
+
1473
+ def _has_negated_actor_action(
1474
+ text: str,
1475
+ *,
1476
+ actor: str,
1477
+ verb_key: str,
1478
+ target: str = "",
1479
+ recipient_requested: bool = False,
1480
+ context_terms: tuple[str, ...] = (),
1481
+ ) -> bool:
1482
+ pattern = re.compile(
1483
+ rf"{_label_pattern(actor)}(?P<gap>.{{0,80}}?)\b"
1484
+ rf"(?:{_verb_forms(verb_key)})\b(?P<body>.{{0,220}})",
1485
+ re.IGNORECASE | re.DOTALL,
1486
+ )
1487
+ for match in pattern.finditer(text):
1488
+ if not _negated_action_gap(match.group("gap")):
1489
+ continue
1490
+ body = local_action_segment(match.group("body"))
1491
+ if target and not re.search(
1492
+ _recipient_label_pattern(target, verb_key=verb_key),
1493
+ body,
1494
+ flags=re.IGNORECASE | re.DOTALL,
1495
+ ):
1496
+ continue
1497
+ if recipient_requested and not _body_has_recipient_for_verb(
1498
+ body,
1499
+ verb_key=verb_key,
1500
+ ):
1501
+ continue
1502
+ if context_terms and not _context_terms_match(body, context_terms):
1503
+ continue
1504
+ return True
1505
+ return False
1506
+
1507
+
1508
+ def _has_actor_action_to_any_recipient(text: str, *, actor: str, verb_key: str) -> bool:
1509
+ preposition_pattern = re.compile(
1510
+ rf"{_label_pattern(actor)}(?P<gap>.{{0,80}}?)\b(?:{_verb_forms(verb_key)})\b"
1511
+ rf".{{0,140}}\b(?:to|for)\s+(?P<recipient>{_LABEL_RE})\b",
1512
+ re.IGNORECASE | re.DOTALL,
1513
+ )
1514
+ for match in preposition_pattern.finditer(text):
1515
+ if _action_gap_blocks_positive_match(match.group("gap")):
1516
+ continue
1517
+ recipient = _clean_label(match.group("recipient"))
1518
+ if recipient and _looks_like_text_recipient(recipient):
1519
+ return True
1520
+ if verb_key in _DIRECT_RECIPIENT_VERBS:
1521
+ direct_pattern = re.compile(
1522
+ rf"{_label_pattern(actor)}(?P<gap>.{{0,80}}?)\b"
1523
+ rf"(?:{_verb_forms(verb_key)})\b\s+"
1524
+ rf"(?P<recipient>{_LABEL_RE})\b(?P<tail>.{{0,40}})",
1525
+ re.IGNORECASE | re.DOTALL,
1526
+ )
1527
+ for match in direct_pattern.finditer(text):
1528
+ if _action_gap_blocks_positive_match(match.group("gap")):
1529
+ continue
1530
+ recipient = _clean_label(match.group("recipient"))
1531
+ if (
1532
+ recipient
1533
+ and _looks_like_direct_recipient(recipient)
1534
+ and _direct_recipient_context_allows(verb_key, match.group("tail"))
1535
+ ):
1536
+ return True
1537
+ passive_pattern = re.compile(
1538
+ rf"(?P<recipient>{_LABEL_RE})\s+(?:was|were)\s+"
1539
+ rf"(?:{_verb_forms(verb_key)})\b.{{0,140}}\bby\s+{_label_pattern(actor)}",
1540
+ re.IGNORECASE | re.DOTALL,
1541
+ )
1542
+ for match in passive_pattern.finditer(text):
1543
+ recipient = _clean_label(match.group("recipient"))
1544
+ if recipient and _looks_like_text_recipient(recipient):
1545
+ return True
1546
+ if verb_key != "recommend":
1547
+ return False
1548
+ russian_direct_pattern = re.compile(
1549
+ rf"{_label_pattern(actor)}(?P<gap>.{{0,80}}?)"
1550
+ rf"\b(?:{_russian_recommendation_forms()})\b\s+"
1551
+ rf"(?P<recipient>{_LABEL_RE})\b",
1552
+ re.IGNORECASE | re.DOTALL,
1553
+ )
1554
+ for match in russian_direct_pattern.finditer(text):
1555
+ if _action_gap_blocks_positive_match(match.group("gap")):
1556
+ continue
1557
+ recipient = _clean_label(match.group("recipient"))
1558
+ if recipient and _looks_like_text_recipient(recipient):
1559
+ return True
1560
+ return False
1561
+
1562
+
1563
+ def _has_actor_action_to_any_recipient_with_context(
1564
+ text: str,
1565
+ *,
1566
+ actor: str,
1567
+ verb_key: str,
1568
+ context_terms: tuple[str, ...],
1569
+ ) -> bool:
1570
+ pattern = re.compile(
1571
+ rf"{_label_pattern(actor)}(?P<gap>.{{0,80}}?)\b"
1572
+ rf"(?:{_verb_forms(verb_key)})\b(?P<body>.{{0,220}})",
1573
+ re.IGNORECASE | re.DOTALL,
1574
+ )
1575
+ for match in pattern.finditer(text):
1576
+ if _action_gap_blocks_positive_match(match.group("gap")):
1577
+ continue
1578
+ body = local_action_segment(match.group("body"))
1579
+ if not _body_has_recipient_for_verb(body, verb_key=verb_key):
1580
+ continue
1581
+ if _context_terms_match(body, context_terms):
1582
+ return True
1583
+ passive_pattern = re.compile(
1584
+ rf"(?P<recipient>{_LABEL_RE})\s+(?:was|were)\s+"
1585
+ rf"(?:{_verb_forms(verb_key)})\b(?P<body>.{{0,180}})"
1586
+ rf"\bby\s+{_label_pattern(actor)}",
1587
+ re.IGNORECASE | re.DOTALL,
1588
+ )
1589
+ for match in passive_pattern.finditer(text):
1590
+ recipient = _clean_label(match.group("recipient"))
1591
+ if not recipient or not _looks_like_text_recipient(recipient):
1592
+ continue
1593
+ body = local_action_segment(match.group("body"))
1594
+ if _context_terms_match(body, context_terms):
1595
+ return True
1596
+ return False
1597
+
1598
+
1599
+ def _body_has_recipient_for_verb(body: str, *, verb_key: str) -> bool:
1600
+ if re.search(rf"\b(?:to|for)\s+{_LABEL_RE}\b", body):
1601
+ return True
1602
+ if verb_key not in _DIRECT_RECIPIENT_VERBS:
1603
+ return False
1604
+ direct_match = re.match(
1605
+ rf"\s+(?P<recipient>{_LABEL_RE})\b(?P<tail>.{{0,80}})",
1606
+ body,
1607
+ flags=re.IGNORECASE | re.DOTALL,
1608
+ )
1609
+ if direct_match is None:
1610
+ return False
1611
+ recipient = _clean_label(direct_match.group("recipient"))
1612
+ if not _looks_like_direct_recipient(recipient):
1613
+ return False
1614
+ return _direct_recipient_context_allows(verb_key, direct_match.group("tail"))
1615
+
1616
+
1617
+ def _reported_subject_shift_before_action(gap: str) -> bool:
1618
+ return bool(_REPORTED_SUBJECT_SHIFT_RE.search(gap))
1619
+
1620
+
1621
+ def _action_gap_blocks_positive_match(gap: str) -> bool:
1622
+ return _reported_subject_shift_before_action(gap) or _negated_action_gap(gap)
1623
+
1624
+
1625
+ def _negated_action_gap(gap: str) -> bool:
1626
+ if not gap:
1627
+ return False
1628
+ if _NEGATION_CANCEL_RE.search(gap):
1629
+ return False
1630
+ return bool(_NEGATED_ACTION_GAP_RE.search(gap))
1631
+
1632
+
1633
+ def _action_prefix_blocks_positive_match(text: str, action_start: int) -> bool:
1634
+ prefix = text[max(0, action_start - 64) : action_start]
1635
+ return _negated_action_gap(prefix)
1636
+
1637
+
1638
+ def _has_actor_info_from_any_source(text: str, *, actor: str) -> bool:
1639
+ pattern = re.compile(
1640
+ rf"{_label_pattern(actor)}(?P<gap>.{{0,80}}?)\b"
1641
+ rf"(?:{_INFO_SOURCE_VERB_RE})\b.{{0,160}}\b(?:from|от)\b(?P<tail>.{{0,80}})",
1642
+ re.IGNORECASE | re.DOTALL,
1643
+ )
1644
+ for match in pattern.finditer(text):
1645
+ if _action_gap_blocks_positive_match(match.group("gap")):
1646
+ continue
1647
+ if _source_tail_has_evidence(match.group("tail")):
1648
+ return True
1649
+ return False
1650
+
1651
+
1652
+ def _has_source_told_actor(text: str, *, actor: str) -> bool:
1653
+ return bool(
1654
+ re.search(
1655
+ rf"\b{_LABEL_RE}\b.{{0,100}}\b"
1656
+ rf"(?:told|said|mentioned|reported|wrote|"
1657
+ rf"сказал(?:а)?|рассказал(?:а)?|упомянул(?:а)?)\b"
1658
+ rf".{{0,100}}{_label_pattern(actor)}",
1659
+ text,
1660
+ flags=re.IGNORECASE | re.DOTALL,
1661
+ )
1662
+ )
1663
+
1664
+
1665
+ def _actor_tells_other(text: str, *, actor: str) -> bool:
1666
+ return bool(
1667
+ re.search(
1668
+ rf"{_label_pattern(actor)}.{{0,100}}\b"
1669
+ rf"(?:told|said|mentioned|reported|wrote|"
1670
+ rf"сказал(?:а)?|рассказал(?:а)?|упомянул(?:а)?)\b"
1671
+ rf".{{0,80}}\b{_LABEL_RE}\b",
1672
+ text,
1673
+ flags=re.IGNORECASE | re.DOTALL,
1674
+ )
1675
+ )
1676
+
1677
+
1678
+ def _has_other_actor_info_from_source(text: str, *, expected_actor: str) -> bool:
1679
+ expected = _normalized_label(expected_actor)
1680
+ pattern = re.compile(
1681
+ rf"(?P<actor>{_LABEL_RE})(?P<gap>.{{0,80}}?)\b"
1682
+ rf"(?:{_INFO_SOURCE_VERB_RE})\b.{{0,160}}\b(?:from|от)\b(?P<tail>.{{0,80}})",
1683
+ re.IGNORECASE | re.DOTALL,
1684
+ )
1685
+ for match in pattern.finditer(text):
1686
+ if _action_gap_blocks_positive_match(match.group("gap")):
1687
+ continue
1688
+ actor = _clean_label(match.group("actor"))
1689
+ if (
1690
+ actor
1691
+ and _normalized_label(actor) != expected
1692
+ and _source_tail_has_evidence(match.group("tail"))
1693
+ ):
1694
+ return True
1695
+ return False
1696
+
1697
+
1698
+ def _has_info_actor_without_source(text: str, *, actor: str) -> bool:
1699
+ return bool(
1700
+ re.search(
1701
+ rf"{_label_pattern(actor)}.{{0,80}}\b(?:{_INFO_SOURCE_VERB_RE})\b",
1702
+ text,
1703
+ flags=re.IGNORECASE | re.DOTALL,
1704
+ )
1705
+ )
1706
+
1707
+
1708
+ def _source_tail_has_evidence(tail: str) -> bool:
1709
+ normalized = tail.casefold()
1710
+ if re.search(rf"\b{_LABEL_RE}\b", tail):
1711
+ return True
1712
+ return bool(
1713
+ re.search(
1714
+ r"\b(?:a|an|the|elderly|veteran|friend|teacher|doctor|mentor|"
1715
+ r"colleague|teammate|volunteer|отец|мать|друг|подруга|учитель|"
1716
+ r"врач|ментор|коллега)\b",
1717
+ normalized,
1718
+ )
1719
+ )
1720
+
1721
+
1722
+ def _has_actor_borrow_from_source(
1723
+ text: str,
1724
+ *,
1725
+ actor: str,
1726
+ context_terms: tuple[str, ...],
1727
+ ) -> bool:
1728
+ pattern = re.compile(
1729
+ rf"{_label_pattern(actor)}(?P<gap>.{{0,80}}?)\b"
1730
+ rf"(?:{_BORROW_SOURCE_VERB_RE})\b(?P<body>.{{0,220}})",
1731
+ re.IGNORECASE | re.DOTALL,
1732
+ )
1733
+ for match in pattern.finditer(text):
1734
+ if _action_gap_blocks_positive_match(match.group("gap")):
1735
+ continue
1736
+ body = match.group("body")
1737
+ if context_terms and not _context_terms_match(body, context_terms):
1738
+ continue
1739
+ if _borrow_body_has_source(body):
1740
+ return True
1741
+ return False
1742
+
1743
+
1744
+ def _has_source_lent_to_actor(
1745
+ text: str,
1746
+ *,
1747
+ actor: str,
1748
+ context_terms: tuple[str, ...],
1749
+ ) -> bool:
1750
+ actor_key = _normalized_label(actor)
1751
+ pattern = re.compile(
1752
+ rf"(?P<source>{_LABEL_RE})(?P<gap>.{{0,80}}?)\b"
1753
+ rf"(?:{_verb_forms('lend')})\b(?P<body>.{{0,220}})",
1754
+ re.IGNORECASE | re.DOTALL,
1755
+ )
1756
+ for match in pattern.finditer(text):
1757
+ if _action_gap_blocks_positive_match(match.group("gap")):
1758
+ continue
1759
+ source = _clean_label(match.group("source"))
1760
+ if not _looks_like_text_recipient(source):
1761
+ continue
1762
+ if _normalized_label(source) == actor_key:
1763
+ continue
1764
+ body = match.group("body")
1765
+ if context_terms and not _context_terms_match(body, context_terms):
1766
+ continue
1767
+ if re.search(_recipient_label_pattern(actor, verb_key="lend"), body, re.IGNORECASE):
1768
+ return True
1769
+ return False
1770
+
1771
+
1772
+ def _actor_lent_to_other(
1773
+ text: str,
1774
+ *,
1775
+ actor: str,
1776
+ context_terms: tuple[str, ...],
1777
+ ) -> bool:
1778
+ if context_terms:
1779
+ return _has_actor_action_to_any_recipient_with_context(
1780
+ text,
1781
+ actor=actor,
1782
+ verb_key="lend",
1783
+ context_terms=context_terms,
1784
+ )
1785
+ return _has_actor_action_to_any_recipient(text, actor=actor, verb_key="lend")
1786
+
1787
+
1788
+ def _other_borrowed_from_actor(
1789
+ text: str,
1790
+ *,
1791
+ actor: str,
1792
+ context_terms: tuple[str, ...],
1793
+ ) -> bool:
1794
+ actor_key = _normalized_label(actor)
1795
+ pattern = re.compile(
1796
+ rf"(?P<borrower>{_LABEL_RE})(?P<gap>.{{0,80}}?)\b"
1797
+ rf"(?:{_BORROW_SOURCE_VERB_RE})\b(?P<body>.{{0,220}})",
1798
+ re.IGNORECASE | re.DOTALL,
1799
+ )
1800
+ for match in pattern.finditer(text):
1801
+ if _action_gap_blocks_positive_match(match.group("gap")):
1802
+ continue
1803
+ borrower = _clean_label(match.group("borrower"))
1804
+ if not _looks_like_text_recipient(borrower):
1805
+ continue
1806
+ if _normalized_label(borrower) == actor_key:
1807
+ continue
1808
+ body = match.group("body")
1809
+ if context_terms and not _context_terms_match(body, context_terms):
1810
+ continue
1811
+ if re.search(
1812
+ rf"\bfrom\s+{_label_pattern(actor)}\b|{_label_pattern(actor)}(?:'s|s')",
1813
+ body,
1814
+ re.IGNORECASE | re.DOTALL,
1815
+ ):
1816
+ return True
1817
+ return False
1818
+
1819
+
1820
+ def _has_actor_borrow_without_source(
1821
+ text: str,
1822
+ *,
1823
+ actor: str,
1824
+ context_terms: tuple[str, ...],
1825
+ ) -> bool:
1826
+ pattern = re.compile(
1827
+ rf"{_label_pattern(actor)}(?P<gap>.{{0,80}}?)\b"
1828
+ rf"(?:{_BORROW_SOURCE_VERB_RE})\b(?P<body>.{{0,160}})",
1829
+ re.IGNORECASE | re.DOTALL,
1830
+ )
1831
+ for match in pattern.finditer(text):
1832
+ if _action_gap_blocks_positive_match(match.group("gap")):
1833
+ continue
1834
+ body = match.group("body")
1835
+ if context_terms and not _context_terms_match(body, context_terms):
1836
+ continue
1837
+ if not _borrow_body_has_source(body):
1838
+ return True
1839
+ return False
1840
+
1841
+
1842
+ def _borrow_body_has_source(body: str) -> bool:
1843
+ from_match = re.search(
1844
+ rf"\bfrom\s+(?P<source>{_LABEL_RE})\b",
1845
+ body,
1846
+ re.IGNORECASE | re.DOTALL,
1847
+ )
1848
+ if from_match is not None:
1849
+ source = _clean_label(from_match.group("source"))
1850
+ if _looks_like_text_recipient(source):
1851
+ return True
1852
+ possessive_match = re.search(
1853
+ rf"\b(?P<source>{_LABEL_RE})(?:'s|s')\b",
1854
+ body,
1855
+ re.IGNORECASE | re.DOTALL,
1856
+ )
1857
+ if possessive_match is None:
1858
+ return False
1859
+ source = _clean_label(possessive_match.group("source"))
1860
+ return _looks_like_text_recipient(source)
1861
+
1862
+
1863
+ def _has_non_actor_action(text: str, *, expected_actor: str, verb_key: str) -> bool:
1864
+ pattern = re.compile(
1865
+ rf"(?=\b(?P<actor>{_LABEL_RE})\b(?P<gap>.{{0,80}}?)"
1866
+ rf"\b(?:{_verb_forms(verb_key)})\b)",
1867
+ re.IGNORECASE | re.DOTALL,
1868
+ )
1869
+ expected = _normalized_label(expected_actor)
1870
+ for match in pattern.finditer(text):
1871
+ if _action_gap_blocks_positive_match(match.group("gap")):
1872
+ continue
1873
+ actor = _clean_label(match.group("actor"))
1874
+ if actor and _normalized_label(actor) != expected:
1875
+ return True
1876
+ return False
1877
+
1878
+
1879
+ def _has_action_to_recipient(text: str, *, recipient: str, verb_key: str) -> bool:
1880
+ recipient_pattern = _recipient_label_pattern(recipient, verb_key=verb_key)
1881
+ preposition_match = re.search(
1882
+ rf"\b(?:{_verb_forms(verb_key)})\b.{{0,140}}\b(?:to|for)\s+"
1883
+ rf"{recipient_pattern}",
1884
+ text,
1885
+ flags=re.IGNORECASE | re.DOTALL,
1886
+ )
1887
+ if preposition_match is not None and not _action_prefix_blocks_positive_match(
1888
+ text,
1889
+ preposition_match.start(),
1890
+ ):
1891
+ return True
1892
+ if verb_key in _DIRECT_RECIPIENT_VERBS:
1893
+ direct_match = re.search(
1894
+ rf"\b(?:{_verb_forms(verb_key)})\b\s+"
1895
+ rf"{recipient_pattern}(?P<tail>.{{0,40}})",
1896
+ text,
1897
+ flags=re.IGNORECASE | re.DOTALL,
1898
+ )
1899
+ if (
1900
+ direct_match is not None
1901
+ and not _action_prefix_blocks_positive_match(text, direct_match.start())
1902
+ and _direct_recipient_context_allows(
1903
+ verb_key,
1904
+ direct_match.group("tail"),
1905
+ )
1906
+ ):
1907
+ return True
1908
+ passive_match = re.search(
1909
+ rf"{recipient_pattern}.{{0,100}}\b(?:was|were)\s+"
1910
+ rf"(?:{_verb_forms(verb_key)})\b",
1911
+ text,
1912
+ flags=re.IGNORECASE | re.DOTALL,
1913
+ )
1914
+ if passive_match is not None and not _negated_action_gap(passive_match.group(0)):
1915
+ return True
1916
+ if verb_key != "recommend":
1917
+ return False
1918
+ if _has_recommendation_that_recipient_acts(text, recipient=recipient):
1919
+ return True
1920
+ return bool(
1921
+ re.search(
1922
+ rf"\b(?:{_russian_recommendation_forms()})\b\s+{recipient_pattern}",
1923
+ text,
1924
+ flags=re.IGNORECASE | re.DOTALL,
1925
+ )
1926
+ )
1927
+
1928
+
1929
+ def _has_negated_action_to_recipient(
1930
+ text: str,
1931
+ *,
1932
+ recipient: str,
1933
+ verb_key: str,
1934
+ object_label: str = "",
1935
+ ) -> bool:
1936
+ recipient_pattern = _recipient_label_pattern(recipient, verb_key=verb_key)
1937
+ target_pattern = (
1938
+ rf".{{0,100}}{_role_label_pattern(object_label)}"
1939
+ if object_label
1940
+ else rf".{{0,140}}(?:to|for)\s+{recipient_pattern}"
1941
+ )
1942
+ preposition_pattern = re.compile(
1943
+ rf"\b(?:{_verb_forms(verb_key)})\b{target_pattern}",
1944
+ re.IGNORECASE | re.DOTALL,
1945
+ )
1946
+ for match in preposition_pattern.finditer(text):
1947
+ if _action_prefix_blocks_positive_match(text, match.start()):
1948
+ return True
1949
+ if verb_key in _DIRECT_RECIPIENT_VERBS:
1950
+ direct_pattern = re.compile(
1951
+ rf"\b(?:{_verb_forms(verb_key)})\b\s+"
1952
+ rf"{recipient_pattern}(?P<tail>.{{0,80}})",
1953
+ re.IGNORECASE | re.DOTALL,
1954
+ )
1955
+ for match in direct_pattern.finditer(text):
1956
+ if _action_prefix_blocks_positive_match(
1957
+ text,
1958
+ match.start(),
1959
+ ) and _direct_recipient_context_allows(verb_key, match.group("tail")):
1960
+ return True
1961
+ passive_pattern = re.compile(
1962
+ rf"{recipient_pattern}.{{0,100}}\b(?:was|were)\s+"
1963
+ rf"(?:{_verb_forms(verb_key)})\b",
1964
+ re.IGNORECASE | re.DOTALL,
1965
+ )
1966
+ return any(_negated_action_gap(match.group(0)) for match in passive_pattern.finditer(text))
1967
+
1968
+
1969
+ def _recipient_acts_to_other(text: str, *, recipient: str, verb_key: str) -> bool:
1970
+ recipient_pattern = _recipient_label_pattern(recipient, verb_key=verb_key)
1971
+ pattern = re.compile(
1972
+ rf"{recipient_pattern}(?P<gap>.{{0,80}}?)\b(?:{_verb_forms(verb_key)})\b"
1973
+ rf".{{0,140}}\b(?:to|for)\s+(?P<other>{_LABEL_RE})\b",
1974
+ re.IGNORECASE | re.DOTALL,
1975
+ )
1976
+ recipient_key = _normalized_label(recipient)
1977
+ for match in pattern.finditer(text):
1978
+ if _action_gap_blocks_positive_match(match.group("gap")):
1979
+ continue
1980
+ other = _clean_label(match.group("other"))
1981
+ if (
1982
+ other
1983
+ and _looks_like_text_recipient(other)
1984
+ and _normalized_label(other) != recipient_key
1985
+ ):
1986
+ return True
1987
+ if verb_key in _DIRECT_RECIPIENT_VERBS:
1988
+ direct_pattern = re.compile(
1989
+ rf"{recipient_pattern}(?P<gap>.{{0,80}}?)\b"
1990
+ rf"(?:{_verb_forms(verb_key)})\b\s+"
1991
+ rf"(?P<other>{_LABEL_RE})\b(?P<tail>.{{0,40}})",
1992
+ re.IGNORECASE | re.DOTALL,
1993
+ )
1994
+ for match in direct_pattern.finditer(text):
1995
+ if _action_gap_blocks_positive_match(match.group("gap")):
1996
+ continue
1997
+ other = _clean_label(match.group("other"))
1998
+ if (
1999
+ other
2000
+ and _looks_like_text_recipient(other)
2001
+ and _normalized_label(other) != recipient_key
2002
+ and _direct_recipient_context_allows(verb_key, match.group("tail"))
2003
+ ):
2004
+ return True
2005
+ if verb_key == "recommend":
2006
+ that_action_pattern = re.compile(
2007
+ rf"{_label_pattern(recipient)}(?P<gap>.{{0,80}}?)"
2008
+ rf"\b(?:{_verb_forms(verb_key)})\b.{{0,80}}\b"
2009
+ rf"(?:that\s+)?(?P<other>{_LABEL_RE})\s+"
2010
+ rf"(?:{_RECIPIENT_FOLLOWUP_ACTION_RE})\b",
2011
+ re.IGNORECASE | re.DOTALL,
2012
+ )
2013
+ for match in that_action_pattern.finditer(text):
2014
+ if _action_gap_blocks_positive_match(match.group("gap")):
2015
+ continue
2016
+ other = _clean_label(match.group("other"))
2017
+ if (
2018
+ other
2019
+ and _looks_like_text_recipient(other)
2020
+ and _normalized_label(other) != recipient_key
2021
+ ):
2022
+ return True
2023
+ russian_direct_pattern = re.compile(
2024
+ rf"{_label_pattern(recipient)}(?P<gap>.{{0,80}}?)"
2025
+ rf"\b(?:{_russian_recommendation_forms()})\b\s+(?P<other>{_LABEL_RE})\b",
2026
+ re.IGNORECASE | re.DOTALL,
2027
+ )
2028
+ for match in russian_direct_pattern.finditer(text):
2029
+ if _action_gap_blocks_positive_match(match.group("gap")):
2030
+ continue
2031
+ other = _clean_label(match.group("other"))
2032
+ if (
2033
+ other
2034
+ and _looks_like_text_recipient(other)
2035
+ and _normalized_label(other) != recipient_key
2036
+ ):
2037
+ return True
2038
+ return False
2039
+
2040
+
2041
+ def _has_recommendation_that_recipient_acts(text: str, *, recipient: str) -> bool:
2042
+ return bool(
2043
+ re.search(
2044
+ rf"\b(?:{_verb_forms('recommend')})\b.{{0,100}}\b"
2045
+ rf"(?:that\s+)?{_label_pattern(recipient)}\s+"
2046
+ rf"(?:{_RECIPIENT_FOLLOWUP_ACTION_RE})\b",
2047
+ text,
2048
+ flags=re.IGNORECASE | re.DOTALL,
2049
+ )
2050
+ )
2051
+
2052
+
2053
+ def _canonical_verb_key(value: str) -> str:
2054
+ token = value.casefold()
2055
+ if token.startswith(("recommend", "suggest", "рекоменд", "порекоменд", "посовет")):
2056
+ return "recommend"
2057
+ if token.startswith(("introduc", "познаком", "представ")):
2058
+ return "introduce"
2059
+ if token.startswith(("promise", "пообещ", "обещ")):
2060
+ return "promise"
2061
+ if token.startswith(("decid", "реш")):
2062
+ return "decide"
2063
+ if token.startswith(("ask", "спрос")):
2064
+ return "ask"
2065
+ if token.startswith(("assign", "назнач")):
2066
+ return "assign"
2067
+ if token.startswith(("approv", "одобр")):
2068
+ return "approve"
2069
+ if token.startswith(("help", "assist", "support", "помог", "поддерж")):
2070
+ return "help"
2071
+ if token.startswith(("attend", "participat")):
2072
+ return "attend"
2073
+ if token.startswith("join"):
2074
+ return "join"
2075
+ if token in {"meet", "met"}:
2076
+ return "meet"
2077
+ if token.startswith("visit"):
2078
+ return "visit"
2079
+ if token.startswith("call"):
2080
+ return "call"
2081
+ if token.startswith(("message", "text")):
2082
+ return "message"
2083
+ if token in {"send", "sent"}:
2084
+ return "send"
2085
+ if token in {"give", "gave", "given"}:
2086
+ return "give"
2087
+ if token in {"lend", "lent"}:
2088
+ return "lend"
2089
+ if token in {"tell", "told"} or token.startswith("сказ"):
2090
+ return "tell"
2091
+ return ""
2092
+
2093
+
2094
+ def _canonical_nominal_action(value: str) -> str:
2095
+ token = value.casefold()
2096
+ if token == "decision":
2097
+ return "decide"
2098
+ if token == "promise":
2099
+ return "promise"
2100
+ if token in {"recommendation", "suggestion"}:
2101
+ return "recommend"
2102
+ return ""
2103
+
2104
+
2105
+ def _recipient_in_tail(tail: str) -> str:
2106
+ match = re.search(
2107
+ rf"\b(?:to|for)\s+(?P<recipient>{_QUERY_LABEL_RE})\b",
2108
+ tail,
2109
+ flags=re.IGNORECASE,
2110
+ )
2111
+ if match is None:
2112
+ return ""
2113
+ return _clean_label(match.group("recipient"))
2114
+
2115
+
2116
+ def _object_label_in_text(value: str) -> str:
2117
+ labels: list[str] = []
2118
+ for match in re.finditer(rf"\b(?P<label>{_QUERY_LABEL_RE})\b", value):
2119
+ label = _clean_label(match.group("label"))
2120
+ if label:
2121
+ labels.append(label)
2122
+ return labels[-1] if labels else ""
2123
+
2124
+
2125
+ def _action_context_terms(value: str, *, verb_key: str) -> tuple[str, ...]:
2126
+ if not value or verb_key == "introduce":
2127
+ return ()
2128
+ terms: list[str] = []
2129
+ for match in re.finditer(r"[A-Za-zА-Яа-яЁё0-9]{3,}", value.casefold()):
2130
+ token = match.group(0)
2131
+ if token in _ACTION_CONTEXT_STOP_WORDS:
2132
+ continue
2133
+ if token.startswith((
2134
+ "send",
2135
+ "sent",
2136
+ "tell",
2137
+ "told",
2138
+ "ask",
2139
+ "help",
2140
+ "support",
2141
+ "join",
2142
+ "meet",
2143
+ "met",
2144
+ "visit",
2145
+ "lend",
2146
+ "lent",
2147
+ )):
2148
+ continue
2149
+ if token.startswith(("сказ", "спрос", "помог", "поддерж")):
2150
+ continue
2151
+ if token not in terms:
2152
+ terms.append(token)
2153
+ if len(terms) >= 6:
2154
+ break
2155
+ return tuple(terms)
2156
+
2157
+
2158
+ def _context_terms_match(text: str, context_terms: tuple[str, ...]) -> bool:
2159
+ if not context_terms:
2160
+ return True
2161
+ normalized = text.casefold()
2162
+ hits = sum(1 for term in context_terms if term in normalized)
2163
+ required = min(len(context_terms), 2)
2164
+ return hits >= required
2165
+
2166
+
2167
+ def _recipient_after_action(query: str, *, match_end: int, verb_key: str) -> str:
2168
+ tail = query[match_end : match_end + 120]
2169
+ preposition_match = re.search(
2170
+ rf"\b(?:to|for)\s+(?P<recipient>{_QUERY_LABEL_RE})\b",
2171
+ tail,
2172
+ flags=re.IGNORECASE,
2173
+ )
2174
+ if preposition_match is not None:
2175
+ return _clean_label(preposition_match.group("recipient"))
2176
+ if verb_key not in _DIRECT_RECIPIENT_VERBS:
2177
+ return ""
2178
+ direct_match = re.match(
2179
+ rf"\s+(?P<recipient>{_QUERY_LABEL_RE})\b",
2180
+ tail,
2181
+ flags=re.IGNORECASE,
2182
+ )
2183
+ if direct_match is None:
2184
+ return ""
2185
+ recipient = _clean_label(direct_match.group("recipient"))
2186
+ if not _looks_like_direct_recipient(recipient):
2187
+ return ""
2188
+ return recipient
2189
+
2190
+
2191
+ def _verb_forms(verb_key: str) -> str:
2192
+ forms = {
2193
+ "recommend": (
2194
+ r"recommend(?:ed|s|ing)?|suggest(?:ed|s|ing)?|"
2195
+ r"made\s+(?:a\s+|the\s+)?(?:recommendation|suggestion)|"
2196
+ rf"{_russian_recommendation_forms()}"
2197
+ ),
2198
+ "introduce": (
2199
+ r"introduc(?:e|ed|es|ing)|"
2200
+ r"познакомил(?:а|и)?|представил(?:а|и)?"
2201
+ ),
2202
+ "promise": (
2203
+ r"promise(?:d|s|ing)?|made\s+(?:a\s+|the\s+)?promise|"
2204
+ r"пообещал(?:а)?|обещал(?:а)?"
2205
+ ),
2206
+ "decide": r"decid(?:e|ed|es|ing)|made\s+(?:a\s+|the\s+)?decision|решил(?:а)?",
2207
+ "ask": r"ask(?:ed|s|ing)?|спросил(?:а)?",
2208
+ "assign": r"assign(?:ed|s|ing)?|назначил(?:а)?",
2209
+ "approve": r"approv(?:e|ed|es|ing)|одобрил(?:а)?",
2210
+ "call": r"call(?:ed|s|ing)?",
2211
+ "help": (
2212
+ r"help(?:ed|s|ing)?|assist(?:ed|s|ing)?|support(?:ed|s|ing)?|"
2213
+ r"помог(?:ла|ли)?|поддержал(?:а|и)?"
2214
+ ),
2215
+ "attend": r"attend(?:ed|s|ing)?|participat(?:e|ed|es|ing)",
2216
+ "join": r"join(?:ed|s|ing)?",
2217
+ "meet": r"meet|met",
2218
+ "message": r"message(?:d|s|ing)?|text(?:ed|s|ing)?",
2219
+ "send": r"send|sent",
2220
+ "give": r"give|gave|given",
2221
+ "lend": r"lend|lent",
2222
+ "tell": r"tell|told|сказал(?:а)?",
2223
+ "visit": r"visit(?:ed|s|ing)?",
2224
+ }
2225
+ return forms.get(verb_key, r"(?!x)x")
2226
+
2227
+
2228
+ def _recipient_preposition_forms(verb_key: str) -> str:
2229
+ if verb_key == "introduce":
2230
+ return r"to|for|with|с|со"
2231
+ return r"to|for"
2232
+
2233
+
2234
+ def _russian_recommendation_forms() -> str:
2235
+ return r"рекомендовал(?:а)?|порекомендовал(?:а)?|посоветовал(?:а)?"
2236
+
2237
+
2238
+ def _label_pattern(label: str) -> str:
2239
+ return rf"(?<!\w){re.escape(label)}(?!\w)"
2240
+
2241
+
2242
+ def _role_label_pattern(label: str) -> str:
2243
+ if not re.search(r"[А-Яа-яЁё]", label):
2244
+ return _label_pattern(label)
2245
+ stem = _russian_label_stem(label)
2246
+ if len(stem) < 3 or stem == label:
2247
+ return _label_pattern(label)
2248
+ return rf"(?<!\w)(?:{re.escape(label)}|{re.escape(stem)}[А-Яа-яЁё]{{0,4}})(?!\w)"
2249
+
2250
+
2251
+ def _recipient_label_pattern(label: str, *, verb_key: str) -> str:
2252
+ if verb_key == "help":
2253
+ return _role_label_pattern(label)
2254
+ return _label_pattern(label)
2255
+
2256
+
2257
+ def _russian_label_stem(label: str) -> str:
2258
+ for suffix in (
2259
+ "иями",
2260
+ "ями",
2261
+ "ами",
2262
+ "ого",
2263
+ "ему",
2264
+ "ыми",
2265
+ "ими",
2266
+ "ом",
2267
+ "ем",
2268
+ "ой",
2269
+ "ей",
2270
+ "ую",
2271
+ "ю",
2272
+ "а",
2273
+ "я",
2274
+ "е",
2275
+ "ы",
2276
+ "и",
2277
+ ):
2278
+ if label.casefold().endswith(suffix) and len(label) > len(suffix) + 2:
2279
+ return label[: -len(suffix)]
2280
+ return label
2281
+
2282
+
2283
+ def _clean_label(value: str) -> str:
2284
+ label = (value or "").strip(" :,.!?;")
2285
+ if not label:
2286
+ return ""
2287
+ if _normalized_label(label) in _TEXT_LABEL_STOP_WORDS:
2288
+ return ""
2289
+ return label
2290
+
2291
+
2292
+ def _looks_like_direct_recipient(label: str) -> bool:
2293
+ if not label:
2294
+ return False
2295
+ if label[:1].isupper():
2296
+ return True
2297
+ return _normalized_label(label) not in _DIRECT_RECIPIENT_OBJECT_STOP_WORDS
2298
+
2299
+
2300
+ def _looks_like_text_recipient(label: str) -> bool:
2301
+ return bool(label) and label[:1].isupper() and _looks_like_direct_recipient(label)
2302
+
2303
+
2304
+ def _direct_recipient_context_allows(verb_key: str, tail: str) -> bool:
2305
+ normalized = tail.casefold()
2306
+ if verb_key in {"ask", "tell"}:
2307
+ context_markers = (
2308
+ r"about|regarding|whether|if|that|to|why|how|when|where|"
2309
+ r"про|что|о|об|почему|как|когда|где"
2310
+ )
2311
+ if verb_key == "ask":
2312
+ context_markers = (
2313
+ r"about|regarding|whether|if|that|to|for|why|how|when|where|"
2314
+ r"про|что|о|об|почему|как|когда|где"
2315
+ )
2316
+ return bool(
2317
+ re.match(
2318
+ rf"\s*(?:{context_markers})\b|\s*[,.;!?]?\s*$",
2319
+ normalized,
2320
+ )
2321
+ )
2322
+ if verb_key == "message":
2323
+ return bool(
2324
+ re.match(
2325
+ r"\s*(?:about|regarding|whether|if|that|why|how|when|where|"
2326
+ r"про|что|о|об|почему|как|когда|где)\b|\s*[,.;!?]?\s*$",
2327
+ normalized,
2328
+ )
2329
+ )
2330
+ if verb_key == "call":
2331
+ return bool(
2332
+ re.match(
2333
+ r"\s*(?:about|regarding|after|before|during|on|at|when|where|"
2334
+ r"про|о|об|после|до|во\s+время|когда|где)\b|\s*[,.;!?]?\s*$",
2335
+ normalized,
2336
+ )
2337
+ )
2338
+ if verb_key == "introduce":
2339
+ return bool(
2340
+ re.match(
2341
+ r"\s*(?:to|with|for|at|during|after|before|on|in|"
2342
+ r"с|со|на|во\s+время|после|до)\b|\s*[,.;!?]?\s*$",
2343
+ normalized,
2344
+ )
2345
+ )
2346
+ if verb_key == "help":
2347
+ return bool(
2348
+ re.match(
2349
+ r"\s*(?:with|on|through|during|after|before|about|for|in|at|"
2350
+ r"с|со|по|в|на|во\s+время|после|до|про|о|об)\b|\s*[,.;!?]?\s*$",
2351
+ normalized,
2352
+ )
2353
+ )
2354
+ if verb_key == "promise":
2355
+ return bool(
2356
+ re.match(
2357
+ r"\s*(?:to|that|he|she|they|we|i|it|would|will|что|он|она|они|мы)\b",
2358
+ normalized,
2359
+ )
2360
+ )
2361
+ return True
2362
+
2363
+
2364
+ def _normalized_label(value: str) -> str:
2365
+ return "".join(char for char in value.casefold() if char.isalnum())