@opengeni/worker-bundle 0.20.3 → 0.22.0-canary.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 (184) hide show
  1. package/dist/activities/agent-turn/admission.d.ts +52 -0
  2. package/dist/activities/agent-turn/agent-build.d.ts +89 -0
  3. package/dist/activities/agent-turn/claim.d.ts +67 -0
  4. package/dist/activities/agent-turn/codex-capacity.d.ts +40 -0
  5. package/dist/activities/agent-turn/codex.d.ts +32 -0
  6. package/dist/activities/agent-turn/compaction-prep.d.ts +88 -0
  7. package/dist/activities/agent-turn/credential-leases.d.ts +56 -0
  8. package/dist/activities/agent-turn/errors.d.ts +253 -0
  9. package/dist/activities/agent-turn/failure-settlement.d.ts +33 -0
  10. package/dist/activities/agent-turn/file-resources.d.ts +37 -0
  11. package/dist/activities/agent-turn/finalization.d.ts +42 -0
  12. package/dist/activities/agent-turn/github-repository-bindings.d.ts +105 -0
  13. package/dist/activities/agent-turn/governance-model.d.ts +66 -0
  14. package/dist/activities/agent-turn/history-sink.d.ts +52 -0
  15. package/dist/activities/agent-turn/history.d.ts +63 -0
  16. package/dist/activities/agent-turn/media-artifacts.d.ts +145 -0
  17. package/dist/activities/agent-turn/model-usage.d.ts +218 -0
  18. package/dist/activities/agent-turn/quiescence.d.ts +135 -0
  19. package/dist/activities/agent-turn/run-credentials.d.ts +84 -0
  20. package/dist/activities/agent-turn/run.d.ts +3 -0
  21. package/dist/activities/agent-turn/sandbox-establish.d.ts +93 -0
  22. package/dist/activities/agent-turn/sandbox-provision.d.ts +89 -0
  23. package/dist/activities/agent-turn/sandbox-route.d.ts +151 -0
  24. package/dist/activities/agent-turn/sandbox-runtime.d.ts +44 -0
  25. package/dist/activities/agent-turn/stream-attempt.d.ts +107 -0
  26. package/dist/activities/agent-turn/tool-environment.d.ts +142 -0
  27. package/dist/activities/agent-turn/tool-policy.d.ts +127 -0
  28. package/dist/activities/agent-turn/tool-result-spill.d.ts +59 -0
  29. package/dist/activities/agent-turn/turn-context.d.ts +144 -0
  30. package/dist/activities/agent-turn/xai-capacity.d.ts +2 -0
  31. package/dist/activities/agent-turn.d.ts +12 -954
  32. package/dist/activities/automations.d.ts +16 -0
  33. package/dist/activities/common.d.ts +1 -1
  34. package/dist/activities/connector-attachments.d.ts +13 -0
  35. package/dist/activities/documents.d.ts +1 -0
  36. package/dist/activities/environment.d.ts +26 -15
  37. package/dist/activities/generated-images.d.ts +1 -1
  38. package/dist/activities/git-credential-renewal.d.ts +3 -1
  39. package/dist/activities/goals.d.ts +4 -1
  40. package/dist/activities/google-drive-provider.d.ts +29 -0
  41. package/dist/activities/google-drive-publication.d.ts +17 -4
  42. package/dist/activities/incident-telemetry-authority.d.ts +52 -0
  43. package/dist/activities/incident-telemetry-preflight.d.ts +71 -0
  44. package/dist/activities/knowledge-source-sync.d.ts +4 -0
  45. package/dist/activities/mcp-credentials.d.ts +9 -3
  46. package/dist/activities/open-suffix-resume.d.ts +77 -0
  47. package/dist/activities/packs.d.ts +3 -11
  48. package/dist/activities/parent-wake.d.ts +19 -0
  49. package/dist/activities/retained-screenshot-reaper.d.ts +1 -1
  50. package/dist/activities/retained-screenshots.d.ts +1 -1
  51. package/dist/activities/rig-verification.d.ts +1 -1
  52. package/dist/activities/run-input.d.ts +9 -10
  53. package/dist/activities/sandbox-images.d.ts +1 -14
  54. package/dist/activities/sandbox-lease.d.ts +21 -3
  55. package/dist/activities/scheduled-tasks.d.ts +47 -0
  56. package/dist/activities/types.d.ts +23 -2
  57. package/dist/activities/video-media-validation.d.ts +1 -1
  58. package/dist/activities/video-output-retention.d.ts +1 -2
  59. package/dist/activities/xai-auth.d.ts +3 -0
  60. package/dist/{activities-control-BJSDRSIP.js → activities-control-DKG22ONJ.js} +2646 -551
  61. package/dist/activities-control-DKG22ONJ.js.map +1 -0
  62. package/dist/activities-control.d.ts +6 -2
  63. package/dist/activities-turn-4IMC3MAA.js +21326 -0
  64. package/dist/activities-turn-4IMC3MAA.js.map +1 -0
  65. package/dist/activities.d.ts +7 -3
  66. package/dist/artifact-materializer-entry.js +41 -22
  67. package/dist/artifact-materializer-entry.js.map +1 -1
  68. package/dist/{chunk-N5TJIFCE.js → chunk-CE7TPKBS.js} +468 -1
  69. package/dist/chunk-CE7TPKBS.js.map +1 -0
  70. package/dist/chunk-CNYGVRKF.js +4698 -0
  71. package/dist/chunk-CNYGVRKF.js.map +1 -0
  72. package/dist/connection-provider-use.d.ts +18 -0
  73. package/dist/editable-artifact-materializer.d.ts +4 -3
  74. package/dist/github-rest-mcp.d.ts +26 -0
  75. package/dist/index.d.ts +10 -2
  76. package/dist/index.js +58 -14
  77. package/dist/index.js.map +1 -1
  78. package/dist/knowledge-source-sync-activity-policy.d.ts +2 -0
  79. package/dist/model-context-contributions.d.ts +8 -4
  80. package/dist/observability-metrics.d.ts +63 -4
  81. package/dist/opensandbox-kubernetes-inventory.d.ts +21 -0
  82. package/dist/personal-github-git-credentials.d.ts +9 -0
  83. package/dist/pr-review-credentials.d.ts +6 -0
  84. package/dist/sandbox-archive-storage.d.ts +45 -0
  85. package/dist/sandbox-resume.d.ts +65 -6
  86. package/dist/sandbox-routing.d.ts +38 -2
  87. package/dist/scheduled-alert-occurrence.d.ts +33 -0
  88. package/dist/workflow-bundle.js +87 -28
  89. package/dist/workflows/activities.d.ts +1 -0
  90. package/dist/workflows/automations.d.ts +6 -0
  91. package/dist/workflows/document-index.d.ts +1 -0
  92. package/dist/workflows.d.ts +1 -0
  93. package/package.json +17 -17
  94. package/src/activities/agent-turn/admission.ts +292 -0
  95. package/src/activities/agent-turn/agent-build.ts +802 -0
  96. package/src/activities/agent-turn/claim.ts +590 -0
  97. package/src/activities/agent-turn/codex-capacity.ts +720 -0
  98. package/src/activities/agent-turn/codex.ts +94 -0
  99. package/src/activities/agent-turn/compaction-prep.ts +722 -0
  100. package/src/activities/agent-turn/credential-leases.ts +240 -0
  101. package/src/activities/agent-turn/errors.ts +1068 -0
  102. package/src/activities/agent-turn/failure-settlement.ts +1436 -0
  103. package/src/activities/agent-turn/file-resources.ts +227 -0
  104. package/src/activities/agent-turn/finalization.ts +754 -0
  105. package/src/activities/agent-turn/github-repository-bindings.ts +300 -0
  106. package/src/activities/agent-turn/governance-model.ts +465 -0
  107. package/src/activities/agent-turn/history-sink.ts +139 -0
  108. package/src/activities/agent-turn/history.ts +200 -0
  109. package/src/activities/agent-turn/media-artifacts.ts +359 -0
  110. package/src/activities/agent-turn/model-usage.ts +823 -0
  111. package/src/activities/agent-turn/quiescence.ts +353 -0
  112. package/src/activities/agent-turn/run-credentials.ts +761 -0
  113. package/src/activities/agent-turn/run.ts +1530 -0
  114. package/src/activities/agent-turn/sandbox-establish.ts +1207 -0
  115. package/src/activities/agent-turn/sandbox-provision.ts +569 -0
  116. package/src/activities/agent-turn/sandbox-route.ts +358 -0
  117. package/src/activities/agent-turn/sandbox-runtime.ts +731 -0
  118. package/src/activities/agent-turn/stream-attempt.ts +1670 -0
  119. package/src/activities/agent-turn/tool-environment.ts +729 -0
  120. package/src/activities/agent-turn/tool-policy.ts +216 -0
  121. package/src/activities/agent-turn/tool-result-spill.ts +265 -0
  122. package/src/activities/agent-turn/turn-context.ts +281 -0
  123. package/src/activities/agent-turn/xai-capacity.ts +229 -0
  124. package/src/activities/agent-turn.ts +12 -12701
  125. package/src/activities/api-integrations.ts +3 -0
  126. package/src/activities/automations.ts +214 -0
  127. package/src/activities/codemode-dispatcher.ts +12 -1
  128. package/src/activities/common.ts +1 -5
  129. package/src/activities/connector-attachments.ts +168 -0
  130. package/src/activities/context-compaction.ts +18 -0
  131. package/src/activities/environment.ts +151 -55
  132. package/src/activities/generated-images.ts +26 -11
  133. package/src/activities/git-credential-renewal.ts +13 -3
  134. package/src/activities/goals.ts +85 -19
  135. package/src/activities/google-drive-provider.ts +178 -0
  136. package/src/activities/google-drive-publication.ts +147 -49
  137. package/src/activities/incident-telemetry-authority.ts +327 -0
  138. package/src/activities/incident-telemetry-preflight.ts +317 -0
  139. package/src/activities/knowledge-source-sync.ts +178 -37
  140. package/src/activities/mcp-credentials.ts +191 -17
  141. package/src/activities/open-suffix-resume.ts +422 -0
  142. package/src/activities/packs.ts +12 -60
  143. package/src/activities/parent-wake.ts +64 -14
  144. package/src/activities/retained-screenshot-reaper.ts +6 -3
  145. package/src/activities/retained-screenshots.ts +15 -47
  146. package/src/activities/rig-verification.ts +1 -1
  147. package/src/activities/run-input.ts +102 -142
  148. package/src/activities/sandbox-images.ts +7 -166
  149. package/src/activities/sandbox-lease.ts +406 -15
  150. package/src/activities/scheduled-tasks.ts +2103 -411
  151. package/src/activities/session-state.ts +10 -2
  152. package/src/activities/types.ts +32 -2
  153. package/src/activities/video-media-validation.ts +3 -3
  154. package/src/activities/video-output-retention.ts +2 -57
  155. package/src/activities/xai-auth.ts +8 -0
  156. package/src/activities-control.ts +2 -0
  157. package/src/activities.ts +3 -0
  158. package/src/activity-services.ts +13 -4
  159. package/src/connection-provider-use.ts +36 -0
  160. package/src/editable-artifact-materializer-subprocess.ts +25 -14
  161. package/src/editable-artifact-materializer.ts +29 -18
  162. package/src/github-rest-mcp.ts +713 -0
  163. package/src/http.ts +16 -6
  164. package/src/index.ts +40 -5
  165. package/src/knowledge-source-sync-activity-policy.ts +2 -0
  166. package/src/model-context-contributions.ts +33 -10
  167. package/src/observability-metrics.ts +357 -0
  168. package/src/opensandbox-kubernetes-inventory.ts +292 -0
  169. package/src/personal-github-git-credentials.ts +331 -0
  170. package/src/pr-review-credentials.ts +163 -0
  171. package/src/sandbox-archive-storage.ts +159 -0
  172. package/src/sandbox-resume.ts +449 -63
  173. package/src/sandbox-routing.ts +314 -16
  174. package/src/scheduled-alert-occurrence.ts +176 -0
  175. package/src/workflows/activities.ts +18 -2
  176. package/src/workflows/automations.ts +11 -0
  177. package/src/workflows/session.ts +13 -3
  178. package/src/workflows.ts +1 -0
  179. package/dist/activities-control-BJSDRSIP.js.map +0 -1
  180. package/dist/activities-turn-J5YO7XXX.js +0 -17243
  181. package/dist/activities-turn-J5YO7XXX.js.map +0 -1
  182. package/dist/chunk-N5TJIFCE.js.map +0 -1
  183. package/dist/chunk-P6Z2SLAA.js +0 -3092
  184. package/dist/chunk-P6Z2SLAA.js.map +0 -1
@@ -1,40 +1,68 @@
1
1
  import {
2
+ DEFAULT_FIRST_PARTY_MCP_PERMISSIONS,
2
3
  OPENGENI_SLACK_BOT_CREDENTIAL_LABEL,
3
4
  OPENGENI_SLACK_BOT_CREDENTIAL_ROLE,
4
5
  OPENGENI_SLACK_BOT_SESSION_METADATA_KEY,
6
+ resolveWorkspaceCodexCompactionDefault,
7
+ SCHEDULED_TASK_OCCURRENCE_PAYLOAD_MAX_BYTES,
8
+ ScheduledTaskRunAcceptedExecution,
9
+ scheduledOccurrencePayloadUtf8Bytes,
10
+ stableJson,
11
+ type ScheduledTask,
12
+ type ScheduledTaskRun,
5
13
  } from "@opengeni/contracts";
6
14
  import {
15
+ defaultSessionMcpServerIds,
7
16
  openGeniSlackBotMetadata,
8
17
  requireOpenGeniSlackBotConnection,
18
+ resolveSessionToolPolicy,
9
19
  scheduledSlackBotConnectionId,
10
20
  } from "@opengeni/core";
11
21
  import {
12
- appendSessionEventsWithLockedSessionUpdate,
22
+ appendSessionEvents,
13
23
  addSessionSystemUpdateWithSourceMutation,
24
+ bindScheduledTaskRunSessionInTransaction,
14
25
  createScheduledTaskRun,
15
26
  createSession,
16
- createSessionGoal,
27
+ createSessionWithIdempotencyKeyResult,
17
28
  enqueueSessionWorkflowWakeIfRunnable,
18
29
  getScheduledTask,
30
+ getScheduledTaskRunAcceptedExecution,
31
+ getScheduledTaskRunByProducerKey,
32
+ getScheduledTargetSessionExecution,
19
33
  ensureKnowledgeSourceSyncState,
20
34
  getScheduledTaskPersonalConnectionDelegations,
35
+ getScheduledTaskRunPersonalResourceAuthority,
36
+ getScheduledTaskPersonalResourceAuthoritySubject,
37
+ getScheduledTaskRevisionAuthority,
21
38
  getScheduledTaskXaiProviderAccountAuthoritySnapshot,
22
39
  getRig,
40
+ getScheduledScopedRigVersionMetadata,
41
+ getNestedAgentDepthDeploymentPolicy,
42
+ getSessionByCreateIdempotencyKey,
23
43
  getVariableSet,
44
+ isCodexBilledModel,
45
+ initializeSessionStartAtomically,
46
+ materializeScheduledTaskReusableSessionFromRun,
47
+ listEnabledMcpCapabilityServerIds,
48
+ listInstalledApiIntegrationServerIdsForDelegations,
24
49
  markScheduledTaskRunFailedIfQueued,
50
+ markScheduledTaskRunSkippedIfQueued,
25
51
  recordKnowledgeSourceSyncWake,
26
52
  recordUsageEvent,
27
- requireScheduledTaskTargetInTransaction,
53
+ requireScheduledTaskIncidentAuthorityInTransaction,
28
54
  requireSession,
29
- setTemporalWorkflowId,
55
+ requireWorkspace,
30
56
  settleScheduledTaskRunInTransaction,
31
57
  SessionSpawnDeniedDbError,
32
- updateScheduledTask,
33
58
  updateScheduledTaskRun,
34
- upsertSessionGoal,
59
+ updateSessionTitle,
60
+ upsertScheduledSessionGoalForRun,
61
+ withSessionActivityRlsContext,
35
62
  } from "@opengeni/db";
36
- import { appendAndPublishEvents, publishDurableSessionEvents } from "@opengeni/events";
63
+ import { publishDurableSessionEvents } from "@opengeni/events";
37
64
  import { resolveFirstPartyMcpToolPolicy } from "@opengeni/config";
