@sixb/server 0.1.0 → 0.1.2

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 (171) hide show
  1. package/README.md +3 -3
  2. package/dist/auth/application-access.d.ts +2 -2
  3. package/dist/auth/application-access.d.ts.map +1 -1
  4. package/dist/auth/connector-callback-cookie.d.ts +12 -0
  5. package/dist/auth/connector-callback-cookie.d.ts.map +1 -0
  6. package/dist/auth/guard.d.ts +3 -3
  7. package/dist/auth/guard.d.ts.map +1 -1
  8. package/dist/auth/public-routes.d.ts.map +1 -1
  9. package/dist/auth/scope.d.ts +21 -27
  10. package/dist/auth/scope.d.ts.map +1 -1
  11. package/dist/auth/session-cookies.d.ts +2 -2
  12. package/dist/auth/session-cookies.d.ts.map +1 -1
  13. package/dist/connectors/http.d.ts +16 -0
  14. package/dist/connectors/http.d.ts.map +1 -0
  15. package/dist/files/content.d.ts +11 -0
  16. package/dist/files/content.d.ts.map +1 -1
  17. package/dist/index.js +4164 -3541
  18. package/dist/index.js.map +57 -52
  19. package/dist/openapi/tags.d.ts +11 -3
  20. package/dist/openapi/tags.d.ts.map +1 -1
  21. package/dist/registerRoutes.d.ts +6 -2
  22. package/dist/registerRoutes.d.ts.map +1 -1
  23. package/dist/routes/action-runs.d.ts +21 -31
  24. package/dist/routes/action-runs.d.ts.map +1 -1
  25. package/dist/routes/actions.d.ts +9 -8
  26. package/dist/routes/actions.d.ts.map +1 -1
  27. package/dist/routes/agent-api-gateway.d.ts +2 -2
  28. package/dist/routes/agent-api-gateway.d.ts.map +1 -1
  29. package/dist/routes/agents.d.ts +130 -131
  30. package/dist/routes/agents.d.ts.map +1 -1
  31. package/dist/routes/auth.d.ts +90 -90
  32. package/dist/routes/auth.d.ts.map +1 -1
  33. package/dist/routes/connector-connection-runs.d.ts +794 -0
  34. package/dist/routes/connector-connection-runs.d.ts.map +1 -0
  35. package/dist/routes/connector-connections.d.ts +219 -0
  36. package/dist/routes/connector-connections.d.ts.map +1 -0
  37. package/dist/routes/connectors.d.ts +10 -4
  38. package/dist/routes/connectors.d.ts.map +1 -1
  39. package/dist/routes/datasets.d.ts +15 -9
  40. package/dist/routes/datasets.d.ts.map +1 -1
  41. package/dist/routes/events.d.ts +8 -8
  42. package/dist/routes/events.d.ts.map +1 -1
  43. package/dist/routes/files.d.ts +11 -10
  44. package/dist/routes/files.d.ts.map +1 -1
  45. package/dist/routes/links.d.ts +2 -2
  46. package/dist/routes/links.d.ts.map +1 -1
  47. package/dist/routes/logs.d.ts +5 -5
  48. package/dist/routes/logs.d.ts.map +1 -1
  49. package/dist/routes/objects.d.ts +34 -34
  50. package/dist/routes/objects.d.ts.map +1 -1
  51. package/dist/routes/ontology.d.ts +62 -62
  52. package/dist/routes/ontology.d.ts.map +1 -1
  53. package/dist/routes/pipelines.d.ts +28 -34
  54. package/dist/routes/pipelines.d.ts.map +1 -1
  55. package/dist/routes/project.d.ts +2 -2
  56. package/dist/routes/project.d.ts.map +1 -1
  57. package/dist/routes/projections.d.ts +131 -83
  58. package/dist/routes/projections.d.ts.map +1 -1
  59. package/dist/routes/rules.d.ts +6 -6
  60. package/dist/routes/rules.d.ts.map +1 -1
  61. package/dist/routes/status.d.ts +2 -2
  62. package/dist/routes/status.d.ts.map +1 -1
  63. package/dist/routes/syncs.d.ts +27 -30
  64. package/dist/routes/syncs.d.ts.map +1 -1
  65. package/dist/routes/telemetry.d.ts +2 -2
  66. package/dist/routes/telemetry.d.ts.map +1 -1
  67. package/dist/routes/webhook-runs.d.ts +9 -9
  68. package/dist/routes/webhook-runs.d.ts.map +1 -1
  69. package/dist/routes/webhooks.d.ts +2 -2
  70. package/dist/routes/webhooks.d.ts.map +1 -1
  71. package/dist/routes/workflows.d.ts +132 -107
  72. package/dist/routes/workflows.d.ts.map +1 -1
  73. package/dist/routes/ws/agents.d.ts +11 -5
  74. package/dist/routes/ws/agents.d.ts.map +1 -1
  75. package/dist/routes/ws/events.d.ts +8 -8
  76. package/dist/routes/ws/events.d.ts.map +1 -1
  77. package/dist/routes/ws/log-subscription-hub.d.ts +6 -3
  78. package/dist/routes/ws/log-subscription-hub.d.ts.map +1 -1
  79. package/dist/routes/ws/logs.d.ts +10 -10
  80. package/dist/routes/ws/logs.d.ts.map +1 -1
  81. package/dist/schemas/actions.d.ts +97 -202
  82. package/dist/schemas/actions.d.ts.map +1 -1
  83. package/dist/schemas/agents.d.ts +443 -440
  84. package/dist/schemas/agents.d.ts.map +1 -1
  85. package/dist/schemas/ai-usage.d.ts +34 -0
  86. package/dist/schemas/ai-usage.d.ts.map +1 -0
  87. package/dist/schemas/auth.d.ts +168 -168
  88. package/dist/schemas/common.d.ts +37 -0
  89. package/dist/schemas/common.d.ts.map +1 -1
  90. package/dist/schemas/connectors.d.ts +713 -2
  91. package/dist/schemas/connectors.d.ts.map +1 -1
  92. package/dist/schemas/datasets.d.ts +29 -23
  93. package/dist/schemas/datasets.d.ts.map +1 -1
  94. package/dist/schemas/event-payloads.d.ts +6 -6
  95. package/dist/schemas/events.d.ts +34 -34
  96. package/dist/schemas/logs.d.ts +22 -22
  97. package/dist/schemas/objects.d.ts +10 -10
  98. package/dist/schemas/ontology.d.ts +174 -174
  99. package/dist/schemas/pipelines.d.ts +131 -190
  100. package/dist/schemas/pipelines.d.ts.map +1 -1
  101. package/dist/schemas/projections.d.ts +918 -563
  102. package/dist/schemas/projections.d.ts.map +1 -1
  103. package/dist/schemas/rules.d.ts +6 -6
  104. package/dist/schemas/status.d.ts +2 -2
  105. package/dist/schemas/syncs.d.ts +88 -124
  106. package/dist/schemas/syncs.d.ts.map +1 -1
  107. package/dist/schemas/webhook-runs.d.ts +41 -41
  108. package/dist/schemas/webhook-runs.d.ts.map +1 -1
  109. package/dist/schemas/workflows.d.ts +792 -406
  110. package/dist/schemas/workflows.d.ts.map +1 -1
  111. package/dist/server.d.ts +6 -6
  112. package/dist/server.d.ts.map +1 -1
  113. package/dist/utils/http.d.ts +2 -0
  114. package/dist/utils/http.d.ts.map +1 -1
  115. package/dist/utils/ws.d.ts +4 -3
  116. package/dist/utils/ws.d.ts.map +1 -1
  117. package/package.json +8 -5
  118. package/src/auth/access-token-boundary.ts +4 -4
  119. package/src/auth/application-access.ts +8 -9
  120. package/src/auth/connector-callback-cookie.ts +95 -0
  121. package/src/auth/guard.ts +11 -11
  122. package/src/auth/public-routes.ts +13 -0
  123. package/src/auth/scope.ts +34 -30
  124. package/src/auth/session-cookies.ts +3 -3
  125. package/src/connectors/http.ts +110 -0
  126. package/src/files/content.ts +11 -0
  127. package/src/openapi/tags.ts +9 -1
  128. package/src/registerRoutes.ts +34 -23
  129. package/src/routes/action-runs.ts +49 -42
  130. package/src/routes/actions.ts +7 -13
  131. package/src/routes/agent-api-gateway.ts +87 -42
  132. package/src/routes/agents.ts +89 -200
  133. package/src/routes/auth.ts +87 -91
  134. package/src/routes/connector-connection-runs.ts +346 -0
  135. package/src/routes/connector-connections.ts +135 -0
  136. package/src/routes/connectors.ts +24 -12
  137. package/src/routes/datasets.ts +55 -54
  138. package/src/routes/events.ts +5 -5
  139. package/src/routes/files.ts +24 -31
  140. package/src/routes/links.ts +32 -35
  141. package/src/routes/logs.ts +6 -11
  142. package/src/routes/objects.ts +51 -114
  143. package/src/routes/ontology.ts +27 -38
  144. package/src/routes/pipelines.ts +28 -62
  145. package/src/routes/project.ts +3 -3
  146. package/src/routes/projections.ts +26 -38
  147. package/src/routes/rules.ts +11 -25
  148. package/src/routes/status.ts +5 -5
  149. package/src/routes/syncs.ts +19 -45
  150. package/src/routes/telemetry.ts +18 -29
  151. package/src/routes/webhook-runs.ts +5 -5
  152. package/src/routes/webhooks.ts +259 -341
  153. package/src/routes/workflows.ts +260 -259
  154. package/src/routes/ws/agents.ts +196 -66
  155. package/src/routes/ws/events.ts +11 -10
  156. package/src/routes/ws/log-subscription-hub.ts +6 -12
  157. package/src/routes/ws/logs.ts +13 -5
  158. package/src/schemas/actions.ts +58 -25
  159. package/src/schemas/agents.ts +19 -14
  160. package/src/schemas/ai-usage.ts +16 -0
  161. package/src/schemas/common.ts +24 -0
  162. package/src/schemas/connectors.ts +164 -0
  163. package/src/schemas/datasets.ts +3 -2
  164. package/src/schemas/pipelines.ts +24 -40
  165. package/src/schemas/projections.ts +17 -3
  166. package/src/schemas/syncs.ts +16 -10
  167. package/src/schemas/webhook-runs.ts +10 -5
  168. package/src/schemas/workflows.ts +42 -25
  169. package/src/server.ts +65 -26
  170. package/src/utils/http.ts +34 -6
  171. package/src/utils/ws.ts +10 -5
