@sixb/server 0.1.1 → 0.1.3

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 (178) 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/experience.d.ts +33 -0
  7. package/dist/auth/experience.d.ts.map +1 -0
  8. package/dist/auth/guard.d.ts +3 -3
  9. package/dist/auth/guard.d.ts.map +1 -1
  10. package/dist/auth/public-routes.d.ts.map +1 -1
  11. package/dist/auth/scope.d.ts +13 -27
  12. package/dist/auth/scope.d.ts.map +1 -1
  13. package/dist/auth/session-cookies.d.ts +2 -2
  14. package/dist/auth/session-cookies.d.ts.map +1 -1
  15. package/dist/connectors/http.d.ts +16 -0
  16. package/dist/connectors/http.d.ts.map +1 -0
  17. package/dist/index.d.ts +1 -0
  18. package/dist/index.d.ts.map +1 -1
  19. package/dist/index.js +3903 -2760
  20. package/dist/index.js.map +58 -50
  21. package/dist/openapi/tags.d.ts +15 -3
  22. package/dist/openapi/tags.d.ts.map +1 -1
  23. package/dist/registerRoutes.d.ts +6 -2
  24. package/dist/registerRoutes.d.ts.map +1 -1
  25. package/dist/routes/action-runs.d.ts +21 -31
  26. package/dist/routes/action-runs.d.ts.map +1 -1
  27. package/dist/routes/actions.d.ts +12 -11
  28. package/dist/routes/actions.d.ts.map +1 -1
  29. package/dist/routes/agent-api-gateway.d.ts +2 -2
  30. package/dist/routes/agent-api-gateway.d.ts.map +1 -1
  31. package/dist/routes/agents.d.ts +230 -159
  32. package/dist/routes/agents.d.ts.map +1 -1
  33. package/dist/routes/ai-accounting.d.ts +31 -0
  34. package/dist/routes/ai-accounting.d.ts.map +1 -0
  35. package/dist/routes/auth.d.ts +149 -92
  36. package/dist/routes/auth.d.ts.map +1 -1
  37. package/dist/routes/connector-connection-runs.d.ts +794 -0
  38. package/dist/routes/connector-connection-runs.d.ts.map +1 -0
  39. package/dist/routes/connector-connections.d.ts +219 -0
  40. package/dist/routes/connector-connections.d.ts.map +1 -0
  41. package/dist/routes/connectors.d.ts +10 -4
  42. package/dist/routes/connectors.d.ts.map +1 -1
  43. package/dist/routes/datasets.d.ts +16 -12
  44. package/dist/routes/datasets.d.ts.map +1 -1
  45. package/dist/routes/events.d.ts +9 -9
  46. package/dist/routes/events.d.ts.map +1 -1
  47. package/dist/routes/files.d.ts +19 -18
  48. package/dist/routes/files.d.ts.map +1 -1
  49. package/dist/routes/links.d.ts +11 -11
  50. package/dist/routes/links.d.ts.map +1 -1
  51. package/dist/routes/logs.d.ts +5 -5
  52. package/dist/routes/logs.d.ts.map +1 -1
  53. package/dist/routes/objects.d.ts +37 -37
  54. package/dist/routes/objects.d.ts.map +1 -1
  55. package/dist/routes/ontology.d.ts +50 -50
  56. package/dist/routes/ontology.d.ts.map +1 -1
  57. package/dist/routes/pipelines.d.ts +28 -38
  58. package/dist/routes/pipelines.d.ts.map +1 -1
  59. package/dist/routes/project.d.ts +2 -2
  60. package/dist/routes/project.d.ts.map +1 -1
  61. package/dist/routes/projections.d.ts +145 -85
  62. package/dist/routes/projections.d.ts.map +1 -1
  63. package/dist/routes/rules.d.ts +6 -6
  64. package/dist/routes/rules.d.ts.map +1 -1
  65. package/dist/routes/status.d.ts +2 -2
  66. package/dist/routes/status.d.ts.map +1 -1
  67. package/dist/routes/syncs.d.ts +23 -28
  68. package/dist/routes/syncs.d.ts.map +1 -1
  69. package/dist/routes/telemetry.d.ts +9 -9
  70. package/dist/routes/telemetry.d.ts.map +1 -1
  71. package/dist/routes/webhook-runs.d.ts +9 -9
  72. package/dist/routes/webhook-runs.d.ts.map +1 -1
  73. package/dist/routes/webhooks.d.ts +2 -2
  74. package/dist/routes/webhooks.d.ts.map +1 -1
  75. package/dist/routes/workflows.d.ts +177 -66
  76. package/dist/routes/workflows.d.ts.map +1 -1
  77. package/dist/routes/ws/agents.d.ts +11 -5
  78. package/dist/routes/ws/agents.d.ts.map +1 -1
  79. package/dist/routes/ws/events.d.ts +8 -8
  80. package/dist/routes/ws/events.d.ts.map +1 -1
  81. package/dist/routes/ws/log-subscription-hub.d.ts +6 -3
  82. package/dist/routes/ws/log-subscription-hub.d.ts.map +1 -1
  83. package/dist/routes/ws/logs.d.ts +10 -10
  84. package/dist/routes/ws/logs.d.ts.map +1 -1
  85. package/dist/schemas/actions.d.ts +89 -194
  86. package/dist/schemas/actions.d.ts.map +1 -1
  87. package/dist/schemas/agents.d.ts +791 -471
  88. package/dist/schemas/agents.d.ts.map +1 -1
  89. package/dist/schemas/ai-accounting.d.ts +2393 -0
  90. package/dist/schemas/ai-accounting.d.ts.map +1 -0
  91. package/dist/schemas/ai-usage.d.ts +34 -0
  92. package/dist/schemas/ai-usage.d.ts.map +1 -0
  93. package/dist/schemas/auth.d.ts +102 -102
  94. package/dist/schemas/common.d.ts +37 -0
  95. package/dist/schemas/common.d.ts.map +1 -1
  96. package/dist/schemas/connectors.d.ts +713 -2
  97. package/dist/schemas/connectors.d.ts.map +1 -1
  98. package/dist/schemas/datasets.d.ts +2 -2
  99. package/dist/schemas/event-payloads.d.ts +12 -12
  100. package/dist/schemas/events.d.ts +40 -40
  101. package/dist/schemas/links.d.ts +2 -2
  102. package/dist/schemas/logs.d.ts +22 -22
  103. package/dist/schemas/objects.d.ts +10 -10
  104. package/dist/schemas/ontology.d.ts +118 -118
  105. package/dist/schemas/pipelines.d.ts +89 -184
  106. package/dist/schemas/pipelines.d.ts.map +1 -1
  107. package/dist/schemas/projections.d.ts +1030 -563
  108. package/dist/schemas/projections.d.ts.map +1 -1
  109. package/dist/schemas/rules.d.ts +6 -6
  110. package/dist/schemas/syncs.d.ts +65 -108
  111. package/dist/schemas/syncs.d.ts.map +1 -1
  112. package/dist/schemas/webhook-runs.d.ts +41 -41
  113. package/dist/schemas/webhook-runs.d.ts.map +1 -1
  114. package/dist/schemas/workflows.d.ts +1189 -185
  115. package/dist/schemas/workflows.d.ts.map +1 -1
  116. package/dist/server.d.ts +11 -6
  117. package/dist/server.d.ts.map +1 -1
  118. package/dist/utils/http.d.ts +2 -0
  119. package/dist/utils/http.d.ts.map +1 -1
  120. package/dist/utils/ws.d.ts +4 -3
  121. package/dist/utils/ws.d.ts.map +1 -1
  122. package/package.json +5 -5
  123. package/src/auth/access-token-boundary.ts +4 -4
  124. package/src/auth/application-access.ts +8 -9
  125. package/src/auth/connector-callback-cookie.ts +95 -0
  126. package/src/auth/experience.ts +190 -0
  127. package/src/auth/guard.ts +11 -11
  128. package/src/auth/public-routes.ts +20 -0
  129. package/src/auth/scope.ts +26 -36
  130. package/src/auth/session-cookies.ts +3 -3
  131. package/src/connectors/http.ts +110 -0
  132. package/src/index.ts +1 -0
  133. package/src/openapi/tags.ts +13 -1
  134. package/src/registerRoutes.ts +36 -23
  135. package/src/routes/action-runs.ts +39 -39
  136. package/src/routes/actions.ts +7 -13
  137. package/src/routes/agent-api-gateway.ts +41 -38
  138. package/src/routes/agents.ts +90 -200
  139. package/src/routes/ai-accounting.ts +166 -0
  140. package/src/routes/auth.ts +274 -122
  141. package/src/routes/connector-connection-runs.ts +346 -0
  142. package/src/routes/connector-connections.ts +135 -0
  143. package/src/routes/connectors.ts +24 -12
  144. package/src/routes/datasets.ts +55 -54
  145. package/src/routes/events.ts +5 -5
  146. package/src/routes/files.ts +22 -30
  147. package/src/routes/links.ts +31 -35
  148. package/src/routes/logs.ts +6 -11
  149. package/src/routes/objects.ts +51 -114
  150. package/src/routes/ontology.ts +27 -38
  151. package/src/routes/pipelines.ts +28 -62
  152. package/src/routes/project.ts +3 -3
  153. package/src/routes/projections.ts +44 -42
  154. package/src/routes/rules.ts +11 -25
  155. package/src/routes/status.ts +5 -5
  156. package/src/routes/syncs.ts +19 -45
  157. package/src/routes/telemetry.ts +18 -29
  158. package/src/routes/webhook-runs.ts +5 -5
  159. package/src/routes/webhooks.ts +259 -341
  160. package/src/routes/workflows.ts +242 -248
  161. package/src/routes/ws/agents.ts +196 -66
  162. package/src/routes/ws/events.ts +11 -10
  163. package/src/routes/ws/log-subscription-hub.ts +6 -12
  164. package/src/routes/ws/logs.ts +13 -5
  165. package/src/schemas/actions.ts +58 -25
  166. package/src/schemas/agents.ts +23 -23
  167. package/src/schemas/ai-accounting.ts +182 -0
  168. package/src/schemas/ai-usage.ts +16 -0
  169. package/src/schemas/common.ts +24 -0
  170. package/src/schemas/connectors.ts +164 -0
  171. package/src/schemas/pipelines.ts +23 -15
  172. package/src/schemas/projections.ts +23 -3
  173. package/src/schemas/syncs.ts +13 -8
  174. package/src/schemas/webhook-runs.ts +10 -5
  175. package/src/schemas/workflows.ts +23 -8
  176. package/src/server.ts +75 -26
  177. package/src/utils/http.ts +34 -6
  178. 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,
