@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,13 +1,22 @@
1
- import type { AuthorizationContext, OntologySource, Sixb } from "@sixb/core"
2
- import { agentRunStreamDefinition, agentRunStreamId } from "@sixb/core/agents/streams"
3
- import type { BrokerRecord } from "@sixb/core/broker"
1
+ import type { OntologySource } from "@sixb/core"
2
+ import {
3
+ AGENT_ACTIVITY_STREAM_ID,
4
+ type AgentRunActivityEvent,
5
+ agentActivityStreamDefinition,
6
+ agentRunStreamDefinition,
7
+ agentRunStreamId,
8
+ isAgentRunActivityEvent,
9
+ } from "@sixb/core/agents/streams"
10
+ import { BrokerCursorExpiredError, type BrokerRecord } from "@sixb/core/broker"
11
+ import type { Sixb } from "@sixb/core/internal/request-execution"
4
12
  import type { Elysia } from "elysia"
5
13
  import { z } from "zod"
6
14
  import type { SixbServer } from "../../server"
7
- import { decodeWsMessage, safeSend, wsAuthz, wsStateKey } from "../../utils/ws"
15
+ import { decodeWsMessage, safeSend, wsRequestSixb, wsStateKey } from "../../utils/ws"
8
16
  import { serializeAgentRun } from "../agents"
9
17
 
10
18
  interface AgentStreamSubscriptionState {
19
+ channel: "run" | "activity" | null
11
20
  runId: string | null
12
21
  unsubscribe: (() => void) | null
13
22
  }
@@ -18,6 +27,10 @@ const SubscribeSchema = z.object({
18
27
  afterCursor: z.string().min(1).optional(),
19
28
  })
20
29
 