65
+ import { Context } from "@temporalio/activity";
38
66
  import {
39
67
  assertReusableSessionRevivable,
40
68
  scheduledUserMessagePayload,
@@ -42,11 +70,163 @@ import {
42
70
  } from "./common";
43
71
  import { withFirstPartyTools } from "./goals";
44
72
  import { agentRunAdmissionDenial } from "./agent-run-admission";
73
+ import {
74
+ scheduledAlertOccurrenceIdentity,
75
+ scheduledAlertResponderSessionCreateIdempotencyKey,
76
+ } from "../scheduled-alert-occurrence";
77
+ import {
78
+ evaluateIncidentTelemetryPreflight,
79
+ incidentTelemetryAuthorityFence,
80
+ incidentTelemetryPreflightDeclaration,
81
+ INCIDENT_TELEMETRY_AUTHORITY_FENCE_LINEAGE_KEY,
82
+ type IncidentTelemetryPreflightBlockReason,
83
+ } from "./incident-telemetry-preflight";
84
+ import { resolveIncidentTelemetryResponderMetadata } from "./incident-telemetry-authority";
45
85
  import type {
46
86
  ControlActivityServices,
47
87
  DispatchScheduledTaskRunInput,
48
88
  DispatchScheduledTaskRunResult,
89
+ WakeSessionWorkflowSignal,
49
90
  } from "./types";
91
+ import type { Database } from "@opengeni/db";
92
+
93
+ type ScheduledTemporalActivityIdentity = {
94
+ namespace: string;
95
+ workflowExecution?: { workflowId: string; runId: string };
96
+ activityId: string;
97
+ };
98
+
99
+ export function scheduledTaskRunProducerKey(
100
+ input: DispatchScheduledTaskRunInput,
101
+ suppliedActivityIdentity?: ScheduledTemporalActivityIdentity,
102
+ ): string {
103
+ if (input.producerKey) return input.producerKey;
104
+ if (input.triggerType !== "scheduled") return input.agentRunUsageIdempotencyKey;
105
+
106
+ let info = suppliedActivityIdentity;
107
+ if (!info) {
108
+ try {
109
+ info = Context.current().info;
110
+ } catch {
111
+ throw new Error(
112
+ "scheduled task delivery without a producer key requires a Temporal activity execution",
113
+ );
114
+ }
115
+ }
116
+ if (!info.workflowExecution) {
117
+ throw new Error("scheduled task Temporal activity is missing its workflow execution identity");
118
+ }
119
+ return `scheduled-temporal:${stableJson({
120
+ namespace: info.namespace,
121
+ workflowId: info.workflowExecution.workflowId,
122
+ })}`;
123
+ }
124
+
125
+ // Same bound the manual rename field and the set_session_title tool enforce.
126
+ const SCHEDULED_SESSION_TITLE_MAX_LENGTH = 200;
127
+
128
+ /**
129
+ * Display title for a session the scheduler generates for a task.
130
+ *
131
+ * A scheduled session's `initialMessage` is the task prompt, byte-identical for
132
+ * every run, so an untitled scheduled session falls back to that prompt in the
133
+ * rail and reads exactly like every other run of the task. The task name is the
134
+ * short human-chosen label that actually identifies it, so that is the title.
135
+ *
136
+ * The title is the task name and NOTHING else. It deliberately does not name the
137
+ * run's fire instant, for two independent reasons:
138
+ *
139
+ * - No run mode guarantees one run per session. `reusable_session` generates
140
+ * its session on the first fire and then reuses it forever, and even under
141
+ * `new_session_per_run` alert-occurrence redelivery converges several runs
142
+ * onto one canonical responder session. `existing_session` never generates a
143
+ * session at all, so the scheduler never titles one. A run-specific fact
144
+ * frozen into the title is therefore right for the generating run and a lie
145
+ * for every run that lands in the same session afterwards.
146
+ * - A title is one stored string shown to every viewer, while every surface
147
+ * that renders a run's fire instant renders it viewer-local: the rail row
148
+ * prints the title beside `relativeTimeLabel(session.updatedAt)`, and the
149
+ * Schedules run list prints `formatTimestamp(run.firedAt)`, which is
150
+ * `toLocaleString()`. A wall clock stored in the title therefore disagrees
151
+ * with the time printed next to it for every viewer outside whichever zone
152
+ * it was rendered in. The task's own `timeZone` is no escape: it labels the
153
+ * schedule rule ("Mon Wed 09:00 Europe/Oslo"), not the rendering of a run,
154
+ * which stays viewer-local. No stored clock can agree with a viewer-local
155
+ * one, so the title carries none and the instant stays on the surfaces that
156
+ * already render it correctly.
157
+ *
158
+ * Dropping the clock settles the determinism constraint outright rather than
159
+ * working around it: the same run titled by the normal dispatch or by a later
160
+ * recovery in a different worker process derives one string from `task.name`
161
+ * alone, with no Intl call and so no host ICU or tzdata build to make the two
162
+ * texts differ.
163
+ */
164
+ export function scheduledTaskSessionTitle(taskName: string): string {
165
+ // `ScheduledTask.name` has no length bound of its own, so it is bounded here
166
+ // to the same ceiling a human rename and set_session_title are held to.
167
+ const name = taskName.trim() || "Scheduled run";
168
+ if (name.length <= SCHEDULED_SESSION_TITLE_MAX_LENGTH) return name;
169
+ return `${name.slice(0, SCHEDULED_SESSION_TITLE_MAX_LENGTH - 1).trimEnd()}…`;
170
+ }
171
+
172
+ /**
173
+ * Title a session the scheduler generated, and emit the same `session.title_set`
174
+ * event every other title write in the product emits.
175
+ *
176
+ * This is the write half of the shared path in `@opengeni/core`
177
+ * `updateSessionTitle`: the db `updateSessionTitle` UPDATE (which is where the
178
+ * clobber guard lives in full) followed by the identical `session.title_set`
179
+ * append, emitted only on a real write. Without the event a live subscriber sees
180
+ * nothing: `packages/react/src/hooks/use-session.ts` patches an open session's
181
+ * title exclusively on `session.title_set`, so a bare row write leaves the
182
+ * viewer on the stale title until an unrelated refetch.
183
+ *
184
+ * The core wrapper itself is not called, because its only additional behavior is
185
+ * `requireSessionAuthorization(grant, ...)` - a check on an HTTP/MCP caller's
186
+ * grant. The scheduler has no such grant; it is naming a session it just created
187
+ * itself moments earlier. Satisfying that gate would mean fabricating a subject,
188
+ * and the gate would then answer for it: a session later transitioned to
189
+ * `user_private` denies any actor whose subject is not the owner
190
+ * (`packages/core/src/session-authorization.ts`), which would turn naming a
191
+ * session into a way to fail a dispatch. Authorization belongs to callers with
192
+ * grants; the durable write and its event are the part that is shared.
193
+ *
194
+ * Only an untitled row is stamped. A keyed create replay can hand back a session
195
+ * that has already run, and recovery re-enters this path for a session the dead
196
+ * dispatch may already have titled; in both cases whatever the agent or a human
197
+ * chose is newer truth than this stamp.
198
+ *
199
+ * `source: "agent"` is deliberate, and is the only system-assigned value the
200
+ * schema offers (`Session.titleSource` is "user" | "agent" | null). The database
201
+ * clobber guard pins only a `user` title, so this title still lets the agent
202
+ * rename its own session through set_session_title, and a human rename still
203
+ * wins permanently. Writing "user" here would quietly make every scheduled
204
+ * session unrenameable by the agent that runs in it.
205
+ *
206
+ * Returns the appended events so the caller can route them through the same
207
+ * defer-or-publish path as every other event this dispatch produces.
208
+ */
209
+ async function stampScheduledSessionTitle(
210
+ db: Database,
211
+ input: {
212
+ workspaceId: string;
213
+ session: { id: string; title: string | null };
214
+ taskName: string;
215
+ },
216
+ ): Promise<Awaited<ReturnType<typeof appendSessionEvents>>> {
217
+ if (input.session.title !== null) return [];
218
+ const title = scheduledTaskSessionTitle(input.taskName);
219
+ const result = await updateSessionTitle(db, {
220
+ workspaceId: input.workspaceId,
221
+ sessionId: input.session.id,
222
+ title,
223
+ source: "agent",
224
+ });
225
+ if (!result.updated) return [];
226
+ return await appendSessionEvents(db, input.workspaceId, input.session.id, [
227
+ { type: "session.title_set", payload: { title: result.title ?? title, source: "agent" } },
228
+ ]);
229
+ }
50
230
 
51
231
  export function createScheduledTaskActivities(services: () => Promise<ControlActivityServices>) {
52
232
  return {
@@ -61,6 +241,79 @@ export function createScheduledTaskActivities(services: () => Promise<ControlAct
61
241
  if (input.triggerType !== "scheduled" && !input.initiator) {
62
242
  return { action: "blocked", reason: "malformed_manual_trigger" };
63
243
  }
244
+ const stableProducerKey = scheduledTaskRunProducerKey(input);
245
+ const priorRun = await getScheduledTaskRunByProducerKey(db, {
246
+ workspaceId: input.workspaceId,
247
+ taskId: input.taskId,
248
+ triggerType: input.triggerType,
249
+ producerKey: stableProducerKey,
250
+ });
251
+ if (priorRun?.actionKind === "agent_turn") {
252
+ const acceptedExecution = await getScheduledTaskRunAcceptedExecution(db, {
253
+ workspaceId: input.workspaceId,
254
+ runId: priorRun.id,
255
+ });
256
+ if (!acceptedExecution) {
257
+ if (
258
+ priorRun.status === "succeeded" ||
259
+ priorRun.status === "failed" ||
260
+ priorRun.status === "skipped"
261
+ ) {
262
+ return scheduledRunTerminalResult(priorRun.error);
263
+ }
264
+ throw new Error("scheduled agent run is missing accepted execution truth");
265
+ }
266
+ await recordScheduledTaskFiredUsage(db, acceptedExecution, priorRun);
267
+ if (priorRun.status === "dispatched") {
268
+ return await replayScheduledTaskDispatch({
269
+ db,
270
+ wakeSessionWorkflow,
271
+ task: acceptedExecution.task,
272
+ run: priorRun,
273
+ acceptedExecution,
274
+ });
275
+ }
276
+ if (priorRun.status === "queued") {
277
+ try {
278
+ return await recoverBoundScheduledTaskDispatch({
279
+ db,
280
+ bus,
281
+ settings,
282
+ wakeSessionWorkflow,
283
+ run: priorRun,
284
+ acceptedExecution,
285
+ input,
286
+ });
287
+ } catch (error) {
288
+ const terminalError = scheduledRunRecoveryTerminalError(error);
289
+ if (!terminalError) throw error;
290
+ await markScheduledTaskRunFailedIfQueued(
291
+ db,
292
+ priorRun.workspaceId,
293
+ priorRun.id,
294
+ terminalError.code,
295
+ );
296
+ return scheduledRunTerminalResult(terminalError.code);
297
+ }
298
+ }
299
+ if (priorRun.status === "succeeded") {
300
+ if (!priorRun.sessionId || !priorRun.triggerEventId) {
301
+ throw new Error("succeeded scheduled run is missing its delivery identity");
302
+ }
303
+ return {
304
+ action: acceptedExecution.task.runMode === "new_session_per_run" ? "start" : "signal",
305
+ accountId: acceptedExecution.task.accountId,
306
+ workspaceId: acceptedExecution.task.workspaceId,
307
+ sessionId: priorRun.sessionId,
308
+ triggerEventId: priorRun.triggerEventId,
309
+ workflowId: workflowIdForSession(priorRun.sessionId),
310
+ workflowWakeRevision: null,
311
+ };
312
+ }
313
+ if (priorRun.status === "failed" || priorRun.status === "skipped") {
314
+ return scheduledRunTerminalResult(priorRun.error);
315
+ }
316
+ }
64
317
  const task = await getScheduledTask(db, input.workspaceId, input.taskId);
65
318
  // Deleting a schedule is authoritative. A fire workflow that was already
66
319
  // created may still start afterward; completing it as a no-op prevents an
@@ -75,11 +328,10 @@ export function createScheduledTaskActivities(services: () => Promise<ControlAct
75
328
  const run = await createScheduledTaskRun(db, {
76
329
  workspaceId: task.workspaceId,
77
330
  taskId: task.id,
331
+ taskAuthorityRevision: task.authorityRevision,
332
+ taskExecutionDigest: task.executionDigest,
78
333
  triggerType: input.triggerType,
79
- producerKey:
80
- input.producerKey ??
81
- input.agentRunUsageIdempotencyKey ??
82
- `knowledge-source-sync:${crypto.randomUUID()}`,
334
+ producerKey: stableProducerKey,
83
335
  scheduledAt: null,
84
336
  });
85
337
  await ensureKnowledgeSourceSyncState(db, task);
@@ -90,10 +342,7 @@ export function createScheduledTaskActivities(services: () => Promise<ControlAct
90
342
  scheduledTaskId: task.id,
91
343
  scheduledTaskRunId: run.id,
92
344
  cause: input.triggerType,
93
- producerKey:
94
- input.producerKey ??
95
- input.agentRunUsageIdempotencyKey ??
96
- `knowledge-source-sync:${run.id}`,
345
+ producerKey: stableProducerKey,
97
346
  sourceConfigGeneration: task.action.sourceConfigGeneration,
98
347
  sourceLifecycleGeneration: task.action.sourceLifecycleGeneration,
99
348
  });
@@ -105,8 +354,14 @@ export function createScheduledTaskActivities(services: () => Promise<ControlAct
105
354
  unit: "run",
106
355
  sourceResourceType: "scheduled_task_run",
107
356
  sourceResourceId: run.id,
108
- initiator: input.initiator ?? { kind: "service", subjectId: "scheduler" },
109
- initiatorContext: { scheduledTaskId: task.id, scheduledTaskRunId: run.id },
357
+ initiator: input.initiator ?? {
358
+ kind: "service",
359
+ subjectId: "scheduler",
360
+ },
361
+ initiatorContext: {
362
+ scheduledTaskId: task.id,
363
+ scheduledTaskRunId: run.id,
364
+ },
110
365
  origin: "scheduled_task",
111
366
  idempotencyKey: `usage:knowledge_source_sync.fired:${run.id}`,
112
367
  });
@@ -126,11 +381,274 @@ export function createScheduledTaskActivities(services: () => Promise<ControlAct
126
381
  overlapPolicy: task.overlapPolicy as "skip" | "buffer_one",
127
382
  };
128
383
  }
384
+ if (task.status !== "active") {
385
+ return { action: "blocked", reason: "scheduled_task_paused" };
386
+ }
387
+ const structuredAlertOccurrence = scheduledAlertOccurrenceIdentity({
388
+ workspaceId: task.workspaceId,
389
+ scheduledTaskId: task.id,
390
+ metadata: task.metadata,
391
+ });
392
+ const alertOccurrence =
393
+ task.runMode === "new_session_per_run" ? structuredAlertOccurrence : null;
394
+ const alertResponderSessionCreateIdempotencyKey = alertOccurrence
395
+ ? scheduledAlertResponderSessionCreateIdempotencyKey({
396
+ occurrence: alertOccurrence,
397
+ taskAuthorityRevision: task.authorityRevision,
398
+ taskExecutionDigest: task.executionDigest,
399
+ })
400
+ : null;
401
+ const incidentDeclaration = incidentTelemetryPreflightDeclaration(
402
+ task.agentConfig,
403
+ structuredAlertOccurrence !== null,
404
+ );
405
+ if (incidentDeclaration.action === "blocked") {
406
+ return incidentDeclaration;
407
+ }
129
408
  const taskPersonalConnectionDelegations = await getScheduledTaskPersonalConnectionDelegations(
130
409
  db,
131
410
  task.workspaceId,
132
411
  task.id,
133
412
  );
413
+ const taskConnectionAuthoritySubjects = [
414
+ ...new Set(
415
+ taskPersonalConnectionDelegations.map((delegation) => delegation.ownerSubjectId),
416
+ ),
417
+ ];
418
+ if (taskConnectionAuthoritySubjects.length > 1) {
419
+ throw new Error("scheduled connection authority has multiple causal humans");
420
+ }
421
+ const taskConnectionAuthoritySubjectId = taskConnectionAuthoritySubjects[0] ?? null;
422
+ const taskPersonalResourceAuthoritySubjectId =
423
+ await getScheduledTaskPersonalResourceAuthoritySubject(db, {
424
+ accountId: task.accountId,
425
+ workspaceId: task.workspaceId,
426
+ taskId: task.id,
427
+ taskAuthorityRevision: task.authorityRevision,
428
+ });
429
+ const taskRevisionAuthority = await getScheduledTaskRevisionAuthority(db, {
430
+ accountId: task.accountId,
431
+ workspaceId: task.workspaceId,
432
+ taskId: task.id,
433
+ taskAuthorityRevision: task.authorityRevision,
434
+ });
435
+ if (
436
+ taskConnectionAuthoritySubjectId &&
437
+ taskPersonalResourceAuthoritySubjectId &&
438
+ taskConnectionAuthoritySubjectId !== taskPersonalResourceAuthoritySubjectId
439
+ ) {
440
+ throw new Error("scheduled authority classes have different causal humans");
441
+ }
442
+ const taskAuthoritySubjectId =
443
+ taskRevisionAuthority?.subjectId ??
444
+ taskPersonalResourceAuthoritySubjectId ??
445
+ taskConnectionAuthoritySubjectId;
446
+ const model = task.agentConfig.model ?? settings.openaiModel;
447
+ const reasoningEffort = task.agentConfig.reasoningEffort ?? settings.openaiReasoningEffort;
448
+ const sandboxBackend = task.agentConfig.sandboxBackend ?? settings.sandboxBackend;
449
+ const taskTools = withFirstPartyTools(settings, task.agentConfig.tools);
450
+ const firstPartyMcpTools = resolveFirstPartyMcpToolPolicy(settings).default;
451
+ const generatedTarget =
452
+ task.runMode === "new_session_per_run" ||
453
+ (task.runMode === "reusable_session" && task.reusableSessionId === null);
454
+ const generatedSessionDepthPolicy = generatedTarget
455
+ ? await (async () => {
456
+ const workspace = await requireWorkspace(db, task.workspaceId);
457
+ const deployment = await getNestedAgentDepthDeploymentPolicy(db);
458
+ const taskOverride = task.agentConfig.maxNestedAgentDepth ?? null;
459
+ const workspaceOverride =
460
+ typeof workspace.settings.maxNestedAgentDepth === "number"
461
+ ? workspace.settings.maxNestedAgentDepth
462
+ : null;
463
+ return taskOverride !== null
464
+ ? {
465
+ effectiveMaxNestedAgentDepth: taskOverride,
466
+ nestedAgentDepthPolicySource: "session" as const,
467
+ codexCompactionMode: isCodexBilledModel(model)
468
+ ? resolveWorkspaceCodexCompactionDefault(workspace.settings)
469
+ : ("portable" as const),
470
+ }
471
+ : workspaceOverride !== null
472
+ ? {
473
+ effectiveMaxNestedAgentDepth: workspaceOverride,
474
+ nestedAgentDepthPolicySource: "workspace" as const,
475
+ codexCompactionMode: isCodexBilledModel(model)
476
+ ? resolveWorkspaceCodexCompactionDefault(workspace.settings)
477
+ : ("portable" as const),
478
+ }
479
+ : {
480
+ effectiveMaxNestedAgentDepth: deployment.maxNestedAgentDepth,
481
+ nestedAgentDepthPolicySource: deployment.policySource,
482
+ codexCompactionMode: isCodexBilledModel(model)
483
+ ? resolveWorkspaceCodexCompactionDefault(workspace.settings)
484
+ : ("portable" as const),
485
+ };
486
+ })()
487
+ : null;
488
+ const acceptedTargetSessionId = generatedTarget
489
+ ? null
490
+ : task.runMode === "existing_session"
491
+ ? task.targetSessionId
492
+ : task.reusableSessionId;
493
+ // A targeted task whose exact session was deleted has no target to
494
+ // resolve; database admission settles that occurrence terminally
495
+ // (`scheduled_target_session_unavailable`) instead of retrying forever.
496
+ const targetSessionExecutionBase = acceptedTargetSessionId
497
+ ? await getScheduledTargetSessionExecution(
498
+ db,
499
+ task.workspaceId,
500
+ acceptedTargetSessionId,
501
+ taskAuthoritySubjectId,
502
+ )
503
+ : null;
504
+ const targetSessionExecution = targetSessionExecutionBase
505
+ ? await (async () => {
506
+ const [capabilityServerIds, apiIntegrationServerIds] = await Promise.all([
507
+ listEnabledMcpCapabilityServerIds(db, task.workspaceId),
508
+ listInstalledApiIntegrationServerIdsForDelegations(
509
+ db,
510
+ task.workspaceId,
511
+ taskPersonalConnectionDelegations,
512
+ ),
513
+ ]);
514
+ const workspaceDefaultMcpServerIds = new Set(
515
+ settings.mcpServers.map((server) => server.id),
516
+ );
517
+ for (const id of capabilityServerIds) workspaceDefaultMcpServerIds.add(id);
518
+ for (const id of apiIntegrationServerIds) workspaceDefaultMcpServerIds.add(id);
519
+ const availableMcpServerIds = new Set(workspaceDefaultMcpServerIds);
520
+ for (const id of targetSessionExecutionBase.mcpServerIds) {
521
+ availableMcpServerIds.add(id);
522
+ }
523
+ const effective = resolveSessionToolPolicy({
524
+ toolPolicy: targetSessionExecutionBase.toolPolicy,
525
+ sessionTools: targetSessionExecutionBase.tools,
526
+ availableMcpServerIds,
527
+ defaultMcpServerIds: defaultSessionMcpServerIds(
528
+ [...workspaceDefaultMcpServerIds].map((id) => ({ id })),
529
+ ),
530
+ });
531
+ return {
532
+ ...targetSessionExecutionBase,
533
+ effectiveMcpServerIds: [
534
+ ...new Set(
535
+ effective.toolRefs.flatMap((tool) => (tool.kind === "mcp" ? [tool.id] : [])),
536
+ ),
537
+ ].sort(),
538
+ };
539
+ })()
540
+ : null;
541
+ const acceptedVariableSet =
542
+ generatedTarget && task.variableSetId
543
+ ? await getVariableSet(
544
+ db,
545
+ {
546
+ accountId: task.accountId,
547
+ workspaceId: task.workspaceId,
548
+ // Organization/workspace sets have no personal-resource causal
549
+ // subject. The scoped resolver still permits those common
550
+ // scopes for a service/legacy actor, while a user-owned set can
551
+ // only resolve through the exact frozen 0252 subject.
552
+ subjectId: taskAuthoritySubjectId ?? task.createdBy.subjectId,
553
+ },
554
+ task.variableSetId,
555
+ )
556
+ : null;
557
+ if (generatedTarget && task.variableSetId && !acceptedVariableSet) {
558
+ throw new Error(`variable set not found: ${task.variableSetId}`);
559
+ }
560
+ const acceptedRig =
561
+ generatedTarget && task.rigId
562
+ ? await getRig(
563
+ db,
564
+ {
565
+ accountId: task.accountId,
566
+ workspaceId: task.workspaceId,
567
+ subjectId: taskAuthoritySubjectId ?? task.createdBy.subjectId,
568
+ },
569
+ task.rigId,
570
+ )
571
+ : null;
572
+ if (generatedTarget && task.rigId && (!acceptedRig || !acceptedRig.activeVersion)) {
573
+ throw new Error(`rig has no active version to bind: ${task.rigId}`);
574
+ }
575
+ const acceptedRigDefaultVariableSets = acceptedRig?.activeVersion
576
+ ? await Promise.all(
577
+ acceptedRig.activeVersion.defaultVariableSetIds.map(async (variableSetId) => {
578
+ const variableSet = await getVariableSet(
579
+ db,
580
+ {
581
+ accountId: task.accountId,
582
+ workspaceId: task.workspaceId,
583
+ subjectId: taskAuthoritySubjectId ?? task.createdBy.subjectId,
584
+ },
585
+ variableSetId,
586
+ );
587
+ if (!variableSet) {
588
+ throw new Error(`rig default Variable Set not found: ${variableSetId}`);
589
+ }
590
+ return { id: variableSet.id, generation: variableSet.generation };
591
+ }),
592
+ )
593
+ : [];
594
+ let incidentPreflightRequired = incidentDeclaration.action === "required";
595
+ if (incidentPreflightRequired) {
596
+ const existingSessionId =
597
+ task.runMode === "existing_session" ? task.targetSessionId : task.reusableSessionId;
598
+ const canonicalAlertSession =
599
+ task.runMode === "new_session_per_run" && alertOccurrence
600
+ ? await getSessionByCreateIdempotencyKey(
601
+ db,
602
+ task.workspaceId,
603
+ alertResponderSessionCreateIdempotencyKey!,
604
+ )
605
+ : null;
606
+ const responderSession = existingSessionId
607
+ ? await requireSession(db, task.workspaceId, existingSessionId)
608
+ : canonicalAlertSession;
609
+ const responder = await resolveIncidentTelemetryResponderMetadata({
610
+ db,
611
+ settings,
612
+ task,
613
+ session: responderSession,
614
+ personalConnectionDelegations: taskPersonalConnectionDelegations,
615
+ personalResourceAuthoritySubjectId: taskAuthoritySubjectId,
616
+ executionPolicy: targetSessionExecution
617
+ ? {
618
+ tools: targetSessionExecution.tools,
619
+ firstPartyMcpTools: targetSessionExecution.firstPartyMcpTools,
620
+ firstPartyMcpPermissions: targetSessionExecution.firstPartyMcpPermissions,
621
+ variableSetId: targetSessionExecution.variableSetId,
622
+ rigId: targetSessionExecution.rigId,
623
+ rigVersionId: targetSessionExecution.rigVersionId,
624
+ toolPolicy: targetSessionExecution.toolPolicy,
625
+ mcpServerIds: targetSessionExecution.mcpServerIds,
626
+ toolPolicyVersion: targetSessionExecution.toolPolicyVersion,
627
+ }
628
+ : {
629
+ tools: taskTools,
630
+ firstPartyMcpTools,
631
+ firstPartyMcpPermissions: [...DEFAULT_FIRST_PARTY_MCP_PERMISSIONS],
632
+ variableSetId: acceptedVariableSet?.id ?? null,
633
+ rigId: acceptedRig?.id ?? null,
634
+ rigVersionId: acceptedRig?.activeVersion?.id ?? null,
635
+ toolPolicy: responderSession?.toolPolicy ?? {
636
+ mode: "explicit",
637
+ inheritedFromSessionId: null,
638
+ },
639
+ mcpServerIds: [],
640
+ toolPolicyVersion: responderSession?.toolPolicyVersion ?? null,
641
+ },
642
+ });
643
+ const preflight = evaluateIncidentTelemetryPreflight({
644
+ agentConfig: task.agentConfig,
645
+ incidentTriggered: structuredAlertOccurrence !== null,
646
+ alertOccurrenceLabels: structuredAlertOccurrence?.labels ?? null,
647
+ responder,
648
+ });
649
+ if (preflight.action === "blocked") return preflight;
650
+ incidentPreflightRequired = preflight.action === "ready";
651
+ }
134
652
  const taskXaiProviderAccountAuthoritySnapshot =
135
653
  await getScheduledTaskXaiProviderAccountAuthoritySnapshot(db, task.workspaceId, task.id);
136
654
  // A scheduled bot selection was authorized when the task was written,
@@ -150,447 +668,887 @@ export function createScheduledTaskActivities(services: () => Promise<ControlAct
150
668
  if (slackBotConnection && !slackBotMetadata) {
151
669
  throw new Error("OpenGeni Slack bot connection metadata is invalid");
152
670
  }
153
- const model = task.agentConfig.model ?? settings.openaiModel;
671
+ const xaiAuthoritySubjectId =
672
+ taskXaiProviderAccountAuthoritySnapshot.scope === "user" &&
673
+ task.createdBy.kind === "subject"
674
+ ? task.createdBy.subjectId
675
+ : null;
676
+ if (taskXaiProviderAccountAuthoritySnapshot.scope === "user" && !xaiAuthoritySubjectId) {
677
+ throw new Error("scheduled user-scoped xAI authority has no causal human");
678
+ }
679
+ if (
680
+ xaiAuthoritySubjectId &&
681
+ taskAuthoritySubjectId &&
682
+ xaiAuthoritySubjectId !== taskAuthoritySubjectId
683
+ ) {
684
+ throw new Error("scheduled authority classes have different causal humans");
685
+ }
686
+ const causalHumanSubjectId = taskAuthoritySubjectId ?? xaiAuthoritySubjectId;
687
+ // Workspace/organization Variable Sets and Rigs run through ordinary
688
+ // workspace authority; only a user-owned resource needs the exact frozen
689
+ // human, and the database admission fence re-proves the same rule for
690
+ // existing/warm targets.
691
+ if (
692
+ (acceptedVariableSet?.scope === "user" || acceptedRig?.scope === "user") &&
693
+ !causalHumanSubjectId
694
+ ) {
695
+ throw new Error("scheduled personal-resource execution has no causal human");
696
+ }
154
697
  const admissionDenial = await agentRunAdmissionDenial(service, {
155
698
  accountId: task.accountId,
156
699
  workspaceId: task.workspaceId,
157
- model,
700
+ model: targetSessionExecution?.model ?? model,
158
701
  requestedAgentRuns: input.agentRunUsageIdempotencyKey ? 0 : 1,
159
702
  });
160
703
  if (admissionDenial) {
161
704
  return { action: "blocked", reason: admissionDenial };
162
705
  }
163
- const run = await createScheduledTaskRun(db, {
164
- workspaceId: task.workspaceId,
165
- taskId: task.id,
166
- triggerType: input.triggerType,
167
- producerKey:
168
- input.producerKey ??
169
- input.agentRunUsageIdempotencyKey ??
170
- `scheduled:${crypto.randomUUID()}`,
171
- scheduledAt: null,
172
- });
173
- await recordUsageEvent(db, {
174
- accountId: task.accountId,
175
- workspaceId: task.workspaceId,
176
- eventType: "scheduled_task.fired",
177
- quantity: 1,
178
- unit: "run",
179
- sourceResourceType: "scheduled_task_run",
180
- sourceResourceId: run.id,
181
- initiator: input.initiator ?? { kind: "service", subjectId: "scheduler" },
182
- initiatorContext: { scheduledTaskId: task.id, scheduledTaskRunId: run.id },
183
- origin: "scheduled_task",
184
- idempotencyKey: `usage:scheduled_task.fired:${run.id}`,
185
- });
186
- if (run.status === "dispatched" && run.sessionId && run.triggerEventId) {
187
- await recordUsageEvent(db, {
188
- accountId: task.accountId,
189
- workspaceId: task.workspaceId,
190
- eventType: "agent_run.created",
191
- quantity: 1,
192
- unit: "run",
193
- sourceResourceType: "scheduled_task_run",
194
- sourceResourceId: run.id,
195
- sessionId: run.sessionId,
196
- initiator: input.initiator ?? { kind: "service", subjectId: "scheduler" },
197
- initiatorContext: { scheduledTaskId: task.id, scheduledTaskRunId: run.id },
198
- origin: "scheduled_task",
199
- idempotencyKey:
200
- input.agentRunUsageIdempotencyKey ?? `usage:agent_run.created:scheduled:${run.id}`,
201
- });
202
- const workflowId = workflowIdForSession(run.sessionId);
203
- const workflowWakeRevision = await enqueueSessionWorkflowWakeIfRunnable(db, {
204
- accountId: task.accountId,
706
+ const deferredEvents: Array<{
707
+ sessionId: string;
708
+ events: Awaited<ReturnType<typeof appendSessionEvents>>;
709
+ }> = [];
710
+ const executeDispatch = async (dispatchDb: Database, deferPublications: boolean) => {
711
+ const admittedRunId = crypto.randomUUID();
712
+ const generatedSessionBinding = generatedTarget
713
+ ? {
714
+ createIdempotencyKey:
715
+ alertResponderSessionCreateIdempotencyKey ??
716
+ (task.runMode === "reusable_session"
717
+ ? `scheduled-task-reusable:${task.id}:${task.authorityRevision}:${task.executionDigest}`
718
+ : `scheduled-task-run:${admittedRunId}`),
719
+ effectiveMaxNestedAgentDepth:
720
+ generatedSessionDepthPolicy!.effectiveMaxNestedAgentDepth,
721
+ nestedAgentDepthPolicySource:
722
+ generatedSessionDepthPolicy!.nestedAgentDepthPolicySource,
723
+ codexCompactionMode: generatedSessionDepthPolicy!.codexCompactionMode,
724
+ }
725
+ : null;
726
+ const acceptedExecution: ScheduledTaskRunAcceptedExecution = {
727
+ version: 1,
728
+ task,
729
+ resolvedModel: model,
730
+ resolvedReasoningEffort: reasoningEffort,
731
+ resolvedLatencyMode: "standard",
732
+ resolvedSandboxBackend: sandboxBackend,
733
+ resolvedSandboxOs: "linux",
734
+ resolvedTools: taskTools,
735
+ resolvedFirstPartyMcpTools: firstPartyMcpTools,
736
+ resolvedFirstPartyMcpPermissions: [...DEFAULT_FIRST_PARTY_MCP_PERMISSIONS],
737
+ resolvedVariableSet: acceptedVariableSet
738
+ ? { id: acceptedVariableSet.id, generation: acceptedVariableSet.generation }
739
+ : null,
740
+ resolvedRig: acceptedRig?.activeVersion
741
+ ? {
742
+ id: acceptedRig.id,
743
+ versionId: acceptedRig.activeVersion.id,
744
+ defaultVariableSets: acceptedRigDefaultVariableSets,
745
+ }
746
+ : null,
747
+ resolvedSlackBotConnection:
748
+ slackBotConnection && slackBotMetadata && slackBotConnection.verifiedInstallVersion
749
+ ? {
750
+ id: slackBotConnection.id,
751
+ version: slackBotConnection.version,
752
+ verifiedInstallVersion: slackBotConnection.verifiedInstallVersion,
753
+ metadata: slackBotMetadata,
754
+ }
755
+ : null,
756
+ targetSessionExecution,
757
+ generatedSessionBinding,
758
+ personalConnectionDelegations: taskPersonalConnectionDelegations,
759
+ personalResourceAuthoritySubjectId: taskPersonalResourceAuthoritySubjectId,
760
+ causalHumanSubjectId,
761
+ causalHumanAuthority: taskRevisionAuthority,
762
+ xaiProviderAccountAuthoritySnapshot: taskXaiProviderAccountAuthoritySnapshot,
763
+ xaiAuthoritySubjectId,
764
+ connectionAuthoritySubjectId: taskConnectionAuthoritySubjectId,
765
+ triggerInitiator: input.initiator ?? { kind: "service", subjectId: "scheduler" },
766
+ agentRunUsageIdempotencyKey: input.agentRunUsageIdempotencyKey ?? null,
767
+ incidentPreflightRequired,
768
+ alertOccurrenceLabels: structuredAlertOccurrence?.labels ?? null,
769
+ };
770
+ // The accepted snapshot is immutable execution truth. A legacy task
771
+ // whose stored shape can no longer be represented as a bounded
772
+ // accepted execution is a deterministic, operator-visible block, not a
773
+ // retryable activity failure that would silently drop the occurrence.
774
+ const acceptedExecutionParse =
775
+ ScheduledTaskRunAcceptedExecution.safeParse(acceptedExecution);
776
+ if (
777
+ !acceptedExecutionParse.success ||
778
+ scheduledOccurrencePayloadUtf8Bytes({
779
+ prompt: task.agentConfig.prompt,
780
+ resources: task.agentConfig.resources,
781
+ tools: taskTools,
782
+ }) > SCHEDULED_TASK_OCCURRENCE_PAYLOAD_MAX_BYTES
783
+ ) {
784
+ return {
785
+ kind: "blocked" as const,
786
+ result: {
787
+ action: "blocked" as const,
788
+ reason: "scheduled_execution_unrepresentable" as const,
789
+ },
790
+ };
791
+ }
792
+ let run = await createScheduledTaskRun(dispatchDb, {
793
+ runId: admittedRunId,
205
794
  workspaceId: task.workspaceId,
206
- sessionId: run.sessionId,
207
- temporalWorkflowId: workflowId,
208
- reason: "scheduled_retry",
795
+ taskId: task.id,
796
+ taskAuthorityRevision: task.authorityRevision,
797
+ taskExecutionDigest: task.executionDigest,
798
+ triggerType: input.triggerType,
799
+ producerKey: stableProducerKey,
800
+ scheduledAt: null,
801
+ acceptedExecutionSnapshot: acceptedExecution,
209
802
  });
210
- const result = {
211
- action: task.runMode === "new_session_per_run" ? "start" : "signal",
212
- accountId: task.accountId,
213
- workspaceId: task.workspaceId,
214
- sessionId: run.sessionId,
215
- triggerEventId: run.triggerEventId,
216
- workflowId,
217
- workflowWakeRevision,
218
- } as const;
219
- if (wakeSessionWorkflow && workflowWakeRevision !== null) {
220
- await wakeSessionWorkflow({
221
- accountId: result.accountId,
222
- workspaceId: result.workspaceId,
223
- sessionId: result.sessionId,
224
- workflowId: result.workflowId,
225
- wakeRevision: workflowWakeRevision,
226
- });
803
+ await recordScheduledTaskFiredUsage(dispatchDb, acceptedExecution, run);
804
+ if (run.status === "failed" && run.error === "scheduled_authority_exhausted") {
805
+ return {
806
+ kind: "blocked" as const,
807
+ result: {
808
+ action: "blocked" as const,
809
+ reason: "scheduled_authority_exhausted" as const,
810
+ },
811
+ run,
812
+ };
227
813
  }
228
- return result;
229
- }
230
- let result: DispatchScheduledTaskRunResult;
231
- try {
232
- const reasoningEffort = task.agentConfig.reasoningEffort ?? settings.openaiReasoningEffort;
233
- const sandboxBackend = task.agentConfig.sandboxBackend ?? settings.sandboxBackend;
234
- const goalSpec = task.agentConfig.goal ?? null;
235
- // Every dispatch carries the first-party MCP server; runtime visibility
236
- // still follows the session's exact selection and authorization.
237
- const taskTools = withFirstPartyTools(settings, task.agentConfig.tools);
238
- const existingSessionId =
239
- task.runMode === "existing_session" ? task.targetSessionId : task.reusableSessionId;
240
- if (task.runMode === "existing_session" && !existingSessionId) {
241
- throw new Error("scheduled task target session is unavailable");
814
+ if (run.status === "failed" || run.status === "skipped") {
815
+ return {
816
+ kind: "blocked" as const,
817
+ result: scheduledRunTerminalResult(run.error),
818
+ run,
819
+ };
242
820
  }
243
- if (
244
- task.runMode === "new_session_per_run" ||
245
- (task.runMode === "reusable_session" && !existingSessionId)
246
- ) {
247
- // The FK on scheduled_tasks.variable_set_id is ON DELETE RESTRICT, so
248
- // an attached variableSet must still exist here; fail closed if not.
249
- const variableSet = task.variableSetId
250
- ? await getVariableSet(db, task.workspaceId, task.variableSetId)
251
- : null;
252
- if (task.variableSetId && !variableSet) {
253
- throw new Error(`variable set not found: ${task.variableSetId}`);
821
+ const runPersonalResourceAuthority = await getScheduledTaskRunPersonalResourceAuthority(
822
+ dispatchDb,
823
+ {
824
+ accountId: task.accountId,
825
+ workspaceId: task.workspaceId,
826
+ runId: run.id,
827
+ },
828
+ );
829
+ if (run?.status === "dispatched" && run.sessionId && run.triggerEventId) {
830
+ if (deferPublications) {
831
+ return { kind: "replay" as const, run, acceptedExecution };
254
832
  }
255
- // RIG BINDING (M3): resolve the task's rig to its CURRENTLY-ACTIVE
256
- // version at FIRE time (not task-create time) and freeze that version
257
- // onto the new session — a task always runs the rig's latest active
258
- // version. A deleted rig FK-nulls task.rigId (rig-less run); a rig that
259
- // somehow has no active version fails the fire closed.
260
- let frozenRigId: string | null = null;
261
- let frozenRigVersionId: string | null = null;
262
- if (task.rigId) {
263
- const rig = await getRig(db, task.workspaceId, task.rigId);
264
- if (!rig || !rig.activeVersion) {
265
- throw new Error(`rig has no active version to bind: ${task.rigId}`);
833
+ await recordUsageEvent(dispatchDb, {
834
+ accountId: task.accountId,
835
+ workspaceId: task.workspaceId,
836
+ eventType: "agent_run.created",
837
+ quantity: 1,
838
+ unit: "run",
839
+ sourceResourceType: "scheduled_task_run",
840
+ sourceResourceId: run.id,
841
+ sessionId: run.sessionId,
842
+ initiator: acceptedExecution.triggerInitiator,
843
+ initiatorContext: {
844
+ scheduledTaskId: task.id,
845
+ scheduledTaskRunId: run.id,
846
+ },
847
+ origin: "scheduled_task",
848
+ idempotencyKey:
849
+ acceptedExecution.agentRunUsageIdempotencyKey ??
850
+ `usage:agent_run.created:scheduled:${run.id}`,
851
+ });
852
+ const workflowId = workflowIdForSession(run.sessionId);
853
+ const workflowWakeRevision = await enqueueSessionWorkflowWakeIfRunnable(dispatchDb, {
854
+ accountId: task.accountId,
855
+ workspaceId: task.workspaceId,
856
+ sessionId: run.sessionId,
857
+ temporalWorkflowId: workflowId,
858
+ reason: "scheduled_retry",
859
+ });
860
+ const result = {
861
+ action: task.runMode === "new_session_per_run" ? "start" : "signal",
862
+ accountId: task.accountId,
863
+ workspaceId: task.workspaceId,
864
+ sessionId: run.sessionId,
865
+ triggerEventId: run.triggerEventId,
866
+ workflowId,
867
+ workflowWakeRevision,
868
+ } as const;
869
+ return { kind: "complete" as const, result, run };
870
+ }
871
+ let result: DispatchScheduledTaskRunResult;
872
+ try {
873
+ if (incidentPreflightRequired) {
874
+ const exactAuthority = await requireScheduledTaskIncidentAuthorityInTransaction(
875
+ dispatchDb,
876
+ {
877
+ workspaceId: task.workspaceId,
878
+ taskId: task.id,
879
+ },
880
+ );
881
+ if (
882
+ stableJson({
883
+ task: exactAuthority.task,
884
+ personalConnectionDelegations: exactAuthority.personalConnectionDelegations,
885
+ }) !==
886
+ stableJson({
887
+ task,
888
+ personalConnectionDelegations: taskPersonalConnectionDelegations,
889
+ })
890
+ ) {
891
+ throw new IncidentTelemetryPreflightBlockedError(
892
+ "incident_preflight_metadata_missing",
893
+ );
266
894
  }
267
- frozenRigId = rig.id;
268
- frozenRigVersionId = rig.activeVersion.id;
269
895
  }
270
- let session: Awaited<ReturnType<typeof createSession>>;
271
- const taskMetadata = { ...task.agentConfig.metadata };
272
- delete taskMetadata[OPENGENI_SLACK_BOT_SESSION_METADATA_KEY];
273
- try {
274
- session = await createSession(db, {
275
- accountId: task.accountId,
276
- workspaceId: task.workspaceId,
277
- initialMessage: task.agentConfig.prompt,
278
- resources: task.agentConfig.resources,
279
- tools: taskTools,
280
- firstPartyMcpTools: resolveFirstPartyMcpToolPolicy(settings).default,
281
- metadata: {
282
- ...taskMetadata,
896
+ const goalSpec = task.agentConfig.goal ?? null;
897
+ // Every dispatch carries the first-party MCP server; runtime visibility
898
+ // still follows the session's exact selection and authorization.
899
+ const existingSessionId =
900
+ task.runMode === "existing_session" ? task.targetSessionId : task.reusableSessionId;
901
+ if (task.runMode === "existing_session" && !existingSessionId) {
902
+ throw new Error("scheduled task target session is unavailable");
903
+ }
904
+ if (
905
+ task.runMode === "new_session_per_run" ||
906
+ (task.runMode === "reusable_session" && !existingSessionId)
907
+ ) {
908
+ // The FK on scheduled_tasks.variable_set_id is ON DELETE RESTRICT, so
909
+ // an attached variableSet must still exist here; fail closed if not.
910
+ const variableSet = acceptedVariableSet;
911
+ if (task.variableSetId && !variableSet) {
912
+ throw new Error(`variable set not found: ${task.variableSetId}`);
913
+ }
914
+ // RIG BINDING (M3): resolve the task's rig to its CURRENTLY-ACTIVE
915
+ // version at FIRE time (not task-create time) and freeze that version
916
+ // onto the new session — a task always runs the rig's latest active
917
+ // version. A deleted rig FK-nulls task.rigId (rig-less run); a rig that
918
+ // somehow has no active version fails the fire closed.
919
+ let frozenRigId: string | null = null;
920
+ let frozenRigVersionId: string | null = null;
921
+ if (task.rigId) {
922
+ const rig = acceptedRig;
923
+ if (!rig || !rig.activeVersion) {
924
+ throw new Error(`rig has no active version to bind: ${task.rigId}`);
925
+ }
926
+ frozenRigId = rig.id;
927
+ frozenRigVersionId =
928
+ runPersonalResourceAuthority?.resources.find(
929
+ (resource) => resource.resourceKind === "rig" && resource.resourceId === rig.id,
930
+ )?.resourceVersionId ?? rig.activeVersion.id;
931
+ if (frozenRigVersionId !== rig.activeVersion.id) {
932
+ throw new Error("scheduled run rig version changed during admission");
933
+ }
934
+ }
935
+ let session: Awaited<ReturnType<typeof createSession>>;
936
+ let sessionCreated = true;
937
+ const taskMetadata = { ...task.agentConfig.metadata };
938
+ delete taskMetadata[OPENGENI_SLACK_BOT_SESSION_METADATA_KEY];
939
+ try {
940
+ const sessionInput: Parameters<typeof createSession>[1] = {
941
+ accountId: task.accountId,
942
+ workspaceId: task.workspaceId,
943
+ initialMessage: task.agentConfig.prompt,
944
+ resources: task.agentConfig.resources,
945
+ tools: taskTools,
946
+ firstPartyMcpTools,
947
+ firstPartyMcpPermissions: [...DEFAULT_FIRST_PARTY_MCP_PERMISSIONS],
948
+ metadata: {
949
+ ...taskMetadata,
950
+ model,
951
+ reasoningEffort,
952
+ scheduledTaskId: task.id,
953
+ scheduledTaskRunMode: task.runMode,
954
+ ...(goalSpec ? { scheduledTaskGoal: goalSpec } : {}),
955
+ ...(run ? { scheduledTaskRunId: run.id } : {}),
956
+ ...(slackBotConnection
957
+ ? {
958
+ [OPENGENI_SLACK_BOT_SESSION_METADATA_KEY]: slackBotConnection.id,
959
+ }
960
+ : {}),
961
+ },
962
+ createdBy: {
963
+ kind: "service",
964
+ subjectId: "scheduler",
965
+ label: "OpenGeni scheduler",
966
+ },
967
+ createdByContext: {
968
+ scheduledTaskId: task.id,
969
+ ...(run ? { scheduledTaskRunId: run.id } : {}),
970
+ },
283
971
  model,
284
972
  reasoningEffort,
285
- scheduledTaskId: task.id,
286
- scheduledTaskRunId: run.id,
287
- ...(slackBotConnection
288
- ? {
289
- [OPENGENI_SLACK_BOT_SESSION_METADATA_KEY]: slackBotConnection.id,
973
+ latencyMode: "standard",
974
+ sandboxBackend,
975
+ sandboxOs: "linux",
976
+ variableSetId: task.variableSetId ?? null,
977
+ rigId: frozenRigId,
978
+ rigVersionId: frozenRigVersionId,
979
+ personalConnectionDelegations: [],
980
+ initialXaiProviderAccountAuthoritySnapshot: taskXaiProviderAccountAuthoritySnapshot,
981
+ maxNestedAgentDepthOverride: task.agentConfig.maxNestedAgentDepth ?? null,
982
+ frozenNestedAgentDepthPolicy: {
983
+ effectiveMaxNestedAgentDepth:
984
+ generatedSessionBinding!.effectiveMaxNestedAgentDepth,
985
+ nestedAgentDepthPolicySource:
986
+ generatedSessionBinding!.nestedAgentDepthPolicySource,
987
+ },
988
+ frozenCodexCompactionMode: generatedSessionBinding!.codexCompactionMode,
989
+ // The durable agent config was privilege-checked when the task
990
+ // was created/updated. Preserve that explicit policy if a broader
991
+ // workspace/deployment limit is narrowed before a later fire.
992
+ allowNestedAgentDepthIncrease: true,
993
+ subjectId: `scheduled_task:${task.id}`,
994
+ beforeCreateCommit: async (tx, sessionId) => {
995
+ if (incidentPreflightRequired) {
996
+ const exactSession = await requireSession(tx, task.workspaceId, sessionId);
997
+ const exactResponder = await resolveIncidentTelemetryResponderMetadata({
998
+ db: tx,
999
+ settings,
1000
+ task: acceptedExecution.task,
1001
+ session: exactSession,
1002
+ personalConnectionDelegations:
1003
+ acceptedExecution.personalConnectionDelegations,
1004
+ personalResourceAuthoritySubjectId:
1005
+ acceptedExecution.personalResourceAuthoritySubjectId,
1006
+ executionPolicy: {
1007
+ tools: exactSession.tools,
1008
+ firstPartyMcpTools: exactSession.firstPartyMcpTools,
1009
+ firstPartyMcpPermissions: exactSession.firstPartyMcpPermissions,
1010
+ variableSetId: exactSession.variableSetId,
1011
+ rigId: exactSession.rigId,
1012
+ rigVersionId: exactSession.rigVersionId,
1013
+ toolPolicy: exactSession.toolPolicy,
1014
+ mcpServerIds: exactSession.mcpServers.map((server) => server.id),
1015
+ toolPolicyVersion: exactSession.toolPolicyVersion,
1016
+ },
1017
+ });
1018
+ const exactPreflight = evaluateIncidentTelemetryPreflight({
1019
+ agentConfig: acceptedExecution.task.agentConfig,
1020
+ incidentTriggered: structuredAlertOccurrence !== null,
1021
+ alertOccurrenceLabels: structuredAlertOccurrence?.labels ?? null,
1022
+ responder: exactResponder,
1023
+ });
1024
+ if (exactPreflight.action === "blocked") {
1025
+ throw new IncidentTelemetryPreflightBlockedError(exactPreflight.reason);
290
1026
  }
291
- : {}),
292
- },
293
- createdBy: {
294
- kind: "service",
295
- subjectId: "scheduler",
296
- label: "OpenGeni scheduler",
297
- },
298
- createdByContext: {
299
- scheduledTaskId: task.id,
300
- scheduledTaskRunId: run.id,
301
- },
302
- model,
303
- sandboxBackend,
304
- variableSetId: task.variableSetId ?? null,
305
- rigId: frozenRigId,
306
- rigVersionId: frozenRigVersionId,
307
- personalConnectionDelegations: taskPersonalConnectionDelegations,
308
- initialXaiProviderAccountAuthoritySnapshot: taskXaiProviderAccountAuthoritySnapshot,
309
- maxNestedAgentDepthOverride: task.agentConfig.maxNestedAgentDepth ?? null,
310
- // The durable agent config was privilege-checked when the task
311
- // was created/updated. Preserve that explicit policy if a broader
312
- // workspace/deployment limit is narrowed before a later fire.
313
- allowNestedAgentDepthIncrease: true,
314
- subjectId: `scheduled_task:${task.id}`,
315
- });
316
- } catch (error) {
317
- if (error instanceof SessionSpawnDeniedDbError) {
318
- throw new Error(`${error.denial.code}: denial=${error.denial.id}`, { cause: error });
1027
+ }
1028
+ await bindScheduledTaskRunSessionInTransaction(tx, {
1029
+ accountId: task.accountId,
1030
+ workspaceId: task.workspaceId,
1031
+ runId: run.id,
1032
+ sessionId,
1033
+ });
1034
+ },
1035
+ };
1036
+ const created = await createSessionWithIdempotencyKeyResult(dispatchDb, {
1037
+ ...sessionInput,
1038
+ createIdempotencyKey: generatedSessionBinding!.createIdempotencyKey,
1039
+ });
1040
+ if (created.denied) {
1041
+ throw new SessionSpawnDeniedDbError(created.denial);
1042
+ }
1043
+ session = created.session;
1044
+ sessionCreated = created.created;
1045
+ if (!sessionCreated) {
1046
+ await bindScheduledTaskRunSessionInTransaction(dispatchDb, {
1047
+ accountId: task.accountId,
1048
+ workspaceId: task.workspaceId,
1049
+ runId: run.id,
1050
+ sessionId: session.id,
1051
+ });
1052
+ run = { ...run, sessionId: session.id };
1053
+ }
1054
+ } catch (error) {
1055
+ if (error instanceof IncidentTelemetryPreflightBlockedError) {
1056
+ throw error;
1057
+ }
1058
+ if (error instanceof SessionSpawnDeniedDbError) {
1059
+ throw new Error(`${error.denial.code}: denial=${error.denial.id}`, {
1060
+ cause: error,
1061
+ });
1062
+ }
1063
+ throw error;
319
1064
  }
320
- throw error;
321
- }
322
- const goal = goalSpec
323
- ? await createSessionGoal(db, {
1065
+ const scheduledRun = run;
1066
+ if (!sessionCreated) {
1067
+ assertReusableSessionRevivable(session.status);
1068
+ if ((session.variableSetId ?? null) !== (task.variableSetId ?? null)) {
1069
+ throw new Error(
1070
+ "scheduled alert occurrence variableSet attachment does not match its canonical session",
1071
+ );
1072
+ }
1073
+ if (
1074
+ scheduledSlackBotConnectionId(session.metadata) !==
1075
+ (task.agentConfig.slackBotConnectionId ?? null)
1076
+ ) {
1077
+ throw new Error(
1078
+ "scheduled alert occurrence OpenGeni Slack bot binding does not match its canonical session",
1079
+ );
1080
+ }
1081
+ }
1082
+ let workflowId: string;
1083
+ if (alertOccurrence) {
1084
+ const started = await initializeSessionStartAtomically(dispatchDb, {
324
1085
  accountId: task.accountId,
325
1086
  workspaceId: task.workspaceId,
326
1087
  sessionId: session.id,
327
- text: goalSpec.text,
328
- successCriteria: goalSpec.successCriteria ?? null,
329
- maxAutoContinuations: goalSpec.maxAutoContinuations ?? null,
330
- createdBy: "scheduled_task",
331
- })
332
- : null;
333
- const workflowId = workflowIdForSession(session.id);
334
- await setTemporalWorkflowId(db, task.workspaceId, session.id, workflowId);
335
- if (task.runMode === "reusable_session") {
336
- await updateScheduledTask(db, task.workspaceId, task.id, {
337
- reusableSessionId: session.id,
338
- });
339
- }
340
- await appendAndPublishEvents(db, bus, task.workspaceId, session.id, [
341
- {
342
- type: "session.created",
343
- payload: {
344
- status: session.status,
345
- createdBy: session.createdBy,
346
- scheduledTaskId: task.id,
347
- scheduledTaskRunId: run.id,
348
- // Names/ids only; never values.
349
- ...(variableSet
350
- ? { variableSetId: variableSet.id, variableSetName: variableSet.name }
351
- : {}),
352
- ...(slackBotConnection && slackBotMetadata
1088
+ reasoningEffortFallback: reasoningEffort,
1089
+ createdEventPayload: {
1090
+ scheduledTaskId:
1091
+ typeof session.metadata.scheduledTaskId === "string"
1092
+ ? session.metadata.scheduledTaskId
1093
+ : task.id,
1094
+ scheduledTaskRunId:
1095
+ typeof session.metadata.scheduledTaskRunId === "string"
1096
+ ? session.metadata.scheduledTaskRunId
1097
+ : run.id,
1098
+ // Names/ids only; never values.
1099
+ ...(variableSet
1100
+ ? {
1101
+ variableSetId: variableSet.id,
1102
+ variableSetName: variableSet.name,
1103
+ }
1104
+ : {}),
1105
+ ...(slackBotConnection && slackBotMetadata
1106
+ ? {
1107
+ slackBotConnection: {
1108
+ credentialRole: OPENGENI_SLACK_BOT_CREDENTIAL_ROLE,
1109
+ credentialLabel: OPENGENI_SLACK_BOT_CREDENTIAL_LABEL,
1110
+ connectionId: slackBotConnection.id,
1111
+ slackTeamId: slackBotMetadata.slackTeamId,
1112
+ },
1113
+ }
1114
+ : {}),
1115
+ },
1116
+ goal: goalSpec
353
1117
  ? {
354
- slackBotConnection: {
355
- credentialRole: OPENGENI_SLACK_BOT_CREDENTIAL_ROLE,
356
- credentialLabel: OPENGENI_SLACK_BOT_CREDENTIAL_LABEL,
357
- connectionId: slackBotConnection.id,
358
- slackTeamId: slackBotMetadata.slackTeamId,
359
- },
1118
+ text: goalSpec.text,
1119
+ successCriteria: goalSpec.successCriteria ?? null,
1120
+ maxAutoContinuations: goalSpec.maxAutoContinuations ?? null,
1121
+ ...(goalSpec.mutationPolicy
1122
+ ? { mutationPolicy: goalSpec.mutationPolicy }
1123
+ : {}),
1124
+ createdBy: "scheduled_task",
360
1125
  }
361
- : {}),
362
- },
363
- },
364
- ...(goal
365
- ? [
366
- {
367
- type: "goal.set" as const,
368
- payload: {
369
- goalId: goal.id,
370
- text: goal.text,
371
- ...(goal.successCriteria ? { successCriteria: goal.successCriteria } : {}),
372
- version: goal.version,
373
- actor: "scheduled_task",
374
- replaced: false,
375
- },
376
- },
377
- ]
378
- : []),
379
- { type: "session.status.changed", payload: { status: session.status } },
380
- ]);
381
- const scheduledUpdate = await addSessionSystemUpdateWithSourceMutation(
382
- db,
383
- {
384
- accountId: task.accountId,
385
- workspaceId: task.workspaceId,
386
- sessionId: session.id,
387
- kind: "scheduled_occurrence",
388
- classification: "info",
389
- sourceId: run.id,
390
- dedupeKey: `scheduled-wake:${run.id}`,
391
- summary: task.agentConfig.prompt,
392
- payload: scheduledUserMessagePayload(
393
- task.agentConfig.prompt,
394
- task.agentConfig.resources,
395
- taskTools,
396
- task.id,
397
- run.id,
398
- ),
399
- lineage: {
400
- scheduledTaskId: task.id,
401
- scheduledTaskRunId: run.id,
402
- ...(taskXaiProviderAccountAuthoritySnapshot.scope === "user" &&
403
- task.createdBy.kind === "subject"
404
- ? { xaiAuthoritySubjectId: task.createdBy.subjectId }
405
- : {}),
406
- },
407
- personalConnectionDelegations: taskPersonalConnectionDelegations,
408
- xaiProviderAccountAuthoritySnapshot: taskXaiProviderAccountAuthoritySnapshot,
409
- },
410
- async (tx, wakeEventId) => {
411
- if (!wakeEventId) throw new Error("Scheduled delivery has no wake event");
412
- await settleScheduledTaskRunInTransaction(tx, {
1126
+ : null,
1127
+ deferInitialTurn: true,
1128
+ deferredStatus: "queued",
1129
+ });
1130
+ workflowId = started.temporalWorkflowId;
1131
+ if (started.events.length > 0) {
1132
+ if (deferPublications) {
1133
+ deferredEvents.push({
1134
+ sessionId: session.id,
1135
+ events: started.events,
1136
+ });
1137
+ } else {
1138
+ await publishDurableSessionEvents(
1139
+ bus,
1140
+ task.workspaceId,
1141
+ session.id,
1142
+ started.events,
1143
+ );
1144
+ }
1145
+ }
1146
+ } else {
1147
+ const started = await initializeSessionStartAtomically(dispatchDb, {
1148
+ accountId: task.accountId,
413
1149
  workspaceId: task.workspaceId,
414
- runId: run.id,
415
1150
  sessionId: session.id,
416
- triggerEventId: wakeEventId,
417
- status: "dispatched",
1151
+ reasoningEffortFallback: reasoningEffort,
1152
+ createdEventPayload: {
1153
+ scheduledTaskId:
1154
+ typeof session.metadata.scheduledTaskId === "string"
1155
+ ? session.metadata.scheduledTaskId
1156
+ : task.id,
1157
+ scheduledTaskRunId:
1158
+ typeof session.metadata.scheduledTaskRunId === "string"
1159
+ ? session.metadata.scheduledTaskRunId
1160
+ : run.id,
1161
+ // Names/ids only; never values.
1162
+ ...(variableSet
1163
+ ? { variableSetId: variableSet.id, variableSetName: variableSet.name }
1164
+ : {}),
1165
+ ...(slackBotConnection && slackBotMetadata
1166
+ ? {
1167
+ slackBotConnection: {
1168
+ credentialRole: OPENGENI_SLACK_BOT_CREDENTIAL_ROLE,
1169
+ credentialLabel: OPENGENI_SLACK_BOT_CREDENTIAL_LABEL,
1170
+ connectionId: slackBotConnection.id,
1171
+ slackTeamId: slackBotMetadata.slackTeamId,
1172
+ },
1173
+ }
1174
+ : {}),
1175
+ },
1176
+ goal: goalSpec
1177
+ ? {
1178
+ text: goalSpec.text,
1179
+ successCriteria: goalSpec.successCriteria ?? null,
1180
+ maxAutoContinuations: goalSpec.maxAutoContinuations ?? null,
1181
+ ...(goalSpec.mutationPolicy
1182
+ ? { mutationPolicy: goalSpec.mutationPolicy }
1183
+ : {}),
1184
+ createdBy: "scheduled_task",
1185
+ }
1186
+ : null,
1187
+ deferInitialTurn: true,
1188
+ deferredStatus: "queued",
418
1189
  });
419
- },
420
- );
421
- if (scheduledUpdate.reason === "session_cancelled") {
422
- throw new Error("new scheduled session was cancelled during dispatch");
423
- }
424
- if (scheduledUpdate.added && scheduledUpdate.events.length > 0) {
425
- await publishDurableSessionEvents(
426
- bus,
427
- task.workspaceId,
428
- session.id,
429
- scheduledUpdate.events,
430
- );
431
- }
432
- result = {
433
- action: "start",
434
- accountId: task.accountId,
435
- workspaceId: task.workspaceId,
436
- sessionId: session.id,
437
- triggerEventId: scheduledUpdate.wakeEventId,
438
- workflowId,
439
- workflowWakeRevision: scheduledUpdate.workflowWakeRevision,
440
- };
441
- } else {
442
- const session = await requireSession(db, task.workspaceId, existingSessionId!);
443
- // A user-cancelled (terminal) reusable session must not be revived and
444
- // re-billed on the next fire. Early check avoids the pre-lock goal
445
- // upsert side-effect; the locked-callback check below is the
446
- // authoritative atomic guard. Mirrors apps/api/src/domain/sessions.ts.
447
- assertReusableSessionRevivable(session.status);
448
- // Defensive backstop for the API-level 409: a reusable session keeps
449
- // its creation-time attachment, so a diverged task attachment must
450
- // fail the run instead of silently running with the wrong secrets.
451
- if ((session.variableSetId ?? null) !== (task.variableSetId ?? null)) {
452
- throw new Error(
453
- "scheduled task variableSet attachment does not match its reusable session",
454
- );
455
- }
456
- if (
457
- scheduledSlackBotConnectionId(session.metadata) !==
458
- (task.agentConfig.slackBotConnectionId ?? null)
459
- ) {
460
- throw new Error(
461
- "scheduled task OpenGeni Slack bot binding does not match its reusable session",
462
- );
463
- }
464
- // A recurring "maintain X" task re-establishes its objective on every
465
- // fire: replace the goal text, reactivate it, and reset the counters.
466
- const reusableGoal =
467
- task.runMode === "reusable_session" && goalSpec && run.status === "queued"
468
- ? await upsertSessionGoal(db, {
1190
+ workflowId = started.temporalWorkflowId;
1191
+ if (started.events.length > 0) {
1192
+ if (deferPublications) {
1193
+ deferredEvents.push({ sessionId: session.id, events: started.events });
1194
+ } else {
1195
+ await publishDurableSessionEvents(
1196
+ bus,
1197
+ task.workspaceId,
1198
+ session.id,
1199
+ started.events,
1200
+ );
1201
+ }
1202
+ }
1203
+ if (task.runMode === "reusable_session") {
1204
+ await materializeScheduledTaskReusableSessionFromRun(dispatchDb, {
469
1205
  accountId: task.accountId,
470
1206
  workspaceId: task.workspaceId,
1207
+ taskId: task.id,
1208
+ runId: run.id,
471
1209
  sessionId: session.id,
472
- text: goalSpec.text,
473
- successCriteria: goalSpec.successCriteria ?? null,
474
- maxAutoContinuations: goalSpec.maxAutoContinuations ?? null,
475
- createdBy: "scheduled_task",
476
- })
477
- : null;
478
- if (reusableGoal) {
479
- const goalEvents = await appendSessionEventsWithLockedSessionUpdate(
480
- db,
481
- task.workspaceId,
482
- session.id,
483
- (locked) => {
484
- assertReusableSessionRevivable(locked.status);
485
- return {
486
- events: [
487
- {
488
- type: "goal.set" as const,
489
- payload: {
490
- goalId: reusableGoal.goal.id,
491
- text: reusableGoal.goal.text,
492
- ...(reusableGoal.goal.successCriteria
493
- ? { successCriteria: reusableGoal.goal.successCriteria }
494
- : {}),
495
- version: reusableGoal.goal.version,
496
- actor: "scheduled_task",
497
- replaced: reusableGoal.replaced,
498
- },
499
- },
500
- ],
501
- };
1210
+ sourceTaskAuthorityRevision:
1211
+ runPersonalResourceAuthority?.taskAuthorityRevision ??
1212
+ run.taskAuthorityRevision!,
1213
+ sourceExecutionDigest:
1214
+ runPersonalResourceAuthority?.executionDigest ?? run.taskExecutionDigest!,
1215
+ });
1216
+ }
1217
+ }
1218
+ // After both start branches, so the timeline reads session.created
1219
+ // then session.title_set rather than titling a session that has not
1220
+ // announced itself yet. `session.created` is what carries the row
1221
+ // into the rail, and this lands on its heels.
1222
+ const titleEvents = await stampScheduledSessionTitle(dispatchDb, {
1223
+ workspaceId: task.workspaceId,
1224
+ session,
1225
+ taskName: task.name,
1226
+ });
1227
+ if (titleEvents.length > 0) {
1228
+ if (deferPublications) {
1229
+ deferredEvents.push({ sessionId: session.id, events: titleEvents });
1230
+ } else {
1231
+ await publishDurableSessionEvents(bus, task.workspaceId, session.id, titleEvents);
1232
+ }
1233
+ }
1234
+ const scheduledUpdate = await addSessionSystemUpdateWithSourceMutation(
1235
+ dispatchDb,
1236
+ {
1237
+ accountId: task.accountId,
1238
+ workspaceId: task.workspaceId,
1239
+ sessionId: session.id,
1240
+ kind: "scheduled_occurrence",
1241
+ classification: "info",
1242
+ sourceId: run.id,
1243
+ dedupeKey: `scheduled-task-run:${run.id}`,
1244
+ summary: task.agentConfig.prompt,
1245
+ payload: scheduledUserMessagePayload(
1246
+ task.agentConfig.prompt,
1247
+ task.agentConfig.resources,
1248
+ taskTools,
1249
+ task.id,
1250
+ run.id,
1251
+ ),
1252
+ lineage: {
1253
+ scheduledTaskId: task.id,
1254
+ scheduledTaskRunId: run.id,
1255
+ ...(causalHumanSubjectId ? { causalHumanSubjectId } : {}),
1256
+ ...(taskConnectionAuthoritySubjectId
1257
+ ? { connectionAuthoritySubjectId: taskConnectionAuthoritySubjectId }
1258
+ : {}),
1259
+ ...(taskXaiProviderAccountAuthoritySnapshot.scope === "user" &&
1260
+ task.createdBy.kind === "subject"
1261
+ ? { xaiAuthoritySubjectId: task.createdBy.subjectId }
1262
+ : {}),
1263
+ },
1264
+ personalConnectionDelegations: taskPersonalConnectionDelegations,
1265
+ xaiProviderAccountAuthoritySnapshot: taskXaiProviderAccountAuthoritySnapshot,
1266
+ scheduledTaskRunId: run.id,
502
1267
  },
503
- { activity: "semantic" },
1268
+ async (tx, wakeEventId) => {
1269
+ if (!wakeEventId) {
1270
+ await settleScheduledTaskRunInTransaction(tx, {
1271
+ workspaceId: task.workspaceId,
1272
+ runId: scheduledRun.id,
1273
+ sessionId: session.id,
1274
+ triggerEventId: null,
1275
+ status: "skipped",
1276
+ error: "session_cancelled",
1277
+ });
1278
+ return;
1279
+ }
1280
+ await settleScheduledTaskRunInTransaction(tx, {
1281
+ workspaceId: task.workspaceId,
1282
+ runId: scheduledRun.id,
1283
+ sessionId: session.id,
1284
+ triggerEventId: wakeEventId,
1285
+ status: "dispatched",
1286
+ });
1287
+ },
1288
+ incidentPreflightRequired
1289
+ ? {
1290
+ prepareSource: async (tx, sessionId) =>
1291
+ await prepareIncidentTelemetrySource({
1292
+ tx,
1293
+ settings,
1294
+ taskId: task.id,
1295
+ workspaceId: task.workspaceId,
1296
+ sessionId,
1297
+ alertOccurrenceLabels: structuredAlertOccurrence?.labels ?? null,
1298
+ acceptedExecution,
1299
+ }),
1300
+ }
1301
+ : undefined,
504
1302
  );
505
- await publishDurableSessionEvents(bus, task.workspaceId, session.id, goalEvents);
506
- }
507
- const bundled = await addSessionSystemUpdateWithSourceMutation(
508
- db,
509
- {
1303
+ if (scheduledUpdate.reason === "session_cancelled") {
1304
+ return {
1305
+ kind: "blocked" as const,
1306
+ result: { action: "blocked" as const, reason: "scheduled_run_terminal" as const },
1307
+ run: { ...run, status: "skipped" as const, error: "session_cancelled" },
1308
+ };
1309
+ }
1310
+ if (scheduledUpdate.added && scheduledUpdate.events.length > 0) {
1311
+ if (deferPublications) {
1312
+ deferredEvents.push({
1313
+ sessionId: session.id,
1314
+ events: scheduledUpdate.events,
1315
+ });
1316
+ } else {
1317
+ await publishDurableSessionEvents(
1318
+ bus,
1319
+ task.workspaceId,
1320
+ session.id,
1321
+ scheduledUpdate.events,
1322
+ );
1323
+ }
1324
+ }
1325
+ result = {
1326
+ action: sessionCreated ? "start" : "signal",
510
1327
  accountId: task.accountId,
511
1328
  workspaceId: task.workspaceId,
512
1329
  sessionId: session.id,
513
- kind: "scheduled_occurrence",
514
- classification: "info",
515
- sourceId: run.id,
516
- dedupeKey: `scheduled-wake:${run.id}`,
517
- summary: task.agentConfig.prompt,
518
- payload: scheduledUserMessagePayload(
519
- task.agentConfig.prompt,
520
- task.agentConfig.resources,
521
- taskTools,
522
- task.id,
523
- run.id,
524
- ),
525
- lineage: {
526
- scheduledTaskId: task.id,
1330
+ triggerEventId: scheduledUpdate.wakeEventId,
1331
+ workflowId,
1332
+ workflowWakeRevision: scheduledUpdate.workflowWakeRevision,
1333
+ };
1334
+ } else {
1335
+ if (!run) throw new Error("scheduled task run was not created");
1336
+ const scheduledRun = run;
1337
+ const session = await requireSession(dispatchDb, task.workspaceId, existingSessionId!);
1338
+ await bindScheduledTaskRunSessionInTransaction(dispatchDb, {
1339
+ accountId: task.accountId,
1340
+ workspaceId: task.workspaceId,
1341
+ runId: run.id,
1342
+ sessionId: session.id,
1343
+ });
1344
+ // A user-cancelled (terminal) reusable session must not be revived and
1345
+ // re-billed on the next fire. Early check avoids the pre-lock goal
1346
+ // upsert side-effect; the locked-callback check below is the
1347
+ // authoritative atomic guard. Mirrors apps/api/src/domain/sessions.ts.
1348
+ assertReusableSessionRevivable(session.status);
1349
+ // Defensive backstop for the API-level 409: a reusable session keeps
1350
+ // its creation-time attachment, so a diverged task attachment must
1351
+ // fail the run instead of silently running with the wrong secrets.
1352
+ assertReusableSessionBindingMatches(session, task);
1353
+ // A recurring "maintain X" task re-establishes its objective on every
1354
+ // fire: replace the goal text, reactivate it, and reset the counters.
1355
+ const goalEvents =
1356
+ task.runMode === "reusable_session" && goalSpec && run.status === "queued"
1357
+ ? await upsertScheduledSessionGoalForRun(dispatchDb, {
1358
+ accountId: task.accountId,
1359
+ workspaceId: task.workspaceId,
1360
+ sessionId: session.id,
1361
+ runId: run.id,
1362
+ text: goalSpec.text,
1363
+ successCriteria: goalSpec.successCriteria ?? null,
1364
+ maxAutoContinuations: goalSpec.maxAutoContinuations ?? null,
1365
+ ...(goalSpec.mutationPolicy ? { mutationPolicy: goalSpec.mutationPolicy } : {}),
1366
+ })
1367
+ : [];
1368
+ if (goalEvents.length > 0) {
1369
+ if (deferPublications) {
1370
+ deferredEvents.push({
1371
+ sessionId: session.id,
1372
+ events: goalEvents,
1373
+ });
1374
+ } else {
1375
+ await publishDurableSessionEvents(bus, task.workspaceId, session.id, goalEvents);
1376
+ }
1377
+ }
1378
+ const bundled = await addSessionSystemUpdateWithSourceMutation(
1379
+ dispatchDb,
1380
+ {
1381
+ accountId: task.accountId,
1382
+ workspaceId: task.workspaceId,
1383
+ sessionId: session.id,
1384
+ kind: "scheduled_occurrence",
1385
+ classification: "info",
1386
+ sourceId: run.id,
1387
+ dedupeKey: `scheduled-task-run:${run.id}`,
1388
+ summary: task.agentConfig.prompt,
1389
+ payload: scheduledUserMessagePayload(
1390
+ task.agentConfig.prompt,
1391
+ task.agentConfig.resources,
1392
+ taskTools,
1393
+ task.id,
1394
+ run.id,
1395
+ ),
1396
+ lineage: {
1397
+ scheduledTaskId: task.id,
1398
+ scheduledTaskRunId: run.id,
1399
+ ...(causalHumanSubjectId ? { causalHumanSubjectId } : {}),
1400
+ ...(taskConnectionAuthoritySubjectId
1401
+ ? { connectionAuthoritySubjectId: taskConnectionAuthoritySubjectId }
1402
+ : {}),
1403
+ ...(taskXaiProviderAccountAuthoritySnapshot.scope === "user" &&
1404
+ task.createdBy.kind === "subject"
1405
+ ? { xaiAuthoritySubjectId: task.createdBy.subjectId }
1406
+ : {}),
1407
+ },
1408
+ personalConnectionDelegations: taskPersonalConnectionDelegations,
1409
+ xaiProviderAccountAuthoritySnapshot: taskXaiProviderAccountAuthoritySnapshot,
527
1410
  scheduledTaskRunId: run.id,
528
- ...(taskXaiProviderAccountAuthoritySnapshot.scope === "user" &&
529
- task.createdBy.kind === "subject"
530
- ? { xaiAuthoritySubjectId: task.createdBy.subjectId }
531
- : {}),
532
1411
  },
533
- personalConnectionDelegations: taskPersonalConnectionDelegations,
534
- xaiProviderAccountAuthoritySnapshot: taskXaiProviderAccountAuthoritySnapshot,
535
- },
536
- async (tx, wakeEventId) => {
537
- if (task.runMode === "existing_session") {
538
- await requireScheduledTaskTargetInTransaction(tx, {
1412
+ async (tx, wakeEventId) => {
1413
+ if (!wakeEventId) {
1414
+ await settleScheduledTaskRunInTransaction(tx, {
1415
+ workspaceId: task.workspaceId,
1416
+ runId: scheduledRun.id,
1417
+ sessionId: session.id,
1418
+ triggerEventId: null,
1419
+ status: "skipped",
1420
+ error: "session_cancelled",
1421
+ });
1422
+ return;
1423
+ }
1424
+ await settleScheduledTaskRunInTransaction(tx, {
539
1425
  workspaceId: task.workspaceId,
540
- taskId: task.id,
541
- targetSessionId: session.id,
1426
+ runId: scheduledRun.id,
1427
+ sessionId: session.id,
1428
+ triggerEventId: wakeEventId,
1429
+ status: "dispatched",
542
1430
  });
1431
+ },
1432
+ incidentPreflightRequired
1433
+ ? {
1434
+ prepareSource: async (tx, sessionId) =>
1435
+ await prepareIncidentTelemetrySource({
1436
+ tx,
1437
+ settings,
1438
+ taskId: task.id,
1439
+ workspaceId: task.workspaceId,
1440
+ sessionId,
1441
+ alertOccurrenceLabels: structuredAlertOccurrence?.labels ?? null,
1442
+ acceptedExecution,
1443
+ }),
1444
+ }
1445
+ : undefined,
1446
+ );
1447
+ if (bundled.reason === "session_cancelled") {
1448
+ return {
1449
+ kind: "blocked" as const,
1450
+ result: { action: "blocked" as const, reason: "scheduled_run_terminal" as const },
1451
+ run: { ...run, status: "skipped" as const, error: "session_cancelled" },
1452
+ };
1453
+ }
1454
+ if (bundled.added && bundled.events.length > 0) {
1455
+ if (deferPublications) {
1456
+ deferredEvents.push({
1457
+ sessionId: session.id,
1458
+ events: bundled.events,
1459
+ });
1460
+ } else {
1461
+ await publishDurableSessionEvents(
1462
+ bus,
1463
+ task.workspaceId,
1464
+ session.id,
1465
+ bundled.events,
1466
+ );
543
1467
  }
544
- if (!wakeEventId) throw new Error("Scheduled delivery has no wake event");
545
- await settleScheduledTaskRunInTransaction(tx, {
546
- workspaceId: task.workspaceId,
547
- runId: run.id,
548
- sessionId: session.id,
549
- triggerEventId: wakeEventId,
550
- status: "dispatched",
551
- });
552
- },
553
- );
554
- if (bundled.reason === "session_cancelled") {
555
- throw new Error(`scheduled wake was not added: ${bundled.reason}`);
556
- }
557
- if (bundled.added && bundled.events.length > 0) {
558
- await publishDurableSessionEvents(bus, task.workspaceId, session.id, bundled.events);
1468
+ }
1469
+ result = {
1470
+ action: "signal",
1471
+ accountId: task.accountId,
1472
+ workspaceId: task.workspaceId,
1473
+ sessionId: session.id,
1474
+ triggerEventId: bundled.wakeEventId,
1475
+ workflowId: workflowIdForSession(session.id),
1476
+ workflowWakeRevision: bundled.workflowWakeRevision,
1477
+ };
559
1478
  }
560
- result = {
561
- action: "signal",
562
- accountId: task.accountId,
563
- workspaceId: task.workspaceId,
564
- sessionId: session.id,
565
- triggerEventId: bundled.wakeEventId,
566
- workflowId: workflowIdForSession(session.id),
567
- workflowWakeRevision: bundled.workflowWakeRevision,
1479
+ } catch (error) {
1480
+ const terminalError = scheduledRunRecoveryTerminalError(error);
1481
+ if (!terminalError) throw error;
1482
+ await markScheduledTaskRunFailedIfQueued(
1483
+ dispatchDb,
1484
+ task.workspaceId,
1485
+ run.id,
1486
+ terminalError.code,
1487
+ );
1488
+ return {
1489
+ kind: "blocked" as const,
1490
+ result: scheduledRunTerminalResult(terminalError.code),
1491
+ run: { ...run, status: "failed" as const, error: terminalError.code },
568
1492
  };
569
1493
  }
1494
+ await recordUsageEvent(dispatchDb, {
1495
+ accountId: task.accountId,
1496
+ workspaceId: task.workspaceId,
1497
+ eventType: "agent_run.created",
1498
+ quantity: 1,
1499
+ unit: "run",
1500
+ sourceResourceType: "scheduled_task_run",
1501
+ sourceResourceId: run.id,
1502
+ sessionId: result.sessionId,
1503
+ initiator: input.initiator ?? {
1504
+ kind: "service",
1505
+ subjectId: "scheduler",
1506
+ },
1507
+ initiatorContext: {
1508
+ scheduledTaskId: task.id,
1509
+ scheduledTaskRunId: run.id,
1510
+ },
1511
+ origin: "scheduled_task",
1512
+ idempotencyKey:
1513
+ input.agentRunUsageIdempotencyKey ?? `usage:agent_run.created:scheduled:${run.id}`,
1514
+ });
1515
+ return { kind: "complete" as const, result, run };
1516
+ };
1517
+
1518
+ let dispatchOutcome: Awaited<ReturnType<typeof executeDispatch>>;
1519
+ try {
1520
+ dispatchOutcome = incidentPreflightRequired
1521
+ ? await withSessionActivityRlsContext(
1522
+ db,
1523
+ { accountId: task.accountId, workspaceId: task.workspaceId },
1524
+ async (tx) => await executeDispatch(tx, true),
1525
+ )
1526
+ : await executeDispatch(db, false);
570
1527
  } catch (error) {
571
- await markScheduledTaskRunFailedIfQueued(
1528
+ if (error instanceof IncidentTelemetryPreflightBlockedError) {
1529
+ return { action: "blocked", reason: error.reason };
1530
+ }
1531
+ throw error;
1532
+ }
1533
+ if (dispatchOutcome.kind === "replay") {
1534
+ return await replayScheduledTaskDispatch({
572
1535
  db,
1536
+ wakeSessionWorkflow,
1537
+ task,
1538
+ run: dispatchOutcome.run,
1539
+ acceptedExecution: dispatchOutcome.acceptedExecution,
1540
+ });
1541
+ }
1542
+ if (dispatchOutcome.kind === "blocked") return dispatchOutcome.result;
1543
+ for (const deferred of deferredEvents) {
1544
+ await publishDurableSessionEvents(
1545
+ bus,
573
1546
  task.workspaceId,
574
- run.id,
575
- error instanceof Error ? error.message : String(error),
576
- ).catch(() => undefined);
577
- throw error;
1547
+ deferred.sessionId,
1548
+ deferred.events,
1549
+ );
578
1550
  }
579
- await recordUsageEvent(db, {
580
- accountId: task.accountId,
581
- workspaceId: task.workspaceId,
582
- eventType: "agent_run.created",
583
- quantity: 1,
584
- unit: "run",
585
- sourceResourceType: "scheduled_task_run",
586
- sourceResourceId: run.id,
587
- sessionId: result.sessionId,
588
- initiator: input.initiator ?? { kind: "service", subjectId: "scheduler" },
589
- initiatorContext: { scheduledTaskId: task.id, scheduledTaskRunId: run.id },
590
- origin: "scheduled_task",
591
- idempotencyKey:
592
- input.agentRunUsageIdempotencyKey ?? `usage:agent_run.created:scheduled:${run.id}`,
593
- });
1551
+ const result = dispatchOutcome.result;
594
1552
  if (wakeSessionWorkflow && result.workflowWakeRevision !== null) {
595
1553
  await wakeSessionWorkflow({
596
1554
  accountId: result.accountId,
@@ -605,6 +1563,740 @@ export function createScheduledTaskActivities(services: () => Promise<ControlAct
605
1563
  };
606
1564
  }
607
1565
 
1566
+ class IncidentTelemetryPreflightBlockedError extends Error {
1567
+ constructor(readonly reason: IncidentTelemetryPreflightBlockReason) {
1568
+ super(reason);
1569
+ this.name = "IncidentTelemetryPreflightBlockedError";
1570
+ }
1571
+ }
1572
+
1573
+ /**
1574
+ * Defensive backstop for the API-level 409: a reusable/existing target session
1575
+ * keeps its creation-time attachment, so a diverged task attachment or Slack
1576
+ * bot binding must settle the run terminally instead of silently running with
1577
+ * the wrong secrets - on the first attempt and on every recovery attempt.
1578
+ */
1579
+ function assertReusableSessionBindingMatches(
1580
+ session: { variableSetId: string | null; metadata: Record<string, unknown> },
1581
+ task: Pick<ScheduledTask, "variableSetId" | "agentConfig">,
1582
+ ): void {
1583
+ if ((session.variableSetId ?? null) !== (task.variableSetId ?? null)) {
1584
+ throw new ScheduledRunTerminalAuthorityError(
1585
+ "scheduled_reusable_binding_changed",
1586
+ "scheduled task variableSet attachment does not match its reusable session",
1587
+ );
1588
+ }
1589
+ if (
1590
+ scheduledSlackBotConnectionId(session.metadata) !==
1591
+ (task.agentConfig.slackBotConnectionId ?? null)
1592
+ ) {
1593
+ throw new ScheduledRunTerminalAuthorityError(
1594
+ "scheduled_reusable_binding_changed",
1595
+ "scheduled task OpenGeni Slack bot binding does not match its reusable session",
1596
+ );
1597
+ }
1598
+ }
1599
+
1600
+ class ScheduledRunTerminalAuthorityError extends Error {
1601
+ constructor(
1602
+ readonly code: string,
1603
+ message: string,
1604
+ ) {
1605
+ super(message);
1606
+ this.name = "ScheduledRunTerminalAuthorityError";
1607
+ }
1608
+ }
1609
+
1610
+ function scheduledRunRecoveryTerminalError(
1611
+ error: unknown,
1612
+ ): ScheduledRunTerminalAuthorityError | null {
1613
+ if (
1614
+ error instanceof Error &&
1615
+ "cause" in error &&
1616
+ error.cause !== undefined &&
1617
+ error.cause !== error
1618
+ ) {
1619
+ const nested = scheduledRunRecoveryTerminalError(error.cause);
1620
+ if (nested) return nested;
1621
+ }
1622
+ if (error instanceof ScheduledRunTerminalAuthorityError) return error;
1623
+ if (error instanceof IncidentTelemetryPreflightBlockedError) {
1624
+ return new ScheduledRunTerminalAuthorityError(
1625
+ `scheduled_incident_${error.reason}`,
1626
+ error.message,
1627
+ );
1628
+ }
1629
+ if (error instanceof SessionSpawnDeniedDbError) {
1630
+ return new ScheduledRunTerminalAuthorityError("scheduled_session_spawn_denied", error.message);
1631
+ }
1632
+ if (error instanceof Error && error.message.startsWith("Session not found:")) {
1633
+ return new ScheduledRunTerminalAuthorityError(
1634
+ "scheduled_target_session_unavailable",
1635
+ error.message,
1636
+ );
1637
+ }
1638
+ if (typeof error === "object" && error !== null && "status" in error && error.status === 422) {
1639
+ return new ScheduledRunTerminalAuthorityError(
1640
+ "scheduled_slack_authority_unavailable",
1641
+ error instanceof Error ? error.message : "scheduled Slack authority is unavailable",
1642
+ );
1643
+ }
1644
+ if (typeof error === "object" && error !== null && "code" in error && error.code === "42501") {
1645
+ return new ScheduledRunTerminalAuthorityError(
1646
+ "scheduled_run_authority_proof_rejected",
1647
+ error instanceof Error ? error.message : "scheduled run authority proof was rejected",
1648
+ );
1649
+ }
1650
+ return null;
1651
+ }
1652
+
1653
+ function scheduledRunTerminalResult(
1654
+ error: string | null,
1655
+ ): Extract<DispatchScheduledTaskRunResult, { action: "blocked" }> {
1656
+ if (error === "scheduled_authority_exhausted") {
1657
+ return { action: "blocked", reason: "scheduled_authority_exhausted" };
1658
+ }
1659
+ if (error === "scheduled_incident_incident_preflight_metadata_missing") {
1660
+ return { action: "blocked", reason: "incident_preflight_metadata_missing" };
1661
+ }
1662
+ if (error === "scheduled_incident_incident_responder_under_capable") {
1663
+ return { action: "blocked", reason: "incident_responder_under_capable" };
1664
+ }
1665
+ if (error === "scheduled_incident_incident_data_source_unsuitable") {
1666
+ return { action: "blocked", reason: "incident_data_source_unsuitable" };
1667
+ }
1668
+ return { action: "blocked", reason: "scheduled_run_terminal" };
1669
+ }
1670
+
1671
+ async function prepareIncidentTelemetrySource(input: {
1672
+ tx: Database;
1673
+ settings: ControlActivityServices["settings"];
1674
+ taskId: string;
1675
+ workspaceId: string;
1676
+ sessionId: string;
1677
+ alertOccurrenceLabels: Readonly<Record<string, string>> | null;
1678
+ acceptedExecution?: ScheduledTaskRunAcceptedExecution;
1679
+ }) {
1680
+ const session = await requireSession(input.tx, input.workspaceId, input.sessionId);
1681
+ const { task, personalConnectionDelegations } = input.acceptedExecution
1682
+ ? {
1683
+ task: input.acceptedExecution.task,
1684
+ personalConnectionDelegations: input.acceptedExecution.personalConnectionDelegations,
1685
+ }
1686
+ : await requireScheduledTaskIncidentAuthorityInTransaction(input.tx, {
1687
+ workspaceId: input.workspaceId,
1688
+ taskId: input.taskId,
1689
+ });
1690
+ const responder = await resolveIncidentTelemetryResponderMetadata({
1691
+ db: input.tx,
1692
+ settings: input.settings,
1693
+ task,
1694
+ session,
1695
+ personalConnectionDelegations,
1696
+ personalResourceAuthoritySubjectId:
1697
+ input.acceptedExecution?.personalResourceAuthoritySubjectId ?? null,
1698
+ ...(input.acceptedExecution
1699
+ ? {
1700
+ executionPolicy: input.acceptedExecution.targetSessionExecution
1701
+ ? {
1702
+ tools: input.acceptedExecution.targetSessionExecution.tools,
1703
+ firstPartyMcpTools:
1704
+ input.acceptedExecution.targetSessionExecution.firstPartyMcpTools,
1705
+ firstPartyMcpPermissions:
1706
+ input.acceptedExecution.targetSessionExecution.firstPartyMcpPermissions,
1707
+ variableSetId: input.acceptedExecution.targetSessionExecution.variableSetId,
1708
+ rigId: input.acceptedExecution.targetSessionExecution.rigId,
1709
+ rigVersionId: input.acceptedExecution.targetSessionExecution.rigVersionId,
1710
+ toolPolicy: input.acceptedExecution.targetSessionExecution.toolPolicy,
1711
+ mcpServerIds: input.acceptedExecution.targetSessionExecution.mcpServerIds,
1712
+ toolPolicyVersion: input.acceptedExecution.targetSessionExecution.toolPolicyVersion,
1713
+ }
1714
+ : {
1715
+ tools: input.acceptedExecution.resolvedTools,
1716
+ firstPartyMcpTools: input.acceptedExecution.resolvedFirstPartyMcpTools,
1717
+ firstPartyMcpPermissions: input.acceptedExecution.resolvedFirstPartyMcpPermissions,
1718
+ variableSetId: input.acceptedExecution.resolvedVariableSet?.id ?? null,
1719
+ rigId: input.acceptedExecution.resolvedRig?.id ?? null,
1720
+ rigVersionId: input.acceptedExecution.resolvedRig?.versionId ?? null,
1721
+ toolPolicy: session.toolPolicy,
1722
+ mcpServerIds: [],
1723
+ toolPolicyVersion: session.toolPolicyVersion,
1724
+ },
1725
+ }
1726
+ : {}),
1727
+ });
1728
+ const preflight = evaluateIncidentTelemetryPreflight({
1729
+ agentConfig: task.agentConfig,
1730
+ incidentTriggered: input.alertOccurrenceLabels !== null,
1731
+ alertOccurrenceLabels: input.alertOccurrenceLabels,
1732
+ responder,
1733
+ });
1734
+ if (preflight.action === "blocked") {
1735
+ throw new IncidentTelemetryPreflightBlockedError(preflight.reason);
1736
+ }
1737
+ const fence = incidentTelemetryAuthorityFence({
1738
+ task,
1739
+ responder,
1740
+ alertOccurrenceLabels: input.alertOccurrenceLabels,
1741
+ });
1742
+ if (!fence) {
1743
+ throw new IncidentTelemetryPreflightBlockedError("incident_preflight_metadata_missing");
1744
+ }
1745
+ return {
1746
+ lineage: {
1747
+ [INCIDENT_TELEMETRY_AUTHORITY_FENCE_LINEAGE_KEY]: fence,
1748
+ },
1749
+ };
1750
+ }
1751
+
1752
+ async function recordScheduledTaskFiredUsage(
1753
+ db: Database,
1754
+ acceptedExecution: ScheduledTaskRunAcceptedExecution,
1755
+ run: ScheduledTaskRun,
1756
+ ): Promise<void> {
1757
+ const task = acceptedExecution.task;
1758
+ await recordUsageEvent(db, {
1759
+ accountId: task.accountId,
1760
+ workspaceId: task.workspaceId,
1761
+ eventType: "scheduled_task.fired",
1762
+ quantity: 1,
1763
+ unit: "run",
1764
+ sourceResourceType: "scheduled_task_run",
1765
+ sourceResourceId: run.id,
1766
+ initiator: acceptedExecution.triggerInitiator,
1767
+ initiatorContext: { scheduledTaskId: task.id, scheduledTaskRunId: run.id },
1768
+ origin: "scheduled_task",
1769
+ idempotencyKey: `usage:scheduled_task.fired:${run.id}`,
1770
+ });
1771
+ }
1772
+
1773
+ async function recoverBoundScheduledTaskDispatch(input: {
1774
+ db: Database;
1775
+ bus: ControlActivityServices["bus"];
1776
+ settings: ControlActivityServices["settings"];
1777
+ wakeSessionWorkflow: WakeSessionWorkflowSignal | null;
1778
+ run: ScheduledTaskRun;
1779
+ acceptedExecution: ScheduledTaskRunAcceptedExecution;
1780
+ input: DispatchScheduledTaskRunInput;
1781
+ }): Promise<DispatchScheduledTaskRunResult> {
1782
+ const { task } = input.acceptedExecution;
1783
+ if (input.run.status !== "queued") {
1784
+ throw new Error("scheduled run is not an exact queued recovery");
1785
+ }
1786
+ const generatedSession =
1787
+ task.runMode === "new_session_per_run" ||
1788
+ (task.runMode === "reusable_session" && task.reusableSessionId === null);
1789
+ const frozenSlack = input.acceptedExecution.resolvedSlackBotConnection;
1790
+ if (frozenSlack) {
1791
+ const currentSlack = await requireOpenGeniSlackBotConnection(
1792
+ input.db,
1793
+ task.workspaceId,
1794
+ frozenSlack.id,
1795
+ );
1796
+ const currentMetadata = openGeniSlackBotMetadata(currentSlack.metadata);
1797
+ if (
1798
+ currentSlack.version !== frozenSlack.version ||
1799
+ currentSlack.verifiedInstallVersion !== frozenSlack.verifiedInstallVersion ||
1800
+ stableJson(currentMetadata) !== stableJson(frozenSlack.metadata)
1801
+ ) {
1802
+ throw new ScheduledRunTerminalAuthorityError(
1803
+ "scheduled_slack_authority_changed",
1804
+ "scheduled Slack bot authority changed after run admission",
1805
+ );
1806
+ }
1807
+ }
1808
+ let session: Awaited<ReturnType<typeof createSession>>;
1809
+ if (input.run.sessionId) {
1810
+ session = await requireSession(input.db, task.workspaceId, input.run.sessionId);
1811
+ // A bound existing/reusable target must still carry the exact Variable Set
1812
+ // and Slack bot binding frozen with this occurrence; recovery must fail the
1813
+ // run terminally rather than deliver into a diverged session.
1814
+ if (!generatedSession) assertReusableSessionBindingMatches(session, task);
1815
+ } else if (generatedSession) {
1816
+ const variableSet = input.acceptedExecution.resolvedVariableSet;
1817
+ if (variableSet) {
1818
+ const current = await getVariableSet(
1819
+ input.db,
1820
+ {
1821
+ accountId: task.accountId,
1822
+ workspaceId: task.workspaceId,
1823
+ subjectId:
1824
+ input.acceptedExecution.personalResourceAuthoritySubjectId ?? task.createdBy.subjectId,
1825
+ },
1826
+ variableSet.id,
1827
+ );
1828
+ if (!current || current.generation !== variableSet.generation) {
1829
+ throw new ScheduledRunTerminalAuthorityError(
1830
+ "scheduled_variable_set_authority_changed",
1831
+ "scheduled variable set changed after run admission",
1832
+ );
1833
+ }
1834
+ }
1835
+ const rig = input.acceptedExecution.resolvedRig;
1836
+ if (rig) {
1837
+ const retained = await getScheduledScopedRigVersionMetadata(
1838
+ input.db,
1839
+ {
1840
+ accountId: task.accountId,
1841
+ workspaceId: task.workspaceId,
1842
+ subjectId:
1843
+ input.acceptedExecution.personalResourceAuthoritySubjectId ?? task.createdBy.subjectId,
1844
+ },
1845
+ rig.id,
1846
+ rig.versionId,
1847
+ );
1848
+ if (!retained) {
1849
+ throw new ScheduledRunTerminalAuthorityError(
1850
+ "scheduled_rig_authority_changed",
1851
+ "scheduled rig changed after run admission",
1852
+ );
1853
+ }
1854
+ }
1855
+ const taskMetadata = { ...task.agentConfig.metadata };
1856
+ delete taskMetadata[OPENGENI_SLACK_BOT_SESSION_METADATA_KEY];
1857
+ const created = await createSessionWithIdempotencyKeyResult(input.db, {
1858
+ accountId: task.accountId,
1859
+ workspaceId: task.workspaceId,
1860
+ initialMessage: task.agentConfig.prompt,
1861
+ resources: task.agentConfig.resources,
1862
+ tools: input.acceptedExecution.resolvedTools,
1863
+ firstPartyMcpTools: input.acceptedExecution.resolvedFirstPartyMcpTools,
1864
+ firstPartyMcpPermissions: input.acceptedExecution.resolvedFirstPartyMcpPermissions,
1865
+ metadata: {
1866
+ ...taskMetadata,
1867
+ model: input.acceptedExecution.resolvedModel,
1868
+ reasoningEffort: input.acceptedExecution.resolvedReasoningEffort,
1869
+ scheduledTaskId: task.id,
1870
+ scheduledTaskRunMode: task.runMode,
1871
+ ...(task.agentConfig.goal ? { scheduledTaskGoal: task.agentConfig.goal } : {}),
1872
+ scheduledTaskRunId: input.run.id,
1873
+ ...(frozenSlack ? { [OPENGENI_SLACK_BOT_SESSION_METADATA_KEY]: frozenSlack.id } : {}),
1874
+ },
1875
+ createdBy: {
1876
+ kind: "service",
1877
+ subjectId: "scheduler",
1878
+ label: "OpenGeni scheduler",
1879
+ },
1880
+ createdByContext: {
1881
+ scheduledTaskId: task.id,
1882
+ scheduledTaskRunId: input.run.id,
1883
+ },
1884
+ model: input.acceptedExecution.resolvedModel,
1885
+ reasoningEffort: input.acceptedExecution.resolvedReasoningEffort,
1886
+ latencyMode: input.acceptedExecution.resolvedLatencyMode,
1887
+ sandboxBackend: input.acceptedExecution.resolvedSandboxBackend,
1888
+ sandboxOs: input.acceptedExecution.resolvedSandboxOs,
1889
+ variableSetId: variableSet?.id ?? null,
1890
+ rigId: rig?.id ?? null,
1891
+ rigVersionId: rig?.versionId ?? null,
1892
+ personalConnectionDelegations: [],
1893
+ initialXaiProviderAccountAuthoritySnapshot:
1894
+ input.acceptedExecution.xaiProviderAccountAuthoritySnapshot,
1895
+ maxNestedAgentDepthOverride: task.agentConfig.maxNestedAgentDepth ?? null,
1896
+ frozenNestedAgentDepthPolicy: {
1897
+ effectiveMaxNestedAgentDepth:
1898
+ input.acceptedExecution.generatedSessionBinding!.effectiveMaxNestedAgentDepth,
1899
+ nestedAgentDepthPolicySource:
1900
+ input.acceptedExecution.generatedSessionBinding!.nestedAgentDepthPolicySource,
1901
+ },
1902
+ frozenCodexCompactionMode:
1903
+ input.acceptedExecution.generatedSessionBinding!.codexCompactionMode,
1904
+ allowNestedAgentDepthIncrease: true,
1905
+ subjectId: `scheduled_task:${task.id}`,
1906
+ createIdempotencyKey:
1907
+ input.acceptedExecution.generatedSessionBinding?.createIdempotencyKey ??
1908
+ (() => {
1909
+ throw new ScheduledRunTerminalAuthorityError(
1910
+ "scheduled_generated_session_binding_missing",
1911
+ "scheduled generated session binding is missing",
1912
+ );
1913
+ })(),
1914
+ beforeCreateCommit: async (tx, sessionId) => {
1915
+ await bindScheduledTaskRunSessionInTransaction(tx, {
1916
+ accountId: task.accountId,
1917
+ workspaceId: task.workspaceId,
1918
+ runId: input.run.id,
1919
+ sessionId,
1920
+ });
1921
+ },
1922
+ });
1923
+ if (created.denied) throw new SessionSpawnDeniedDbError(created.denial);
1924
+ session = created.session;
1925
+ if (!created.created) {
1926
+ await bindScheduledTaskRunSessionInTransaction(input.db, {
1927
+ accountId: task.accountId,
1928
+ workspaceId: task.workspaceId,
1929
+ runId: input.run.id,
1930
+ sessionId: session.id,
1931
+ });
1932
+ }
1933
+ } else {
1934
+ const targetSessionId =
1935
+ task.runMode === "existing_session" ? task.targetSessionId : task.reusableSessionId;
1936
+ if (!targetSessionId) {
1937
+ throw new ScheduledRunTerminalAuthorityError(
1938
+ "scheduled_target_session_unavailable",
1939
+ "scheduled accepted target session is unavailable",
1940
+ );
1941
+ }
1942
+ session = await requireSession(input.db, task.workspaceId, targetSessionId);
1943
+ // Same backstop for a first attempt that died before its bind landed.
1944
+ assertReusableSessionBindingMatches(session, task);
1945
+ await bindScheduledTaskRunSessionInTransaction(input.db, {
1946
+ accountId: task.accountId,
1947
+ workspaceId: task.workspaceId,
1948
+ runId: input.run.id,
1949
+ sessionId: session.id,
1950
+ });
1951
+ }
1952
+ if (session.status === "cancelled") {
1953
+ await markScheduledTaskRunSkippedIfQueued(input.db, {
1954
+ workspaceId: task.workspaceId,
1955
+ runId: input.run.id,
1956
+ sessionId: session.id,
1957
+ error: "session_cancelled",
1958
+ });
1959
+ return { action: "blocked", reason: "scheduled_run_terminal" };
1960
+ }
1961
+ if (generatedSession) {
1962
+ const expectedCreateKey = input.acceptedExecution.generatedSessionBinding?.createIdempotencyKey;
1963
+ if (!expectedCreateKey) {
1964
+ throw new ScheduledRunTerminalAuthorityError(
1965
+ "scheduled_generated_session_binding_missing",
1966
+ "scheduled generated session binding is missing",
1967
+ );
1968
+ }
1969
+ const generatedBinding = input.acceptedExecution.generatedSessionBinding!;
1970
+ const canonicalGeneratedRunId =
1971
+ task.runMode === "reusable_session" && typeof session.metadata.scheduledTaskRunId === "string"
1972
+ ? session.metadata.scheduledTaskRunId
1973
+ : input.run.id;
1974
+ const expectedTaskMetadata = { ...task.agentConfig.metadata };
1975
+ delete expectedTaskMetadata[OPENGENI_SLACK_BOT_SESSION_METADATA_KEY];
1976
+ const expectedMetadata = {
1977
+ ...expectedTaskMetadata,
1978
+ model: input.acceptedExecution.resolvedModel,
1979
+ reasoningEffort: input.acceptedExecution.resolvedReasoningEffort,
1980
+ scheduledTaskId: task.id,
1981
+ scheduledTaskRunMode: task.runMode,
1982
+ ...(task.agentConfig.goal ? { scheduledTaskGoal: task.agentConfig.goal } : {}),
1983
+ scheduledTaskRunId: canonicalGeneratedRunId,
1984
+ ...(frozenSlack ? { [OPENGENI_SLACK_BOT_SESSION_METADATA_KEY]: frozenSlack.id } : {}),
1985
+ };
1986
+ if (
1987
+ session.createIdempotencyKey !== expectedCreateKey ||
1988
+ session.initialMessage !== task.agentConfig.prompt ||
1989
+ session.instructions !== null ||
1990
+ session.policyRole !== null ||
1991
+ stableJson(session.skills) !== "[]" ||
1992
+ stableJson(session.toolPolicy) !==
1993
+ stableJson({ mode: "explicit", inheritedFromSessionId: null }) ||
1994
+ session.toolPolicyVersion !== 1 ||
1995
+ session.mcpServers.length !== 0 ||
1996
+ session.parentSessionId !== null ||
1997
+ session.rootSessionId !== session.id ||
1998
+ session.nestedAgentDepth !== 0 ||
1999
+ session.effectiveMaxNestedAgentDepth !== generatedBinding.effectiveMaxNestedAgentDepth ||
2000
+ session.nestedAgentDepthPolicySource !== generatedBinding.nestedAgentDepthPolicySource ||
2001
+ session.nestedAgentDepthPolicySessionId !==
2002
+ (generatedBinding.nestedAgentDepthPolicySource === "session" ? session.id : null) ||
2003
+ session.sandboxGroupId !== session.id ||
2004
+ session.channelId !== null ||
2005
+ session.codexCompactionMode !== generatedBinding.codexCompactionMode ||
2006
+ stableJson(session.metadata) !== stableJson(expectedMetadata) ||
2007
+ session.createdBy.kind !== "service" ||
2008
+ session.createdBy.subjectId !== "scheduler" ||
2009
+ stableJson(session.createdByContext) !==
2010
+ stableJson({
2011
+ label: "OpenGeni scheduler",
2012
+ scheduledTaskId: task.id,
2013
+ scheduledTaskRunId: canonicalGeneratedRunId,
2014
+ }) ||
2015
+ (task.runMode === "new_session_per_run" &&
2016
+ input.acceptedExecution.alertOccurrenceLabels === null &&
2017
+ (session.metadata.scheduledTaskRunId !== input.run.id ||
2018
+ session.createdByContext.scheduledTaskRunId !== input.run.id)) ||
2019
+ session.model !== input.acceptedExecution.resolvedModel ||
2020
+ session.reasoningEffort !== input.acceptedExecution.resolvedReasoningEffort ||
2021
+ session.latencyMode !== input.acceptedExecution.resolvedLatencyMode ||
2022
+ session.sandboxBackend !== input.acceptedExecution.resolvedSandboxBackend ||
2023
+ session.sandboxOs !== input.acceptedExecution.resolvedSandboxOs ||
2024
+ stableJson(session.resources) !== stableJson(task.agentConfig.resources) ||
2025
+ stableJson(session.tools) !== stableJson(input.acceptedExecution.resolvedTools) ||
2026
+ stableJson(session.firstPartyMcpTools) !==
2027
+ stableJson(input.acceptedExecution.resolvedFirstPartyMcpTools) ||
2028
+ stableJson(session.firstPartyMcpPermissions) !==
2029
+ stableJson(input.acceptedExecution.resolvedFirstPartyMcpPermissions) ||
2030
+ session.maxNestedAgentDepthOverride !== (task.agentConfig.maxNestedAgentDepth ?? null) ||
2031
+ (session.variableSetId ?? null) !==
2032
+ (input.acceptedExecution.resolvedVariableSet?.id ?? null) ||
2033
+ (session.rigId ?? null) !== (input.acceptedExecution.resolvedRig?.id ?? null) ||
2034
+ (session.rigVersionId ?? null) !== (input.acceptedExecution.resolvedRig?.versionId ?? null) ||
2035
+ scheduledSlackBotConnectionId(session.metadata) !== (frozenSlack?.id ?? null)
2036
+ ) {
2037
+ throw new ScheduledRunTerminalAuthorityError(
2038
+ "scheduled_generated_session_changed",
2039
+ "scheduled generated session differs from accepted execution",
2040
+ );
2041
+ }
2042
+ }
2043
+ let workflowId = workflowIdForSession(session.id);
2044
+ if (generatedSession) {
2045
+ const goalSpec = task.agentConfig.goal ?? null;
2046
+ const started = await initializeSessionStartAtomically(input.db, {
2047
+ accountId: task.accountId,
2048
+ workspaceId: task.workspaceId,
2049
+ sessionId: session.id,
2050
+ reasoningEffortFallback: input.acceptedExecution.resolvedReasoningEffort,
2051
+ createdEventPayload: {
2052
+ scheduledTaskId: task.id,
2053
+ scheduledTaskRunId: input.run.id,
2054
+ },
2055
+ goal: goalSpec
2056
+ ? {
2057
+ text: goalSpec.text,
2058
+ successCriteria: goalSpec.successCriteria ?? null,
2059
+ maxAutoContinuations: goalSpec.maxAutoContinuations ?? null,
2060
+ ...(goalSpec.mutationPolicy ? { mutationPolicy: goalSpec.mutationPolicy } : {}),
2061
+ createdBy: "scheduled_task",
2062
+ }
2063
+ : null,
2064
+ deferInitialTurn: true,
2065
+ deferredStatus: "queued",
2066
+ });
2067
+ workflowId = started.temporalWorkflowId;
2068
+ if (started.events.length > 0) {
2069
+ await publishDurableSessionEvents(input.bus, task.workspaceId, session.id, started.events);
2070
+ }
2071
+ if (task.runMode === "reusable_session") {
2072
+ await materializeScheduledTaskReusableSessionFromRun(input.db, {
2073
+ accountId: task.accountId,
2074
+ workspaceId: task.workspaceId,
2075
+ taskId: task.id,
2076
+ runId: input.run.id,
2077
+ sessionId: session.id,
2078
+ sourceTaskAuthorityRevision: input.run.taskAuthorityRevision!,
2079
+ sourceExecutionDigest: input.run.taskExecutionDigest!,
2080
+ });
2081
+ }
2082
+ // Same title the normal dispatch writes, from the same durable task row, in
2083
+ // the same position relative to session.created. A session the dead dispatch
2084
+ // already titled is left alone.
2085
+ const titleEvents = await stampScheduledSessionTitle(input.db, {
2086
+ workspaceId: task.workspaceId,
2087
+ session,
2088
+ taskName: task.name,
2089
+ });
2090
+ if (titleEvents.length > 0) {
2091
+ await publishDurableSessionEvents(input.bus, task.workspaceId, session.id, titleEvents);
2092
+ }
2093
+ }
2094
+ if (!generatedSession && task.runMode === "reusable_session" && task.agentConfig.goal) {
2095
+ const goalEvents = await upsertScheduledSessionGoalForRun(input.db, {
2096
+ accountId: task.accountId,
2097
+ workspaceId: task.workspaceId,
2098
+ sessionId: session.id,
2099
+ runId: input.run.id,
2100
+ text: task.agentConfig.goal.text,
2101
+ successCriteria: task.agentConfig.goal.successCriteria ?? null,
2102
+ maxAutoContinuations: task.agentConfig.goal.maxAutoContinuations ?? null,
2103
+ ...(task.agentConfig.goal.mutationPolicy
2104
+ ? { mutationPolicy: task.agentConfig.goal.mutationPolicy }
2105
+ : {}),
2106
+ });
2107
+ if (goalEvents.length > 0) {
2108
+ await publishDurableSessionEvents(input.bus, task.workspaceId, session.id, goalEvents);
2109
+ }
2110
+ }
2111
+ const scheduledUpdate = await addSessionSystemUpdateWithSourceMutation(
2112
+ input.db,
2113
+ {
2114
+ accountId: task.accountId,
2115
+ workspaceId: task.workspaceId,
2116
+ sessionId: session.id,
2117
+ kind: "scheduled_occurrence",
2118
+ classification: "info",
2119
+ sourceId: input.run.id,
2120
+ dedupeKey: `scheduled-task-run:${input.run.id}`,
2121
+ summary: task.agentConfig.prompt,
2122
+ payload: scheduledUserMessagePayload(
2123
+ task.agentConfig.prompt,
2124
+ task.agentConfig.resources,
2125
+ input.acceptedExecution.resolvedTools,
2126
+ task.id,
2127
+ input.run.id,
2128
+ ),
2129
+ lineage: {
2130
+ scheduledTaskId: task.id,
2131
+ scheduledTaskRunId: input.run.id,
2132
+ ...(input.acceptedExecution.causalHumanSubjectId
2133
+ ? { causalHumanSubjectId: input.acceptedExecution.causalHumanSubjectId }
2134
+ : {}),
2135
+ ...(input.acceptedExecution.connectionAuthoritySubjectId
2136
+ ? {
2137
+ connectionAuthoritySubjectId: input.acceptedExecution.connectionAuthoritySubjectId,
2138
+ }
2139
+ : {}),
2140
+ ...(input.acceptedExecution.xaiAuthoritySubjectId
2141
+ ? { xaiAuthoritySubjectId: input.acceptedExecution.xaiAuthoritySubjectId }
2142
+ : {}),
2143
+ },
2144
+ personalConnectionDelegations: input.acceptedExecution.personalConnectionDelegations,
2145
+ xaiProviderAccountAuthoritySnapshot:
2146
+ input.acceptedExecution.xaiProviderAccountAuthoritySnapshot,
2147
+ scheduledTaskRunId: input.run.id,
2148
+ },
2149
+ async (tx, wakeEventId) => {
2150
+ if (!wakeEventId) {
2151
+ await settleScheduledTaskRunInTransaction(tx, {
2152
+ workspaceId: task.workspaceId,
2153
+ runId: input.run.id,
2154
+ sessionId: session.id,
2155
+ triggerEventId: null,
2156
+ status: "skipped",
2157
+ error: "session_cancelled",
2158
+ });
2159
+ return;
2160
+ }
2161
+ await settleScheduledTaskRunInTransaction(tx, {
2162
+ workspaceId: task.workspaceId,
2163
+ runId: input.run.id,
2164
+ sessionId: session.id,
2165
+ triggerEventId: wakeEventId,
2166
+ status: "dispatched",
2167
+ });
2168
+ },
2169
+ input.acceptedExecution.incidentPreflightRequired
2170
+ ? {
2171
+ prepareSource: async (tx, sessionId) =>
2172
+ await prepareIncidentTelemetrySource({
2173
+ tx,
2174
+ settings: input.settings,
2175
+ taskId: task.id,
2176
+ workspaceId: task.workspaceId,
2177
+ sessionId,
2178
+ alertOccurrenceLabels: input.acceptedExecution.alertOccurrenceLabels,
2179
+ acceptedExecution: input.acceptedExecution,
2180
+ }),
2181
+ }
2182
+ : undefined,
2183
+ );
2184
+ if (scheduledUpdate.reason === "session_cancelled") {
2185
+ return { action: "blocked", reason: "scheduled_run_terminal" };
2186
+ }
2187
+ if (scheduledUpdate.added && scheduledUpdate.events.length > 0) {
2188
+ await publishDurableSessionEvents(
2189
+ input.bus,
2190
+ task.workspaceId,
2191
+ session.id,
2192
+ scheduledUpdate.events,
2193
+ );
2194
+ }
2195
+ await recordUsageEvent(input.db, {
2196
+ accountId: task.accountId,
2197
+ workspaceId: task.workspaceId,
2198
+ eventType: "agent_run.created",
2199
+ quantity: 1,
2200
+ unit: "run",
2201
+ sourceResourceType: "scheduled_task_run",
2202
+ sourceResourceId: input.run.id,
2203
+ sessionId: session.id,
2204
+ initiator: input.acceptedExecution.triggerInitiator,
2205
+ initiatorContext: { scheduledTaskId: task.id, scheduledTaskRunId: input.run.id },
2206
+ origin: "scheduled_task",
2207
+ idempotencyKey:
2208
+ input.acceptedExecution.agentRunUsageIdempotencyKey ??
2209
+ `usage:agent_run.created:scheduled:${input.run.id}`,
2210
+ });
2211
+ const workflowWakeRevision = await enqueueSessionWorkflowWakeIfRunnable(input.db, {
2212
+ accountId: task.accountId,
2213
+ workspaceId: task.workspaceId,
2214
+ sessionId: session.id,
2215
+ temporalWorkflowId: workflowId,
2216
+ reason: "scheduled_retry",
2217
+ });
2218
+ const result = {
2219
+ action: generatedSession ? "start" : "signal",
2220
+ accountId: task.accountId,
2221
+ workspaceId: task.workspaceId,
2222
+ sessionId: session.id,
2223
+ triggerEventId: scheduledUpdate.wakeEventId,
2224
+ workflowId,
2225
+ workflowWakeRevision,
2226
+ } as const;
2227
+ if (input.wakeSessionWorkflow && workflowWakeRevision !== null) {
2228
+ await input.wakeSessionWorkflow({
2229
+ accountId: result.accountId,
2230
+ workspaceId: result.workspaceId,
2231
+ sessionId: result.sessionId,
2232
+ workflowId: result.workflowId,
2233
+ wakeRevision: workflowWakeRevision,
2234
+ });
2235
+ }
2236
+ return result;
2237
+ }
2238
+
2239
+ async function replayScheduledTaskDispatch(input: {
2240
+ db: Database;
2241
+ wakeSessionWorkflow: WakeSessionWorkflowSignal | null;
2242
+ task: ScheduledTask;
2243
+ run: ScheduledTaskRun;
2244
+ acceptedExecution: ScheduledTaskRunAcceptedExecution;
2245
+ }): Promise<DispatchScheduledTaskRunResult> {
2246
+ if (!input.run.sessionId || !input.run.triggerEventId) {
2247
+ throw new Error("dispatched scheduled task run is missing its delivery identity");
2248
+ }
2249
+ const sessionId = input.run.sessionId;
2250
+ const triggerEventId = input.run.triggerEventId;
2251
+ await requireSession(input.db, input.task.workspaceId, sessionId);
2252
+ await recordUsageEvent(input.db, {
2253
+ accountId: input.task.accountId,
2254
+ workspaceId: input.task.workspaceId,
2255
+ eventType: "agent_run.created",
2256
+ quantity: 1,
2257
+ unit: "run",
2258
+ sourceResourceType: "scheduled_task_run",
2259
+ sourceResourceId: input.run.id,
2260
+ sessionId,
2261
+ initiator: input.acceptedExecution.triggerInitiator,
2262
+ initiatorContext: {
2263
+ scheduledTaskId: input.task.id,
2264
+ scheduledTaskRunId: input.run.id,
2265
+ },
2266
+ origin: "scheduled_task",
2267
+ idempotencyKey:
2268
+ input.acceptedExecution.agentRunUsageIdempotencyKey ??
2269
+ `usage:agent_run.created:scheduled:${input.run.id}`,
2270
+ });
2271
+ const workflowId = workflowIdForSession(sessionId);
2272
+ const workflowWakeRevision = await enqueueSessionWorkflowWakeIfRunnable(input.db, {
2273
+ accountId: input.task.accountId,
2274
+ workspaceId: input.task.workspaceId,
2275
+ sessionId,
2276
+ temporalWorkflowId: workflowId,
2277
+ reason: "scheduled_retry",
2278
+ });
2279
+ const result = {
2280
+ action: input.acceptedExecution.task.runMode === "new_session_per_run" ? "start" : "signal",
2281
+ accountId: input.task.accountId,
2282
+ workspaceId: input.task.workspaceId,
2283
+ sessionId,
2284
+ triggerEventId,
2285
+ workflowId,
2286
+ workflowWakeRevision,
2287
+ } as const;
2288
+ if (input.wakeSessionWorkflow && workflowWakeRevision !== null) {
2289
+ await input.wakeSessionWorkflow({
2290
+ accountId: result.accountId,
2291
+ workspaceId: result.workspaceId,
2292
+ sessionId: result.sessionId,
2293
+ workflowId: result.workflowId,
2294
+ wakeRevision: workflowWakeRevision,
2295
+ });
2296
+ }
2297
+ return result;
2298
+ }
2299
+
608
2300
  function knowledgeSourceSyncEffectivelyPaused(task: {
609
2301
  status: "active" | "paused";
610
2302
  metadata: Record<string, unknown>;