@@ -147,50 +145,86 @@ function serializeWorkflowAgentExecutionSummary(execution: WorkflowAgentNodeRunR
147
145
  modelId: execution.modelId,
148
146
  finishReason: execution.finishReason,
149
147
  usage: execution.usage,
148
+ cost: execution.cost,
150
149
  startedAt: execution.startedAt ? toIsoString(execution.startedAt) : undefined,
151
150
  completedAt: execution.completedAt ? toIsoString(execution.completedAt) : undefined,
152
151
  }
153
152
  }
154
153
 
155
- function serializeWorkflowAgentExecution(execution: WorkflowAgentNodeRunRecord) {
154
+ function serializeWorkflowAgentExecution(execution: WorkflowAgentNodeRunView) {
156
155
  return {
157
156
  ...serializeWorkflowAgentExecutionSummary(execution),
158
157
  nodeRunId: execution.nodeRunId,
159
158
  prompt: execution.prompt,
160
- executionPrincipal: execution.executionPrincipal
161
- ? serializePrincipal(execution.executionPrincipal)
162
- : undefined,
163
159
  trace: execution.trace,
164
160
  diagnostics: execution.diagnostics,
161
+ failurePhase: workflowAgentFailurePhase(execution),
165
162
  error: execution.error,
166
163
  createdAt: toIsoString(execution.createdAt),
167
164
  }
168
165
  }
169
166
 
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)
167
+ function workflowAgentFailurePhase(
168
+ execution: WorkflowAgentNodeRunView
169
+ ): "agent-loop" | "structured-finalizer" | undefined {
170
+ const details = execution.error?.details
171
+ if (!isRecord(details)) return undefined
172
+
173
+ const phase = details.failurePhase
174
+ return phase === "agent-loop" || phase === "structured-finalizer" ? phase : undefined
182
175
  }
183
176
 
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
- )
177
+ function isRecord(value: unknown): value is Readonly<Record<string, unknown>> {
178
+ return value !== null && typeof value === "object" && !Array.isArray(value)
179
+ }
180
+
181
+ function principalForExecution(sixb: Sixb<readonly OntologySource[]>): Principal {
182
+ return sixb.execution.requestedBy ?? SYSTEM_PRINCIPAL
192
183
  }
