@sixb/server 0.2.4 → 0.2.6

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 (90) hide show
  1. package/dist/index.d.ts +1 -1
  2. package/dist/index.d.ts.map +1 -1
  3. package/dist/index.js +877 -89
  4. package/dist/index.js.map +26 -25
  5. package/dist/live/room.d.ts +2 -0
  6. package/dist/live/room.d.ts.map +1 -1
  7. package/dist/registerRoutes.d.ts +1 -0
  8. package/dist/registerRoutes.d.ts.map +1 -1
  9. package/dist/routes/agent-approvals.d.ts +22 -0
  10. package/dist/routes/agent-approvals.d.ts.map +1 -1
  11. package/dist/routes/agent-computers.d.ts +36 -0
  12. package/dist/routes/agent-computers.d.ts.map +1 -1
  13. package/dist/routes/agent-runners.d.ts +5 -1
  14. package/dist/routes/agent-runners.d.ts.map +1 -1
  15. package/dist/routes/agent-thread-reads.d.ts +82 -0
  16. package/dist/routes/agent-thread-reads.d.ts.map +1 -0
  17. package/dist/routes/agents.d.ts +1760 -86
  18. package/dist/routes/agents.d.ts.map +1 -1
  19. package/dist/routes/ai-accounting.d.ts.map +1 -1
  20. package/dist/routes/artifact-frames.d.ts +3 -3
  21. package/dist/routes/artifact-frames.d.ts.map +1 -1
  22. package/dist/routes/artifacts.d.ts +287 -21
  23. package/dist/routes/artifacts.d.ts.map +1 -1
  24. package/dist/routes/auth.d.ts +7 -7
  25. package/dist/routes/events.d.ts +4 -4
  26. package/dist/routes/links.d.ts +8 -0
  27. package/dist/routes/links.d.ts.map +1 -1
  28. package/dist/routes/model-generations.d.ts +6 -0
  29. package/dist/routes/model-generations.d.ts.map +1 -1
  30. package/dist/routes/objects.d.ts +8 -0
  31. package/dist/routes/objects.d.ts.map +1 -1
  32. package/dist/routes/observed-agent-threads.d.ts +22 -0
  33. package/dist/routes/observed-agent-threads.d.ts.map +1 -1
  34. package/dist/routes/projections.d.ts +2 -0
  35. package/dist/routes/projections.d.ts.map +1 -1
  36. package/dist/routes/status.d.ts +4 -1
  37. package/dist/routes/status.d.ts.map +1 -1
  38. package/dist/routes/syncs.d.ts +175 -12
  39. package/dist/routes/syncs.d.ts.map +1 -1
  40. package/dist/routes/workflows.d.ts +6 -6
  41. package/dist/routes/ws/agents.d.ts.map +1 -1
  42. package/dist/routes/ws/events.d.ts +6 -6
  43. package/dist/schemas/activity.d.ts +4 -4
  44. package/dist/schemas/agents.d.ts +7697 -262
  45. package/dist/schemas/agents.d.ts.map +1 -1
  46. package/dist/schemas/ai-accounting.d.ts +3 -0
  47. package/dist/schemas/ai-accounting.d.ts.map +1 -1
  48. package/dist/schemas/artifacts.d.ts +676 -36
  49. package/dist/schemas/artifacts.d.ts.map +1 -1
  50. package/dist/schemas/auth.d.ts +8 -8
  51. package/dist/schemas/common.d.ts.map +1 -1
  52. package/dist/schemas/events.d.ts +36 -24
  53. package/dist/schemas/events.d.ts.map +1 -1
  54. package/dist/schemas/lineage.d.ts +112 -0
  55. package/dist/schemas/lineage.d.ts.map +1 -1
  56. package/dist/schemas/models.d.ts +46 -0
  57. package/dist/schemas/models.d.ts.map +1 -1
  58. package/dist/schemas/projections.d.ts +11 -0
  59. package/dist/schemas/projections.d.ts.map +1 -1
  60. package/dist/schemas/syncs.d.ts +724 -25
  61. package/dist/schemas/syncs.d.ts.map +1 -1
  62. package/dist/schemas/workflows.d.ts +22 -22
  63. package/dist/server.d.ts +16 -1
  64. package/dist/server.d.ts.map +1 -1
  65. package/package.json +5 -5
  66. package/src/index.ts +6 -1
  67. package/src/live/README.md +9 -6
  68. package/src/live/room.ts +16 -8
  69. package/src/registerRoutes.ts +4 -1
  70. package/src/routes/agent-computers.ts +7 -6
  71. package/src/routes/agent-runners.ts +15 -4
  72. package/src/routes/agent-thread-reads.ts +67 -0
  73. package/src/routes/agents.ts +331 -20
  74. package/src/routes/ai-accounting.ts +1 -0
  75. package/src/routes/artifact-frames.ts +22 -9
  76. package/src/routes/artifacts.ts +124 -3
  77. package/src/routes/model-generations.ts +1 -1
  78. package/src/routes/observed-agent-threads.ts +3 -4
  79. package/src/routes/status.ts +18 -1
  80. package/src/routes/syncs.ts +135 -8
  81. package/src/routes/ws/agents.ts +43 -3
  82. package/src/schemas/agents.ts +148 -12
  83. package/src/schemas/ai-accounting.ts +12 -0
  84. package/src/schemas/artifacts.ts +66 -1
  85. package/src/schemas/common.ts +15 -9
  86. package/src/schemas/events.ts +1 -0
  87. package/src/schemas/models.ts +10 -0
  88. package/src/schemas/projections.ts +1 -0
  89. package/src/schemas/syncs.ts +63 -1
  90. package/src/server.ts +208 -14
