collavre 0.24.1 → 0.30.0

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 (1320) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +19 -11
  3. data/app/assets/images/collavre/kollavy.png +0 -0
  4. data/app/assets/images/collavre/kollavy.svg +9 -0
  5. data/app/assets/stylesheets/collavre/actiontext.css +24 -165
  6. data/app/assets/stylesheets/collavre/activity_logs.css +23 -12
  7. data/app/assets/stylesheets/collavre/agent_connections.css +196 -0
  8. data/app/assets/stylesheets/collavre/agent_gateways.css +153 -0
  9. data/app/assets/stylesheets/collavre/attachment_card.css +34 -0
  10. data/app/assets/stylesheets/collavre/comments_popup.css +668 -165
  11. data/app/assets/stylesheets/collavre/creatives.css +1165 -155
  12. data/app/assets/stylesheets/collavre/dark_mode.css +0 -20
  13. data/app/assets/stylesheets/collavre/design_tokens.css +79 -0
  14. data/app/assets/stylesheets/collavre/desktop_setup.css +241 -0
  15. data/app/assets/stylesheets/collavre/gnb.css +64 -3
  16. data/app/assets/stylesheets/collavre/image_lightbox.css +12 -0
  17. data/app/assets/stylesheets/collavre/landing.css +130 -106
  18. data/app/assets/stylesheets/collavre/llm_usages.css +67 -0
  19. data/app/assets/stylesheets/collavre/mention_menu.css +50 -0
  20. data/app/assets/stylesheets/collavre/modal_dialog.css +66 -3
  21. data/app/assets/stylesheets/collavre/notice_bar.css +465 -0
  22. data/app/assets/stylesheets/collavre/org_chart.css +0 -4
  23. data/app/assets/stylesheets/collavre/popup.css +29 -18
  24. data/app/assets/stylesheets/collavre/print.css +0 -1
  25. data/app/assets/stylesheets/collavre/search_popup.css +1 -35
  26. data/app/assets/stylesheets/collavre/secret_fields.css +3 -0
  27. data/app/assets/stylesheets/collavre/slide_view.css +221 -4
  28. data/app/assets/stylesheets/collavre/workflow_editor.css +101 -0
  29. data/app/assets/stylesheets/collavre/workspace.css +310 -0
  30. data/app/channels/collavre/agent_channel.rb +61 -43
  31. data/app/channels/collavre/comments_presence_channel.rb +181 -21
  32. data/app/channels/collavre/inbox_badge_channel.rb +7 -1
  33. data/app/components/collavre/command_menu_component.html.erb +5 -0
  34. data/app/components/collavre/command_menu_component.rb +8 -0
  35. data/app/components/collavre/comment_user_menu_component.html.erb +61 -0
  36. data/app/components/collavre/comment_user_menu_component.rb +14 -0
  37. data/app/components/collavre/cron_badge_component.html.erb +80 -0
  38. data/app/components/collavre/cron_badge_component.rb +50 -0
  39. data/app/components/collavre/inbox/badge_component.rb +6 -13
  40. data/app/components/collavre/progress_filter_component.html.erb +2 -2
  41. data/app/components/collavre/progress_filter_component.rb +20 -0
  42. data/app/controllers/collavre/admin/orchestration_controller.rb +59 -32
  43. data/app/controllers/collavre/admin/settings_controller.rb +80 -8
  44. data/app/controllers/collavre/agent_connections_controller.rb +153 -0
  45. data/app/controllers/collavre/agent_gateways_controller.rb +132 -0
  46. data/app/controllers/collavre/agent_models_controller.rb +55 -0
  47. data/app/controllers/collavre/agent_provisioning_controller.rb +88 -0
  48. data/app/controllers/collavre/api/v1/agents_controller.rb +81 -249
  49. data/app/controllers/collavre/api/v1/task_suspensions_controller.rb +60 -0
  50. data/app/controllers/collavre/application_controller.rb +13 -2
  51. data/app/controllers/collavre/attachments_controller.rb +12 -12
  52. data/app/controllers/collavre/comment_read_pointers_controller.rb +69 -40
  53. data/app/controllers/collavre/comments/activity_logs_controller.rb +2 -0
  54. data/app/controllers/collavre/comments/reactions_controller.rb +10 -12
  55. data/app/controllers/collavre/comments/resends_controller.rb +21 -0
  56. data/app/controllers/collavre/comments/versions_controller.rb +24 -21
  57. data/app/controllers/collavre/comments_controller.rb +104 -139
  58. data/app/controllers/collavre/concerns/creative_history_trackable.rb +31 -0
  59. data/app/controllers/collavre/concerns/shareable.rb +9 -3
  60. data/app/controllers/collavre/concerns/tree_manageable.rb +4 -0
  61. data/app/controllers/collavre/creative_change_sets_controller.rb +112 -0
  62. data/app/controllers/collavre/creative_imports_controller.rb +9 -1
  63. data/app/controllers/collavre/creative_invitations_controller.rb +2 -11
  64. data/app/controllers/collavre/creative_shares_controller.rb +7 -15
  65. data/app/controllers/collavre/creatives/attachments_controller.rb +16 -4
  66. data/app/controllers/collavre/creatives/file_drops_controller.rb +35 -0
  67. data/app/controllers/collavre/creatives/ppt_previews_controller.rb +66 -0
  68. data/app/controllers/collavre/creatives_controller.rb +186 -276
  69. data/app/controllers/collavre/crons_controller.rb +48 -0
  70. data/app/controllers/collavre/desktop_gateway_registrations_controller.rb +156 -0
  71. data/app/controllers/collavre/desktop_setup_controller.rb +102 -0
  72. data/app/controllers/collavre/desktop_sidecar_health_controller.rb +23 -0
  73. data/app/controllers/collavre/features_controller.rb +57 -0
  74. data/app/controllers/collavre/google_auth_controller.rb +3 -4
  75. data/app/controllers/collavre/inline_agent_logins_controller.rb +79 -0
  76. data/app/controllers/collavre/invites_controller.rb +12 -1
  77. data/app/controllers/collavre/llm_models_controller.rb +8 -0
  78. data/app/controllers/collavre/llm_usages_controller.rb +51 -0
  79. data/app/controllers/collavre/notices_controller.rb +20 -0
  80. data/app/controllers/collavre/onboarding_replays_controller.rb +10 -0
  81. data/app/controllers/collavre/public_assets_controller.rb +48 -4
  82. data/app/controllers/collavre/seo_controller.rb +83 -0
  83. data/app/controllers/collavre/sessions_controller.rb +5 -9
  84. data/app/controllers/collavre/tasks_controller.rb +40 -6
  85. data/app/controllers/collavre/topics_controller.rb +136 -74
  86. data/app/controllers/collavre/user_creative_preferences_controller.rb +182 -31
  87. data/app/controllers/collavre/user_notices_controller.rb +64 -0
  88. data/app/controllers/collavre/user_themes_controller.rb +11 -7
  89. data/app/controllers/collavre/users_controller.rb +1 -0
  90. data/app/controllers/concerns/collavre/active_storage_access.rb +127 -0
  91. data/app/controllers/concerns/collavre/api/claude_channel_prompts.rb +209 -0
  92. data/app/controllers/concerns/collavre/blob_access_control.rb +57 -0
  93. data/app/controllers/concerns/collavre/comments/approval_actions.rb +24 -22
  94. data/app/controllers/concerns/collavre/comments/approval_gate_actions.rb +29 -0
  95. data/app/controllers/concerns/collavre/comments/batch_operations.rb +16 -4
  96. data/app/controllers/concerns/collavre/comments/comment_scoping.rb +4 -2
  97. data/app/controllers/concerns/collavre/comments/conversion.rb +1 -1
  98. data/app/controllers/concerns/collavre/comments/history_topic_guard.rb +22 -0
  99. data/app/controllers/concerns/collavre/creative_destroyable.rb +41 -0
  100. data/app/controllers/concerns/collavre/creative_permission_guard.rb +6 -0
  101. data/app/controllers/concerns/collavre/creative_type_editable.rb +76 -0
  102. data/app/controllers/concerns/collavre/loopback_request.rb +32 -0
  103. data/app/controllers/concerns/collavre/origin_verification.rb +29 -0
  104. data/app/controllers/concerns/collavre/permission_update_response.rb +23 -0
  105. data/app/controllers/concerns/collavre/proxy_error_response.rb +14 -0
  106. data/app/controllers/concerns/collavre/public_creative_page.rb +52 -0
  107. data/app/controllers/concerns/collavre/public_creative_redirectable.rb +31 -0
  108. data/app/controllers/concerns/collavre/successful_authentication.rb +12 -0
  109. data/app/controllers/concerns/collavre/svg_streaming.rb +39 -0
  110. data/app/controllers/concerns/collavre/topics/reserved_topic_guard.rb +52 -0
  111. data/app/controllers/concerns/collavre/user_creative_preferences_controller/persistence.rb +98 -0
  112. data/app/controllers/concerns/collavre/users_controller/admin_operations.rb +38 -12
  113. data/app/controllers/concerns/collavre/users_controller/ai_profile_access.rb +25 -0
  114. data/app/controllers/concerns/collavre/users_controller/ai_user_management.rb +182 -32
  115. data/app/controllers/concerns/collavre/users_controller/contact_management.rb +31 -18
  116. data/app/controllers/concerns/collavre/users_controller/profile_and_settings.rb +15 -15
  117. data/app/controllers/concerns/collavre/users_controller/registration.rb +9 -13
  118. data/app/controllers/concerns/collavre/workflow_editable.rb +112 -0
  119. data/app/errors/collavre/approval_gate_pending_error.rb +12 -0
  120. data/app/errors/collavre/approval_pending_error.rb +5 -2
  121. data/app/errors/collavre/screening_pending_error.rb +17 -0
  122. data/app/errors/collavre/task_suspended_error.rb +11 -0
  123. data/app/errors/collavre/turn_deadline_error.rb +20 -0
  124. data/app/helpers/collavre/activity_logs_helper.rb +26 -0
  125. data/app/helpers/collavre/application_helper.rb +45 -41
  126. data/app/helpers/collavre/creative_move_helper.rb +15 -0
  127. data/app/helpers/collavre/creatives_helper.rb +158 -72
  128. data/app/helpers/collavre/navigation_helper.rb +134 -0
  129. data/app/helpers/collavre/public_assets_helper.rb +14 -0
  130. data/app/helpers/collavre/public_creatives_helper.rb +44 -0
  131. data/app/javascript/__tests__/slide_view.test.js +30 -0
  132. data/app/javascript/collavre.js +4 -2
  133. data/app/javascript/collavre_pdf_viewer.js +3 -0
  134. data/app/javascript/collavre_pdf_worker.js +1 -0
  135. data/app/javascript/comments/__tests__/user_menu.test.js +137 -0
  136. data/app/javascript/comments/user_menu.js +205 -0
  137. data/app/javascript/components/EmojiPicker.js +90 -0
  138. data/app/javascript/components/HeadingButtons.js +45 -0
  139. data/app/javascript/components/HistoryButtons.jsx +26 -0
  140. data/app/javascript/components/ImageResizer.jsx +11 -30
  141. data/app/javascript/components/InlineLexicalEditor.jsx +29 -78
  142. data/app/javascript/components/__tests__/EmojiPicker.test.js +127 -0
  143. data/app/javascript/components/__tests__/HeadingButtons.test.js +111 -0
  144. data/app/javascript/components/__tests__/creative_tree_row_document_view.test.js +814 -0
  145. data/app/javascript/components/__tests__/creative_tree_row_parent_id.test.js +32 -2
  146. data/app/javascript/components/__tests__/creative_tree_row_progress_toggle.test.js +255 -0
  147. data/app/javascript/components/__tests__/document_row_swipe.test.js +206 -0
  148. data/app/javascript/components/creative_header_progress.js +24 -0
  149. data/app/javascript/components/creative_tree_row.js +96 -99
  150. data/app/javascript/components/creative_tree_row_document_view.js +251 -0
  151. data/app/javascript/components/document_row_swipe.js +107 -0
  152. data/app/javascript/components/plugins/__tests__/creative_links_plugin.test.js +59 -0
  153. data/app/javascript/components/plugins/__tests__/image_upload_plugin.test.js +107 -0
  154. data/app/javascript/components/plugins/creative_links_plugin.js +24 -0
  155. data/app/javascript/components/plugins/image_upload_plugin.jsx +24 -28
  156. data/app/javascript/components/plugins/markdown_shortcuts_plugin.jsx +5 -2
  157. data/app/javascript/controllers/__tests__/agent_connection_controller.test.js +625 -0
  158. data/app/javascript/controllers/__tests__/agent_vendor_controller.test.js +94 -0
  159. data/app/javascript/controllers/__tests__/comment_agent_model_controller.test.js +148 -0
  160. data/app/javascript/controllers/__tests__/comment_approval_controls.test.js +45 -0
  161. data/app/javascript/controllers/__tests__/comment_user_menu_controller.test.js +285 -0
  162. data/app/javascript/controllers/__tests__/comment_version_controller.test.js +51 -0
  163. data/app/javascript/controllers/__tests__/controller_csrf_requests.test.js +137 -0
  164. data/app/javascript/controllers/__tests__/creative_history_controller.test.js +123 -0
  165. data/app/javascript/controllers/__tests__/creative_history_detail_controller.test.js +104 -0
  166. data/app/javascript/controllers/__tests__/creative_history_undo_controller.test.js +55 -0
  167. data/app/javascript/controllers/__tests__/creative_image_lightbox_controller.test.js +583 -0
  168. data/app/javascript/controllers/__tests__/creative_move_controller.test.js +713 -0
  169. data/app/javascript/controllers/__tests__/cron_badge_controller.test.js +436 -0
  170. data/app/javascript/controllers/__tests__/desktop_proxy_setup_controller.test.js +96 -0
  171. data/app/javascript/controllers/__tests__/document_preview_controller.test.js +33 -0
  172. data/app/javascript/controllers/__tests__/entity_list_controller.test.js +270 -0
  173. data/app/javascript/controllers/__tests__/expansion_controller.test.js +235 -0
  174. data/app/javascript/controllers/__tests__/index.test.js +48 -0
  175. data/app/javascript/controllers/__tests__/inline_creative_picker_controller.test.js +232 -0
  176. data/app/javascript/controllers/__tests__/last_visited_creative_controller.test.js +103 -0
  177. data/app/javascript/controllers/__tests__/link_creative_controller.test.js +354 -2
  178. data/app/javascript/controllers/__tests__/notice_bar_controller.test.js +1076 -0
  179. data/app/javascript/controllers/__tests__/popup_menu_controller.test.js +267 -0
  180. data/app/javascript/controllers/__tests__/progress_filter_controller.test.js +134 -0
  181. data/app/javascript/controllers/__tests__/search_popup_controller.test.js +324 -0
  182. data/app/javascript/controllers/__tests__/share_modal_public_link.test.js +113 -0
  183. data/app/javascript/controllers/__tests__/share_user_search_controller.test.js +17 -0
  184. data/app/javascript/controllers/__tests__/tabs_controller.test.js +137 -0
  185. data/app/javascript/controllers/__tests__/tool_category_controller.test.js +116 -0
  186. data/app/javascript/controllers/__tests__/topic_list_controller.test.js +125 -0
  187. data/app/javascript/controllers/__tests__/workspace_tree_controller.test.js +1746 -0
  188. data/app/javascript/controllers/agent_auth_controller.js +245 -0
  189. data/app/javascript/controllers/agent_connection_controller.js +197 -0
  190. data/app/javascript/controllers/agent_vendor_controller.js +69 -0
  191. data/app/javascript/controllers/comment_agent_model_controller.js +87 -0
  192. data/app/javascript/controllers/comment_controller.js +34 -75
  193. data/app/javascript/controllers/comment_image_lightbox_controller.js +15 -0
  194. data/app/javascript/controllers/comment_user_menu_controller.js +122 -0
  195. data/app/javascript/controllers/comment_version_controller.js +10 -8
  196. data/app/javascript/controllers/comments/__tests__/comment_form_request.test.js +72 -0
  197. data/app/javascript/controllers/comments/__tests__/comment_list_response.test.js +30 -0
  198. data/app/javascript/controllers/comments/__tests__/comment_read_tracker.test.js +71 -0
  199. data/app/javascript/controllers/comments/__tests__/contexts_controller_context_list.test.js +466 -0
  200. data/app/javascript/controllers/comments/__tests__/contexts_controller_dnd.test.js +484 -0
  201. data/app/javascript/controllers/comments/__tests__/creative_quote_review_controller.test.js +111 -0
  202. data/app/javascript/controllers/comments/__tests__/drop_trigger_controller.test.js +48 -0
  203. data/app/javascript/controllers/comments/__tests__/feature_cards_controller.test.js +223 -0
  204. data/app/javascript/controllers/comments/__tests__/form_controller_default_placeholder.test.js +83 -0
  205. data/app/javascript/controllers/comments/__tests__/form_controller_double_submit.test.js +38 -0
  206. data/app/javascript/controllers/comments/__tests__/form_controller_draft.test.js +2254 -0
  207. data/app/javascript/controllers/comments/__tests__/form_controller_ime_send.test.js +324 -0
  208. data/app/javascript/controllers/comments/__tests__/form_controller_review.test.js +377 -2
  209. data/app/javascript/controllers/comments/__tests__/form_controller_search_submit.test.js +106 -0
  210. data/app/javascript/controllers/comments/__tests__/form_controller_stashed_draft.test.js +134 -0
  211. data/app/javascript/controllers/comments/__tests__/form_controller_stashed_draft_creative_switch.test.js +166 -0
  212. data/app/javascript/controllers/comments/__tests__/form_controller_stashed_draft_failure.test.js +183 -0
  213. data/app/javascript/controllers/comments/__tests__/form_draft_manager.test.js +154 -0
  214. data/app/javascript/controllers/comments/__tests__/inline_login_preservation.test.js +23 -0
  215. data/app/javascript/controllers/comments/__tests__/list_controller_approval_gate.test.js +51 -0
  216. data/app/javascript/controllers/comments/__tests__/list_controller_batch_delete.test.js +97 -0
  217. data/app/javascript/controllers/comments/__tests__/list_controller_deep_link_topic.test.js +467 -0
  218. data/app/javascript/controllers/comments/__tests__/list_controller_dnd.test.js +26 -0
  219. data/app/javascript/controllers/comments/__tests__/list_controller_docked.test.js +168 -0
  220. data/app/javascript/controllers/comments/__tests__/list_controller_load_result.test.js +56 -0
  221. data/app/javascript/controllers/comments/__tests__/list_controller_loading.test.js +43 -0
  222. data/app/javascript/controllers/comments/__tests__/list_controller_prev_message.test.js +217 -1
  223. data/app/javascript/controllers/comments/__tests__/list_controller_read_pointer.test.js +288 -0
  224. data/app/javascript/controllers/comments/__tests__/list_controller_resend.test.js +69 -0
  225. data/app/javascript/controllers/comments/__tests__/list_controller_resume_pagination.test.js +191 -0
  226. data/app/javascript/controllers/comments/__tests__/list_controller_stream_archived.test.js +221 -0
  227. data/app/javascript/controllers/comments/__tests__/list_controller_stream_search.test.js +104 -0
  228. data/app/javascript/controllers/comments/__tests__/loading_controller.test.js +83 -0
  229. data/app/javascript/controllers/comments/__tests__/mention_menu_controller.test.js +59 -0
  230. data/app/javascript/controllers/comments/__tests__/placeholder_controller.test.js +79 -0
  231. data/app/javascript/controllers/comments/__tests__/popup_controller.test.js +1070 -3
  232. data/app/javascript/controllers/comments/__tests__/popup_exit_target.test.js +118 -0
  233. data/app/javascript/controllers/comments/__tests__/popup_focus.test.js +72 -0
  234. data/app/javascript/controllers/comments/__tests__/popup_fullscreen.test.js +320 -0
  235. data/app/javascript/controllers/comments/__tests__/popup_initial_url.test.js +103 -0
  236. data/app/javascript/controllers/comments/__tests__/popup_resume_resync.test.js +154 -0
  237. data/app/javascript/controllers/comments/__tests__/presence_controller.test.js +775 -15
  238. data/app/javascript/controllers/comments/__tests__/presence_controller_agent_online.test.js +244 -0
  239. data/app/javascript/controllers/comments/__tests__/presence_controller_participant_list.test.js +570 -0
  240. data/app/javascript/controllers/comments/__tests__/resend_action.test.js +137 -0
  241. data/app/javascript/controllers/comments/__tests__/review_quotes_store.test.js +27 -0
  242. data/app/javascript/controllers/comments/__tests__/run_options_controller.test.js +248 -0
  243. data/app/javascript/controllers/comments/__tests__/topics_controller_action_errors.test.js +6 -0
  244. data/app/javascript/controllers/comments/__tests__/topics_controller_archived.test.js +924 -0
  245. data/app/javascript/controllers/comments/__tests__/topics_controller_creation_container.test.js +165 -0
  246. data/app/javascript/controllers/comments/__tests__/topics_controller_creation_scroll.test.js +92 -0
  247. data/app/javascript/controllers/comments/__tests__/topics_controller_delete.test.js +361 -0
  248. data/app/javascript/controllers/comments/__tests__/topics_controller_dnd.test.js +120 -0
  249. data/app/javascript/controllers/comments/__tests__/topics_controller_docked.test.js +32 -0
  250. data/app/javascript/controllers/comments/__tests__/topics_controller_last_topic_echo.test.js +1938 -0
  251. data/app/javascript/controllers/comments/__tests__/topics_controller_primary_agent.test.js +112 -2
  252. data/app/javascript/controllers/comments/__tests__/topics_controller_scroll.test.js +199 -0
  253. data/app/javascript/controllers/comments/__tests__/topics_controller_selection_race.test.js +1253 -0
  254. data/app/javascript/controllers/comments/__tests__/topics_controller_topic_list.test.js +313 -0
  255. data/app/javascript/controllers/comments/approval_request_options.js +12 -0
  256. data/app/javascript/controllers/comments/comment_form_request.js +26 -0
  257. data/app/javascript/controllers/comments/comment_list_response.js +20 -0
  258. data/app/javascript/controllers/comments/comment_read_tracker.js +143 -0
  259. data/app/javascript/controllers/comments/contexts_controller.js +338 -226
  260. data/app/javascript/controllers/comments/creative_chat_navigation.js +50 -0
  261. data/app/javascript/controllers/comments/creative_quote_review_controller.js +30 -0
  262. data/app/javascript/controllers/comments/drop_trigger_controller.js +2 -7
  263. data/app/javascript/controllers/comments/feature_cards_controller.js +83 -0
  264. data/app/javascript/controllers/comments/form_controller.js +375 -175
  265. data/app/javascript/controllers/comments/form_draft_manager.js +269 -0
  266. data/app/javascript/controllers/comments/form_draft_migration.js +142 -0
  267. data/app/javascript/controllers/comments/form_draft_persistence.js +228 -0
  268. data/app/javascript/controllers/comments/inline_login_preservation.js +18 -0
  269. data/app/javascript/controllers/comments/list_controller.js +283 -186
  270. data/app/javascript/controllers/comments/loading_controller.js +48 -0
  271. data/app/javascript/controllers/comments/mention_menu_controller.js +1 -24
  272. data/app/javascript/controllers/comments/optimistic_removal.js +15 -0
  273. data/app/javascript/controllers/comments/pagination_state.js +20 -0
  274. data/app/javascript/controllers/comments/placeholder_controller.js +30 -0
  275. data/app/javascript/controllers/comments/popup_controller.js +379 -473
  276. data/app/javascript/controllers/comments/popup_creative_progress.js +51 -0
  277. data/app/javascript/controllers/comments/popup_exit_target.js +92 -0
  278. data/app/javascript/controllers/comments/popup_focus.js +5 -0
  279. data/app/javascript/controllers/comments/popup_fullscreen.js +335 -0
  280. data/app/javascript/controllers/comments/popup_resume_resync.js +28 -0
  281. data/app/javascript/controllers/comments/popup_url_open.js +22 -0
  282. data/app/javascript/controllers/comments/presence_controller.js +598 -183
  283. data/app/javascript/controllers/comments/previous_message_navigation.js +154 -0
  284. data/app/javascript/controllers/comments/resend_action.js +63 -0
  285. data/app/javascript/controllers/comments/review_quotes_store.js +11 -8
  286. data/app/javascript/controllers/comments/run_options_controller.js +145 -0
  287. data/app/javascript/controllers/comments/topics/last_topic_save.js +124 -0
  288. data/app/javascript/controllers/comments/topics/reserved_topic_deletion.js +25 -0
  289. data/app/javascript/controllers/comments/topics/save_order_state.js +127 -0
  290. data/app/javascript/controllers/comments/topics/selection_restore_state.js +42 -0
  291. data/app/javascript/controllers/comments/topics/self_echo_state.js +24 -0
  292. data/app/javascript/controllers/comments/topics_controller.js +1723 -240
  293. data/app/javascript/controllers/common_popup_controller.js +2 -2
  294. data/app/javascript/controllers/creative_history_controller.js +57 -0
  295. data/app/javascript/controllers/creative_history_detail_controller.js +41 -0
  296. data/app/javascript/controllers/creative_history_undo_controller.js +19 -0
  297. data/app/javascript/controllers/creative_image_lightbox_controller.js +114 -0
  298. data/app/javascript/controllers/creative_move_controller.js +202 -0
  299. data/app/javascript/controllers/creatives/__tests__/document_selection_controller.test.js +241 -0
  300. data/app/javascript/controllers/creatives/__tests__/document_view_controller.test.js +173 -0
  301. data/app/javascript/controllers/creatives/__tests__/drag_drop_controller.test.js +74 -0
  302. data/app/javascript/controllers/creatives/__tests__/import_controller.test.js +48 -0
  303. data/app/javascript/controllers/creatives/__tests__/select_mode_controller.test.js +250 -0
  304. data/app/javascript/controllers/creatives/__tests__/tree_controller.test.js +1210 -2
  305. data/app/javascript/controllers/creatives/__tests__/workflow_rule_controller.test.js +322 -0
  306. data/app/javascript/controllers/creatives/__tests__/write_access_request_controller.test.js +125 -0
  307. data/app/javascript/controllers/creatives/document_selection_controller.js +77 -0
  308. data/app/javascript/controllers/creatives/document_view_controller.js +82 -0
  309. data/app/javascript/controllers/creatives/drag_drop_controller.js +13 -19
  310. data/app/javascript/controllers/creatives/expansion_controller.js +21 -19
  311. data/app/javascript/controllers/creatives/import_controller.js +1 -1
  312. data/app/javascript/controllers/creatives/select_mode_controller.js +38 -6
  313. data/app/javascript/controllers/creatives/tree_controller.js +260 -59
  314. data/app/javascript/controllers/creatives/workflow_rule_controller.js +96 -0
  315. data/app/javascript/controllers/creatives/workflow_rule_form.js +183 -0
  316. data/app/javascript/controllers/creatives/write_access_request_controller.js +58 -0
  317. data/app/javascript/controllers/cron_badge_controller.js +187 -0
  318. data/app/javascript/controllers/desktop_proxy_setup_controller.js +51 -0
  319. data/app/javascript/controllers/document_preview_controller.js +16 -0
  320. data/app/javascript/controllers/entity_list_controller.js +147 -0
  321. data/app/javascript/controllers/gateway_check_controller.js +34 -0
  322. data/app/javascript/controllers/image_lightbox_controller.js +29 -70
  323. data/app/javascript/controllers/image_lightbox_values.js +13 -0
  324. data/app/javascript/controllers/index.js +106 -8
  325. data/app/javascript/controllers/inline_creative_picker_controller.js +110 -0
  326. data/app/javascript/controllers/last_visited_creative_controller.js +58 -0
  327. data/app/javascript/controllers/link_creative_controller.js +102 -28
  328. data/app/javascript/controllers/notice_bar_controller.js +364 -0
  329. data/app/javascript/controllers/org_chart_controller.js +2 -23
  330. data/app/javascript/controllers/popup_menu_controller.js +128 -45
  331. data/app/javascript/controllers/progress_filter_controller.js +12 -22
  332. data/app/javascript/controllers/search_popup_controller.js +72 -50
  333. data/app/javascript/controllers/searchable_list_controller.js +30 -0
  334. data/app/javascript/controllers/share_modal_controller.js +18 -56
  335. data/app/javascript/controllers/share_user_search_controller.js +3 -13
  336. data/app/javascript/controllers/tabs_controller.js +5 -1
  337. data/app/javascript/controllers/tool_category_controller.js +39 -0
  338. data/app/javascript/controllers/topic_list_controller.js +42 -31
  339. data/app/javascript/controllers/topic_search_controller.js +2 -4
  340. data/app/javascript/controllers/workspace_tree_controller.js +803 -0
  341. data/app/javascript/creatives/__tests__/row_progress.test.js +155 -0
  342. data/app/javascript/creatives/__tests__/tree_reload_events.test.js +101 -0
  343. data/app/javascript/creatives/__tests__/tree_renderer.test.js +195 -0
  344. data/app/javascript/creatives/__tests__/tree_view_state.test.js +277 -0
  345. data/app/javascript/creatives/branch_expansion.js +39 -0
  346. data/app/javascript/creatives/drag_drop/__tests__/command_integration.test.js +194 -0
  347. data/app/javascript/creatives/drag_drop/__tests__/creative_tree_drop.test.js +964 -0
  348. data/app/javascript/creatives/drag_drop/__tests__/drop_completion_empty_state.test.js +98 -0
  349. data/app/javascript/creatives/drag_drop/__tests__/event_handlers_data.test.js +329 -0
  350. data/app/javascript/creatives/drag_drop/__tests__/file_drop.test.js +99 -0
  351. data/app/javascript/creatives/drag_drop/__tests__/hover_expand.test.js +134 -0
  352. data/app/javascript/creatives/drag_drop/__tests__/indicator.test.js +36 -0
  353. data/app/javascript/creatives/drag_drop/__tests__/move_command.test.js +471 -0
  354. data/app/javascript/creatives/drag_drop/__tests__/move_command_dom_recovery.test.js +346 -0
  355. data/app/javascript/creatives/drag_drop/__tests__/move_command_queued_saves.test.js +201 -0
  356. data/app/javascript/creatives/drag_drop/__tests__/move_feedback.test.js +79 -0
  357. data/app/javascript/creatives/drag_drop/__tests__/source_window_bundle.test.js +141 -0
  358. data/app/javascript/creatives/drag_drop/__tests__/state.test.js +36 -0
  359. data/app/javascript/creatives/drag_drop/__tests__/topic_move_released_agent.test.js +125 -0
  360. data/app/javascript/creatives/drag_drop/__tests__/touch_tree_integration.test.js +209 -0
  361. data/app/javascript/creatives/drag_drop/__tests__/workspace_tree_adapter.test.js +517 -0
  362. data/app/javascript/creatives/drag_drop/__tests__/workspace_tree_dom.test.js +105 -0
  363. data/app/javascript/creatives/drag_drop/dom.js +9 -0
  364. data/app/javascript/creatives/drag_drop/event_handlers.js +291 -431
  365. data/app/javascript/creatives/drag_drop/file_drop.js +60 -0
  366. data/app/javascript/creatives/drag_drop/indicator.js +2 -1
  367. data/app/javascript/creatives/drag_drop/move_command.js +297 -0
  368. data/app/javascript/creatives/drag_drop/move_feedback.js +22 -0
  369. data/app/javascript/creatives/drag_drop/operations.js +73 -0
  370. data/app/javascript/creatives/drag_drop/state.js +10 -1
  371. data/app/javascript/creatives/drag_drop/workspace_tree_adapter.js +161 -0
  372. data/app/javascript/creatives/drag_drop/workspace_tree_dom.js +52 -0
  373. data/app/javascript/creatives/row_progress.js +96 -0
  374. data/app/javascript/creatives/tree_reload_events.js +42 -0
  375. data/app/javascript/creatives/tree_renderer.js +132 -40
  376. data/app/javascript/creatives/tree_view_state.js +87 -0
  377. data/app/javascript/lib/__tests__/chat_drafts.test.js +1458 -0
  378. data/app/javascript/lib/__tests__/common_popup_bounds.test.js +8 -3
  379. data/app/javascript/lib/__tests__/common_popup_placement.test.js +374 -0
  380. data/app/javascript/lib/__tests__/common_popup_reshow.test.js +131 -0
  381. data/app/javascript/lib/__tests__/creative_link_prefetch.test.js +46 -0
  382. data/app/javascript/lib/__tests__/creative_quote.test.js +343 -0
  383. data/app/javascript/lib/__tests__/creative_title.test.js +15 -0
  384. data/app/javascript/lib/__tests__/entity_list_popup.test.js +47 -0
  385. data/app/javascript/lib/__tests__/html_escape.test.js +29 -0
  386. data/app/javascript/lib/__tests__/last_visited_creative.test.js +87 -0
  387. data/app/javascript/lib/__tests__/permission_update.test.js +38 -0
  388. data/app/javascript/lib/__tests__/popup_toggle_guard.test.js +82 -0
  389. data/app/javascript/lib/__tests__/ppt_background.test.js +86 -0
  390. data/app/javascript/lib/__tests__/ppt_connector.test.js +80 -0
  391. data/app/javascript/lib/__tests__/ppt_formatting.test.js +246 -0
  392. data/app/javascript/lib/__tests__/ppt_media.test.js +86 -0
  393. data/app/javascript/lib/__tests__/quote_selection_popup.test.js +100 -0
  394. data/app/javascript/lib/__tests__/skill_html_entities.test.js +23 -0
  395. data/app/javascript/lib/__tests__/touch_drag_tap.test.js +31 -0
  396. data/app/javascript/lib/__tests__/touch_drag_targets.test.js +293 -0
  397. data/app/javascript/lib/__tests__/turbo_stream_actions.test.js +218 -0
  398. data/app/javascript/lib/__tests__/workspace_scroll_restoration.test.js +310 -0
  399. data/app/javascript/lib/__tests__/workspace_tree_removal.test.js +108 -0
  400. data/app/javascript/lib/api/__tests__/creatives.test.js +94 -0
  401. data/app/javascript/lib/api/__tests__/csrf_fetch.test.js +27 -0
  402. data/app/javascript/lib/api/__tests__/drag_drop.test.js +136 -0
  403. data/app/javascript/lib/api/__tests__/expansion_intent.test.js +75 -0
  404. data/app/javascript/lib/api/__tests__/expansion_save_queue.test.js +110 -0
  405. data/app/javascript/lib/api/__tests__/queue_manager.test.js +237 -0
  406. data/app/javascript/lib/api/__tests__/queue_reconciliation.test.js +57 -0
  407. data/app/javascript/lib/api/__tests__/queue_recovery.test.js +28 -0
  408. data/app/javascript/lib/api/__tests__/topics_save_last_topic.test.js +112 -0
  409. data/app/javascript/lib/api/creatives.js +35 -13
  410. data/app/javascript/lib/api/csrf_fetch.js +6 -4
  411. data/app/javascript/lib/api/drag_drop.js +25 -1
  412. data/app/javascript/lib/api/expansion_intent.js +28 -0
  413. data/app/javascript/lib/api/expansion_save_queue.js +51 -0
  414. data/app/javascript/lib/api/queue_completion.js +30 -0
  415. data/app/javascript/lib/api/queue_manager.js +34 -50
  416. data/app/javascript/lib/api/queue_reconciliation.js +44 -0
  417. data/app/javascript/lib/api/queue_recovery.js +24 -0
  418. data/app/javascript/lib/api/topics.js +63 -5
  419. data/app/javascript/lib/chat_drafts.js +786 -0
  420. data/app/javascript/lib/common_popup.js +164 -25
  421. data/app/javascript/lib/creative_link_prefetch.js +27 -0
  422. data/app/javascript/lib/creative_quote.js +135 -0
  423. data/app/javascript/lib/creative_title.js +9 -0
  424. data/app/javascript/lib/creative_tree_invalidation.js +4 -0
  425. data/app/javascript/lib/dnd/__tests__/bundle_image.test.js +32 -0
  426. data/app/javascript/lib/dnd/__tests__/envelope.test.js +262 -0
  427. data/app/javascript/lib/dnd/__tests__/hit_test.test.js +69 -0
  428. data/app/javascript/lib/dnd/__tests__/preview.test.js +22 -0
  429. data/app/javascript/lib/dnd/__tests__/registry.test.js +797 -0
  430. data/app/javascript/lib/dnd/__tests__/session.test.js +213 -0
  431. data/app/javascript/lib/dnd/__tests__/session_without_window.test.js +15 -0
  432. data/app/javascript/lib/dnd/__tests__/touch_bridge.test.js +532 -0
  433. data/app/javascript/lib/dnd/creative_label.js +21 -0
  434. data/app/javascript/lib/dnd/envelope.js +194 -0
  435. data/app/javascript/lib/dnd/hit_test.js +42 -0
  436. data/app/javascript/lib/dnd/preview.js +10 -0
  437. data/app/javascript/lib/dnd/registry.js +315 -0
  438. data/app/javascript/lib/dnd/session.js +152 -0
  439. data/app/javascript/lib/dnd/touch_bridge.js +184 -0
  440. data/app/javascript/lib/editor/__tests__/code_languages.test.js +1 -5
  441. data/app/javascript/lib/editor/code_languages.js +0 -1
  442. data/app/javascript/lib/entity_list_popup.js +45 -0
  443. data/app/javascript/lib/html_escape.js +12 -0
  444. data/app/javascript/lib/image_lightbox_pan.js +31 -0
  445. data/app/javascript/lib/last_visited_creative.js +97 -0
  446. data/app/javascript/lib/lexical/__tests__/attachment_payload.test.js +12 -189
  447. data/app/javascript/lib/lexical/__tests__/creative_link_drop.test.js +414 -0
  448. data/app/javascript/lib/lexical/__tests__/creative_link_node.test.js +212 -0
  449. data/app/javascript/lib/lexical/__tests__/creative_link_trigger.test.js +133 -0
  450. data/app/javascript/lib/lexical/__tests__/file_drop.test.js +288 -0
  451. data/app/javascript/lib/lexical/__tests__/link_toolbar.test.js +62 -0
  452. data/app/javascript/lib/lexical/attachment_node.jsx +21 -42
  453. data/app/javascript/lib/lexical/attachment_payload.js +0 -274
  454. data/app/javascript/lib/lexical/attachment_presentation.js +57 -0
  455. data/app/javascript/lib/lexical/creative_link_drop.js +98 -0
  456. data/app/javascript/lib/lexical/creative_link_node.js +127 -0
  457. data/app/javascript/lib/lexical/creative_link_trigger.js +98 -0
  458. data/app/javascript/lib/lexical/file_drop.js +106 -0
  459. data/app/javascript/lib/lexical/link_toolbar.js +23 -0
  460. data/app/javascript/lib/lexical/ppt_attachment.js +26 -0
  461. data/app/javascript/lib/lexical/upload_anchor_node.js +16 -0
  462. data/app/javascript/lib/lexical/upload_nodes.js +6 -0
  463. data/app/javascript/lib/media_loading.js +25 -0
  464. data/app/javascript/lib/permission_update.js +21 -0
  465. data/app/javascript/lib/popup_toggle_guard.js +56 -0
  466. data/app/javascript/lib/ppt_background.js +48 -0
  467. data/app/javascript/lib/ppt_chart.js +59 -0
  468. data/app/javascript/lib/ppt_connector.js +63 -0
  469. data/app/javascript/lib/ppt_formatting.js +138 -0
  470. data/app/javascript/lib/ppt_media.js +61 -0
  471. data/app/javascript/lib/quote_selection_popup.js +67 -0
  472. data/app/javascript/lib/responsive_images.js +0 -12
  473. data/app/javascript/lib/search_combobox.js +61 -0
  474. data/app/javascript/lib/share_modal_links.js +35 -0
  475. data/app/javascript/lib/touch_drag.js +119 -37
  476. data/app/javascript/lib/turbo_stream_actions.js +41 -16
  477. data/app/javascript/lib/utils/__tests__/attachment_rendering.test.js +189 -0
  478. data/app/javascript/lib/utils/__tests__/creative_link_navigation.test.js +301 -0
  479. data/app/javascript/lib/utils/__tests__/filter_navigation.test.js +226 -0
  480. data/app/javascript/lib/utils/__tests__/focus.test.js +144 -0
  481. data/app/javascript/lib/utils/__tests__/markdown.test.js +44 -0
  482. data/app/javascript/lib/utils/__tests__/mermaid_math.test.js +24 -0
  483. data/app/javascript/lib/utils/__tests__/sanitize_description.test.js +10 -0
  484. data/app/javascript/lib/utils/creative_link_navigation.js +64 -0
  485. data/app/javascript/lib/utils/filter_navigation.js +114 -0
  486. data/app/javascript/lib/utils/focus.js +36 -0
  487. data/app/javascript/lib/utils/markdown.js +0 -4
  488. data/app/javascript/lib/utils/sanitize_description.js +12 -4
  489. data/app/javascript/lib/utils/workspace_chat_navigation.js +25 -0
  490. data/app/javascript/lib/viewport_region.js +20 -0
  491. data/app/javascript/lib/workspace_scroll_restoration.js +134 -0
  492. data/app/javascript/lib/workspace_tree_removal.js +91 -0
  493. data/app/javascript/lib/workspace_tree_visibility.js +25 -0
  494. data/app/javascript/modules/__tests__/background_creative_create.test.js +114 -0
  495. data/app/javascript/modules/__tests__/command_args_form.test.js +96 -0
  496. data/app/javascript/modules/__tests__/command_menu_creative_picker.test.js +62 -0
  497. data/app/javascript/modules/__tests__/command_menu_details.test.js +199 -0
  498. data/app/javascript/modules/__tests__/command_menu_typeahead.test.js +267 -0
  499. data/app/javascript/modules/__tests__/creative_inline_dataset.test.js +35 -0
  500. data/app/javascript/modules/__tests__/creative_inline_payload.test.js +35 -0
  501. data/app/javascript/modules/__tests__/creative_link_picker.test.js +85 -0
  502. data/app/javascript/modules/__tests__/creative_row_editor_archive.test.js +391 -0
  503. data/app/javascript/modules/__tests__/creative_row_editor_background_create.test.js +2147 -0
  504. data/app/javascript/modules/__tests__/creative_row_editor_delegated_clicks.test.js +158 -0
  505. data/app/javascript/modules/__tests__/creative_row_editor_empty_state_recovery.test.js +214 -0
  506. data/app/javascript/modules/__tests__/creative_row_editor_empty_state_removal.test.js +468 -0
  507. data/app/javascript/modules/__tests__/creative_row_editor_instant_reveal.test.js +186 -0
  508. data/app/javascript/modules/__tests__/creative_row_editor_save_paths.test.js +1132 -0
  509. data/app/javascript/modules/__tests__/creative_row_editor_session.test.js +144 -0
  510. data/app/javascript/modules/__tests__/creative_save_state.test.js +165 -0
  511. data/app/javascript/modules/__tests__/creative_tree_dom.test.js +30 -0
  512. data/app/javascript/modules/__tests__/creative_tree_empty_state.test.js +186 -0
  513. data/app/javascript/modules/__tests__/creative_type_editor.test.js +203 -0
  514. data/app/javascript/modules/__tests__/dom_listener_registry.test.js +67 -0
  515. data/app/javascript/modules/__tests__/failed_creative_save.test.js +65 -0
  516. data/app/javascript/modules/__tests__/html_content_empty.test.js +11 -0
  517. data/app/javascript/modules/__tests__/integration_wizard.test.js +139 -0
  518. data/app/javascript/modules/__tests__/mention_menu.test.js +190 -0
  519. data/app/javascript/modules/__tests__/notice_bar_motion.test.js +132 -0
  520. data/app/javascript/modules/__tests__/notice_bar_sheet.test.js +108 -0
  521. data/app/javascript/modules/__tests__/notice_bar_spotlight.test.js +109 -0
  522. data/app/javascript/modules/__tests__/notice_bar_storage.test.js +31 -0
  523. data/app/javascript/modules/__tests__/notice_bar_toasts.test.js +71 -0
  524. data/app/javascript/modules/__tests__/notice_bar_view.test.js +89 -0
  525. data/app/javascript/modules/__tests__/pdf_assets.test.js +26 -0
  526. data/app/javascript/modules/__tests__/pdf_document.test.js +231 -0
  527. data/app/javascript/modules/__tests__/persisted_creative_tree.test.js +48 -0
  528. data/app/javascript/modules/__tests__/ppt_viewer.test.js +219 -0
  529. data/app/javascript/modules/__tests__/queued_creative_row.test.js +206 -0
  530. data/app/javascript/modules/__tests__/recovered_creative_position.test.js +57 -0
  531. data/app/javascript/modules/__tests__/slide_view_attachments.test.js +71 -0
  532. data/app/javascript/modules/__tests__/support/inline_editor_dom.js +183 -0
  533. data/app/javascript/modules/__tests__/viewer_navigation.test.js +42 -0
  534. data/app/javascript/modules/background_creative_create.js +326 -0
  535. data/app/javascript/modules/command_args_form.js +30 -40
  536. data/app/javascript/modules/command_menu.js +171 -55
  537. data/app/javascript/modules/creative_inline_dataset.js +34 -0
  538. data/app/javascript/modules/creative_inline_payload.js +10 -14
  539. data/app/javascript/modules/creative_link_picker.js +56 -0
  540. data/app/javascript/modules/creative_removal.js +39 -0
  541. data/app/javascript/modules/creative_row_editor.js +818 -634
  542. data/app/javascript/modules/creative_row_editor_delegated_clicks.js +87 -0
  543. data/app/javascript/modules/creative_save_state.js +141 -0
  544. data/app/javascript/modules/creative_tree_dom.js +4 -0
  545. data/app/javascript/modules/creative_tree_empty_state.js +80 -0
  546. data/app/javascript/modules/creative_type_editor.js +139 -0
  547. data/app/javascript/modules/dom_listener_registry.js +23 -0
  548. data/app/javascript/modules/failed_creative_save.js +42 -0
  549. data/app/javascript/modules/html_content_empty.js +5 -3
  550. data/app/javascript/modules/integration_wizard.js +68 -19
  551. data/app/javascript/modules/keep_goal_text.js +4 -0
  552. data/app/javascript/modules/lexical_inline_editor.jsx +9 -2
  553. data/app/javascript/modules/mention_menu.js +36 -25
  554. data/app/javascript/modules/notice_bar/__tests__/refresh.test.js +177 -0
  555. data/app/javascript/modules/notice_bar/motion.js +59 -0
  556. data/app/javascript/modules/notice_bar/refresh.js +53 -0
  557. data/app/javascript/modules/notice_bar/sheet.js +102 -0
  558. data/app/javascript/modules/notice_bar/spotlight.js +78 -0
  559. data/app/javascript/modules/notice_bar/storage.js +14 -0
  560. data/app/javascript/modules/notice_bar/toasts.js +49 -0
  561. data/app/javascript/modules/notice_bar/view.js +101 -0
  562. data/app/javascript/modules/pdf_document.js +65 -0
  563. data/app/javascript/modules/pending_creative_sync.js +23 -0
  564. data/app/javascript/modules/persisted_creative_tree.js +39 -0
  565. data/app/javascript/modules/ppt_viewer.js +76 -0
  566. data/app/javascript/modules/queued_creative_row.js +128 -0
  567. data/app/javascript/modules/recovered_creative_position.js +52 -0
  568. data/app/javascript/modules/slide_view.js +9 -3
  569. data/app/javascript/modules/viewer_navigation.js +16 -0
  570. data/app/javascript/slide_view.js +9 -0
  571. data/app/javascript/utils/__tests__/caret_anchor.test.js +75 -0
  572. data/app/javascript/utils/__tests__/chevron_icons.test.js +15 -0
  573. data/app/javascript/utils/caret_position.js +10 -0
  574. data/app/javascript/utils/chevron_icons.js +7 -0
  575. data/app/jobs/collavre/agent_provisioning_sync_job.rb +79 -0
  576. data/app/jobs/collavre/ai_agent_job.rb +346 -135
  577. data/app/jobs/collavre/approval_continuation_job.rb +102 -0
  578. data/app/jobs/collavre/approval_gate_resume_job.rb +22 -0
  579. data/app/jobs/collavre/async_approval_resume_job.rb +20 -0
  580. data/app/jobs/collavre/async_approval_sweep_job.rb +44 -0
  581. data/app/jobs/collavre/batch_destroy_comments_job.rb +17 -0
  582. data/app/jobs/collavre/cancel_offline_delegated_tasks_job.rb +34 -106
  583. data/app/jobs/collavre/cdn_invalidation_job.rb +62 -0
  584. data/app/jobs/collavre/claude_approval_resume_job.rb +15 -0
  585. data/app/jobs/collavre/comment_badges_broadcast_job.rb +28 -0
  586. data/app/jobs/collavre/comment_link_preview_job.rb +33 -0
  587. data/app/jobs/collavre/comment_notification_job.rb +14 -0
  588. data/app/jobs/collavre/comment_push_delivery_sweep_job.rb +25 -0
  589. data/app/jobs/collavre/compress_job.rb +36 -49
  590. data/app/jobs/collavre/creative_broadcast_job.rb +166 -56
  591. data/app/jobs/collavre/creative_history_notice_job.rb +40 -0
  592. data/app/jobs/collavre/creative_history_prune_job.rb +86 -0
  593. data/app/jobs/collavre/creative_tree_invalidation_job.rb +33 -0
  594. data/app/jobs/collavre/cron_action_job.rb +55 -20
  595. data/app/jobs/collavre/cron_scheduler_job.rb +44 -3
  596. data/app/jobs/collavre/drop_trigger_job.rb +54 -35
  597. data/app/jobs/collavre/endpoint_health_probe_job.rb +23 -0
  598. data/app/jobs/collavre/endpoint_health_sweep_job.rb +22 -0
  599. data/app/jobs/collavre/gateway_health_probe_job.rb +33 -0
  600. data/app/jobs/collavre/gateway_health_sweep_job.rb +22 -0
  601. data/app/jobs/collavre/inline_agent_replay_job.rb +32 -0
  602. data/app/jobs/collavre/keep_loop_reconcile_job.rb +57 -0
  603. data/app/jobs/collavre/merge_comments_job.rb +37 -44
  604. data/app/jobs/collavre/offline_task_sweep_job.rb +44 -0
  605. data/app/jobs/collavre/permission_cache_job.rb +19 -10
  606. data/app/jobs/collavre/purge_unreferenced_blob_job.rb +19 -0
  607. data/app/jobs/collavre/push_notification_job.rb +65 -20
  608. data/app/jobs/collavre/recover_interrupted_tasks_job.rb +79 -0
  609. data/app/jobs/collavre/restore_dropped_dispatches_job.rb +17 -0
  610. data/app/jobs/collavre/resume_suspended_tasks_job.rb +29 -0
  611. data/app/jobs/collavre/solid_queue_worker_watchdog_job.rb +108 -0
  612. data/app/jobs/collavre/stuck_detector_job.rb +1 -1
  613. data/app/jobs/collavre/touch_creative_subtree_job.rb +14 -0
  614. data/app/jobs/collavre/trigger_loop_check_job.rb +104 -105
  615. data/app/jobs/collavre/trigger_loop_verify_job.rb +51 -82
  616. data/app/jobs/collavre/workflow_outbox_job.rb +10 -0
  617. data/app/jobs/collavre/workflow_push_job.rb +11 -0
  618. data/app/jobs/collavre/workflow_sweep_job.rb +11 -0
  619. data/app/jobs/concerns/collavre/comment_serializable.rb +3 -1
  620. data/app/jobs/concerns/collavre/dispatch_admission.rb +95 -0
  621. data/app/jobs/concerns/collavre/trigger_loop_helpers.rb +178 -13
  622. data/app/models/collavre/agent_gateway.rb +276 -0
  623. data/app/models/collavre/agent_subscription.rb +4 -4
  624. data/app/models/collavre/agent_workspace.rb +245 -0
  625. data/app/models/collavre/blob_access_flag.rb +24 -0
  626. data/app/models/collavre/channel.rb +17 -0
  627. data/app/models/collavre/comment/approvable.rb +14 -1
  628. data/app/models/collavre/comment/approval_gate.rb +98 -0
  629. data/app/models/collavre/comment/batch_destruction.rb +173 -0
  630. data/app/models/collavre/comment/broadcastable.rb +40 -52
  631. data/app/models/collavre/comment/bulk_follow_up_jobs.rb +29 -0
  632. data/app/models/collavre/comment/claude_channel_permission.rb +59 -5
  633. data/app/models/collavre/comment/creative_quote.rb +55 -0
  634. data/app/models/collavre/comment/image_cdn_eviction.rb +36 -0
  635. data/app/models/collavre/comment/notifiable.rb +157 -79
  636. data/app/models/collavre/comment/suspension_notice.rb +20 -0
  637. data/app/models/collavre/comment/topic_membership.rb +61 -0
  638. data/app/models/collavre/comment/trigger_loop_resume.rb +77 -0
  639. data/app/models/collavre/comment.rb +350 -121
  640. data/app/models/collavre/comment_notification_delivery.rb +72 -0
  641. data/app/models/collavre/comment_presence_store/cache_batches.rb +64 -0
  642. data/app/models/collavre/comment_presence_store.rb +272 -12
  643. data/app/models/collavre/comment_read_pointer.rb +9 -15
  644. data/app/models/collavre/comment_version.rb +4 -0
  645. data/app/models/collavre/creative/ai_write_policy.rb +26 -0
  646. data/app/models/collavre/creative/contextual.rb +153 -0
  647. data/app/models/collavre/creative/describable.rb +42 -27
  648. data/app/models/collavre/creative/history_trackable.rb +104 -0
  649. data/app/models/collavre/creative/linkable.rb +21 -1
  650. data/app/models/collavre/creative/permissible.rb +23 -20
  651. data/app/models/collavre/creative/publishable.rb +65 -0
  652. data/app/models/collavre/creative/type_selectable.rb +25 -0
  653. data/app/models/collavre/creative.rb +55 -114
  654. data/app/models/collavre/creative_change.rb +55 -0
  655. data/app/models/collavre/creative_change_set.rb +71 -0
  656. data/app/models/collavre/creative_share.rb +128 -4
  657. data/app/models/collavre/current.rb +9 -1
  658. data/app/models/collavre/llm_model.rb +63 -0
  659. data/app/models/collavre/llm_usage/requester.rb +9 -0
  660. data/app/models/collavre/llm_usage.rb +33 -0
  661. data/app/models/collavre/mcp_tool.rb +25 -3
  662. data/app/models/collavre/orchestrator_policy.rb +25 -29
  663. data/app/models/collavre/preview_channel.rb +1 -15
  664. data/app/models/collavre/retired_task_execution.rb +9 -0
  665. data/app/models/collavre/system_setting.rb +72 -9
  666. data/app/models/collavre/task/approval_gate_cleanup.rb +23 -0
  667. data/app/models/collavre/task/async_approval.rb +30 -0
  668. data/app/models/collavre/task.rb +194 -29
  669. data/app/models/collavre/task_merged_instruction.rb +6 -0
  670. data/app/models/collavre/tool_usage/requester.rb +9 -0
  671. data/app/models/collavre/tool_usage.rb +69 -0
  672. data/app/models/collavre/topic.rb +89 -0
  673. data/app/models/collavre/user/ai_capabilities.rb +38 -0
  674. data/app/models/collavre/user.rb +114 -50
  675. data/app/models/collavre/user_creative_preference.rb +27 -1
  676. data/app/models/collavre/user_notice.rb +64 -0
  677. data/app/models/collavre/workflow/chain.rb +27 -0
  678. data/app/models/collavre/workflow/execution.rb +28 -0
  679. data/app/models/collavre/workflow/outbox.rb +43 -0
  680. data/app/models/collavre/workflow/receipt.rb +26 -0
  681. data/app/models/concerns/collavre/agent_liveness.rb +95 -0
  682. data/app/models/concerns/collavre/agent_run_options.rb +45 -0
  683. data/app/models/concerns/collavre/ai_agent_resolvable.rb +30 -7
  684. data/app/models/concerns/collavre/comment/dispatch_revocation.rb +99 -0
  685. data/app/models/concerns/collavre/comment/keep_loop.rb +166 -0
  686. data/app/models/concerns/collavre/creation_events.rb +25 -0
  687. data/app/models/concerns/collavre/creative/trigger_loop.rb +57 -0
  688. data/app/models/concerns/collavre/creative_history_topic.rb +34 -0
  689. data/app/models/concerns/collavre/creative_reserved_topics.rb +25 -0
  690. data/app/models/concerns/collavre/has_inbox_creative.rb +9 -1
  691. data/app/models/concerns/collavre/system_agent_identity.rb +13 -0
  692. data/app/models/concerns/collavre/task/replay_loop_completion.rb +78 -0
  693. data/app/models/concerns/collavre/task/trigger_loop_completion.rb +126 -0
  694. data/app/models/concerns/collavre/task/usage_attribution_tracking.rb +28 -0
  695. data/app/models/concerns/collavre/task/workflow_completion.rb +59 -0
  696. data/app/services/collavre/agent_health/endpoint_request.rb +64 -0
  697. data/app/services/collavre/agent_health/openai_endpoint_checker.rb +77 -0
  698. data/app/services/collavre/agent_health/probe.rb +47 -0
  699. data/app/services/collavre/agent_health.rb +43 -0
  700. data/app/services/collavre/agent_provisioning/archive.rb +108 -0
  701. data/app/services/collavre/agent_session_abort.rb +2 -2
  702. data/app/services/collavre/agent_tool_permission.rb +43 -0
  703. data/app/services/collavre/ai_agent/a2a_dispatcher.rb +38 -7
  704. data/app/services/collavre/ai_agent/agent_lifecycle_manager.rb +78 -6
  705. data/app/services/collavre/ai_agent/approval_conversation.rb +60 -0
  706. data/app/services/collavre/ai_agent/approval_gate_handler.rb +37 -0
  707. data/app/services/collavre/ai_agent/approval_handler.rb +20 -5
  708. data/app/services/collavre/ai_agent/budgeted_tree_renderer.rb +80 -0
  709. data/app/services/collavre/ai_agent/chat_history_window.rb +87 -0
  710. data/app/services/collavre/ai_agent/claude_channel_adapter.rb +67 -11
  711. data/app/services/collavre/ai_agent/creative_quote_context.rb +66 -0
  712. data/app/services/collavre/ai_agent/history_notice.rb +94 -0
  713. data/app/services/collavre/ai_agent/merged_trigger_comments.rb +164 -0
  714. data/app/services/collavre/ai_agent/message_builder.rb +217 -83
  715. data/app/services/collavre/ai_agent/prompt_budget.rb +142 -0
  716. data/app/services/collavre/ai_agent/referenced_creative_renderer.rb +101 -0
  717. data/app/services/collavre/ai_agent/reply_placeholder.rb +28 -0
  718. data/app/services/collavre/ai_agent/response_finalizer.rb +15 -1
  719. data/app/services/collavre/ai_agent/review_handler.rb +17 -17
  720. data/app/services/collavre/ai_agent/screening_approval_handler.rb +26 -0
  721. data/app/services/collavre/ai_agent/session_context_resolver.rb +6 -1
  722. data/app/services/collavre/ai_agent/session_provisioner.rb +3 -7
  723. data/app/services/collavre/ai_agent/task_claim_service.rb +84 -18
  724. data/app/services/collavre/ai_agent/task_reply_service.rb +90 -0
  725. data/app/services/collavre/ai_agent/task_workspace_user.rb +20 -0
  726. data/app/services/collavre/ai_agent/turn_screening.rb +132 -0
  727. data/app/services/collavre/ai_agent/workspace_authentication.rb +74 -0
  728. data/app/services/collavre/ai_agent_service.rb +113 -50
  729. data/app/services/collavre/ai_approval_summary.rb +51 -0
  730. data/app/services/collavre/ai_client/approval_gate.rb +90 -0
  731. data/app/services/collavre/ai_client/cli_tool_events.rb +100 -0
  732. data/app/services/collavre/ai_client/decision.rb +101 -0
  733. data/app/services/collavre/ai_client/error_handling.rb +67 -0
  734. data/app/services/collavre/ai_client/provider_context.rb +69 -0
  735. data/app/services/collavre/ai_client.rb +175 -136
  736. data/app/services/collavre/ai_usage_tracking.rb +93 -0
  737. data/app/services/collavre/auto_theme_generator.rb +5 -8
  738. data/app/services/collavre/blob_access.rb +166 -0
  739. data/app/services/collavre/cdn_invalidation.rb +67 -0
  740. data/app/services/collavre/claude_approval_handoff.rb +28 -0
  741. data/app/services/collavre/cli_proxy/adapter_engine.rb +27 -0
  742. data/app/services/collavre/cli_proxy/client.rb +190 -0
  743. data/app/services/collavre/cli_proxy/endpoint_policy.rb +81 -0
  744. data/app/services/collavre/cli_proxy/engine_unauthenticated_error.rb +31 -0
  745. data/app/services/collavre/cli_proxy/health_probe.rb +137 -0
  746. data/app/services/collavre/cli_proxy/identity.rb +47 -0
  747. data/app/services/collavre/cli_proxy/inline_login.rb +239 -0
  748. data/app/services/collavre/cli_proxy/inline_replay_admission.rb +57 -0
  749. data/app/services/collavre/cli_proxy/replay_claims.rb +48 -0
  750. data/app/services/collavre/cli_proxy/replay_routing.rb +43 -0
  751. data/app/services/collavre/cli_proxy/replay_workspace.rb +53 -0
  752. data/app/services/collavre/cli_proxy/run_options.rb +90 -0
  753. data/app/services/collavre/cli_proxy/safe_net_http_adapter.rb +19 -0
  754. data/app/services/collavre/command_menu_service.rb +18 -14
  755. data/app/services/collavre/comment_move_service.rb +100 -7
  756. data/app/services/collavre/comment_resend_service/cleanup.rb +20 -0
  757. data/app/services/collavre/comment_resend_service.rb +125 -0
  758. data/app/services/collavre/comment_snapshot_restore_service.rb +27 -15
  759. data/app/services/collavre/comments/action_executor.rb +45 -27
  760. data/app/services/collavre/comments/approval_gate_decision.rb +54 -0
  761. data/app/services/collavre/comments/approved_tool_invocation.rb +37 -0
  762. data/app/services/collavre/comments/async_approval_decision.rb +41 -0
  763. data/app/services/collavre/comments/command_processor.rb +14 -1
  764. data/app/services/collavre/comments/creative_quote_rejection_notice.rb +37 -0
  765. data/app/services/collavre/comments/creative_quote_review.rb +54 -0
  766. data/app/services/collavre/comments/keep_command.rb +80 -0
  767. data/app/services/collavre/comments/list_response.rb +161 -0
  768. data/app/services/collavre/comments/read_pointer_writer.rb +210 -0
  769. data/app/services/collavre/comments/read_receipt_broadcaster.rb +157 -0
  770. data/app/services/collavre/comments/read_receipt_index.rb +152 -0
  771. data/app/services/collavre/comments/topic_command.rb +68 -18
  772. data/app/services/collavre/comments/topic_mutation.rb +20 -0
  773. data/app/services/collavre/creatives/agent_turn_history.rb +15 -0
  774. data/app/services/collavre/creatives/ai_write_policy.rb +36 -0
  775. data/app/services/collavre/creatives/breadcrumb_resolver.rb +1 -1
  776. data/app/services/collavre/creatives/change_set_apply_service.rb +309 -0
  777. data/app/services/collavre/creatives/change_set_diff.rb +216 -0
  778. data/app/services/collavre/creatives/change_set_restore_service.rb +20 -0
  779. data/app/services/collavre/creatives/change_set_revert_service.rb +21 -0
  780. data/app/services/collavre/creatives/change_set_visibility.rb +108 -0
  781. data/app/services/collavre/creatives/children_index.rb +19 -8
  782. data/app/services/collavre/creatives/comment_badge_index.rb +299 -27
  783. data/app/services/collavre/creatives/create_service.rb +3 -1
  784. data/app/services/collavre/creatives/creation_revert_policy.rb +35 -0
  785. data/app/services/collavre/creatives/creative_quote_turn.rb +96 -0
  786. data/app/services/collavre/creatives/description_image_dimensions.rb +50 -0
  787. data/app/services/collavre/creatives/destroy_service.rb +18 -2
  788. data/app/services/collavre/creatives/draft_change_set_applicator.rb +121 -0
  789. data/app/services/collavre/creatives/draft_change_set_capture.rb +206 -0
  790. data/app/services/collavre/creatives/draft_change_set_reject_service.rb +45 -0
  791. data/app/services/collavre/creatives/draft_conflict_dependency_pruner.rb +110 -0
  792. data/app/services/collavre/creatives/draft_link_authorization.rb +61 -0
  793. data/app/services/collavre/creatives/draft_link_progress_reconciliation.rb +36 -0
  794. data/app/services/collavre/creatives/drop_trigger_missing_agent_notifier.rb +31 -0
  795. data/app/services/collavre/creatives/expansion_save_order.rb +70 -0
  796. data/app/services/collavre/creatives/file_drop_service.rb +49 -0
  797. data/app/services/collavre/creatives/filter_pipeline.rb +7 -4
  798. data/app/services/collavre/creatives/filter_state.rb +25 -0
  799. data/app/services/collavre/creatives/filters/base_filter.rb +3 -2
  800. data/app/services/collavre/creatives/filters/cron_filter.rb +19 -0
  801. data/app/services/collavre/creatives/filters/reaction_filter.rb +17 -0
  802. data/app/services/collavre/creatives/historical_snapshot_authorization.rb +108 -0
  803. data/app/services/collavre/creatives/history.rb +302 -0
  804. data/app/services/collavre/creatives/history_page.rb +61 -0
  805. data/app/services/collavre/creatives/importer.rb +19 -5
  806. data/app/services/collavre/creatives/index_query.rb +6 -12
  807. data/app/services/collavre/creatives/keep_agent_scope.rb +29 -0
  808. data/app/services/collavre/creatives/origin_chain_preloader.rb +23 -0
  809. data/app/services/collavre/creatives/permission_cache_builder.rb +72 -42
  810. data/app/services/collavre/creatives/permission_checker.rb +51 -4
  811. data/app/services/collavre/creatives/permission_filter.rb +14 -1
  812. data/app/services/collavre/creatives/progress_propagation_targets.rb +36 -0
  813. data/app/services/collavre/creatives/propagated_change_authorization.rb +142 -0
  814. data/app/services/collavre/creatives/reorderer.rb +140 -49
  815. data/app/services/collavre/creatives/snapshot_assignment.rb +25 -0
  816. data/app/services/collavre/creatives/snapshot_comparison.rb +14 -0
  817. data/app/services/collavre/creatives/tree_builder.rb +41 -14
  818. data/app/services/collavre/creatives/tree_formatter.rb +6 -7
  819. data/app/services/collavre/creatives/trigger_action_command.rb +222 -0
  820. data/app/services/collavre/creatives/type_transition.rb +58 -0
  821. data/app/services/collavre/creatives/workspace_expansion_candidates.rb +24 -0
  822. data/app/services/collavre/creatives/workspace_expansion_order.rb +58 -0
  823. data/app/services/collavre/creatives/workspace_expansion_presence.rb +38 -0
  824. data/app/services/collavre/creatives/workspace_path_resolver.rb +50 -0
  825. data/app/services/collavre/creatives/workspace_tree_builder.rb +100 -0
  826. data/app/services/collavre/crons/change_broadcaster.rb +35 -0
  827. data/app/services/collavre/crons/recurring_task_arguments.rb +10 -2
  828. data/app/services/collavre/crons/recurring_task_index.rb +78 -0
  829. data/app/services/collavre/crons/recurring_topic_tasks.rb +23 -0
  830. data/app/services/collavre/decision_questions.rb +125 -0
  831. data/app/services/collavre/home_page_path.rb +55 -0
  832. data/app/services/collavre/html_text.rb +64 -0
  833. data/app/services/collavre/http_client.rb +52 -7
  834. data/app/services/collavre/inbox_reply_service.rb +2 -1
  835. data/app/services/collavre/keep_loop.rb +84 -0
  836. data/app/services/collavre/kollavy/access_scope.rb +84 -0
  837. data/app/services/collavre/kollavy/identity.rb +25 -0
  838. data/app/services/collavre/kollavy.rb +153 -0
  839. data/app/services/collavre/llm_usage/attribution.rb +99 -0
  840. data/app/services/collavre/llm_usage/history_omission.rb +30 -0
  841. data/app/services/collavre/llm_usage/recorder.rb +59 -0
  842. data/app/services/collavre/llm_usage/report.rb +116 -0
  843. data/app/services/collavre/llm_usage/token_normalizer.rb +54 -0
  844. data/app/services/collavre/markdown_converter.rb +45 -29
  845. data/app/services/collavre/mcp_service.rb +61 -35
  846. data/app/services/collavre/mcp_tool_access.rb +40 -0
  847. data/app/services/collavre/mcp_tool_categories.rb +57 -0
  848. data/app/services/collavre/mcp_tool_registrar.rb +217 -0
  849. data/app/services/collavre/mcp_tool_registry.rb +38 -0
  850. data/app/services/collavre/mention_parser.rb +48 -14
  851. data/app/services/collavre/notices/feed.rb +127 -0
  852. data/app/services/collavre/notices/onboarding_replay.rb +17 -0
  853. data/app/services/collavre/notices/refresh_deadline.rb +15 -0
  854. data/app/services/collavre/notices/tracker.rb +68 -0
  855. data/app/services/collavre/openai_endpoint.rb +24 -0
  856. data/app/services/collavre/orchestration/agent_context_builder.rb +3 -1
  857. data/app/services/collavre/orchestration/agent_orchestrator.rb +488 -144
  858. data/app/services/collavre/orchestration/agent_recovery_trigger.rb +16 -0
  859. data/app/services/collavre/orchestration/arbiter.rb +57 -7
  860. data/app/services/collavre/orchestration/deferred_trigger_scope.rb +64 -0
  861. data/app/services/collavre/orchestration/delivery_record.rb +740 -0
  862. data/app/services/collavre/orchestration/dispatch_records.rb +52 -0
  863. data/app/services/collavre/orchestration/execution_fence.rb +97 -0
  864. data/app/services/collavre/orchestration/job_enqueue.rb +150 -0
  865. data/app/services/collavre/orchestration/keep_dispatch_admission.rb +76 -0
  866. data/app/services/collavre/orchestration/keep_dispatch_restoration.rb +57 -0
  867. data/app/services/collavre/orchestration/keep_loop_ownership.rb +152 -0
  868. data/app/services/collavre/orchestration/keep_queued_dispatches.rb +73 -0
  869. data/app/services/collavre/orchestration/loop_breaker.rb +20 -9
  870. data/app/services/collavre/orchestration/matcher.rb +232 -44
  871. data/app/services/collavre/orchestration/offline_task_grace.rb +50 -0
  872. data/app/services/collavre/orchestration/policy_resolver.rb +110 -11
  873. data/app/services/collavre/orchestration/resource_tracker.rb +4 -1
  874. data/app/services/collavre/orchestration/restored_dispatch_admission.rb +103 -0
  875. data/app/services/collavre/orchestration/restored_dispatch_context.rb +15 -0
  876. data/app/services/collavre/orchestration/resume_context.rb +90 -0
  877. data/app/services/collavre/orchestration/scheduler.rb +19 -6
  878. data/app/services/collavre/orchestration/scheduling_hooks.rb +10 -0
  879. data/app/services/collavre/orchestration/selection.rb +76 -0
  880. data/app/services/collavre/orchestration/solid_queue_retry_recovery.rb +54 -0
  881. data/app/services/collavre/orchestration/stuck_detector.rb +25 -112
  882. data/app/services/collavre/orchestration/task_coalescer.rb +385 -0
  883. data/app/services/collavre/orchestration/task_resumer.rb +408 -0
  884. data/app/services/collavre/orchestration/topic_slot.rb +73 -0
  885. data/app/services/collavre/orchestration/waiting_notice_manager.rb +289 -0
  886. data/app/services/collavre/orchestration/workflow_dispatch.rb +47 -0
  887. data/app/services/collavre/orchestration/workflow_routing.rb +96 -0
  888. data/app/services/collavre/ppt_archive.rb +123 -0
  889. data/app/services/collavre/ppt_backgrounds.rb +97 -0
  890. data/app/services/collavre/ppt_colors.rb +104 -0
  891. data/app/services/collavre/ppt_connectors.rb +33 -0
  892. data/app/services/collavre/ppt_diagrams.rb +50 -0
  893. data/app/services/collavre/ppt_formatting.rb +134 -0
  894. data/app/services/collavre/ppt_geometry.rb +92 -0
  895. data/app/services/collavre/ppt_importer.rb +45 -64
  896. data/app/services/collavre/ppt_inheritance.rb +122 -0
  897. data/app/services/collavre/ppt_inline_media.rb +19 -0
  898. data/app/services/collavre/ppt_media.rb +49 -0
  899. data/app/services/collavre/ppt_numbering.rb +63 -0
  900. data/app/services/collavre/ppt_paragraphs.rb +52 -0
  901. data/app/services/collavre/ppt_preview.rb +12 -0
  902. data/app/services/collavre/ppt_renderer.rb +259 -0
  903. data/app/services/collavre/ppt_shape_fills.rb +35 -0
  904. data/app/services/collavre/ppt_shape_styles.rb +81 -0
  905. data/app/services/collavre/ppt_tables.rb +35 -0
  906. data/app/services/collavre/public_asset_allowlist.rb +41 -0
  907. data/app/services/collavre/quota/agent_execution.rb +31 -0
  908. data/app/services/collavre/quota/exceeded_error.rb +48 -0
  909. data/app/services/collavre/quota/notice.rb +15 -0
  910. data/app/services/collavre/quota/pending_dispatch.rb +35 -0
  911. data/app/services/collavre/quota/probe.rb +44 -0
  912. data/app/services/collavre/quota/recovery.rb +97 -0
  913. data/app/services/collavre/quota/retry_time.rb +65 -0
  914. data/app/services/collavre/ruby_llm_interaction_logger.rb +17 -7
  915. data/app/services/collavre/screening/gate.rb +47 -0
  916. data/app/services/collavre/screening/input.rb +30 -0
  917. data/app/services/collavre/screening/scope.rb +34 -0
  918. data/app/services/collavre/screening/screener.rb +57 -0
  919. data/app/services/collavre/screening/screener_resolver.rb +23 -0
  920. data/app/services/collavre/screening/turn_guard.rb +196 -0
  921. data/app/services/collavre/screening/verdict.rb +30 -0
  922. data/app/services/collavre/screening.rb +65 -0
  923. data/app/services/collavre/source_browser.rb +186 -0
  924. data/app/services/collavre/system_events/context_builder.rb +105 -15
  925. data/app/services/collavre/system_events/dispatcher.rb +67 -12
  926. data/app/services/collavre/system_events/envelope.rb +73 -0
  927. data/app/services/collavre/system_events/vocabulary.rb +48 -0
  928. data/app/services/collavre/task_execution_time.rb +33 -0
  929. data/app/services/collavre/tool_approval_policy.rb +42 -0
  930. data/app/services/collavre/tool_description.rb +35 -0
  931. data/app/services/collavre/tool_usage/approved_invocation.rb +28 -0
  932. data/app/services/collavre/tool_usage/cli_proxy_recorder.rb +100 -0
  933. data/app/services/collavre/tool_usage/mcp_call.rb +52 -0
  934. data/app/services/collavre/tool_usage/recorder.rb +34 -0
  935. data/app/services/collavre/tool_usage/report.rb +38 -0
  936. data/app/services/collavre/tools/approval_request_service.rb +54 -0
  937. data/app/services/collavre/tools/async_approval_request.rb +58 -0
  938. data/app/services/collavre/tools/creative_attach_files_service.rb +10 -2
  939. data/app/services/collavre/tools/creative_batch_service.rb +90 -10
  940. data/app/services/collavre/tools/creative_create_service.rb +33 -4
  941. data/app/services/collavre/tools/creative_import_service.rb +26 -3
  942. data/app/services/collavre/tools/creative_link_service.rb +75 -0
  943. data/app/services/collavre/tools/creative_list_attachments_service.rb +2 -2
  944. data/app/services/collavre/tools/creative_remove_attachment_service.rb +30 -5
  945. data/app/services/collavre/tools/creative_reorder_service.rb +111 -0
  946. data/app/services/collavre/tools/creative_retrieval_service.rb +27 -21
  947. data/app/services/collavre/tools/creative_unlink_service.rb +60 -0
  948. data/app/services/collavre/tools/creative_update_service.rb +88 -81
  949. data/app/services/collavre/tools/cron_cancel_service.rb +3 -17
  950. data/app/services/collavre/tools/cron_create_service.rb +101 -32
  951. data/app/services/collavre/tools/cron_list_service.rb +2 -2
  952. data/app/services/collavre/tools/cron_update_service.rb +5 -19
  953. data/app/services/collavre/tools/id_list.rb +38 -0
  954. data/app/services/collavre/tools/keep_service.rb +42 -0
  955. data/app/services/collavre/tools/source_list_service.rb +25 -0
  956. data/app/services/collavre/tools/source_read_service.rb +26 -0
  957. data/app/services/collavre/tools/source_search_service.rb +25 -0
  958. data/app/services/collavre/tools/source_tool_support.rb +39 -0
  959. data/app/services/collavre/tools/task_descendants.rb +32 -0
  960. data/app/services/collavre/tools/task_lineage.rb +46 -0
  961. data/app/services/collavre/tools/task_status_service.rb +61 -0
  962. data/app/services/collavre/tools/task_tracking.rb +67 -0
  963. data/app/services/collavre/tools/topic_authorizer.rb +62 -8
  964. data/app/services/collavre/tools/topic_branch_service.rb +85 -0
  965. data/app/services/collavre/tools/topic_create_service.rb +117 -0
  966. data/app/services/collavre/tools/topic_list_service.rb +101 -0
  967. data/app/services/collavre/tools/topic_message_create_service.rb +210 -0
  968. data/app/services/collavre/tools/topic_messages_service.rb +346 -0
  969. data/app/services/collavre/tools/topic_move_service.rb +107 -0
  970. data/app/services/collavre/tools/topic_selection.rb +59 -0
  971. data/app/services/collavre/tools/topic_tasks_service.rb +42 -0
  972. data/app/services/collavre/tools/topic_update_service.rb +165 -0
  973. data/app/services/collavre/topic_branch_service.rb +40 -10
  974. data/app/services/collavre/topics/active_topic_snapshot.rb +30 -0
  975. data/app/services/collavre/topics/agent_resolver.rb +93 -0
  976. data/app/services/collavre/topics/char_budget.rb +70 -0
  977. data/app/services/collavre/topics/cron_badge_decorator.rb +32 -0
  978. data/app/services/collavre/topics/last_topic_preference_payload.rb +19 -0
  979. data/app/services/collavre/topics/message_page.rb +380 -0
  980. data/app/services/collavre/topics/message_page_markdown.rb +102 -0
  981. data/app/services/collavre/topics/message_scope.rb +61 -0
  982. data/app/services/collavre/topics/message_stats.rb +92 -0
  983. data/app/services/collavre/topics/move_blocker.rb +47 -0
  984. data/app/services/collavre/topics/next_name.rb +35 -0
  985. data/app/services/collavre/topics/orphaned_cron_notifier.rb +33 -14
  986. data/app/services/collavre/topics/pin_rejection.rb +39 -0
  987. data/app/services/collavre/topics/read_pointer_relocator.rb +71 -0
  988. data/app/services/collavre/topics/reserved_name.rb +24 -0
  989. data/app/services/collavre/topics/serializer.rb +99 -0
  990. data/app/services/collavre/topics/topic_destroy.rb +30 -0
  991. data/app/services/collavre/topics/topic_move.rb +99 -0
  992. data/app/services/collavre/topics/topic_move_effects.rb +35 -0
  993. data/app/services/collavre/topics/topic_mutation.rb +27 -0
  994. data/app/services/collavre/trigger_loop_config.rb +15 -0
  995. data/app/services/collavre/workflow/admission.rb +109 -0
  996. data/app/services/collavre/workflow/child_context.rb +35 -0
  997. data/app/services/collavre/workflow/conditions.rb +79 -0
  998. data/app/services/collavre/workflow/context_preloader.rb +48 -0
  999. data/app/services/collavre/workflow/continuation.rb +16 -0
  1000. data/app/services/collavre/workflow/dispatch_identity.rb +25 -0
  1001. data/app/services/collavre/workflow/dispatch_outcome.rb +9 -0
  1002. data/app/services/collavre/workflow/editor.rb +79 -0
  1003. data/app/services/collavre/workflow/envelope_validation.rb +18 -0
  1004. data/app/services/collavre/workflow/fallback_delivery.rb +59 -0
  1005. data/app/services/collavre/workflow/fixed_anchor.rb +32 -0
  1006. data/app/services/collavre/workflow/human_handoff.rb +39 -0
  1007. data/app/services/collavre/workflow/inbox_notice.rb +56 -0
  1008. data/app/services/collavre/workflow/invocation.rb +60 -0
  1009. data/app/services/collavre/workflow/materialization.rb +41 -0
  1010. data/app/services/collavre/workflow/publication.rb +55 -0
  1011. data/app/services/collavre/workflow/push_delivery.rb +97 -0
  1012. data/app/services/collavre/workflow/recovery.rb +62 -0
  1013. data/app/services/collavre/workflow/resolver.rb +83 -0
  1014. data/app/services/collavre/workflow/rule.rb +189 -0
  1015. data/app/services/collavre/workflow/safety.rb +61 -0
  1016. data/app/services/collavre/workflow/settlement.rb +60 -0
  1017. data/app/services/collavre/workflow/source_message.rb +39 -0
  1018. data/app/services/collavre/workflow/task_admission.rb +114 -0
  1019. data/app/views/collavre/admin/integrations/_setting_row.html.erb +5 -8
  1020. data/app/views/collavre/admin/settings/_system_tab.html.erb +56 -11
  1021. data/app/views/collavre/agent_connections/show.html.erb +84 -0
  1022. data/app/views/collavre/agent_gateways/_form.html.erb +85 -0
  1023. data/app/views/collavre/agent_gateways/edit.html.erb +4 -0
  1024. data/app/views/collavre/agent_gateways/index.html.erb +51 -0
  1025. data/app/views/collavre/agent_gateways/new.html.erb +4 -0
  1026. data/app/views/collavre/agent_models/_editor.html.erb +20 -0
  1027. data/app/views/collavre/comments/_activity_log_details.html.erb +10 -4
  1028. data/app/views/collavre/comments/_agent_run_options.html.erb +12 -0
  1029. data/app/views/collavre/comments/_approval_gate.html.erb +8 -0
  1030. data/app/views/collavre/comments/_comment.html.erb +45 -8
  1031. data/app/views/collavre/comments/_comments_popup.html.erb +105 -29
  1032. data/app/views/collavre/comments/_creative_quote_review.html.erb +29 -0
  1033. data/app/views/collavre/comments/_empty_state.html.erb +33 -0
  1034. data/app/views/collavre/comments/_empty_state_minimal.html.erb +7 -0
  1035. data/app/views/collavre/comments/_feature_card.html.erb +35 -0
  1036. data/app/views/collavre/comments/_list.html.erb +3 -2
  1037. data/app/views/collavre/comments/_loading.html.erb +7 -0
  1038. data/app/views/collavre/comments/_no_search_results.html.erb +3 -0
  1039. data/app/views/collavre/comments/_reaction_buttons.html.erb +7 -0
  1040. data/app/views/collavre/comments/_reaction_picker.html.erb +1 -8
  1041. data/app/views/collavre/comments/_run_options.html.erb +4 -0
  1042. data/app/views/collavre/comments/_thinking_icon.html.erb +7 -0
  1043. data/app/views/collavre/creative_change_sets/_detail.html.erb +88 -0
  1044. data/app/views/collavre/creative_change_sets/_list.html.erb +40 -0
  1045. data/app/views/collavre/creative_change_sets/_undo_toast.html.erb +22 -0
  1046. data/app/views/collavre/creatives/_add_button.html.erb +17 -19
  1047. data/app/views/collavre/creatives/_empty_state.html.erb +109 -0
  1048. data/app/views/collavre/creatives/_import_upload_zone.html.erb +1 -1
  1049. data/app/views/collavre/creatives/_inline_edit_form.html.erb +18 -6
  1050. data/app/views/collavre/creatives/_public_meta.html.erb +16 -0
  1051. data/app/views/collavre/creatives/_share_modal.html.erb +9 -0
  1052. data/app/views/collavre/creatives/_tree_loading_placeholder.html.erb +16 -0
  1053. data/app/views/collavre/creatives/_type_selector.html.erb +15 -0
  1054. data/app/views/collavre/creatives/_workflow_panel.html.erb +71 -0
  1055. data/app/views/collavre/creatives/_workspace_tree.html.erb +36 -0
  1056. data/app/views/collavre/creatives/edit.html.erb +7 -4
  1057. data/app/views/collavre/creatives/index.html.erb +149 -25
  1058. data/app/views/collavre/creatives/ppt_previews/_markdown_document.html.erb +17 -0
  1059. data/app/views/collavre/creatives/ppt_previews/show.html.erb +38 -0
  1060. data/app/views/collavre/creatives/show.html.erb +1 -1
  1061. data/app/views/collavre/desktop_setup/show.html.erb +114 -0
  1062. data/app/views/collavre/features/index.html.erb +42 -0
  1063. data/app/views/collavre/features/show.html.erb +52 -0
  1064. data/app/views/collavre/inline_agent_logins/show.html.erb +31 -0
  1065. data/app/views/collavre/invitation_mailer/invite.html.erb +1 -1
  1066. data/app/views/collavre/invitation_mailer/invite.text.erb +1 -1
  1067. data/app/views/collavre/invites/show.html.erb +14 -1
  1068. data/app/views/collavre/landing/show.html.erb +5 -1
  1069. data/app/views/collavre/llm_usages/_metrics.html.erb +13 -0
  1070. data/app/views/collavre/llm_usages/index.html.erb +88 -0
  1071. data/app/views/collavre/notices/_payload.html.erb +5 -0
  1072. data/app/views/collavre/notices/_zone.html.erb +20 -0
  1073. data/app/views/collavre/seo/sitemap.xml.builder +8 -0
  1074. data/app/views/collavre/seo/sitemap_index.xml.builder +8 -0
  1075. data/app/views/collavre/shared/_creative_move_modal.html.erb +59 -0
  1076. data/app/views/collavre/shared/_link_creative_modal.html.erb +6 -1
  1077. data/app/views/collavre/shared/navigation/_help_button.html.erb +22 -1
  1078. data/app/views/collavre/shared/navigation/_panels.html.erb +1 -8
  1079. data/app/views/collavre/shared/navigation/_search_form.html.erb +27 -3
  1080. data/app/views/collavre/user_themes/index.html.erb +1 -1
  1081. data/app/views/collavre/users/_llm_fields.html.erb +60 -0
  1082. data/app/views/collavre/users/_model_picker.html.erb +36 -0
  1083. data/app/views/collavre/users/_org_chart_node.html.erb +1 -1
  1084. data/app/views/collavre/users/_tool_details.html.erb +7 -0
  1085. data/app/views/collavre/users/_tool_selection.html.erb +51 -0
  1086. data/app/views/collavre/users/edit_ai.html.erb +60 -53
  1087. data/app/views/collavre/users/index.html.erb +96 -80
  1088. data/app/views/collavre/users/new.html.erb +4 -2
  1089. data/app/views/collavre/users/new_ai.html.erb +17 -53
  1090. data/app/views/collavre/users/show.html.erb +19 -24
  1091. data/app/views/layouts/collavre/landing.html.erb +23 -4
  1092. data/app/views/layouts/collavre/slide.html.erb +4 -1
  1093. data/config/initializers/active_storage_access.rb +34 -0
  1094. data/config/initializers/agent_health.rb +7 -0
  1095. data/config/initializers/kollavy_feature_card.rb +11 -0
  1096. data/config/initializers/mcp_discovery_access.rb +17 -0
  1097. data/config/initializers/mcp_tool_usage.rb +17 -0
  1098. data/config/initializers/meta_tool_access.rb +35 -0
  1099. data/config/initializers/rate_limits.rb +4 -0
  1100. data/config/initializers/ruby_llm_openai_cli_events.rb +43 -0
  1101. data/config/initializers/ruby_llm_openai_reasoning_content.rb +78 -0
  1102. data/config/initializers/screening_integration_settings.rb +5 -0
  1103. data/config/initializers/task_recovery.rb +30 -0
  1104. data/config/locales/admin.en.yml +21 -3
  1105. data/config/locales/admin.ko.yml +21 -3
  1106. data/config/locales/agent_gateway.en.yml +221 -0
  1107. data/config/locales/agent_gateway.ko.yml +221 -0
  1108. data/config/locales/ai_agent.en.yml +11 -1
  1109. data/config/locales/ai_agent.ko.yml +11 -1
  1110. data/config/locales/approval_gate.en.yml +11 -0
  1111. data/config/locales/approval_gate.ko.yml +11 -0
  1112. data/config/locales/claude_channel.en.yml +1 -0
  1113. data/config/locales/claude_channel.ko.yml +1 -0
  1114. data/config/locales/comments.en.yml +146 -48
  1115. data/config/locales/comments.ko.yml +146 -48
  1116. data/config/locales/contacts.en.yml +0 -5
  1117. data/config/locales/contacts.ko.yml +0 -5
  1118. data/config/locales/contexts.en.yml +6 -0
  1119. data/config/locales/contexts.ko.yml +6 -0
  1120. data/config/locales/creative_history.en.yml +62 -0
  1121. data/config/locales/creative_history.ko.yml +56 -0
  1122. data/config/locales/creative_types.en.yml +19 -0
  1123. data/config/locales/creative_types.ko.yml +19 -0
  1124. data/config/locales/creatives.en.yml +51 -52
  1125. data/config/locales/creatives.ko.yml +49 -45
  1126. data/config/locales/crons.en.yml +16 -0
  1127. data/config/locales/crons.ko.yml +16 -0
  1128. data/config/locales/dnd.en.yml +26 -0
  1129. data/config/locales/dnd.ko.yml +26 -0
  1130. data/config/locales/drop_trigger.en.yml +5 -16
  1131. data/config/locales/drop_trigger.ko.yml +5 -16
  1132. data/config/locales/features.en.yml +155 -0
  1133. data/config/locales/features.ko.yml +155 -0
  1134. data/config/locales/file_drop.en.yml +5 -0
  1135. data/config/locales/file_drop.ko.yml +5 -0
  1136. data/config/locales/inline_agent_login.en.yml +20 -0
  1137. data/config/locales/inline_agent_login.ko.yml +16 -0
  1138. data/config/locales/integrations.en.yml +0 -1
  1139. data/config/locales/integrations.ko.yml +0 -1
  1140. data/config/locales/invites.en.yml +3 -0
  1141. data/config/locales/invites.ko.yml +3 -0
  1142. data/config/locales/keep.en.yml +18 -0
  1143. data/config/locales/keep.ko.yml +18 -0
  1144. data/config/locales/kollavy.en.yml +4 -0
  1145. data/config/locales/kollavy.ko.yml +4 -0
  1146. data/config/locales/landing.en.yml +4 -7
  1147. data/config/locales/landing.ko.yml +4 -7
  1148. data/config/locales/llm_usages.en.yml +45 -0
  1149. data/config/locales/llm_usages.ko.yml +45 -0
  1150. data/config/locales/mcp_tools.en.yml +6 -0
  1151. data/config/locales/mcp_tools.ko.yml +6 -0
  1152. data/config/locales/notices.en.yml +52 -0
  1153. data/config/locales/notices.ko.yml +52 -0
  1154. data/config/locales/orchestration.en.yml +34 -2
  1155. data/config/locales/orchestration.ko.yml +34 -2
  1156. data/config/locales/ppt_viewer.en.yml +18 -0
  1157. data/config/locales/ppt_viewer.ko.yml +18 -0
  1158. data/config/locales/public_creatives.en.yml +6 -0
  1159. data/config/locales/public_creatives.ko.yml +6 -0
  1160. data/config/locales/quota.en.yml +5 -0
  1161. data/config/locales/quota.ko.yml +5 -0
  1162. data/config/locales/screening.en.yml +22 -0
  1163. data/config/locales/screening.ko.yml +22 -0
  1164. data/config/locales/search_popup.en.yml +2 -3
  1165. data/config/locales/search_popup.ko.yml +2 -3
  1166. data/config/locales/task_tracking.en.yml +13 -0
  1167. data/config/locales/task_tracking.ko.yml +13 -0
  1168. data/config/locales/tool_categories.en.yml +14 -0
  1169. data/config/locales/tool_categories.ko.yml +14 -0
  1170. data/config/locales/tool_descriptions.en.yml +163 -0
  1171. data/config/locales/tool_descriptions.ko.yml +163 -0
  1172. data/config/locales/tools.en.yml +46 -0
  1173. data/config/locales/tools.ko.yml +46 -0
  1174. data/config/locales/users.en.yml +33 -36
  1175. data/config/locales/users.ko.yml +32 -35
  1176. data/config/locales/workflow_editor.en.yml +62 -0
  1177. data/config/locales/workflow_editor.ko.yml +62 -0
  1178. data/config/locales/workflow_runtime.en.yml +19 -0
  1179. data/config/locales/workflow_runtime.ko.yml +19 -0
  1180. data/config/routes/account_settings.rb +4 -0
  1181. data/config/routes/agent_api.rb +6 -0
  1182. data/config/routes/agent_connections.rb +24 -0
  1183. data/config/routes/attachments.rb +2 -0
  1184. data/config/routes/comment_actions.rb +10 -0
  1185. data/config/routes/creative_attachments.rb +2 -0
  1186. data/config/routes/creative_contexts.rb +8 -0
  1187. data/config/routes/creative_history.rb +4 -0
  1188. data/config/routes/creative_preferences.rb +3 -0
  1189. data/config/routes/notices.rb +16 -0
  1190. data/config/routes/public_creatives.rb +12 -0
  1191. data/config/routes.rb +35 -38
  1192. data/db/migrate/20250911084338_backfill_creative_in_comment_inbox_items.rb +9 -1
  1193. data/db/migrate/20251204161133_set_default_routing_expression_for_ai_agents.rb +10 -3
  1194. data/db/migrate/20260724000000_add_creative_id_index_to_comments.rb +34 -0
  1195. data/db/migrate/20260724000001_add_creative_private_id_index_to_comments.rb +50 -0
  1196. data/db/migrate/20260724090000_add_async_comment_notification_state.rb +64 -0
  1197. data/db/migrate/20260727000000_add_waiting_notice_scope_to_comments.rb +30 -0
  1198. data/db/migrate/20260727010000_add_waiting_notice_scope_to_tasks.rb +26 -0
  1199. data/db/migrate/20260805000000_add_creative_workspace_enabled_to_users.rb +5 -0
  1200. data/db/migrate/20260807050000_add_dismissed_notices_to_users.rb +5 -0
  1201. data/db/migrate/20260809000000_change_creative_workspace_enabled_default_to_true.rb +18 -0
  1202. data/db/migrate/20260809000001_create_llm_models.rb +64 -0
  1203. data/db/migrate/20260809000002_create_agent_gateways_and_workspaces.rb +35 -0
  1204. data/db/migrate/20260809000003_hash_agent_workspace_callback_tokens.rb +44 -0
  1205. data/db/migrate/20260809000004_encrypt_agent_workspace_manifest_tokens.rb +59 -0
  1206. data/db/migrate/20260810000000_split_agent_workspace_identity_axes.rb +58 -0
  1207. data/db/migrate/20260810000001_seed_codex_custom_proxy_llm_models.rb +28 -0
  1208. data/db/migrate/20260810010000_add_last_visited_creative_to_users.rb +8 -0
  1209. data/db/migrate/20260810020000_add_last_visited_creative_at_to_users.rb +5 -0
  1210. data/db/migrate/20260810030000_add_last_visited_creative_navigation_order_to_users.rb +6 -0
  1211. data/db/migrate/20260810060000_add_last_visited_creative_issued_sequence_to_users.rb +5 -0
  1212. data/db/migrate/20260810100000_add_desktop_managed_to_agent_gateways.rb +40 -0
  1213. data/db/migrate/20260810100001_add_desktop_preset_adapter_to_users.rb +38 -0
  1214. data/db/migrate/20260811000000_make_agent_gateway_completion_key_nullable.rb +7 -0
  1215. data/db/migrate/20260812000000_remove_workflow_columns_from_tasks.rb +101 -0
  1216. data/db/migrate/20260812000001_remove_android_devices.rb +24 -0
  1217. data/db/migrate/20260812010000_drop_inbox_items.rb +22 -0
  1218. data/db/migrate/20260813000000_clear_claude_channel_presence_routing_expressions.rb +16 -0
  1219. data/db/migrate/20260813000001_add_last_topic_revision_to_user_creative_preferences.rb +5 -0
  1220. data/db/migrate/20260813000002_add_last_topic_save_order_to_user_creative_preferences.rb +6 -0
  1221. data/db/migrate/20260813000003_add_last_topic_save_sequences_to_user_creative_preferences.rb +5 -0
  1222. data/db/migrate/20260813000004_add_last_topic_save_fences_to_user_creative_preferences.rb +6 -0
  1223. data/db/migrate/20260814000000_add_topic_to_comment_read_pointers.rb +79 -0
  1224. data/db/migrate/20260814000001_add_creative_topic_private_id_index_to_comments.rb +37 -0
  1225. data/db/migrate/20260816000000_add_watermark_index_to_comment_read_pointers.rb +27 -0
  1226. data/db/migrate/20260816000001_add_private_recipient_topic_index_to_comments.rb +48 -0
  1227. data/db/migrate/20260820000000_add_topic_assigned_at_to_comments.rb +13 -0
  1228. data/db/migrate/20260903070800_add_joined_at_order_index_to_users.rb +23 -0
  1229. data/db/migrate/20260903180000_create_creative_change_history.rb +47 -0
  1230. data/db/migrate/20260903190000_add_system_kind_to_topics.rb +8 -0
  1231. data/db/migrate/20260908000000_add_last_topic_all_messages_to_user_creative_preferences.rb +5 -0
  1232. data/db/migrate/20260910000000_add_health_to_agent_gateways.rb +10 -0
  1233. data/db/migrate/20260911000000_add_endpoint_health_to_users.rb +9 -0
  1234. data/db/migrate/20260911020000_normalize_user_names_to_single_line.rb +32 -0
  1235. data/db/migrate/20260913000000_create_workflow_executions.rb +64 -0
  1236. data/db/migrate/20260913000002_add_ordinary_delivery_to_workflow_outboxes.rb +7 -0
  1237. data/db/migrate/20260913000100_add_reserved_to_workflow_executions.rb +7 -0
  1238. data/db/migrate/20260914063000_add_justify_creative_descriptions_to_users.rb +5 -0
  1239. data/db/migrate/20260922000000_create_llm_usages.rb +44 -0
  1240. data/db/migrate/20260922000001_add_expansion_save_sequences_to_users.rb +5 -0
  1241. data/db/migrate/20260922000002_deduplicate_root_creative_preferences.rb +37 -0
  1242. data/db/migrate/20260922000003_add_agent_quota_recovery.rb +7 -0
  1243. data/db/migrate/20260922000004_add_agent_quota_probe.rb +6 -0
  1244. data/db/migrate/20260922000005_add_suspension_to_tasks.rb +12 -0
  1245. data/db/migrate/20260922010000_create_retired_task_executions.rb +11 -0
  1246. data/db/migrate/20260922113000_add_emoji_comment_index_to_comment_reactions.rb +17 -0
  1247. data/db/migrate/20260922120000_create_tool_usages.rb +37 -0
  1248. data/db/migrate/20260923000000_link_tool_usages_to_agent_workspaces.rb +44 -0
  1249. data/db/migrate/20260924000000_add_agent_run_options.rb +7 -0
  1250. data/db/migrate/20260924010000_add_run_options_to_comment_versions.rb +5 -0
  1251. data/db/migrate/20260924060000_add_async_approval_recovery_to_comments.rb +24 -0
  1252. data/db/migrate/20260924070000_add_async_approval_task_to_comments.rb +22 -0
  1253. data/db/migrate/20260929000000_create_user_notices.rb +14 -0
  1254. data/db/migrate/20260929010000_add_system_agent_to_users.rb +5 -0
  1255. data/db/migrate/20260929120000_add_actor_lookup_index_to_creative_change_sets.rb +19 -0
  1256. data/db/migrate/20261008000000_add_public_id_to_creatives.rb +6 -0
  1257. data/db/migrate/20261008010000_backfill_public_creative_ids.rb +26 -0
  1258. data/db/migrate/20261012000000_create_blob_access_flags.rb +17 -0
  1259. data/db/migrate/20261012000001_publish_landing_public_assets.rb +16 -0
  1260. data/db/migrate/20261014000000_backfill_implicit_main_cron_topics.rb +38 -0
  1261. data/db/migrate/20261016000000_add_trigger_comment_id_to_tasks.rb +21 -0
  1262. data/db/migrate/20261016000001_index_task_event_causation.rb +20 -0
  1263. data/db/migrate/20261016000002_index_task_merged_instructions.rb +25 -0
  1264. data/db/migrate/20261019000000_add_quoted_creative_to_comments.rb +10 -0
  1265. data/db/migrate/20261028000000_add_deleting_at_to_comments.rb +5 -0
  1266. data/db/seeds.rb +4 -70
  1267. data/lib/collavre/bulk_job_enqueue.rb +145 -0
  1268. data/lib/collavre/engine.rb +119 -7
  1269. data/lib/collavre/feature_card_registry.rb +174 -0
  1270. data/lib/collavre/hashed_access_token_lookup.rb +22 -0
  1271. data/lib/collavre/notice_registry.rb +179 -0
  1272. data/lib/collavre/onboarding_notices.rb +107 -0
  1273. data/lib/collavre/profile_preferences.rb +17 -0
  1274. data/lib/collavre/sensitive_request_silencer.rb +23 -0
  1275. data/lib/collavre/stylesheets_helper.rb +72 -0
  1276. data/lib/collavre/user_extensions.rb +0 -2
  1277. data/lib/collavre/version.rb +1 -1
  1278. data/lib/collavre.rb +13 -0
  1279. data/lib/generators/collavre/install/install_generator.rb +50 -8
  1280. data/lib/generators/collavre/install/templates/build.cjs.tt +1 -0
  1281. data/lib/tasks/public_assets.rake +24 -0
  1282. data/skills/collavre/SKILL.md +207 -0
  1283. data/skills/collavre/package.json +8 -0
  1284. data/skills/collavre/references/tool-authoring.md +121 -0
  1285. data/skills/collavre/references/tool-reference.md +445 -0
  1286. data/skills/collavre/scripts/collavre +653 -0
  1287. data/skills/collavre/scripts/html_entities.js +24 -0
  1288. metadata +995 -36
  1289. data/app/controllers/collavre/api/v1/mobile/agent_events_controller.rb +0 -224
  1290. data/app/controllers/collavre/api/v1/mobile/base_controller.rb +0 -95
  1291. data/app/controllers/collavre/api/v1/mobile/devices_controller.rb +0 -31
  1292. data/app/controllers/collavre/api/v1/mobile/voice_commands_controller.rb +0 -25
  1293. data/app/controllers/collavre/inbox_items_controller.rb +0 -70
  1294. data/app/controllers/collavre/typo_corrections_controller.rb +0 -39
  1295. data/app/javascript/lib/__tests__/typo_correction.test.js +0 -192
  1296. data/app/javascript/lib/typo_correction.js +0 -146
  1297. data/app/javascript/modules/__tests__/typo_corrector.test.js +0 -365
  1298. data/app/javascript/modules/creative_guide.js +0 -67
  1299. data/app/javascript/modules/typo_corrector.js +0 -534
  1300. data/app/jobs/collavre/inbox_summary_job.rb +0 -24
  1301. data/app/mailers/collavre/inbox_mailer.rb +0 -19
  1302. data/app/models/collavre/inbox_item.rb +0 -89
  1303. data/app/services/collavre/comments/concerns/workflow_support.rb +0 -115
  1304. data/app/services/collavre/comments/work_command.rb +0 -161
  1305. data/app/services/collavre/comments/workflow_executor.rb +0 -276
  1306. data/app/services/collavre/gemini_parent_recommender.rb +0 -77
  1307. data/app/services/collavre/mobile/event_summarizer.rb +0 -40
  1308. data/app/services/collavre/typo_corrector.rb +0 -188
  1309. data/app/views/collavre/inbox_items/_item.html.erb +0 -18
  1310. data/app/views/collavre/inbox_items/_items.html.erb +0 -3
  1311. data/app/views/collavre/inbox_items/_list.html.erb +0 -7
  1312. data/app/views/collavre/inbox_items/index.html.erb +0 -1
  1313. data/app/views/collavre/inbox_mailer/daily_summary.html.erb +0 -11
  1314. data/app/views/collavre/inbox_mailer/daily_summary.text.erb +0 -8
  1315. data/app/views/collavre/users/typo_correction.html.erb +0 -50
  1316. data/config/locales/mobile.en.yml +0 -16
  1317. data/config/locales/mobile.ko.yml +0 -16
  1318. data/config/locales/notifications.en.yml +0 -8
  1319. data/config/locales/notifications.ko.yml +0 -8
  1320. /data/app/javascript/{utils/drag_bundle_image.js → lib/dnd/bundle_image.js} +0 -0