193
184
 
185
+ function toWorkflowCancellationFailure(input: {
186
+ readonly message: string
187
+ readonly at: Date
188
+ readonly workflowId: string
189
+ readonly runId: string
190
+ readonly nodeRunId?: string
191
+ }) {
192
+ const details: Readonly<Record<string, string>> = input.nodeRunId
193
+ ? {
194
+ workflowId: input.workflowId,
195
+ workflowRunId: input.runId,
196
+ nodeRunId: input.nodeRunId,
197
+ }
198
+ : { workflowId: input.workflowId, workflowRunId: input.runId }
199
+ return toSixbFailure(createSixbError("runtime.cancelled", input.message, { details }), {
200
+ allowedCodes: WORKFLOW_RUN_FAILURE_CODES,
201
+ at: input.at,
202
+ })
203
+ }
204
+
205
+ function toAgentCancellationFailure(input: {
206
+ readonly message: string
207
+ readonly at: Date
208
+ readonly agentId: string
209
+ readonly workflowId: string
210
+ readonly runId: string
211
+ readonly nodeRunId: string
212
+ }) {
213
+ return toSixbFailure(
214
+ createSixbError("runtime.cancelled", input.message, {
215
+ details: {
216
+ agentId: input.agentId,
217
+ workflowId: input.workflowId,
218
+ workflowRunId: input.runId,
219
+ nodeRunId: input.nodeRunId,
220
+ },
221
+ }),
222
+ {
223
+ allowedCodes: AGENT_RUN_FAILURE_CODES,
224
+ at: input.at,
225
+ }
226
+ )
227
+ }
194
228
  function serializeWorkflowIntervention(intervention: WorkflowInterventionRecord) {
195
229
  return {
196
230
  id: intervention.id,
@@ -217,7 +251,7 @@ function serializeWorkflowIntervention(intervention: WorkflowInterventionRecord)
217
251
  }
218
252
 
219
253
  async function workflowRunFileContentResponse(
220
- sixb: Sixb<readonly OntologySource[]>,
254
+ host: SixbHostView,
221
255
  context: {
222
256
  readonly params: { readonly runId: string }
223
257
  readonly query: unknown
@@ -226,33 +260,29 @@ async function workflowRunFileContentResponse(
226
260
  },
227
261
  options: { readonly head?: boolean } = {}
228
262
  ) {
229
- const { authz, scoped } = requestAuthState(context)
263
+ const sixb = requireRequestSixb(context)
230
264
 
231
- const storage = sixb.storage.workflowRuns
265
+ const storage = host.storage.workflowRuns
232
266
  if (!storage) {
233
267
  return unconfiguredStorageResponse(context.set, "Workflow run storage")
234
268
  }
235
269
 
236
270
  return createContextualFileContentResponse({
237
- blobStorage: sixb.blobStorage,
271
+ blobStorage: host.blobStorage,
238
272
  query: context.query,
239
273
  querySchema: WorkflowRunFileContentQuerySchema,
240
274
  request: context.request,
241
275
  set: context.set,
242
276
  head: options.head,
243
277
  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)
278
+ const run = await sixb.workflows.runs.getById(context.params.runId)
279
+ return run ? serializeWorkflowRunDetail(run) : null
250
280
  },
251
281
  })
252
282
  }
253
283
 