package/src/server.ts CHANGED
@@ -1,13 +1,14 @@
1
1
  import { cors } from "@elysiajs/cors"
2
2
  import { openapi } from "@elysiajs/openapi"
3
3
  import type { OntologyMaintenanceHandle, SixbHostView } from "@sixb/core"
4
+ import { ACTION_RUN_DRAIN_TIMEOUT_MS } from "@sixb/core/internal/actions"
4
5
  import { bindAgentRunRequestExecution } from "@sixb/core/internal/agent-execution"
5
6
  import { ARTIFACT_FRAME_ROUTE_PREFIX } from "@sixb/core/internal/artifacts/frames"
6
7
  import { CSRF_HEADER_NAME, setRequestClientAddress } from "@sixb/core/internal/auth"
7
8
  import { bindRequestExecution } from "@sixb/core/internal/request-execution"
8
9
  import { findDatasetIngestRuntime, setApiPublicOrigin } from "@sixb/core/internal/runtime"
9
10
  import { checkPipelineMarkings, warmAppliedMarkings } from "@sixb/core/internal/security"
10
- import type { Server } from "bun"
11
+ import type { Server, ServerWebSocket } from "bun"
11
12
  import { Elysia, ParseError } from "elysia"
12
13
  import { websocket as elysiaWebSocket } from "elysia/ws"
