@opengeni/api-router 0.30.3 → 1.0.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 (434) hide show
  1. package/dist/access-grant-rls.d.ts +3 -0
  2. package/dist/admission-trace.d.ts +8 -0
  3. package/dist/api-websocket.d.ts +18 -0
  4. package/dist/app.d.ts +50 -2
  5. package/dist/app.js +12 -1
  6. package/dist/artifact-catalog-cursor.d.ts +27 -0
  7. package/dist/auth/managed-auth-attempt-context.d.ts +10 -0
  8. package/dist/auth/managed-auth-rate-limits.d.ts +167 -0
  9. package/dist/auth/managed-auth-session-adapter.d.ts +6 -0
  10. package/dist/auth/managed-auth.d.ts +102 -3
  11. package/dist/auth/managed-email.d.ts +29 -0
  12. package/dist/auth/managed-sign-in-notifications.d.ts +4 -0
  13. package/dist/auth/new-signups-gate.d.ts +23 -0
  14. package/dist/auth/organization-user-setup.d.ts +80 -0
  15. package/dist/auth/signup-funnel-metrics.d.ts +44 -0
  16. package/dist/browser-controller-authority.d.ts +1 -61
  17. package/dist/browser-ephemeral.d.ts +5 -0
  18. package/dist/browser-state-authority.d.ts +1 -27
  19. package/dist/chunk-6TZ5LG3O.js +123948 -0
  20. package/dist/chunk-6TZ5LG3O.js.map +1 -0
  21. package/dist/chunk-OGQDXVXY.js +18 -0
  22. package/dist/chunk-OGQDXVXY.js.map +1 -0
  23. package/dist/claude-subscription-account-conflicts.d.ts +1 -0
  24. package/dist/claude-subscription-account-usage.d.ts +39 -0
  25. package/dist/claude-subscription-oauth.d.ts +25 -0
  26. package/dist/claude-subscription-profile.d.ts +9 -0
  27. package/dist/claude-subscription-usage.d.ts +44 -0
  28. package/dist/claude-workspace-connection.d.ts +15 -0
  29. package/dist/codemode.d.ts +32 -1
  30. package/dist/company-brain-okf.d.ts +24 -0
  31. package/dist/connected-machine-computer-access.d.ts +11 -0
  32. package/dist/connection-ownership.d.ts +54 -0
  33. package/dist/controller-data-plane.d.ts +32 -0
  34. package/dist/editable-artifact-source-session.d.ts +3 -0
  35. package/dist/editable-artifact-websocket.d.ts +10 -18
  36. package/dist/editable-artifact-workspace-files.d.ts +1 -1
  37. package/dist/fatal-process-boundary.d.ts +25 -0
  38. package/dist/github-access.d.ts +31 -2
  39. package/dist/http/acting-person.d.ts +24 -0
  40. package/dist/http/api-contract-compat.d.ts +19 -0
  41. package/dist/http/api-error.d.ts +27 -1
  42. package/dist/http/cors.d.ts +17 -0
  43. package/dist/http/deprecation.d.ts +42 -0
  44. package/dist/http/interaction-control-error.d.ts +13 -0
  45. package/dist/http/keyed-admission.d.ts +12 -0
  46. package/dist/http/local-browser-boundary.d.ts +72 -0
  47. package/dist/http/metrics-listener.d.ts +22 -0
  48. package/dist/http/path-identifier.d.ts +17 -0
  49. package/dist/http/registered-route-label.d.ts +16 -0
  50. package/dist/http/request-body.d.ts +27 -0
  51. package/dist/http/request-source.d.ts +52 -0
  52. package/dist/http/sse.d.ts +17 -6
  53. package/dist/http/unmatched-route.d.ts +18 -0
  54. package/dist/http/user-content.d.ts +36 -0
  55. package/dist/http/websocket-upgrade-dispatch.d.ts +18 -0
  56. package/dist/index.d.ts +10 -3
  57. package/dist/index.js +1091 -162
  58. package/dist/index.js.map +1 -1
  59. package/dist/integration-endpoint-test.d.ts +20 -0
  60. package/dist/integrations/api-integrations.d.ts +2 -2
  61. package/dist/integrations/atlassian.d.ts +29 -3
  62. package/dist/integrations/connect-authority.d.ts +1 -0
  63. package/dist/integrations/connect-callback-return.d.ts +6 -0
  64. package/dist/integrations/fiken.d.ts +16 -2
  65. package/dist/integrations/github-app-connect.d.ts +47 -0
  66. package/dist/integrations/github-installation-proof.d.ts +3 -0
  67. package/dist/integrations/github-lens-connect.d.ts +116 -0
  68. package/dist/integrations/github-skill-source.d.ts +1 -5
  69. package/dist/integrations/google-drive.d.ts +24 -0
  70. package/dist/integrations/integration-commit-authority.d.ts +13 -0
  71. package/dist/integrations/marketplace-plugin.d.ts +2691 -0
  72. package/dist/integrations/oauth-client.d.ts +178 -18
  73. package/dist/integrations/oauth-profiles.d.ts +222 -0
  74. package/dist/integrations/oauth-return-path.d.ts +1 -0
  75. package/dist/integrations/personal-github-repositories.d.ts +84 -0
  76. package/dist/integrations/personal-github.d.ts +42 -0
  77. package/dist/integrations/pr-review-provider.d.ts +22 -0
  78. package/dist/integrations/provider-oauth.d.ts +19 -1
  79. package/dist/integrations/slack-app-home.d.ts +24 -0
  80. package/dist/integrations/slack-bot.d.ts +367 -12
  81. package/dist/integrations/slack-file-upload-flow.d.ts +62 -0
  82. package/dist/integrations/slack-install.d.ts +20 -0
  83. package/dist/integrations/slack-interactions.d.ts +107 -9
  84. package/dist/integrations/slack-mrkdwn.d.ts +41 -0
  85. package/dist/integrations/slack-routing.d.ts +120 -0
  86. package/dist/integrations/slack-session-defaults.d.ts +58 -0
  87. package/dist/integrations/slack-task-file-upload.d.ts +36 -0
  88. package/dist/integrations/social-oauth.d.ts +7 -0
  89. package/dist/interaction-agent-access.d.ts +15 -0
  90. package/dist/interaction-frame-proxy.d.ts +68 -0
  91. package/dist/interaction-metrics.d.ts +2 -0
  92. package/dist/mcp/action-catalog-types.d.ts +9 -0
  93. package/dist/mcp/action-catalog.gen.d.ts +2 -0
  94. package/dist/mcp/capability-account-readiness.d.ts +7 -0
  95. package/dist/mcp/child-read-evidence.d.ts +9 -0
  96. package/dist/mcp/command-read.d.ts +24 -0
  97. package/dist/mcp/company-profile-agent-admin.d.ts +44 -0
  98. package/dist/mcp/contract-input.d.ts +11 -0
  99. package/dist/mcp/documents.d.ts +7 -5
  100. package/dist/mcp/files.d.ts +2 -2
  101. package/dist/mcp/first-party-tool-permissions.d.ts +650 -0
  102. package/dist/mcp/knowledge-entries.d.ts +5 -0
  103. package/dist/mcp/orchestration-failure-diagnostic.d.ts +43 -0
  104. package/dist/mcp/receipts.d.ts +9 -0
  105. package/dist/mcp/request-abort.d.ts +19 -0
  106. package/dist/mcp/scheduled-task-input.d.ts +1189 -0
  107. package/dist/mcp/scheduled-task-view.d.ts +22 -4
  108. package/dist/mcp/server.d.ts +255 -3
  109. package/dist/mcp/session-event-view.d.ts +94 -0
  110. package/dist/mcp/session-view.d.ts +66 -2
  111. package/dist/mcp/session-wait.d.ts +174 -0
  112. package/dist/mcp-oauth.d.ts +52 -0
  113. package/dist/model-catalog.d.ts +11 -31
  114. package/dist/observability.d.ts +3 -1
  115. package/dist/organization-mcp.d.ts +52 -0
  116. package/dist/organization-recovery-notifications.d.ts +41 -0
  117. package/dist/routes/analytics-consent.d.ts +26 -0
  118. package/dist/routes/api-integrations.d.ts +25 -0
  119. package/dist/routes/api-keys.d.ts +17 -0
  120. package/dist/routes/artifact-catalog.d.ts +6 -0
  121. package/dist/routes/automations.d.ts +19 -0
  122. package/dist/routes/billing.d.ts +49 -0
  123. package/dist/routes/browser-sessions.d.ts +59 -2
  124. package/dist/routes/claude-subscription-accounts.d.ts +3 -0
  125. package/dist/routes/claude-subscription-oauth.d.ts +3 -0
  126. package/dist/routes/client-errors.d.ts +146 -0
  127. package/dist/routes/codex.d.ts +76 -2
  128. package/dist/routes/company-brain.d.ts +3 -0
  129. package/dist/routes/company-profile.d.ts +2 -1
  130. package/dist/routes/computer-sessions.d.ts +14 -0
  131. package/dist/routes/connect.d.ts +5 -0
  132. package/dist/routes/editable-artifacts.d.ts +2 -0
  133. package/dist/routes/external-identity-links.d.ts +6 -0
  134. package/dist/routes/feedback.d.ts +3 -0
  135. package/dist/routes/github-browser-pages.d.ts +12 -0
  136. package/dist/routes/github.d.ts +2 -0
  137. package/dist/routes/insights-response-cache.d.ts +10 -0
  138. package/dist/routes/insights-usage.d.ts +12 -0
  139. package/dist/routes/insights.d.ts +30 -1
  140. package/dist/routes/knowledge.d.ts +3 -0
  141. package/dist/routes/managed-auth-session-sets.d.ts +26 -0
  142. package/dist/routes/managed-onboarding.d.ts +29 -0
  143. package/dist/routes/managed-sign-in-methods.d.ts +6 -0
  144. package/dist/routes/model-connection-access.d.ts +3 -0
  145. package/dist/routes/organization-integration-policy.d.ts +3 -0
  146. package/dist/routes/organization-integrations.d.ts +6 -0
  147. package/dist/routes/organization-mcp-connections.d.ts +3 -0
  148. package/dist/routes/organization-model-providers.d.ts +3 -0
  149. package/dist/routes/organization-recovery.d.ts +19 -0
  150. package/dist/routes/organization-service-accounts.d.ts +3 -0
  151. package/dist/routes/organization-sessions.d.ts +26 -0
  152. package/dist/routes/personal-github-git-broker.d.ts +29 -0
  153. package/dist/routes/personal-github.d.ts +3 -0
  154. package/dist/routes/pr-review-github.d.ts +3 -0
  155. package/dist/routes/pr-review.d.ts +3 -0
  156. package/dist/routes/session-artifact-associations.d.ts +4 -0
  157. package/dist/routes/session-history-imports.d.ts +9 -0
  158. package/dist/routes/sessions.d.ts +44 -6
  159. package/dist/routes/skill-content.d.ts +4 -0
  160. package/dist/routes/skill-install-authority.d.ts +6 -0
  161. package/dist/routes/subscription-account-pools.d.ts +34 -0
  162. package/dist/routes/subscription-pool-access.d.ts +16 -0
  163. package/dist/routes/supergrok.d.ts +6 -1
  164. package/dist/routes/usage-allowances.d.ts +12 -0
  165. package/dist/routes/user-resource-authorities.d.ts +3 -0
  166. package/dist/routes/workspace-artifacts.d.ts +2 -1
  167. package/dist/routes/workspace-capture.d.ts +10 -2
  168. package/dist/routes/workspace-integrations.d.ts +35 -0
  169. package/dist/routes/workspace-learning.d.ts +3 -0
  170. package/dist/routes/workspace-model-providers.d.ts +4 -0
  171. package/dist/routes/workspaces.d.ts +29 -0
  172. package/dist/sandbox/auth-callout.d.ts +8 -0
  173. package/dist/sandbox/channel-a.d.ts +19 -1
  174. package/dist/sandbox/connection-authority.d.ts +3 -0
  175. package/dist/sandbox/enrollment.d.ts +9 -1
  176. package/dist/sandbox/machines.d.ts +5 -2
  177. package/dist/sandbox/metrics-ingestion.d.ts +28 -20
  178. package/dist/sandbox/rematerialize.d.ts +2 -0
  179. package/dist/sandbox/session-attach-variable-sets.d.ts +30 -0
  180. package/dist/sandbox/viewer.d.ts +26 -1
  181. package/dist/sandbox-file-artifacts.d.ts +50 -0
  182. package/dist/scheduled-task-deletion.d.ts +13 -0
  183. package/dist/session-create-observability.d.ts +5 -0
  184. package/dist/session-event-view-MPS2OZD3.js +292 -0
  185. package/dist/session-event-view-MPS2OZD3.js.map +1 -0
  186. package/dist/site-session-origin.d.ts +13 -0
  187. package/dist/site-uploads.d.ts +41 -0
  188. package/dist/slack-reaction-files.d.ts +1 -1
  189. package/dist/temporal-schedule-cleanup.d.ts +1 -0
  190. package/dist/temporal-schedule-sync.d.ts +26 -0
  191. package/dist/transcription/providers/xai-subscription.d.ts +1 -0
  192. package/dist/transcription/service.d.ts +2 -1
  193. package/dist/work-discovery-observability.d.ts +33 -0
  194. package/dist/workspace-artifact-content.d.ts +28 -0
  195. package/dist/workspace-artifact-provenance.d.ts +7 -0
  196. package/dist/workspace-delete-observability.d.ts +10 -0
  197. package/dist/workspace-deletion.d.ts +6 -0
  198. package/dist/workspace-state-projection.d.ts +3 -5
  199. package/dist/workspace-tool-gateway-observability.d.ts +17 -0
  200. package/dist/workspace-tool-gateway.d.ts +123 -0
  201. package/dist/workspace-webhook-dispatch.d.ts +26 -0
  202. package/package.json +23 -19
  203. package/src/access-grant-rls.ts +75 -0
  204. package/src/admission-trace.ts +14 -0
  205. package/src/api-websocket.ts +23 -0
  206. package/src/app.ts +1967 -252
  207. package/src/artifact-catalog-cursor.ts +60 -0
  208. package/src/auth/managed-auth-attempt-context.ts +70 -0
  209. package/src/auth/managed-auth-rate-limits.ts +252 -0
  210. package/src/auth/managed-auth-session-adapter.ts +222 -0
  211. package/src/auth/managed-auth.ts +536 -60
  212. package/src/auth/managed-email.ts +174 -0
  213. package/src/auth/managed-sign-in-notifications.ts +81 -0
  214. package/src/auth/new-signups-gate.ts +49 -0
  215. package/src/auth/organization-user-setup.ts +307 -0
  216. package/src/auth/signup-funnel-metrics.ts +164 -0
  217. package/src/browser-controller-authority.ts +1 -186
  218. package/src/browser-ephemeral.ts +39 -0
  219. package/src/browser-state-authority.ts +1 -236
  220. package/src/claude-subscription-account-conflicts.ts +12 -0
  221. package/src/claude-subscription-account-usage.ts +48 -0
  222. package/src/claude-subscription-oauth.ts +362 -0
  223. package/src/claude-subscription-profile.ts +73 -0
  224. package/src/claude-subscription-usage.ts +97 -0
  225. package/src/claude-workspace-connection.ts +82 -0
  226. package/src/codemode.ts +194 -30
  227. package/src/codex-realtime.ts +8 -2
  228. package/src/company-brain-okf.ts +340 -0
  229. package/src/connected-machine-computer-access.ts +33 -0
  230. package/src/connection-ownership.ts +199 -0
  231. package/src/controller-data-plane.ts +81 -0
  232. package/src/editable-artifact-native-kernel.ts +26 -15
  233. package/src/editable-artifact-office-import.ts +28 -3
  234. package/src/editable-artifact-production.ts +23 -7
  235. package/src/editable-artifact-source-session.ts +123 -0
  236. package/src/editable-artifact-websocket.ts +27 -21
  237. package/src/editable-artifact-workspace-files.ts +31 -6
  238. package/src/fatal-process-boundary.ts +246 -0
  239. package/src/gateway-realtime.ts +1 -1
  240. package/src/github-access.ts +147 -5
  241. package/src/http/acting-person.ts +71 -0
  242. package/src/http/api-contract-compat.ts +49 -0
  243. package/src/http/api-error.ts +115 -1
  244. package/src/http/auth.ts +48 -0
  245. package/src/http/cors.ts +67 -0
  246. package/src/http/deprecation.ts +118 -0
  247. package/src/http/interaction-control-error.ts +164 -0
  248. package/src/http/keyed-admission.ts +29 -0
  249. package/src/http/local-browser-boundary.ts +343 -0
  250. package/src/http/metrics-listener.ts +57 -0
  251. package/src/http/path-identifier.ts +54 -0
  252. package/src/http/registered-route-label.ts +34 -0
  253. package/src/http/request-body.ts +127 -0
  254. package/src/http/request-source.ts +178 -0
  255. package/src/http/sse.ts +486 -132
  256. package/src/http/unmatched-route.ts +60 -0
  257. package/src/http/user-content.ts +158 -0
  258. package/src/http/websocket-upgrade-dispatch.ts +29 -0
  259. package/src/index.ts +157 -52
  260. package/src/integration-endpoint-test.ts +291 -0
  261. package/src/integrations/api-integrations.ts +98 -11
  262. package/src/integrations/atlassian.ts +474 -136
  263. package/src/integrations/connect-authority.ts +2 -0
  264. package/src/integrations/connect-callback-return.ts +86 -0
  265. package/src/integrations/fiken.ts +354 -64
  266. package/src/integrations/github-app-connect.ts +435 -0
  267. package/src/integrations/github-installation-proof.ts +60 -0
  268. package/src/integrations/github-lens-connect.ts +76 -0
  269. package/src/integrations/github-skill-source.ts +2 -142
  270. package/src/integrations/google-drive.ts +717 -225
  271. package/src/integrations/integration-commit-authority.ts +69 -0
  272. package/src/integrations/marketplace-plugin.ts +48 -0
  273. package/src/integrations/oauth-client.ts +1356 -459
  274. package/src/integrations/oauth-profiles.ts +537 -0
  275. package/src/integrations/oauth-return-path.ts +18 -0
  276. package/src/integrations/personal-github-repositories.ts +674 -0
  277. package/src/integrations/personal-github.ts +825 -0
  278. package/src/integrations/pr-review-provider.ts +246 -0
  279. package/src/integrations/provider-oauth.ts +250 -22
  280. package/src/integrations/slack-app-home.ts +300 -0
  281. package/src/integrations/slack-bot.ts +963 -239
  282. package/src/integrations/slack-file-upload-flow.ts +147 -0
  283. package/src/integrations/slack-install.ts +83 -0
  284. package/src/integrations/slack-interactions.ts +3167 -474
  285. package/src/integrations/slack-mrkdwn.ts +193 -0
  286. package/src/integrations/slack-routing.ts +337 -0
  287. package/src/integrations/slack-session-defaults.ts +224 -0
  288. package/src/integrations/slack-task-file-upload.ts +323 -0
  289. package/src/integrations/social-oauth.ts +213 -15
  290. package/src/interaction-agent-access.ts +48 -0
  291. package/src/interaction-frame-proxy.ts +409 -0
  292. package/src/interaction-metrics.ts +30 -0
  293. package/src/mcp/action-catalog-types.ts +9 -0
  294. package/src/mcp/action-catalog.gen.ts +6696 -0
  295. package/src/mcp/capability-account-readiness.ts +17 -0
  296. package/src/mcp/child-read-evidence.ts +19 -0
  297. package/src/mcp/command-read.ts +109 -0
  298. package/src/mcp/company-profile-agent-admin.ts +198 -0
  299. package/src/mcp/contract-input.ts +141 -0
  300. package/src/mcp/documents.ts +65 -377
  301. package/src/mcp/editable-artifacts.ts +12 -6
  302. package/src/mcp/files.ts +80 -43
  303. package/src/mcp/first-party-tool-permissions.ts +305 -0
  304. package/src/mcp/knowledge-entries.ts +322 -0
  305. package/src/mcp/orchestration-failure-diagnostic.ts +43 -0
  306. package/src/mcp/receipts.ts +34 -0
  307. package/src/mcp/request-abort.ts +44 -0
  308. package/src/mcp/scheduled-task-input.ts +76 -0
  309. package/src/mcp/scheduled-task-view.ts +44 -3
  310. package/src/mcp/server.ts +3030 -1267
  311. package/src/mcp/session-event-view.ts +383 -0
  312. package/src/mcp/session-view.ts +84 -3
  313. package/src/mcp/session-wait.ts +665 -0
  314. package/src/mcp-oauth.ts +1123 -0
  315. package/src/model-catalog.ts +91 -333
  316. package/src/observability.ts +15 -1
  317. package/src/organization-mcp.ts +397 -0
  318. package/src/organization-recovery-notifications.ts +103 -0
  319. package/src/routes/analytics-consent.ts +123 -0
  320. package/src/routes/api-integrations.ts +364 -74
  321. package/src/routes/api-keys.ts +336 -10
  322. package/src/routes/artifact-catalog.ts +279 -0
  323. package/src/routes/automations.ts +768 -0
  324. package/src/routes/billing.ts +461 -46
  325. package/src/routes/browser-sessions.ts +780 -112
  326. package/src/routes/canonical-human-identities.ts +90 -14
  327. package/src/routes/capabilities.ts +107 -6
  328. package/src/routes/channels.ts +20 -3
  329. package/src/routes/claude-subscription-accounts.ts +334 -0
  330. package/src/routes/claude-subscription-oauth.ts +142 -0
  331. package/src/routes/client-errors.ts +362 -0
  332. package/src/routes/codex.ts +833 -116
  333. package/src/routes/company-brain.ts +322 -0
  334. package/src/routes/company-profile.ts +65 -1
  335. package/src/routes/computer-sessions.ts +557 -106
  336. package/src/routes/connect.ts +1938 -0
  337. package/src/routes/connections.ts +1039 -349
  338. package/src/routes/documents.ts +488 -287
  339. package/src/routes/editable-artifacts.ts +79 -9
  340. package/src/routes/enrollments.ts +135 -39
  341. package/src/routes/environments.ts +154 -58
  342. package/src/routes/external-identity-links.ts +201 -0
  343. package/src/routes/feedback.ts +113 -0
  344. package/src/routes/files.ts +267 -33
  345. package/src/routes/github-browser-pages.ts +162 -0
  346. package/src/routes/github.ts +446 -144
  347. package/src/routes/insights-response-cache.ts +83 -0
  348. package/src/routes/insights-usage.ts +345 -0
  349. package/src/routes/insights.ts +153 -19
  350. package/src/routes/install.ts +133 -38
  351. package/src/routes/integration-facets.ts +107 -33
  352. package/src/routes/interaction-resources.ts +30 -3
  353. package/src/routes/knowledge.ts +488 -0
  354. package/src/routes/machines.ts +313 -15
  355. package/src/routes/managed-auth-session-sets.ts +1218 -0
  356. package/src/routes/managed-onboarding.ts +337 -0
  357. package/src/routes/managed-sign-in-methods.ts +433 -0
  358. package/src/routes/memory-slack-publications.ts +3 -2
  359. package/src/routes/model-connection-access.ts +208 -0
  360. package/src/routes/organization-integration-policy.ts +116 -0
  361. package/src/routes/organization-integrations.ts +326 -0
  362. package/src/routes/organization-mcp-connections.ts +178 -0
  363. package/src/routes/organization-memberships.ts +1019 -27
  364. package/src/routes/organization-model-providers.ts +293 -0
  365. package/src/routes/organization-recovery.ts +325 -0
  366. package/src/routes/organization-service-accounts.ts +128 -0
  367. package/src/routes/organization-sessions.ts +350 -0
  368. package/src/routes/personal-github-git-broker.ts +785 -0
  369. package/src/routes/personal-github.ts +370 -0
  370. package/src/routes/plugins.ts +251 -22
  371. package/src/routes/pr-review-github.ts +881 -0
  372. package/src/routes/pr-review.ts +622 -0
  373. package/src/routes/preference-registry.ts +12 -93
  374. package/src/routes/rigs.ts +167 -50
  375. package/src/routes/scheduled-tasks.ts +346 -41
  376. package/src/routes/session-artifact-associations.ts +87 -0
  377. package/src/routes/session-history-imports.ts +280 -0
  378. package/src/routes/sessions.ts +2306 -264
  379. package/src/routes/skill-content.ts +297 -0
  380. package/src/routes/skill-install-authority.ts +64 -0
  381. package/src/routes/skills.ts +62 -9
  382. package/src/routes/social.ts +46 -18
  383. package/src/routes/subscription-account-pools.ts +436 -0
  384. package/src/routes/subscription-pool-access.ts +126 -0
  385. package/src/routes/supergrok.ts +219 -303
  386. package/src/routes/transcription-recordings.ts +38 -3
  387. package/src/routes/transcriptions.ts +4 -0
  388. package/src/routes/usage-allowances.ts +378 -0
  389. package/src/routes/user-resource-authorities.ts +138 -0
  390. package/src/routes/video-generation.ts +14 -10
  391. package/src/routes/workspace-artifacts.ts +275 -69
  392. package/src/routes/workspace-capture.ts +21 -0
  393. package/src/routes/workspace-integrations.ts +576 -0
  394. package/src/routes/workspace-learning.ts +159 -0
  395. package/src/routes/workspace-model-providers.ts +148 -0
  396. package/src/routes/workspace-state.ts +38 -25
  397. package/src/routes/workspaces.ts +1004 -127
  398. package/src/sandbox/auth-callout.ts +96 -19
  399. package/src/sandbox/channel-a.ts +323 -24
  400. package/src/sandbox/connection-authority.ts +3 -0
  401. package/src/sandbox/enrollment.ts +81 -6
  402. package/src/sandbox/machines.ts +218 -76
  403. package/src/sandbox/metrics-ingestion.ts +411 -62
  404. package/src/sandbox/rematerialize.ts +121 -19
  405. package/src/sandbox/session-attach-variable-sets.ts +131 -0
  406. package/src/sandbox/viewer.ts +280 -25
  407. package/src/sandbox-file-artifacts.ts +413 -0
  408. package/src/scheduled-task-deletion.ts +138 -0
  409. package/src/session-create-observability.ts +46 -0
  410. package/src/session-realtime-context.ts +6 -3
  411. package/src/site-session-origin.ts +48 -0
  412. package/src/site-uploads.ts +224 -0
  413. package/src/slack-reaction-files.ts +16 -5
  414. package/src/temporal-schedule-cleanup.ts +13 -0
  415. package/src/temporal-schedule-sync.ts +74 -0
  416. package/src/transcription/providers/codex-subscription.ts +2 -0
  417. package/src/transcription/providers/openai.ts +1 -0
  418. package/src/transcription/providers/xai-subscription.ts +49 -3
  419. package/src/transcription/segmenter.ts +8 -2
  420. package/src/transcription/service.ts +43 -6
  421. package/src/work-discovery-observability.ts +121 -0
  422. package/src/workspace-artifact-content.ts +144 -0
  423. package/src/workspace-artifact-provenance.ts +104 -0
  424. package/src/workspace-delete-observability.ts +75 -0
  425. package/src/workspace-deletion.ts +73 -0
  426. package/src/workspace-state-projection.ts +18 -148
  427. package/src/workspace-tool-gateway-observability.ts +100 -0
  428. package/src/workspace-tool-gateway.ts +870 -0
  429. package/src/workspace-webhook-dispatch.ts +190 -0
  430. package/src/xai-subscription-auth.ts +9 -2
  431. package/dist/chunk-WCXHI3FF.js +0 -49920
  432. package/dist/chunk-WCXHI3FF.js.map +0 -1
  433. package/dist/routes/packs.d.ts +0 -3
  434. package/src/routes/packs.ts +0 -729
@@ -3,22 +3,37 @@ import {
3
3
  approvalIdentifier,
4
4
  ApproveSlackUserLinkAccessRequest,
5
5
  DEFAULT_FIRST_PARTY_MCP_TOOLS,
6
+ resolveWorkspaceSessionToolDefaults,
6
7
  hasOpenGeniSlackReactionScope,
7
8
  ListSlackUserLinkAccessRequestsResponse,
8
9
  PrepareSlackUserLinkAccessRequest,
9
10
  evaluateSlackTaskPolicy,
11
+ isStreamedAssistantMessageCompletion,
12
+ turnCompletedReply,
13
+ resolveWorkspaceSlackOrchestrationNoticeSettings,
10
14
  resolveWorkspaceSlackReactionSummonSettings,
15
+ SlackChannelRouteListResponse,
11
16
  SlackReactionChannelListResponse,
17
+ UpdateSlackChannelRoutesRequest,
12
18
  SlackUserLinkAccessMutationRequest,
13
19
  SlackUserLinkAccessRequest,
14
20
  type AccessGrant,
15
21
  type FileResourceRef,
16
22
  type FirstPartyMcpToolName,
17
23
  type HumanInputQuestion,
24
+ type ResolvedWorkspaceSlackOrchestrationNoticeSettings,
25
+ type ChildRequiresActionPayload,
26
+ type RepositoryResourceRef,
27
+ type Session,
28
+ type SessionAuthorizationListScope,
18
29
  type SessionEvent,
19
30
  type WorkspaceSlackReactionSummonSettings,
20
31
  workspaceSlackReactionChannelAllowed,
21
32
  } from "@opengeni/contracts";