254
284
  async function workflowNodeFileContentResponse(
255
- sixb: Sixb<readonly OntologySource[]>,
285
+ host: SixbHostView,
256
286
  context: {
257
287
  readonly params: { readonly runId: string; readonly nodeKey: string }
258
288
  readonly query: unknown
@@ -261,34 +291,32 @@ async function workflowNodeFileContentResponse(
261
291
  },
262
292
  options: { readonly head?: boolean } = {}
263
293
  ) {
264
- const { authz, scoped } = requestAuthState(context)
294
+ const sixb = requireRequestSixb(context)
265
295
 
266
- const storage = sixb.storage.workflowRuns
296
+ const storage = host.storage.workflowRuns
267
297
  if (!storage) {
268
298
  return unconfiguredStorageResponse(context.set, "Workflow run storage")
269
299
  }
270
300
 
271
301
  return createContextualFileContentResponse({
272
- blobStorage: sixb.blobStorage,
302
+ blobStorage: host.blobStorage,
273
303
  query: context.query,
274
304
  querySchema: WorkflowNodeFileContentQuerySchema,
275
305
  request: context.request,
276
306
  set: context.set,
277
307
  head: options.head,
278
308
  resolveRoot: async () => {
279
- const run = await storage.getById({ projectId: sixb.id, id: context.params.runId })
280
- if (!run || !canAccessWorkflowRun(authz, scoped, run)) {
309
+ const run = await sixb.workflows.runs.getById(context.params.runId)
310
+ if (!run) {
281
311
  return null
282
312
  }
283
313
 
284
- const result = await storage.nodes.list({
285
- projectId: sixb.id,
286
- workflowRunId: run.id,
314
+ const result = await sixb.workflows.runs.listNodes(run.id, {
287
315
  nodeKey: context.params.nodeKey,
288
316
  limit: 1,
289
317
  order: "asc",
290
318
  })
291
- const [node] = result.nodes
319
+ const [node] = result?.nodes ?? []
292
320
  if (!node) {
293
321
  return null
294
322
  }
@@ -299,36 +327,24 @@ async function workflowNodeFileContentResponse(
299
327
  }
300
328
 
301
329
  async function getLatestWorkflowRun(
302
- sixb: Sixb<readonly OntologySource[]>,
330
+ sixb: ReturnType<typeof requireRequestSixb>,
303
331
  workflowId: string
304
332
  ): 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
- })
333
+ const result = await sixb.workflows.runs.listLatest([workflowId])
314
334
 
315
335
  const [latest] = result.runs
316
336
  return latest ? serializeWorkflowRunSummary(latest) : null
317
337
  }
318
338
 
319
339
  async function getLatestWorkflowRuns(
320
- sixb: Sixb<readonly OntologySource[]>,
340
+ sixb: ReturnType<typeof requireRequestSixb>,
321
341
  workflowIds: readonly string[]
322
342
  ): Promise<Map<string, SerializedWorkflowRun>> {
323
- const storage = sixb.storage.workflowRuns
324
- if (!storage || workflowIds.length === 0) {
343
+ if (workflowIds.length === 0) {
325
344
  return new Map()
326
345
  }
327
346
 
328
- const result = await storage.listLatestByWorkflowIds({
329
- projectId: sixb.id,
330
- workflowIds,
331
- })
347
+ const result = await sixb.workflows.runs.listLatest(workflowIds)
332
348
 
333
349
  return new Map(result.runs.map((run) => [run.workflowId, serializeWorkflowRunSummary(run)]))
334
350
  }
@@ -414,14 +430,14 @@ function assertPendingIntervention(intervention: WorkflowInterventionRecord): vo
414
430
  }
415
431
 
416
432
  async function emitWorkflowInterventionSubmitted(
417
- sixb: Sixb<readonly OntologySource[]>,
433
+ host: SixbHostView,
418
434
  intervention: WorkflowInterventionRecord
419
435
  ): Promise<void> {
420
436
  if (!intervention.submittedAt) {
421
437
  throw new Error(`[SixbServer] Submitted intervention '${intervention.id}' has no submittedAt.`)
422
438
  }
423
439
 
424
- await sixb.events.append({
440
+ await host.events.append({
425
441
  events: [
426
442
  {
427
443
  type: "workflow.intervention.submitted",
@@ -439,13 +455,13 @@ async function emitWorkflowInterventionSubmitted(
439
455
  }
440
456
 
441
457
  async function emitWorkflowInterventionCancelled(input: {
442
- readonly sixb: Sixb<readonly OntologySource[]>
458
+ readonly host: SixbHostView
443
459
  readonly workflow: WorkflowDefinition
444
460
  readonly intervention: WorkflowInterventionRecord
445
461
  readonly node: WorkflowNodeRunRecord
446
462
  readonly run: WorkflowRunRecord
447
463
  }): Promise<void> {
448
- const { sixb, workflow, intervention, node, run } = input
464
+ const { host, workflow, intervention, node, run } = input
449
465
  if (!intervention.cancelledAt) {
450
466
  throw new Error(`[SixbServer] Cancelled intervention '${intervention.id}' has no cancelledAt.`)
451
467
  }
@@ -456,7 +472,7 @@ async function emitWorkflowInterventionCancelled(input: {
456
472
  throw new Error(`[SixbServer] Cancelled workflow run '${run.id}' has no finishedAt.`)
457
473
  }
458
474
 
459
- await sixb.events.append({
475
+ await host.events.append({
460
476
  events: [
461
477
  {
462
478
  type: "workflow.intervention.cancelled",
@@ -500,16 +516,16 @@ async function emitWorkflowInterventionCancelled(input: {
500
516
  }
501
517
 
502
518
  async function emitWorkflowRunCancelled(input: {
503
- readonly sixb: Sixb<readonly OntologySource[]>
519
+ readonly host: SixbHostView
504
520
  readonly workflow: WorkflowDefinition
505
521
  readonly run: WorkflowRunRecord
506
522
  readonly node?: WorkflowNodeRunRecord
507
523
  }): Promise<void> {
508
- const { sixb, workflow, run, node } = input
524
+ const { host, workflow, run, node } = input
509
525
  if (!run.finishedAt) {
510
526
  throw new Error(`[SixbServer] Cancelled workflow run '${run.id}' has no finishedAt.`)
511
527
  }
512
- await sixb.events.append({
528
+ await host.events.append({
513
529
  events: [
514
530
  ...(node?.finishedAt
515
531
  ? [
@@ -545,13 +561,13 @@ async function emitWorkflowRunCancelled(input: {
545
561
  })
546
562
  }
547
563
 
548
- export function registerWorkflowRoutes(app: Elysia, sixb: Sixb<readonly OntologySource[]>) {
564
+ export function registerWorkflowRoutes(app: Elysia, host: SixbHostView) {
549
565
  return app
550
566
  .get(
551
567
  "/api/workflows",
552
568
  async (context) => {
553
- const { scoped } = requestAuthState(context)
554
- const workflows = scoped ? scoped.listWorkflows() : sixb.workflows.list()
569
+ const sixb = requireRequestSixb(context)
570
+ const workflows = sixb.workflows.list()
555
571
  const latestRuns = await getLatestWorkflowRuns(
556
572
  sixb,
557
573
  workflows.map((workflow) => workflow.id)
@@ -575,12 +591,10 @@ export function registerWorkflowRoutes(app: Elysia, sixb: Sixb<readonly Ontology
575
591
  "/api/workflows/:workflowId",
576
592
  async (context) => {
577
593
  const { params, set } = context
578
- const { scoped } = requestAuthState(context)
594
+ const sixb = requireRequestSixb(context)
579
595
  // Non-runnable workflows are hidden as 404 (existence-hiding), matching
580
596
  // the object/action read routes.
581
- const workflow = scoped
582
- ? scoped.getWorkflowById(params.workflowId)
583
- : sixb.workflows.getById(params.workflowId)
597
+ const workflow = sixb.workflows.getById(params.workflowId)
584
598
  if (!workflow) {
585
599
  set.status = 404
586
600
  return { error: "Workflow not found" }
@@ -603,25 +617,18 @@ export function registerWorkflowRoutes(app: Elysia, sixb: Sixb<readonly Ontology
603
617
  "/api/workflow-interventions",
604
618
  async (context) => {
605
619
  const { query, set } = context
606
- const { authz, scoped } = requestAuthState(context)
620
+ const sixb = requireRequestSixb(context)
607
621
  try {
608
622
  const parsed = WorkflowInterventionsQuerySchema.parse(query)
609
623
  const limit = parseOptionalInt(parsed.limit)
610
624
  const offset = parseOptionalInt(parsed.offset)
611
- const storage = sixb.storage.workflowInterventions
625
+ const storage = host.storage.workflowInterventions
612
626
  if (!storage) {
613
627
  return unconfiguredStorageResponse(set, "Workflow intervention storage")
614
628
  }
615
629
 
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,
630
+ const result = await sixb.workflows.interventions.list({
623
631
  workflowId: parsed.workflowId,
624
- workflowIds,
625
632
  workflowRunId: parsed.workflowRunId,
626
633
  nodeRunId: parsed.nodeRunId,
627
634
  nodeId: parsed.nodeId,
@@ -662,22 +669,15 @@ export function registerWorkflowRoutes(app: Elysia, sixb: Sixb<readonly Ontology
662
669
  "/api/workflow-interventions/:interventionId",
663
670
  async (context) => {
664
671
  const { params, set } = context
665
- const { authz, scoped } = requestAuthState(context)
672
+ const sixb = requireRequestSixb(context)
666
673
  try {
667
- const storage = sixb.storage.workflowInterventions
674
+ const storage = host.storage.workflowInterventions
668
675
  if (!storage) {
669
676
  return unconfiguredStorageResponse(set, "Workflow intervention storage")
670
677
  }
671
678
 
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
- ) {
679
+ const intervention = await sixb.workflows.interventions.getById(params.interventionId)
680
+ if (!intervention) {
681
681
  set.status = 404
682
682
  return { error: "Workflow intervention not found" }
683
683
  }
@@ -706,22 +706,15 @@ export function registerWorkflowRoutes(app: Elysia, sixb: Sixb<readonly Ontology
706
706
  "/api/workflow-interventions/:interventionId/submit",
707
707
  async (context) => {
708
708
  const { params, body, set } = context
709
- const { authz, scoped } = requestAuthState(context)
709
+ const sixb = requireRequestSixb(context)
710
710
  try {
711
- const storage = sixb.storage.workflowInterventions
711
+ const storage = host.storage.workflowInterventions
712
712
  if (!storage) {
713
713
  return unconfiguredStorageResponse(set, "Workflow intervention storage")
714
714
  }
715
715
 
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
- ) {
716
+ const intervention = await sixb.workflows.interventions.getById(params.interventionId)
717
+ if (!intervention) {
725
718
  set.status = 404
726
719
  return { error: "Workflow intervention not found" }
727
720
  }
@@ -739,31 +732,30 @@ export function registerWorkflowRoutes(app: Elysia, sixb: Sixb<readonly Ontology
739
732
  workflowId: workflow.id,
740
733
  intervention: node.intervention,
741
734
  value: parsedBody.response,
742
- valueTypesById: sixb.ontology.getValueTypesById(),
735
+ valueTypesById: host.definitions.ontology.getValueTypesById(),
743
736
  })
744
737
 
745
738
  const submitted = await storage.submit({
746
- projectId: sixb.id,
739
+ projectId: host.id,
747
740
  id: intervention.id,
748
741
  response,
749
- submittedBy: serializePrincipal(authz?.principal ?? SYSTEM_PRINCIPAL),
742
+ submittedBy: serializePrincipal(principalForExecution(sixb)),
750
743
  })
751
744
 
752
- const [job] = await sixb.queues.workflows.enqueue({
753
- projectId: sixb.id,
745
+ const [job] = await host.queues.workflows.enqueue({
746
+ projectId: host.id,
754
747
  jobs: [
755
748
  {
756
749
  type: "workflow.run.resume.requested",
757
750
  payload: {
758
- workflowId: submitted.workflowId,
759
751
  runId: submitted.workflowRunId,
760
- resume: { kind: "intervention", interventionId: submitted.id },
752
+ nodeRunId: submitted.nodeRunId,
761
753
  },
762
754
  },
763
755
  ],
764
756
  })
765
757
 
766
- await emitWorkflowInterventionSubmitted(sixb, submitted)
758
+ await emitWorkflowInterventionSubmitted(host, submitted)
767
759
 
768
760
  set.status = 202
769
761
  return SubmitWorkflowInterventionResponseSchema.parse({
@@ -795,27 +787,20 @@ export function registerWorkflowRoutes(app: Elysia, sixb: Sixb<readonly Ontology
795
787
  "/api/workflow-interventions/:interventionId/cancel",
796
788
  async (context) => {
797
789
  const { params, body, set } = context
798
- const { authz, scoped } = requestAuthState(context)
790
+ const sixb = requireRequestSixb(context)
799
791
  try {
800
- const interventionStorage = sixb.storage.workflowInterventions
792
+ const interventionStorage = host.storage.workflowInterventions
801
793
  if (!interventionStorage) {
802
794
  return unconfiguredStorageResponse(set, "Workflow intervention storage")
803
795
  }
804
796
 
805
- const runStorage = sixb.storage.workflowRuns
797
+ const runStorage = host.storage.workflowRuns
806
798
  if (!runStorage) {
807
799
  return unconfiguredStorageResponse(set, "Workflow run storage")
808
800
  }
809
801
 
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
- ) {
802
+ const intervention = await sixb.workflows.interventions.getById(params.interventionId)
803
+ if (!intervention) {
819
804
  set.status = 404
820
805
  return { error: "Workflow intervention not found" }
821
806
  }
@@ -828,10 +813,7 @@ export function registerWorkflowRoutes(app: Elysia, sixb: Sixb<readonly Ontology
828
813
  }
829
814
  requireRegisteredInterventionNode(workflow, intervention)
830
815
 
831
- const run = await runStorage.getById({
832
- projectId: sixb.id,
833
- id: intervention.workflowRunId,
834
- })
816
+ const run = await sixb.workflows.runs.getById(intervention.workflowRunId)
835
817
  if (!run) {
836
818
  set.status = 404
837
819
  return { error: "Workflow run not found" }
@@ -842,7 +824,7 @@ export function registerWorkflowRoutes(app: Elysia, sixb: Sixb<readonly Ontology
842
824
  }
843
825
 
844
826
  const nodeRun = await runStorage.nodes.getById({
845
- projectId: sixb.id,
827
+ projectId: host.id,
846
828
  id: intervention.nodeRunId,
847
829
  })
848
830
  if (!nodeRun) {
@@ -856,37 +838,49 @@ export function registerWorkflowRoutes(app: Elysia, sixb: Sixb<readonly Ontology
856
838
 
857
839
  CancelWorkflowInterventionBodySchema.parse(body ?? {})
858
840
  const cancelledAt = new Date()
859
- const { cancelled, cancelledNode, cancelledRun } = await sixb.storage.transaction(
841
+ const cancellationMessage = "Workflow intervention cancelled."
842
+ const { cancelled, cancelledNode, cancelledRun } = await host.storage.transaction(
860
843
  async (tx) => {
861
844
  if (!tx.workflowInterventions || !tx.workflowRuns) {
862
845
  throw new Error("[SixbServer] Workflow storage disappeared during cancellation.")
863
846
  }
864
847
  const cancelled = await tx.workflowInterventions.cancel({
865
- projectId: sixb.id,
848
+ projectId: host.id,
866
849
  id: intervention.id,
867
850
  cancelledAt,
868
- cancelledBy: serializePrincipal(authz?.principal ?? SYSTEM_PRINCIPAL),
851
+ cancelledBy: serializePrincipal(principalForExecution(sixb)),
869
852
  })
870
853
  const cancelledNode = await tx.workflowRuns.nodes.finish({
871
- projectId: sixb.id,
854
+ projectId: host.id,
872
855
  id: intervention.nodeRunId,
873
856
  status: "cancelled",
874
857
  finishedAt: cancelledAt,
875
- error: "Workflow intervention cancelled.",
858
+ error: toWorkflowCancellationFailure({
859
+ message: cancellationMessage,
860
+ at: cancelledAt,
861
+ workflowId: intervention.workflowId,
862
+ runId: intervention.workflowRunId,
863
+ nodeRunId: intervention.nodeRunId,
864
+ }),
876
865
  })
877
866
  const cancelledRun = await tx.workflowRuns.finish({
878
- projectId: sixb.id,
867
+ projectId: host.id,
879
868
  id: intervention.workflowRunId,
880
869
  status: "cancelled",
881
870
  finishedAt: cancelledAt,
882
- error: "Workflow intervention cancelled.",
871
+ error: toWorkflowCancellationFailure({
872
+ message: cancellationMessage,
873
+ at: cancelledAt,
874
+ workflowId: intervention.workflowId,
875
+ runId: intervention.workflowRunId,
876
+ }),
883
877
  })
884
878
  return { cancelled, cancelledNode, cancelledRun }
885
879
  }
886
880
  )
887
881
 
888
882
  await emitWorkflowInterventionCancelled({
889
- sixb,
883
+ host,
890
884
  workflow,
891
885
  intervention: cancelled,
892
886
  node: cancelledNode,
@@ -921,25 +915,18 @@ export function registerWorkflowRoutes(app: Elysia, sixb: Sixb<readonly Ontology
921
915
  "/api/workflow-runs",
922
916
  async (context) => {
923
917
  const { query, set } = context
924
- const { authz, scoped } = requestAuthState(context)
918
+ const sixb = requireRequestSixb(context)
925
919
  try {
926
920
  const parsed = WorkflowRunsQuerySchema.parse(query)
927
921
  const limit = parseOptionalInt(parsed.limit)
928
922
  const offset = parseOptionalInt(parsed.offset)
929
- const storage = sixb.storage.workflowRuns
923
+ const storage = host.storage.workflowRuns
930
924
  if (!storage) {
931
925
  return unconfiguredStorageResponse(set, "Workflow run storage")
932
926
  }
933
927
 
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,
928
+ const result = await sixb.workflows.runs.list({
941
929
  workflowId: parsed.workflowId,
942
- workflowIds,
943
930
  statuses: parsed.status ? [parsed.status] : undefined,
944
931
  startedAfter: parseDate(parsed.startedAfter),
945
932
  startedBefore: parseDate(parsed.startedBefore),
@@ -976,15 +963,16 @@ export function registerWorkflowRoutes(app: Elysia, sixb: Sixb<readonly Ontology
976
963
  "/api/workflow-runs/:runId",
977
964
  async (context) => {
978
965
  const { params, set } = context
979
- const { agentExecution, authz, scoped } = requestAuthState(context)
966
+ const { agentExecution } = requestAuthState(context)
967
+ const sixb = requireRequestSixb(context)
980
968
  try {
981
- const storage = sixb.storage.workflowRuns
969
+ const storage = host.storage.workflowRuns
982
970
  if (!storage) {
983
971
  return unconfiguredStorageResponse(set, "Workflow run storage")
984
972
  }
985
973
 
986
- const run = await storage.getById({ projectId: sixb.id, id: params.runId })
987
- if (!run || !canAccessWorkflowRun(authz, scoped, run)) {
974
+ const run = await sixb.workflows.runs.getById(params.runId)
975
+ if (!run) {
988
976
  set.status = 404
989
977
  return { error: "Workflow run not found" }
990
978
  }
@@ -995,17 +983,16 @@ export function registerWorkflowRoutes(app: Elysia, sixb: Sixb<readonly Ontology
995
983
  return WorkflowRunDetailResponseSchema.parse({ run: serializedRun, nodes: [] })
996
984
  }
997
985
 
998
- const nodes = await storage.nodes.list({
999
- projectId: sixb.id,
1000
- workflowRunId: run.id,
1001
- order: "asc",
1002
- })
986
+ const nodes = await sixb.workflows.runs.listNodes(run.id, { order: "asc" })
987
+ if (!nodes) {
988
+ throw new Error(
989
+ `[SixbServer] Authorized workflow run '${run.id}' lost its execution view.`
990
+ )
991
+ }
1003
992
 
1004
993
  return WorkflowRunDetailResponseSchema.parse({
1005
994
  run: serializedRun,
1006
- nodes: await Promise.all(
1007
- nodes.nodes.map((node) => serializeWorkflowNodeWithExecution(storage, node))
1008
- ),
995
+ nodes: nodes.nodes.map(serializeWorkflowNodeRun),
1009
996
  })
1010
997
  } catch (error) {
1011
998
  return handleRouteError(error, set)
@@ -1031,33 +1018,24 @@ export function registerWorkflowRoutes(app: Elysia, sixb: Sixb<readonly Ontology
1031
1018
  "/api/workflow-runs/:runId/nodes/:nodeKey/agent-execution",
1032
1019
  async (context) => {
1033
1020
  const { params, set } = context
1034
- const { authz, scoped } = requestAuthState(context)
1021
+ const sixb = requireRequestSixb(context)
1035
1022
  try {
1036
- const storage = sixb.storage.workflowRuns
1023
+ const storage = host.storage.workflowRuns
1037
1024
  if (!storage) {
1038
1025
  return unconfiguredStorageResponse(set, "Workflow run storage")
1039
1026
  }
1040
- const run = await storage.getById({ projectId: sixb.id, id: params.runId })
1041
- if (!run || !canAccessWorkflowRun(authz, scoped, run)) {
1027
+ const run = await sixb.workflows.runs.getById(params.runId)
1028
+ if (!run) {
1042
1029
  set.status = 404
1043
1030
  return { error: "Workflow agent execution not found" }
1044
1031
  }
1045
- const listed = await storage.nodes.list({
1046
- projectId: sixb.id,
1047
- workflowRunId: run.id,
1032
+ const listed = await sixb.workflows.runs.listNodes(run.id, {
1048
1033
  nodeKey: params.nodeKey,
1049
1034
  limit: 1,
1050
1035
  order: "asc",
1051
1036
  })
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
- })
1037
+ const node = listed?.nodes[0]
1038
+ const execution = node?.nodeType === "agent" ? node.agentExecution : undefined
1061
1039
  if (!execution) {
1062
1040
  set.status = 404
1063
1041
  return { error: "Workflow agent execution not found" }
@@ -1087,15 +1065,15 @@ export function registerWorkflowRoutes(app: Elysia, sixb: Sixb<readonly Ontology
1087
1065
  "/api/workflow-runs/:runId/cancel",
1088
1066
  async (context) => {
1089
1067
  const { params, body, set } = context
1090
- const { authz, scoped } = requestAuthState(context)
1068
+ const sixb = requireRequestSixb(context)
1091
1069
  try {
1092
1070
  CancelWorkflowRunBodySchema.parse(body ?? {})
1093
- const storage = sixb.storage.workflowRuns
1071
+ const storage = host.storage.workflowRuns
1094
1072
  if (!storage) {
1095
1073
  return unconfiguredStorageResponse(set, "Workflow run storage")
1096
1074
  }
1097
- const existing = await storage.getById({ projectId: sixb.id, id: params.runId })
1098
- if (!existing || !canAccessWorkflowRun(authz, scoped, existing)) {
1075
+ const existing = await sixb.workflows.runs.getById(params.runId)
1076
+ if (!existing) {
1099
1077
  set.status = 404
1100
1078
  return { error: "Workflow run not found" }
1101
1079
  }
@@ -1104,32 +1082,36 @@ export function registerWorkflowRoutes(app: Elysia, sixb: Sixb<readonly Ontology
1104
1082
  set.status = 404
1105
1083
  return { error: "Workflow not found" }
1106
1084
  }
1107
- const listed = await storage.nodes.list({
1108
- projectId: sixb.id,
1109
- workflowRunId: existing.id,
1110
- order: "asc",
1111
- })
1112
1085
  if (existing.status === "cancelled") {
1086
+ const nodes = await sixb.workflows.runs.listNodes(existing.id, { order: "asc" })
1087
+ if (!nodes) {
1088
+ throw new Error(
1089
+ `[SixbServer] Authorized workflow run '${existing.id}' lost its execution view.`
1090
+ )
1091
+ }
1113
1092
  return CancelWorkflowRunResponseSchema.parse({
1114
1093
  run: serializeWorkflowRunDetail(existing),
1115
- nodes: await Promise.all(
1116
- listed.nodes.map((node) => serializeWorkflowNodeWithExecution(storage, node))
1117
- ),
1094
+ nodes: nodes.nodes.map(serializeWorkflowNodeRun),
1118
1095
  })
1119
1096
  }
1120
1097
  if (existing.status === "succeeded" || existing.status === "failed") {
1121
1098
  set.status = 400
1122
1099
  return { error: `Workflow run is already ${existing.status}` }
1123
1100
  }
1101
+ const listed = await storage.nodes.list({
1102
+ projectId: host.id,
1103
+ workflowRunId: existing.id,
1104
+ order: "asc",
1105
+ })
1124
1106
 
1125
1107
  const cancelledAt = new Date()
1126
- const cancellationError = "Workflow run cancelled."
1127
- const result = await sixb.storage.transaction(async (tx) => {
1108
+ const cancellationMessage = "Workflow run cancelled."
1109
+ const result = await host.storage.transaction(async (tx) => {
1128
1110
  const runs = tx.workflowRuns
1129
1111
  if (!runs) {
1130
1112
  throw new Error("[SixbServer] Workflow storage disappeared during cancellation.")
1131
1113
  }
1132
- const run = await runs.getById({ projectId: sixb.id, id: existing.id })
1114
+ const run = await runs.getById({ projectId: host.id, id: existing.id })
1133
1115
  if (!run || !["queued", "running", "waiting"].includes(run.status)) {
1134
1116
  throw new Error(`[SixbServer] Workflow run '${existing.id}' is no longer active.`)
1135
1117
  }
@@ -1139,21 +1121,28 @@ export function registerWorkflowRoutes(app: Elysia, sixb: Sixb<readonly Ontology
1139
1121
  let cancelledNode: WorkflowNodeRunRecord | undefined
1140
1122
  if (active?.nodeType === "agent") {
1141
1123
  const execution = await runs.agentNodes.getByNodeRunId({
1142
- projectId: sixb.id,
1124
+ projectId: host.id,
1143
1125
  nodeRunId: active.id,
1144
1126
  })
1145
1127
  if (execution?.status === "queued" || execution?.status === "running") {
1146
1128
  await runs.agentNodes.cancel({
1147
- projectId: sixb.id,
1129
+ projectId: host.id,
1148
1130
  nodeRunId: active.id,
1149
1131
  completedAt: cancelledAt,
1150
- error: cancellationError,
1132
+ error: toAgentCancellationFailure({
1133
+ message: cancellationMessage,
1134
+ at: cancelledAt,
1135
+ agentId: execution.agentId,
1136
+ workflowId: run.workflowId,
1137
+ runId: run.id,
1138
+ nodeRunId: active.id,
1139
+ }),
1151
1140
  })
1152
1141
  }
1153
1142
  }
1154
1143
  if (active?.nodeType === "intervention" && tx.workflowInterventions) {
1155
1144
  const pending = await tx.workflowInterventions.list({
1156
- projectId: sixb.id,
1145
+ projectId: host.id,
1157
1146
  workflowRunId: run.id,
1158
1147
  nodeRunId: active.id,
1159
1148
  statuses: ["pending"],
@@ -1162,38 +1151,49 @@ export function registerWorkflowRoutes(app: Elysia, sixb: Sixb<readonly Ontology
1162
1151
  const intervention = pending.interventions[0]
1163
1152
  if (intervention) {
1164
1153
  await tx.workflowInterventions.cancel({
1165
- projectId: sixb.id,
1154
+ projectId: host.id,
1166
1155
  id: intervention.id,
1167
1156
  cancelledAt,
1168
- cancelledBy: serializePrincipal(authz?.principal ?? SYSTEM_PRINCIPAL),
1157
+ cancelledBy: serializePrincipal(principalForExecution(sixb)),
1169
1158
  })
1170
1159
  }
1171
1160
  }
1172
1161
  if (active) {
1173
1162
  cancelledNode = await runs.nodes.finish({
1174
- projectId: sixb.id,
1163
+ projectId: host.id,
1175
1164
  id: active.id,
1176
1165
  status: "cancelled",
1177
1166
  finishedAt: cancelledAt,
1178
- error: cancellationError,
1167
+ error: toWorkflowCancellationFailure({
1168
+ message: cancellationMessage,
1169
+ at: cancelledAt,
1170
+ workflowId: run.workflowId,
1171
+ runId: run.id,
1172
+ nodeRunId: active.id,
1173
+ }),
1179
1174
  executionToken: run.execution?.token,
1180
1175
  })
1181
1176
  }
1182
1177
  const cancelledRun = await runs.finish({
1183
- projectId: sixb.id,
1178
+ projectId: host.id,
1184
1179
  id: run.id,
1185
1180
  status: "cancelled",
1186
1181
  finishedAt: cancelledAt,
1187
- error: cancellationError,
1182
+ error: toWorkflowCancellationFailure({
1183
+ message: cancellationMessage,
1184
+ at: cancelledAt,
1185
+ workflowId: run.workflowId,
1186
+ runId: run.id,
1187
+ }),
1188
1188
  executionToken: run.execution?.token,
1189
1189
  })
1190
1190
  return { run: cancelledRun, node: cancelledNode }
1191
1191
  })
1192
1192
 
1193
- await emitWorkflowRunCancelled({ sixb, workflow, ...result })
1193
+ await emitWorkflowRunCancelled({ host, workflow, ...result })
1194
1194
  if (result.node?.nodeType === "agent") {
1195
- await publishAgentRunCancel(sixb.broker, {
1196
- projectId: sixb.id,
1195
+ await publishAgentRunCancel(host.broker, {
1196
+ projectId: host.id,
1197
1197
  runId: result.node.id,
1198
1198
  }).catch((error) => {
1199
1199
  console.error(
@@ -1202,16 +1202,18 @@ export function registerWorkflowRoutes(app: Elysia, sixb: Sixb<readonly Ontology
1202
1202
  )
1203
1203
  })
1204
1204
  }
1205
- const nodes = await storage.nodes.list({
1206
- projectId: sixb.id,
1207
- workflowRunId: existing.id,
1208
- order: "asc",
1209
- })
1205
+ const nodes = await sixb.workflows.runs.listNodes(existing.id, { order: "asc" })
1206
+ if (!nodes) {
1207
+ throw new Error(
1208
+ `[SixbServer] Authorized workflow run '${existing.id}' lost its execution view.`
1209
+ )
1210
+ }
1210
1211
  return CancelWorkflowRunResponseSchema.parse({
1211
- run: serializeWorkflowRunDetail(result.run),
1212
- nodes: await Promise.all(
1213
- nodes.nodes.map((node) => serializeWorkflowNodeWithExecution(storage, node))
1214
- ),
1212
+ run: serializeWorkflowRunDetail({
1213
+ ...result.run,
1214
+ ...(existing.requestedBy === undefined ? {} : { requestedBy: existing.requestedBy }),
1215
+ }),
1216
+ nodes: nodes.nodes.map(serializeWorkflowNodeRun),
1215
1217
  })
1216
1218
  } catch (error) {
1217
1219
  return handleRouteError(error, set)
@@ -1236,7 +1238,7 @@ export function registerWorkflowRoutes(app: Elysia, sixb: Sixb<readonly Ontology
1236
1238
  )
1237
1239
  .get(
1238
1240
  "/api/workflow-runs/:runId/files/content",
1239
- (context) => workflowRunFileContentResponse(sixb, context),
1241
+ (context) => workflowRunFileContentResponse(host, context),
1240
1242
  {
1241
1243
  params: WorkflowRunParamsSchema,
1242
1244
  query: WorkflowRunFileContentQuerySchema,
@@ -1252,7 +1254,7 @@ export function registerWorkflowRoutes(app: Elysia, sixb: Sixb<readonly Ontology
1252
1254
  )
1253
1255
  .head(
1254
1256
  "/api/workflow-runs/:runId/files/content",
1255
- (context) => workflowRunFileContentResponse(sixb, context, { head: true }),
1257
+ (context) => workflowRunFileContentResponse(host, context, { head: true }),
1256
1258
  {
1257
1259
  params: WorkflowRunParamsSchema,
1258
1260
  query: WorkflowRunFileContentQuerySchema,
@@ -1268,7 +1270,7 @@ export function registerWorkflowRoutes(app: Elysia, sixb: Sixb<readonly Ontology
1268
1270
  )
1269
1271
  .get(
1270
1272
  "/api/workflow-runs/:runId/nodes/:nodeKey/files/content",
1271
- (context) => workflowNodeFileContentResponse(sixb, context),
1273
+ (context) => workflowNodeFileContentResponse(host, context),
1272
1274
  {
1273
1275
  params: WorkflowNodeFileContentParamsSchema,
1274
1276
  query: WorkflowNodeFileContentQuerySchema,
@@ -1284,7 +1286,7 @@ export function registerWorkflowRoutes(app: Elysia, sixb: Sixb<readonly Ontology
1284
1286
  )
1285
1287
  .head(
1286
1288
  "/api/workflow-runs/:runId/nodes/:nodeKey/files/content",
1287
- (context) => workflowNodeFileContentResponse(sixb, context, { head: true }),
1289
+ (context) => workflowNodeFileContentResponse(host, context, { head: true }),
1288
1290
  {
1289
1291
  params: WorkflowNodeFileContentParamsSchema,
1290
1292
  query: WorkflowNodeFileContentQuerySchema,
@@ -1302,27 +1304,19 @@ export function registerWorkflowRoutes(app: Elysia, sixb: Sixb<readonly Ontology
1302
1304
  "/api/workflows/:workflowId/runs",
1303
1305
  async (context) => {
1304
1306
  const { params, body, set } = context
1305
- const { scoped } = requestAuthState(context)
1307
+ const sixb = requireRequestSixb(context)
1306
1308
  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) {
1309
+ if (!host.storage.workflowRuns) {
1314
1310
  return unconfiguredStorageResponse(set, "Workflow run storage")
1315
1311
  }
1316
1312
 
1317
1313
  const parsedBody = RequestWorkflowRunBodySchema.parse(body)
1318
1314
  const input = {
1319
- workflowId: workflow.id,
1315
+ workflowId: params.workflowId,
1320
1316
  input: parsedBody.input ?? {},
1321
1317
  source: { type: "manual" } as const,
1322
1318
  }
1323
- const result = scoped
1324
- ? await scoped.requestWorkflowRun(input)
1325
- : await sixb.requestWorkflowRun(input)
1319
+ const result = await sixb.workflows.requestById(input)
1326
1320
 
1327
1321
  set.status = 202
1328
1322
  return RequestWorkflowRunResponseSchema.parse({