13
14
  import {
@@ -70,6 +71,8 @@ import {
70
71
 
71
72
  // Clients send only subscription messages; Bun's default would buffer up to 16 MB for one.
72
73
  export const MAX_WEBSOCKET_MESSAGE_BYTES = 64 * 1024
74
+ /** RFC 6455 Service Restart: tells clients to hand off immediately to the new release. */
75
+ const SERVICE_RESTART_CLOSE_CODE = 1012
73
76
 
74
77
  export interface SixbServerOptions {
75
78
  host: SixbHostView
@@ -100,6 +103,15 @@ export interface SixbServerOptions {
100
103
  ingestFlush?: boolean
101
104
  }
102
105
 
106
+ export interface SixbServerStopOptions {
107
+ /**
108
+ * How long in-flight HTTP requests may take to finish before remaining connections are closed.
109
+ * Defaults to 65 seconds, matching the Action drain window; `0` closes them at once. A stop
110
+ * called while another one drains joins it, and a shorter timeout brings its deadline closer.
111
+ */
112
+ readonly timeoutMs?: number
113
+ }
114
+
103
115
  export function createSixbServer(options: SixbServerOptions): SixbServer {
104
116
  return new SixbServer(options)
105
117
  }
@@ -118,10 +130,13 @@ export class SixbServer {
118
130
  private readonly timing: SixbRequestTimingOptions
119
131
  private app: SixbApp | null = null
120
132
  private bunServer: ReturnType<typeof Bun.serve> | null = null
133
+ private closeWebSockets: (() => void) | null = null
134
+ private stopping = false
121
135
  private readonly hostsOntologyMaintenance: boolean
122
136
  private readonly flushesIngestion: boolean
123
137
  private maintenance: OntologyMaintenanceHandle | null = null
124
138
  private stopSweeper: (() => void) | null = null
139
+ private inProgressStop: InProgressStop | null = null
125
140
 
126
141
  constructor(options: SixbServerOptions) {
127
142
  this.hostRuntime = options.host
@@ -190,10 +205,20 @@ export class SixbServer {
190
205
  return this.sharedAccessOptions
191
206
  }
192
207
 
208
+ /** Marks the server as stopping so `/ready` answers `503` before connections close. */
209
+ markStopping(): void {
210
+ this.stopping = true
211
+ }
212
+
213
+ isStopping(): boolean {
214
+ return this.stopping
215
+ }
216
+
193
217
  async start(): Promise<void> {
194
218
  if (this.app !== null || this.bunServer !== null || this.maintenance !== null) {
195
219
  throw new Error("[SixbServer] Server is already running.")
196
220
  }
221
+ this.stopping = false
197
222
 
198
223
  // Startup cannot see through SQL steps: check what they derive before serving any request.
199
224
  await checkPipelineMarkings({
@@ -215,15 +240,18 @@ export class SixbServer {
215
240
 
216
241
  try {
217
242
  this.app = createSixbApi(this)
218
- this.bunServer = startApiServer(this.app, {
243
+ const started = startApiServer(this.app, {
219
244
  host: this.hostname,
220
245
  port: this.port,
221
246
  resolveClientAddress: this.resolveClientAddress,
222
247
  timing: this.timing,
223
248
  })
249
+ this.bunServer = started.bunServer
250
+ this.closeWebSockets = started.closeWebSockets
224
251
  } catch (error) {
225
252
  this.app = null
226
253
  this.bunServer = null
254
+ this.closeWebSockets = null
227
255
  this.maintenance = null
228
256
  await maintenance?.stop()
229
257
  const message = error instanceof Error ? error.message : String(error)
@@ -245,18 +273,49 @@ export class SixbServer {
245
273
  if (ingest) ingest.writesInline = inline
246
274
  }
247
275
 
248
- async stop(): Promise<void> {
276
+ async stop(options: SixbServerStopOptions = {}): Promise<void> {
277
+ const timeoutMs = resolveStopTimeoutMs(options.timeoutMs, ACTION_RUN_DRAIN_TIMEOUT_MS)
278
+
279
+ // A stop that arrives while another one drains joins it, and can only bring its deadline
280
+ // closer: `sixb api` passes `timeoutMs: 0` when SIGINT follows SIGTERM.
281
+ if (this.inProgressStop) {
282
+ this.inProgressStop.deadline.bringForward(timeoutMs)
283
+ return this.inProgressStop.done
284
+ }
285
+
286
+ const deadline = new StopDeadline(timeoutMs)
287
+ const done = this.stopUntil(deadline).finally(() => {
288
+ this.inProgressStop = null
289
+ })
290
+ this.inProgressStop = { deadline, done }
291
+ return done
292
+ }
293
+
294
+ private async stopUntil(deadline: StopDeadline): Promise<void> {
295
+ this.stopping = true
249
296
  this.setIngestFlush(false)
250
297
  this.stopSweeper?.()
251
298
  this.stopSweeper = null
252
- if (this.bunServer) {
253
- this.bunServer.stop(true)
254
- this.bunServer = null
299
+
300
+ const bunServer = this.bunServer
301
+ const closeWebSockets = this.closeWebSockets
302
+ const app = this.app
303
+ this.bunServer = null
304
+ this.closeWebSockets = null
305
+ this.app = null
306
+
307
+ const stopAppAndSockets = async () => {
308
+ try {
309
+ if (app) await stopElysiaHooks(app)
310
+ } finally {
311
+ closeWebSockets?.()
312
+ }
255
313
  }
256
314
 
257
- if (this.app) {
258
- await this.app.stop()
259
- this.app = null
315
+ if (bunServer) {
316
+ await drainBunServer(bunServer, deadline, stopAppAndSockets)
317
+ } else {
318
+ await stopAppAndSockets()
260
319
  }
261
320
 
262
321
  const maintenance = this.maintenance
@@ -306,7 +365,9 @@ export function createSixbApi(server: SixbServer) {
306
365
  )
307
366
  // A runner's session requests, before anything parses them: a runner token speaks only there.
308
367
  const serveRunnerSession = agentRunSessionHandler(host)
309
- app.onRequest(async ({ request }) => (await serveRunnerSession(request)) ?? undefined)
368
+ app.onRequest(
369
+ async ({ request, server }) => (await serveRunnerSession(request, server)) ?? undefined
370
+ )
310
371
  app.onRequest(async ({ request }) => {
311
372
  // An Agent's commands, as their run: decided before Elysia parses the body, which the approval
312
373
  // gate reads from a copy. No session, share or cookie speaks for such a request.
@@ -515,6 +576,7 @@ export function createSixbApi(server: SixbServer) {
515
576
  registerHttpRoutes(app, host, {
516
577
  sharedAccess,
517
578
  browserPolicy: apiBrowserPolicy,
579
+ isStopping: () => server.isStopping(),
518
580
  connectorConnections: {
519
581
  resolveReturnTo: (request, returnTo) =>
520
582
  server.resolveAuthRedirectContext(request, { returnTo }).returnTo,
@@ -606,6 +668,7 @@ function startApiServer(
606
668
  readonly timing: SixbRequestTimingOptions
607
669
  }
608
670
  ) {
671
+ const openSockets = new Set<ServerWebSocket<unknown>>()
609
672
  const bunServer = Bun.serve({
610
673
  port: options.port,
611
674
  hostname: options.host,
@@ -622,22 +685,153 @@ function startApiServer(
622
685
  }
623
686
  return timeRequest(request, options.timing, () => app.fetch(request))
624
687
  },
625
- websocket: getElysiaWsHandler(app),
688
+ websocket: getElysiaWsHandler(app, openSockets),
626
689
  } as Parameters<typeof Bun.serve>[0])
627
690
 
628
691
  attachBunServer(app, bunServer)
629
- return bunServer
692
+ return {
693
+ bunServer,
694
+ closeWebSockets: () => {
695
+ for (const ws of [...openSockets]) {
696
+ try {
697
+ ws.close(SERVICE_RESTART_CLOSE_CODE, "Service Restart")
698
+ } catch {
699
+ // Already closed.
700
+ }
701
+ }
702
+ openSockets.clear()
703
+ },
704
+ }
630
705
  }
631
706
 
632
- function getElysiaWsHandler(app: SixbApp) {
707
+ function getElysiaWsHandler(app: SixbApp, openSockets: Set<ServerWebSocket<unknown>>) {
633
708
  const cfg = (app as unknown as { config?: { websocket?: Record<string, unknown> } }).config
634
- return {
709
+ const base = {
635
710
  ...elysiaWebSocket,
636
711
  ...(cfg?.websocket ?? {}),
712
+ } as NonNullable<Parameters<typeof Bun.serve>[0]["websocket"]>
713
+ return {
714
+ ...base,
637
715
  maxPayloadLength: MAX_WEBSOCKET_MESSAGE_BYTES,
716
+ open(ws: ServerWebSocket<unknown>) {
717
+ openSockets.add(ws)
718
+ return base.open?.(ws)
719
+ },
720
+ close(ws: ServerWebSocket<unknown>, code: number, reason: string) {
721
+ openSockets.delete(ws)
722
+ return base.close?.(ws, code, reason)
723
+ },
638
724
  } as Parameters<typeof Bun.serve>[0]["websocket"]
639
725
  }
640
726
 
727
+ function resolveStopTimeoutMs(timeoutMs: number | undefined, fallbackMs: number): number {
728
+ const resolved = timeoutMs ?? fallbackMs
729
+ if (!Number.isFinite(resolved) || resolved < 0) {
730
+ throw new Error("[SixbServer] Stop timeout must be a non-negative finite number.")
731
+ }
732
+ return resolved
733
+ }
734
+
735
+ /** A stop under way: a later `stop()` joins `done` and may bring `deadline` closer. */
736
+ interface InProgressStop {
737
+ readonly deadline: StopDeadline
738
+ readonly done: Promise<void>
739
+ }
740
+
741
+ /** When a stopping server gives up on in-flight requests. A later `stop()` can only bring it closer. */
742
+ class StopDeadline {
743
+ private at: number
744
+
745
+ constructor(timeoutMs: number) {
746
+ this.at = Date.now() + timeoutMs
747
+ }
748
+
749
+ remainingMs(): number {
750
+ return this.at - Date.now()
751
+ }
752
+
753
+ bringForward(timeoutMs: number): void {
754
+ this.at = Math.min(this.at, Date.now() + timeoutMs)
755
+ }
756
+ }
757
+
758
+ async function drainBunServer(
759
+ server: ReturnType<typeof Bun.serve>,
760
+ deadline: StopDeadline,
761
+ beforeWait: () => Promise<void>
762
+ ): Promise<void> {
763
+ if (deadline.remainingMs() <= 0) {
764
+ try {
765
+ await beforeWait()
766
+ } finally {
767
+ await server.stop(true)
768
+ }
769
+ return
770
+ }
771
+
772
+ const draining = server.stop(false)
773
+ try {
774
+ await beforeWait()
775
+ } catch (error) {
776
+ await server.stop(true)
777
+ await draining
778
+ throw error
779
+ }
780
+
781
+ // Bun's `stop(false)` can stay pending on an already-answered 413 socket whose unread upload
782
+ // body has not hit its idle timeout yet, even though `pendingRequests` is already 0.
783
+ await waitUntilIdleOrTimeout(server, draining, deadline)
784
+ await server.stop(true)
785
+ await draining
786
+ }
787
+
788
+ /** Polls every 25 ms, so a deadline brought forward takes effect within one poll. */
789
+ async function waitUntilIdleOrTimeout(
790
+ server: ReturnType<typeof Bun.serve>,
791
+ draining: Promise<void>,
792
+ deadline: StopDeadline
793
+ ): Promise<void> {
794
+ let settled = false
795
+ void draining.then(
796
+ () => {
797
+ settled = true
798
+ },
799
+ () => {
800
+ settled = true
801
+ }
802
+ )
803
+
804
+ while (!settled && (server.pendingRequests > 0 || server.pendingWebSockets > 0)) {
805
+ const remaining = deadline.remainingMs()
806
+ if (remaining <= 0) return
807
+ let timer: ReturnType<typeof setTimeout> | undefined
808
+ try {
809
+ await Promise.race([
810
+ draining,
811
+ new Promise<void>((resolve) => {
812
+ timer = setTimeout(resolve, Math.min(remaining, 25))
813
+ }),
814
+ ])
815
+ } finally {
816
+ if (timer !== undefined) clearTimeout(timer)
817
+ }
818
+ }
819
+ }
820
+
641
821
  function attachBunServer(app: SixbApp, bunServer: ReturnType<typeof Bun.serve>) {
642
822
  ;(app as unknown as { server: typeof bunServer }).server = bunServer
643
823
  }
824
+
825
+ /**
826
+ * Runs Elysia's `onStop` hooks (such as `LiveRooms.close()`) and awaits their promises without
827
+ * waiting on `bunServer.stop(false)` first, which stays pending while WebSockets are still open.
828
+ */
829
+ async function stopElysiaHooks(app: SixbApp): Promise<void> {
830
+ const internal = app as unknown as {
831
+ server: unknown
832
+ event?: { stop?: ReadonlyArray<{ fn: (instance: SixbApp) => unknown }> }
833
+ }
834
+ const hooks = internal.event?.stop ?? []
835
+ internal.server = null
836
+ await Promise.all(hooks.map((hook) => Promise.resolve(hook.fn(app))))
837
+ }