@@ -1,32 +1,34 @@
1
1
  import {
2
2
  type OntologySource,
3
3
  type Principal,
4
- type ScopedSixb,
5
- type Sixb,
4
+ type SixbHostView,
6
5
  SYSTEM_PRINCIPAL,
6
+ type WorkflowAgentNodeRunView,
7
7
  type WorkflowDefinition,
8
+ type WorkflowNodeRunView,
8
9
  } from "@sixb/core"
9
10
  import { publishAgentRunCancel } from "@sixb/core/internal/agents"
10
- import { canViewWorkflowIntervention, canViewWorkflowRun } from "@sixb/core/internal/authorization"
11
+ import { createSixbError, toSixbFailure } from "@sixb/core/internal/errors"
12
+ import type { Sixb, WorkflowRunView } from "@sixb/core/internal/request-execution"
11
13
  import {
12
14
  snapshotWorkflowInterventionResponse,
13
15
  type WorkflowInterventionNodeDefinition,
14
16
  } from "@sixb/core/internal/workflows"
15
17
  import type {
16
- WorkflowAgentNodeRunRecord,
17
18
  WorkflowInterventionRecord,
18
19
  WorkflowNodeRunRecord,
19
20
  WorkflowRunRecord,
20
- WorkflowRunStorage,
21
21
  } from "@sixb/core/storage"
22
+ import { AGENT_RUN_FAILURE_CODES, WORKFLOW_RUN_FAILURE_CODES } from "@sixb/core/storage"
22
23
  import type { Elysia } from "elysia"
23
24
  import { z } from "zod"
24
25
  import { bearerSecurityRequirement } from "../auth/access-token-boundary"
25
- import { requestAuthState } from "../auth/scope"
26
+ import { requestAuthState, requireRequestSixb } from "../auth/scope"
26
27
  import {
27
28
  createContextualFileContentResponse,
28
29
  fileContentGetResponses,
29
30
  fileContentHeadResponses,
31
+ handleFileContentQueryValidationError,
30
32
  } from "../files/content"
31
33
  import { SIXB_CSRF_SECURITY_REQUIREMENT } from "../openapi/security"
32
34
  import { OPENAPI_TAGS } from "../openapi/tags"
@@ -65,7 +67,10 @@ const WorkflowRunFileContentQuerySchema = FileContentQuerySchema.extend({
65
67
  path: z
66
68
  .string()
67
69
  .min(1)
68
- .regex(/^\/input(?:\/|$)/, "Workflow run file content paths must start with /input/"),
70
+ .regex(
71
+ /^\/(?:input|output)(?:\/|$)/,
72
+ "Workflow run file content paths must start with /input/ or /output/"
73
+ ),
69
74
  })
70
75
 
