@sequenceholdings/artifact-studio 0.8.0 → 0.9.0

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 (44) hide show
  1. package/README.md +101 -3
  2. package/dist/api.d.ts +2 -1
  3. package/dist/api.js +15 -5
  4. package/dist/cli.d.ts +2 -0
  5. package/dist/cli.js +58 -20
  6. package/dist/gateway/authorization-cache.js +1 -1
  7. package/dist/gateway/control.d.ts +13 -1
  8. package/dist/gateway/control.js +3 -0
  9. package/dist/gateway/datadog-startup.d.ts +13 -0
  10. package/dist/gateway/datadog-startup.js +51 -0
  11. package/dist/gateway/hosted-proxy.d.ts +2 -0
  12. package/dist/gateway/hosted-proxy.js +9 -11
  13. package/dist/gateway/ingress-handler.js +2 -0
  14. package/dist/gateway/server.d.ts +1 -1
  15. package/dist/gateway/server.js +51 -20
  16. package/dist/gateway/session.d.ts +14 -0
  17. package/dist/gateway/session.js +31 -2
  18. package/dist/gateway-main.js +5 -3
  19. package/dist/next-build.d.ts +3 -1
  20. package/dist/next-build.js +10 -6
  21. package/dist/sdk.d.ts +2 -0
  22. package/dist/server/agent-responses.d.ts +425 -0
  23. package/dist/server/agent-responses.js +154 -0
  24. package/dist/server/broker-contract.d.ts +5 -0
  25. package/dist/server/broker-contract.js +6 -2
  26. package/dist/server/client.d.ts +2 -0
  27. package/dist/server/client.js +10 -2
  28. package/dist/server/processes.d.ts +11 -0
  29. package/dist/server/request-authentication.d.ts +42 -0
  30. package/dist/server/request-authentication.js +93 -0
  31. package/dist/server/request-context.d.ts +1 -0
  32. package/dist/server/request-context.js +1 -0
  33. package/dist/server.d.ts +2 -1
  34. package/dist/server.js +1 -1
  35. package/dist/templates/react-vite/package.json +1 -1
  36. package/dist/tunnel/broker.d.ts +14 -1
  37. package/dist/tunnel/broker.js +94 -10
  38. package/dist/tunnel/client.js +6 -0
  39. package/dist/tunnel/redis-types.d.ts +5 -1
  40. package/dist/tunnel/redis.d.ts +12 -0
  41. package/dist/tunnel/redis.js +102 -4
  42. package/dist/tunnel/transport.d.ts +3 -0
  43. package/package.json +3 -3
  44. package/templates/react-vite/package.json +1 -1