33
+ import {
34
+ allowanceExhaustedMessage,
35
+ parseAllowanceExhaustedRefusal,
36
+ } from "@opengeni/contracts/allowance-refusal";
22
37
  import {
23
38
  allowedFirstPartyMcpToolsForSession,
24
39
  resolveFirstPartyMcpToolPolicy,
@@ -31,50 +46,79 @@ import {
31
46
  advanceSlackInteractionDelivery,
32
47
  bindSlackInteractionSession,
33
48
  cancelSlackUserLinkAccessRequest,
49
+ claimSlackAppHomeRefresh,
34
50
  claimSlackInteractionDelivery,
35
51
  claimSlackInteractionProgressDelivery,
36
52
  claimSlackInteractionInbox,
37
53
  closeSlackInteractionDelivery,
38
54
  completeSlackUserLinkAccessIfGranted,
55
+ decodeSessionListCursor,
39
56
  deferSlackInteractionDelivery,
40
57
  deleteSlackBotUserLink,
58
+ enqueueSlackAppHomeRefresh,
41
59
  enqueueSlackInteractionInbox,
42
60
  getConnectionMetadata,
43
61
  getOrCreateSlackInteraction,
44
62
  getLatestSessionModelForSubject,
45
63
  getSession,
64
+ getSessionByCreateIdempotencyKey,
46
65
  getSessionEvent,
47
66
  getSessionHumanInputRequest,
67
+ childRequiresActionResolutionExists,
68
+ getSessionSystemUpdateById,
48
69
  getSlackBotPostOperation,
49
70
  getSlackBotUserLink,
50
71
  getSlackInteractionActionHandle,
51
72
  getSlackInteractionByClientEventId,
52
73
  getSlackInteractionById,
53
- getSlackInteractionByRoute,
74
+ getSlackInteractionByConnectionRoute,
75
+ probeSlackActionHandleTenancy,
76
+ openSlackRoutePrompt,
77
+ getSlackRoutePromptByOption,
78
+ settleSlackRoutePrompt,
79
+ answerSlackRoutePrompt,
80
+ type SlackRoutableWorkspace,
81
+ getSlackChannelRoute,
82
+ listSlackChannelRoutes,
83
+ upsertSlackChannelRoute,
84
+ deleteSlackChannelRoute,
85
+ getSlackUserDmRoute,
86
+ listNamedSubjectSlackRoutableWorkspaces,
54
87
  getActiveSlackTaskPolicy,
55
88
  getSlackSharedTaskOrigin,
56
89
  getSessionEventByClientEventId,
57
90
  getWorkspace,
58
91
  getWorkspaceGrant,
92
+ namedSubjectPersonalWorkspaceId,
93
+ resolveSlackTargetAuthority,
59
94
  listSlackInteractionProgressDeliveryEvidence,
60
95
  listSessionEventPage,
61
96
  listSessionHumanInputRequests,
97
+ listPendingSlackInteractionMessageActionHandles,
62
98
  listPendingSlackUserLinkAccessRequests,
99
+ listSlackInstallationBindings,
100
+ slackUserLinkAccessRequestTeam,
63
101
  rekeySlackInteractionRoute,
64
102
  reopenSlackInteractionDelivery,
65
103
  reserveSlackInteractionActionHandles,
104
+ renewSlackAppHomeRefreshClaim,
66
105
  releaseSlackInteractionDelivery,
106
+ releaseSlackAppHomeRefresh,
67
107
  releaseSlackInteractionInbox,
68
108
  requestSlackUserLinkWorkspaceAccess,
69
109
  resolveSlackInstallationRoute,
70
- saveSlackInteractionInboxReactionCheckpoint,
110
+ resolveSlackInteractionFirstTaskHint,
71
111
  saveSlackSharedTaskOrigin,
72
112
  settleSlackInteractionInbox,
113
+ settleSlackAppHomeRefresh,
73
114
  settleSlackInteractionActionHandles,
115
+ listSessionsForSubject,
116
+ SessionListAccessError,
74
117
  denySlackUserLinkAccessRequest,
75
118
  prepareSlackUserLinkAccessRequest,
76
119
  SlackUserLinkAccessPersistenceError,
77
120
  type SlackInstallationRoute,
121
+ type SlackAppHomeRefresh,
78
122
  type SlackInteraction,
79
123
  type SlackInteractionActionHandle,
80
124
  type SlackInteractionActionKind,
@@ -85,12 +129,19 @@ import {
85
129
  acceptSessionUserMessage,
86
130
  controlHumanSessionWorkstream,
87
131
  createSessionForRequest,
132
+ getActorNewSessionModelChoice,
88
133
  hasPermission,
89
134
  requireAccessContext,
90
135
  requireAccessGrant,
136
+ requireSessionAuthorizationListScope,
137
+ resolveWorkspaceCatalogSettings,
91
138
  type ApiRouteDeps,
92
139
  } from "@opengeni/core";
93
140
  import { publishDurableSessionEvents } from "@opengeni/events";
141
+ import {
142
+ otherDeploymentLinkContext,
143
+ slackRecentRepositoryResources,
144
+ } from "./slack-session-defaults";
94
145
  import type { Context, Hono } from "hono";
95
146
  import { HTTPException } from "hono/http-exception";
96
147
  import {
@@ -98,9 +149,27 @@ import {
98
149
  SLACK_REACTION_IMAGE_MAX_BYTES,
99
150
  type OpenGeniSlackBotClient,
100
151
  type SlackMessageBlock,
152
+ SlackBotOperationConflictError,
101
153
  SlackBotProviderError,
154
+ slackHistoryRateLimited,
102
155
  } from "./slack-bot";
156
+ import { slackMrkdwnFromMarkdown } from "./slack-mrkdwn";
157
+ import {
158
+ isSlackDirectMessageConversation,
159
+ resolveSlackWorkspaceRoute,
160
+ slackRoutedRequestText,
161
+ type SlackRouteResolution,
162
+ type SlackRouteTenancy,
163
+ } from "./slack-routing";
164
+ import {
165
+ buildSlackAppHomeAccessBlocks,
166
+ buildSlackAppHomeBlocks,
167
+ escapeSlackMrkdwn,
168
+ isSlackAppHomeLinkAction,
169
+ slackAppHomeOpenedEvent,
170
+ } from "./slack-app-home";
103
171
  import { importSlackReactionImage, type ImportedSlackReactionImage } from "../slack-reaction-files";
172
+ import { parseRequestJson } from "../http/request-body";
104
173
 
105
174
  export const SLACK_INTERACTION_MAX_BODY_BYTES = 256 * 1024;
106
175
  export const SLACK_SIGNATURE_REPLAY_WINDOW_SECONDS = 300;
@@ -112,6 +181,13 @@ export const SLACK_DELIVERY_EVENT_TYPES = [
112
181
  "turn.failed",
113
182
  "turn.cancelled",
114
183
  "session.status.changed",
184
+ // Orchestration surfacing. Both are filtered again inside the pump: the
185
+ // workspace must have opted in (both notices default off), and then only a
186
+ // `child_requires_action` notice and a `limits` / `max_auto_continuations`
187
+ // goal pause reach Slack, so the thread stays quiet for the deferred child
188
+ // lifecycle kinds and for human/API/agent pauses the human already made.
189
+ "system.update.pending",
190
+ "goal.paused",
115
191
  ] as const;
116
192
 
117
193
  const MAX_SLACK_TEXT_CHARS = 3_500;
@@ -125,6 +201,7 @@ const MAX_SLACK_REACTION_IMAGE_AGGREGATE_BYTES = 16 * 1024 * 1024;
125
201
  const MAX_PROGRESS_MESSAGES = 3;
126
202
  const SLACK_USER_LINK_TTL_MS = 15 * 60_000;
127
203
  const INBOX_LEASE_MS = 30_000;
204
+ const APP_HOME_LEASE_MS = 300_000;
128
205
  const DELIVERY_LEASE_MS = 30_000;
129
206
  const MAX_DELIVERY_ATTEMPTS = 8;
130
207
  const MAX_DELIVERY_RETRY_MS = 5 * 60_000;
@@ -132,6 +209,23 @@ const SLACK_INTERACTION_BOT_SUBJECT_ID = "service:slack-interaction";
132
209
  const SLACK_ACTION_TTL_MS = 7 * 24 * 60 * 60_000;
133
210
  const MAX_SLACK_APPROVALS_PER_CARD = 8;
134
211
  const MAX_SLACK_ACTIONS_PER_CARD = 20;
212
+ /** Blocked-worker cards are pointers, so the question preview stays short. */
213
+ const MAX_SLACK_CHILD_DETAIL_CHARS = 240;
214
+ const SLACK_UUID_PATTERN = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/iu;
215
+ /**
216
+ * Goal pause reasons the Slack thread announces. `user_pause`, `api`, `agent`,
217
+ * and `no_progress` are deliberately absent.
218
+ *
219
+ * A Map, not a plain object: this lookup is the sole gate of a two-reason
220
+ * invariant, and a plain object indexed by a payload-derived string answers
221
+ * `constructor` (and every other prototype key) with a truthy value that would
222
+ * sail past the `if (!headline)` guard. No payload reaches it with such a
223
+ * reason today; the gate should not depend on that staying true.
224
+ */
225
+ const SLACK_GOAL_PAUSED_HEADLINES = new Map<string, string>([
226
+ ["limits", "Goal paused (budget)"],
227
+ ["max_auto_continuations", "Goal paused (continuation cap)"],
228
+ ]);
135
229
  /**
136
230
  * Slack delivery restrictions are durable session-level authority, not
137
231
  * attacker-adjacent user-message context. Migration 0240 backfills this exact
@@ -148,9 +242,9 @@ export const SLACK_SESSION_INSTRUCTIONS = [
148
242
 
149
243
  /**
150
244
  * Slack-originated tasks may retrieve the workspace bot's bounded read surface
151
- * on demand. Connector tools are explicit-only, so freeze that narrow context
152
- * selection at session creation while keeping Slack mutations out of the model
153
- * surface; interaction delivery remains owned by the durable delivery pump.
245
+ * and explicitly deliver a retained task file to their own thread. Connector
246
+ * tools are explicit-only, so freeze that narrow selection at session creation;
247
+ * ordinary interaction messages remain owned by the durable delivery pump.
154
248
  */
155
249
  export const SLACK_TASK_FIRST_PARTY_MCP_TOOLS = [
156
250
  ...DEFAULT_FIRST_PARTY_MCP_TOOLS,
@@ -161,14 +255,25 @@ export const SLACK_TASK_FIRST_PARTY_MCP_TOOLS = [
161
255
  "slack_bot_list_files",
162
256
  "slack_bot_file_info",
163
257
  "slack_bot_file_content",
258
+ "slack_bot_upload_file",
164
259
  ] satisfies readonly FirstPartyMcpToolName[];
165
260
 
166
- function slackTaskFirstPartyMcpTools(settings: Settings): FirstPartyMcpToolName[] {
167
- const policy = resolveFirstPartyMcpToolPolicy(settings);
261
+ /**
262
+ * The workspace's default first-party selection (or the deployment default)
263
+ * plus the bounded Slack task tools. These are added whatever the base is, then
264
+ * intersected with deployment policy; existing accepted selections stay frozen.
265
+ */
266
+ export function slackTaskFirstPartyMcpTools(
267
+ settings: Settings,
268
+ workspaceSettings: unknown,
269
+ ): FirstPartyMcpToolName[] {
270
+ const base =
271
+ resolveWorkspaceSessionToolDefaults(workspaceSettings)?.firstPartyMcpTools ??
272
+ resolveFirstPartyMcpToolPolicy(settings).default;
168
273
  const connectorTools = SLACK_TASK_FIRST_PARTY_MCP_TOOLS.filter(
169
274
  (tool) => !DEFAULT_FIRST_PARTY_MCP_TOOLS.includes(tool),
170
275
  );
171
- return allowedFirstPartyMcpToolsForSession(settings, [...policy.default, ...connectorTools]);
276
+ return allowedFirstPartyMcpToolsForSession(settings, [...base, ...connectorTools]);
172
277
  }
173
278
 
174
279
  export type NormalizedSlackInteraction = {
@@ -340,6 +445,46 @@ export function slackReactionInboxEntry(
340
445
  };
341
446
  }
342
447
 
448
+ /**
449
+ * Prove a Slack link token may name this workspace.
450
+ *
451
+ * Before routing, a token could only ever name the installation's own
452
+ * workspace, and both intent routes asserted exactly that. A routed
453
+ * conversation asks for access to the workspace the work would actually land
454
+ * in, which is a different one, so the assertion becomes: the installation
455
+ * binding resolves for the token's team, it is the same connection the token
456
+ * was minted against, and the named workspace belongs to that installation's
457
+ * organization.
458
+ *
459
+ * That is the same fence the route tables carry. It does not widen access by
460
+ * itself: this only decides which workspace an access REQUEST may be raised
461
+ * for, and the request still goes through the ordinary approval path.
462
+ */
463
+ async function resolveSlackLinkTargetInstallation(
464
+ deps: ApiRouteDeps,
465
+ link: { slackTeamId: string; connectionId: string },
466
+ workspaceId: string,
467
+ ): Promise<{
468
+ accountId: string;
469
+ connectionId: string;
470
+ workspaceName: string;
471
+ /** Where the identity link lives, which is not the routed workspace. */
472
+ identityHome: { accountId: string; workspaceId: string };
473
+ } | null> {
474
+ const route = await resolveSlackInstallationRoute(deps.db, link.slackTeamId);
475
+ if (!route || route.connectionId !== link.connectionId || route.accountId.length === 0) {
476
+ return null;
477
+ }
478
+ const workspace = await getWorkspace(deps.db, workspaceId);
479
+ if (!workspace || workspace.accountId !== route.accountId) return null;
480
+ return {
481
+ accountId: route.accountId,
482
+ connectionId: route.connectionId,
483
+ workspaceName: workspace.name,
484
+ identityHome: { accountId: route.accountId, workspaceId: route.workspaceId },
485
+ };
486
+ }
487
+
343
488
  export function registerSlackInteractionRoutes(app: Hono, deps: ApiRouteDeps): void {
344
489
  app.post("/v1/integrations/slack/events", async (c) => {
345
490
  const signed = await readSignedSlackRequest(c, deps);
@@ -357,6 +502,19 @@ export function registerSlackInteractionRoutes(app: Hono, deps: ApiRouteDeps): v
357
502
  message: "Slack installation unavailable",
358
503
  });
359
504
  const event = record(payload.event);
505
+ const appHomeEvent = slackAppHomeOpenedEvent(payload);
506
+ if (appHomeEvent) {
507
+ await enqueueSlackAppHomeRefresh(deps.db, {
508
+ accountId: installation.accountId,
509
+ workspaceId: installation.workspaceId,
510
+ connectionId: installation.connectionId,
511
+ slackTeamId: appHomeEvent.slackTeamId,
512
+ slackUserId: appHomeEvent.slackUserId,
513
+ providerEventId: appHomeEvent.eventId,
514
+ providerViewHash: appHomeEvent.viewHash,
515
+ });
516
+ return c.json({ ok: true });
517
+ }
360
518
  if (event?.type === "reaction_added") {
361
519
  const [workspace, connection] = await Promise.all([
362
520
  getWorkspace(deps.db, installation.workspaceId),
@@ -383,7 +541,7 @@ export function registerSlackInteractionRoutes(app: Hono, deps: ApiRouteDeps): v
383
541
  app.post("/v1/integrations/slack/commands", async (c) => {
384
542
  const signed = await readSignedSlackRequest(c, deps);
385
543
  const form = new URLSearchParams(signed.rawBody);
386
- if (form.get("command") !== "/opengeni") {
544
+ if (form.get("command") !== deps.settings.slackCommand) {
387
545
  throw new HTTPException(400, { message: "invalid Slack command" });
388
546
  }
389
547
  const entry = normalizedFormInteraction(form, "slash_command");
@@ -392,6 +550,12 @@ export function registerSlackInteractionRoutes(app: Hono, deps: ApiRouteDeps): v
392
550
  throw new HTTPException(403, {
393
551
  message: "Slack installation unavailable",
394
552
  });
553
+ // `<command> info` is a read-only ephemeral explainer. It never reaches the
554
+ // durable inbox, never verifies channel membership, and never creates a
555
+ // session, so it is safe anywhere the command is available.
556
+ if (isSlackInfoCommand(entry.text)) {
557
+ return c.json(await slackInfoCommandResponse(deps, installation, entry));
558
+ }
395
559
  const client = await createOpenGeniSlackBotInteractionClient(deps, {
396
560
  accountId: installation.accountId,
397
561
  workspaceId: installation.workspaceId,
@@ -403,7 +567,7 @@ export function registerSlackInteractionRoutes(app: Hono, deps: ApiRouteDeps): v
403
567
  } catch (error) {
404
568
  if (error instanceof SlackBotProviderError && error.code === "not_in_channel") {
405
569
  return c.text(
406
- "OpenGeni is not a member of this channel. Add @OpenGeni, then run /opengeni again.",
570
+ "Opengeni is not a member of this channel. Add @OpenGeni, then run /opengeni again.",
407
571
  200,
408
572
  );
409
573
  }
@@ -416,7 +580,7 @@ export function registerSlackInteractionRoutes(app: Hono, deps: ApiRouteDeps): v
416
580
  }
417
581
  }
418
582
  await enqueueNormalizedSlackInteraction(deps, installation, entry);
419
- return c.text("OpenGeni accepted this task and will reply in a thread.", 200);
583
+ return c.text("Opengeni accepted this task and will reply in a thread.", 200);
420
584
  });
421
585
 
422
586
  app.post("/v1/integrations/slack/interactions", async (c) => {
@@ -424,6 +588,7 @@ export function registerSlackInteractionRoutes(app: Hono, deps: ApiRouteDeps): v
424
588
  const form = new URLSearchParams(signed.rawBody);
425
589
  const payload = parseJsonObject(form.get("payload") ?? "");
426
590
  if (payload.type === "block_actions") {
591
+ if (isSlackAppHomeLinkAction(payload)) return c.json({ ok: true });
427
592
  const entry = normalizedBlockActionInteraction(payload);
428
593
  const installation = await resolveSlackInstallationRoute(deps.db, entry.slackTeamId);
429
594
  if (!installation) {
@@ -476,28 +641,19 @@ export function registerSlackInteractionRoutes(app: Hono, deps: ApiRouteDeps): v
476
641
  app.post("/v1/workspaces/:workspaceId/integrations/slack/user-link-intents", async (c) => {
477
642
  const workspaceId = c.req.param("workspaceId");
478
643
  const context = await requireManagedSlackLinkHuman(c, deps);
479
- const payload = PrepareSlackUserLinkAccessRequest.parse(await c.req.json());
644
+ const payload = await parseRequestJson(c, PrepareSlackUserLinkAccessRequest);
480
645
  const signingSecret = deps.settings.slackSigningSecret;
481
646
  const link = signingSecret ? verifySlackUserLinkToken(signingSecret, payload.linkToken) : null;
482
647
  if (!link || link.workspaceId !== workspaceId) {
483
648
  throw freshSlackLinkRequired();
484
649
  }
485
- const route = await resolveSlackInstallationRoute(deps.db, link.slackTeamId);
486
- if (
487
- !route ||
488
- route.workspaceId !== workspaceId ||
489
- route.connectionId !== link.connectionId ||
490
- route.accountId.length === 0
491
- ) {
492
- throw freshSlackLinkRequired();
493
- }
494
- const workspace = await getWorkspace(deps.db, workspaceId);
495
- if (!workspace || workspace.accountId !== route.accountId) {
650
+ const installation = await resolveSlackLinkTargetInstallation(deps, link, workspaceId);
651
+ if (!installation) {
496
652
  throw freshSlackLinkRequired();
497
653
  }
498
654
  try {
499
655
  const prepared = await prepareSlackUserLinkAccessRequest(deps.db, {
500
- accountId: route.accountId,
656
+ accountId: installation.accountId,
501
657
  workspaceId,
502
658
  tokenDigest: createHash("sha256").update(payload.linkToken).digest("hex"),
503
659
  connectionId: link.connectionId,
@@ -511,12 +667,13 @@ export function registerSlackInteractionRoutes(app: Hono, deps: ApiRouteDeps): v
511
667
  workspaceId,
512
668
  requestId: prepared.id,
513
669
  subjectId: context.subjectId,
670
+ identityHome: installation.identityHome,
514
671
  });
515
672
  if (!completed) throw freshSlackLinkRequired();
516
673
  return c.json(
517
674
  SlackUserLinkAccessRequest.parse({
518
675
  ...completed,
519
- workspaceDisplayName: workspace.name,
676
+ workspaceDisplayName: installation.workspaceName,
520
677
  }),
521
678
  201,
522
679
  );
@@ -556,7 +713,7 @@ export function registerSlackInteractionRoutes(app: Hono, deps: ApiRouteDeps): v
556
713
  async (c) => {
557
714
  const workspaceId = c.req.param("workspaceId");
558
715
  const context = await requireManagedSlackLinkHuman(c, deps);
559
- const payload = SlackUserLinkAccessMutationRequest.parse(await c.req.json());
716
+ const payload = await parseRequestJson(c, SlackUserLinkAccessMutationRequest);
560
717
  try {
561
718
  const request = await requestSlackUserLinkWorkspaceAccess(deps.db, {
562
719
  workspaceId,
@@ -582,7 +739,7 @@ export function registerSlackInteractionRoutes(app: Hono, deps: ApiRouteDeps): v
582
739
  async (c) => {
583
740
  const workspaceId = c.req.param("workspaceId");
584
741
  const context = await requireManagedSlackLinkHuman(c, deps);
585
- const payload = SlackUserLinkAccessMutationRequest.parse(await c.req.json());
742
+ const payload = await parseRequestJson(c, SlackUserLinkAccessMutationRequest);
586
743
  try {
587
744
  const request = await cancelSlackUserLinkAccessRequest(deps.db, {
588
745
  workspaceId,
@@ -623,15 +780,34 @@ export function registerSlackInteractionRoutes(app: Hono, deps: ApiRouteDeps): v
623
780
  async (c) => {
624
781
  const workspaceId = c.req.param("workspaceId");
625
782
  const grant = await requireAccessGrant(c, deps, workspaceId, "members:manage");
626
- const payload = ApproveSlackUserLinkAccessRequest.parse(await c.req.json());
783
+ const payload = await parseRequestJson(c, ApproveSlackUserLinkAccessRequest);
784
+ // The identity link belongs to the installation, which for a routed
785
+ // request is a different workspace from the one being granted. The
786
+ // request row names its Slack team, so the binding resolves from it.
787
+ const requestId = c.req.param("requestId");
788
+ const requestTeam = await slackUserLinkAccessRequestTeam(deps.db, {
789
+ workspaceId,
790
+ requestId,
791
+ });
792
+ const installation = requestTeam
793
+ ? await resolveSlackInstallationRoute(deps.db, requestTeam.slackTeamId)
794
+ : null;
627
795
  try {
628
796
  const request = await approveSlackUserLinkAccessRequest(deps.db, {
629
797
  workspaceId,
630
- requestId: c.req.param("requestId"),
798
+ requestId,
631
799
  actorSubjectId: grant.subjectId,
632
800
  expectedVersion: payload.expectedVersion,
633
801
  idempotencyKey: payload.idempotencyKey,
634
802
  permissions: payload.permissions,
803
+ ...(installation
804
+ ? {
805
+ identityHome: {
806
+ accountId: installation.accountId,
807
+ workspaceId: installation.workspaceId,
808
+ },
809
+ }
810
+ : {}),
635
811
  ...(payload.role !== undefined ? { role: payload.role } : {}),
636
812
  });
637
813
  const workspace = await getWorkspace(deps.db, workspaceId);
@@ -652,7 +828,7 @@ export function registerSlackInteractionRoutes(app: Hono, deps: ApiRouteDeps): v
652
828
  async (c) => {
653
829
  const workspaceId = c.req.param("workspaceId");
654
830
  const grant = await requireAccessGrant(c, deps, workspaceId, "members:manage");
655
- const payload = SlackUserLinkAccessMutationRequest.parse(await c.req.json());
831
+ const payload = await parseRequestJson(c, SlackUserLinkAccessMutationRequest);
656
832
  try {
657
833
  const request = await denySlackUserLinkAccessRequest(deps.db, {
658
834
  workspaceId,
@@ -690,8 +866,8 @@ export function registerSlackInteractionRoutes(app: Hono, deps: ApiRouteDeps): v
690
866
  message: "invalid or expired Slack identity link",
691
867
  });
692
868
  }
693
- const route = await resolveSlackInstallationRoute(deps.db, link.slackTeamId);
694
- if (!route || route.workspaceId !== workspaceId || route.connectionId !== link.connectionId) {
869
+ const installation = await resolveSlackLinkTargetInstallation(deps, link, workspaceId);
870
+ if (!installation) {
695
871
  throw new HTTPException(404, {
696
872
  message: "Slack installation not found",
697
873
  });
@@ -712,6 +888,7 @@ export function registerSlackInteractionRoutes(app: Hono, deps: ApiRouteDeps): v
712
888
  workspaceId,
713
889
  requestId: prepared.id,
714
890
  subjectId: grant.subjectId,
891
+ identityHome: installation.identityHome,
715
892
  });
716
893
  if (completed?.status !== "completed") throw freshSlackLinkRequired();
717
894
  const saved = await getSlackBotUserLink(
@@ -745,6 +922,135 @@ export function registerSlackInteractionRoutes(app: Hono, deps: ApiRouteDeps): v
745
922
  },
746
923
  );
747
924
 
925
+ app.get("/v1/workspaces/:workspaceId/integrations/slack/channel-routes", async (c) => {
926
+ const workspaceId = c.req.param("workspaceId");
927
+ // Same gate as the reaction-channel allowlist beside it. The response names
928
+ // workspaces across the organization, including ones the reader may hold no
929
+ // grant in, so it is not an ordinary connections read.
930
+ const grant = await requireAccessGrant(c, deps, workspaceId, "workspace:admin");
931
+ const connectionId = boundedString(c.req.query("connectionId"), 64);
932
+ if (!connectionId) throw new HTTPException(400, { message: "connectionId is required" });
933
+ const routes = await listSlackChannelRoutes(
934
+ deps.db,
935
+ { accountId: grant.accountId, workspaceId },
936
+ { connectionId },
937
+ );
938
+ // Names are resolved once per distinct target rather than per route.
939
+ const names = new Map<string, string | null>();
940
+ for (const route of routes) {
941
+ if (names.has(route.targetWorkspaceId)) continue;
942
+ names.set(
943
+ route.targetWorkspaceId,
944
+ (await getWorkspace(deps.db, route.targetWorkspaceId))?.name ?? null,
945
+ );
946
+ }
947
+ return c.json(
948
+ SlackChannelRouteListResponse.parse({
949
+ routingEnabled: deps.settings.slackWorkspaceRoutingEnabled,
950
+ routes: routes.map((route) => ({
951
+ slackChannelId: route.slackChannelId,
952
+ targetWorkspaceId: route.targetWorkspaceId,
953
+ targetWorkspaceName: names.get(route.targetWorkspaceId) ?? null,
954
+ source: route.source,
955
+ updatedAt: route.updatedAt.toISOString(),
956
+ })),
957
+ }),
958
+ );
959
+ });
960
+
961
+ app.put("/v1/workspaces/:workspaceId/integrations/slack/channel-routes", async (c) => {
962
+ const workspaceId = c.req.param("workspaceId");
963
+ // Same gate as the reaction-channel allowlist this sits beside: pointing a
964
+ // channel somewhere is an installation-administration act.
965
+ const grant = await requireAccessGrant(c, deps, workspaceId, "workspace:admin");
966
+ const payload = await parseRequestJson(c, UpdateSlackChannelRoutesRequest);
967
+ const home = { accountId: grant.accountId, workspaceId };
968
+ const installation = await listSlackInstallationBindings(deps.db, home);
969
+ const binding = installation.find(
970
+ (candidate) => candidate.connectionId === payload.connectionId,
971
+ );
972
+ // The connection must be this workspace's own installation, and the route
973
+ // rows carry its team id, so a missing binding is a refusal rather than a
974
+ // row written with an empty team the column's own bound would reject.
975
+ if (!binding) {
976
+ throw new HTTPException(404, { message: "Slack installation not found" });
977
+ }
978
+ // Authorize EVERY target before writing anything. Applying routes one at a
979
+ // time means a refusal partway through would otherwise leave some channels
980
+ // moved and the rest not, behind an error that says the save failed.
981
+ //
982
+ // An admin may only point a channel at a workspace they could start work in
983
+ // themselves, and only inside this organization. The table's own CHECK
984
+ // enforces the second half; this refuses before writing rather than
985
+ // surfacing a constraint error.
986
+ // A channel may never be pointed at a personal workspace. The routing
987
+ // resolver already refuses to treat one as a candidate in a channel, and
988
+ // this is the same rule at the write boundary, where a durable route would
989
+ // otherwise smuggle it back in: every message in that channel would land
990
+ // somewhere nobody else in the channel can see.
991
+ //
992
+ // Only the caller's OWN personal workspace has to be excluded, and that is
993
+ // the whole rule rather than a partial one, because no other person's can
994
+ // reach this loop. `resolveSlackTargetAuthority` admits exactly two shapes:
995
+ // a `workspace_memberships` row this subject holds, or this subject's own
996
+ // personal-workspace pointer. There is no account-admin union, so an
997
+ // administrator does not thereby reach anyone else's personal workspace.
998
+ //
999
+ // Deliberately NOT justified by "the schema forbids a membership row on a
1000
+ // personal workspace". It does not. Migration 0219's `42501` is a
1001
+ // precondition inside the provisioning function, checked at provisioning
1002
+ // time only, and `personal_workspace_has_no_membership_row` is recorded as
1003
+ // `basis: "runtime"` - the parity report's own term for a property nothing
1004
+ // in the schema prevents. It holds because every membership writer either
1005
+ // requires `members:manage` on the target (which a personal-workspace grant
1006
+ // deliberately omits) or excludes personal workspaces outright.
1007
+ const callerPersonalWorkspaceId = await namedSubjectPersonalWorkspaceId(deps.db, {
1008
+ accountId: grant.accountId,
1009
+ subjectId: grant.subjectId,
1010
+ });
1011
+ for (const route of payload.routes) {
1012
+ if (route.targetWorkspaceId === null) continue;
1013
+ const authorized = await resolveSlackTargetAuthority(deps.db, {
1014
+ subjectId: grant.subjectId,
1015
+ targetAccountId: grant.accountId,
1016
+ targetWorkspaceId: route.targetWorkspaceId,
1017
+ });
1018
+ if (!authorized || !hasPermission(authorized.permissions, "sessions:create")) {
1019
+ throw new HTTPException(403, {
1020
+ message: `you cannot start work in the workspace chosen for ${route.slackChannelId}`,
1021
+ });
1022
+ }
1023
+ if (
1024
+ callerPersonalWorkspaceId !== null &&
1025
+ route.targetWorkspaceId === callerPersonalWorkspaceId
1026
+ ) {
1027
+ throw new HTTPException(422, {
1028
+ message: `a channel cannot be routed to a personal workspace: work started in ${route.slackChannelId} would be invisible to everyone else in it`,
1029
+ });
1030
+ }
1031
+ }
1032
+ for (const route of payload.routes) {
1033
+ if (route.targetWorkspaceId === null) {
1034
+ await deleteSlackChannelRoute(deps.db, home, {
1035
+ connectionId: payload.connectionId,
1036
+ slackChannelId: route.slackChannelId,
1037
+ });
1038
+ continue;
1039
+ }
1040
+ await upsertSlackChannelRoute(deps.db, home, {
1041
+ connectionId: payload.connectionId,
1042
+ slackTeamId: binding.slackTeamId,
1043
+ slackChannelId: route.slackChannelId,
1044
+ targetAccountId: grant.accountId,
1045
+ targetWorkspaceId: route.targetWorkspaceId,
1046
+ decidedBySubjectId: grant.subjectId,
1047
+ decidedBySlackUserId: "admin",
1048
+ source: "admin",
1049
+ });
1050
+ }
1051
+ return c.json({ ok: true });
1052
+ });
1053
+
748
1054
  app.get("/v1/workspaces/:workspaceId/integrations/slack/reaction-channels", async (c) => {
749
1055
  const workspaceId = c.req.param("workspaceId");
750
1056
  const grant = await requireAccessGrant(c, deps, workspaceId, "workspace:admin");
@@ -783,7 +1089,302 @@ export function registerSlackInteractionRoutes(app: Hono, deps: ApiRouteDeps): v
783
1089
  });
784
1090
  }
785
1091
 
1092
+ async function withCatalogSettings(
1093
+ deps: ApiRouteDeps,
1094
+ grant: Pick<AccessGrant, "accountId" | "workspaceId">,
1095
+ ): Promise<ApiRouteDeps> {
1096
+ const catalogSourceSettings = deps.catalogSourceSettings ?? deps.settings;
1097
+ return {
1098
+ ...deps,
1099
+ catalogSourceSettings,
1100
+ settings: (
1101
+ await resolveWorkspaceCatalogSettings(deps.db, catalogSourceSettings, {
1102
+ accountId: grant.accountId,
1103
+ workspaceId: grant.workspaceId,
1104
+ })
1105
+ ).settings,
1106
+ };
1107
+ }
1108
+
1109
+ async function publishSlackAppHome(
1110
+ deps: ApiRouteDeps,
1111
+ installation: SlackInstallationRoute,
1112
+ refresh: SlackAppHomeRefresh,
1113
+ renewLease: () => Promise<void>,
1114
+ ): Promise<void> {
1115
+ const link = await getSlackBotUserLink(
1116
+ deps.db,
1117
+ installation.workspaceId,
1118
+ installation.connectionId,
1119
+ refresh.slackUserId,
1120
+ );
1121
+ const grant = link
1122
+ ? await getWorkspaceGrant(deps.db, link.subjectId, installation.workspaceId, {
1123
+ principalKind: "human_session",
1124
+ })
1125
+ : null;
1126
+ if (
1127
+ !grant ||
1128
+ grant.accountId !== installation.accountId ||
1129
+ !hasPermission(grant.permissions, "sessions:read")
1130
+ ) {
1131
+ const client = await createOpenGeniSlackBotInteractionClient(deps, {
1132
+ accountId: installation.accountId,
1133
+ workspaceId: installation.workspaceId,
1134
+ connectionId: installation.connectionId,
1135
+ subjectId: "service:slack-app-home",
1136
+ });
1137
+ await publishSlackAppHomeAccessView(
1138
+ client,
1139
+ refresh,
1140
+ renewLease,
1141
+ buildSlackAppHomeAccessBlocks({
1142
+ title: link ? "Opengeni access needed" : "Connect your Opengeni account",
1143
+ message: link
1144
+ ? "Your Slack identity is linked, but it does not currently have access to this Opengeni workspace."
1145
+ : "Link this Slack identity to see your active tasks, requests, and recent results here.",
1146
+ actionLabel: link ? "Request access" : "Connect Opengeni",
1147
+ actionUrl: slackAppHomeLinkUrl(
1148
+ deps,
1149
+ installation,
1150
+ refresh.slackTeamId,
1151
+ refresh.slackUserId,
1152
+ ),
1153
+ }),
1154
+ );
1155
+ return;
1156
+ }
1157
+
1158
+ const client = await createOpenGeniSlackBotInteractionClient(deps, {
1159
+ accountId: installation.accountId,
1160
+ workspaceId: installation.workspaceId,
1161
+ connectionId: installation.connectionId,
1162
+ subjectId: grant.subjectId,
1163
+ });
1164
+ // Host session authorization is resolved at publication time, before the
1165
+ // database query, so App Home cannot leak rows through a stale broad list.
1166
+ const authorizationScope = await requireSessionAuthorizationListScope(deps, grant, "core");
1167
+ const sessions: Awaited<ReturnType<typeof listSessionsForSubject>>["sessions"] = [];
1168
+ try {
1169
+ let cursor: ReturnType<typeof decodeSessionListCursor> | undefined;
1170
+ do {
1171
+ await renewLease();
1172
+ const page = await listSessionsForSubject(deps.db, installation.workspaceId, {
1173
+ subjectId: grant.subjectId,
1174
+ limit: 500,
1175
+ ...(cursor ? { cursor } : {}),
1176
+ ...(authorizationScope ? { authorizationScope } : {}),
1177
+ });
1178
+ if (!cursor) sessions.push(...page.pinned);
1179
+ sessions.push(...page.sessions);
1180
+ if (!page.nextCursor) break;
1181
+ cursor = decodeSessionListCursor(page.nextCursor) ?? undefined;
1182
+ if (!cursor) throw new Error("Slack App Home session cursor was invalid");
1183
+ } while (cursor);
1184
+ } catch (error) {
1185
+ if (!(error instanceof SessionListAccessError)) throw error;
1186
+ await publishSlackAppHomeAccessView(
1187
+ client,
1188
+ refresh,
1189
+ renewLease,
1190
+ buildSlackAppHomeAccessBlocks({
1191
+ title: "Opengeni access changed",
1192
+ message:
1193
+ "Your current Opengeni access could not be verified. Reconnect before tasks are shown here.",
1194
+ actionLabel: "Reconnect Opengeni",
1195
+ actionUrl: slackAppHomeLinkUrl(
1196
+ deps,
1197
+ installation,
1198
+ refresh.slackTeamId,
1199
+ refresh.slackUserId,
1200
+ ),
1201
+ }),
1202
+ );
1203
+ return;
1204
+ }
1205
+ const currentLink = await getSlackBotUserLink(
1206
+ deps.db,
1207
+ installation.workspaceId,
1208
+ installation.connectionId,
1209
+ refresh.slackUserId,
1210
+ );
1211
+ const currentGrant = currentLink
1212
+ ? await getWorkspaceGrant(deps.db, currentLink.subjectId, installation.workspaceId, {
1213
+ principalKind: "human_session",
1214
+ })
1215
+ : null;
1216
+ if (
1217
+ !currentLink ||
1218
+ currentLink.subjectId !== grant.subjectId ||
1219
+ !currentGrant ||
1220
+ currentGrant.accountId !== installation.accountId ||
1221
+ !hasPermission(currentGrant.permissions, "sessions:read")
1222
+ ) {
1223
+ await publishSlackAppHomeAccessView(
1224
+ client,
1225
+ refresh,
1226
+ renewLease,
1227
+ buildSlackAppHomeAccessBlocks({
1228
+ title: "Opengeni access changed",
1229
+ message:
1230
+ "Your current Opengeni access could not be verified. Reconnect before tasks are shown here.",
1231
+ actionLabel: "Reconnect Opengeni",
1232
+ actionUrl: slackAppHomeLinkUrl(
1233
+ deps,
1234
+ installation,
1235
+ refresh.slackTeamId,
1236
+ refresh.slackUserId,
1237
+ ),
1238
+ }),
1239
+ );
1240
+ return;
1241
+ }
1242
+ const currentAuthorizationScope = await requireSessionAuthorizationListScope(
1243
+ deps,
1244
+ currentGrant,
1245
+ "core",
1246
+ );
1247
+ if (
1248
+ slackSessionAuthorizationScopeKey(currentAuthorizationScope) !==
1249
+ slackSessionAuthorizationScopeKey(authorizationScope)
1250
+ ) {
1251
+ await publishSlackAppHomeAccessView(
1252
+ client,
1253
+ refresh,
1254
+ renewLease,
1255
+ buildSlackAppHomeAccessBlocks({
1256
+ title: "Opengeni access changed",
1257
+ message:
1258
+ "Your current task access changed while this view was loading. Reopen Home to refresh it safely.",
1259
+ actionLabel: "Open Opengeni",
1260
+ actionUrl: slackWorkspaceUrl(deps, installation.workspaceId),
1261
+ }),
1262
+ );
1263
+ return;
1264
+ }
1265
+ if (!refresh.providerViewHash) {
1266
+ await publishSlackAppHomeAccessView(
1267
+ client,
1268
+ refresh,
1269
+ renewLease,
1270
+ buildSlackAppHomeAccessBlocks({
1271
+ title: "Refresh Opengeni Home",
1272
+ message:
1273
+ "Reopen Home to refresh your tasks safely. Opengeni does not publish task data without Slack's current view version.",
1274
+ actionLabel: "Open Opengeni",
1275
+ actionUrl: slackWorkspaceUrl(deps, installation.workspaceId),
1276
+ }),
1277
+ );
1278
+ return;
1279
+ }
1280
+ await renewLease();
1281
+ await client.publishHomeView({
1282
+ userId: refresh.slackUserId,
1283
+ hash: refresh.providerViewHash,
1284
+ blocks: buildSlackAppHomeBlocks({
1285
+ sessions,
1286
+ workspaceUrl: slackWorkspaceUrl(deps, installation.workspaceId),
1287
+ sessionUrl: (sessionId) => slackSessionUrl(deps, installation.workspaceId, sessionId),
1288
+ }),
1289
+ });
1290
+ }
1291
+
1292
+ async function publishSlackAppHomeAccessView(
1293
+ client: OpenGeniSlackBotClient,
1294
+ refresh: SlackAppHomeRefresh,
1295
+ renewLease: () => Promise<void>,
1296
+ blocks: ReturnType<typeof buildSlackAppHomeAccessBlocks>,
1297
+ ): Promise<void> {
1298
+ try {
1299
+ await renewLease();
1300
+ await client.publishHomeView({
1301
+ userId: refresh.slackUserId,
1302
+ hash: refresh.providerViewHash,
1303
+ blocks,
1304
+ });
1305
+ } catch (error) {
1306
+ if (!(error instanceof SlackBotProviderError) || error.code !== "hash_conflict") throw error;
1307
+ // Access views contain no task data. If an older authorized publication
1308
+ // advanced Slack's optimistic-concurrency hash, replace it fail-closed
1309
+ // rather than dropping the newer clearing obligation.
1310
+ await renewLease();
1311
+ await client.publishHomeView({
1312
+ userId: refresh.slackUserId,
1313
+ blocks,
1314
+ });
1315
+ }
1316
+ }
1317
+
786
1318
  export async function drainSlackInteractionsOnce(deps: ApiRouteDeps): Promise<boolean> {
1319
+ const appHomeHolder = crypto.randomUUID();
1320
+ const refresh = await claimSlackAppHomeRefresh(deps.db, appHomeHolder, APP_HOME_LEASE_MS);
1321
+ if (refresh) {
1322
+ try {
1323
+ const installation = await resolveSlackInstallationRoute(deps.db, refresh.slackTeamId);
1324
+ if (
1325
+ !installation ||
1326
+ installation.accountId !== refresh.accountId ||
1327
+ installation.workspaceId !== refresh.workspaceId ||
1328
+ installation.connectionId !== refresh.connectionId
1329
+ ) {
1330
+ throw new SlackInteractionPermanentError("slack_app_home_installation_changed");
1331
+ }
1332
+ const renewLease = async () => {
1333
+ if (
1334
+ !(await renewSlackAppHomeRefreshClaim(deps.db, {
1335
+ refresh,
1336
+ claimHolderId: appHomeHolder,
1337
+ claimLeaseMs: APP_HOME_LEASE_MS,
1338
+ }))
1339
+ ) {
1340
+ throw new SlackInteractionPermanentError("slack_app_home_claim_lost");
1341
+ }
1342
+ };
1343
+ await renewLease();
1344
+ await publishSlackAppHome(deps, installation, refresh, renewLease);
1345
+ await settleSlackAppHomeRefresh(deps.db, {
1346
+ refresh,
1347
+ claimHolderId: appHomeHolder,
1348
+ });
1349
+ } catch (error) {
1350
+ const code = safeErrorCode(error);
1351
+ console.error("[slack-interactions] App Home refresh failed", {
1352
+ workspaceId: refresh.workspaceId,
1353
+ connectionId: refresh.connectionId,
1354
+ slackUserId: refresh.slackUserId,
1355
+ providerEventId: refresh.providerEventId,
1356
+ desiredRevision: refresh.desiredRevision,
1357
+ attemptCount: refresh.attemptCount,
1358
+ errorCode: code,
1359
+ });
1360
+ if (error instanceof SlackBotProviderError && error.code === "hash_conflict") {
1361
+ await settleSlackAppHomeRefresh(deps.db, {
1362
+ refresh,
1363
+ claimHolderId: appHomeHolder,
1364
+ errorCode: code,
1365
+ });
1366
+ } else if (
1367
+ refresh.attemptCount >= 5 ||
1368
+ error instanceof SlackInteractionPermanentError ||
1369
+ permanentSlackDeliveryError(error)
1370
+ ) {
1371
+ await settleSlackAppHomeRefresh(deps.db, {
1372
+ refresh,
1373
+ claimHolderId: appHomeHolder,
1374
+ errorCode: code,
1375
+ });
1376
+ } else {
1377
+ await releaseSlackAppHomeRefresh(deps.db, {
1378
+ refresh,
1379
+ claimHolderId: appHomeHolder,
1380
+ errorCode: code,
1381
+ retryAt: new Date(Date.now() + slackDeliveryRetryMs(error, refresh.attemptCount)),
1382
+ });
1383
+ }
1384
+ }
1385
+ return true;
1386
+ }
1387
+
787
1388
  const holder = crypto.randomUUID();
788
1389
  const entry = await claimSlackInteractionInbox(deps.db, holder, INBOX_LEASE_MS);
789
1390
  if (entry) {
@@ -890,85 +1491,786 @@ export function startSlackInteractionPump(
890
1491
  };
891
1492
  }
892
1493
 
893
- async function processSlackInboxEntry(deps: ApiRouteDeps, entry: SlackInteractionInboxEntry) {
894
- if (entry.triggerKind === "block_action") {
895
- await processSlackBlockAction(deps, entry);
896
- return;
897
- }
898
- if (entry.triggerKind === "reaction") {
899
- await processSlackReactionInboxEntry(deps, entry);
900
- return;
901
- }
902
- const installation = await resolveSlackInstallationRoute(deps.db, entry.slackTeamId);
1494
+ /**
1495
+ * A concurrent creator may have won this thread in another workspace between the
1496
+ * tenancy probe and the insert, in which case `getOrCreateSlackInteraction`
1497
+ * adopts that row. The grant, the imported attachments, and the resolved route
1498
+ * all belong to the workspace this pass chose, so none of them may be used
1499
+ * against the adopted row. Retry instead: the next pass sees the thread through
1500
+ * the probe, routes to it, and authorizes it properly.
1501
+ */
1502
+ function requireSlackInteractionMatchesTarget(
1503
+ interaction: Pick<SlackInteraction, "accountId" | "workspaceId">,
1504
+ target: { accountId: string; workspaceId: string },
1505
+ ): void {
903
1506
  if (
904
- !installation ||
905
- installation.accountId !== entry.accountId ||
906
- installation.workspaceId !== entry.workspaceId ||
907
- installation.connectionId !== entry.connectionId
1507
+ interaction.accountId !== target.accountId ||
1508
+ interaction.workspaceId !== target.workspaceId
908
1509
  ) {
909
- throw new SlackInteractionPermanentError("slack_task_installation_changed");
910
- }
911
- const client = await createOpenGeniSlackBotInteractionClient(deps, {
912
- accountId: entry.accountId,
913
- workspaceId: entry.workspaceId,
914
- connectionId: entry.connectionId,
915
- subjectId: SLACK_INTERACTION_BOT_SUBJECT_ID,
916
- });
917
- const policyResolution = isDirectMessageShortcut(entry)
918
- ? ({
919
- decision: {
920
- disposition: "ordinary",
921
- publication: "allow",
922
- reason: "ordinary_conversation",
923
- } as const,
924
- activePolicy: null,
925
- } as const)
926
- : await slackTaskPolicyDecision(deps, client, entry);
927
- const policyDecision = policyResolution.decision;
928
- if (policyDecision.disposition === "deny") {
929
- await client.postMessage({
930
- operationId: deterministicUuid(`slack-policy-denied:${entry.id}`),
931
- userId: entry.slackUserId,
932
- text: "OpenGeni cannot start a task from this shared conversation under the current workspace policy. No conversation content was read or retained.",
933
- });
934
- return;
1510
+ throw new SlackInteractionRetryableError("slack_route_creation_pending");
935
1511
  }
936
- const routePolicy = slackInteractionRoutePolicy(entry, {
937
- privateHandoff: policyDecision.disposition === "private_handoff",
938
- });
939
- const routeKey = routePolicy.initialRouteKey;
940
- const existing = await getSlackInteractionByRoute(
941
- deps.db,
942
- entry.workspaceId,
943
- entry.connectionId,
944
- routeKey,
945
- );
946
- if (entry.triggerKind === "thread_reply" && !existing) return;
1512
+ }
947
1513
 
948
- const link = await getSlackBotUserLink(
949
- deps.db,
950
- entry.workspaceId,
951
- entry.connectionId,
952
- entry.slackUserId,
953
- );
954
- if (!link) {
955
- await client.postMessage({
956
- operationId: deterministicUuid(`slack-link:${entry.id}`),
957
- userId: entry.slackUserId,
958
- text: `Link your Slack identity to OpenGeni before starting work: ${linkUrl(deps, entry)}. No session was created.`,
959
- });
960
- return;
1514
+ /**
1515
+ * Gather the durable facts the routing decision needs, then decide.
1516
+ *
1517
+ * With the flag off nothing is read at all: the resolver short-circuits to the
1518
+ * installation's own workspace, so an existing single-workspace install issues
1519
+ * exactly the queries it issued before.
1520
+ */
1521
+ async function resolveSlackRouteForEntry(
1522
+ deps: ApiRouteDeps,
1523
+ home: SlackRouteTenancy,
1524
+ entry: SlackInteractionInboxEntry,
1525
+ subjectId: string,
1526
+ options: {
1527
+ threadTenancy: SlackRouteTenancy | null;
1528
+ askEnabled: boolean;
1529
+ botUserId: string | null;
1530
+ },
1531
+ ): Promise<SlackRouteResolution> {
1532
+ const base = {
1533
+ home,
1534
+ entry,
1535
+ botUserId: options.botUserId,
1536
+ threadTenancy: options.threadTenancy,
1537
+ channelRoute: null,
1538
+ dmRoute: null,
1539
+ personalWorkspaceId: null,
1540
+ candidates: [] as const,
1541
+ routingEnabled: false,
1542
+ askEnabled: false,
1543
+ } as const;
1544
+ // A mapped thread wins outright, and with the flag off nothing is consulted at
1545
+ // all, so neither case reads anything.
1546
+ if (!deps.settings.slackWorkspaceRoutingEnabled || options.threadTenancy) {
1547
+ return resolveSlackWorkspaceRoute(base);
961
1548
  }
962
- const grant = await getWorkspaceGrant(deps.db, link.subjectId, entry.workspaceId, {
963
- principalKind: "human_session",
1549
+ const directMessage = isSlackDirectMessageConversation(entry);
1550
+ const [channelRoute, dmRoute, candidates] = await Promise.all([
1551
+ directMessage
1552
+ ? Promise.resolve(null)
1553
+ : getSlackChannelRoute(deps.db, home, {
1554
+ connectionId: entry.connectionId,
1555
+ slackChannelId: entry.slackChannelId,
1556
+ }),
1557
+ directMessage
1558
+ ? getSlackUserDmRoute(deps.db, home, {
1559
+ connectionId: entry.connectionId,
1560
+ slackUserId: entry.slackUserId,
1561
+ })
1562
+ : Promise.resolve(null),
1563
+ listNamedSubjectSlackRoutableWorkspaces(deps.db, { accountId: home.accountId, subjectId }),
1564
+ ]);
1565
+ return resolveSlackWorkspaceRoute({
1566
+ ...base,
1567
+ channelRoute,
1568
+ dmRoute,
1569
+ candidates,
1570
+ // The candidate set is built from the same derived pointer, so reading it
1571
+ // again would be a second query for an answer already in hand.
1572
+ personalWorkspaceId: candidates.find((candidate) => candidate.personal)?.workspaceId ?? null,
1573
+ routingEnabled: true,
1574
+ askEnabled: options.askEnabled,
964
1575
  });
965
- if (!grant || grant.accountId !== entry.accountId) {
966
- throw new SlackInteractionPermanentError("identity_access_revoked");
967
- }
1576
+ }
1577
+
1578
+ /**
1579
+ * Tell the person exactly why nothing started, in their own bot DM.
1580
+ *
1581
+ * Every branch ends with "No session was created." because the one thing a
1582
+ * refusal must never do is leave someone believing work is under way.
1583
+ *
1584
+ * Only `no_access_to_route` mints an access-request link, and it mints one for
1585
+ * the workspace they actually need rather than the installation's. The other
1586
+ * two branches deliberately do not: a prefix naming something they cannot
1587
+ * reach, and an empty candidate list, both describe a workspace OpenGeni has
1588
+ * not established it will confirm to this person, so they point at an
1589
+ * administrator instead.
1590
+ */
1591
+ async function postSlackRouteRefusal(
1592
+ deps: ApiRouteDeps,
1593
+ client: OpenGeniSlackBotClient,
1594
+ entry: SlackInteractionInboxEntry,
1595
+ refusal: Extract<SlackRouteResolution, { kind: "denied" }> & Partial<SlackRouteTenancy>,
1596
+ ): Promise<void> {
1597
+ const available = refusal.candidates.map((candidate) => candidate.label);
1598
+ const text =
1599
+ refusal.reason === "no_access_to_named"
1600
+ ? `OpenGeni does not see a workspace named ${JSON.stringify(refusal.requested ?? "")} that you can start work in from here.${
1601
+ available.length > 0 ? ` You can use: ${available.join(", ")}.` : ""
1602
+ } No session was created.`
1603
+ : refusal.reason === "no_access_to_route"
1604
+ ? `OpenGeni starts work from this conversation in ${
1605
+ refusal.requested ?? "another workspace"
1606
+ }, and you do not have access to it. Request access: ${linkUrl(
1607
+ deps,
1608
+ {
1609
+ // Minted for the workspace they actually need, not the
1610
+ // installation's. The token only decides which workspace an
1611
+ // access request may be raised for; approval is unchanged.
1612
+ workspaceId: refusal.workspaceId ?? entry.workspaceId,
1613
+ connectionId: entry.connectionId,
1614
+ slackTeamId: entry.slackTeamId,
1615
+ slackUserId: entry.slackUserId,
1616
+ },
1617
+ entry.createdAt.getTime(),
1618
+ )}. No session was created.`
1619
+ : // Deliberately "from this conversation" rather than "for you": in a
1620
+ // channel this also fires for someone whose only workspace is their
1621
+ // own personal one, and telling that person they have no workspace
1622
+ // at all would be false. Their bot DM still works.
1623
+ "OpenGeni has no workspace it can start this task in from this conversation. Ask an OpenGeni administrator to give you access to one, or message OpenGeni directly to work in your own workspace. No session was created.";
1624
+ await client.postMessage({
1625
+ operationId: deterministicUuid(`slack-route-denied:${entry.id}:${refusal.reason}`),
1626
+ userId: entry.slackUserId,
1627
+ text: boundedOutput(text),
1628
+ });
1629
+ }
1630
+
1631
+ /**
1632
+ * A workspace name as a Slack button label.
1633
+ *
1634
+ * Slack renders a button label as plain text and the stored option snapshot caps
1635
+ * it at 128 bytes, so bound it once at the boundary: the button, the row and
1636
+ * every outcome card then agree. Counted in UTF-8 bytes, because that is what
1637
+ * the CHECK counts.
1638
+ */
1639
+ function boundedSlackRouteLabel(label: string): string {
1640
+ const encoder = new TextEncoder();
1641
+ if (encoder.encode(label).length <= 72) return label;
1642
+ let bounded = label;
1643
+ while (bounded.length > 0 && encoder.encode(`${bounded}...`).length > 72) {
1644
+ bounded = bounded.slice(0, -1);
1645
+ }
1646
+ return `${bounded}...`;
1647
+ }
1648
+
1649
+ /**
1650
+ * Say where the work went, quietly.
1651
+ *
1652
+ * The line exists to stop work landing somewhere surprising, so it appears only
1653
+ * when the routing decision was a genuine choice: a null label means routing is
1654
+ * off, or the person had exactly one workspace anyway, and a constant footer on
1655
+ * every message would be noise rather than information.
1656
+ *
1657
+ * The label is frozen on the interaction rather than looked up here, because a
1658
+ * workspace rename between the original post and a reconciliation would make the
1659
+ * post ledger's byte comparison raise.
1660
+ */
1661
+ export function withWorkspaceLine(
1662
+ label: string | null,
1663
+ text: string,
1664
+ blocks?: SlackMessageBlock[],
1665
+ ): { text: string; blocks?: SlackMessageBlock[] } {
1666
+ if (!label) return blocks ? { text, blocks } : { text };
1667
+ const suffix = `\n-> ${label}`;
1668
+ // Reserve the suffix rather than truncating after the fact, so the bounded
1669
+ // body plus the line still fits.
1670
+ const body = boundedOutput(text, MAX_SLACK_TEXT_CHARS - suffix.length);
1671
+ const line: SlackMessageBlock = {
1672
+ type: "context",
1673
+ elements: [{ type: "mrkdwn", text: `-> ${label}` }],
1674
+ };
1675
+ return {
1676
+ text: `${body}${suffix}`,
1677
+ ...(blocks ? { blocks: [...blocks, line] } : {}),
1678
+ };
1679
+ }
1680
+
1681
+ /**
1682
+ * The post-ledger seed for one interaction's messages.
1683
+ *
1684
+ * The ledger binds one operation id to a digest over the rendered bytes, so
1685
+ * adding the workspace line under an unchanged id would wedge every interaction
1686
+ * with a claimed-but-unposted row. But bumping the id for interactions whose
1687
+ * bytes did NOT change is not free either: their ledger rows become unreachable,
1688
+ * so a repair or a cross-deploy retry posts a duplicate instead of finding the
1689
+ * completed original, and an action handle still points at the old operation.
1690
+ *
1691
+ * Only a labelled interaction renders different bytes, and the label is written
1692
+ * once at insert and never updated, so it is a stable discriminator. Version the
1693
+ * id on the label rather than on the release: a labelled interaction can only
1694
+ * have been created by an image that already renders the line, and every
1695
+ * pre-existing and unlabelled interaction keeps its exact ledger identity.
1696
+ *
1697
+ * A compact interaction (one with a frozen `startMessageLine`) names its
1698
+ * workspace once, on its first message, and nowhere else. For a labelled one
1699
+ * that is again different bytes from what an older image renders for the same
1700
+ * message, so its ids move once more, to `:v3`. Unlabelled interactions render
1701
+ * the same bytes in both formats and keep their ids.
1702
+ */
1703
+ export function slackPostSeed(
1704
+ interaction: Pick<SlackInteraction, "routedWorkspaceLabel" | "startMessageLine">,
1705
+ seed: string,
1706
+ ): string {
1707
+ if (!interaction.routedWorkspaceLabel) return seed;
1708
+ return interaction.startMessageLine === null ? `${seed}:v2` : `${seed}:v3`;
1709
+ }
1710
+
1711
+ /**
1712
+ * The workspace line a message other than the first one carries.
1713
+ *
1714
+ * Only the previous format repeats it. A compact task states its workspace in
1715
+ * the first message's opening sentence, and every later task message is posted
1716
+ * in that message's thread, so repeating it would be noise.
1717
+ */
1718
+ function slackMessageWorkspaceLabel(
1719
+ interaction: Pick<SlackInteraction, "routedWorkspaceLabel" | "startMessageLine">,
1720
+ ): string | null {
1721
+ return interaction.startMessageLine === null ? interaction.routedWorkspaceLabel : null;
1722
+ }
1723
+
1724
+ /**
1725
+ * The acknowledgement seed for interactions that already carry a frozen
1726
+ * "Using" line from before new acknowledgements stopped including it.
1727
+ *
1728
+ * The old line was written once at session bind and never changes, so a
1729
+ * historical retry must keep the same ledger identity and bytes. New
1730
+ * interactions bind no line and use the original acknowledgement seed.
1731
+ */
1732
+ export function slackDefaultsLinePostSeed(
1733
+ interaction: Pick<SlackInteraction, "sessionDefaultsLine">,
1734
+ seed: string,
1735
+ ): string {
1736
+ return interaction.sessionDefaultsLine ? `${seed}:defaults` : seed;
1737
+ }
1738
+
1739
+ /**
1740
+ * The post operation of an interaction's first message.
1741
+ *
1742
+ * A compact first message shares no bytes with anything an older image renders
1743
+ * for the same interaction, so it has its own seed: an older image repairing it
1744
+ * during a rolling deploy posts a second message rather than wedging on a digest
1745
+ * conflict. Every handle on the first message carries this id.
1746
+ */
1747
+ export function slackAcknowledgementOperationId(
1748
+ interaction: Pick<
1749
+ SlackInteraction,
1750
+ "id" | "routedWorkspaceLabel" | "sessionDefaultsLine" | "startMessageLine"
1751
+ >,
1752
+ ): string {
1753
+ if (interaction.startMessageLine !== null) {
1754
+ return deterministicUuid(`slack-ack:${interaction.id}:start`);
1755
+ }
1756
+ return deterministicUuid(
1757
+ slackDefaultsLinePostSeed(
1758
+ interaction,
1759
+ slackPostSeed(interaction, `slack-ack:${interaction.id}`),
1760
+ ),
1761
+ );
1762
+ }
1763
+
1764
+ /** Where a compact task's first message says the task came from. */
1765
+ export type SlackStartMessageOrigin =
1766
+ | { kind: "task" }
1767
+ | { kind: "private_dm_message" }
1768
+ | { kind: "private_conversation" }
1769
+ | { kind: "reaction"; emoji: string };
1770
+
1771
+ /**
1772
+ * The opening sentence of a new task's first Slack message.
1773
+ *
1774
+ * The sentence itself links to the session, and a routed workspace is named
1775
+ * inline, here and on no other message. The private variants keep their privacy
1776
+ * sentence, and the reaction variant keeps its how-to, because a reaction-started
1777
+ * task has no button and never shows the one-time hint.
1778
+ *
1779
+ * Rendered once when the session binds and frozen as `startMessageLine`, so a
1780
+ * later change to the web base URL or the workspace name cannot change the bytes
1781
+ * of a message the post and update ledgers already hold.
1782
+ */
1783
+ export function renderSlackStartMessageLine(input: {
1784
+ sessionUrl: string;
1785
+ routedWorkspaceLabel: string | null;
1786
+ origin: SlackStartMessageOrigin;
1787
+ }): string {
1788
+ const where = input.routedWorkspaceLabel
1789
+ ? ` in *${escapeSlackMrkdwn(input.routedWorkspaceLabel)}*`
1790
+ : "";
1791
+ switch (input.origin.kind) {
1792
+ case "private_dm_message":
1793
+ return `<${input.sessionUrl}|Opengeni started a private task>${where} from the selected DM message. The source DM was not opened to the bot or made workspace-visible.`;
1794
+ case "private_conversation":
1795
+ return `<${input.sessionUrl}|Opengeni started a private task>${where} from the selected Slack conversation. Results stay private unless a separate authorized publication is approved.`;
1796
+ case "reaction":
1797
+ return `<${input.sessionUrl}|Opengeni started this task>${where} from the :${input.origin.emoji}: reaction. If the request is unclear, Opengeni will ask in this thread. Reply here to continue, or reply \`stop\` to stop.`;
1798
+ case "task":
1799
+ return `<${input.sessionUrl}|Opengeni started this task>${where}.`;
1800
+ }
1801
+ }
1802
+
1803
+ /**
1804
+ * The start line to freeze for a mention, command, DM, or shortcut task, or
1805
+ * null to keep the previous format. Null only when the deployment has no
1806
+ * absolute web base URL, which the acknowledgement then reports exactly as it
1807
+ * always did.
1808
+ */
1809
+ function slackStartMessageLineForEntry(
1810
+ deps: ApiRouteDeps,
1811
+ interaction: Pick<SlackInteraction, "workspaceId" | "visibility" | "routedWorkspaceLabel">,
1812
+ sessionId: string,
1813
+ entry: Pick<SlackInteractionInboxEntry, "triggerKind" | "slackChannelId">,
1814
+ ): string | null {
1815
+ const sessionUrl = slackAcknowledgementSessionUrl(deps, interaction.workspaceId, sessionId);
1816
+ if (!sessionUrl) return null;
1817
+ const directMessageShortcut = isDirectMessageShortcut(entry);
1818
+ return renderSlackStartMessageLine({
1819
+ sessionUrl,
1820
+ routedWorkspaceLabel: interaction.routedWorkspaceLabel,
1821
+ origin: directMessageShortcut
1822
+ ? { kind: "private_dm_message" }
1823
+ : interaction.visibility === "private" && entry.triggerKind !== "dm"
1824
+ ? { kind: "private_conversation" }
1825
+ : { kind: "task" },
1826
+ });
1827
+ }
1828
+
1829
+ /** The start line to freeze for a reaction-started task, or null (see above). */
1830
+ function slackStartMessageLineForReaction(
1831
+ deps: ApiRouteDeps,
1832
+ interaction: Pick<SlackInteraction, "workspaceId" | "routedWorkspaceLabel">,
1833
+ sessionId: string,
1834
+ emoji: string,
1835
+ ): string | null {
1836
+ const sessionUrl = slackAcknowledgementSessionUrl(deps, interaction.workspaceId, sessionId);
1837
+ if (!sessionUrl) return null;
1838
+ return renderSlackStartMessageLine({
1839
+ sessionUrl,
1840
+ routedWorkspaceLabel: interaction.routedWorkspaceLabel,
1841
+ origin: { kind: "reaction", emoji },
1842
+ });
1843
+ }
1844
+
1845
+ /**
1846
+ * A compact task's first message: the frozen opening sentence, a note once the
1847
+ * requester stopped it, the one-time hint when this interaction won it, and at
1848
+ * most one button (Stop while running, Resume once stopped, none once settled).
1849
+ *
1850
+ * A pure function of frozen facts and the handle it is given, so a repair, a
1851
+ * retried click, and the settle update each reproduce their exact bytes. The
1852
+ * button is rendered from the handle whatever its status: a repair after the
1853
+ * button was pressed must still match the bytes the post ledger holds.
1854
+ */
1855
+ function slackStartMessage(
1856
+ deps: ApiRouteDeps,
1857
+ startMessageLine: string,
1858
+ options: {
1859
+ hint: boolean;
1860
+ stopped: boolean;
1861
+ button: SlackInteractionActionHandle | null;
1862
+ },
1863
+ ): { text: string; blocks: SlackMessageBlock[] } {
1864
+ const text = `${startMessageLine}${options.stopped ? "\nStopped." : ""}${
1865
+ options.hint ? slackStartMessageFirstTaskHintText(deps) : ""
1866
+ }`;
1867
+ const button = options.button;
1868
+ return {
1869
+ text,
1870
+ blocks: [
1871
+ { type: "section", text: { type: "mrkdwn", text } },
1872
+ ...(button
1873
+ ? [
1874
+ {
1875
+ type: "actions" as const,
1876
+ block_id: `opengeni_start_${button.messageOperationId}`,
1877
+ elements: [
1878
+ {
1879
+ type: "button" as const,
1880
+ action_id: SLACK_ACTION_ID_BY_KIND[button.actionKind],
1881
+ value: button.id,
1882
+ text: {
1883
+ type: "plain_text" as const,
1884
+ text: button.actionKind === "session_resume" ? "Resume" : "Stop",
1885
+ emoji: true,
1886
+ },
1887
+ ...(button.actionKind === "session_pause" ? { style: "danger" as const } : {}),
1888
+ },
1889
+ ],
1890
+ },
1891
+ ]
1892
+ : []),
1893
+ ] as SlackMessageBlock[],
1894
+ };
1895
+ }
1896
+
1897
+ /** Sources that mean somebody, or something, actually chose this workspace. */
1898
+ const SLACK_ROUTE_SOURCES_WORTH_NAMING: ReadonlySet<string> = new Set([
1899
+ // Not "thread": a mapped thread reuses the interaction that already exists,
1900
+ // so it inherits the label frozen when that interaction was created and never
1901
+ // reaches the writer below.
1902
+ "prefix",
1903
+ "channel",
1904
+ "dm_route",
1905
+ "dm_personal",
1906
+ ]);
1907
+
1908
+ /**
1909
+ * The label to freeze on a new interaction, or null to say nothing.
1910
+ *
1911
+ * `installation` and `sole_candidate` both mean there was only ever one place
1912
+ * this could go, so naming it on every message would be noise.
1913
+ *
1914
+ * Bounded with the picker's own button cap so the frozen label and the button
1915
+ * that produced it agree; the column allows 128 bytes, which is headroom.
1916
+ */
1917
+ async function routedWorkspaceLabelFor(
1918
+ deps: ApiRouteDeps,
1919
+ resolution: Extract<SlackRouteResolution, { kind: "resolved" }>,
1920
+ ): Promise<string | null> {
1921
+ if (!SLACK_ROUTE_SOURCES_WORTH_NAMING.has(resolution.source)) return null;
1922
+ const label = resolution.label ?? (await getWorkspace(deps.db, resolution.workspaceId))?.name;
1923
+ if (!label) return null;
1924
+ const bounded = boundedSlackRouteLabel(label);
1925
+ return bounded.length > 0 ? bounded : null;
1926
+ }
1927
+
1928
+ /** How long a first-use picker stays answerable. */
1929
+ const SLACK_ROUTE_PROMPT_TTL_MS = 24 * 60 * 60 * 1000;
1930
+
1931
+ /** Buttons a card can carry before it stops being readable. */
1932
+ const MAX_SLACK_ROUTE_PROMPT_BUTTONS = 5;
1933
+
1934
+ /**
1935
+ * Ask, once, where this conversation should work.
1936
+ *
1937
+ * The card is a bot DM rather than an ephemeral message: `chat.postEphemeral`
1938
+ * returns no durable `(channel, ts)` and no `client_msg_id` echo, and the whole
1939
+ * post ledger and its reconciliation are built on both.
1940
+ *
1941
+ * Nothing is created in any workspace here. The request is parked on the prompt
1942
+ * row and re-queued only once the person answers, so a picker that is never
1943
+ * answered leaves no session, no interaction and no half-started work.
1944
+ */
1945
+ async function askSlackRouteChoice(
1946
+ deps: ApiRouteDeps,
1947
+ client: OpenGeniSlackBotClient,
1948
+ home: SlackRouteTenancy,
1949
+ entry: SlackInteractionInboxEntry,
1950
+ candidates: readonly SlackRoutableWorkspace[],
1951
+ ): Promise<void> {
1952
+ const offered = candidates.slice(0, MAX_SLACK_ROUTE_PROMPT_BUTTONS);
1953
+ // Slack renders a button label as plain text and the stored snapshot caps it
1954
+ // at 128 bytes, so bound it once here: the button, the stored row and every
1955
+ // outcome card then agree, and the full name still shows in OpenGeni.
1956
+ const bounded = offered.map((candidate) => ({
1957
+ ...candidate,
1958
+ label: boundedSlackRouteLabel(candidate.label),
1959
+ }));
1960
+ const opened = await openSlackRoutePrompt(deps.db, home, {
1961
+ connectionId: entry.connectionId,
1962
+ inboxId: entry.id,
1963
+ slackTeamId: entry.slackTeamId,
1964
+ slackUserId: entry.slackUserId,
1965
+ slackChannelId: entry.slackChannelId,
1966
+ slackMessageTs: entry.slackMessageTs,
1967
+ providerEventId: entry.providerEventId,
1968
+ triggerKind: entry.triggerKind,
1969
+ requestText: entry.text,
1970
+ hasFiles: entry.hasFiles,
1971
+ slackThreadTs: entry.slackThreadTs,
1972
+ messageOperationId: deterministicUuid(`slack-route-prompt:${entry.id}`),
1973
+ expiresAt: new Date(Date.now() + SLACK_ROUTE_PROMPT_TTL_MS),
1974
+ candidates: bounded,
1975
+ });
1976
+ if (!opened) return;
1977
+ // The prompt commits before the card is posted, so a transient Slack failure
1978
+ // would otherwise leave a pending row with no card in front of anybody and
1979
+ // silently deaden the conversation. Post unconditionally instead: the
1980
+ // operation id is persisted on the prompt and the card is rendered from the
1981
+ // prompt's own stored options, so a replay reproduces identical bytes and the
1982
+ // post ledger returns the completed original rather than posting twice.
1983
+ const where = opened.prompt.slackChannelId.startsWith("D")
1984
+ ? "your messages with OpenGeni"
1985
+ : `<#${opened.prompt.slackChannelId}>`;
1986
+ const blocks: SlackMessageBlock[] = [
1987
+ {
1988
+ type: "section",
1989
+ text: {
1990
+ type: "mrkdwn",
1991
+ text: `OpenGeni can start this task in more than one workspace. Pick where ${where} should work. This choice is remembered.`,
1992
+ },
1993
+ },
1994
+ {
1995
+ type: "actions",
1996
+ block_id: "opengeni-route-choice",
1997
+ elements: [
1998
+ ...opened.prompt.options.map((option) => ({
1999
+ type: "button" as const,
2000
+ action_id: "opengeni.route.select",
2001
+ value: option.id,
2002
+ text: { type: "plain_text" as const, text: option.candidateLabel },
2003
+ })),
2004
+ {
2005
+ type: "button" as const,
2006
+ action_id: "opengeni.route.cancel",
2007
+ value: opened.prompt.options[0]?.id ?? opened.prompt.id,
2008
+ text: { type: "plain_text" as const, text: "Not now" },
2009
+ },
2010
+ ],
2011
+ },
2012
+ ];
2013
+ // Deliberately unconditional rather than derived from the live candidate
2014
+ // count: a replay must render identical bytes, and membership can change
2015
+ // between attempts.
2016
+ const url = slackWorkspaceUrl(deps, home.workspaceId);
2017
+ blocks.push({
2018
+ type: "section",
2019
+ text: {
2020
+ type: "mrkdwn",
2021
+ text: url
2022
+ ? `You can change this later in <${url}|OpenGeni>, under Capabilities, then Slack.`
2023
+ : "You can change this later in OpenGeni, under Capabilities, then Slack.",
2024
+ },
2025
+ });
2026
+ await client.postMessage({
2027
+ operationId: opened.prompt.messageOperationId,
2028
+ userId: opened.prompt.slackUserId,
2029
+ text: boundedOutput(
2030
+ `OpenGeni can start this task in more than one workspace. Pick where ${where} should work.`,
2031
+ ),
2032
+ blocks,
2033
+ });
2034
+ }
2035
+
2036
+ /**
2037
+ * Process one click on a picker.
2038
+ *
2039
+ * The stored option row is a prompt-time snapshot and is NEVER authority: the
2040
+ * chosen workspace is re-authorized live, because access can have been revoked
2041
+ * between the question and the answer.
2042
+ */
2043
+ async function handleSlackRouteChoice(
2044
+ deps: ApiRouteDeps,
2045
+ home: SlackRouteTenancy,
2046
+ entry: SlackInteractionInboxEntry,
2047
+ actionId: string,
2048
+ optionId: string,
2049
+ ): Promise<void> {
2050
+ if (!deps.settings.slackWorkspaceRoutingEnabled) {
2051
+ // A card can outlive the flag being turned back off. Honouring the click
2052
+ // would write a route row and start work through a path the operator has
2053
+ // disabled.
2054
+ throw new SlackInteractionPermanentError("slack_route_choice_unavailable");
2055
+ }
2056
+ const found = await getSlackRoutePromptByOption(deps.db, home, optionId);
2057
+ if (!found) throw new SlackInteractionPermanentError("slack_route_prompt_invalid");
2058
+ const { prompt, chosen } = found;
2059
+ // The same authorization pinning ordinary action handles use: a different
2060
+ // Slack human clicking someone else's card is ignored, not obeyed.
2061
+ if (prompt.slackUserId !== entry.slackUserId) {
2062
+ throw new SlackInteractionPermanentError("slack_route_prompt_user_mismatch");
2063
+ }
2064
+ const client = await createOpenGeniSlackBotInteractionClient(deps, {
2065
+ ...home,
2066
+ connectionId: entry.connectionId,
2067
+ subjectId: SLACK_INTERACTION_BOT_SUBJECT_ID,
2068
+ });
2069
+ // The post ledger binds one operation id to a digest over the text, so the
2070
+ // outcome has to be part of the seed. A second click on a settled card
2071
+ // renders different bytes than the first, and reusing one id would wedge the
2072
+ // ledger rather than saying so.
2073
+ const replaceCard = async (outcome: string, text: string) => {
2074
+ await client.updateMessage({
2075
+ operationId: deterministicUuid(
2076
+ `slack-route-answered:${prompt.id}:${actionId}:${chosen.id}:${outcome}`,
2077
+ ),
2078
+ channelId: entry.slackChannelId,
2079
+ timestamp: entry.slackMessageTs,
2080
+ text,
2081
+ // Every button goes with the card, so a sibling option cannot be pressed
2082
+ // after the question is settled.
2083
+ blocks: [{ type: "section", text: { type: "mrkdwn", text } }],
2084
+ });
2085
+ };
2086
+
2087
+ if (actionId === "opengeni.route.cancel") {
2088
+ const cancelled = await settleSlackRoutePrompt(deps.db, home, {
2089
+ promptId: prompt.id,
2090
+ status: "cancelled",
2091
+ });
2092
+ await replaceCard(
2093
+ cancelled ? "cancelled" : "already_settled",
2094
+ cancelled
2095
+ ? "OpenGeni did not start this task. No session was created."
2096
+ : "OpenGeni already settled this choice.",
2097
+ );
2098
+ return;
2099
+ }
2100
+
2101
+ const link = await getSlackBotUserLink(
2102
+ deps.db,
2103
+ home.workspaceId,
2104
+ entry.connectionId,
2105
+ entry.slackUserId,
2106
+ );
2107
+ const grant = link
2108
+ ? await resolveSlackTargetAuthority(deps.db, {
2109
+ subjectId: link.subjectId,
2110
+ targetAccountId: chosen.candidateAccountId,
2111
+ targetWorkspaceId: chosen.candidateWorkspaceId,
2112
+ })
2113
+ : null;
2114
+ if (!grant || !hasPermission(grant.permissions, "sessions:create")) {
2115
+ // Fail closed WITHOUT touching the card: the person may still have another
2116
+ // workspace on it that they can use, and replacing the card would take the
2117
+ // remaining buttons away and leave a pending prompt nobody can answer.
2118
+ await client.postMessage({
2119
+ operationId: deterministicUuid(`slack-route-denied:${prompt.id}:${chosen.id}`),
2120
+ userId: entry.slackUserId,
2121
+ text: boundedOutput(
2122
+ `OpenGeni cannot start work in ${chosen.candidateLabel}: you do not have access to it. No session was created. Pick another workspace on the card, or ask an OpenGeni administrator for access.`,
2123
+ ),
2124
+ });
2125
+ return;
2126
+ }
2127
+
2128
+ const answered = await answerSlackRoutePrompt(deps.db, home, {
2129
+ promptId: prompt.id,
2130
+ targetAccountId: chosen.candidateAccountId,
2131
+ targetWorkspaceId: chosen.candidateWorkspaceId,
2132
+ answeredBySubjectId: grant.subjectId,
2133
+ decidedBySlackUserId: entry.slackUserId,
2134
+ });
2135
+ if (!answered.answered) {
2136
+ await replaceCard(
2137
+ "settled",
2138
+ "OpenGeni already settled this choice. No new session was created.",
2139
+ );
2140
+ return;
2141
+ }
2142
+ await replaceCard(
2143
+ "answered",
2144
+ `OpenGeni will use *${chosen.candidateLabel}* here. You can change this in OpenGeni under Capabilities, then Slack.`,
2145
+ );
2146
+ }
2147
+
2148
+ async function processSlackInboxEntry(deps: ApiRouteDeps, entry: SlackInteractionInboxEntry) {
2149
+ if (entry.triggerKind === "block_action") {
2150
+ await processSlackBlockAction(deps, entry);
2151
+ return;
2152
+ }
2153
+ if (entry.triggerKind === "reaction") {
2154
+ await processSlackReactionInboxEntry(deps, entry);
2155
+ return;
2156
+ }
2157
+ // HOME is the installation binding's tenancy: it owns the bot credential and
2158
+ // connection, this inbox row, the identity links, and the post ledgers. TARGET
2159
+ // is the tenancy the resulting session lives in. They are equal today; the
2160
+ // routing resolver is what makes them diverge.
2161
+ const home = { accountId: entry.accountId, workspaceId: entry.workspaceId } as const;
2162
+ const installation = await resolveSlackInstallationRoute(deps.db, entry.slackTeamId);
2163
+ if (
2164
+ !installation ||
2165
+ installation.accountId !== entry.accountId ||
2166
+ installation.workspaceId !== entry.workspaceId ||
2167
+ installation.connectionId !== entry.connectionId
2168
+ ) {
2169
+ throw new SlackInteractionPermanentError("slack_task_installation_changed");
2170
+ }
2171
+ const client = await createOpenGeniSlackBotInteractionClient(deps, {
2172
+ ...home,
2173
+ connectionId: entry.connectionId,
2174
+ subjectId: SLACK_INTERACTION_BOT_SUBJECT_ID,
2175
+ });
2176
+ const policyResolution = isDirectMessageShortcut(entry)
2177
+ ? ({
2178
+ decision: {
2179
+ disposition: "ordinary",
2180
+ publication: "allow",
2181
+ reason: "ordinary_conversation",
2182
+ } as const,
2183
+ activePolicy: null,
2184
+ } as const)
2185
+ : await slackTaskPolicyDecision(deps, client, entry);
2186
+ const policyDecision = policyResolution.decision;
2187
+ if (policyDecision.disposition === "deny") {
2188
+ await client.postMessage({
2189
+ operationId: deterministicUuid(`slack-policy-denied:${entry.id}`),
2190
+ userId: entry.slackUserId,
2191
+ text: "OpenGeni cannot start a task from this shared conversation under the current workspace policy. No conversation content was read or retained.",
2192
+ });
2193
+ return;
2194
+ }
2195
+ const routePolicy = slackInteractionRoutePolicy(entry, {
2196
+ privateHandoff: policyDecision.disposition === "private_handoff",
2197
+ });
2198
+ const routeKey = routePolicy.initialRouteKey;
2199
+ // A mapped thread keeps the workspace it was created in, so the continuation
2200
+ // lookup is connection-scoped rather than workspace-fenced.
2201
+ const existing = await getSlackInteractionByConnectionRoute(deps.db, {
2202
+ accountId: home.accountId,
2203
+ connectionId: entry.connectionId,
2204
+ routeKey,
2205
+ });
2206
+ if (entry.triggerKind === "thread_reply" && !existing) return;
2207
+ // The identity link is a HOME fact: `slack_bot_user_links` is unique on
2208
+ // `(connection_id, slack_user_id)` and RLS-visible only under the
2209
+ // installation's own tenancy. It is read before the target is chosen because
2210
+ // the routing decision needs the subject it names.
2211
+ const link = await getSlackBotUserLink(
2212
+ deps.db,
2213
+ home.workspaceId,
2214
+ entry.connectionId,
2215
+ entry.slackUserId,
2216
+ );
2217
+ if (!link) {
2218
+ await client.postMessage({
2219
+ operationId: deterministicUuid(`slack-link:${entry.id}`),
2220
+ userId: entry.slackUserId,
2221
+ text: `Link your Slack identity to OpenGeni before starting work: ${linkUrl(deps, entry, entry.createdAt.getTime())}. No session was created.`,
2222
+ });
2223
+ return;
2224
+ }
2225
+ const resolution = await resolveSlackRouteForEntry(deps, home, entry, link.subjectId, {
2226
+ botUserId: installation.botUserId,
2227
+ threadTenancy: existing
2228
+ ? { accountId: existing.accountId, workspaceId: existing.workspaceId }
2229
+ : null,
2230
+ askEnabled: true,
2231
+ });
2232
+ if (resolution.kind === "ask") {
2233
+ await askSlackRouteChoice(deps, client, home, entry, resolution.candidates);
2234
+ return;
2235
+ }
2236
+ if (resolution.kind === "denied") {
2237
+ await postSlackRouteRefusal(deps, client, entry, resolution);
2238
+ return;
2239
+ }
2240
+ const target: { accountId: string; workspaceId: string } = {
2241
+ accountId: resolution.accountId,
2242
+ workspaceId: resolution.workspaceId,
2243
+ };
2244
+ const grant = await resolveSlackTargetAuthority(deps.db, {
2245
+ subjectId: link.subjectId,
2246
+ targetAccountId: target.accountId,
2247
+ targetWorkspaceId: target.workspaceId,
2248
+ });
2249
+ if (!grant) {
2250
+ // Never quietly serve this from a workspace the person did not name. A
2251
+ // routed workspace the subject cannot reach is a refusal with the existing
2252
+ // access-request path, minted for the workspace they actually need.
2253
+ if (resolution.source === "installation" || resolution.source === "thread") {
2254
+ throw new SlackInteractionPermanentError("identity_access_revoked");
2255
+ }
2256
+ await postSlackRouteRefusal(deps, client, entry, {
2257
+ kind: "denied",
2258
+ reason: "no_access_to_route",
2259
+ requested: resolution.label,
2260
+ candidates: [],
2261
+ ...target,
2262
+ });
2263
+ return;
2264
+ }
2265
+ // An override addresses the message; it is not part of the request.
2266
+ const routedEntry: SlackInteractionInboxEntry = {
2267
+ ...entry,
2268
+ text: slackRoutedRequestText(entry.text, resolution, installation.botUserId),
2269
+ };
968
2270
 
969
2271
  const alreadyDurable = await getSlackInteractionByClientEventId(
970
2272
  deps.db,
971
- entry.workspaceId,
2273
+ target.workspaceId,
972
2274
  entry.connectionId,
973
2275
  `slack:${entry.providerEventId}`,
974
2276
  );
@@ -987,18 +2289,23 @@ async function processSlackInboxEntry(deps: ApiRouteDeps, entry: SlackInteractio
987
2289
  ...interaction,
988
2290
  owningSubjectId: grant.subjectId,
989
2291
  sessionId: eventSessionId,
2292
+ startMessageLine: slackStartMessageLineForEntry(
2293
+ deps,
2294
+ interaction,
2295
+ eventSessionId,
2296
+ entry,
2297
+ ),
990
2298
  });
991
2299
  if (!boundInteraction) {
992
2300
  throw new Error("Durable Slack interaction could not bind its reserved session");
993
2301
  }
994
- await ensureSlackSharedTaskOrigin(deps, boundInteraction, entry, policyResolution);
2302
+ await ensureSlackSharedTaskOrigin(deps, boundInteraction, entry, home, policyResolution);
995
2303
  const shouldRepairAcknowledgement =
996
2304
  interaction.triggeringProviderEventId === entry.providerEventId ||
997
2305
  (usesPrivateBotDm(boundInteraction, entry) && boundInteraction.ackSlackMessageTs === null);
998
2306
  if (shouldRepairAcknowledgement) {
999
2307
  const boundClient = await createOpenGeniSlackBotInteractionClient(deps, {
1000
- accountId: entry.accountId,
1001
- workspaceId: entry.workspaceId,
2308
+ ...home,
1002
2309
  connectionId: entry.connectionId,
1003
2310
  subjectId: SLACK_INTERACTION_BOT_SUBJECT_ID,
1004
2311
  sessionId: eventSessionId,
@@ -1008,7 +2315,8 @@ async function processSlackInboxEntry(deps: ApiRouteDeps, entry: SlackInteractio
1008
2315
  return;
1009
2316
  }
1010
2317
 
1011
- let preparedEntry = entry;
2318
+ let preparedEntry = routedEntry;
2319
+ let preparedModelContext: string | null = null;
1012
2320
  let preparedAttachments: PreparedSlackReactionTask = {
1013
2321
  resources: [],
1014
2322
  attachments: [],
@@ -1019,7 +2327,8 @@ async function processSlackInboxEntry(deps: ApiRouteDeps, entry: SlackInteractio
1019
2327
  const prepared = await prepareSlackInvocationEntry(
1020
2328
  deps,
1021
2329
  client,
1022
- entry,
2330
+ routedEntry,
2331
+ target,
1023
2332
  policyDecision.disposition === "private_handoff"
1024
2333
  ? async () => {
1025
2334
  await requireSlackSharedReadAuthorization(deps, client, entry, policyResolution);
@@ -1027,31 +2336,38 @@ async function processSlackInboxEntry(deps: ApiRouteDeps, entry: SlackInteractio
1027
2336
  : undefined,
1028
2337
  );
1029
2338
  preparedEntry = prepared.entry;
2339
+ preparedModelContext = prepared.modelContext;
1030
2340
  preparedAttachments = prepared.attachments;
1031
2341
  }
1032
2342
 
1033
2343
  if (existing?.sessionId) {
1034
2344
  const remounted = await remountSlackReactionTaskForSession(
1035
2345
  deps,
1036
- entry.workspaceId,
2346
+ target.workspaceId,
1037
2347
  existing.sessionId,
1038
2348
  preparedAttachments,
1039
2349
  );
1040
- await continueSlackSession(
2350
+ const prepared = slackInvocationPreparedMessage(
1041
2351
  deps,
1042
- grant,
1043
- existing,
1044
- slackInvocationPreparedEntry(preparedEntry, remounted),
1045
- remounted.resources,
2352
+ preparedEntry,
2353
+ remounted,
2354
+ preparedModelContext,
1046
2355
  );
2356
+ await continueSlackSession(deps, grant, existing, prepared.entry, remounted.resources, {
2357
+ modelContext: prepared.modelContext,
2358
+ });
1047
2359
  return;
1048
2360
  }
1049
2361
  if (!hasPermission(grant.permissions, "sessions:create")) {
1050
2362
  throw new SlackInteractionPermanentError("sessions_create_denied");
1051
2363
  }
1052
2364
  const { interaction } = await getOrCreateSlackInteraction(deps.db, {
1053
- accountId: entry.accountId,
1054
- workspaceId: entry.workspaceId,
2365
+ ...target,
2366
+ // Frozen here, after authorization, so a workspace name is never shown to
2367
+ // someone who could not reach it. Null when the routing decision was not a
2368
+ // genuine choice, which is what keeps the line off every message in an
2369
+ // install that only ever had one workspace.
2370
+ routedWorkspaceLabel: await routedWorkspaceLabelFor(deps, resolution),
1055
2371
  connectionId: entry.connectionId,
1056
2372
  slackTeamId: entry.slackTeamId,
1057
2373
  slackChannelId: entry.slackChannelId,
@@ -1062,39 +2378,69 @@ async function processSlackInboxEntry(deps: ApiRouteDeps, entry: SlackInteractio
1062
2378
  owningSubjectId: grant.subjectId,
1063
2379
  visibility: routePolicy.visibility,
1064
2380
  });
2381
+ requireSlackInteractionMatchesTarget(interaction, target);
1065
2382
  if (interaction.sessionId) {
1066
2383
  const remounted = await remountSlackReactionTaskForSession(
1067
2384
  deps,
1068
- entry.workspaceId,
2385
+ interaction.workspaceId,
1069
2386
  interaction.sessionId,
1070
2387
  preparedAttachments,
1071
2388
  );
1072
- await continueSlackSession(
2389
+ const prepared = slackInvocationPreparedMessage(
1073
2390
  deps,
1074
- grant,
1075
- interaction,
1076
- slackInvocationPreparedEntry(preparedEntry, remounted),
1077
- remounted.resources,
2391
+ preparedEntry,
2392
+ remounted,
2393
+ preparedModelContext,
1078
2394
  );
2395
+ await continueSlackSession(deps, grant, interaction, prepared.entry, remounted.resources, {
2396
+ modelContext: prepared.modelContext,
2397
+ });
1079
2398
  return;
1080
2399
  }
1081
2400
  const preferredModel = await getLatestSessionModelForSubject(
1082
2401
  deps.db,
1083
- entry.workspaceId,
2402
+ interaction.workspaceId,
1084
2403
  grant.subjectId,
1085
2404
  );
1086
2405
  let session: Awaited<ReturnType<typeof createSessionForRequest>>;
1087
2406
  try {
1088
- session = await createSessionForRequest(deps, grant, entry.workspaceId, {
1089
- requestedSessionId: interaction.sessionReservationId,
1090
- initialMessage: slackInvocationPreparedEntry(preparedEntry, preparedAttachments).text,
1091
- instructions: SLACK_SESSION_INSTRUCTIONS,
1092
- firstPartyMcpTools: slackTaskFirstPartyMcpTools(deps.settings),
1093
- resources: preparedAttachments.resources,
1094
- ...(preferredModel ? { model: preferredModel } : {}),
1095
- idempotencyKey: `slack:${entry.connectionId}:${entry.providerEventId}`,
1096
- clientEventId: `slack:${entry.providerEventId}`,
1097
- });
2407
+ const defaults = await slackNewSessionDefaults(
2408
+ deps,
2409
+ grant,
2410
+ interaction,
2411
+ `slack:${entry.connectionId}:${entry.providerEventId}`,
2412
+ );
2413
+ const workspace = await getWorkspace(deps.db, interaction.workspaceId);
2414
+ const prepared = slackInvocationPreparedMessage(
2415
+ deps,
2416
+ preparedEntry,
2417
+ preparedAttachments,
2418
+ preparedModelContext,
2419
+ );
2420
+ session = await createSessionForRequest(
2421
+ await withCatalogSettings(deps, grant),
2422
+ grant,
2423
+ interaction.workspaceId,
2424
+ {
2425
+ ...defaults,
2426
+ requestedSessionId: interaction.sessionReservationId,
2427
+ initialMessage: prepared.entry.text,
2428
+ ...(prepared.modelContext ? { modelContext: prepared.modelContext } : {}),
2429
+ instructions: SLACK_SESSION_INSTRUCTIONS,
2430
+ // A retried create replays the reserved shell's exact selection;
2431
+ // otherwise the workspace default plus the Slack read tools.
2432
+ firstPartyMcpTools:
2433
+ defaults.firstPartyMcpTools !== undefined
2434
+ ? allowedFirstPartyMcpToolsForSession(deps.settings, defaults.firstPartyMcpTools)
2435
+ : slackTaskFirstPartyMcpTools(deps.settings, workspace?.settings),
2436
+ resources: [...defaults.resources, ...preparedAttachments.resources],
2437
+ ...(!defaults.model && preferredModel ? { model: preferredModel } : {}),
2438
+ idempotencyKey: `slack:${entry.connectionId}:${entry.providerEventId}`,
2439
+ clientEventId: `slack:${entry.providerEventId}`,
2440
+ },
2441
+ undefined,
2442
+ { surface: "slack" },
2443
+ );
1098
2444
  } catch (error) {
1099
2445
  if (error instanceof HTTPException) {
1100
2446
  await client.postMessage({
@@ -1116,12 +2462,12 @@ async function processSlackInboxEntry(deps: ApiRouteDeps, entry: SlackInteractio
1116
2462
  ...interaction,
1117
2463
  owningSubjectId: grant.subjectId,
1118
2464
  sessionId: session.id,
2465
+ startMessageLine: slackStartMessageLineForEntry(deps, interaction, session.id, entry),
1119
2466
  });
1120
2467
  if (!bound) throw new Error("Slack route could not bind its durable session");
1121
- await ensureSlackSharedTaskOrigin(deps, bound, entry, policyResolution);
2468
+ await ensureSlackSharedTaskOrigin(deps, bound, entry, home, policyResolution);
1122
2469
  const boundClient = await createOpenGeniSlackBotInteractionClient(deps, {
1123
- accountId: entry.accountId,
1124
- workspaceId: entry.workspaceId,
2470
+ ...home,
1125
2471
  connectionId: entry.connectionId,
1126
2472
  subjectId: SLACK_INTERACTION_BOT_SUBJECT_ID,
1127
2473
  sessionId: session.id,
@@ -1164,6 +2510,7 @@ async function ensureSlackSharedTaskOrigin(
1164
2510
  deps: ApiRouteDeps,
1165
2511
  interaction: SlackInteraction,
1166
2512
  entry: SlackInteractionInboxEntry,
2513
+ home: { accountId: string; workspaceId: string },
1167
2514
  resolution:
1168
2515
  | Awaited<ReturnType<typeof slackTaskPolicyDecision>>
1169
2516
  | {
@@ -1189,6 +2536,11 @@ async function ensureSlackSharedTaskOrigin(
1189
2536
  sourceChannelId: entry.slackChannelId,
1190
2537
  sourceThreadTs: entry.slackThreadTs ?? entry.slackMessageTs,
1191
2538
  initiatingSlackUserId: interaction.initiatingSlackUserId,
2539
+ // The task policy governs what may be read out of the Slack conversation,
2540
+ // which is an installation-surface concern, so its frozen revision keeps
2541
+ // home tenancy while the row itself follows the routed task.
2542
+ policyAccountId: home.accountId,
2543
+ policyWorkspaceId: home.workspaceId,
1192
2544
  policyRevisionId: resolution.activePolicy.revision.id,
1193
2545
  policyHash: resolution.activePolicy.revision.policyHash,
1194
2546
  policyActivationVersion: resolution.activePolicy.head.activationVersion,
@@ -1219,89 +2571,173 @@ export type SlackInvocationMessageContext = {
1219
2571
  messages: Awaited<ReturnType<OpenGeniSlackBotClient["threadReplies"]>>["messages"];
1220
2572
  nextCursor: string | null;
1221
2573
  kind: "thread" | "channel";
2574
+ unavailable?: "rate_limited" | "attachments_only";
1222
2575
  };
1223
2576
 
2577
+ /** Context is optional; only Slack's explicit history throttling may be omitted. */
2578
+ export async function loadSlackInvocationMessageContext(
2579
+ client: Pick<OpenGeniSlackBotClient, "threadReplies" | "channelHistory">,
2580
+ entry: Pick<SlackInteractionInboxEntry, "slackChannelId" | "slackThreadTs" | "slackMessageTs">,
2581
+ authorizeRead?: () => Promise<void>,
2582
+ exactMessageOnly = false,
2583
+ ): Promise<SlackInvocationMessageContext> {
2584
+ const kind = entry.slackThreadTs ? "thread" : "channel";
2585
+ try {
2586
+ const context = entry.slackThreadTs
2587
+ ? await client.threadReplies({
2588
+ channelId: entry.slackChannelId,
2589
+ threadTimestamp: entry.slackThreadTs,
2590
+ limit: exactMessageOnly ? 1 : MAX_SLACK_INVOCATION_CONTEXT_MESSAGES,
2591
+ ...(exactMessageOnly
2592
+ ? {
2593
+ oldest: entry.slackMessageTs,
2594
+ latest: entry.slackMessageTs,
2595
+ inclusive: true,
2596
+ }
2597
+ : {}),
2598
+ ...(authorizeRead ? { authorizeRead } : {}),
2599
+ })
2600
+ : await client.channelHistory({
2601
+ channelId: entry.slackChannelId,
2602
+ latest: entry.slackMessageTs,
2603
+ inclusive: true,
2604
+ limit: MAX_SLACK_CHANNEL_CONTEXT_MESSAGES,
2605
+ ...(authorizeRead ? { authorizeRead } : {}),
2606
+ });
2607
+ return {
2608
+ messages: context.messages,
2609
+ nextCursor: context.nextCursor,
2610
+ kind,
2611
+ ...(exactMessageOnly && kind === "thread"
2612
+ ? { unavailable: "attachments_only" as const }
2613
+ : {}),
2614
+ };
2615
+ } catch (error) {
2616
+ if (!slackHistoryRateLimited(error)) throw error;
2617
+ return { messages: [], nextCursor: null, kind, unavailable: "rate_limited" };
2618
+ }
2619
+ }
2620
+
1224
2621
  async function prepareSlackInvocationEntry(
1225
2622
  deps: ApiRouteDeps,
1226
2623
  client: OpenGeniSlackBotClient,
1227
2624
  entry: SlackInteractionInboxEntry,
2625
+ // Imported attachments become File resources of the session's workspace, so
2626
+ // they follow TARGET tenancy rather than the installation's.
2627
+ target: { accountId: string; workspaceId: string },
1228
2628
  authorizeRead?: () => Promise<void>,
1229
- ): Promise<{ entry: SlackInteractionInboxEntry; attachments: PreparedSlackReactionTask }> {
1230
- const context = entry.slackThreadTs
1231
- ? await client.threadReplies({
2629
+ ): Promise<{
2630
+ entry: SlackInteractionInboxEntry;
2631
+ attachments: PreparedSlackReactionTask;
2632
+ modelContext: string | null;
2633
+ }> {
2634
+ // In an unlisted app, the exact attachment message gets the one available
2635
+ // thread-read slot before optional surrounding history does.
2636
+ const context = await loadSlackInvocationMessageContext(
2637
+ client,
2638
+ entry,
2639
+ authorizeRead,
2640
+ entry.hasFiles && deps.settings.slackAccessMode === "limited",
2641
+ );
2642
+ let exactMessage = context.messages.find((message) => message.timestamp === entry.slackMessageTs);
2643
+ if (!exactMessage && entry.hasFiles && entry.slackThreadTs && !context.unavailable) {
2644
+ try {
2645
+ const exact = await client.threadReplies({
1232
2646
  channelId: entry.slackChannelId,
1233
2647
  threadTimestamp: entry.slackThreadTs,
1234
- limit: MAX_SLACK_INVOCATION_CONTEXT_MESSAGES,
1235
- ...(authorizeRead ? { authorizeRead } : {}),
1236
- })
1237
- : await client.channelHistory({
1238
- channelId: entry.slackChannelId,
2648
+ oldest: entry.slackMessageTs,
1239
2649
  latest: entry.slackMessageTs,
1240
2650
  inclusive: true,
1241
- limit: MAX_SLACK_CHANNEL_CONTEXT_MESSAGES,
2651
+ limit: 1,
1242
2652
  ...(authorizeRead ? { authorizeRead } : {}),
1243
2653
  });
1244
- let exactMessage = context.messages.find((message) => message.timestamp === entry.slackMessageTs);
1245
- if (!exactMessage && entry.hasFiles && entry.slackThreadTs) {
1246
- const exact = await client.threadReplies({
1247
- channelId: entry.slackChannelId,
1248
- threadTimestamp: entry.slackThreadTs,
1249
- oldest: entry.slackMessageTs,
1250
- latest: entry.slackMessageTs,
1251
- inclusive: true,
1252
- limit: 1,
1253
- ...(authorizeRead ? { authorizeRead } : {}),
1254
- });
1255
- exactMessage = exact.messages.find((message) => message.timestamp === entry.slackMessageTs);
2654
+ exactMessage = exact.messages.find((message) => message.timestamp === entry.slackMessageTs);
2655
+ } catch (error) {
2656
+ if (!slackHistoryRateLimited(error)) throw error;
2657
+ }
1256
2658
  }
1257
2659
  const attachments = exactMessage
1258
- ? await prepareSlackMessageAttachments(deps, client, entry, exactMessage.files)
2660
+ ? await prepareSlackMessageAttachments(
2661
+ deps,
2662
+ client,
2663
+ entry,
2664
+ target,
2665
+ exactMessage.files,
2666
+ authorizeRead,
2667
+ )
1259
2668
  : {
1260
2669
  resources: [],
1261
2670
  attachments: [],
1262
2671
  omissionCodes: entry.hasFiles ? ["attachment_unavailable"] : [],
1263
2672
  omittedCount: entry.hasFiles ? 1 : 0,
1264
2673
  };
1265
- const baseText =
2674
+ const modelContext =
1266
2675
  entry.triggerKind === "app_mention"
1267
- ? slackInvocationTaskText(entry, {
2676
+ ? slackInvocationModelContext(entry.slackMessageTs, {
1268
2677
  messages: context.messages,
1269
2678
  nextCursor: context.nextCursor,
1270
- kind: entry.slackThreadTs ? "thread" : "channel",
2679
+ kind: context.kind,
2680
+ ...(context.unavailable ? { unavailable: context.unavailable } : {}),
1271
2681
  })
1272
- : entry.text;
2682
+ : null;
1273
2683
  return {
1274
- entry: { ...entry, text: baseText },
2684
+ entry,
1275
2685
  attachments,
2686
+ modelContext,
1276
2687
  };
1277
2688
  }
1278
2689
 
1279
- function slackInvocationPreparedEntry(
2690
+ /**
2691
+ * The model-context note for links to another OpenGeni deployment. A pure
2692
+ * function of the accepted text and this deployment's web origin, so a retried
2693
+ * delivery composes the same bytes.
2694
+ */
2695
+ function slackOtherDeploymentModelContext(
2696
+ deps: Pick<ApiRouteDeps, "settings">,
2697
+ text: string,
2698
+ ): string | null {
2699
+ return otherDeploymentLinkContext(text, deps.settings.webBaseUrl ?? deps.settings.publicBaseUrl);
2700
+ }
2701
+
2702
+ function slackInvocationPreparedMessage(
2703
+ deps: Pick<ApiRouteDeps, "settings">,
1280
2704
  entry: SlackInteractionInboxEntry,
1281
2705
  attachments: PreparedSlackReactionTask,
1282
- ): SlackInteractionInboxEntry {
2706
+ modelContext: string | null,
2707
+ ): { entry: SlackInteractionInboxEntry; modelContext: string | null } {
1283
2708
  const manifest = slackAttachmentManifest(
1284
2709
  attachments,
1285
2710
  "Imported invocation attachments",
1286
2711
  "invocation",
1287
2712
  );
1288
- return manifest ? { ...entry, text: `${entry.text}\n\n${manifest}` } : entry;
2713
+ const otherDeployment = slackOtherDeploymentModelContext(
2714
+ deps,
2715
+ [entry.text, modelContext ?? ""].join("\n"),
2716
+ );
2717
+ const combinedModelContext = [modelContext, manifest, otherDeployment]
2718
+ .filter(Boolean)
2719
+ .join("\n\n");
2720
+ return {
2721
+ entry,
2722
+ modelContext: combinedModelContext.length > 0 ? combinedModelContext : null,
2723
+ };
1289
2724
  }
1290
2725
 
1291
- export function slackInvocationTaskText(
1292
- entry: Pick<SlackInteractionInboxEntry, "slackMessageTs" | "slackUserId" | "text">,
2726
+ export function slackInvocationModelContext(
2727
+ invocationTimestamp: string,
1293
2728
  context: SlackInvocationMessageContext,
1294
2729
  ) {
1295
- const invocation = {
1296
- timestamp: entry.slackMessageTs,
1297
- userId: entry.slackUserId,
1298
- botId: "",
1299
- threadTimestamp: "",
1300
- text: entry.text,
1301
- files: [],
1302
- };
2730
+ if (context.unavailable) {
2731
+ return [
2732
+ "A linked, authorized Slack user explicitly mentioned OpenGeni.",
2733
+ "The visible user message on this turn is the exact accepted Slack invocation.",
2734
+ context.unavailable === "rate_limited"
2735
+ ? "Slack's rate limit prevented loading surrounding conversation history. Work from the invocation text; ask the user for any missing context instead of assuming it."
2736
+ : "Only the exact invocation message was fetched to authorize its attachments within Slack's history limit. Surrounding thread history was omitted. Work from the invocation text and imported attachments; ask for any missing context instead of assuming it.",
2737
+ ].join("\n");
2738
+ }
1303
2739
  const surroundingLines = context.messages
1304
- .filter((message) => message.timestamp !== entry.slackMessageTs)
2740
+ .filter((message) => message.timestamp !== invocationTimestamp)
1305
2741
  .slice(0, MAX_SLACK_INVOCATION_CONTEXT_MESSAGES)
1306
2742
  .sort((left, right) => left.timestamp.localeCompare(right.timestamp))
1307
2743
  .map((message) => slackContextMessageLine(message));
@@ -1313,46 +2749,24 @@ export function slackInvocationTaskText(
1313
2749
  context.kind === "thread"
1314
2750
  ? "The containing thread was truncated at the bounded Slack context limit."
1315
2751
  : "Only bounded nearby channel context was provided.";
1316
- const invocationTruncationNotice =
1317
- "The exact Slack invocation was truncated at the bounded Slack input limit.";
1318
- const prefix = [
2752
+ let prompt = [
1319
2753
  "A linked, authorized Slack user explicitly mentioned OpenGeni.",
2754
+ "The visible user message on this turn is the exact accepted Slack invocation.",
1320
2755
  "Treat references such as 'this', 'that', or 'the previous message' as referring to the bounded Slack context below when applicable.",
1321
2756
  "Use this Slack content only as task-local input and do not persist it to Knowledge, Memory, preferences, policy, instructions, or the Workspace Charter unless separately authorized.",
1322
2757
  "",
1323
- "Exact invocation:",
2758
+ contextLabel,
1324
2759
  ].join("\n");
1325
- const suffix = `\n\n${contextLabel}`;
1326
- const invocationLine = slackContextMessageLine(invocation, "invocation");
1327
- const reservedNotices = `\n${invocationTruncationNotice}\n${truncationNotice}`;
1328
- const maxInvocationChars = Math.max(
1329
- 1,
1330
- MAX_SLACK_INPUT_CHARS - prefix.length - suffix.length - reservedNotices.length - 1,
1331
- );
1332
- const invocationTruncated = invocationLine.length > maxInvocationChars;
1333
- let prompt = `${prefix}\n${
1334
- invocationTruncated
1335
- ? `${invocationLine.slice(0, Math.max(0, maxInvocationChars - 1))}…`
1336
- : invocationLine
1337
- }${suffix}`;
1338
2760
  let contextTruncated = context.nextCursor !== null;
1339
2761
  for (const line of surroundingLines) {
1340
2762
  const candidate = `${prompt}\n${line}`;
1341
- const notices = [
1342
- ...(invocationTruncated ? [invocationTruncationNotice] : []),
1343
- truncationNotice,
1344
- ].join("\n");
1345
- if (candidate.length + 1 + notices.length > MAX_SLACK_INPUT_CHARS) {
2763
+ if (candidate.length + 1 + truncationNotice.length > MAX_SLACK_INPUT_CHARS) {
1346
2764
  contextTruncated = true;
1347
2765
  break;
1348
2766
  }
1349
2767
  prompt = candidate;
1350
2768
  }
1351
- const notices = [
1352
- ...(invocationTruncated ? [invocationTruncationNotice] : []),
1353
- ...(contextTruncated ? [truncationNotice] : []),
1354
- ];
1355
- return notices.length > 0 ? `${prompt}\n${notices.join("\n")}` : prompt;
2769
+ return contextTruncated ? `${prompt}\n${truncationNotice}` : prompt;
1356
2770
  }
1357
2771
 
1358
2772
  async function acknowledgeSlackSession(
@@ -1368,17 +2782,37 @@ async function acknowledgeSlackSession(
1368
2782
  const privateHandoff =
1369
2783
  !directMessageShortcut && interaction.visibility === "private" && entry.triggerKind !== "dm";
1370
2784
  const privateBotDm = directMessageShortcut || privateHandoff;
1371
- const operationId = deterministicUuid(`slack-ack:${interaction.id}`);
1372
- const text = directMessageShortcut
1373
- ? `OpenGeni started a private task from the selected DM message. ${openSessionText(deps, entry.workspaceId, interaction.sessionId)} Reply in this bot-DM thread to continue, or reply \`stop\` to stop. The source DM was not opened to the bot or made workspace-visible.`
1374
- : privateHandoff
1375
- ? `OpenGeni started a private task from the selected Slack conversation. ${openSessionText(deps, entry.workspaceId, interaction.sessionId)} Reply in this bot-DM thread to continue, or reply \`stop\` to stop. Results stay private unless a separate authorized publication is approved.`
1376
- : `OpenGeni started this task. ${openSessionText(deps, entry.workspaceId, interaction.sessionId)} Reply in this thread to continue, or reply \`stop\` to stop. Start a new top-level DM or invoke /opengeni again for a new session.`;
1377
- const controls = await controlActionBlocks(deps, interaction, {
1378
- messageOperationId: operationId,
1379
- sessionEventSequence: 0,
1380
- state: "active",
2785
+ const operationId = slackAcknowledgementOperationId(interaction);
2786
+ // The post ledger binds this fixed operation id to a digest over the message
2787
+ // text, and this function re-runs on every acknowledgement repair. Resolving
2788
+ // the hint freezes it durably before the post, so a repair renders identical
2789
+ // bytes; a failure here raises into the retryable inbox path rather than
2790
+ // binding the ledger to a hint-less acknowledgement it can never revise.
2791
+ const showHint = await resolveSlackInteractionFirstTaskHint(deps.db, {
2792
+ // The identity link is HOME; the interaction row is TARGET.
2793
+ workspaceId: entry.workspaceId,
2794
+ interactionWorkspaceId: interaction.workspaceId,
2795
+ connectionId: entry.connectionId,
2796
+ slackUserId: entry.slackUserId,
2797
+ interactionId: interaction.id,
1381
2798
  });
2799
+ const acknowledgement =
2800
+ interaction.startMessageLine !== null
2801
+ ? slackStartMessage(deps, interaction.startMessageLine, {
2802
+ hint: showHint,
2803
+ stopped: false,
2804
+ button: await reserveSlackStartMessageButton(deps, interaction, operationId, {
2805
+ actionKind: "session_pause",
2806
+ actionKey: `${operationId}:session_pause`,
2807
+ }),
2808
+ })
2809
+ : await legacySlackAcknowledgement(deps, interaction, {
2810
+ sessionId: interaction.sessionId,
2811
+ operationId,
2812
+ showHint,
2813
+ directMessageShortcut,
2814
+ privateHandoff,
2815
+ });
1382
2816
  const ack = await client.postMessage({
1383
2817
  operationId,
1384
2818
  ...(privateBotDm
@@ -1389,15 +2823,8 @@ async function acknowledgeSlackSession(
1389
2823
  ? {}
1390
2824
  : { threadTimestamp: entry.slackThreadTs ?? entry.slackMessageTs }),
1391
2825
  }),
1392
- text,
1393
- ...(controls.length > 0
1394
- ? {
1395
- blocks: [
1396
- { type: "section", text: { type: "mrkdwn", text } },
1397
- ...controls,
1398
- ] as SlackMessageBlock[],
1399
- }
1400
- : {}),
2826
+ text: acknowledgement.text,
2827
+ ...(acknowledgement.blocks ? { blocks: acknowledgement.blocks } : {}),
1401
2828
  });
1402
2829
  if (entry.triggerKind === "slash_command" || privateBotDm) {
1403
2830
  const rekeyed = await rekeySlackInteractionRoute(deps.db, {
@@ -1410,16 +2837,139 @@ async function acknowledgeSlackSession(
1410
2837
  });
1411
2838
  if (!rekeyed) throw new Error("Slack acknowledgement could not rekey its durable route");
1412
2839
  }
2840
+ if (interaction.startMessageLine !== null) {
2841
+ // The task may have settled while this post was still being retried, when
2842
+ // the delivery pump had no posted message to take the button off.
2843
+ const current = await getSlackInteractionById(deps.db, {
2844
+ accountId: interaction.accountId,
2845
+ workspaceId: interaction.workspaceId,
2846
+ interactionId: interaction.id,
2847
+ });
2848
+ if (current && SLACK_SETTLED_DELIVERY_STATES.has(current.terminalDeliveryState)) {
2849
+ await retireSlackStartMessageButton(deps, client, current, {
2850
+ accountId: entry.accountId,
2851
+ workspaceId: entry.workspaceId,
2852
+ });
2853
+ }
2854
+ }
2855
+ }
2856
+
2857
+ /** Delivery states that mean the task itself settled. */
2858
+ const SLACK_SETTLED_DELIVERY_STATES: ReadonlySet<SlackInteraction["terminalDeliveryState"]> =
2859
+ new Set(["completed", "failed", "cancelled"]);
2860
+
2861
+ /**
2862
+ * Reserve the one button a compact first message carries. The action key is
2863
+ * deterministic, so a repair or a retried click finds the same handle and
2864
+ * renders the same bytes. Null when the task has no Slack requester to bind it
2865
+ * to, in which case the message has no button at all.
2866
+ */
2867
+ async function reserveSlackStartMessageButton(
2868
+ deps: ApiRouteDeps,
2869
+ interaction: SlackInteraction,
2870
+ messageOperationId: string,
2871
+ action: { actionKind: "session_pause" | "session_resume"; actionKey: string },
2872
+ ): Promise<SlackInteractionActionHandle | null> {
2873
+ if (!interaction.sessionId || !interaction.initiatingSlackUserId) return null;
2874
+ const [handle] = await reserveSlackInteractionActionHandles(deps.db, {
2875
+ interaction,
2876
+ sessionEventSequence: 0,
2877
+ messageOperationId,
2878
+ expiresAt: new Date(Date.now() + SLACK_ACTION_TTL_MS),
2879
+ actions: [action],
2880
+ });
2881
+ return handle ?? null;
2882
+ }
2883
+
2884
+ /**
2885
+ * The acknowledgement exactly as interactions bound before compact messages
2886
+ * render it: a separate session link, any frozen defaults line, the Status and
2887
+ * Stop buttons, and the workspace line. Kept byte for byte because a repair of
2888
+ * an acknowledgement that is already in the post ledger must reproduce it.
2889
+ */
2890
+ async function legacySlackAcknowledgement(
2891
+ deps: ApiRouteDeps,
2892
+ interaction: SlackInteraction,
2893
+ input: {
2894
+ sessionId: string;
2895
+ operationId: string;
2896
+ showHint: boolean;
2897
+ directMessageShortcut: boolean;
2898
+ privateHandoff: boolean;
2899
+ },
2900
+ ): Promise<{ text: string; blocks?: SlackMessageBlock[] }> {
2901
+ const started = input.directMessageShortcut
2902
+ ? `OpenGeni started a private task from the selected DM message. ${openSessionText(deps, interaction.workspaceId, input.sessionId)} The source DM was not opened to the bot or made workspace-visible.`
2903
+ : input.privateHandoff
2904
+ ? `OpenGeni started a private task from the selected Slack conversation. ${openSessionText(deps, interaction.workspaceId, input.sessionId)} Results stay private unless a separate authorized publication is approved.`
2905
+ : `OpenGeni started this task. ${openSessionText(deps, interaction.workspaceId, input.sessionId)}`;
2906
+ const startedWithDefaults = interaction.sessionDefaultsLine
2907
+ ? `${started}\n${interaction.sessionDefaultsLine}`
2908
+ : started;
2909
+ const text = `${startedWithDefaults}${input.showHint ? slackFirstTaskHintText(deps) : ""}`;
2910
+ const controls = await controlActionBlocks(deps, interaction, {
2911
+ messageOperationId: input.operationId,
2912
+ sessionEventSequence: 0,
2913
+ state: "active",
2914
+ });
2915
+ // The approval and control cards let `text` and `blocks[0]` diverge, so the
2916
+ // line is appended to each independently rather than assuming they match.
2917
+ return withWorkspaceLine(
2918
+ interaction.routedWorkspaceLabel,
2919
+ text,
2920
+ controls.length > 0
2921
+ ? ([{ type: "section", text: { type: "mrkdwn", text } }, ...controls] as SlackMessageBlock[])
2922
+ : undefined,
2923
+ );
2924
+ }
2925
+
2926
+ /**
2927
+ * The one-time onboarding sentence on a compact task's first message: how to
2928
+ * continue and stop it, and where to find the rest. Scheduling is deliberately
2929
+ * not named here. Whether the agent can create a schedule depends on the
2930
+ * session's tools and the person's `scheduled_tasks:manage`, neither of which
2931
+ * is frozen with this text, so the live-gated `<command> info` card names it.
2932
+ *
2933
+ * Same determinism note as `slackFirstTaskHintText`: the configured command is
2934
+ * the only input that is not frozen.
2935
+ */
2936
+ function slackStartMessageFirstTaskHintText(deps: ApiRouteDeps): string {
2937
+ const command = deps.settings.slackCommand;
2938
+ return `\n\nFirst time here: reply in this thread to continue, or reply \`stop\` to stop. Run \`${command} info\` for more.`;
2939
+ }
2940
+
2941
+ /**
2942
+ * The one-time onboarding sentence shown on a Slack identity's first
2943
+ * acknowledged task in one installation, in the previous message format.
2944
+ *
2945
+ * Rendering is a pure function of the configured command plus the frozen
2946
+ * `firstTaskHint` fact, so every re-render of the same acknowledgement produces
2947
+ * the same bytes for the digest-bound post ledger.
2948
+ *
2949
+ * Note the one remaining input that is not frozen: the text embeds
2950
+ * `settings.slackCommand`. Changing that setting between an acknowledgement
2951
+ * post and a later repair of the same interaction would diverge the digest and
2952
+ * conflict its post operation. The command is deployment configuration that
2953
+ * must already match the registered Slack slash command, so it does not change
2954
+ * under a live installation; a deployment that does rename it should drain
2955
+ * in-flight Slack interactions first.
2956
+ */
2957
+ function slackFirstTaskHintText(deps: ApiRouteDeps): string {
2958
+ const command = deps.settings.slackCommand;
2959
+ return `\n\nFirst time here: reply in this thread to continue this task, or reply \`stop\` to stop it. Start a new top-level DM or run \`${command}\` again for a new task. Run \`${command} info\` any time for the full summary.`;
1413
2960
  }
1414
2961
 
1415
2962
  async function processSlackReactionInboxEntry(
1416
2963
  deps: ApiRouteDeps,
1417
2964
  entry: SlackInteractionInboxEntry,
1418
2965
  ) {
2966
+ // Reaction summon is configured on the installation surface, so the settings,
2967
+ // the connection scopes, and the identity link are all HOME reads.
2968
+ const home = { accountId: entry.accountId, workspaceId: entry.workspaceId } as const;
1419
2969
  const [workspace, connection, link] = await Promise.all([
1420
- getWorkspace(deps.db, entry.workspaceId),
1421
- getConnectionMetadata(deps.db, entry.workspaceId, entry.connectionId, null),
1422
- getSlackBotUserLink(deps.db, entry.workspaceId, entry.connectionId, entry.slackUserId),
2970
+ getWorkspace(deps.db, home.workspaceId),
2971
+ getConnectionMetadata(deps.db, home.workspaceId, entry.connectionId, null),
2972
+ getSlackBotUserLink(deps.db, home.workspaceId, entry.connectionId, entry.slackUserId),
1423
2973
  ]);
1424
2974
  const settings = resolveWorkspaceSlackReactionSummonSettings(workspace?.settings);
1425
2975
  if (
@@ -1433,23 +2983,55 @@ async function processSlackReactionInboxEntry(
1433
2983
  ) {
1434
2984
  return;
1435
2985
  }
1436
- const grant = await getWorkspaceGrant(deps.db, link.subjectId, entry.workspaceId, {
1437
- principalKind: "human_session",
2986
+ // A reaction is never a direct message, so the routing decision here is the
2987
+ // channel's remembered answer, the sole candidate, or the installation's own
2988
+ // workspace. It runs before the provider context read so an unauthorized
2989
+ // subject never causes OpenGeni to read the Slack conversation.
2990
+ const routeResolution = await resolveSlackRouteForEntry(deps, home, entry, link.subjectId, {
2991
+ // A reaction carries no message text of the reacting person, so no prefix.
2992
+ botUserId: null,
2993
+ threadTenancy: null,
2994
+ askEnabled: false,
1438
2995
  });
1439
- if (!grant || grant.accountId !== entry.accountId) {
1440
- throw new SlackInteractionPermanentError("identity_access_revoked");
2996
+ if (routeResolution.kind === "ask") {
2997
+ throw new SlackInteractionPermanentError("slack_route_choice_unavailable");
1441
2998
  }
1442
- if (
1443
- !hasPermission(grant.permissions, "sessions:create") ||
1444
- !hasPermission(grant.permissions, "sessions:control")
1445
- ) {
1446
- throw new SlackInteractionPermanentError("reaction_session_permissions_denied");
2999
+ if (routeResolution.kind === "denied") {
3000
+ const summonClient = await createOpenGeniSlackBotInteractionClient(deps, {
3001
+ ...home,
3002
+ connectionId: entry.connectionId,
3003
+ subjectId: SLACK_INTERACTION_BOT_SUBJECT_ID,
3004
+ });
3005
+ await postSlackRouteRefusal(deps, summonClient, entry, routeResolution);
3006
+ return;
1447
3007
  }
3008
+ let target: { accountId: string; workspaceId: string } = {
3009
+ accountId: routeResolution.accountId,
3010
+ workspaceId: routeResolution.workspaceId,
3011
+ };
3012
+ const authorizeTarget = async () => {
3013
+ const resolved = await resolveSlackTargetAuthority(deps.db, {
3014
+ subjectId: link.subjectId,
3015
+ targetAccountId: target.accountId,
3016
+ targetWorkspaceId: target.workspaceId,
3017
+ });
3018
+ if (!resolved) {
3019
+ throw new SlackInteractionPermanentError("identity_access_revoked");
3020
+ }
3021
+ if (
3022
+ !hasPermission(resolved.permissions, "sessions:create") ||
3023
+ !hasPermission(resolved.permissions, "sessions:control")
3024
+ ) {
3025
+ throw new SlackInteractionPermanentError("reaction_session_permissions_denied");
3026
+ }
3027
+ return resolved;
3028
+ };
3029
+ let grant = await authorizeTarget();
1448
3030
 
1449
3031
  const clientEventId = `slack:${entry.providerEventId}`;
1450
3032
  const durableInteraction = await getSlackInteractionByClientEventId(
1451
3033
  deps.db,
1452
- entry.workspaceId,
3034
+ target.workspaceId,
1453
3035
  entry.connectionId,
1454
3036
  clientEventId,
1455
3037
  );
@@ -1474,6 +3056,12 @@ async function processSlackReactionInboxEntry(
1474
3056
  ...interaction,
1475
3057
  owningSubjectId: grant.subjectId,
1476
3058
  sessionId: eventSessionId,
3059
+ startMessageLine: slackStartMessageLineForReaction(
3060
+ deps,
3061
+ interaction,
3062
+ eventSessionId,
3063
+ settings.emoji,
3064
+ ),
1477
3065
  });
1478
3066
  if (!boundInteraction) {
1479
3067
  throw new Error("Durable Slack reaction route could not bind its reserved session");
@@ -1481,8 +3069,7 @@ async function processSlackReactionInboxEntry(
1481
3069
  await reopenSlackInteractionDelivery(deps.db, boundInteraction);
1482
3070
  if (shouldRepairAcknowledgement) {
1483
3071
  const client = await createOpenGeniSlackBotInteractionClient(deps, {
1484
- accountId: entry.accountId,
1485
- workspaceId: entry.workspaceId,
3072
+ ...home,
1486
3073
  connectionId: entry.connectionId,
1487
3074
  subjectId: SLACK_INTERACTION_BOT_SUBJECT_ID,
1488
3075
  sessionId: eventSessionId,
@@ -1493,8 +3080,7 @@ async function processSlackReactionInboxEntry(
1493
3080
  }
1494
3081
 
1495
3082
  const client = await createOpenGeniSlackBotInteractionClient(deps, {
1496
- accountId: entry.accountId,
1497
- workspaceId: entry.workspaceId,
3083
+ ...home,
1498
3084
  connectionId: entry.connectionId,
1499
3085
  subjectId: SLACK_INTERACTION_BOT_SUBJECT_ID,
1500
3086
  });
@@ -1513,30 +3099,26 @@ async function processSlackReactionInboxEntry(
1513
3099
  slackChannelId: entry.slackChannelId,
1514
3100
  slackMessageTs: entry.slackMessageTs,
1515
3101
  },
1516
- saveCheckpoint: async (checkpoint) => {
1517
- if (!entry.claimHolderId) {
1518
- throw new Error("Slack reaction inbox checkpoint requires an active claim");
1519
- }
1520
- const saved = await saveSlackInteractionInboxReactionCheckpoint(deps.db, {
1521
- entry,
1522
- claimHolderId: entry.claimHolderId,
1523
- checkpoint,
1524
- });
1525
- if (!saved) throw new Error("Slack reaction inbox checkpoint claim was lost");
1526
- },
1527
3102
  });
1528
- const preparedTask = await prepareSlackReactionTask(deps, client, entry, context);
1529
3103
  const routeKey = slackRouteKey(entry.slackChannelId, context.threadTimestamp);
1530
- const existing = await getSlackInteractionByRoute(
1531
- deps.db,
1532
- entry.workspaceId,
1533
- entry.connectionId,
3104
+ const existing = await getSlackInteractionByConnectionRoute(deps.db, {
3105
+ accountId: home.accountId,
3106
+ connectionId: entry.connectionId,
1534
3107
  routeKey,
1535
- );
3108
+ });
3109
+ if (
3110
+ existing &&
3111
+ (existing.accountId !== target.accountId || existing.workspaceId !== target.workspaceId)
3112
+ ) {
3113
+ // A mapped thread keeps the workspace it was created in.
3114
+ target = { accountId: existing.accountId, workspaceId: existing.workspaceId };
3115
+ grant = await authorizeTarget();
3116
+ }
3117
+ const preparedTask = await prepareSlackReactionTask(deps, client, entry, target, context);
1536
3118
  if (existing?.sessionId) {
1537
3119
  const appendedTask = await remountSlackReactionTaskForSession(
1538
3120
  deps,
1539
- entry.workspaceId,
3121
+ existing.workspaceId,
1540
3122
  existing.sessionId,
1541
3123
  preparedTask,
1542
3124
  );
@@ -1550,8 +3132,11 @@ async function processSlackReactionInboxEntry(
1550
3132
  return;
1551
3133
  }
1552
3134
  const { interaction } = await getOrCreateSlackInteraction(deps.db, {
1553
- accountId: entry.accountId,
1554
- workspaceId: entry.workspaceId,
3135
+ ...target,
3136
+ // A reaction routes exactly like a mention, so it names its workspace the
3137
+ // same way. Resolved after `authorizeTarget`, so a name never reaches
3138
+ // somebody who could not reach the workspace.
3139
+ routedWorkspaceLabel: await routedWorkspaceLabelFor(deps, routeResolution),
1555
3140
  connectionId: entry.connectionId,
1556
3141
  slackTeamId: entry.slackTeamId,
1557
3142
  slackChannelId: entry.slackChannelId,
@@ -1562,10 +3147,11 @@ async function processSlackReactionInboxEntry(
1562
3147
  owningSubjectId: grant.subjectId,
1563
3148
  visibility: "workspace",
1564
3149
  });
3150
+ requireSlackInteractionMatchesTarget(interaction, target);
1565
3151
  if (interaction.sessionId) {
1566
3152
  const appendedTask = await remountSlackReactionTaskForSession(
1567
3153
  deps,
1568
- entry.workspaceId,
3154
+ interaction.workspaceId,
1569
3155
  interaction.sessionId,
1570
3156
  preparedTask,
1571
3157
  );
@@ -1588,27 +3174,44 @@ async function processSlackReactionInboxEntry(
1588
3174
  }
1589
3175
  const preferredModel = await getLatestSessionModelForSubject(
1590
3176
  deps.db,
1591
- entry.workspaceId,
3177
+ interaction.workspaceId,
1592
3178
  grant.subjectId,
1593
3179
  );
1594
3180
  const preparedEntry = slackReactionPreparedEntry(entry, context, preparedTask);
3181
+ // The reacted message and its thread are the accepted text, so a link to
3182
+ // another deployment there gets the same note as one in a mention.
3183
+ const reactionModelContext = slackOtherDeploymentModelContext(deps, preparedEntry.text);
1595
3184
  let session: Awaited<ReturnType<typeof createSessionForRequest>>;
1596
3185
  try {
1597
- session = await createSessionForRequest(deps, grant, entry.workspaceId, {
1598
- requestedSessionId: interaction.sessionReservationId,
1599
- initialMessage: preparedEntry.text,
1600
- instructions: SLACK_SESSION_INSTRUCTIONS,
1601
- // The exact reacted message and bounded containing thread are already in
1602
- // the prompt; do not expose general Slack history tools for this trigger.
1603
- firstPartyMcpTools: resolveFirstPartyMcpToolPolicy(deps.settings).default,
1604
- resources: preparedTask.resources,
1605
- ...(preferredModel ? { model: preferredModel } : {}),
1606
- // Every reaction entry converging on this route must use the same create
1607
- // key. This closes the same-owner multi-event race while the owner check
1608
- // above prevents a different subject from winning creation authority.
1609
- idempotencyKey: `slack-interaction:${interaction.id}`,
1610
- clientEventId: `slack:${entry.providerEventId}`,
1611
- });
3186
+ const defaults = await slackNewSessionDefaults(
3187
+ deps,
3188
+ grant,
3189
+ interaction,
3190
+ `slack-interaction:${interaction.id}`,
3191
+ );
3192
+ session = await createSessionForRequest(
3193
+ await withCatalogSettings(deps, grant),
3194
+ grant,
3195
+ interaction.workspaceId,
3196
+ {
3197
+ ...defaults,
3198
+ requestedSessionId: interaction.sessionReservationId,
3199
+ initialMessage: preparedEntry.text,
3200
+ ...(reactionModelContext ? { modelContext: reactionModelContext } : {}),
3201
+ instructions: SLACK_SESSION_INSTRUCTIONS,
3202
+ // Reaction context stays bounded: connectors and first-party tools
3203
+ // follow the workspace defaults, without adding the Slack read tools.
3204
+ resources: [...defaults.resources, ...preparedTask.resources],
3205
+ ...(!defaults.model && preferredModel ? { model: preferredModel } : {}),
3206
+ // Every reaction entry converging on this route must use the same create
3207
+ // key. This closes the same-owner multi-event race while the owner check
3208
+ // above prevents a different subject from winning creation authority.
3209
+ idempotencyKey: `slack-interaction:${interaction.id}`,
3210
+ clientEventId: `slack:${entry.providerEventId}`,
3211
+ },
3212
+ undefined,
3213
+ { surface: "slack" },
3214
+ );
1612
3215
  // The route-wide create key converges every replica on one reserved
1613
3216
  // session, but its first writer's initial message is the only event created
1614
3217
  // by that operation. Replay this exact Slack event through the normal
@@ -1630,6 +3233,12 @@ async function processSlackReactionInboxEntry(
1630
3233
  ...interaction,
1631
3234
  owningSubjectId: grant.subjectId,
1632
3235
  sessionId: session.id,
3236
+ startMessageLine: slackStartMessageLineForReaction(
3237
+ deps,
3238
+ interaction,
3239
+ session.id,
3240
+ settings.emoji,
3241
+ ),
1633
3242
  });
1634
3243
  if (!bound) throw new Error("Slack reaction route could not bind its durable session");
1635
3244
  await acknowledgeSlackReactionSession(deps, client, bound, settings.emoji);
@@ -1644,11 +3253,32 @@ async function acknowledgeSlackReactionSession(
1644
3253
  if (!interaction.sessionId) {
1645
3254
  throw new Error("Slack reaction acknowledgement requires a bound session");
1646
3255
  }
3256
+ if (interaction.startMessageLine !== null) {
3257
+ // The frozen line already names the emoji and the workspace; a reaction
3258
+ // task has no button, so the message is the line alone.
3259
+ await client.postMessage({
3260
+ operationId: deterministicUuid(`slack-reaction-ack:${interaction.id}:start`),
3261
+ channelId: interaction.slackChannelId,
3262
+ threadTimestamp: interaction.slackThreadTs,
3263
+ text: interaction.startMessageLine,
3264
+ });
3265
+ return;
3266
+ }
3267
+ const started = `OpenGeni started from the :${emoji}: reaction. ${openSessionText(deps, interaction.workspaceId, interaction.sessionId)} If the intended action is unclear, OpenGeni will ask in this thread. Reply here to continue, or reply \`stop\` to stop.`;
3268
+ const rendered = withWorkspaceLine(
3269
+ interaction.routedWorkspaceLabel,
3270
+ interaction.sessionDefaultsLine ? `${started}\n${interaction.sessionDefaultsLine}` : started,
3271
+ );
1647
3272
  await client.postMessage({
1648
- operationId: deterministicUuid(`slack-reaction-ack:${interaction.id}`),
3273
+ operationId: deterministicUuid(
3274
+ slackDefaultsLinePostSeed(
3275
+ interaction,
3276
+ slackPostSeed(interaction, `slack-reaction-ack:${interaction.id}`),
3277
+ ),
3278
+ ),
1649
3279
  channelId: interaction.slackChannelId,
1650
3280
  threadTimestamp: interaction.slackThreadTs,
1651
- text: `OpenGeni started from the :${emoji}: reaction. ${openSessionText(deps, interaction.workspaceId, interaction.sessionId)} If the intended action is unclear, OpenGeni will ask in this thread. Reply here to continue, or reply \`stop\` to stop.`,
3281
+ text: rendered.text,
1652
3282
  });
1653
3283
  }
1654
3284
 
@@ -1675,6 +3305,92 @@ function slackReactionPreparedEntry(
1675
3305
  };
1676
3306
  }
1677
3307
 
3308
+ type SlackNewSessionDefaults = {
3309
+ model?: string;
3310
+ reasoningEffort?: Session["reasoningEffort"];
3311
+ latencyMode?: Session["latencyMode"];
3312
+ resources: RepositoryResourceRef[];
3313
+ tools?: Session["tools"];
3314
+ excludedMcpServerIds?: string[] | undefined;
3315
+ firstPartyMcpTools?: FirstPartyMcpToolName[];
3316
+ firstPartyMcpPermissions?: NonNullable<Session["firstPartyMcpPermissions"]>;
3317
+ variableSetIds?: string[];
3318
+ rigId?: string;
3319
+ sandboxBackend?: Session["sandboxBackend"];
3320
+ targetSandboxId?: string;
3321
+ workingDir?: string;
3322
+ };
3323
+
3324
+ /**
3325
+ * What a new Slack task starts with.
3326
+ *
3327
+ * A Slack message is not the website composer, so the person's last composer
3328
+ * selection (an explicit connector list, a Variable Set, a compute target)
3329
+ * never narrows it. Connectors, first-party tools, Variable Sets, Sandbox
3330
+ * Environment and compute are left to session creation, which applies the
3331
+ * workspace defaults and freezes the person's own connections through the
3332
+ * ordinary delegation snapshot. Repositories are the person's own recently
3333
+ * used repositories that the workspace GitHub App still offers them, attached
3334
+ * best effort. Only an explicitly chosen model carries over, because choosing
3335
+ * a model narrows nothing.
3336
+ *
3337
+ * A create can commit its reserved shell before initial-event acceptance. Keep
3338
+ * that shell's exact selections on retry instead of resolving defaults again.
3339
+ */
3340
+ async function slackNewSessionDefaults(
3341
+ deps: ApiRouteDeps,
3342
+ grant: AccessGrant,
3343
+ interaction: { workspaceId: string; sessionReservationId: string; owningSubjectId: string },
3344
+ createKey: string,
3345
+ ): Promise<SlackNewSessionDefaults> {
3346
+ const pending = await getSessionByCreateIdempotencyKey(
3347
+ deps.db,
3348
+ interaction.workspaceId,
3349
+ createKey,
3350
+ );
3351
+ if (!pending) {
3352
+ const [modelChoice, resources] = await Promise.all([
3353
+ getActorNewSessionModelChoice(deps, grant, interaction.workspaceId),
3354
+ slackRecentRepositoryResources(deps, grant, interaction.workspaceId),
3355
+ ]);
3356
+ return { ...modelChoice, resources };
3357
+ }
3358
+ if (
3359
+ pending.id !== interaction.sessionReservationId ||
3360
+ interaction.owningSubjectId !== grant.subjectId ||
3361
+ pending.createdBy.kind !== "subject" ||
3362
+ pending.createdBy.subjectId !== grant.subjectId
3363
+ ) {
3364
+ throw new SlackInteractionPermanentError("slack_session_create_identity_mismatch");
3365
+ }
3366
+ return {
3367
+ model: pending.model,
3368
+ reasoningEffort: pending.reasoningEffort,
3369
+ latencyMode: pending.latencyMode,
3370
+ resources: pending.resources.filter(
3371
+ (resource): resource is RepositoryResourceRef => resource.kind === "repository",
3372
+ ),
3373
+ ...(pending.toolPolicy.mode === "explicit"
3374
+ ? { tools: pending.tools }
3375
+ : {
3376
+ excludedMcpServerIds: pending.toolPolicy.excludedMcpServerIds,
3377
+ }),
3378
+ firstPartyMcpTools: pending.firstPartyMcpTools,
3379
+ ...(pending.firstPartyMcpPermissions
3380
+ ? { firstPartyMcpPermissions: pending.firstPartyMcpPermissions }
3381
+ : {}),
3382
+ variableSetIds: pending.variableSetIds,
3383
+ ...(pending.rigId ? { rigId: pending.rigId } : {}),
3384
+ sandboxBackend: pending.sandboxBackend,
3385
+ ...(pending.sandboxBackend === "selfhosted" && pending.activeSandboxId
3386
+ ? {
3387
+ targetSandboxId: pending.activeSandboxId,
3388
+ ...(pending.workingDir ? { workingDir: pending.workingDir } : {}),
3389
+ }
3390
+ : {}),
3391
+ };
3392
+ }
3393
+
1678
3394
  async function remountSlackReactionTaskForSession(
1679
3395
  deps: ApiRouteDeps,
1680
3396
  workspaceId: string,
@@ -1722,8 +3438,9 @@ export function slackReactionTaskText(
1722
3438
  .slice(0, MAX_SLACK_REACTION_CONTEXT_MESSAGES)
1723
3439
  .filter((message) => message.timestamp !== context.reactedMessage.timestamp)
1724
3440
  .map((message) => slackReactionMessageLine(message, false));
1725
- const truncationNotice =
1726
- "The containing thread was truncated at the bounded Slack context limit.";
3441
+ const truncationNotice = context.contextUnavailable
3442
+ ? "Slack's rate limit prevented loading surrounding thread history. Work from the exact reacted message and ask for any missing context."
3443
+ : "The containing thread was truncated at the bounded Slack context limit.";
1727
3444
  let prompt = [
1728
3445
  "A linked, authorized Slack user explicitly summoned OpenGeni by reacting to one message.",
1729
3446
  "Use only the exact reacted message and bounded containing-thread context below.",
@@ -1761,16 +3478,25 @@ async function prepareSlackReactionTask(
1761
3478
  deps: ApiRouteDeps,
1762
3479
  client: OpenGeniSlackBotClient,
1763
3480
  entry: SlackInteractionInboxEntry,
3481
+ target: { accountId: string; workspaceId: string },
1764
3482
  context: SlackReactionMessageContext,
1765
3483
  ): Promise<PreparedSlackReactionTask> {
1766
- return await prepareSlackMessageAttachments(deps, client, entry, context.reactedMessage.files);
3484
+ return await prepareSlackMessageAttachments(
3485
+ deps,
3486
+ client,
3487
+ entry,
3488
+ target,
3489
+ context.reactedMessage.files,
3490
+ );
1767
3491
  }
1768
3492
 
1769
3493
  async function prepareSlackMessageAttachments(
1770
3494
  deps: ApiRouteDeps,
1771
3495
  client: OpenGeniSlackBotClient,
1772
3496
  entry: SlackInteractionInboxEntry,
3497
+ target: { accountId: string; workspaceId: string },
1773
3498
  exactFiles: readonly { id: string; name: string; title: string }[],
3499
+ authorizeSharedRead?: () => Promise<void>,
1774
3500
  ): Promise<PreparedSlackReactionTask> {
1775
3501
  if (exactFiles.length === 0) {
1776
3502
  return { resources: [], attachments: [], omissionCodes: [], omittedCount: 0 };
@@ -1790,12 +3516,13 @@ async function prepareSlackMessageAttachments(
1790
3516
  name: file.name,
1791
3517
  title: file.title,
1792
3518
  })),
3519
+ ...(authorizeSharedRead ? { authorizeSharedRead } : {}),
1793
3520
  });
1794
3521
  const downloaded: Awaited<ReturnType<OpenGeniSlackBotClient["downloadReactionImage"]>>[] = [];
1795
3522
  let aggregateBytes = 0;
1796
3523
  for (const image of prepared) {
1797
3524
  try {
1798
- const value = await client.downloadReactionImage(image);
3525
+ const value = await client.downloadReactionImage(image, authorizeSharedRead);
1799
3526
  if (
1800
3527
  value.bytes.byteLength > SLACK_REACTION_IMAGE_MAX_BYTES ||
1801
3528
  aggregateBytes + value.bytes.byteLength > MAX_SLACK_REACTION_IMAGE_AGGREGATE_BYTES
@@ -1828,8 +3555,8 @@ async function prepareSlackMessageAttachments(
1828
3555
  await importSlackReactionImage(
1829
3556
  { db: deps.db, objectStorage: deps.objectStorage },
1830
3557
  {
1831
- accountId: entry.accountId,
1832
- workspaceId: entry.workspaceId,
3558
+ accountId: target.accountId,
3559
+ workspaceId: target.workspaceId,
1833
3560
  connectionId: entry.connectionId,
1834
3561
  slackTeamId: entry.slackTeamId,
1835
3562
  slackChannelId: entry.slackChannelId,
@@ -1957,7 +3684,10 @@ async function continueSlackReactionSession(
1957
3684
  throw new SlackInteractionPermanentError("session_owner_mismatch");
1958
3685
  }
1959
3686
  await reopenSlackInteractionDelivery(deps.db, interaction);
1960
- await acceptSlackReactionTask(deps, grant, interaction.sessionId, entry, resources);
3687
+ const sessionId = interaction.sessionId;
3688
+ await withSlackAllowanceAdmissionNotice(deps, entry, interaction, async () => {
3689
+ await acceptSlackReactionTask(deps, grant, sessionId, entry, resources);
3690
+ });
1961
3691
  }
1962
3692
 
1963
3693
  async function acceptSlackReactionTask(
@@ -1968,9 +3698,11 @@ async function acceptSlackReactionTask(
1968
3698
  resources: FileResourceRef[],
1969
3699
  ) {
1970
3700
  const clientEventId = `slack:${entry.providerEventId}`;
3701
+ // Session events and user messages belong to the workspace that owns the
3702
+ // session, which is the workspace the grant authorized, not the installation.
1971
3703
  const existing = await getSessionEventByClientEventId(
1972
3704
  deps.db,
1973
- entry.workspaceId,
3705
+ grant.workspaceId,
1974
3706
  sessionId,
1975
3707
  clientEventId,
1976
3708
  );
@@ -1980,11 +3712,20 @@ async function acceptSlackReactionTask(
1980
3712
  }
1981
3713
  return;
1982
3714
  }
1983
- await acceptSessionUserMessage(deps, grant, entry.workspaceId, sessionId, {
1984
- text: entry.text,
1985
- resources,
1986
- clientEventId,
1987
- });
3715
+ const modelContext = slackOtherDeploymentModelContext(deps, entry.text);
3716
+ await acceptSessionUserMessage(
3717
+ await withCatalogSettings(deps, grant),
3718
+ grant,
3719
+ grant.workspaceId,
3720
+ sessionId,
3721
+ {
3722
+ text: entry.text,
3723
+ ...(modelContext ? { modelContext } : {}),
3724
+ resources,
3725
+ clientEventId,
3726
+ surface: "slack",
3727
+ },
3728
+ );
1988
3729
  }
1989
3730
 
1990
3731
  async function continueSlackSession(
@@ -1993,6 +3734,7 @@ async function continueSlackSession(
1993
3734
  interaction: SlackInteraction,
1994
3735
  entry: SlackInteractionInboxEntry,
1995
3736
  resources: FileResourceRef[] = [],
3737
+ options: { modelContext?: string | null } = {},
1996
3738
  ) {
1997
3739
  if (
1998
3740
  !interaction.sessionId ||
@@ -2000,6 +3742,7 @@ async function continueSlackSession(
2000
3742
  ) {
2001
3743
  throw new SlackInteractionPermanentError("session_owner_mismatch");
2002
3744
  }
3745
+ const sessionId = interaction.sessionId;
2003
3746
  await reopenSlackInteractionDelivery(deps.db, interaction);
2004
3747
  if (entry.text.trim().toLowerCase() === "stop") {
2005
3748
  if (!hasPermission(grant.permissions, "sessions:control")) {
@@ -2019,11 +3762,12 @@ async function continueSlackSession(
2019
3762
  reason: "Stopped from the originating Slack thread",
2020
3763
  },
2021
3764
  );
3765
+ await showSlackStartMessageStopped(deps, grant, interaction, entry);
2022
3766
  return;
2023
3767
  }
2024
3768
  const pending = await listSessionHumanInputRequests(
2025
3769
  deps.db,
2026
- entry.workspaceId,
3770
+ interaction.workspaceId,
2027
3771
  interaction.sessionId,
2028
3772
  { status: "pending", limit: 2 },
2029
3773
  );
@@ -2039,13 +3783,15 @@ async function continueSlackSession(
2039
3783
  respondedBy: grant.subjectId,
2040
3784
  clientEventId: `slack:${entry.providerEventId}`,
2041
3785
  });
2042
- if (accepted.action === "accepted") {
2043
- await publishDurableSessionEvents(
2044
- deps.bus,
2045
- grant.workspaceId,
2046
- interaction.sessionId,
2047
- accepted.events,
2048
- );
3786
+ if (accepted.action === "accepted" || accepted.action === "completed") {
3787
+ if (accepted.events.length > 0) {
3788
+ await publishDurableSessionEvents(
3789
+ deps.bus,
3790
+ grant.workspaceId,
3791
+ interaction.sessionId,
3792
+ accepted.events,
3793
+ );
3794
+ }
2049
3795
  if (accepted.workflowWakeRevision !== null) {
2050
3796
  await deps.workflowClient.signalApprovalDecision({
2051
3797
  accountId: grant.accountId,
@@ -2056,20 +3802,64 @@ async function continueSlackSession(
2056
3802
  workflowWakeRevision: accepted.workflowWakeRevision,
2057
3803
  });
2058
3804
  }
2059
- return;
3805
+ const committedOutcome = accepted.request.response?.outcome;
3806
+ if (committedOutcome === "answered" || committedOutcome === "skipped") return;
2060
3807
  }
2061
3808
  }
2062
3809
  }
2063
3810
  if (!hasPermission(grant.permissions, "sessions:control")) {
2064
3811
  throw new SlackInteractionPermanentError("sessions_control_denied");
2065
3812
  }
2066
- await acceptSessionUserMessage(deps, grant, entry.workspaceId, interaction.sessionId, {
2067
- text: entry.text,
2068
- resources,
2069
- clientEventId: `slack:${entry.providerEventId}`,
3813
+ await withSlackAllowanceAdmissionNotice(deps, entry, interaction, async () => {
3814
+ await acceptSessionUserMessage(
3815
+ await withCatalogSettings(deps, grant),
3816
+ grant,
3817
+ interaction.workspaceId,
3818
+ sessionId,
3819
+ {
3820
+ text: entry.text,
3821
+ ...(options.modelContext ? { modelContext: options.modelContext } : {}),
3822
+ resources,
3823
+ clientEventId: `slack:${entry.providerEventId}`,
3824
+ surface: "slack",
3825
+ },
3826
+ );
2070
3827
  });
2071
3828
  }
2072
3829
 
3830
+ /** Existing-thread allowance refusals are permanent admission results, not
3831
+ * silent inbox failures. Other source/authority failures retain their behavior.
3832
+ */
3833
+ async function withSlackAllowanceAdmissionNotice(
3834
+ deps: ApiRouteDeps,
3835
+ entry: SlackInteractionInboxEntry,
3836
+ destination: Pick<SlackInteraction, "slackChannelId" | "slackThreadTs">,
3837
+ admit: () => Promise<void>,
3838
+ ) {
3839
+ try {
3840
+ await admit();
3841
+ } catch (error) {
3842
+ if (
3843
+ error instanceof HTTPException &&
3844
+ (parseAllowanceExhaustedRefusal(error.cause) ?? parseAllowanceExhaustedRefusal(error))
3845
+ ) {
3846
+ const client = await createOpenGeniSlackBotInteractionClient(deps, {
3847
+ accountId: entry.accountId,
3848
+ workspaceId: entry.workspaceId,
3849
+ connectionId: entry.connectionId,
3850
+ subjectId: SLACK_INTERACTION_BOT_SUBJECT_ID,
3851
+ });
3852
+ await client.postMessage({
3853
+ operationId: deterministicUuid(`slack-allowance-admission-failed:${entry.id}`),
3854
+ channelId: destination.slackChannelId,
3855
+ threadTimestamp: destination.slackThreadTs,
3856
+ text: slackAdmissionFailureText(error),
3857
+ });
3858
+ }
3859
+ throw error;
3860
+ }
3861
+ }
3862
+
2073
3863
  const SLACK_ACTION_ID_BY_KIND: Record<SlackInteractionActionKind, string> = {
2074
3864
  approval_approve: "opengeni.approval.approve",
2075
3865
  approval_reject: "opengeni.approval.reject",
@@ -2085,6 +3875,9 @@ async function processSlackBlockAction(deps: ApiRouteDeps, entry: SlackInteracti
2085
3875
  const separator = entry.text.lastIndexOf(":");
2086
3876
  const actionId = separator > 0 ? entry.text.slice(0, separator) : "";
2087
3877
  const handleId = separator > 0 ? entry.text.slice(separator + 1) : "";
3878
+ // The block-action inbox row carries HOME tenancy, exactly like every other
3879
+ // inbox row: it is the installation binding that received the click.
3880
+ const home = { accountId: entry.accountId, workspaceId: entry.workspaceId } as const;
2088
3881
  const route = await resolveSlackInstallationRoute(deps.db, entry.slackTeamId);
2089
3882
  if (
2090
3883
  !route ||
@@ -2094,19 +3887,39 @@ async function processSlackBlockAction(deps: ApiRouteDeps, entry: SlackInteracti
2094
3887
  ) {
2095
3888
  throw new SlackInteractionPermanentError("slack_action_installation_changed");
2096
3889
  }
3890
+ // A route choice is not an action handle: the picker exists BEFORE any
3891
+ // session, so there is no handle row to load and the ordinary path below
3892
+ // would refuse it. Branch first.
3893
+ if (actionId === "opengeni.route.select" || actionId === "opengeni.route.cancel") {
3894
+ await handleSlackRouteChoice(deps, home, entry, actionId, handleId);
3895
+ return;
3896
+ }
3897
+ // A handle lives in the workspace that owns its session, which is not the
3898
+ // installation's workspace once routing is on. Learn that scope from the
3899
+ // content-free probe, then read the full handle under it: every authorization
3900
+ // check below is unchanged and still runs in the handle's own tenancy.
3901
+ const handleTenancy =
3902
+ (await probeSlackActionHandleTenancy(deps.db, {
3903
+ accountId: home.accountId,
3904
+ connectionId: entry.connectionId,
3905
+ handleId,
3906
+ })) ?? home;
2097
3907
  const handle = await getSlackInteractionActionHandle(deps.db, {
2098
- accountId: entry.accountId,
2099
- workspaceId: entry.workspaceId,
3908
+ ...handleTenancy,
2100
3909
  handleId,
2101
3910
  });
2102
3911
  if (!handle || SLACK_ACTION_ID_BY_KIND[handle.actionKind] !== actionId) {
2103
3912
  throw new SlackInteractionPermanentError("slack_action_handle_invalid");
2104
3913
  }
3914
+ // An action handle is a TARGET fact: it is composite-FK'd to its interaction
3915
+ // and its RESTRICTIVE session-visibility policy resolves the session under the
3916
+ // handle's own tenancy, so every read, settle and interaction lookup below
3917
+ // uses the handle's scope rather than the installation's.
3918
+ const target = { accountId: handle.accountId, workspaceId: handle.workspaceId } as const;
2105
3919
  if (handle.status !== "pending") return;
2106
3920
  if (handle.expiresAt.getTime() <= Date.now()) {
2107
3921
  await settleSlackInteractionActionHandles(deps.db, {
2108
- accountId: entry.accountId,
2109
- workspaceId: entry.workspaceId,
3922
+ ...target,
2110
3923
  handleId: handle.id,
2111
3924
  result: "expired",
2112
3925
  stale: true,
@@ -2118,14 +3931,15 @@ async function processSlackBlockAction(deps: ApiRouteDeps, entry: SlackInteracti
2118
3931
  }
2119
3932
  const [interaction, link, post] = await Promise.all([
2120
3933
  getSlackInteractionById(deps.db, {
2121
- accountId: entry.accountId,
2122
- workspaceId: entry.workspaceId,
3934
+ ...target,
2123
3935
  interactionId: handle.interactionId,
2124
3936
  }),
2125
- getSlackBotUserLink(deps.db, entry.workspaceId, entry.connectionId, entry.slackUserId),
3937
+ // Identity stays HOME.
3938
+ getSlackBotUserLink(deps.db, home.workspaceId, entry.connectionId, entry.slackUserId),
3939
+ // The post ledger is written by the bot credential, so it stays HOME too.
2126
3940
  getSlackBotPostOperation(
2127
3941
  deps.db,
2128
- entry.workspaceId,
3942
+ home.workspaceId,
2129
3943
  entry.connectionId,
2130
3944
  handle.messageOperationId,
2131
3945
  ),
@@ -2146,34 +3960,73 @@ async function processSlackBlockAction(deps: ApiRouteDeps, entry: SlackInteracti
2146
3960
  ) {
2147
3961
  throw new SlackInteractionPermanentError("slack_action_authority_changed");
2148
3962
  }
2149
- const grant = await getWorkspaceGrant(deps.db, link.subjectId, entry.workspaceId, {
2150
- principalKind: "human_session",
3963
+ const grant = await resolveSlackTargetAuthority(deps.db, {
3964
+ subjectId: link.subjectId,
3965
+ targetAccountId: target.accountId,
3966
+ targetWorkspaceId: target.workspaceId,
2151
3967
  });
2152
- if (
2153
- !grant ||
2154
- grant.accountId !== entry.accountId ||
2155
- !hasPermission(grant.permissions, "sessions:control")
2156
- ) {
3968
+ if (!grant || !hasPermission(grant.permissions, "sessions:control")) {
2157
3969
  throw new SlackInteractionPermanentError("sessions_control_denied");
2158
3970
  }
2159
3971
  const client = await createOpenGeniSlackBotInteractionClient(deps, {
2160
- accountId: entry.accountId,
2161
- workspaceId: entry.workspaceId,
3972
+ ...home,
2162
3973
  connectionId: entry.connectionId,
2163
3974
  subjectId: grant.subjectId,
2164
3975
  sessionId: handle.sessionId,
2165
3976
  });
2166
3977
  const outcome = await executeSlackAction(deps, grant, interaction, handle);
3978
+ // A compact task's controls live only on its first message. Control buttons
3979
+ // anywhere else on a compact interaction were rendered by an older image during
3980
+ // a rolling deploy, so they keep that format's outcome, including the controls
3981
+ // card that carries Resume.
3982
+ if (
3983
+ interaction.startMessageLine !== null &&
3984
+ (!SLACK_CONTROL_ACTION_KINDS.has(handle.actionKind) ||
3985
+ handle.messageOperationId === slackAcknowledgementOperationId(interaction))
3986
+ ) {
3987
+ await applyCompactSlackActionOutcome(
3988
+ deps,
3989
+ client,
3990
+ { ...interaction, startMessageLine: interaction.startMessageLine },
3991
+ handle,
3992
+ outcome,
3993
+ entry,
3994
+ );
3995
+ return;
3996
+ }
3997
+ // A control click replaces the message it was pressed on. When that message
3998
+ // is this interaction's acknowledgement and this interaction won the one-time
3999
+ // onboarding hint, the update carries the hint forward: otherwise pressing
4000
+ // Status would permanently destroy the only copy of prose a Slack identity is
4001
+ // ever shown. The handle's message operation identifies the acknowledgement
4002
+ // exactly, independent of route rekeying. Later control cards have their own
4003
+ // operation ids, so the hint still appears on exactly one message.
4004
+ const onAcknowledgement =
4005
+ handle.messageOperationId === slackAcknowledgementOperationId(interaction);
4006
+ // Older acknowledgements may still carry a frozen defaults line.
4007
+ const outcomeText =
4008
+ onAcknowledgement && interaction.sessionDefaultsLine
4009
+ ? `${outcome.text}\n${interaction.sessionDefaultsLine}`
4010
+ : outcome.text;
4011
+ const updateText =
4012
+ interaction.firstTaskHint === true && onAcknowledgement
4013
+ ? `${outcomeText}${slackFirstTaskHintText(deps)}`
4014
+ : outcomeText;
4015
+ // Preserve the bytes on those older acknowledgements for update retries.
4016
+ const rendered = withWorkspaceLine(interaction.routedWorkspaceLabel, updateText, [
4017
+ { type: "section", text: { type: "mrkdwn", text: updateText } },
4018
+ ]);
2167
4019
  await client.updateMessage({
2168
- operationId: deterministicUuid(`slack-action-update:${handle.id}:${outcome.result}`),
4020
+ operationId: deterministicUuid(
4021
+ slackPostSeed(interaction, `slack-action-update:${handle.id}:${outcome.result}`),
4022
+ ),
2169
4023
  channelId: entry.slackChannelId,
2170
4024
  timestamp: entry.slackMessageTs,
2171
- text: outcome.text,
2172
- blocks: [{ type: "section", text: { type: "mrkdwn", text: outcome.text } }],
4025
+ text: rendered.text,
4026
+ ...(rendered.blocks ? { blocks: rendered.blocks } : {}),
2173
4027
  });
2174
4028
  await settleSlackInteractionActionHandles(deps.db, {
2175
- accountId: entry.accountId,
2176
- workspaceId: entry.workspaceId,
4029
+ ...target,
2177
4030
  handleId: handle.id,
2178
4031
  result: outcome.result,
2179
4032
  ...(outcome.stale ? { stale: true } : {}),
@@ -2189,6 +4042,260 @@ async function processSlackBlockAction(deps: ApiRouteDeps, entry: SlackInteracti
2189
4042
  }
2190
4043
  }
2191
4044
 
4045
+ /**
4046
+ * Show a compact task's click outcome. Nothing here posts a new message.
4047
+ *
4048
+ * Stop and Resume live on the first message and swap in place: the click's own
4049
+ * lifecycle action has already run, the replacement button is reserved under a
4050
+ * key derived from the pressed handle (so a retry finds the same one), the
4051
+ * message is re-rendered from its frozen line, and only then is the pressed
4052
+ * handle settled, without superseding the replacement it now sits next to.
4053
+ *
4054
+ * Any other card (an approval, a question, a publication) is replaced by the
4055
+ * outcome sentence, with no workspace line. Its update id keeps the
4056
+ * `slackPostSeed` identity, so an unlabelled interaction renders exactly the
4057
+ * bytes an older image would for the same click.
4058
+ */
4059
+ async function applyCompactSlackActionOutcome(
4060
+ deps: ApiRouteDeps,
4061
+ client: OpenGeniSlackBotClient,
4062
+ interaction: SlackInteraction & { startMessageLine: string },
4063
+ handle: SlackInteractionActionHandle,
4064
+ outcome: Awaited<ReturnType<typeof executeSlackAction>>,
4065
+ entry: SlackInteractionInboxEntry,
4066
+ ) {
4067
+ const target = { accountId: handle.accountId, workspaceId: handle.workspaceId } as const;
4068
+ if (
4069
+ (handle.actionKind === "session_pause" || handle.actionKind === "session_resume") &&
4070
+ handle.messageOperationId === slackAcknowledgementOperationId(interaction)
4071
+ ) {
4072
+ await swapSlackStartMessageButton(deps, client, interaction, handle, outcome.result, {
4073
+ channelId: entry.slackChannelId,
4074
+ timestamp: entry.slackMessageTs,
4075
+ });
4076
+ return;
4077
+ }
4078
+ // Exactly the previous format's bytes for an unlabelled interaction.
4079
+ const text = outcome.text;
4080
+ await client.updateMessage({
4081
+ operationId: deterministicUuid(
4082
+ slackPostSeed(interaction, `slack-action-update:${handle.id}:${outcome.result}`),
4083
+ ),
4084
+ channelId: entry.slackChannelId,
4085
+ timestamp: entry.slackMessageTs,
4086
+ text,
4087
+ blocks: [{ type: "section", text: { type: "mrkdwn", text } }],
4088
+ });
4089
+ await settleSlackInteractionActionHandles(deps.db, {
4090
+ ...target,
4091
+ handleId: handle.id,
4092
+ result: outcome.result,
4093
+ ...(outcome.stale ? { stale: true } : {}),
4094
+ });
4095
+ }
4096
+
4097
+ const SLACK_CONTROL_ACTION_KINDS: ReadonlySet<SlackInteractionActionKind> = new Set([
4098
+ "session_status",
4099
+ "session_pause",
4100
+ "session_resume",
4101
+ ]);
4102
+
4103
+ /**
4104
+ * Swap the one button on a compact task's first message after its lifecycle
4105
+ * action ran: Stop becomes Resume (with a `Stopped.` note), Resume becomes Stop.
4106
+ *
4107
+ * The replacement is reserved under a key derived from the pressed handle, so a
4108
+ * retry finds the same one, the message is re-rendered from its frozen line, and
4109
+ * only then is the pressed handle settled, without superseding the replacement
4110
+ * it now sits next to. The update has its own seed: an older image renders the
4111
+ * previous format for the same click under a different id.
4112
+ */
4113
+ async function swapSlackStartMessageButton(
4114
+ deps: ApiRouteDeps,
4115
+ client: OpenGeniSlackBotClient,
4116
+ interaction: SlackInteraction & { startMessageLine: string },
4117
+ pressed: SlackInteractionActionHandle,
4118
+ result: string,
4119
+ message: { channelId: string; timestamp: string },
4120
+ ) {
4121
+ const stopped = pressed.actionKind === "session_pause";
4122
+ const nextKind = stopped ? "session_resume" : "session_pause";
4123
+ const next = await reserveSlackStartMessageButton(deps, interaction, pressed.messageOperationId, {
4124
+ actionKind: nextKind,
4125
+ actionKey: `${pressed.messageOperationId}:${nextKind}:${pressed.id}`,
4126
+ });
4127
+ const rendered = slackStartMessage(deps, interaction.startMessageLine, {
4128
+ hint: interaction.firstTaskHint === true,
4129
+ stopped,
4130
+ button: next,
4131
+ });
4132
+ await client.updateMessage({
4133
+ operationId: deterministicUuid(`slack-start-update:${pressed.id}:${result}`),
4134
+ channelId: message.channelId,
4135
+ timestamp: message.timestamp,
4136
+ text: rendered.text,
4137
+ blocks: rendered.blocks,
4138
+ });
4139
+ await settleSlackInteractionActionHandles(deps.db, {
4140
+ accountId: pressed.accountId,
4141
+ workspaceId: pressed.workspaceId,
4142
+ handleId: pressed.id,
4143
+ result,
4144
+ supersedeSiblings: false,
4145
+ });
4146
+ }
4147
+
4148
+ /**
4149
+ * A `stop` reply pauses exactly like the Stop button, so the first message
4150
+ * should say so too: swap its pending Stop to Resume, as a press would.
4151
+ * Nothing to do for the previous format, a task without a button (a reaction),
4152
+ * or a first message that already shows Resume or has settled.
4153
+ *
4154
+ * The pause has already committed, so a first message Slack no longer has does
4155
+ * not fail the reply.
4156
+ */
4157
+ async function showSlackStartMessageStopped(
4158
+ deps: ApiRouteDeps,
4159
+ grant: AccessGrant,
4160
+ interaction: SlackInteraction,
4161
+ entry: SlackInteractionInboxEntry,
4162
+ ) {
4163
+ if (interaction.startMessageLine === null || !interaction.sessionId) return;
4164
+ const operationId = slackAcknowledgementOperationId(interaction);
4165
+ const stop = (
4166
+ await listPendingSlackInteractionMessageActionHandles(deps.db, {
4167
+ accountId: interaction.accountId,
4168
+ workspaceId: interaction.workspaceId,
4169
+ interactionId: interaction.id,
4170
+ messageOperationId: operationId,
4171
+ })
4172
+ ).find((handle) => handle.actionKind === "session_pause");
4173
+ if (!stop) return;
4174
+ // The first message is written by the bot credential, so its ledger row is HOME.
4175
+ const post = await getSlackBotPostOperation(
4176
+ deps.db,
4177
+ entry.workspaceId,
4178
+ entry.connectionId,
4179
+ operationId,
4180
+ );
4181
+ if (post?.status !== "completed" || !post.slackChannelId || !post.slackMessageTimestamp) {
4182
+ return;
4183
+ }
4184
+ const client = await createOpenGeniSlackBotInteractionClient(deps, {
4185
+ accountId: entry.accountId,
4186
+ workspaceId: entry.workspaceId,
4187
+ connectionId: entry.connectionId,
4188
+ subjectId: grant.subjectId,
4189
+ sessionId: interaction.sessionId,
4190
+ });
4191
+ try {
4192
+ await swapSlackStartMessageButton(
4193
+ deps,
4194
+ client,
4195
+ { ...interaction, startMessageLine: interaction.startMessageLine },
4196
+ stop,
4197
+ "paused",
4198
+ { channelId: post.slackChannelId, timestamp: post.slackMessageTimestamp },
4199
+ );
4200
+ } catch (error) {
4201
+ if (!permanentSlackStartMessageUpdateError(error)) throw error;
4202
+ console.warn("[slack-interactions] first message not marked stopped", {
4203
+ code: slackDeliveryErrorCode(error),
4204
+ });
4205
+ }
4206
+ }
4207
+
4208
+ /**
4209
+ * Take the button off a compact task's first message once the task settles.
4210
+ *
4211
+ * Called from the delivery pump when an interaction reaches a terminal
4212
+ * delivery state, before that state commits, so a transient failure retries
4213
+ * with the delivery. The update id is derived from the pending handle it
4214
+ * retires, so a retry reproduces the same operation and a later settle (after a
4215
+ * follow-up turn put nothing back) finds no pending handle and does nothing.
4216
+ *
4217
+ * The first message's own acknowledgement calls it too, once its post
4218
+ * completes, for a task that settled while that post was still being retried:
4219
+ * the pump found nothing posted to update then.
4220
+ *
4221
+ * A first message Slack no longer has, or that can no longer be updated, is not
4222
+ * worth failing a delivery over: the handle is retired anyway.
4223
+ */
4224
+ async function retireSlackStartMessageButton(
4225
+ deps: ApiRouteDeps,
4226
+ client: OpenGeniSlackBotClient,
4227
+ claimed: SlackInteraction,
4228
+ home: { accountId: string; workspaceId: string },
4229
+ ) {
4230
+ if (claimed.startMessageLine === null) return;
4231
+ // Re-read: the caller's snapshot can predate the frozen hint decision, and the
4232
+ // settle update must render the same bytes on every attempt.
4233
+ const interaction =
4234
+ (await getSlackInteractionById(deps.db, {
4235
+ accountId: claimed.accountId,
4236
+ workspaceId: claimed.workspaceId,
4237
+ interactionId: claimed.id,
4238
+ })) ?? claimed;
4239
+ if (interaction.startMessageLine === null) return;
4240
+ const operationId = slackAcknowledgementOperationId(interaction);
4241
+ const pending = await listPendingSlackInteractionMessageActionHandles(deps.db, {
4242
+ accountId: interaction.accountId,
4243
+ workspaceId: interaction.workspaceId,
4244
+ interactionId: interaction.id,
4245
+ messageOperationId: operationId,
4246
+ });
4247
+ const first = pending[0];
4248
+ if (!first) return;
4249
+ // The first message is written by the bot credential, so its ledger row is HOME.
4250
+ const post = await getSlackBotPostOperation(
4251
+ deps.db,
4252
+ home.workspaceId,
4253
+ interaction.connectionId,
4254
+ operationId,
4255
+ );
4256
+ // Not posted yet: leave the button alone rather than retire it under a
4257
+ // message that may still appear with it.
4258
+ if (post?.status !== "completed" || !post.slackChannelId || !post.slackMessageTimestamp) {
4259
+ return;
4260
+ }
4261
+ const message = slackStartMessage(deps, interaction.startMessageLine, {
4262
+ hint: interaction.firstTaskHint === true,
4263
+ stopped: false,
4264
+ button: null,
4265
+ });
4266
+ try {
4267
+ await client.updateMessage({
4268
+ operationId: deterministicUuid(`slack-start-settled:${first.id}`),
4269
+ channelId: post.slackChannelId,
4270
+ timestamp: post.slackMessageTimestamp,
4271
+ text: message.text,
4272
+ blocks: message.blocks,
4273
+ });
4274
+ } catch (error) {
4275
+ if (!permanentSlackStartMessageUpdateError(error)) throw error;
4276
+ console.warn("[slack-interactions] first message button not retired", {
4277
+ code: slackDeliveryErrorCode(error),
4278
+ });
4279
+ }
4280
+ for (const handle of pending) {
4281
+ await settleSlackInteractionActionHandles(deps.db, {
4282
+ accountId: handle.accountId,
4283
+ workspaceId: handle.workspaceId,
4284
+ handleId: handle.id,
4285
+ result: "task_settled",
4286
+ stale: true,
4287
+ });
4288
+ }
4289
+ }
4290
+
4291
+ function permanentSlackStartMessageUpdateError(error: unknown): boolean {
4292
+ return (
4293
+ permanentSlackDeliveryError(error) ||
4294
+ (error instanceof SlackBotProviderError &&
4295
+ (error.code === "cant_update_message" || error.code === "edit_window_closed"))
4296
+ );
4297
+ }
4298
+
2192
4299
  async function executeSlackAction(
2193
4300
  deps: ApiRouteDeps,
2194
4301
  grant: AccessGrant,
@@ -2296,19 +4403,21 @@ async function executeSlackAction(
2296
4403
  respondedBy: grant.subjectId,
2297
4404
  clientEventId: `slack-action:${handle.id}`,
2298
4405
  });
2299
- if (accepted.action !== "accepted") {
4406
+ if (accepted.action === "not_found" || accepted.action === "conflict") {
2300
4407
  return {
2301
4408
  result: "stale",
2302
4409
  stale: true,
2303
4410
  text: `${mention}This question is no longer pending.`,
2304
4411
  };
2305
4412
  }
2306
- await publishDurableSessionEvents(
2307
- deps.bus,
2308
- grant.workspaceId,
2309
- handle.sessionId,
2310
- accepted.events,
2311
- );
4413
+ if (accepted.events.length > 0) {
4414
+ await publishDurableSessionEvents(
4415
+ deps.bus,
4416
+ grant.workspaceId,
4417
+ handle.sessionId,
4418
+ accepted.events,
4419
+ );
4420
+ }
2312
4421
  if (accepted.workflowWakeRevision !== null) {
2313
4422
  await deps.workflowClient.signalApprovalDecision({
2314
4423
  accountId: grant.accountId,
@@ -2319,9 +4428,17 @@ async function executeSlackAction(
2319
4428
  workflowWakeRevision: accepted.workflowWakeRevision,
2320
4429
  });
2321
4430
  }
4431
+ const committedOutcome = accepted.request.response?.outcome ?? response.outcome;
4432
+ if (committedOutcome !== "answered" && committedOutcome !== "skipped") {
4433
+ return {
4434
+ result: "stale",
4435
+ stale: true,
4436
+ text: `${mention}This question is already ${committedOutcome}.`,
4437
+ };
4438
+ }
2322
4439
  return {
2323
- result: response.outcome === "skipped" ? "skipped" : "answered",
2324
- text: `${mention}${response.outcome === "skipped" ? "Skipped the question" : "Answer submitted"}. OpenGeni will continue.`,
4440
+ result: committedOutcome === "skipped" ? "skipped" : "answered",
4441
+ text: `${mention}${committedOutcome === "skipped" ? "Skipped the question" : "Answer submitted"}. OpenGeni will continue.`,
2325
4442
  };
2326
4443
  }
2327
4444
  if (handle.actionKind === "session_pause" || handle.actionKind === "session_resume") {
@@ -2348,9 +4465,22 @@ async function executeSlackAction(
2348
4465
  }
2349
4466
  const session = await getSession(deps.db, grant.workspaceId, handle.sessionId);
2350
4467
  if (!session) throw new SlackInteractionPermanentError("slack_action_session_missing");
4468
+ // Status exists only on interactions bound before compact messages; new tasks
4469
+ // have no Status button. Its reply is kept byte for byte so a click an older
4470
+ // image started re-renders identically, including Make recurring, and only
4471
+ // while this exact requester still holds schedule authority in this
4472
+ // workspace. The deep link and the schedules authority behind it are
4473
+ // unchanged: the browser rechecks both.
4474
+ const recurring =
4475
+ hasPermission(grant.permissions, "sessions:read") &&
4476
+ hasPermission(grant.permissions, "scheduled_tasks:manage")
4477
+ ? makeRecurringText(deps, grant.workspaceId, handle.sessionId)
4478
+ : null;
2351
4479
  return {
2352
4480
  result: "status",
2353
- text: `${mention}OpenGeni task status: *${session.status.replaceAll("_", " ")}*.`,
4481
+ text: `${mention}OpenGeni task status: *${session.status.replaceAll("_", " ")}*.${
4482
+ recurring ? `\n\n${recurring}` : ""
4483
+ }`,
2354
4484
  ...(session.status === "cancelled" || session.status === "failed"
2355
4485
  ? {}
2356
4486
  : { controlState: "active" as const }),
@@ -2367,18 +4497,25 @@ async function publishSlackSharedResult(
2367
4497
  if (!handle.targetId || !interaction.sessionId) {
2368
4498
  throw new SlackInteractionPermanentError("slack_shared_publication_target_invalid");
2369
4499
  }
2370
- const [origin, activePolicy, event] = await Promise.all([
4500
+ const publicationHome = await resolveSlackDeliveryHome(deps, interaction);
4501
+ const [origin, event] = await Promise.all([
2371
4502
  getSlackSharedTaskOrigin(deps.db, {
2372
4503
  accountId: interaction.accountId,
2373
4504
  workspaceId: interaction.workspaceId,
2374
4505
  interactionId: interaction.id,
2375
4506
  }),
2376
- getActiveSlackTaskPolicy(deps.db, {
2377
- accountId: interaction.accountId,
2378
- workspaceId: interaction.workspaceId,
2379
- }),
2380
4507
  getSessionEvent(deps.db, interaction.workspaceId, handle.targetId),
2381
4508
  ]);
4509
+ // The Slack task policy governs what may be read out of, and published back
4510
+ // to, the Slack conversation, so it is an installation-surface fact. The
4511
+ // origin froze which tenancy that was; a row written before routing existed
4512
+ // carries null and implied its own, because the two could not differ then.
4513
+ const activePolicy = origin
4514
+ ? await getActiveSlackTaskPolicy(deps.db, {
4515
+ accountId: origin.policyAccountId ?? origin.accountId,
4516
+ workspaceId: origin.policyWorkspaceId ?? origin.workspaceId,
4517
+ })
4518
+ : null;
2382
4519
  if (
2383
4520
  !origin ||
2384
4521
  origin.connectionId !== interaction.connectionId ||
@@ -2400,8 +4537,7 @@ async function publishSlackSharedResult(
2400
4537
  };
2401
4538
  }
2402
4539
  const client = await createOpenGeniSlackBotInteractionClient(deps, {
2403
- accountId: interaction.accountId,
2404
- workspaceId: interaction.workspaceId,
4540
+ ...publicationHome,
2405
4541
  connectionId: interaction.connectionId,
2406
4542
  subjectId: grant.subjectId,
2407
4543
  sessionId: interaction.sessionId,
@@ -2453,13 +4589,16 @@ async function publishSlackSharedResult(
2453
4589
  text: `${mention}This result is no longer available for publication.`,
2454
4590
  };
2455
4591
  }
2456
- await client.postMessage({
2457
- operationId: deterministicUuid(
2458
- `slack-shared-result:${origin.interactionId}:${event.id}:${origin.policyActivationVersion}`,
2459
- ),
2460
- channelId: origin.sourceChannelId,
2461
- threadTimestamp: origin.sourceThreadTs,
2462
- text: `<@${origin.initiatingSlackUserId}> ${boundedOutput(output)}`,
4592
+ const publicationOperationId = deterministicUuid(
4593
+ `slack-shared-result:${origin.interactionId}:${event.id}:${origin.policyActivationVersion}`,
4594
+ );
4595
+ await deliverSlackModelText(async (format) => {
4596
+ await client.postMessage({
4597
+ operationId: publicationOperationId,
4598
+ channelId: origin.sourceChannelId,
4599
+ threadTimestamp: origin.sourceThreadTs,
4600
+ text: `<@${origin.initiatingSlackUserId}> ${boundedOutput(format(output))}`,
4601
+ });
2463
4602
  });
2464
4603
  return {
2465
4604
  result: "published",
@@ -2480,31 +4619,22 @@ async function validatedSlackRequester(
2480
4619
  SlackInteraction,
2481
4620
  "accountId" | "workspaceId" | "connectionId" | "initiatingSlackUserId" | "owningSubjectId"
2482
4621
  >,
4622
+ // Identity links are HOME facts, unique on (connection_id, slack_user_id) and
4623
+ // RLS-visible only under the installation's own tenancy.
4624
+ home: { accountId: string; workspaceId: string },
2483
4625
  ) {
2484
4626
  if (!interaction.initiatingSlackUserId) {
2485
- return { authorized: false, canSchedule: false, mention: "" };
4627
+ return { authorized: false, mention: "" };
2486
4628
  }
2487
- const [link, grant] = await Promise.all([
2488
- getSlackBotUserLink(
2489
- deps.db,
2490
- interaction.workspaceId,
2491
- interaction.connectionId,
2492
- interaction.initiatingSlackUserId,
2493
- ),
2494
- getWorkspaceGrant(deps.db, interaction.owningSubjectId, interaction.workspaceId, {
2495
- principalKind: "human_session",
2496
- }),
2497
- ]);
4629
+ const link = await getSlackBotUserLink(
4630
+ deps.db,
4631
+ home.workspaceId,
4632
+ interaction.connectionId,
4633
+ interaction.initiatingSlackUserId,
4634
+ );
2498
4635
  const authorized = link?.subjectId === interaction.owningSubjectId;
2499
- const canSchedule =
2500
- authorized &&
2501
- grant?.accountId === interaction.accountId &&
2502
- hasPermission(grant.permissions, "sessions:read") &&
2503
- hasPermission(grant.permissions, "sessions:control") &&
2504
- hasPermission(grant.permissions, "scheduled_tasks:manage");
2505
4636
  return {
2506
4637
  authorized,
2507
- canSchedule,
2508
4638
  mention: authorized ? slackRequesterMention(interaction) : "",
2509
4639
  };
2510
4640
  }
@@ -2533,13 +4663,17 @@ async function controlActionBlocks(
2533
4663
  actionKey: `${input.messageOperationId}:${actionKind}`,
2534
4664
  })),
2535
4665
  });
2536
- const pending = handles.filter((handle) => handle.status === "pending");
2537
- if (pending.length === 0) return [];
4666
+ // Render every reserved handle, whatever its status. These handles are
4667
+ // reserved fresh for the message's first post, so they are all pending then;
4668
+ // a repair of that post after one was pressed must render the same buttons,
4669
+ // or its bytes differ from the ledger's digest and the repair conflicts. A
4670
+ // pressed or stale handle is refused when clicked, never re-executed.
4671
+ if (handles.length === 0) return [];
2538
4672
  return [
2539
4673
  {
2540
4674
  type: "actions",
2541
4675
  block_id: `opengeni_controls_${input.messageOperationId}`,
2542
- elements: pending.map((handle) => ({
4676
+ elements: handles.map((handle) => ({
2543
4677
  type: "button" as const,
2544
4678
  action_id: SLACK_ACTION_ID_BY_KIND[handle.actionKind],
2545
4679
  value: handle.id,
@@ -2610,7 +4744,7 @@ async function slackApprovalCard(
2610
4744
  requesterAuthorized: boolean,
2611
4745
  ): Promise<{ text: string; blocks?: SlackMessageBlock[]; operationId: string }> {
2612
4746
  const operationId = deterministicUuid(
2613
- `slack-delivery:${interaction.id}:${event.sequence}:approval`,
4747
+ slackPostSeed(interaction, `slack-delivery:${interaction.id}:${event.sequence}:approval`),
2614
4748
  );
2615
4749
  const approvals = slackApprovalSummaries(event.payload);
2616
4750
  const fallback = approvals.length
@@ -2763,7 +4897,7 @@ async function slackHumanInputCard(
2763
4897
  );
2764
4898
  if (!request || request.status !== "pending") return null;
2765
4899
  const operationId = deterministicUuid(
2766
- `slack-delivery:${interaction.id}:${event.sequence}:human-input`,
4900
+ slackPostSeed(interaction, `slack-delivery:${interaction.id}:${event.sequence}:human-input`),
2767
4901
  );
2768
4902
  const text = `${mention}OpenGeni needs your input:\n${formatQuestions(request.questions)}\nReply in this thread${request.allowSkip ? " or choose Skip" : ""}.`;
2769
4903
  if (!requesterAuthorized || !interaction.initiatingSlackUserId) return { text, operationId };
@@ -2831,6 +4965,157 @@ async function slackHumanInputCard(
2831
4965
  return { text, blocks, operationId };
2832
4966
  }
2833
4967
 
4968
+ /**
4969
+ * A child of a Slack-originated session is never itself mapped to a Slack
4970
+ * thread, so the parent's bounded `child_requires_action` notice is the only
4971
+ * path that can tell the human that a worker they started is blocked. The card
4972
+ * is a pointer, not a second question card: one bounded preview of the first
4973
+ * question (or the waiting approval count) plus the child link. The child's own
4974
+ * OpenGeni card remains the place the human actually answers.
4975
+ *
4976
+ * Every deferred child lifecycle kind (`child_progress`,
4977
+ * `child_waiting_capacity`, `child_requires_action_resolved`, `child_paused`)
4978
+ * returns null here so Slack stays quiet, and so does every non-child machine
4979
+ * input that shares the `system.update.pending` event type.
4980
+ *
4981
+ * The caller has already proven that this workspace turned the notice on. The
4982
+ * card defaults off per workspace because the in-app rail and priority feed
4983
+ * already surface a blocked child.
4984
+ */
4985
+ /**
4986
+ * The exact durable notice behind one `system.update.pending` event, or null
4987
+ * when it no longer describes a blocked worker.
4988
+ *
4989
+ * Slack delivery runs behind the session: a widened retry window, a replica
4990
+ * claim, or simply a later page can reach this event after the child already
4991
+ * got its answer. Two facts have to agree before a card is worth posting.
4992
+ *
4993
+ * A resolution supersedes a still-`pending` notice in the same commit, so
4994
+ * `superseded` (and an explicitly `cancelled` notice) is already a "no longer
4995
+ * blocked" fact. But a notice the parent's turn has claimed is `delivered` and
4996
+ * keeps that state forever, so state alone is not enough - the resolution row
4997
+ * for that exact (child, turn, generation) boundary is checked as well, on the
4998
+ * parent's own rows. A re-freeze is a new generation and a new notice.
4999
+ *
5000
+ * Every read is wrapped: a malformed or unreadable durable row is not a
5001
+ * delivery failure. Throwing would burn a delivery attempt and, after
5002
+ * MAX_DELIVERY_ATTEMPTS, close this interaction's whole delivery over one
5003
+ * notice nobody could have posted anyway. Silence is the correct outcome, and
5004
+ * it is the same outcome every other unpostable notice already takes.
5005
+ */
5006
+ async function resolveSlackBlockedChildNotice(
5007
+ deps: ApiRouteDeps,
5008
+ interaction: SlackInteraction,
5009
+ sessionId: string,
5010
+ updateId: string,
5011
+ ): Promise<ChildRequiresActionPayload | null> {
5012
+ try {
5013
+ const update = await getSessionSystemUpdateById(
5014
+ deps.db,
5015
+ interaction.workspaceId,
5016
+ sessionId,
5017
+ updateId,
5018
+ );
5019
+ if (!update || update.payload.type !== "child_requires_action") return null;
5020
+ if (update.state === "superseded" || update.state === "cancelled") return null;
5021
+ const resolved = await childRequiresActionResolutionExists(
5022
+ deps.db,
5023
+ interaction.workspaceId,
5024
+ sessionId,
5025
+ {
5026
+ childSessionId: update.payload.childSessionId,
5027
+ childTurnId: update.payload.childTurnId,
5028
+ childTurnGeneration: update.payload.childTurnGeneration,
5029
+ },
5030
+ );
5031
+ return resolved ? null : update.payload;
5032
+ } catch {
5033
+ return null;
5034
+ }
5035
+ }
5036
+
5037
+ async function slackChildRequiresActionCard(
5038
+ deps: ApiRouteDeps,
5039
+ interaction: SlackInteraction,
5040
+ event: SessionEvent,
5041
+ mention: string,
5042
+ ): Promise<{ text: string } | null> {
5043
+ const sessionId = interaction.sessionId;
5044
+ if (!sessionId) return null;
5045
+ const preview = record(event.payload);
5046
+ if (boundedString(preview?.kind, 64) !== "child_requires_action") return null;
5047
+ const updateId = boundedString(preview?.updateId, 64);
5048
+ if (!updateId || !SLACK_UUID_PATTERN.test(updateId)) return null;
5049
+ // The bounded event preview is lossy by construction. Resolve the exact
5050
+ // durable notice under the ordinary workspace scope; the child session is
5051
+ // never read, only linked.
5052
+ const notice = await resolveSlackBlockedChildNotice(deps, interaction, sessionId, updateId);
5053
+ if (!notice) return null;
5054
+ const questions = notice.requests.filter((request) => request.kind === "human_input");
5055
+ const approvals = notice.requests.filter((request) => request.kind === "approval");
5056
+ const first = questions[0];
5057
+ const detail = first
5058
+ ? `${boundedSlackChildDetail(first.firstQuestion)}${
5059
+ first.questionCount > 1 ? ` (+${first.questionCount - 1} more)` : ""
5060
+ }`
5061
+ : approvals.length > 0
5062
+ ? `${approvals.length} tool approval${approvals.length > 1 ? "s are" : " is"} waiting for a human.`
5063
+ : "";
5064
+ const link = slackSessionUrl(deps, interaction.workspaceId, notice.childSessionId);
5065
+ const lines = [`${mention}A worker you started needs input.`];
5066
+ if (detail) lines.push(`> ${detail}`);
5067
+ if (link) lines.push(`<${link}|Open in OpenGeni>`);
5068
+ return { text: boundedOutput(lines.join("\n")) };
5069
+ }
5070
+
5071
+ /**
5072
+ * A goal that paused because it ran out of budget or hit the continuation cap
5073
+ * stops making progress with nobody watching, so the Slack thread gets one
5074
+ * bounded line. A `user_pause` / `api` / `agent` pause is a decision the human
5075
+ * or their agent already made, and `no_progress` is not a stop the human must
5076
+ * act on, so none of them post. `goal.resumed` never posts.
5077
+ *
5078
+ * The caller has already proven that this workspace turned the notice on; it
5079
+ * defaults off per workspace.
5080
+ */
5081
+ function slackGoalPausedText(
5082
+ deps: ApiRouteDeps,
5083
+ interaction: SlackInteraction,
5084
+ event: SessionEvent,
5085
+ mention: string,
5086
+ ): string | null {
5087
+ if (!interaction.sessionId) return null;
5088
+ const reason = boundedString(record(event.payload)?.reason, 64);
5089
+ const headline = reason ? SLACK_GOAL_PAUSED_HEADLINES.get(reason) : undefined;
5090
+ if (!headline) return null;
5091
+ const link = slackSessionUrl(deps, interaction.workspaceId, interaction.sessionId);
5092
+ return boundedOutput(`${mention}${headline}.${link ? ` <${link}|Open in OpenGeni>` : ""}`);
5093
+ }
5094
+
5095
+ /**
5096
+ * The installation tenancy that owns this interaction's Slack credential.
5097
+ *
5098
+ * `slack_interactions.connection_id` deliberately has no composite
5099
+ * `(workspace_id, connection_id)` foreign key, so a routed interaction may point
5100
+ * at the installation's connection from another workspace.
5101
+ *
5102
+ * A binding that does not resolve, or that now names a different connection, is
5103
+ * NOT turned into a new delivery precondition: this code previously used the
5104
+ * interaction's own tenancy unconditionally, so falling back to it keeps every
5105
+ * delivery that used to succeed succeeding. The fallback cannot post from the
5106
+ * wrong workspace either, because `claimSlackBotPostOperation` selects the
5107
+ * connection under the tenancy it is given and fails when it does not own it.
5108
+ */
5109
+ async function resolveSlackDeliveryHome(
5110
+ deps: ApiRouteDeps,
5111
+ interaction: Pick<SlackInteraction, "accountId" | "workspaceId" | "connectionId" | "slackTeamId">,
5112
+ ): Promise<{ accountId: string; workspaceId: string }> {
5113
+ const installation = await resolveSlackInstallationRoute(deps.db, interaction.slackTeamId);
5114
+ return installation && installation.connectionId === interaction.connectionId
5115
+ ? { accountId: installation.accountId, workspaceId: installation.workspaceId }
5116
+ : { accountId: interaction.accountId, workspaceId: interaction.workspaceId };
5117
+ }
5118
+
2834
5119
  async function deliverSlackSessionEvents(
2835
5120
  deps: ApiRouteDeps,
2836
5121
  interaction: SlackInteraction,
@@ -2850,17 +5135,67 @@ async function deliverSlackSessionEvents(
2850
5135
  });
2851
5136
  return;
2852
5137
  }
5138
+ // The bot credential is owned by the installation's workspace and every
5139
+ // provider call is fenced on it, so the delivery client is always built from
5140
+ // HOME even when the session it reports on lives in another workspace.
5141
+ const home = await resolveSlackDeliveryHome(deps, interaction);
2853
5142
  const client = await createOpenGeniSlackBotInteractionClient(deps, {
2854
- accountId: interaction.accountId,
2855
- workspaceId: interaction.workspaceId,
5143
+ ...home,
2856
5144
  connectionId: interaction.connectionId,
2857
5145
  subjectId: SLACK_INTERACTION_BOT_SUBJECT_ID,
2858
5146
  sessionId: interaction.sessionId,
2859
5147
  });
2860
- const requester = await validatedSlackRequester(deps, interaction);
5148
+ const requester = await validatedSlackRequester(deps, interaction, home);
5149
+ // Both orchestration notices are per-workspace and OFF unless this workspace
5150
+ // turned them on. Resolved lazily so an ordinary page of turn/progress events
5151
+ // costs no extra workspace read, and memoized so one page resolves once. A
5152
+ // disabled notice takes the same "nothing to post for this event" path as an
5153
+ // undeliverable one: no post operation, no progress slot, and the delivery
5154
+ // cursor still advances past the event exactly as it would have.
5155
+ let orchestrationNotices: ResolvedWorkspaceSlackOrchestrationNoticeSettings | null = null;
5156
+ const orchestrationNoticeEnabled = async (
5157
+ notice: keyof ResolvedWorkspaceSlackOrchestrationNoticeSettings,
5158
+ ): Promise<boolean> => {
5159
+ orchestrationNotices ??= resolveWorkspaceSlackOrchestrationNoticeSettings(
5160
+ (await getWorkspace(deps.db, interaction.workspaceId))?.settings,
5161
+ );
5162
+ return orchestrationNotices[notice];
5163
+ };
5164
+ /**
5165
+ * Post one orchestration notice through the SAME durable per-interaction slot
5166
+ * budget as assistant progress, so an orchestration that fans out to many
5167
+ * blocked children cannot turn one Slack task into an unbounded feed. That
5168
+ * budget is deliberately shared rather than a second private allowance: the
5169
+ * ceiling worth enforcing is the total number of posts the human did not ask
5170
+ * for, not a per-category one, and this whole feature exists because an
5171
+ * unsolicited post is worse than a missed one. Beyond the cap the notice goes
5172
+ * silent exactly like a fourth progress message.
5173
+ *
5174
+ * The slot is claimed only once a card is actually going to be posted, so a
5175
+ * skipped, stale, or already-resolved notice never burns one. Claims are
5176
+ * durable and keyed on the session event sequence, so a reaper retry, a
5177
+ * replica claim, or a replayed page reuses the same slot and the same post
5178
+ * operation instead of posting twice or consuming a second slot. The claimed
5179
+ * row never becomes terminal-coalescing evidence: that lookup only joins
5180
+ * `agent.message.completed` events.
5181
+ */
5182
+ const postUnsolicitedNotice = async (event: SessionEvent, text: string, kind: string) => {
5183
+ const slot = await claimSlackInteractionProgressDelivery(deps.db, {
5184
+ accountId: interaction.accountId,
5185
+ workspaceId: interaction.workspaceId,
5186
+ interactionId: interaction.id,
5187
+ claimHolderId,
5188
+ sessionEventSequence: event.sequence,
5189
+ maxProgress: MAX_PROGRESS_MESSAGES,
5190
+ });
5191
+ if (slot.kind === "not_owned") {
5192
+ throw new Error("Slack orchestration notice lost its durable interaction claim");
5193
+ }
5194
+ if (slot.kind !== "claimed") return;
5195
+ await postDelivery(client, interaction, event, text, kind, slot.delivery.operationId);
5196
+ };
2861
5197
  let lastSequence = interaction.lastDeliveredSessionEventSequence;
2862
5198
  let terminal: Exclude<SlackInteraction["terminalDeliveryState"], "open"> | null = null;
2863
- let latestAssistantText = "";
2864
5199
  const orderedEvents = page.events
2865
5200
  .filter(
2866
5201
  (event) =>
@@ -2877,6 +5212,7 @@ async function deliverSlackSessionEvents(
2877
5212
  const event = orderedEvents[index]!;
2878
5213
  if (event.type !== "turn.completed") continue;
2879
5214
  const finalOutput = safePayloadText(event.payload, "output").trim();
5215
+ if (!finalOutput || safePayloadText(event.payload, "segmentLimit")) continue;
2880
5216
  const candidates: SessionEvent[] = [];
2881
5217
  for (let candidateIndex = index - 1; candidateIndex >= 0; candidateIndex -= 1) {
2882
5218
  const candidate = orderedEvents[candidateIndex]!;
@@ -2891,13 +5227,7 @@ async function deliverSlackSessionEvents(
2891
5227
  if (event.turnId && candidate.turnId && event.turnId !== candidate.turnId) continue;
2892
5228
  candidates.push(candidate);
2893
5229
  }
2894
- const terminalText =
2895
- finalOutput ||
2896
- candidates
2897
- .map((candidate) => safePayloadText(candidate.payload, "text").trim())
2898
- .find(Boolean) ||
2899
- "";
2900
- if (!terminalText) continue;
5230
+ const terminalText = finalOutput;
2901
5231
  let matchedTerminalSuffix = false;
2902
5232
  for (const candidate of candidates) {
2903
5233
  const assistantText = safePayloadText(candidate.payload, "text").trim();
@@ -2925,8 +5255,16 @@ async function deliverSlackSessionEvents(
2925
5255
  for (const event of orderedEvents) {
2926
5256
  lastSequence = Math.max(lastSequence, event.sequence);
2927
5257
  if (event.type === "agent.message.completed") {
2928
- latestAssistantText = safePayloadText(event.payload, "text");
2929
- if (latestAssistantText && !terminalAssistantSequences.has(event.sequence)) {
5258
+ const latestAssistantText = safePayloadText(event.payload, "text");
5259
+ // A streamed per-message completion is either commentary (activity,
5260
+ // never a Slack post) or a final message that its turn.completed
5261
+ // delivers with the requester mention. Posting it early as progress
5262
+ // would turn the mention into an edit, which Slack does not notify.
5263
+ if (
5264
+ latestAssistantText &&
5265
+ !terminalAssistantSequences.has(event.sequence) &&
5266
+ !isStreamedAssistantMessageCompletion(event)
5267
+ ) {
2930
5268
  const progress = await claimSlackInteractionProgressDelivery(deps.db, {
2931
5269
  accountId: interaction.accountId,
2932
5270
  workspaceId: interaction.workspaceId,
@@ -2939,13 +5277,15 @@ async function deliverSlackSessionEvents(
2939
5277
  throw new Error("Slack progress delivery lost its durable interaction claim");
2940
5278
  }
2941
5279
  if (progress.kind === "claimed") {
2942
- await postDelivery(
2943
- client,
2944
- interaction,
2945
- event,
2946
- latestAssistantText,
2947
- "progress",
2948
- progress.delivery.operationId,
5280
+ await deliverSlackModelText((format) =>
5281
+ postDelivery(
5282
+ client,
5283
+ interaction,
5284
+ event,
5285
+ format(latestAssistantText),
5286
+ "progress",
5287
+ progress.delivery.operationId,
5288
+ ),
2949
5289
  );
2950
5290
  }
2951
5291
  }
@@ -2985,14 +5325,60 @@ async function deliverSlackSessionEvents(
2985
5325
  card.blocks,
2986
5326
  );
2987
5327
  }
5328
+ } else if (event.type === "system.update.pending") {
5329
+ const card = (await orchestrationNoticeEnabled("childRequiresAction"))
5330
+ ? await slackChildRequiresActionCard(deps, interaction, event, requester.mention)
5331
+ : null;
5332
+ if (card) await postUnsolicitedNotice(event, card.text, "child-blocked");
5333
+ } else if (event.type === "goal.paused") {
5334
+ const paused = (await orchestrationNoticeEnabled("goalPaused"))
5335
+ ? slackGoalPausedText(deps, interaction, event, requester.mention)
5336
+ : null;
5337
+ if (paused) await postUnsolicitedNotice(event, paused, "goal-paused");
2988
5338
  } else if (event.type === "turn.completed") {
2989
5339
  const payloadOutput = safePayloadText(event.payload, "output");
2990
5340
  const hasPublishableOutput = payloadOutput.trim().length > 0;
2991
- const output = hasPublishableOutput ? payloadOutput : latestAssistantText;
5341
+ if (safePayloadText(event.payload, "segmentLimit") === "budget_exhausted") {
5342
+ const allowance = parseAllowanceExhaustedRefusal(event.payload);
5343
+ await postDelivery(
5344
+ client,
5345
+ interaction,
5346
+ event,
5347
+ allowance
5348
+ ? `${requester.mention}${allowanceExhaustedMessage(allowance)} After the allowance is available, reply in this thread to resume.`
5349
+ : `${requester.mention}OpenGeni reached a billing or usage limit. Ask your organization owner to check credits and usage limits, then reply in this thread to resume.`,
5350
+ "billing-limit",
5351
+ );
5352
+ terminal = "failed";
5353
+ continue;
5354
+ }
5355
+ // A completed turn is not necessarily a completed task. Input waits and
5356
+ // pacing yields settle without a result. Keep the cursor moving
5357
+ // and delivery open for the eventual response; never promote commentary
5358
+ // or invent a success message for these boundaries.
5359
+ if (!hasPublishableOutput || safePayloadText(event.payload, "segmentLimit")) {
5360
+ // The one exception is the reply the worker recorded for a human's
5361
+ // message before the turn waited again (for example a status answer):
5362
+ // it answers that message, so it is posted, and delivery stays open.
5363
+ const reply = safePayloadText(event.payload, "segmentLimit")
5364
+ ? null
5365
+ : turnCompletedReply(event.payload);
5366
+ if (reply) {
5367
+ await deliverSlackModelText((format) =>
5368
+ postDelivery(
5369
+ client,
5370
+ interaction,
5371
+ event,
5372
+ `${requester.mention}${format(reply)}`,
5373
+ "reply",
5374
+ ),
5375
+ );
5376
+ }
5377
+ terminal = null;
5378
+ continue;
5379
+ }
5380
+ const output = payloadOutput;
2992
5381
  const normalizedOutput = output.trim();
2993
- const recurringLink = requester.canSchedule
2994
- ? `\n\n${makeRecurringText(deps, interaction.workspaceId, interaction.sessionId)}`
2995
- : "";
2996
5382
  const existingProgress = progressEvidence.find(
2997
5383
  (delivery) =>
2998
5384
  slackDeliveryTextsCoalesce(boundedOutput(delivery.text).trim(), normalizedOutput) &&
@@ -3012,56 +5398,64 @@ async function deliverSlackSessionEvents(
3012
5398
  // replica observed turn.completed. Reconcile the same provider
3013
5399
  // operation id (including response-loss retries) instead of inventing
3014
5400
  // a second final post.
3015
- await postDelivery(
3016
- client,
3017
- interaction,
3018
- event,
3019
- boundedOutput(existingProgress.text),
3020
- "progress",
3021
- existingProgress.operationId,
5401
+ // Render exactly as the progress post did: the same bounded text
5402
+ // through the same formatter, under the same operation.
5403
+ await deliverSlackModelText((format) =>
5404
+ postDelivery(
5405
+ client,
5406
+ interaction,
5407
+ event,
5408
+ format(boundedOutput(existingProgress.text)),
5409
+ "progress",
5410
+ existingProgress.operationId,
5411
+ ),
3022
5412
  );
3023
5413
  const posted = await getSlackBotPostOperation(
3024
5414
  deps.db,
3025
- interaction.workspaceId,
5415
+ home.workspaceId,
3026
5416
  interaction.connectionId,
3027
5417
  existingProgress.operationId,
3028
5418
  );
3029
5419
  if (posted?.slackChannelId && posted.slackMessageTimestamp) {
3030
- const text = boundedOutputWithSuffix(
3031
- `${requester.mention}${existingProgress.text}`,
3032
- recurringLink,
5420
+ const { slackChannelId, slackMessageTimestamp } = posted;
5421
+ const updateOperationId = deterministicUuid(
5422
+ slackPostSeed(interaction, `slack-terminal-update:${interaction.id}:${event.sequence}`),
3033
5423
  );
3034
- await client.updateMessage({
3035
- operationId: deterministicUuid(
3036
- `slack-terminal-update:${interaction.id}:${event.sequence}`,
3037
- ),
3038
- channelId: posted.slackChannelId,
3039
- timestamp: posted.slackMessageTimestamp,
3040
- text,
3041
- ...(publicationBlocks.length > 0
3042
- ? {
3043
- blocks: [
3044
- {
3045
- type: "section",
3046
- text: {
3047
- type: "mrkdwn",
3048
- text,
3049
- },
3050
- },
5424
+ await deliverSlackModelText(async (format) => {
5425
+ // The result is the result. Continuation prose lives in the
5426
+ // one-time hint and `<command> info`.
5427
+ const text = boundedOutput(
5428
+ `${requester.mention}${format(boundedOutput(existingProgress.text))}`,
5429
+ );
5430
+ // In the previous format this rewrites a message that already
5431
+ // carried the workspace line, so it has to carry it too, or the
5432
+ // update would quietly strip it. A compact task has no such line.
5433
+ const rendered = withWorkspaceLine(
5434
+ slackMessageWorkspaceLabel(interaction),
5435
+ text,
5436
+ publicationBlocks.length > 0
5437
+ ? ([
5438
+ { type: "section", text: { type: "mrkdwn", text } },
3051
5439
  ...publicationBlocks,
3052
- ],
3053
- }
3054
- : {}),
5440
+ ] as SlackMessageBlock[])
5441
+ : undefined,
5442
+ );
5443
+ await client.updateMessage({
5444
+ operationId: updateOperationId,
5445
+ channelId: slackChannelId,
5446
+ timestamp: slackMessageTimestamp,
5447
+ text: rendered.text,
5448
+ ...(rendered.blocks
5449
+ ? {
5450
+ blocks: rendered.blocks,
5451
+ }
5452
+ : {}),
5453
+ });
3055
5454
  });
3056
5455
  }
3057
5456
  } else {
3058
5457
  const operationId = deterministicUuid(
3059
- `slack-delivery:${interaction.id}:${event.sequence}:final`,
3060
- );
3061
- const finalSuffix = `\n\nReply in this thread to continue.${recurringLink}`;
3062
- const text = boundedOutputWithSuffix(
3063
- `${requester.mention}${output || "OpenGeni finished this task."}`,
3064
- finalSuffix,
5458
+ slackPostSeed(interaction, `slack-delivery:${interaction.id}:${event.sequence}:final`),
3065
5459
  );
3066
5460
  const publicationBlocks = hasPublishableOutput
3067
5461
  ? await slackSharedResultPublicationBlocks(
@@ -3072,17 +5466,20 @@ async function deliverSlackSessionEvents(
3072
5466
  operationId,
3073
5467
  )
3074
5468
  : [];
3075
- await postDelivery(
3076
- client,
3077
- interaction,
3078
- event,
3079
- text,
3080
- "final",
3081
- operationId,
3082
- publicationBlocks.length > 0
3083
- ? [{ type: "section", text: { type: "mrkdwn", text } }, ...publicationBlocks]
3084
- : undefined,
3085
- );
5469
+ await deliverSlackModelText((format) => {
5470
+ const text = boundedOutput(`${requester.mention}${format(output)}`);
5471
+ return postDelivery(
5472
+ client,
5473
+ interaction,
5474
+ event,
5475
+ text,
5476
+ "final",
5477
+ operationId,
5478
+ publicationBlocks.length > 0
5479
+ ? [{ type: "section", text: { type: "mrkdwn", text } }, ...publicationBlocks]
5480
+ : undefined,
5481
+ );
5482
+ });
3086
5483
  }
3087
5484
  terminal = "completed";
3088
5485
  } else if (event.type === "turn.failed") {
@@ -3106,6 +5503,8 @@ async function deliverSlackSessionEvents(
3106
5503
  }
3107
5504
  }
3108
5505
  if (terminal) {
5506
+ // The task settled: its first message stops offering Stop (or Resume).
5507
+ await retireSlackStartMessageButton(deps, client, interaction, home);
3109
5508
  await closeSlackInteractionDelivery(deps.db, {
3110
5509
  ...interaction,
3111
5510
  claimHolderId,
@@ -3159,16 +5558,74 @@ async function postDelivery(
3159
5558
  event: SessionEvent,
3160
5559
  text: string,
3161
5560
  kind: string,
3162
- operationId = deterministicUuid(`slack-delivery:${interaction.id}:${event.sequence}:${kind}`),
5561
+ operationId = deterministicUuid(
5562
+ slackPostSeed(interaction, `slack-delivery:${interaction.id}:${event.sequence}:${kind}`),
5563
+ ),
3163
5564
  blocks?: SlackMessageBlock[],
3164
5565
  ) {
3165
- await client.postMessage({
3166
- operationId,
3167
- channelId: interaction.slackChannelId,
3168
- threadTimestamp: interaction.slackThreadTs,
3169
- text: boundedOutput(text),
3170
- ...(blocks ? { blocks } : {}),
3171
- });
5566
+ const post = async (label: string | null) => {
5567
+ const rendered = withWorkspaceLine(label, boundedOutput(text), blocks);
5568
+ await client.postMessage({
5569
+ operationId,
5570
+ channelId: interaction.slackChannelId,
5571
+ threadTimestamp: interaction.slackThreadTs,
5572
+ text: rendered.text,
5573
+ ...(rendered.blocks ? { blocks: rendered.blocks } : {}),
5574
+ });
5575
+ };
5576
+ const label = slackMessageWorkspaceLabel(interaction);
5577
+ try {
5578
+ await post(label);
5579
+ } catch (error) {
5580
+ // Progress posts keep a durable random operation id that `slackPostSeed`
5581
+ // does not version. During a rolling deploy an older image may already have
5582
+ // bound one to the bytes with the workspace line. The ledger reports that
5583
+ // from its claim, before any Slack write, so one retry with those bytes
5584
+ // under the same id is safe and keeps the delivery from wedging.
5585
+ if (
5586
+ label !== null ||
5587
+ !interaction.routedWorkspaceLabel ||
5588
+ !(error instanceof SlackBotOperationConflictError)
5589
+ ) {
5590
+ throw error;
5591
+ }
5592
+ await post(interaction.routedWorkspaceLabel);
5593
+ }
5594
+ }
5595
+
5596
+ /**
5597
+ * Deliver model-authored text in Slack's own formatting.
5598
+ *
5599
+ * Agents write Markdown for the OpenGeni console; `slackMrkdwnFromMarkdown`
5600
+ * rewrites it for Slack at this sink only, so stored events and history keep
5601
+ * the exact text. Pass every model-authored part of the message through
5602
+ * `format`, and nothing else: fixed prose, mentions, and cards are already
5603
+ * Slack syntax.
5604
+ *
5605
+ * The post and update ledgers bind an operation id to a digest over the exact
5606
+ * bytes, and an operation an earlier release already bound to the unformatted
5607
+ * bytes (a claimed-but-unposted row, a response-loss retry, or a page replayed
5608
+ * across the deploy) must keep them, or every retry would conflict and wedge
5609
+ * the interaction. The ledger reports that conflict from its durable claim,
5610
+ * before any Slack write, so one retry with the unformatted bytes under the
5611
+ * same operation id is safe. The id itself never changes: action handles and
5612
+ * the post lookups keyed on it stay exact, and a new operation always carries
5613
+ * the formatted bytes.
5614
+ */
5615
+ async function deliverSlackModelText(
5616
+ deliver: (format: (text: string) => string) => Promise<unknown>,
5617
+ ): Promise<void> {
5618
+ let formatted = false;
5619
+ try {
5620
+ await deliver((text) => {
5621
+ const slackText = slackMrkdwnFromMarkdown(text);
5622
+ if (slackText !== text) formatted = true;
5623
+ return slackText;
5624
+ });
5625
+ } catch (error) {
5626
+ if (!formatted || !(error instanceof SlackBotOperationConflictError)) throw error;
5627
+ await deliver((text) => text);
5628
+ }
3172
5629
  }
3173
5630
 
3174
5631
  async function enqueueNormalizedSlackInteraction(
@@ -3247,6 +5704,8 @@ const SLACK_BLOCK_ACTION_IDS = new Set([
3247
5704
  "opengeni.session.pause",
3248
5705
  "opengeni.session.resume",
3249
5706
  "opengeni.shared_result.publish",
5707
+ "opengeni.route.select",
5708
+ "opengeni.route.cancel",
3250
5709
  ]);
3251
5710
 
3252
5711
  export function normalizedBlockActionInteraction(
@@ -3311,7 +5770,14 @@ function humanInputResponse(questions: HumanInputQuestion[], text: string) {
3311
5770
  const matches = question.options.filter(
3312
5771
  (option) => option.id.toLowerCase() === normalized || option.label.toLowerCase() === normalized,
3313
5772
  );
3314
- if (matches.length !== 1) return null;
5773
+ if (matches.length !== 1) {
5774
+ const other = text.trim();
5775
+ if (!other) return null;
5776
+ return {
5777
+ outcome: "answered" as const,
5778
+ answers: [{ questionId: question.id, values: [], other }],
5779
+ };
5780
+ }
3315
5781
  return {
3316
5782
  outcome: "answered" as const,
3317
5783
  answers: [{ questionId: question.id, values: [matches[0]!.id] }],
@@ -3326,27 +5792,250 @@ function formatQuestions(questions: HumanInputQuestion[]) {
3326
5792
  .slice(0, 10)
3327
5793
  .map((option) => option.label)
3328
5794
  .join(", ");
3329
- return `${index + 1}. ${boundedOutput(question.prompt)}${options ? ` (${options})` : ""}`;
5795
+ return `${index + 1}. ${boundedOutput(question.prompt)}${
5796
+ options ? ` (${options}, or reply with another value)` : ""
5797
+ }`;
3330
5798
  })
3331
5799
  .join("\n");
3332
5800
  }
3333
5801
 
5802
+ export function isSlackInfoCommand(text: string): boolean {
5803
+ return text.trim().toLowerCase() === "info";
5804
+ }
5805
+
5806
+ type SlackSlashResponse = {
5807
+ response_type: "ephemeral";
5808
+ text: string;
5809
+ unfurl_links: false;
5810
+ unfurl_media: false;
5811
+ blocks?: SlackMessageBlock[];
5812
+ };
5813
+
5814
+ /**
5815
+ * The ephemeral `<command> info` card.
5816
+ *
5817
+ * It is a projection of what the caller can already do, not an action: no
5818
+ * session, no durable inbox row, no provider post. It re-proves the exact
5819
+ * Slack identity link plus live workspace grants before any
5820
+ * workspace-identifying text is echoed, so an unlinked or access-revoked
5821
+ * identity receives the ordinary connect view instead.
5822
+ */
5823
+ async function slackInfoCommandResponse(
5824
+ deps: ApiRouteDeps,
5825
+ installation: SlackInstallationRoute,
5826
+ entry: NormalizedSlackInteraction,
5827
+ ): Promise<SlackSlashResponse> {
5828
+ const identity = {
5829
+ workspaceId: installation.workspaceId,
5830
+ connectionId: installation.connectionId,
5831
+ slackTeamId: entry.slackTeamId,
5832
+ slackUserId: entry.slackUserId,
5833
+ };
5834
+ const link = await getSlackBotUserLink(
5835
+ deps.db,
5836
+ installation.workspaceId,
5837
+ installation.connectionId,
5838
+ entry.slackUserId,
5839
+ );
5840
+ const grant = link
5841
+ ? await getWorkspaceGrant(deps.db, link.subjectId, installation.workspaceId, {
5842
+ principalKind: "human_session",
5843
+ })
5844
+ : null;
5845
+ if (
5846
+ !grant ||
5847
+ grant.accountId !== installation.accountId ||
5848
+ !hasPermission(grant.permissions, "sessions:read")
5849
+ ) {
5850
+ return ephemeralSlackResponse(
5851
+ link
5852
+ ? `Your Slack identity is linked, but it does not currently have access to this Opengeni workspace. Request access: ${linkUrl(deps, identity)}. No session was created.`
5853
+ : `Link your Slack identity to Opengeni before starting work: ${linkUrl(deps, identity)}. No session was created.`,
5854
+ );
5855
+ }
5856
+ const workspace = await getWorkspace(deps.db, installation.workspaceId);
5857
+ const command = deps.settings.slackCommand;
5858
+ const botMention = slackBotMention(installation.botUserId);
5859
+ // Every line is gated on the grant that actually authorizes it, so the card
5860
+ // stays a projection of what this caller can do rather than a generic manual.
5861
+ const canControl = hasPermission(grant.permissions, "sessions:control");
5862
+ const canCreate = hasPermission(grant.permissions, "sessions:create");
5863
+ const canSchedule = hasPermission(grant.permissions, "scheduled_tasks:manage");
5864
+ const schedules = canSchedule ? slackSchedulesUrl(deps, installation.workspaceId) : null;
5865
+ // Asking OpenGeni in a task's thread to repeat it needs the scheduling tool in
5866
+ // new Slack tasks here, `scheduled_tasks:manage` for the tool, and
5867
+ // `sessions:control` for the thread reply itself.
5868
+ const agentSchedules =
5869
+ canSchedule &&
5870
+ canControl &&
5871
+ slackTaskFirstPartyMcpTools(deps.settings, workspace?.settings).includes(
5872
+ "scheduled_tasks_create",
5873
+ );
5874
+ const workspaceUrl = slackWorkspaceUrl(deps, installation.workspaceId);
5875
+ const workspaceName = (workspace?.name ?? "").trim().slice(0, 120);
5876
+ const destination = workspaceName
5877
+ ? `the *${escapeSlackMrkdwn(workspaceName)}* workspace`
5878
+ : "your Opengeni workspace";
5879
+ const lines = [
5880
+ "*Working with Opengeni in Slack*",
5881
+ "",
5882
+ ...(canControl
5883
+ ? [
5884
+ "• *Continue a task:* reply in its Slack thread.",
5885
+ "• *Stop a task:* press *Stop* on its first message, or reply `stop` in its thread.",
5886
+ ]
5887
+ : []),
5888
+ ...(canCreate
5889
+ ? [
5890
+ `• *Start a new task:* mention ${botMention} in a channel, run \`${command} <task>\`, or send a new top-level direct message.`,
5891
+ ]
5892
+ : []),
5893
+ ...(agentSchedules || schedules
5894
+ ? [
5895
+ `• *Repeat a task on a schedule:* ${[
5896
+ ...(agentSchedules ? ["ask Opengeni in its thread"] : []),
5897
+ ...(schedules ? [`open <${schedules}|Schedules>`] : []),
5898
+ ].join(", or ")}.`,
5899
+ ]
5900
+ : []),
5901
+ `• *Where work lands:* ${destination}${workspaceUrl ? ` (<${workspaceUrl}|open Opengeni>)` : ""}.`,
5902
+ ];
5903
+ const text = lines.join("\n");
5904
+ return ephemeralSlackResponse(text, [
5905
+ { type: "section", text: { type: "mrkdwn", text } },
5906
+ ] as SlackMessageBlock[]);
5907
+ }
5908
+
5909
+ function ephemeralSlackResponse(text: string, blocks?: SlackMessageBlock[]): SlackSlashResponse {
5910
+ return {
5911
+ response_type: "ephemeral",
5912
+ text,
5913
+ unfurl_links: false,
5914
+ unfurl_media: false,
5915
+ ...(blocks ? { blocks } : {}),
5916
+ };
5917
+ }
5918
+
5919
+ function slackBotMention(botUserId: string | null): string {
5920
+ return botUserId && /^[UWB][A-Z0-9]{1,63}$/.test(botUserId) ? `<@${botUserId}>` : "@OpenGeni";
5921
+ }
5922
+
3334
5923
  function openSessionText(deps: ApiRouteDeps, workspaceId: string, sessionId: string) {
3335
- const base = deps.settings.webBaseUrl ?? deps.settings.publicBaseUrl;
3336
- if (!base) throw new Error("Slack session acknowledgement requires an absolute web base URL");
3337
- const url = new URL(`/workspaces/${workspaceId}/sessions/${sessionId}`, base).toString();
5924
+ const url = slackAcknowledgementSessionUrl(deps, workspaceId, sessionId);
5925
+ if (!url) throw new Error("Slack session acknowledgement requires an absolute web base URL");
3338
5926
  return `<${url}|Open in OpenGeni>`;
3339
5927
  }
3340
5928
 
3341
- function makeRecurringText(deps: ApiRouteDeps, workspaceId: string, sessionId: string) {
5929
+ /** The session link an acknowledgement carries, or null without a base URL. */
5930
+ function slackAcknowledgementSessionUrl(
5931
+ deps: ApiRouteDeps,
5932
+ workspaceId: string,
5933
+ sessionId: string,
5934
+ ): string | null {
5935
+ const base = deps.settings.webBaseUrl ?? deps.settings.publicBaseUrl;
5936
+ if (!base) return null;
5937
+ return new URL(`/workspaces/${workspaceId}/sessions/${sessionId}`, base).toString();
5938
+ }
5939
+
5940
+ function slackWorkspaceUrl(deps: ApiRouteDeps, workspaceId: string): string | null {
5941
+ return safeSlackActionUrl(deps, `/workspaces/${encodeURIComponent(workspaceId)}`);
5942
+ }
5943
+
5944
+ function slackSessionAuthorizationScopeKey(scope: SessionAuthorizationListScope | null): string {
5945
+ if (scope === null) return "standalone";
5946
+ if (scope.kind === "all") return "all";
5947
+ return JSON.stringify({
5948
+ kind: scope.kind,
5949
+ rootSessionIds: [...scope.rootSessionIds].sort(),
5950
+ sessionIds: [...scope.sessionIds].sort(),
5951
+ });
5952
+ }
5953
+
5954
+ function slackSessionUrl(
5955
+ deps: ApiRouteDeps,
5956
+ workspaceId: string,
5957
+ sessionId: string,
5958
+ ): string | null {
5959
+ return safeSlackActionUrl(
5960
+ deps,
5961
+ `/workspaces/${encodeURIComponent(workspaceId)}/sessions/${encodeURIComponent(sessionId)}`,
5962
+ );
5963
+ }
5964
+
5965
+ function safeSlackActionUrl(deps: ApiRouteDeps, pathname: string): string | null {
3342
5966
  const base = deps.settings.webBaseUrl ?? deps.settings.publicBaseUrl;
3343
- if (!base) throw new Error("Slack recurring action requires an absolute web base URL");
3344
- const url = new URL(`/workspaces/${workspaceId}/schedules`, base);
5967
+ if (!base) return null;
5968
+ try {
5969
+ const url = new URL(pathname, base);
5970
+ if ((url.protocol !== "https:" && url.protocol !== "http:") || url.username || url.password) {
5971
+ return null;
5972
+ }
5973
+ return url.toString();
5974
+ } catch {
5975
+ return null;
5976
+ }
5977
+ }
5978
+
5979
+ function slackAppHomeLinkUrl(
5980
+ deps: ApiRouteDeps,
5981
+ installation: SlackInstallationRoute,
5982
+ slackTeamId: string,
5983
+ slackUserId: string,
5984
+ ): string | null {
5985
+ const signingSecret = deps.settings.slackSigningSecret;
5986
+ const base = slackWorkspaceUrl(deps, installation.workspaceId);
5987
+ if (!signingSecret || !base) return base;
5988
+ const url = new URL(
5989
+ `/workspaces/${encodeURIComponent(installation.workspaceId)}/capabilities`,
5990
+ base,
5991
+ );
5992
+ url.hash = new URLSearchParams({
5993
+ slack_link: createSlackUserLinkToken(signingSecret, {
5994
+ workspaceId: installation.workspaceId,
5995
+ connectionId: installation.connectionId,
5996
+ slackTeamId,
5997
+ slackUserId,
5998
+ }),
5999
+ }).toString();
6000
+ return url.toString();
6001
+ }
6002
+
6003
+ function makeRecurringText(
6004
+ deps: ApiRouteDeps,
6005
+ workspaceId: string,
6006
+ sessionId: string,
6007
+ ): string | null {
6008
+ // Unchanged deep-link contract: the schedules editor plus the exact source
6009
+ // session UUID, and nothing copied out of Slack. A deployment without an
6010
+ // absolute web base URL simply omits the action instead of failing a card.
6011
+ const base = slackSchedulesUrl(deps, workspaceId);
6012
+ if (!base) return null;
6013
+ const url = new URL(base);
3345
6014
  url.searchParams.set("sourceSessionId", sessionId);
3346
6015
  return `<${url.toString()}|Make recurring>`;
3347
6016
  }
3348
6017
 
3349
- function linkUrl(deps: ApiRouteDeps, entry: SlackInteractionInboxEntry) {
6018
+ function slackSchedulesUrl(deps: ApiRouteDeps, workspaceId: string): string | null {
6019
+ return safeSlackActionUrl(deps, `/workspaces/${encodeURIComponent(workspaceId)}/schedules`);
6020
+ }
6021
+
6022
+ function linkUrl(
6023
+ deps: ApiRouteDeps,
6024
+ entry: Pick<
6025
+ SlackInteractionInboxEntry,
6026
+ "workspaceId" | "connectionId" | "slackTeamId" | "slackUserId"
6027
+ >,
6028
+ /**
6029
+ * When this URL goes into a message posted under a deterministic operation
6030
+ * id, the token has to be deterministic too. The post ledger binds that id to
6031
+ * a digest over the text, so a retry that mints a fresh token renders
6032
+ * different bytes and is refused as a conflicting request, permanently. Pass
6033
+ * the originating inbox row's creation time: the token is then stable for that
6034
+ * row and still expires fifteen minutes after the message could first have
6035
+ * been read.
6036
+ */
6037
+ mintedAtMs?: number,
6038
+ ) {
3350
6039
  const base = deps.settings.webBaseUrl ?? deps.settings.publicBaseUrl;
3351
6040
  const signingSecret = deps.settings.slackSigningSecret;
3352
6041
  if (!base || !signingSecret) return "OpenGeni Settings → Integrations → Slack";
@@ -3354,7 +6043,7 @@ function linkUrl(deps: ApiRouteDeps, entry: SlackInteractionInboxEntry) {
3354
6043
  // Fragments stay out of HTTP request lines, reverse-proxy logs, Referer
3355
6044
  // headers, and managed-auth callback URLs. Query-form bearers are rejected.
3356
6045
  url.hash = new URLSearchParams({
3357
- slack_link: createSlackUserLinkToken(signingSecret, entry),
6046
+ slack_link: createSlackUserLinkToken(signingSecret, entry, mintedAtMs),
3358
6047
  }).toString();
3359
6048
  return url.toString();
3360
6049
  }
@@ -3519,17 +6208,16 @@ function boundedText(value: unknown): string | null {
3519
6208
  return trimmed.slice(0, MAX_SLACK_INPUT_CHARS);
3520
6209
  }
3521
6210
 
3522
- function boundedOutput(value: string) {
3523
- return value.length <= MAX_SLACK_TEXT_CHARS
3524
- ? value
3525
- : `${value.slice(0, MAX_SLACK_TEXT_CHARS - 20)}\n… output truncated`;
6211
+ /** Single-line, character-bounded preview for a blocked-worker pointer card. */
6212
+ function boundedSlackChildDetail(value: string) {
6213
+ const collapsed = value.replace(/\s+/gu, " ").trim();
6214
+ return collapsed.length <= MAX_SLACK_CHILD_DETAIL_CHARS
6215
+ ? collapsed
6216
+ : `${collapsed.slice(0, MAX_SLACK_CHILD_DETAIL_CHARS - 1)}…`;
3526
6217
  }
3527
6218
 
3528
- function boundedOutputWithSuffix(value: string, suffix: string) {
3529
- const maxValueChars = Math.max(0, MAX_SLACK_TEXT_CHARS - suffix.length);
3530
- if (value.length <= maxValueChars) return `${value}${suffix}`;
3531
- const truncation = "\n… output truncated";
3532
- return `${value.slice(0, Math.max(0, maxValueChars - truncation.length))}${truncation}${suffix}`;
6219
+ function boundedOutput(value: string, limit = MAX_SLACK_TEXT_CHARS) {
6220
+ return value.length <= limit ? value : `${value.slice(0, limit - 20)}\n… output truncated`;
3533
6221
  }
3534
6222
 
3535
6223
  function safePayloadText(payload: unknown, field: string) {
@@ -3551,7 +6239,12 @@ function safeErrorCode(error: unknown) {
3551
6239
  );
3552
6240
  }
3553
6241
 
3554
- function slackAdmissionFailureText(error: HTTPException) {
6242
+ export function slackAdmissionFailureText(error: HTTPException) {
6243
+ const allowance =
6244
+ parseAllowanceExhaustedRefusal(error.cause) ?? parseAllowanceExhaustedRefusal(error);
6245
+ if (allowance) {
6246
+ return `OpenGeni could not start this task. ${allowanceExhaustedMessage(allowance)} After the allowance is available, reply in this thread to try again.`;
6247
+ }
3555
6248
  if (error.status === 402) {
3556
6249
  return "OpenGeni could not start this task because the selected model has no available billing source. Open OpenGeni, select a connected subscription model, and try again.";
3557
6250
  }