71
76
  const WorkflowNodeFileContentQuerySchema = FileContentQuerySchema.extend({
@@ -82,27 +87,31 @@ const WorkflowNodeFileContentParamsSchema = WorkflowRunParamsSchema.extend({
82
87
  nodeKey: z.string().min(1),
83
88
  })
84
89
 
85
- function serializeWorkflowRun(run: WorkflowRunRecord) {
90
+ function serializeWorkflowRunSummary(run: WorkflowRunView) {
86
91
  return {
87
92
  id: run.id,
88
93
  projectId: run.projectId,
89
94
  workflowId: run.workflowId,
90
95
  status: run.status,
91
- input: run.input,
92
96
  queuedAt: run.queuedAt ? toIsoString(run.queuedAt) : undefined,
93
97
  startedAt: toIsoString(run.startedAt),
94
98
  finishedAt: run.finishedAt ? toIsoString(run.finishedAt) : undefined,
95
99
  error: run.error,
96
- requestedBy: serializePrincipal(run.requestedByPrincipal),
100
+ requestedBy: serializePrincipal(run.requestedBy ?? SYSTEM_PRINCIPAL),
101
+ }
102
+ }
103
+
104
+ function serializeWorkflowRunDetail(run: WorkflowRunView) {
105
+ return {
106
+ ...serializeWorkflowRunSummary(run),
107
+ input: run.input,
108
+ output: run.output,
97
109
  }
98
110
  }
99
111
 
100
- type SerializedWorkflowRun = ReturnType<typeof serializeWorkflowRun>
112
+ type SerializedWorkflowRun = ReturnType<typeof serializeWorkflowRunSummary>
101
113
 
102
- function serializeWorkflowNodeRun(
103
- node: WorkflowNodeRunRecord,
104
- agentExecution?: WorkflowAgentNodeRunRecord | null
105
- ) {
114
+ function serializeWorkflowNodeRun(node: WorkflowNodeRunView) {
106
115
  return {
107
116
  id: node.id,
108
117
  projectId: node.projectId,
@@ -118,8 +127,8 @@ function serializeWorkflowNodeRun(
118
127
  finishedAt: node.finishedAt ? toIsoString(node.finishedAt) : undefined,
119
128
  output: node.output,
120
129
  error: node.error,
121
- ...(agentExecution
122
- ? { agentExecution: serializeWorkflowAgentExecutionSummary(agentExecution) }
130
+ ...(node.agentExecution
131
+ ? { agentExecution: serializeWorkflowAgentExecutionSummary(node.agentExecution) }
123
132
  : {}),
124
133
  }
125
134
  }
@@ -128,7 +137,7 @@ function serializePrincipal(principal: Principal) {
128
137
  return { principalType: principal.type, principalId: principal.id }
129
138
  }
130
139
 
131
- function serializeWorkflowAgentExecutionSummary(execution: WorkflowAgentNodeRunRecord) {
140
+ function serializeWorkflowAgentExecutionSummary(execution: WorkflowAgentNodeRunView) {
132
141
  return {
133
142
  agentId: execution.agentId,
134
143
  status: execution.status,
@@ -141,14 +150,11 @@ function serializeWorkflowAgentExecutionSummary(execution: WorkflowAgentNodeRunR
141
150
  }
142
151
  }
143
152
 
144
- function serializeWorkflowAgentExecution(execution: WorkflowAgentNodeRunRecord) {
153
+ function serializeWorkflowAgentExecution(execution: WorkflowAgentNodeRunView) {
145
154
  return {
146
155
  ...serializeWorkflowAgentExecutionSummary(execution),
147
156
  nodeRunId: execution.nodeRunId,
148
157
  prompt: execution.prompt,
149
- executionPrincipal: execution.executionPrincipal
150
- ? serializePrincipal(execution.executionPrincipal)
151
- : undefined,
152
158
  trace: execution.trace,
153
159
  diagnostics: execution.diagnostics,
154
160
  error: execution.error,
@@ -156,30 +162,53 @@ function serializeWorkflowAgentExecution(execution: WorkflowAgentNodeRunRecord)
156
162
  }
157
163
  }
158
164
 
159
- async function serializeWorkflowNodeWithExecution(
160
- storage: WorkflowRunStorage,
161
- node: WorkflowNodeRunRecord
162
- ) {
163
- const execution =
164
- node.nodeType === "agent"
165
- ? await storage.agentNodes.getByNodeRunId({
166
- projectId: node.projectId,
167
- nodeRunId: node.id,
168
- })
169
- : null
170
- return serializeWorkflowNodeRun(node, execution)
165
+ function principalForExecution(sixb: Sixb<readonly OntologySource[]>): Principal {
166
+ return sixb.execution.requestedBy ?? SYSTEM_PRINCIPAL
171
167
  }
172
168
 
173
- function canAccessWorkflowRun(
174
- authz: ReturnType<typeof requestAuthState>["authz"],
175
- scoped: ScopedSixb<readonly OntologySource[]> | null,
176
- run: WorkflowRunRecord
177
- ): boolean {
178
- return (
179
- canViewWorkflowRun(authz, run) && (!scoped || scoped.getWorkflowById(run.workflowId) !== null)
180
- )
169
+ function toWorkflowCancellationFailure(input: {
170
+ readonly message: string
171
+ readonly at: Date
172
+ readonly workflowId: string
173
+ readonly runId: string
174
+ readonly nodeRunId?: string
175
+ }) {
176
+ const details: Readonly<Record<string, string>> = input.nodeRunId
177
+ ? {
178
+ workflowId: input.workflowId,
179
+ workflowRunId: input.runId,
180
+ nodeRunId: input.nodeRunId,
181
+ }
182
+ : { workflowId: input.workflowId, workflowRunId: input.runId }
183
+ return toSixbFailure(createSixbError("runtime.cancelled", input.message, { details }), {
184
+ allowedCodes: WORKFLOW_RUN_FAILURE_CODES,
185
+ at: input.at,
186
+ })
181
187
  }
182
188
 
189
+ function toAgentCancellationFailure(input: {
190
+ readonly message: string
191
+ readonly at: Date
192
+ readonly agentId: string
193
+ readonly workflowId: string
194
+ readonly runId: string
195
+ readonly nodeRunId: string
196
+ }) {
197
+ return toSixbFailure(
198
+ createSixbError("runtime.cancelled", input.message, {
199
+ details: {
200
+ agentId: input.agentId,
201
+ workflowId: input.workflowId,
202
+ workflowRunId: input.runId,
203
+ nodeRunId: input.nodeRunId,
204
+ },
205
+ }),
206
+ {
207
+ allowedCodes: AGENT_RUN_FAILURE_CODES,
208
+ at: input.at,
209
+ }
210
+ )
211
+ }
183
212
  function serializeWorkflowIntervention(intervention: WorkflowInterventionRecord) {
184
213
  return {
185
214
  id: intervention.id,
@@ -206,7 +235,7 @@ function serializeWorkflowIntervention(intervention: WorkflowInterventionRecord)
206
235
  }
207
236
 
208
237
  async function workflowRunFileContentResponse(
209
- sixb: Sixb<readonly OntologySource[]>,
238
+ host: SixbHostView,
210
239
  context: {
211
240
  readonly params: { readonly runId: string }
212
241
  readonly query: unknown
@@ -215,33 +244,29 @@ async function workflowRunFileContentResponse(
215
244
  },
216
245
  options: { readonly head?: boolean } = {}
217
246
  ) {
218
- const { authz, scoped } = requestAuthState(context)
247
+ const sixb = requireRequestSixb(context)
219
248
 
220
- const storage = sixb.storage.workflowRuns
249
+ const storage = host.storage.workflowRuns
221
250
  if (!storage) {
222
251
  return unconfiguredStorageResponse(context.set, "Workflow run storage")
223
252
  }
224
253
 
225
254
  return createContextualFileContentResponse({
226
- blobStorage: sixb.blobStorage,
255
+ blobStorage: host.blobStorage,
227
256
  query: context.query,
228
257
  querySchema: WorkflowRunFileContentQuerySchema,
229
258
  request: context.request,
230
259
  set: context.set,
231
260
  head: options.head,
232
261
  resolveRoot: async () => {
233
- const run = await storage.getById({ projectId: sixb.id, id: context.params.runId })
234
- if (!run || !canAccessWorkflowRun(authz, scoped, run)) {
235
- return null
236
- }
237
-
238
- return serializeWorkflowRun(run)
262
+ const run = await sixb.workflows.runs.getById(context.params.runId)
263
+ return run ? serializeWorkflowRunDetail(run) : null
239
264
  },
240
265
  })
241
266
  }
242
267
 
243
268
  async function workflowNodeFileContentResponse(
244
- sixb: Sixb<readonly OntologySource[]>,
269
+ host: SixbHostView,
245
270
  context: {
246
271
  readonly params: { readonly runId: string; readonly nodeKey: string }
247
272
  readonly query: unknown
@@ -250,34 +275,32 @@ async function workflowNodeFileContentResponse(
250
275
  },
251
276
  options: { readonly head?: boolean } = {}
252
277
  ) {
253
- const { authz, scoped } = requestAuthState(context)
278
+ const sixb = requireRequestSixb(context)
254
279
 
255
- const storage = sixb.storage.workflowRuns
280
+ const storage = host.storage.workflowRuns
256
281
  if (!storage) {
257
282
  return unconfiguredStorageResponse(context.set, "Workflow run storage")
258
283
  }
259
284
 
260
285
  return createContextualFileContentResponse({
261
- blobStorage: sixb.blobStorage,
286
+ blobStorage: host.blobStorage,
262
287
  query: context.query,
263
288
  querySchema: WorkflowNodeFileContentQuerySchema,
264
289
  request: context.request,
265
290
  set: context.set,
266
291
  head: options.head,
267
292
  resolveRoot: async () => {
268
- const run = await storage.getById({ projectId: sixb.id, id: context.params.runId })
269
- if (!run || !canAccessWorkflowRun(authz, scoped, run)) {
293
+ const run = await sixb.workflows.runs.getById(context.params.runId)
294
+ if (!run) {
270
295
  return null
271
296
  }
272
297
 
273
- const result = await storage.nodes.list({
274
- projectId: sixb.id,
275
- workflowRunId: run.id,
298
+ const result = await sixb.workflows.runs.listNodes(run.id, {
276
299
  nodeKey: context.params.nodeKey,
277
300
  limit: 1,
278
301
  order: "asc",
279
302
  })
280
- const [node] = result.nodes
303
+ const [node] = result?.nodes ?? []
281
304
  if (!node) {
282
305
  return null
283
306
  }
@@ -288,38 +311,26 @@ async function workflowNodeFileContentResponse(
288
311
  }
289
312
 
290
313
  async function getLatestWorkflowRun(
291
- sixb: Sixb<readonly OntologySource[]>,
314
+ sixb: ReturnType<typeof requireRequestSixb>,
292
315
  workflowId: string
293
316
  ): Promise<SerializedWorkflowRun | null> {
294
- if (!sixb.storage.workflowRuns) {
295
- return null
296
- }
297
-
298
- const storage = sixb.storage.workflowRuns
299
- const result = await storage.listLatestByWorkflowIds({
300
- projectId: sixb.id,
301
- workflowIds: [workflowId],
302
- })
317
+ const result = await sixb.workflows.runs.listLatest([workflowId])
303
318
 
304
319
  const [latest] = result.runs
305
- return latest ? serializeWorkflowRun(latest) : null
320
+ return latest ? serializeWorkflowRunSummary(latest) : null
306
321
  }
307
322
 
308
323
  async function getLatestWorkflowRuns(
309
- sixb: Sixb<readonly OntologySource[]>,
324
+ sixb: ReturnType<typeof requireRequestSixb>,
310
325
  workflowIds: readonly string[]
311
326
  ): Promise<Map<string, SerializedWorkflowRun>> {
312
- const storage = sixb.storage.workflowRuns
313
- if (!storage || workflowIds.length === 0) {
327
+ if (workflowIds.length === 0) {
314
328
  return new Map()
315
329
  }
316
330
 
317
- const result = await storage.listLatestByWorkflowIds({
318
- projectId: sixb.id,
319
- workflowIds,
320
- })
331
+ const result = await sixb.workflows.runs.listLatest(workflowIds)
321
332
 
322
- return new Map(result.runs.map((run) => [run.workflowId, serializeWorkflowRun(run)]))
333
+ return new Map(result.runs.map((run) => [run.workflowId, serializeWorkflowRunSummary(run)]))
323
334
  }
324
335
 
325
336
  function serializeWorkflow(
@@ -403,14 +414,14 @@ function assertPendingIntervention(intervention: WorkflowInterventionRecord): vo
403
414
  }
404
415
 
405
416
  async function emitWorkflowInterventionSubmitted(
406
- sixb: Sixb<readonly OntologySource[]>,
417
+ host: SixbHostView,
407
418
  intervention: WorkflowInterventionRecord
408
419
  ): Promise<void> {
409
420
  if (!intervention.submittedAt) {
410
421
  throw new Error(`[SixbServer] Submitted intervention '${intervention.id}' has no submittedAt.`)
411
422
  }
412
423
 
413
- await sixb.events.append({
424
+ await host.events.append({
414
425
  events: [
415
426
  {
416
427
  type: "workflow.intervention.submitted",
@@ -428,13 +439,13 @@ async function emitWorkflowInterventionSubmitted(
428
439
  }
429
440
 
430
441
  async function emitWorkflowInterventionCancelled(input: {
431
- readonly sixb: Sixb<readonly OntologySource[]>
442
+ readonly host: SixbHostView
432
443
  readonly workflow: WorkflowDefinition
433
444
  readonly intervention: WorkflowInterventionRecord
434
445
  readonly node: WorkflowNodeRunRecord
435
446
  readonly run: WorkflowRunRecord
436
447
  }): Promise<void> {
437
- const { sixb, workflow, intervention, node, run } = input
448
+ const { host, workflow, intervention, node, run } = input
438
449
  if (!intervention.cancelledAt) {
439
450
  throw new Error(`[SixbServer] Cancelled intervention '${intervention.id}' has no cancelledAt.`)
440
451
  }
@@ -445,7 +456,7 @@ async function emitWorkflowInterventionCancelled(input: {
445
456
  throw new Error(`[SixbServer] Cancelled workflow run '${run.id}' has no finishedAt.`)
446
457
  }
447
458
 
448
- await sixb.events.append({
459
+ await host.events.append({
449
460
  events: [
450
461
  {
451
462
  type: "workflow.intervention.cancelled",
@@ -489,16 +500,16 @@ async function emitWorkflowInterventionCancelled(input: {
489
500
  }
490
501
 
491
502
  async function emitWorkflowRunCancelled(input: {
492
- readonly sixb: Sixb<readonly OntologySource[]>
503
+ readonly host: SixbHostView
493
504
  readonly workflow: WorkflowDefinition
494
505
  readonly run: WorkflowRunRecord
495
506
  readonly node?: WorkflowNodeRunRecord
496
507
  }): Promise<void> {
497
- const { sixb, workflow, run, node } = input
508
+ const { host, workflow, run, node } = input
498
509
  if (!run.finishedAt) {
499
510
  throw new Error(`[SixbServer] Cancelled workflow run '${run.id}' has no finishedAt.`)
500
511
  }
501
- await sixb.events.append({
512
+ await host.events.append({
502
513
  events: [
503
514
  ...(node?.finishedAt
504
515
  ? [
@@ -534,13 +545,13 @@ async function emitWorkflowRunCancelled(input: {
534
545
  })
535
546
  }
536
547
 
537
- export function registerWorkflowRoutes(app: Elysia, sixb: Sixb<readonly OntologySource[]>) {
548
+ export function registerWorkflowRoutes(app: Elysia, host: SixbHostView) {
538
549
  return app
539
550
  .get(
540
551
  "/api/workflows",
541
552
  async (context) => {
542
- const { scoped } = requestAuthState(context)
543
- const workflows = scoped ? scoped.listWorkflows() : sixb.workflows.list()
553
+ const sixb = requireRequestSixb(context)
554
+ const workflows = sixb.workflows.list()
544
555
  const latestRuns = await getLatestWorkflowRuns(
545
556
  sixb,
546
557
  workflows.map((workflow) => workflow.id)
@@ -564,12 +575,10 @@ export function registerWorkflowRoutes(app: Elysia, sixb: Sixb<readonly Ontology
564
575
  "/api/workflows/:workflowId",
565
576
  async (context) => {
566
577
  const { params, set } = context
567
- const { scoped } = requestAuthState(context)
578
+ const sixb = requireRequestSixb(context)
568
579
  // Non-runnable workflows are hidden as 404 (existence-hiding), matching
569
580
  // the object/action read routes.
570
- const workflow = scoped
571
- ? scoped.getWorkflowById(params.workflowId)
572
- : sixb.workflows.getById(params.workflowId)
581
+ const workflow = sixb.workflows.getById(params.workflowId)
573
582
  if (!workflow) {
574
583
  set.status = 404
575
584
  return { error: "Workflow not found" }
@@ -592,25 +601,18 @@ export function registerWorkflowRoutes(app: Elysia, sixb: Sixb<readonly Ontology
592
601
  "/api/workflow-interventions",
593
602
  async (context) => {
594
603
  const { query, set } = context
595
- const { authz, scoped } = requestAuthState(context)
604
+ const sixb = requireRequestSixb(context)
596
605
  try {
597
606
  const parsed = WorkflowInterventionsQuerySchema.parse(query)
598
607
  const limit = parseOptionalInt(parsed.limit)
599
608
  const offset = parseOptionalInt(parsed.offset)
600
- const storage = sixb.storage.workflowInterventions
609
+ const storage = host.storage.workflowInterventions
601
610
  if (!storage) {
602
611
  return unconfiguredStorageResponse(set, "Workflow intervention storage")
603
612
  }
604
613
 
605
- const workflowIds = scoped
606
- ? scoped.listWorkflows().map((workflow) => workflow.id)
607
- : authz
608
- ? [...authz.grants["run:workflow"]]
609
- : undefined
610
- const result = await storage.list({
611
- projectId: sixb.id,
614
+ const result = await sixb.workflows.interventions.list({
612
615
  workflowId: parsed.workflowId,
613
- workflowIds,
614
616
  workflowRunId: parsed.workflowRunId,
615
617
  nodeRunId: parsed.nodeRunId,
616
618
  nodeId: parsed.nodeId,
@@ -651,22 +653,15 @@ export function registerWorkflowRoutes(app: Elysia, sixb: Sixb<readonly Ontology
651
653
  "/api/workflow-interventions/:interventionId",
652
654
  async (context) => {
653
655
  const { params, set } = context
654
- const { authz, scoped } = requestAuthState(context)
656
+ const sixb = requireRequestSixb(context)
655
657
  try {
656
- const storage = sixb.storage.workflowInterventions
658
+ const storage = host.storage.workflowInterventions
657
659
  if (!storage) {
658
660
  return unconfiguredStorageResponse(set, "Workflow intervention storage")
659
661
  }
660
662
 
661
- const intervention = await storage.getById({
662
- projectId: sixb.id,
663
- id: params.interventionId,
664
- })
665
- if (
666
- !intervention ||
667
- !canViewWorkflowIntervention(authz, intervention) ||
668
- (scoped && !scoped.getWorkflowById(intervention.workflowId))
669
- ) {
663
+ const intervention = await sixb.workflows.interventions.getById(params.interventionId)
664
+ if (!intervention) {
670
665
  set.status = 404
671
666
  return { error: "Workflow intervention not found" }
672
667
  }
@@ -695,22 +690,15 @@ export function registerWorkflowRoutes(app: Elysia, sixb: Sixb<readonly Ontology
695
690
  "/api/workflow-interventions/:interventionId/submit",
696
691
  async (context) => {
697
692
  const { params, body, set } = context
698
- const { authz, scoped } = requestAuthState(context)
693
+ const sixb = requireRequestSixb(context)
699
694
  try {
700
- const storage = sixb.storage.workflowInterventions
695
+ const storage = host.storage.workflowInterventions
701
696
  if (!storage) {
702
697
  return unconfiguredStorageResponse(set, "Workflow intervention storage")
703
698
  }
704
699
 
705
- const intervention = await storage.getById({
706
- projectId: sixb.id,
707
- id: params.interventionId,
708
- })
709
- if (
710
- !intervention ||
711
- !canViewWorkflowIntervention(authz, intervention) ||
712
- (scoped && !scoped.getWorkflowById(intervention.workflowId))
713
- ) {
700
+ const intervention = await sixb.workflows.interventions.getById(params.interventionId)
701
+ if (!intervention) {
714
702
  set.status = 404
715
703
  return { error: "Workflow intervention not found" }
716
704
  }
@@ -728,31 +716,30 @@ export function registerWorkflowRoutes(app: Elysia, sixb: Sixb<readonly Ontology
728
716
  workflowId: workflow.id,
729
717
  intervention: node.intervention,
730
718
  value: parsedBody.response,
731
- valueTypesById: sixb.ontology.getValueTypesById(),
719
+ valueTypesById: host.definitions.ontology.getValueTypesById(),
732
720
  })
733
721
 
734
722
  const submitted = await storage.submit({
735
- projectId: sixb.id,
723
+ projectId: host.id,
736
724
  id: intervention.id,
737
725
  response,
738
- submittedBy: serializePrincipal(authz?.principal ?? SYSTEM_PRINCIPAL),
726
+ submittedBy: serializePrincipal(principalForExecution(sixb)),
739
727
  })
740
728
 
741
- const [job] = await sixb.queues.workflows.enqueue({
742
- projectId: sixb.id,
729
+ const [job] = await host.queues.workflows.enqueue({
730
+ projectId: host.id,
743
731
  jobs: [
744
732
  {
745
733
  type: "workflow.run.resume.requested",
746
734
  payload: {
747
- workflowId: submitted.workflowId,
748
735
  runId: submitted.workflowRunId,
749
- resume: { kind: "intervention", interventionId: submitted.id },
736
+ nodeRunId: submitted.nodeRunId,
750
737
  },
751
738
  },
752
739
  ],
753
740
  })
754
741
 
755
- await emitWorkflowInterventionSubmitted(sixb, submitted)
742
+ await emitWorkflowInterventionSubmitted(host, submitted)
756
743
 
757
744
  set.status = 202
758
745
  return SubmitWorkflowInterventionResponseSchema.parse({
@@ -784,27 +771,20 @@ export function registerWorkflowRoutes(app: Elysia, sixb: Sixb<readonly Ontology
784
771
  "/api/workflow-interventions/:interventionId/cancel",
785
772
  async (context) => {
786
773
  const { params, body, set } = context
787
- const { authz, scoped } = requestAuthState(context)
774
+ const sixb = requireRequestSixb(context)
788
775
  try {
789
- const interventionStorage = sixb.storage.workflowInterventions
776
+ const interventionStorage = host.storage.workflowInterventions
790
777
  if (!interventionStorage) {
791
778
  return unconfiguredStorageResponse(set, "Workflow intervention storage")
792
779
  }
793
780
 
794
- const runStorage = sixb.storage.workflowRuns
781
+ const runStorage = host.storage.workflowRuns
795
782
  if (!runStorage) {
796
783
  return unconfiguredStorageResponse(set, "Workflow run storage")
797
784
  }
798
785
 
799
- const intervention = await interventionStorage.getById({
800
- projectId: sixb.id,
801
- id: params.interventionId,
802
- })
803
- if (
804
- !intervention ||
805
- !canViewWorkflowIntervention(authz, intervention) ||
806
- (scoped && !scoped.getWorkflowById(intervention.workflowId))
807
- ) {
786
+ const intervention = await sixb.workflows.interventions.getById(params.interventionId)
787
+ if (!intervention) {
808
788
  set.status = 404
809
789
  return { error: "Workflow intervention not found" }
810
790
  }
@@ -817,10 +797,7 @@ export function registerWorkflowRoutes(app: Elysia, sixb: Sixb<readonly Ontology
817
797
  }
818
798
  requireRegisteredInterventionNode(workflow, intervention)
819
799
 
820
- const run = await runStorage.getById({
821
- projectId: sixb.id,
822
- id: intervention.workflowRunId,
823
- })
800
+ const run = await sixb.workflows.runs.getById(intervention.workflowRunId)
824
801
  if (!run) {
825
802
  set.status = 404
826
803
  return { error: "Workflow run not found" }
@@ -831,7 +808,7 @@ export function registerWorkflowRoutes(app: Elysia, sixb: Sixb<readonly Ontology
831
808
  }
832
809
 
833
810
  const nodeRun = await runStorage.nodes.getById({
834
- projectId: sixb.id,
811
+ projectId: host.id,
835
812
  id: intervention.nodeRunId,
836
813
  })
837
814
  if (!nodeRun) {
@@ -845,37 +822,49 @@ export function registerWorkflowRoutes(app: Elysia, sixb: Sixb<readonly Ontology
845
822
 
846
823
  CancelWorkflowInterventionBodySchema.parse(body ?? {})
847
824
  const cancelledAt = new Date()
848
- const { cancelled, cancelledNode, cancelledRun } = await sixb.storage.transaction(
825
+ const cancellationMessage = "Workflow intervention cancelled."
826
+ const { cancelled, cancelledNode, cancelledRun } = await host.storage.transaction(
849
827
  async (tx) => {
850
828
  if (!tx.workflowInterventions || !tx.workflowRuns) {
851
829
  throw new Error("[SixbServer] Workflow storage disappeared during cancellation.")
852
830
  }
853
831
  const cancelled = await tx.workflowInterventions.cancel({
854
- projectId: sixb.id,
832
+ projectId: host.id,
855
833
  id: intervention.id,
856
834
  cancelledAt,
857
- cancelledBy: serializePrincipal(authz?.principal ?? SYSTEM_PRINCIPAL),
835
+ cancelledBy: serializePrincipal(principalForExecution(sixb)),
858
836
  })
859
837
  const cancelledNode = await tx.workflowRuns.nodes.finish({
860
- projectId: sixb.id,
838
+ projectId: host.id,
861
839
  id: intervention.nodeRunId,
862
840
  status: "cancelled",
863
841
  finishedAt: cancelledAt,
864
- error: "Workflow intervention cancelled.",
842
+ error: toWorkflowCancellationFailure({
843
+ message: cancellationMessage,
844
+ at: cancelledAt,
845
+ workflowId: intervention.workflowId,
846
+ runId: intervention.workflowRunId,
847
+ nodeRunId: intervention.nodeRunId,
848
+ }),
865
849
  })
866
850
  const cancelledRun = await tx.workflowRuns.finish({
867
- projectId: sixb.id,
851
+ projectId: host.id,
868
852
  id: intervention.workflowRunId,
869
853
  status: "cancelled",
870
854
  finishedAt: cancelledAt,
871
- error: "Workflow intervention cancelled.",
855
+ error: toWorkflowCancellationFailure({
856
+ message: cancellationMessage,
857
+ at: cancelledAt,
858
+ workflowId: intervention.workflowId,
859
+ runId: intervention.workflowRunId,
860
+ }),
872
861
  })
873
862
  return { cancelled, cancelledNode, cancelledRun }
874
863
  }
875
864
  )
876
865
 
877
866
  await emitWorkflowInterventionCancelled({
878
- sixb,
867
+ host,
879
868
  workflow,
880
869
  intervention: cancelled,
881
870
  node: cancelledNode,
@@ -910,25 +899,18 @@ export function registerWorkflowRoutes(app: Elysia, sixb: Sixb<readonly Ontology
910
899
  "/api/workflow-runs",
911
900
  async (context) => {
912
901
  const { query, set } = context
913
- const { authz, scoped } = requestAuthState(context)
902
+ const sixb = requireRequestSixb(context)
914
903
  try {
915
904
  const parsed = WorkflowRunsQuerySchema.parse(query)
916
905
  const limit = parseOptionalInt(parsed.limit)
917
906
  const offset = parseOptionalInt(parsed.offset)
918
- const storage = sixb.storage.workflowRuns
907
+ const storage = host.storage.workflowRuns
919
908
  if (!storage) {
920
909
  return unconfiguredStorageResponse(set, "Workflow run storage")
921
910
  }
922
911
 
923
- const workflowIds = scoped
924
- ? scoped.listWorkflows().map((workflow) => workflow.id)
925
- : authz
926
- ? [...authz.grants["run:workflow"]]
927
- : undefined
928
- const result = await storage.list({
929
- projectId: sixb.id,
912
+ const result = await sixb.workflows.runs.list({
930
913
  workflowId: parsed.workflowId,
931
- workflowIds,
932
914
  statuses: parsed.status ? [parsed.status] : undefined,
933
915
  startedAfter: parseDate(parsed.startedAfter),
934
916
  startedBefore: parseDate(parsed.startedBefore),
@@ -938,7 +920,7 @@ export function registerWorkflowRoutes(app: Elysia, sixb: Sixb<readonly Ontology
938
920
  })
939
921
 
940
922
  return {
941
- runs: result.runs.map(serializeWorkflowRun),
923
+ runs: result.runs.map(serializeWorkflowRunSummary),
942
924
  hasMore: result.hasMore,
943
925
  total: result.total,
944
926
  }
@@ -957,6 +939,7 @@ export function registerWorkflowRoutes(app: Elysia, sixb: Sixb<readonly Ontology
957
939
  summary: "List workflow run history",
958
940
  tags: [OPENAPI_TAGS.workflowRuns.name],
959
941
  operationId: "listWorkflowRuns",
942
+ security: bearerSecurityRequirement("listWorkflowRuns"),
960
943
  },
961
944
  }
962
945
  )
@@ -964,30 +947,36 @@ export function registerWorkflowRoutes(app: Elysia, sixb: Sixb<readonly Ontology
964
947
  "/api/workflow-runs/:runId",
965
948
  async (context) => {
966
949
  const { params, set } = context
967
- const { authz, scoped } = requestAuthState(context)
950
+ const { agentExecution } = requestAuthState(context)
951
+ const sixb = requireRequestSixb(context)
968
952
  try {
969
- const storage = sixb.storage.workflowRuns
953
+ const storage = host.storage.workflowRuns
970
954
  if (!storage) {
971
955
  return unconfiguredStorageResponse(set, "Workflow run storage")
972
956
  }
973
957
 
974
- const run = await storage.getById({ projectId: sixb.id, id: params.runId })
975
- if (!run || !canAccessWorkflowRun(authz, scoped, run)) {
958
+ const run = await sixb.workflows.runs.getById(params.runId)
959
+ if (!run) {
976
960
  set.status = 404
977
961
  return { error: "Workflow run not found" }
978
962
  }
979
963
 
980
- const nodes = await storage.nodes.list({
981
- projectId: sixb.id,
982
- workflowRunId: run.id,
983
- order: "asc",
984
- })
964
+ const serializedRun = serializeWorkflowRunDetail(run)
965
+
966
+ if (agentExecution) {
967
+ return WorkflowRunDetailResponseSchema.parse({ run: serializedRun, nodes: [] })
968
+ }
969
+
970
+ const nodes = await sixb.workflows.runs.listNodes(run.id, { order: "asc" })
971
+ if (!nodes) {
972
+ throw new Error(
973
+ `[SixbServer] Authorized workflow run '${run.id}' lost its execution view.`
974
+ )
975
+ }
985
976
 
986
977
  return WorkflowRunDetailResponseSchema.parse({
987
- run: serializeWorkflowRun(run),
988
- nodes: await Promise.all(
989
- nodes.nodes.map((node) => serializeWorkflowNodeWithExecution(storage, node))
990
- ),
978
+ run: serializedRun,
979
+ nodes: nodes.nodes.map(serializeWorkflowNodeRun),
991
980
  })
992
981
  } catch (error) {
993
982
  return handleRouteError(error, set)
@@ -1005,6 +994,7 @@ export function registerWorkflowRoutes(app: Elysia, sixb: Sixb<readonly Ontology
1005
994
  summary: "Get workflow run detail",
1006
995
  tags: [OPENAPI_TAGS.workflowRuns.name],
1007
996
  operationId: "getWorkflowRun",
997
+ security: bearerSecurityRequirement("getWorkflowRun"),
1008
998
  },
1009
999
  }
1010
1000
  )
@@ -1012,33 +1002,24 @@ export function registerWorkflowRoutes(app: Elysia, sixb: Sixb<readonly Ontology
1012
1002
  "/api/workflow-runs/:runId/nodes/:nodeKey/agent-execution",
1013
1003
  async (context) => {
1014
1004
  const { params, set } = context
1015
- const { authz, scoped } = requestAuthState(context)
1005
+ const sixb = requireRequestSixb(context)
1016
1006
  try {
1017
- const storage = sixb.storage.workflowRuns
1007
+ const storage = host.storage.workflowRuns
1018
1008
  if (!storage) {
1019
1009
  return unconfiguredStorageResponse(set, "Workflow run storage")
1020
1010
  }
1021
- const run = await storage.getById({ projectId: sixb.id, id: params.runId })
1022
- if (!run || !canAccessWorkflowRun(authz, scoped, run)) {
1011
+ const run = await sixb.workflows.runs.getById(params.runId)
1012
+ if (!run) {
1023
1013
  set.status = 404
1024
1014
  return { error: "Workflow agent execution not found" }
1025
1015
  }
1026
- const listed = await storage.nodes.list({
1027
- projectId: sixb.id,
1028
- workflowRunId: run.id,
1016
+ const listed = await sixb.workflows.runs.listNodes(run.id, {
1029
1017
  nodeKey: params.nodeKey,
1030
1018
  limit: 1,
1031
1019
  order: "asc",
1032
1020
  })
1033
- const node = listed.nodes[0]
1034
- if (!node || node.nodeType !== "agent") {
1035
- set.status = 404
1036
- return { error: "Workflow agent execution not found" }
1037
- }
1038
- const execution = await storage.agentNodes.getByNodeRunId({
1039
- projectId: sixb.id,
1040
- nodeRunId: node.id,
1041
- })
1021
+ const node = listed?.nodes[0]
1022
+ const execution = node?.nodeType === "agent" ? node.agentExecution : undefined
1042
1023
  if (!execution) {
1043
1024
  set.status = 404
1044
1025
  return { error: "Workflow agent execution not found" }
@@ -1068,15 +1049,15 @@ export function registerWorkflowRoutes(app: Elysia, sixb: Sixb<readonly Ontology
1068
1049
  "/api/workflow-runs/:runId/cancel",
1069
1050
  async (context) => {
1070
1051
  const { params, body, set } = context
1071
- const { authz, scoped } = requestAuthState(context)
1052
+ const sixb = requireRequestSixb(context)
1072
1053
  try {
1073
1054
  CancelWorkflowRunBodySchema.parse(body ?? {})
1074
- const storage = sixb.storage.workflowRuns
1055
+ const storage = host.storage.workflowRuns
1075
1056
  if (!storage) {
1076
1057
  return unconfiguredStorageResponse(set, "Workflow run storage")
1077
1058
  }
1078
- const existing = await storage.getById({ projectId: sixb.id, id: params.runId })
1079
- if (!existing || !canAccessWorkflowRun(authz, scoped, existing)) {
1059
+ const existing = await sixb.workflows.runs.getById(params.runId)
1060
+ if (!existing) {
1080
1061
  set.status = 404
1081
1062
  return { error: "Workflow run not found" }
1082
1063
  }
@@ -1085,32 +1066,36 @@ export function registerWorkflowRoutes(app: Elysia, sixb: Sixb<readonly Ontology
1085
1066
  set.status = 404
1086
1067
  return { error: "Workflow not found" }
1087
1068
  }
1088
- const listed = await storage.nodes.list({
1089
- projectId: sixb.id,
1090
- workflowRunId: existing.id,
1091
- order: "asc",
1092
- })
1093
1069
  if (existing.status === "cancelled") {
1070
+ const nodes = await sixb.workflows.runs.listNodes(existing.id, { order: "asc" })
1071
+ if (!nodes) {
1072
+ throw new Error(
1073
+ `[SixbServer] Authorized workflow run '${existing.id}' lost its execution view.`
1074
+ )
1075
+ }
1094
1076
  return CancelWorkflowRunResponseSchema.parse({
1095
- run: serializeWorkflowRun(existing),
1096
- nodes: await Promise.all(
1097
- listed.nodes.map((node) => serializeWorkflowNodeWithExecution(storage, node))
1098
- ),
1077
+ run: serializeWorkflowRunDetail(existing),
1078
+ nodes: nodes.nodes.map(serializeWorkflowNodeRun),
1099
1079
  })
1100
1080
  }
1101
1081
  if (existing.status === "succeeded" || existing.status === "failed") {
1102
1082
  set.status = 400
1103
1083
  return { error: `Workflow run is already ${existing.status}` }
1104
1084
  }
1085
+ const listed = await storage.nodes.list({
1086
+ projectId: host.id,
1087
+ workflowRunId: existing.id,
1088
+ order: "asc",
1089
+ })
1105
1090
 
1106
1091
  const cancelledAt = new Date()
1107
- const cancellationError = "Workflow run cancelled."
1108
- const result = await sixb.storage.transaction(async (tx) => {
1092
+ const cancellationMessage = "Workflow run cancelled."
1093
+ const result = await host.storage.transaction(async (tx) => {
1109
1094
  const runs = tx.workflowRuns
1110
1095
  if (!runs) {
1111
1096
  throw new Error("[SixbServer] Workflow storage disappeared during cancellation.")
1112
1097
  }
1113
- const run = await runs.getById({ projectId: sixb.id, id: existing.id })
1098
+ const run = await runs.getById({ projectId: host.id, id: existing.id })
1114
1099
  if (!run || !["queued", "running", "waiting"].includes(run.status)) {
1115
1100
  throw new Error(`[SixbServer] Workflow run '${existing.id}' is no longer active.`)
1116
1101
  }
@@ -1120,21 +1105,28 @@ export function registerWorkflowRoutes(app: Elysia, sixb: Sixb<readonly Ontology
1120
1105
  let cancelledNode: WorkflowNodeRunRecord | undefined
1121
1106
  if (active?.nodeType === "agent") {
1122
1107
  const execution = await runs.agentNodes.getByNodeRunId({
1123
- projectId: sixb.id,
1108
+ projectId: host.id,
1124
1109
  nodeRunId: active.id,
1125
1110
  })
1126
1111
  if (execution?.status === "queued" || execution?.status === "running") {
1127
1112
  await runs.agentNodes.cancel({
1128
- projectId: sixb.id,
1113
+ projectId: host.id,
1129
1114
  nodeRunId: active.id,
1130
1115
  completedAt: cancelledAt,
1131
- error: cancellationError,
1116
+ error: toAgentCancellationFailure({
1117
+ message: cancellationMessage,
1118
+ at: cancelledAt,
1119
+ agentId: execution.agentId,
1120
+ workflowId: run.workflowId,
1121
+ runId: run.id,
1122
+ nodeRunId: active.id,
1123
+ }),
1132
1124
  })
1133
1125
  }
1134
1126
  }
1135
1127
  if (active?.nodeType === "intervention" && tx.workflowInterventions) {
1136
1128
  const pending = await tx.workflowInterventions.list({
1137
- projectId: sixb.id,
1129
+ projectId: host.id,
1138
1130
  workflowRunId: run.id,
1139
1131
  nodeRunId: active.id,
1140
1132
  statuses: ["pending"],
@@ -1143,38 +1135,49 @@ export function registerWorkflowRoutes(app: Elysia, sixb: Sixb<readonly Ontology
1143
1135
  const intervention = pending.interventions[0]
1144
1136
  if (intervention) {
1145
1137
  await tx.workflowInterventions.cancel({
1146
- projectId: sixb.id,
1138
+ projectId: host.id,
1147
1139
  id: intervention.id,
1148
1140
  cancelledAt,
1149
- cancelledBy: serializePrincipal(authz?.principal ?? SYSTEM_PRINCIPAL),
1141
+ cancelledBy: serializePrincipal(principalForExecution(sixb)),
1150
1142
  })
1151
1143
  }
1152
1144
  }
1153
1145
  if (active) {
1154
1146
  cancelledNode = await runs.nodes.finish({
1155
- projectId: sixb.id,
1147
+ projectId: host.id,
1156
1148
  id: active.id,
1157
1149
  status: "cancelled",
1158
1150
  finishedAt: cancelledAt,
1159
- error: cancellationError,
1151
+ error: toWorkflowCancellationFailure({
1152
+ message: cancellationMessage,
1153
+ at: cancelledAt,
1154
+ workflowId: run.workflowId,
1155
+ runId: run.id,
1156
+ nodeRunId: active.id,
1157
+ }),
1160
1158
  executionToken: run.execution?.token,
1161
1159
  })
1162
1160
  }
1163
1161
  const cancelledRun = await runs.finish({
1164
- projectId: sixb.id,
1162
+ projectId: host.id,
1165
1163
  id: run.id,
1166
1164
  status: "cancelled",
1167
1165
  finishedAt: cancelledAt,
1168
- error: cancellationError,
1166
+ error: toWorkflowCancellationFailure({
1167
+ message: cancellationMessage,
1168
+ at: cancelledAt,
1169
+ workflowId: run.workflowId,
1170
+ runId: run.id,
1171
+ }),
1169
1172
  executionToken: run.execution?.token,
1170
1173
  })
1171
1174
  return { run: cancelledRun, node: cancelledNode }
1172
1175
  })
1173
1176
 
1174
- await emitWorkflowRunCancelled({ sixb, workflow, ...result })
1177
+ await emitWorkflowRunCancelled({ host, workflow, ...result })
1175
1178
  if (result.node?.nodeType === "agent") {
1176
- await publishAgentRunCancel(sixb.broker, {
1177
- projectId: sixb.id,
1179
+ await publishAgentRunCancel(host.broker, {
1180
+ projectId: host.id,
1178
1181
  runId: result.node.id,
1179
1182
  }).catch((error) => {
1180
1183
  console.error(
@@ -1183,16 +1186,18 @@ export function registerWorkflowRoutes(app: Elysia, sixb: Sixb<readonly Ontology
1183
1186
  )
1184
1187
  })
1185
1188
  }
1186
- const nodes = await storage.nodes.list({
1187
- projectId: sixb.id,
1188
- workflowRunId: existing.id,
1189
- order: "asc",
1190
- })
1189
+ const nodes = await sixb.workflows.runs.listNodes(existing.id, { order: "asc" })
1190
+ if (!nodes) {
1191
+ throw new Error(
1192
+ `[SixbServer] Authorized workflow run '${existing.id}' lost its execution view.`
1193
+ )
1194
+ }
1191
1195
  return CancelWorkflowRunResponseSchema.parse({
1192
- run: serializeWorkflowRun(result.run),
1193
- nodes: await Promise.all(
1194
- nodes.nodes.map((node) => serializeWorkflowNodeWithExecution(storage, node))
1195
- ),
1196
+ run: serializeWorkflowRunDetail({
1197
+ ...result.run,
1198
+ ...(existing.requestedBy === undefined ? {} : { requestedBy: existing.requestedBy }),
1199
+ }),
1200
+ nodes: nodes.nodes.map(serializeWorkflowNodeRun),
1196
1201
  })
1197
1202
  } catch (error) {
1198
1203
  return handleRouteError(error, set)
@@ -1217,10 +1222,11 @@ export function registerWorkflowRoutes(app: Elysia, sixb: Sixb<readonly Ontology
1217
1222
  )
1218
1223
  .get(
1219
1224
  "/api/workflow-runs/:runId/files/content",
1220
- (context) => workflowRunFileContentResponse(sixb, context),
1225
+ (context) => workflowRunFileContentResponse(host, context),
1221
1226
  {
1222
1227
  params: WorkflowRunParamsSchema,
1223
- query: FileContentQuerySchema,
1228
+ query: WorkflowRunFileContentQuerySchema,
1229
+ error: handleFileContentQueryValidationError,
1224
1230
  detail: {
1225
1231
  summary: "Get workflow run file content",
1226
1232
  tags: ["Workflows"],
@@ -1232,10 +1238,11 @@ export function registerWorkflowRoutes(app: Elysia, sixb: Sixb<readonly Ontology
1232
1238
  )
1233
1239
  .head(
1234
1240
  "/api/workflow-runs/:runId/files/content",
1235
- (context) => workflowRunFileContentResponse(sixb, context, { head: true }),
1241
+ (context) => workflowRunFileContentResponse(host, context, { head: true }),
1236
1242
  {
1237
1243
  params: WorkflowRunParamsSchema,
1238
- query: FileContentQuerySchema,
1244
+ query: WorkflowRunFileContentQuerySchema,
1245
+ error: handleFileContentQueryValidationError,
1239
1246
  detail: {
1240
1247
  summary: "Head workflow run file content",
1241
1248
  tags: ["Workflows"],
@@ -1247,10 +1254,11 @@ export function registerWorkflowRoutes(app: Elysia, sixb: Sixb<readonly Ontology
1247
1254
  )
1248
1255
  .get(
1249
1256
  "/api/workflow-runs/:runId/nodes/:nodeKey/files/content",
1250
- (context) => workflowNodeFileContentResponse(sixb, context),
1257
+ (context) => workflowNodeFileContentResponse(host, context),
1251
1258
  {
1252
1259
  params: WorkflowNodeFileContentParamsSchema,
1253
- query: FileContentQuerySchema,
1260
+ query: WorkflowNodeFileContentQuerySchema,
1261
+ error: handleFileContentQueryValidationError,
1254
1262
  detail: {
1255
1263
  summary: "Get workflow node run file content",
1256
1264
  tags: ["Workflows"],
@@ -1262,10 +1270,11 @@ export function registerWorkflowRoutes(app: Elysia, sixb: Sixb<readonly Ontology
1262
1270
  )
1263
1271
  .head(
1264
1272
  "/api/workflow-runs/:runId/nodes/:nodeKey/files/content",
1265
- (context) => workflowNodeFileContentResponse(sixb, context, { head: true }),
1273
+ (context) => workflowNodeFileContentResponse(host, context, { head: true }),
1266
1274
  {
1267
1275
  params: WorkflowNodeFileContentParamsSchema,
1268
- query: FileContentQuerySchema,
1276
+ query: WorkflowNodeFileContentQuerySchema,
1277
+ error: handleFileContentQueryValidationError,
1269
1278
  detail: {
1270
1279
  summary: "Head workflow node run file content",
1271
1280
  tags: ["Workflows"],
@@ -1279,27 +1288,19 @@ export function registerWorkflowRoutes(app: Elysia, sixb: Sixb<readonly Ontology
1279
1288
  "/api/workflows/:workflowId/runs",
1280
1289
  async (context) => {
1281
1290
  const { params, body, set } = context
1282
- const { scoped } = requestAuthState(context)
1291
+ const sixb = requireRequestSixb(context)
1283
1292
  try {
1284
- const workflow = sixb.workflows.getById(params.workflowId)
1285
- if (!workflow) {
1286
- set.status = 404
1287
- return { error: "Workflow not found" }
1288
- }
1289
-
1290
- if (!sixb.storage.workflowRuns) {
1293
+ if (!host.storage.workflowRuns) {
1291
1294
  return unconfiguredStorageResponse(set, "Workflow run storage")
1292
1295
  }
1293
1296
 
1294
1297
  const parsedBody = RequestWorkflowRunBodySchema.parse(body)
1295
1298
  const input = {
1296
- workflowId: workflow.id,
1299
+ workflowId: params.workflowId,
1297
1300
  input: parsedBody.input ?? {},
1298
1301
  source: { type: "manual" } as const,
1299
1302
  }
1300
- const result = scoped
1301
- ? await scoped.requestWorkflowRun(input)
1302
- : await sixb.requestWorkflowRun(input)
1303
+ const result = await sixb.workflows.requestById(input)
1303
1304
 
1304
1305
  set.status = 202
1305
1306
  return RequestWorkflowRunResponseSchema.parse({