30
+ const SubscribeActivitySchema = z.object({
31
+ type: z.literal("subscribe.activity"),
32
+ })
33
+
21
34
  const ReplaySchema = z.object({
22
35
  type: z.literal("replay"),
23
36
  runId: z.string().min(1),
@@ -30,7 +43,12 @@ const UnsubscribeSchema = z.object({
30
43
  runId: z.string().min(1).optional(),
31
44
  })
32
45
 
33
- const AgentStreamMessageSchema = z.union([SubscribeSchema, ReplaySchema, UnsubscribeSchema])
46
+ const AgentStreamMessageSchema = z.union([
47
+ SubscribeSchema,
48
+ SubscribeActivitySchema,
49
+ ReplaySchema,
50
+ UnsubscribeSchema,
51
+ ])
34
52
 
35
53
  type SubscribeMessage = z.infer<typeof SubscribeSchema>
36
54
  type ReplayMessage = z.infer<typeof ReplaySchema>
@@ -62,42 +80,29 @@ export function parseAgentStreamMessage(payload: unknown):
62
80
 
63
81
  export async function canAccessAgentRunStream(
64
82
  sixb: Sixb<readonly OntologySource[]>,
65
- input: {
66
- readonly runId: string
67
- readonly authz: AuthorizationContext | null
68
- }
83
+ runId: string
69
84
  ): Promise<{ ok: true } | { ok: false; message: string }> {
70
- const storage = sixb.storage.agents
71
- if (!storage) {
72
- return { ok: false, message: "Agent storage is not configured." }
73
- }
74
-
75
- const run = await storage.runs.getById({ projectId: sixb.id, id: input.runId })
85
+ const run = await sixb.agents.runs.getById(runId)
76
86
  if (!run) {
77
87
  return { ok: false, message: "Agent run not found." }
78
88
  }
79
-
80
- // Auth-disabled development runtimes are privileged, matching the existing HTTP agent routes.
81
- if (!input.authz) {
82
- return { ok: true }
83
- }
84
-
85
- // Reuse the one owner + `run:agent` rule instead of re-implementing it here: the scoped surface
86
- // returns null for a thread the caller may not read (absent, not owner, or ungranted).
87
- const thread = await sixb.as(input.authz).getThread(run.threadId)
88
- if (!thread) {
89
- return { ok: false, message: "Agent run not found." }
90
- }
91
-
92
89
  return { ok: true }
93
90
  }
94
91
 
92
+ export async function canAccessAgentThreadActivity(
93
+ sixb: Sixb<readonly OntologySource[]>,
94
+ event: AgentRunActivityEvent
95
+ ): Promise<boolean> {
96
+ const thread = await sixb.agents.threads.getById(event.threadId)
97
+ return thread?.agentId === event.agentId
98
+ }
99
+
95
100
  export function registerAgentStreamRoutes(app: Elysia, server: SixbServer) {
96
101
  const states = new WeakMap<object, AgentStreamSubscriptionState>()
97
102
 
98
103
  return app.ws("/ws/agents", {
99
104
  open(ws) {
100
- states.set(wsStateKey(ws), { runId: null, unsubscribe: null })
105
+ states.set(wsStateKey(ws), { channel: null, runId: null, unsubscribe: null })
101
106
  safeSend(ws, { type: "connected", channel: "agents" })
102
107
  },
103
108
 
@@ -119,6 +124,11 @@ export function registerAgentStreamRoutes(app: Elysia, server: SixbServer) {
119
124
  return
120
125
  }
121
126
 
127
+ if (parsed.data.type === "subscribe.activity") {
128
+ await subscribeAgentActivity(states, server, ws)
129
+ return
130
+ }
131
+
122
132
  await subscribeAgentStream(states, server, ws, parsed.data)
123
133
  },
124
134
 
@@ -135,11 +145,13 @@ async function subscribeAgentStream(
135
145
  ws: { send: (message: string) => void },
136
146
  message: SubscribeMessage
137
147
  ): Promise<void> {
138
- const sixb = server.getSixb()
139
- const access = await canAccessAgentRunStream(sixb, {
140
- runId: message.runId,
141
- authz: wsAuthz(ws),
142
- })
148
+ const host = server.getHost()
149
+ const sixb = wsRequestSixb(ws)
150
+ if (!sixb) {
151
+ safeSend(ws, { type: "error", message: "Execution scope is not available." })
152
+ return
153
+ }
154
+ const access = await canAccessAgentRunStream(sixb, message.runId)
143
155
  if (!access.ok) {
144
156
  safeSend(ws, { type: "error", message: access.message })
145
157
  return
@@ -147,8 +159,8 @@ async function subscribeAgentStream(
147
159
 
148
160
  const streamId = agentRunStreamId(message.runId)
149
161
  try {
150
- await sixb.broker.ensureStream({
151
- projectId: sixb.id,
162
+ await host.broker.ensureStream({
163
+ projectId: host.id,
152
164
  stream: agentRunStreamDefinition(message.runId),
153
165
  })
154
166
  } catch (error) {
@@ -156,41 +168,73 @@ async function subscribeAgentStream(
156
168
  return
157
169
  }
158
170
 
159
- const state = states.get(wsStateKey(ws)) ?? { runId: null, unsubscribe: null }
171
+ const state = states.get(wsStateKey(ws)) ?? {
172
+ channel: null,
173
+ runId: null,
174
+ unsubscribe: null,
175
+ }
160
176
  stopSubscription(state)
161
177
  states.set(wsStateKey(ws), state)
162
178
 
163
179
  const buffered: BrokerRecord[] = []
164
180
  let live = false
181
+ const receive = (records: readonly BrokerRecord[]): void => {
182
+ if (!live) {
183
+ buffered.push(...records)
184
+ return
185
+ }
186
+ sendRecords(ws, records)
187
+ }
188
+
189
+ // Set when the requested cursor had already been trimmed, so the client learns its resume point
190
+ // is gone instead of silently receiving a stream that starts after the gap.
191
+ let resumedFromEarliest = false
165
192
  try {
166
- state.unsubscribe = await sixb.broker.subscribe(
193
+ state.unsubscribe = await host.broker.subscribe(
167
194
  {
168
- projectId: sixb.id,
195
+ projectId: host.id,
169
196
  streamId,
170
197
  ...(message.afterCursor === undefined
171
198
  ? { from: "earliest" as const }
172
199
  : { afterCursor: message.afterCursor }),
173
200
  },
174
- (records) => {
175
- if (!live) {
176
- buffered.push(...records)
177
- return
178
- }
179
- sendRecords(ws, records)
180
- }
201
+ receive
181
202
  )
203
+ state.channel = "run"
182
204
  state.runId = message.runId
183
205
  } catch (error) {
184
- state.unsubscribe = null
185
- state.runId = null
186
- safeSend(ws, { type: "error", message: errorMessage(error) })
187
- return
206
+ // Retention can outrun a paused tab: an agent run stream keeps 5000 records and a streamed
207
+ // turn writes one per chunk. Replaying everything still retained beats leaving the socket
208
+ // open with a cursor the broker will reject forever.
209
+ if (error instanceof BrokerCursorExpiredError) {
210
+ try {
211
+ state.unsubscribe = await host.broker.subscribe(
212
+ { projectId: host.id, streamId, from: "earliest" },
213
+ receive
214
+ )
215
+ state.channel = "run"
216
+ state.runId = message.runId
217
+ resumedFromEarliest = true
218
+ } catch (retryError) {
219
+ state.unsubscribe = null
220
+ state.channel = null
221
+ state.runId = null
222
+ safeSend(ws, { type: "error", message: errorMessage(retryError) })
223
+ return
224
+ }
225
+ } else {
226
+ state.unsubscribe = null
227
+ state.channel = null
228
+ state.runId = null
229
+ safeSend(ws, { type: "error", message: errorMessage(error) })
230
+ return
231
+ }
188
232
  }
189
233
 
190
234
  safeSend(ws, {
191
235
  type: "subscribed",
192
236
  runId: message.runId,
193
- afterCursor: message.afterCursor ?? null,
237
+ afterCursor: resumedFromEarliest ? null : (message.afterCursor ?? null),
194
238
  })
195
239
  sendRecords(ws, buffered.splice(0))
196
240
  if (!(await sendRunSnapshot(sixb, ws, message.runId))) {
@@ -201,16 +245,95 @@ async function subscribeAgentStream(
201
245
  sendRecords(ws, buffered.splice(0))
202
246
  }
203
247
 
248
+ async function subscribeAgentActivity(
249
+ states: WeakMap<object, AgentStreamSubscriptionState>,
250
+ server: SixbServer,
251
+ ws: { send: (message: string) => void }
252
+ ): Promise<void> {
253
+ const host = server.getHost()
254
+ const sixb = wsRequestSixb(ws)
255
+ if (!sixb) {
256
+ safeSend(ws, { type: "error", message: "Execution scope is not available." })
257
+ return
258
+ }
259
+
260
+ try {
261
+ await host.broker.ensureStream({
262
+ projectId: host.id,
263
+ stream: agentActivityStreamDefinition(),
264
+ })
265
+ } catch (error) {
266
+ safeSend(ws, { type: "error", message: errorMessage(error) })
267
+ return
268
+ }
269
+
270
+ const state = states.get(wsStateKey(ws)) ?? {
271
+ channel: null,
272
+ runId: null,
273
+ unsubscribe: null,
274
+ }
275
+ stopSubscription(state)
276
+ states.set(wsStateKey(ws), state)
277
+
278
+ try {
279
+ state.unsubscribe = await host.broker.subscribe(
280
+ {
281
+ projectId: host.id,
282
+ streamId: AGENT_ACTIVITY_STREAM_ID,
283
+ from: "latest",
284
+ },
285
+ (records) => {
286
+ void sendVisibleActivityRecords(sixb, ws, records).catch((error) => {
287
+ console.error("[SixbServer] Agent activity visibility check failed:", error)
288
+ safeSend(ws, {
289
+ type: "error",
290
+ message: "Could not verify agent activity visibility.",
291
+ })
292
+ })
293
+ }
294
+ )
295
+ state.channel = "activity"
296
+ } catch (error) {
297
+ state.unsubscribe = null
298
+ state.channel = null
299
+ safeSend(ws, { type: "error", message: errorMessage(error) })
300
+ return
301
+ }
302
+
303
+ // The client reconciles its durable thread query on every subscription, closing the race between
304
+ // the initial HTTP snapshot and this live subscription and recovering changes missed offline.
305
+ safeSend(ws, { type: "subscribed.activity" })
306
+ }
307
+
308
+ async function sendVisibleActivityRecords(
309
+ sixb: Sixb<readonly OntologySource[]>,
310
+ ws: { send: (message: string) => void },
311
+ records: readonly BrokerRecord[]
312
+ ): Promise<void> {
313
+ const events: AgentRunActivityEvent[] = []
314
+ for (const record of records) {
315
+ if (isAgentRunActivityEvent(record.payload)) events.push(record.payload)
316
+ }
317
+ const visible = await Promise.all(
318
+ events.map((event) => canAccessAgentThreadActivity(sixb, event))
319
+ )
320
+ for (const [index, event] of events.entries()) {
321
+ if (visible[index]) safeSend(ws, { type: "activity", event })
322
+ }
323
+ }
324
+
204
325
  async function replayAgentStream(
205
326
  server: SixbServer,
206
327
  ws: { send: (message: string) => void },
207
328
  message: ReplayMessage
208
329
  ): Promise<void> {
209
- const sixb = server.getSixb()
210
- const access = await canAccessAgentRunStream(sixb, {
211
- runId: message.runId,
212
- authz: wsAuthz(ws),
213
- })
330
+ const host = server.getHost()
331
+ const sixb = wsRequestSixb(ws)
332
+ if (!sixb) {
333
+ safeSend(ws, { type: "error", message: "Execution scope is not available." })
334
+ return
335
+ }
336
+ const access = await canAccessAgentRunStream(sixb, message.runId)
214
337
  if (!access.ok) {
215
338
  safeSend(ws, { type: "error", message: access.message })
216
339
  return
@@ -218,12 +341,12 @@ async function replayAgentStream(
218
341
 
219
342
  const streamId = agentRunStreamId(message.runId)
220
343
  try {
221
- await sixb.broker.ensureStream({
222
- projectId: sixb.id,
344
+ await host.broker.ensureStream({
345
+ projectId: host.id,
223
346
  stream: agentRunStreamDefinition(message.runId),
224
347
  })
225
- const page = await sixb.broker.read({
226
- projectId: sixb.id,
348
+ const page = await host.broker.read({
349
+ projectId: host.id,
227
350
  streamId,
228
351
  afterCursor: message.afterCursor,
229
352
  limit: message.limit,
@@ -248,15 +371,18 @@ function unsubscribeAgentStream(
248
371
  ): void {
249
372
  const state = states.get(wsStateKey(ws))
250
373
  const previousRunId = state?.runId ?? null
374
+ const previousChannel = state?.channel ?? null
251
375
 
252
376
  if (!message.runId || previousRunId === null || message.runId === previousRunId) {
253
377
  stopSubscription(state)
254
378
  }
255
379
 
256
- safeSend(ws, {
257
- type: "unsubscribed",
258
- runId: message.runId ?? previousRunId,
259
- })
380
+ safeSend(
381
+ ws,
382
+ previousChannel === "activity"
383
+ ? { type: "unsubscribed.activity" }
384
+ : { type: "unsubscribed", runId: message.runId ?? previousRunId }
385
+ )
260
386
  }
261
387
 
262
388
  function stopSubscription(state: AgentStreamSubscriptionState | undefined): void {
@@ -265,6 +391,7 @@ function stopSubscription(state: AgentStreamSubscriptionState | undefined): void
265
391
  }
266
392
  state.unsubscribe?.()
267
393
  state.unsubscribe = null
394
+ state.channel = null
268
395
  state.runId = null
269
396
  }
270
397
 
@@ -274,12 +401,15 @@ async function sendRunSnapshot(
274
401
  runId: string
275
402
  ): Promise<boolean> {
276
403
  try {
277
- const run = await sixb.storage.agents?.runs.getById({ projectId: sixb.id, id: runId })
404
+ const run = await sixb.agents.runs.getById(runId)
278
405
  if (!run) {
279
406
  safeSend(ws, { type: "error", message: "Agent run not found." })
280
407
  return false
281
408
  }
282
- safeSend(ws, { type: "run.snapshot", run: serializeAgentRun(run) })
409
+ safeSend(ws, {
410
+ type: "run.snapshot",
411
+ run: serializeAgentRun(run),
412
+ })
283
413
  return true
284
414
  } catch (error) {
285
415
  safeSend(ws, { type: "error", message: errorMessage(error) })
@@ -1,11 +1,10 @@
1
1
  import type { DomainEvent } from "@sixb/core"
2
2
  import { scopeKeysForEvent } from "@sixb/core/events/scope"
3
- import { canViewEvent } from "@sixb/core/internal/authorization"
4
3
  import type { Elysia } from "elysia"
5
4
  import { z } from "zod"
6
5
  import { EVENT_TOPICS, EVENT_TYPES } from "../../schemas/events"
7
6
  import type { SixbServer } from "../../server"
8
- import { decodeWsMessage, safeSend, wsAuthz, wsStateKey } from "../../utils/ws"
7
+ import { decodeWsMessage, safeSend, wsRequestSixb, wsStateKey } from "../../utils/ws"
9
8
 
10
9
  interface EventSubscriptionState {
11
10
  topics?: DomainEvent["topic"][]
@@ -65,7 +64,7 @@ export function parseSubscriptionMessage(payload: unknown):
65
64
  }
66
65
 
67
66
  async function resolveLatestCursor(server: SixbServer): Promise<string | undefined> {
68
- return server.getSixb().events.latestCursor()
67
+ return server.getHost().events.latestCursor()
69
68
  }
70
69
 
71
70
  function createDefaultState(): EventSubscriptionState {
@@ -126,7 +125,11 @@ export function registerEventStreamRoutes(app: Elysia, server: SixbServer) {
126
125
  return
127
126
  }
128
127
 
129
- const authz = wsAuthz(ws)
128
+ const sixb = wsRequestSixb(ws)
129
+ if (!sixb) {
130
+ safeSend(ws, { type: "error", message: "Execution scope is not available." })
131
+ return
132
+ }
130
133
 
131
134
  const tick = async () => {
132
135
  if (state.polling) {
@@ -135,7 +138,7 @@ export function registerEventStreamRoutes(app: Elysia, server: SixbServer) {
135
138
 
136
139
  state.polling = true
137
140
  try {
138
- const p = server.getSixb()
141
+ const p = server.getHost()
139
142
  const events = await p.events.read({
140
143
  afterCursor: state.afterCursor,
141
144
  limit: state.limit,
@@ -143,15 +146,13 @@ export function registerEventStreamRoutes(app: Elysia, server: SixbServer) {
143
146
  types: state.types,
144
147
  })
145
148
 
146
- // Each event payload may carry object data, so visibility is derived
147
- // per-event from the principal's grants (see `canViewEvent`), and the
148
- // optional scope narrows the stream further. The cursor advances over
149
- // every event read, not just the ones sent.
149
+ // The execution facade owns per-event visibility. The cursor still advances over every
150
+ // record read, not only the visible ones, so an invisible tail cannot stall polling.
150
151
  for (const event of events) {
151
152
  if (!eventMatchesScope(event, state)) {
152
153
  continue
153
154
  }
154
- if (canViewEvent(authz, event)) {
155
+ if (sixb.events.canRead(event)) {
155
156
  safeSend(ws, { type: "event", event })
156
157
  }
157
158
  }
@@ -1,12 +1,6 @@
1
- import type {
2
- LogLevel,
3
- LogRunRef,
4
- OntologySource,
5
- Sixb,
6
- SixbRunKind,
7
- StoredLogLine,
8
- } from "@sixb/core"
1
+ import type { LogLevel, LogRunRef, SixbRunKind, StoredLogLine } from "@sixb/core"
9
2
  import { BrokerCursorExpiredError } from "@sixb/core/broker"
3
+ import type { LoggingService } from "@sixb/core/internal/logging"
10
4
 
11
5
  const MAX_CLIENT_RECORDS = 1_000
12
6
  const MAX_CLIENT_BYTES = 1_048_576
@@ -56,7 +50,7 @@ export class LogSubscriptionHub {
56
50
  private unsubscribeBroker: (() => void) | null = null
57
51
  private closed = false
58
52
 
59
- constructor(private readonly sixb: Sixb<readonly OntologySource[]>) {}
53
+ constructor(private readonly host: { readonly logging: LoggingService }) {}
60
54
 
61
55
  async subscribe(
62
56
  key: object,
@@ -133,7 +127,7 @@ export class LogSubscriptionHub {
133
127
  private async ensureStarted(): Promise<void> {
134
128
  if (this.closed) throw new Error("Log subscription hub is closed")
135
129
  if (!this.startPromise) {
136
- this.startPromise = this.sixb.logs
130
+ this.startPromise = this.host.logging
137
131
  .subscribe({ from: "latest" }, (lines) => this.deliverLive(lines))
138
132
  .then((unsubscribe) => {
139
133
  if (this.closed) unsubscribe()
@@ -162,7 +156,7 @@ export class LogSubscriptionHub {
162
156
  try {
163
157
  let hasMore = true
164
158
  while (hasMore) {
165
- const page = await this.sixb.logs.read({
159
+ const page = await this.host.logging.read({
166
160
  afterCursor,
167
161
  limit: READ_PAGE_SIZE,
168
162
  kinds: state.filter.kinds,
@@ -205,7 +199,7 @@ export class LogSubscriptionHub {
205
199
 
206
200
  if (error instanceof BrokerCursorExpiredError) {
207
201
  try {
208
- const latest = await this.sixb.logs.tail({ limit: 1 })
202
+ const latest = await this.host.logging.tail({ limit: 1 })
209
203
  this.send(state, {
210
204
  type: "reset",
211
205
  reason: "cursor_expired",
@@ -1,9 +1,9 @@
1
- import { isAllowed, type LogLevel } from "@sixb/core"
1
+ import type { LogLevel } from "@sixb/core"
2
2
  import type { Elysia } from "elysia"
3
3
  import { z } from "zod"
4
4
  import { LOG_LEVELS, LogRunIdSchema, SIXB_RUN_KINDS } from "../../schemas/logs"
5
5
  import type { SixbServer } from "../../server"
6
- import { decodeWsMessage, safeSend, wsAuthz, wsStateKey } from "../../utils/ws"
6
+ import { decodeWsMessage, safeSend, wsRequestSixb, wsStateKey } from "../../utils/ws"
7
7
  import { LogSubscriptionHub } from "./log-subscription-hub"
8
8
 
9
9
  const SubscribeSchema = z
@@ -49,12 +49,16 @@ export function parseLogSubscriptionMessage(
49
49
  }
50
50
 
51
51
  export function registerLogStreamRoutes(app: Elysia, server: SixbServer) {
52
- const hub = new LogSubscriptionHub(server.getSixb())
52
+ const hub = new LogSubscriptionHub(server.getHost())
53
53
 
54
54
  app.onStop(() => hub.close())
55
55
  return app.ws("/ws/logs", {
56
56
  open(ws) {
57
- if (!isAllowed(wsAuthz(ws), { kind: "logs.observe" })) {
57
+ try {
58
+ const sixb = wsRequestSixb(ws)
59
+ if (!sixb) throw new Error("Execution scope is not available.")
60
+ sixb.logs.assertObservable()
61
+ } catch {
58
62
  ws.close(1008, "Missing required capability 'observe:logs'.")
59
63
  return
60
64
  }
@@ -62,7 +66,11 @@ export function registerLogStreamRoutes(app: Elysia, server: SixbServer) {
62
66
  },
63
67
 
64
68
  async message(ws, message) {
65
- if (!isAllowed(wsAuthz(ws), { kind: "logs.observe" })) {
69
+ try {
70
+ const sixb = wsRequestSixb(ws)
71
+ if (!sixb) throw new Error("Execution scope is not available.")
72
+ sixb.logs.assertObservable()
73
+ } catch {
66
74
  ws.close(1008, "Missing required capability 'observe:logs'.")
67
75
  return
68
76
  }
@@ -1,4 +1,10 @@
1
+ import {
2
+ ACTION_RUN_FAILURE_CODES,
3
+ ACTION_RUN_PHASES,
4
+ type ActionRunFailure,
5
+ } from "@sixb/core/storage"
1
6
  import { z } from "zod"
7
+ import { JsonValueSchema, sixbFailureSchema } from "./common"
2
8
  import { ActionParamSchema } from "./ontology"
3
9
 
4
10
  export const ActionIdParamsSchema = z.object({
@@ -55,16 +61,7 @@ export const ActionRunStatusSchema = z.enum([
55
61
  "cancelled",
56
62
  ])
57
63
 
58
- export const ActionRunPhaseSchema = z.enum([
59
- "request",
60
- "enqueue",
61
- "validation",
62
- "writeback",
63
- "edits",
64
- "commit",
65
- "effects",
66
- "cancelled",
67
- ])
64
+ export const ActionRunPhaseSchema = z.enum(ACTION_RUN_PHASES)
68
65
 
69
66
  export const ActionRunsQuerySchema = z.object({
70
67
  actionId: z.string().optional(),
@@ -78,10 +75,34 @@ export const ActionRunsQuerySchema = z.object({
78
75
  order: z.enum(["asc", "desc"]).optional(),
79
76
  })
80
77
 
81
- export const ActionRunFailureSchema = z.object({
82
- name: z.string().optional(),
83
- message: z.string(),
84
- phase: ActionRunPhaseSchema.optional(),
78
+ export const ActionRunFailureSchema: z.ZodType<ActionRunFailure> = sixbFailureSchema(
79
+ ACTION_RUN_FAILURE_CODES
80
+ ).extend({
81
+ details: z.object({
82
+ actionId: z.string(),
83
+ runId: z.string(),
84
+ phase: ActionRunPhaseSchema,
85
+ }),
86
+ })
87
+
88
+ const ActionRunWritebackFailureSchema: z.ZodType<ActionRunFailure<"writeback">> = sixbFailureSchema(
89
+ ACTION_RUN_FAILURE_CODES
90
+ ).extend({
91
+ details: z.object({
92
+ actionId: z.string(),
93
+ runId: z.string(),
94
+ phase: z.literal("writeback"),
95
+ }),
96
+ })
97
+
98
+ const ActionRunEffectsFailureSchema: z.ZodType<ActionRunFailure<"effects">> = sixbFailureSchema(
99
+ ACTION_RUN_FAILURE_CODES
100
+ ).extend({
101
+ details: z.object({
102
+ actionId: z.string(),
103
+ runId: z.string(),
104
+ phase: z.literal("effects"),
105
+ }),
85
106
  })
86
107
 
87
108
  export const ActionRunSummarySchema = z.object({
@@ -97,18 +118,30 @@ export const ActionRunSummarySchema = z.object({
97
118
  error: ActionRunFailureSchema.optional(),
98
119
  })
99
120
 
100
- const ActionRunWritebackRecordSchema = z.object({
101
- status: z.enum(["succeeded", "failed"]),
102
- completedAt: z.string(),
103
- result: z.unknown().optional(),
104
- error: ActionRunFailureSchema.optional(),
105
- })
121
+ const ActionRunWritebackRecordSchema = z.discriminatedUnion("status", [
122
+ z.object({
123
+ status: z.literal("succeeded"),
124
+ completedAt: z.string(),
125
+ result: JsonValueSchema,
126
+ }),
127
+ z.object({
128
+ status: z.literal("failed"),
129
+ completedAt: z.string(),
130
+ error: ActionRunWritebackFailureSchema,
131
+ }),
132
+ ])
106
133
 
107
- const ActionRunEffectsRecordSchema = z.object({
108
- status: z.enum(["succeeded", "failed"]),
109
- completedAt: z.string(),
110
- error: ActionRunFailureSchema.optional(),
111
- })
134
+ const ActionRunEffectsRecordSchema = z.discriminatedUnion("status", [
135
+ z.object({
136
+ status: z.literal("succeeded"),
137
+ completedAt: z.string(),
138
+ }),
139
+ z.object({
140
+ status: z.literal("failed"),
141
+ completedAt: z.string(),
142
+ error: ActionRunEffectsFailureSchema,
143
+ }),
144
+ ])
112
145
 
113
146
  export const ActionRunDetailSchema = ActionRunSummarySchema.extend({
114
147
  params: z.record(z.unknown()),