@@ -0,0 +1,740 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Collavre
4
+ module Orchestration
5
+ # What a turn actually handed its agent, written down at the moment it was
6
+ # handed over.
7
+ #
8
+ # A burst of external events (a PR comment sync, a webhook batch) creates
9
+ # several comments in one topic within milliseconds.
10
+ # Orchestration::TaskCoalescer folds the *un-started* tasks of that burst
11
+ # together, but it cannot help the comment that arrives while a turn is
12
+ # already running: that dispatch parks a waiter, and the agent speaks a
13
+ # second time when the waiter is promoted.
14
+ #
15
+ # For a non-session agent the second turn is usually redundant.
16
+ # AiAgent::MessageBuilder#append_chat_history reads the topic's comments at
17
+ # *execution* time, so a comment committed before the payload was assembled
18
+ # is already inside the running turn's context — the agent has read it.
19
+ #
20
+ # For a session-backed agent it is not redundant at all:
21
+ # AiAgent::SessionContextResolver#incremental_payload keeps only the
22
+ # :trigger message, so the running turn never carried that text. Dropping
23
+ # the dispatch there would be message loss rather than de-duplication, and
24
+ # coalescing (merge into "merged_comment_ids") stays the right answer.
25
+ #
26
+ # So "the agent is busy" is the wrong predicate and "the agent has already
27
+ # been given that comment" is the right one — and the only place that can
28
+ # answer it is the resolved payload itself. This module records the answer
29
+ # off that payload and is the single door every reader goes through.
30
+ module DeliveryRecord
31
+ extend RestoredDispatchContext
32
+ extend RestoredDispatchAdmission
33
+ extend KeepDispatchRestoration
34
+ # Comment ids a turn delivered as chat history although it was not created
35
+ # for them. Sits beside TaskCoalescer::PAYLOAD_KEY and
36
+ # TaskCoalescer::ACQUIRED_ANCHOR_KEY, which record the other two ways a
37
+ # turn ends up carrying a comment it was not dispatched for.
38
+ KEY = "history_delivered_comment_ids"
39
+
40
+ # Comment ids whose dispatch this turn actually refused.
41
+ #
42
+ # A strict subset of KEY, and the distinction matters because the two are
43
+ # read for opposite purposes. KEY answers "has the agent been given this
44
+ # comment?", and chat history is the whole topic — Matcher's exclusive
45
+ # routings do not filter it, so every agent in a topic reads every public
46
+ # comment in it, including ones addressed to somebody else. That is the
47
+ # right predicate for the drop, which is only ever asked downstream of
48
+ # Matcher about a dispatch that already exists.
49
+ #
50
+ # It is the wrong predicate for the restore, whose subject is dispatches
51
+ # and which creates one per id it decides on. Reading KEY there turns a
52
+ # comment routed past this agent into a turn for it, past Matcher
53
+ # entirely; and it reads a dispatch still sitting in the job queue — an
54
+ # :immediate decision's Task row is created only when its job runs — as a
55
+ # discarded one, putting a second turn on a comment that already has one.
56
+ # Neither is visible in the absence of a Task row, so the drop is written
57
+ # down instead of reconstructed.
58
+ DROPPED_KEY = "dropped_dispatch_comment_ids"
59
+
60
+ # This turn asked the provider for an answer and the request never got
61
+ # there.
62
+ #
63
+ # Every other reader here decides off the turn's *status*, which works
64
+ # while the endings line up with what was delivered. This one does not:
65
+ # AiClient#chat catches the provider error, streams "⚠️ AI Error" into
66
+ # the reply and returns nil, and AiAgentJob marks an ordinary task `done`
67
+ # on that — the single status every reader counts as delivery. So a turn
68
+ # can end in the delivered ending having handed over nothing at all, and
69
+ # the comments it discarded were read by nobody.
70
+ #
71
+ # Recorded rather than inferred, because nothing else in the row says it:
72
+ # `done` with an error in the reply text looks exactly like `done`. The
73
+ # only place that knows is the service holding the client that failed.
74
+ HANDOFF_FAILED_KEY = "handoff_failed"
75
+
76
+ # This turn did hand its payload to the provider — the same question,
77
+ # asked positively, for the endings that never reach the line above.
78
+ #
79
+ # HANDOFF_FAILED_KEY is written after #chat returns, so a turn that leaves
80
+ # by exception never records anything: a user pressing Stop mid-answer
81
+ # ends `cancelled`, which every reader counts as undelivered outright,
82
+ # although the provider has the payload by then and AgentLifecycleManager
83
+ # keeps the partial reply. The comments that turn swallowed were read
84
+ # along with it, so restoring them starts fresh work the moment the user
85
+ # asked for none, and answers them twice.
86
+ #
87
+ # Positive rather than a second failure flag, because the absence of the
88
+ # record has to keep meaning "restore": most cancellations and every hard
89
+ # crash write nothing at all, and losing a comment costs more than a
90
+ # redundant turn. Only a turn that got far enough to say so is exempt.
91
+ HANDED_OFF_KEY = "handed_off"
92
+
93
+ # The comment ids an attempt that handed off actually carried.
94
+ #
95
+ # HANDED_OFF_KEY answers for the *turn*, and that is enough while a turn
96
+ # is one attempt. It is not: a tool approval pauses the turn and
97
+ # Comments::ActionExecutor resumes the same Task row, so AiAgentService
98
+ # runs over it again with a fresh client. The first pass reached its tools,
99
+ # which means the provider had that payload; if the resumed request then
100
+ # never lands, the row carries both records, Task#ended_undelivered? reads
101
+ # the failure first, and the restore puts back every dispatch ever dropped
102
+ # against the turn — including the ones the first pass handed over.
103
+ #
104
+ # So the exemption is recorded per comment, which is the subject the
105
+ # restore actually has. KEY at the moment of the handoff is exactly what
106
+ # went over: record! writes it before the request, and a drop taken later
107
+ # against an id already in KEY was in that payload too. Accumulates across
108
+ # attempts for the same reason KEY does — what one pass delivered it still
109
+ # delivered — and a comment only the failed pass read is not in it, so it
110
+ # is still owed back.
111
+ HANDED_OFF_IDS_KEY = "handed_off_comment_ids"
112
+
113
+ # Comments this turn's restore has already put back on their way.
114
+ #
115
+ # AiAgentJob creates the Task row when it *runs*, so between a restored
116
+ # enqueue and its execution the comment has no row and reads as orphaned —
117
+ # a worker outage or a backlog longer than the sweep interval therefore
118
+ # had every pass enqueue another copy, and the copies that lose the
119
+ # admission race become independent waiters that answer the comment again.
120
+ # Eventual Task creation cannot be the idempotency marker for work that
121
+ # has not started.
122
+ #
123
+ # Written before the enqueue and given back if the enqueue raises, so the
124
+ # claim never outlives the dispatch it stands for: a comment claimed by an
125
+ # enqueue that never happened is the previous finding pointing the other
126
+ # way.
127
+ RESTORED_KEY = "restored_dispatch_comment_ids"
128
+
129
+ # A refused current keep dispatch is released rather than retried by the sweep.
130
+ RESTORE_REJECTED_KEY = "rejected_restore_comment_ids"
131
+
132
+ # StuckDetector failed this `running` row from outside its worker while
133
+ # the worker may still be inside the provider call.
134
+ #
135
+ # The failed status alone cannot say whether the payload reached the
136
+ # provider: AiAgentService writes that evidence only when the call returns
137
+ # or raises. The status callback therefore defers while this marker is
138
+ # present. The live worker clears it in AiAgentJob's ensure; if the worker
139
+ # died outright, restore_missed! waits out the provider timeout and asks
140
+ # again.
141
+ WORKER_SETTLING_KEY = "delivery_worker_settling"
142
+
143
+ # Everything a turn writes onto its own payload while it runs, as opposed
144
+ # to what it was dispatched with.
145
+ #
146
+ # One list rather than a strip enumerated at the point of use: a restored
147
+ # dispatch is the one that was discarded, so it carries what a dispatch
148
+ # carries and nothing the turn wrote onto itself afterwards. Enumerating
149
+ # that at the call site is how HANDOFF_FAILED_KEY came to be left in — it
150
+ # was added here long after restored_context had named the other four —
151
+ # and the drift test on this constant is what will catch the sixth.
152
+ DISPATCH_SCOPED_KEYS = %w[workflow_execution_id].freeze
153
+
154
+ TURN_SCOPED_KEYS = [
155
+ KEY, DROPPED_KEY, HANDOFF_FAILED_KEY, HANDED_OFF_KEY, HANDED_OFF_IDS_KEY,
156
+ RESTORED_KEY, RESTORE_REJECTED_KEY, KeepDispatchRestoration::KEY, WORKER_SETTLING_KEY, TaskCoalescer::PAYLOAD_KEY,
157
+ TaskCoalescer::ACQUIRED_ANCHOR_KEY, ResumeContext::KEY, *ExecutionFence::KEYS
158
+ ].freeze
159
+
160
+ # Statuses in which a turn is still the thing that will answer.
161
+ #
162
+ # Narrower than AgentOrchestrator::DELIVERED_STATUSES, which also counts
163
+ # `done`, and deliberately so: the two readers ask different questions.
164
+ # The promotion door asks "may this *parked* waiter still speak?", and a
165
+ # waiter that may not simply loses its turn — it had no independent claim
166
+ # on the slot. This module's other reader is the dispatch door, where the
167
+ # alternative to running is discarding the dispatch outright. A turn that
168
+ # has already finished is no longer answering anything, so a comment
169
+ # arriving after it gets its own turn rather than being silently dropped.
170
+ #
171
+ # `queued`/`pending` are excluded at both: nothing has been delivered yet.
172
+ IN_FLIGHT_STATUSES = %w[running delegated pending_approval].freeze
173
+
174
+ # Terminal statuses in which the turn delivered nothing after all.
175
+ #
176
+ # The complement of AgentOrchestrator::DELIVERED_STATUSES over the
177
+ # terminal set, and that is the whole point: promotion says a dead turn
178
+ # silences nothing by leaving these statuses out of its list, and a parked
179
+ # waiter survives on that — it is still a row, and the refresh re-reads
180
+ # the covering turn's status before deciding anything.
181
+ #
182
+ # A *dropped* dispatch has no row to re-read anything. So the drop owes
183
+ # the same answer through the only means it has left: putting the
184
+ # discarded dispatch back. Without it the two doors disagree about what a
185
+ # failure means, and the disagreement costs a comment nobody answers.
186
+ #
187
+ # DeliveryRecordTest asserts this is exactly the complement; a status
188
+ # added to one list and not the other is how they drift apart.
189
+ UNDELIVERED_TERMINAL_STATUSES = %w[failed cancelled escalated].freeze
190
+
191
+ # Statuses a row can be in and still owe a restore — the undelivered
192
+ # endings, plus the one that reads as delivered until the payload is
193
+ # consulted. Derived rather than listed so the sweep cannot come to scan
194
+ # for less than Task#ended_undelivered? decides on.
195
+ RESTORABLE_STATUSES = (UNDELIVERED_TERMINAL_STATUSES + %w[done]).freeze
196
+
197
+ # How far back restore_missed! looks.
198
+ #
199
+ # Comfortably more than its schedule, so a restore the callback lost gets
200
+ # several attempts, and finite because it is a backstop: a comment whose
201
+ # turn ended an hour ago has been overtaken by whatever the topic did
202
+ # since, and re-dispatching it then would be the more surprising answer.
203
+ RESTORE_SWEEP_WINDOW = 1.hour
204
+
205
+ # Slack added to the provider timeout when waiting for a worker that
206
+ # another process ended to settle — see .worker_settle_grace.
207
+ WORKER_SETTLE_SLACK = 5.minutes
208
+
209
+ # Record what `resolved` carries as chat history.
210
+ #
211
+ # Measured off the *resolved* payload — the messages the adapter is handed
212
+ # — rather than off MessageBuilder's output, because the session filter
213
+ # sits between the two and is exactly the distinction that decides whether
214
+ # a comment was delivered at all. A future filter added there is then
215
+ # accounted for without a second place having to learn about it.
216
+ #
217
+ # Restricted to ids above the turn's anchor. Chat history normally holds
218
+ # the topic's backlog, which this turn did not swallow and must not
219
+ # silence; a history comment *newer* than the anchor can only have landed
220
+ # after this turn was dispatched, which is the burst case and the only one
221
+ # this record is for. Ids rather than created_at: a burst is written by
222
+ # several processes and the clock is whichever one wrote the row (the same
223
+ # reason AiAgent::MergedTriggerComments orders by id).
224
+ #
225
+ # Accumulates. A resumed turn (pending_approval, or a retry) assembles a
226
+ # second time, and what the first pass delivered it still delivered.
227
+ #
228
+ # Written onto the row's payload under the same lock as claim_drop!, not
229
+ # onto the caller's. A resumed turn's task object is loaded before it
230
+ # assembles, and a dispatch refused in the meantime writes DROPPED_KEY
231
+ # into the same JSON column from another process; a merge onto the stale
232
+ # copy would erase the drop, and a drop no restore can find is the
233
+ # message lost. Both writers of this column now read it under the lock.
234
+ def self.record!(task, resolved)
235
+ return if task.nil?
236
+
237
+ Task.transaction do
238
+ row = Task.lock.find_by(id: task.id)
239
+ payload = row&.trigger_event_payload
240
+ next unless payload.is_a?(Hash)
241
+
242
+ anchor_id = payload.dig("comment", "id").to_i
243
+ next if anchor_id.zero?
244
+
245
+ swallowed = history_comment_ids(resolved).select { |id| id > anchor_id }
246
+ merged = (ids_in(payload) + swallowed).uniq.sort
247
+ next if merged == ids_in(payload)
248
+
249
+ row.update!(trigger_event_payload: payload.merge(KEY => merged))
250
+ task.reload unless task.equal?(row)
251
+ end
252
+ end
253
+
254
+ def self.ids_in(payload)
255
+ return [] unless payload.is_a?(Hash)
256
+
257
+ Array(payload[KEY]).compact.map(&:to_i)
258
+ end
259
+
260
+ # Write down that this turn's request never reached the provider.
261
+ #
262
+ # Under the same row lock as record! and claim_drop!, for the same
263
+ # reason: a dispatch refused in another process writes DROPPED_KEY into
264
+ # this JSON column while the turn runs, and a merge onto the caller's
265
+ # copy would erase it.
266
+ def self.mark_handoff_failed!(task)
267
+ return if task.nil?
268
+
269
+ Task.transaction do
270
+ row = Task.lock.find_by(id: task.id)
271
+ payload = row&.trigger_event_payload
272
+ next unless payload.is_a?(Hash)
273
+ next if handoff_failed?(payload)
274
+
275
+ row.update!(trigger_event_payload: payload.merge(HANDOFF_FAILED_KEY => true))
276
+ task.reload unless task.equal?(row)
277
+ end
278
+ end
279
+
280
+ def self.handoff_failed?(payload)
281
+ return false unless payload.is_a?(Hash)
282
+
283
+ payload[HANDOFF_FAILED_KEY] == true
284
+ end
285
+
286
+ # Write down that this turn's payload did reach the provider.
287
+ #
288
+ # Under the same row lock and for the same reason as its two siblings: a
289
+ # dispatch refused in another process writes DROPPED_KEY into this column
290
+ # while the turn runs, and this is written as the turn is being torn down
291
+ # by the very cancellation that dispatch is racing.
292
+ def self.mark_handed_off!(task)
293
+ return if task.nil?
294
+
295
+ Task.transaction do
296
+ row = Task.lock.find_by(id: task.id)
297
+ payload = row&.trigger_event_payload
298
+ next unless payload.is_a?(Hash)
299
+
300
+ # Not `next if handed_off?`: a resumed attempt records the handoff a
301
+ # second time, and what it carried this time is the point of asking.
302
+ recorded = handed_off_ids_in(payload)
303
+ covered = (recorded + ids_in(payload)).uniq.sort
304
+ next if handed_off?(payload) && covered == recorded
305
+
306
+ row.update!(trigger_event_payload: payload.merge(
307
+ HANDED_OFF_KEY => true, HANDED_OFF_IDS_KEY => covered
308
+ ))
309
+ task.reload unless task.equal?(row)
310
+ end
311
+ end
312
+
313
+ def self.handed_off?(payload)
314
+ return false unless payload.is_a?(Hash)
315
+
316
+ payload[HANDED_OFF_KEY] == true
317
+ end
318
+
319
+ def self.handed_off_ids_in(payload)
320
+ return [] unless payload.is_a?(Hash)
321
+
322
+ Array(payload[HANDED_OFF_IDS_KEY]).compact.map(&:to_i)
323
+ end
324
+
325
+ # Fail a running row from outside its worker without letting the status
326
+ # callback decide from evidence the worker has not written yet.
327
+ #
328
+ # Status and marker are one locked update. A marker written before a
329
+ # separate status update could be observed by the sweep while the row was
330
+ # still running; one written afterwards is too late for the callback.
331
+ def self.fail_while_worker_settles!(task)
332
+ return false if task.nil?
333
+
334
+ failed = false
335
+ Task.transaction do
336
+ row = Task.lock.find_by(id: task.id)
337
+ next unless row&.status == "running"
338
+
339
+ payload = row.trigger_event_payload
340
+ payload = {} unless payload.is_a?(Hash)
341
+ row.update!(
342
+ status: "failed",
343
+ trigger_event_payload: payload.merge(WORKER_SETTLING_KEY => true)
344
+ )
345
+ failed = true
346
+ end
347
+ task.reload if failed
348
+ failed
349
+ end
350
+
351
+ def self.worker_settling?(payload) = payload.is_a?(Hash) && payload[WORKER_SETTLING_KEY] == true
352
+
353
+ # The original worker has left the provider call, so the handoff records
354
+ # now carry the best answer this attempt can give. Clearing under the row
355
+ # lock preserves any drop written by another process in the meantime.
356
+ def self.settle_worker!(task)
357
+ return if task.nil?
358
+
359
+ Task.transaction do
360
+ row = Task.lock.find_by(id: task.id)
361
+ payload = row&.trigger_event_payload
362
+ next unless worker_settling?(payload)
363
+
364
+ row.update!(trigger_event_payload: payload.except(WORKER_SETTLING_KEY))
365
+ end
366
+ task.reload
367
+ end
368
+
369
+ def self.restored_ids_in(payload) = payload.is_a?(Hash) ? Array(payload[RESTORED_KEY]).compact.map(&:to_i) : []
370
+
371
+ # Take responsibility for putting one dispatch back, or refuse it because
372
+ # somebody already has.
373
+ #
374
+ # The same shape as claim_drop!, and for the same reason: the callback and
375
+ # the sweep can be looking at this row at once, and the thing they would
376
+ # both act on leaves no trace until a worker picks it up.
377
+ #
378
+ # @return [Boolean] whether the caller may enqueue the dispatch
379
+ def self.claim_restore!(task, comment_id)
380
+ id = comment_id.to_i
381
+ return false if task.nil? || id.zero?
382
+
383
+ claimed = false
384
+ Task.transaction do
385
+ row = Task.lock.find_by(id: task.id)
386
+ payload = row&.trigger_event_payload
387
+ next unless payload.is_a?(Hash)
388
+ next if restored_ids_in(payload).include?(id) || rejected_restore_ids_in(payload).include?(id)
389
+
390
+ restored = (restored_ids_in(payload) + [ id ]).uniq.sort
391
+ row.update!(trigger_event_payload: payload.merge(RESTORED_KEY => restored))
392
+ claimed = true
393
+ end
394
+ claimed
395
+ end
396
+
397
+ # Give the claim back for a dispatch that was never enqueued after all.
398
+ def self.release_restore_claim!(task, comment_id)
399
+ id = comment_id.to_i
400
+ return if task.nil? || id.zero?
401
+
402
+ Task.transaction do
403
+ row = Task.lock.find_by(id: task.id)
404
+ payload = row&.trigger_event_payload
405
+ next unless payload.is_a?(Hash)
406
+
407
+ restored = restored_ids_in(payload) - [ id ]
408
+ row.update!(trigger_event_payload: payload.merge(RESTORED_KEY => restored))
409
+ end
410
+ end
411
+
412
+ def self.dropped_ids_in(payload) = payload.is_a?(Hash) ? Array(payload[DROPPED_KEY]).compact.map(&:to_i) : []
413
+
414
+ def self.rejected_restore_ids_in(payload) = payload.is_a?(Hash) ? Array(payload[RESTORE_REJECTED_KEY]).compact.map(&:to_i) : []
415
+
416
+ def self.pending_restoration?(task, comment_id: nil)
417
+ payload = task&.trigger_event_payload
418
+ return false unless task&.ended_undelivered? && task.agent
419
+
420
+ pending = dropped_ids_in(payload) - handed_off_ids_in(payload) - claimed_comment_ids(task) - rejected_restore_ids_in(payload)
421
+ restorable_comments(task, comment_id ? pending & [ comment_id.to_i ] : pending).exists?
422
+ end
423
+
424
+ # Take responsibility for a dispatch about to be discarded, or refuse it.
425
+ #
426
+ # The drop and its record are one act, and this is where they are made
427
+ # one. `covering_task` answers off the caller's snapshot; between that
428
+ # answer and this write the turn can end, run its restore, and leave —
429
+ # and a record written after that restore is a comment nobody ever comes
430
+ # back for. So the status is re-read from the locked row, and a caller
431
+ # told `false` dispatches normally instead of dropping. Refusing a sound
432
+ # drop costs a redundant turn; taking an unsound one costs the message.
433
+ #
434
+ # @return [Boolean] whether the caller may discard the dispatch
435
+ def self.claim_drop!(covering, comment_id, context: nil)
436
+ id = comment_id.to_i
437
+ return false if covering.nil? || id.zero?
438
+
439
+ claimed = false
440
+ Task.transaction do
441
+ task = Task.lock.find_by(id: covering.id)
442
+ payload = task&.trigger_event_payload
443
+ next unless task&.status.to_s.in?(IN_FLIGHT_STATUSES) && payload.is_a?(Hash)
444
+
445
+ dropped = (dropped_ids_in(payload) + [ id ]).uniq.sort
446
+ task.update!(trigger_event_payload: record_keep_drop(payload, task, id, context).merge(DROPPED_KEY => dropped))
447
+ claimed = true
448
+ end
449
+ claimed
450
+ end
451
+
452
+ # The in-flight turn that has already given `comment_id` to `agent`, or nil.
453
+ #
454
+ # Every gate that drops a dispatch goes through this one method: the
455
+ # orchestrator's enqueue door, AiAgentJob's late-admission door, and any
456
+ # later one. Two doors that ask this question separately are how they come
457
+ # to disagree about it.
458
+ #
459
+ # Scoped exactly like AgentOrchestrator.delivered_comment_ids — same agent,
460
+ # topic, creative and trigger event — so a dispatch in another
461
+ # creative, or a different event over the same comment, is a different
462
+ # question and not an answer to this one.
463
+ #
464
+ # `policy_resolver` is the dispatch's own, when the caller has one for
465
+ # this same context, so the policies are not read a second time.
466
+ def self.covering_task(agent, comment_id, context, trigger_event_name, policy_resolver: nil)
467
+ return if Workflow::DispatchIdentity.valid?(context, agent.id)
468
+ return nil if agent.nil? || comment_id.blank?
469
+ return nil unless context.is_a?(Hash) && context.key?("topic")
470
+ return nil unless (policy_resolver || PolicyResolver.new(context)).drop_delivered_dispatches_for?(agent)
471
+ # A review request is bound to the comment it quotes and can only run as
472
+ # its own turn: AiAgentService and ReviewHandler read review behaviour
473
+ # off the anchor, never off the history window. Having been read as
474
+ # context is not having been answered, so a review is never dropped —
475
+ # the same boundary TaskCoalescer and refresh_deferred_context! keep.
476
+ return nil if Comment.own_turn_message_ids([ comment_id ]).any?
477
+
478
+ Task.where(
479
+ agent_id: agent.id,
480
+ topic_id: context.dig("topic", "id"),
481
+ creative_id: context.dig("creative", "id"),
482
+ trigger_event_name: trigger_event_name,
483
+ status: IN_FLIGHT_STATUSES
484
+ ).select(:id, :trigger_event_payload).find do |task|
485
+ payload = task.trigger_event_payload
486
+ # A turn still `running` after its request failed is not going to
487
+ # answer anything, and dropping against it would record a drop after
488
+ # the restore that ends it has already looked.
489
+ next false if handoff_failed?(payload)
490
+
491
+ ids_in(payload).include?(comment_id.to_i)
492
+ end
493
+ end
494
+
495
+ # Put back the dispatches this turn's record caused to be discarded, now
496
+ # that the turn has ended without delivering them.
497
+ #
498
+ # Driven off Task's status callback rather than from AiAgentJob's rescue:
499
+ # the job is not the only door onto a failed turn — StuckDetector fails a
500
+ # task that never reached a rescue at all — and a restore wired to one of
501
+ # them is a restore that does not happen on the other.
502
+ #
503
+ # Its subject is DROPPED_KEY — the dispatches this turn refused — and not
504
+ # KEY, which is merely everything it read. Only a refused dispatch is a
505
+ # thing this turn owes back; see DROPPED_KEY for what taking the wider
506
+ # set costs.
507
+ #
508
+ # Narrowed once more by what nothing else is on the hook for. A comment
509
+ # swept into this turn's history may still have a waiter of its own
510
+ # (parked before the turn assembled, so never droppable), and promotion
511
+ # decides that waiter's fate by re-reading this turn's status. Restoring
512
+ # it as well would put two turns on one comment. Belt to DROPPED_KEY's
513
+ # braces: the record already excludes what was never dropped, and this
514
+ # excludes what has since acquired a row of its own.
515
+ #
516
+ # And once more by HANDED_OFF_IDS_KEY, for the turn that ran more than
517
+ # once: a comment an earlier attempt handed to the provider is not owed
518
+ # back by a later attempt's failure.
519
+ #
520
+ # Each orphan is re-dispatched on its own, exactly as it would have been
521
+ # had it never been dropped, and the ordinary admission path folds them
522
+ # back together — rather than this method inventing a second merge rule
523
+ # beside TaskCoalescer's.
524
+ def self.restore!(task)
525
+ # Re-read the row rather than trust the caller's copy. The drop record
526
+ # is written by whichever dispatch was refused, in another process
527
+ # entirely, onto a row this turn's own object was loaded before — so
528
+ # the in-memory payload that reaches this callback is exactly the one
529
+ # that predates every drop it is being asked about.
530
+ payload = Task.find_by(id: task.id)&.trigger_event_payload
531
+ return unless payload.is_a?(Hash) && payload.key?("topic")
532
+
533
+ orphaned = dropped_ids_in(payload) - handed_off_ids_in(payload) -
534
+ claimed_comment_ids(task) - restored_ids_in(payload) - rejected_restore_ids_in(payload)
535
+ return if orphaned.empty?
536
+
537
+ agent = task.agent
538
+ return if agent.nil?
539
+
540
+ # Same eligibility the refresh applies when it moves an anchor: a
541
+ # comment that was deleted, made private, or turned into an approval
542
+ # action while the turn ran has nothing left to answer.
543
+ restorable_comments(task, orphaned).order(:id).each do |comment|
544
+ begin
545
+ enqueue_restored(task, agent, task.trigger_event_name, restored_context(payload, comment))
546
+ rescue StandardError => e
547
+ # Each orphan is a dispatch of its own; one that cannot be enqueued
548
+ # says nothing about the next. What it leaves behind is the absence
549
+ # of a row, which is exactly what restore_missed! reads.
550
+ Rails.logger.error(
551
+ "[DeliveryRecord] Failed to restore dispatch for comment #{comment.id} " \
552
+ "of task #{task.id}: #{e.class}: #{e.message}"
553
+ )
554
+ end
555
+ end
556
+ end
557
+
558
+ # Ask again for every turn whose restore may not have been made.
559
+ #
560
+ # restore! runs from an after_update_commit callback, which is to say
561
+ # after the covering turn's status is already committed: a transient
562
+ # failure there has nothing left to roll back and no later transition that
563
+ # would retry it, and the dropped dispatch is the one thing in this
564
+ # feature with no row of its own to fall back on.
565
+ #
566
+ # Nothing extra is persisted for it, because the orphan set already is:
567
+ # DROPPED_KEY is on the row and claimed_comment_ids subtracts whatever has
568
+ # since acquired a Task, so a comment stays orphaned exactly until its
569
+ # dispatch exists again and restore! is idempotent by construction. What
570
+ # was lost is only the asking.
571
+ #
572
+ # A dispatch the Scheduler refuses leaves no row either, so it is asked
573
+ # again on each pass through the window — which is the right answer for a
574
+ # quota that has since reset, and a bounded number of cheap refusals if it
575
+ # has not.
576
+ # How long after another process ends a worker this sweep waits before
577
+ # deciding it.
578
+ #
579
+ # Task#ended_while_worker_settles? explains why the status callback
580
+ # declines: Stop and StuckDetector both commit from another process while
581
+ # the worker can be inside AiClient#chat, and the worker's account of the
582
+ # handoff is written only on the way out. This door cannot tell "still
583
+ # settling" from "nobody is coming" either. Without a wait it answers in
584
+ # the callback's place and re-dispatches everything the turn swallowed.
585
+ #
586
+ # Derived from the configured provider timeout because that is what bounds
587
+ # the wait in the shape this is for: Stop pressed while the turn is still
588
+ # waiting for its first token, where AgentLifecycleManager#check_cancelled!
589
+ # cannot fire at all because it polls from inside the streaming block.
590
+ #
591
+ # It is not a bound on a conversation that keeps making requests without
592
+ # yielding content — a tool loop can outlast any grace, and for that one
593
+ # this sweep can still be early. What that costs is a duplicate reply;
594
+ # declining these endings outright would cost the comment when the worker
595
+ # died before it could clear the marker.
596
+ def self.worker_settle_grace
597
+ SystemSetting.llm_request_timeout_seconds.seconds + WORKER_SETTLE_SLACK
598
+ end
599
+
600
+ def self.restore_missed!(window: RESTORE_SWEEP_WINDOW)
601
+ grace = worker_settle_grace
602
+
603
+ [
604
+ # The endings whose status is written after the worker is out of the
605
+ # provider call, so the row is as settled as it will ever be.
606
+ Task.where(status: RESTORABLE_STATUSES - %w[cancelled failed])
607
+ .where(updated_at: window.ago..),
608
+ # And the one that is not. The window moves with the wait rather than
609
+ # being spent by it: subtracting the grace from a fixed horizon would
610
+ # leave a cancellation less backstop than every other ending gets, and
611
+ # at the default timeout almost none.
612
+ Task.where(status: "cancelled")
613
+ .where(updated_at: (window + grace).ago..grace.ago)
614
+ ].each do |scope|
615
+ scope.find_each { |task| restore_if_undelivered!(task) }
616
+ end
617
+
618
+ # A normal failed ending is settled and keeps the ordinary window. A
619
+ # StuckDetector failure carries WORKER_SETTLING_KEY and gets the same
620
+ # grace as cancellation. Read the marker in Ruby so this stays portable
621
+ # across the JSON implementations used by SQLite tests and PostgreSQL.
622
+ Task.where(status: "failed")
623
+ .where(updated_at: (window + grace).ago..)
624
+ .find_each do |task|
625
+ settling = worker_settling?(task.trigger_event_payload)
626
+ next if settling && task.updated_at > grace.ago
627
+ next if !settling && task.updated_at < window.ago
628
+
629
+ restore_if_undelivered!(task)
630
+ end
631
+ end
632
+
633
+ # Ask the restore question of a turn that has settled.
634
+ #
635
+ # The one door for every caller that is not Task's status callback: this
636
+ # sweep, and AiAgentJob's cancellation teardown, which is where a turn
637
+ # stopped mid-answer settles — see Task#ended_while_worker_settles? for why
638
+ # the cancel itself cannot decide. Gated on the same predicate the callback
639
+ # is, because a second door deciding "terminal" where the callback decides
640
+ # Task#ended_undelivered? is how one of them comes to re-answer comments the
641
+ # turn had already answered.
642
+ #
643
+ # Best effort, like the callback's: the teardown runs inside the job's last
644
+ # rescue, where an escaping exception would turn a cancelled turn into a
645
+ # failed job, and the sweep must not lose the rest of its pass to one row.
646
+ # Nothing is lost by swallowing — the orphan set is on the row, so the next
647
+ # pass asks again.
648
+ def self.restore_if_undelivered!(task)
649
+ return if task.nil? || !task.ended_undelivered?
650
+
651
+ restore!(task)
652
+ rescue StandardError => e
653
+ Rails.logger.error(
654
+ "[DeliveryRecord] Restore failed for task #{task.id}: #{e.class}: #{e.message} — " \
655
+ "leaving it to the restore sweep"
656
+ )
657
+ end
658
+
659
+ def self.restorable_comments(task, ids)
660
+ Comment.public_only.without_approval_action
661
+ .where(id: ids, topic_id: task.topic_id, creative_id: task.creative_id)
662
+ .where.not(user_id: [ task.agent_id, nil ])
663
+ end
664
+ private_class_method :restorable_comments
665
+
666
+ # Claim the comment, then enqueue — and only in that order. The job leaves
667
+ # no row until a worker runs it, so between the two the comment reads as
668
+ # orphaned to anyone else looking, and the sweep is looking every ten
669
+ # minutes. A claim released on failure is the whole difference between
670
+ # this and a dispatch nobody comes back for.
671
+ def self.enqueue_claimed(task, comment_id, context: nil, &publication)
672
+ return unless claim_restore!(task, comment_id)
673
+
674
+ publish_restore_after_claim(task, comment_id, context: context, &publication)
675
+ end
676
+ private_class_method :enqueue_claimed
677
+
678
+ # The dispatch that was discarded — not a descendant of the turn that
679
+ # discarded it. reanchor_payload is the single door for moving an anchor,
680
+ # and TURN_SCOPED_KEYS is stripped after it because every one of them
681
+ # describes the dead turn: its drop list would make a restored turn that
682
+ # fails again restore the same comments a second time, its history record
683
+ # would licence dropping them, its failed handoff would leave a turn that
684
+ # succeeded unable to cover anything it read, its merged list would
685
+ # re-send comments it was created for, and its acquired anchor would
686
+ # label this turn's own trigger as borrowed.
687
+
688
+
689
+ # Comment ids some other task in this turn's scope is already on the hook
690
+ # for, as its trigger or as a merged block. Any status counts: what is
691
+ # being asked is whether a dispatch exists at all, since the only thing
692
+ # this restore undoes is a dispatch that was never allowed to become one.
693
+ #
694
+ # A terminal sibling counts too, and deliberately. For a comment to be in
695
+ # DROPPED_KEY *and* have a row, the row came from a different dispatch of
696
+ # the same comment — a redelivered event, which `duplicate_running_for_
697
+ # comment?` lets past a merely queued waiter — so the discarded one is the
698
+ # duplicate and the row is the comment's own turn. Reading `cancelled` or
699
+ # `failed` there as "no longer coverage" would re-dispatch the duplicate
700
+ # of a turn the user stopped, or that reassignment, agent teardown or a
701
+ # workflow stop cancelled on purpose; restore! re-asks the comment's
702
+ # eligibility but has no way to re-ask those decisions. A turn that failed
703
+ # leaves a failed Task the product already shows and offers to retry,
704
+ # which is the ordinary ending for every turn — not this restore's
705
+ # subject, which is the dispatch that left nothing behind at all.
706
+ # DeliveredHistoryDispatchTest reproduces that state and holds both.
707
+ def self.claimed_comment_ids(task)
708
+ Task.where(
709
+ agent_id: task.agent_id, topic_id: task.topic_id,
710
+ creative_id: task.creative_id, trigger_event_name: task.trigger_event_name
711
+ ).where.not(id: task.id).select(:id, :trigger_event_payload).flat_map { |other|
712
+ other_payload = other.trigger_event_payload
713
+ next [] unless other_payload.is_a?(Hash)
714
+
715
+ [ other_payload.dig("comment", "id") ] + Array(other_payload[TaskCoalescer::PAYLOAD_KEY])
716
+ }.compact.map(&:to_i)
717
+ end
718
+ private_class_method :claimed_comment_ids
719
+
720
+ # The comments the history window carried *whole*, read off the tag
721
+ # MessageBuilder#append_chat_history writes. A window entry can render a
722
+ # comment without delivering it — a blob rides only on the trigger
723
+ # path — and those entries go untagged, so this
724
+ # record never claims them. That judgement lives with the builder, which
725
+ # is the only place that knows what it rendered; see
726
+ # MessageBuilder#delivered_whole?.
727
+ def self.history_comment_ids(resolved)
728
+ messages = resolved.is_a?(Hash) ? Array(resolved[:messages] || resolved["messages"]) : Array(resolved)
729
+ messages.filter_map do |message|
730
+ next unless message.is_a?(Hash)
731
+ next unless (message[:kind] || message["kind"]).to_s == "chat_history"
732
+
733
+ id = message[:comment_id] || message["comment_id"]
734
+ id&.to_i
735
+ end.uniq
736
+ end
737
+ private_class_method :history_comment_ids
738
+ end
739
+ end
740
+ end