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