@@ -4,6 +4,9 @@ import { redisIdentifier as identifier, redisNamespaceSchema as namespaceSchema,
4
4
  import { RelaySocket } from './redis-socket.js';
5
5
  import { deriveRelayFrameKey, RelayFrameAuthenticator } from './redis-auth.js';
6
6
  export { RedisTunnelOwnership } from './redis-ownership.js';
7
+ // Broker stream IDs are UUIDs. This authenticated, otherwise-unused ACK is ignored by old
8
+ // owners and never forwarded to the developer. It negotiates cancel-as-completion-receipt.
9
+ const DRAIN_CAPABILITY_ID = 'relay-drain-v1';
7
10
  /** Internal Redis channels contain authorized application traffic. Deploy them
8
11
  * on the environment's private, authenticated Redis and disable command replay.
9
12
  * Every message is authenticated with a gateway-only key: other Redis clients
@@ -12,6 +15,10 @@ export class RedisTunnelRelay {
12
15
  options;
13
16
  started = false;
14
17
  closed = false;
18
+ draining = false;
19
+ drainSealed = false;
20
+ drainWork = Promise.resolve();
21
+ drainWaiters = new Set();
15
22
  attached = new Map();
16
23
  connected = new Map();
17
24
  frames;
@@ -165,10 +172,14 @@ export class RedisTunnelRelay {
165
172
  }
166
173
  async attachOwned(input, socket) {
167
174
  this.assertStarted();
175
+ if (this.draining)
176
+ throw new Error('Tunnel relay is draining');
168
177
  const lease = await this.verifiedLease(input);
169
178
  if (!(await this.current(lease, this.options.nodeId)) || socket.readyState !== 1)
170
179
  throw new Error('Tunnel is owned by another node');
171
180
  this.assertStarted();
181
+ if (this.draining)
182
+ throw new Error('Tunnel relay is draining');
172
183
  const previous = this.attached.get(lease.sessionId);
173
184
  if (previous && previous.lease.epoch >= lease.epoch)
174
185
  throw new Error('Duplicate tunnel owner');
@@ -242,6 +253,10 @@ export class RedisTunnelRelay {
242
253
  }
243
254
  const remote = owner.remotes.get(message.connection);
244
255
  if (message.type === 'open') {
256
+ if (this.draining && (this.drainSealed || message.source !== this.options.nodeId)) {
257
+ await this.publish(message.source, this.envelope(owner.lease, message.connection, 'close', 'requester'));
258
+ return;
259
+ }
245
260
  if (remote || owner.remotes.size >= DEFAULT_TUNNEL_LIMITS.maxStreams)
246
261
  throw new Error('Duplicate or excessive tunnel connections');
247
262
  owner.remotes.set(message.connection, { nodeId: message.source, connection: message.connection, lastSeen: Date.now() });
@@ -272,6 +287,11 @@ export class RedisTunnelRelay {
272
287
  if (message.type === 'ready') {
273
288
  if (connection.socket.readyState !== 0)
274
289
  throw new Error('Duplicate tunnel ready');
290
+ // Publish before exposing the socket, preserving marker-before-request ordering even
291
+ // when this node is also the owner. Old owners ignore ACKs for unknown stream IDs.
292
+ await this.publish(connection.ownerNode, this.envelope(connection.lease, message.connection, 'frame', 'owner', JSON.stringify({ type: 'ack', id: DRAIN_CAPABILITY_ID, bytes: 1 })));
293
+ if (this.connected.get(message.connection) !== connection || connection.socket.readyState !== 0)
294
+ return;
275
295
  clearTimeout(connection.timer);
276
296
  connection.socket.readyState = 1;
277
297
  connection.resolve(connection.socket);
@@ -287,6 +307,11 @@ export class RedisTunnelRelay {
287
307
  const frame = parseTunnelFrame(raw);
288
308
  if (frame.type === 'ready' || frame.type === 'response' || frame.type === 'ws-open')
289
309
  throw new Error('Invalid requester frame direction');
310
+ if (frame.id === DRAIN_CAPABILITY_ID) {
311
+ if (frame.type === 'ack' && frame.bytes === 1)
312
+ remote.receipts = true;
313
+ return;
314
+ }
290
315
  const route = owner.streams.get(frame.id);
291
316
  if (frame.type === 'request') {
292
317
  if (route || owner.streams.size >= DEFAULT_TUNNEL_LIMITS.maxStreams) {
@@ -294,13 +319,37 @@ export class RedisTunnelRelay {
294
319
  await this.publish(remote.nodeId, this.envelope(owner.lease, remote.connection, 'close', 'requester'));
295
320
  return;
296
321
  }
297
- owner.streams.set(frame.id, remote);
322
+ const refused = this.draining && (this.drainSealed || remote.nodeId !== this.options.nodeId || frame.websocket);
323
+ if (refused && frame.websocket) {
324
+ // Upgrades cannot consume HTTP response frames. Cancel only this stream; no
325
+ // route or completion receipt is needed because it never reached the developer.
326
+ await this.publish(remote.nodeId, this.envelope(owner.lease, remote.connection, 'frame', 'requester', JSON.stringify({ type: 'cancel', id: frame.id })));
327
+ return;
328
+ }
329
+ owner.streams.set(frame.id, { ...remote, websocket: frame.websocket, ended: refused });
330
+ if (refused) {
331
+ await this.publish(remote.nodeId, this.envelope(owner.lease, remote.connection, 'frame', 'requester', JSON.stringify({ type: 'response', id: frame.id, status: 503, headers: { 'cache-control': 'no-store' } })));
332
+ await this.publish(remote.nodeId, this.envelope(owner.lease, remote.connection, 'frame', 'requester', JSON.stringify({ type: 'end', id: frame.id })));
333
+ if (!remote.receipts)
334
+ owner.streams.delete(frame.id);
335
+ this.notifyDrained();
336
+ return;
337
+ }
298
338
  }
299
339
  else if (!route || route.connection !== remote.connection || route.nodeId !== remote.nodeId)
300
340
  return;
301
- if (frame.type === 'cancel' || frame.type === 'ws-close')
302
- owner.streams.delete(frame.id);
341
+ if (route?.ended) {
342
+ if (frame.type === 'cancel' || frame.type === 'ws-close') {
343
+ owner.streams.delete(frame.id);
344
+ this.notifyDrained();
345
+ }
346
+ return;
347
+ }
303
348
  await this.writeDeveloper(owner, raw);
349
+ if (frame.type === 'cancel' || frame.type === 'ws-close') {
350
+ owner.streams.delete(frame.id);
351
+ this.notifyDrained();
352
+ }
304
353
  }
305
354
  writeDeveloper(owner, raw) {
306
355
  if (owner.stopped || owner.socket.readyState !== 1 || owner.socket.bufferedAmount + Buffer.byteLength(raw) > DEFAULT_TUNNEL_LIMITS.maxBufferedBytes) {
@@ -336,9 +385,15 @@ export class RedisTunnelRelay {
336
385
  const remote = owner.streams.get(frame.id);
337
386
  if (!remote)
338
387
  return;
388
+ if (remote.ended)
389
+ return;
390
+ const retainReceipt = frame.type === 'end' && !remote.websocket && remote.receipts;
391
+ if (retainReceipt)
392
+ remote.ended = true;
339
393
  await this.publish(remote.nodeId, this.envelope(owner.lease, remote.connection, 'frame', 'requester', raw));
340
- if (frame.type === 'end' || frame.type === 'cancel' || frame.type === 'ws-close')
394
+ if (!retainReceipt && (frame.type === 'end' || frame.type === 'cancel' || frame.type === 'ws-close'))
341
395
  owner.streams.delete(frame.id);
396
+ this.notifyDrained();
342
397
  }).catch(() => this.closeOwner(owner)).finally(() => { owner.queuedBytes -= bytes; });
343
398
  }
344
399
  closeRemote(owner, remote) {
@@ -349,6 +404,7 @@ export class RedisTunnelRelay {
349
404
  owner.streams.delete(id);
350
405
  void this.writeDeveloper(owner, JSON.stringify({ type: 'cancel', id })).catch(() => this.closeOwner(owner));
351
406
  }
407
+ this.notifyDrained();
352
408
  }
353
409
  closeOwner(owner) {
354
410
  if (owner.stopped)
@@ -362,6 +418,7 @@ export class RedisTunnelRelay {
362
418
  void this.publish(remote.nodeId, this.envelope(owner.lease, remote.connection, 'close', 'requester')).catch(() => undefined);
363
419
  owner.remotes.clear();
364
420
  owner.streams.clear();
421
+ this.notifyDrained();
365
422
  void this.options.ownership.release(owner.lease.sessionId, owner.lease.epoch, this.options.nodeId).catch(() => undefined);
366
423
  }
367
424
  closeConnection(id, connection) {
@@ -387,6 +444,47 @@ export class RedisTunnelRelay {
387
444
  return this.publish(remote.nodeId, { type: 'revoke', destination: 'owner', source: this.options.nodeId, connection: randomUUID(), sessionId, epoch: remote.epoch });
388
445
  }).catch(() => undefined);
389
446
  }
447
+ /** Reject new remote requests, but allow same-node work already admitted by the HTTP
448
+ * gateway until drain() seals it. Security checks and ownership timers stay live. */
449
+ beginDrain() {
450
+ if (this.draining)
451
+ return;
452
+ this.draining = true;
453
+ const cancellations = [];
454
+ for (const owner of this.attached.values())
455
+ for (const [id, route] of owner.streams) {
456
+ if (!route.websocket)
457
+ continue;
458
+ route.ended = true;
459
+ const frame = JSON.stringify({ type: 'cancel', id });
460
+ cancellations.push(Promise.all([
461
+ this.writeDeveloper(owner, frame),
462
+ this.publish(route.nodeId, this.envelope(owner.lease, route.connection, 'frame', 'requester', frame)),
463
+ ]).then(() => { owner.streams.delete(id); this.notifyDrained(); }).catch(() => this.closeOwner(owner)));
464
+ }
465
+ this.drainWork = Promise.all(cancellations).then(() => undefined);
466
+ }
467
+ notifyDrained() {
468
+ if (!this.drainWaiters.size)
469
+ return;
470
+ for (const owner of this.attached.values())
471
+ if (owner.streams.size)
472
+ return;
473
+ for (const resolve of this.drainWaiters)
474
+ resolve();
475
+ this.drainWaiters.clear();
476
+ }
477
+ /** Updated requesters acknowledge consumed final responses with cancel. Keep those bounded
478
+ * routes even before drain starts to cover an end-vs-shutdown race. Legacy requesters retain
479
+ * the previous publish-only completion behavior; they never acquire a new receipt wait. */
480
+ async drain() {
481
+ this.beginDrain();
482
+ this.drainSealed = true;
483
+ await this.receiving;
484
+ await this.drainWork;
485
+ await new Promise((resolve) => { this.drainWaiters.add(resolve); this.notifyDrained(); });
486
+ await this.publishing;
487
+ }
390
488
  async close() {
391
489
  if (this.closed)
392
490
  return;
@@ -13,5 +13,8 @@ export interface TunnelRelay {
13
13
  attach(lease: TunnelLease, socket: TunnelSocket): Promise<void>;
14
14
  connect(lease: TunnelLease): Promise<TunnelSocket>;
15
15
  disconnect(sessionId: string): void;
16
+ /** Optional for custom adapters; built-in Redis retains owned finite streams during drain. */
17
+ beginDrain?(): void;
18
+ drain?(): Promise<void>;
16
19
  close(): Promise<void>;
17
20
  }
package/package.json CHANGED
@@ -9,7 +9,7 @@
9
9
  "publishConfig": {
10
10
  "access": "public"
11
11
  },
12
- "version": "0.8.0",
12
+ "version": "0.9.0",
13
13
  "description": "SDK types and CLI library for building Artifact Studio apps. Driven via `seq-studio artifact <sub>` (@sequenceholdings/studio-cli), which runs the `./cli` runCli export.",
14
14
  "type": "module",
15
15
  "exports": {
@@ -167,9 +167,9 @@
167
167
  "google-auth-library": "^10.5.0",
168
168
  "server-only": "0.0.1",
169
169
  "parse5": "7.3.0",
170
+ "@sequenceholdings/lattice-form-renderer": "0.2.0",
170
171
  "@sequenceholdings/storage": "0.1.1",
171
- "@sequenceholdings/orm": "2.6.1",
172
- "@sequenceholdings/lattice-form-renderer": "0.2.0"
172
+ "@sequenceholdings/orm": "2.6.2"
173
173
  },
174
174
  "devDependencies": {
175
175
  "@types/js-yaml": "^4.0.9",
@@ -10,7 +10,7 @@
10
10
  },
11
11
  "dependencies": {},
12
12
  "devDependencies": {
13
- "@sequenceholdings/artifact-studio": "^0.8.0",
13
+ "@sequenceholdings/artifact-studio": "^0.9.0",
14
14
  "@sequenceholdings/atlas-ui": "^3.1.1",
15
15
  "@tailwindcss/postcss": "^4.2.2",
16
16
  "@tailwindcss/vite": "^4.2.2",