castle-web-cli 0.4.140 → 0.4.141

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.
package/dist/agent.js CHANGED
@@ -3853,14 +3853,26 @@ function foldRouterQueueTurn(ctx) {
3853
3853
  state.autoFoldUsed = true;
3854
3854
  broadcastQueueState(ctx);
3855
3855
  }
3856
- function handleQueueUserMessage(ctx, text, images) {
3856
+ // A client-supplied message id, or null if it isn't one we can safely adopt.
3857
+ // It becomes a message id AND an attachment directory name, so the character
3858
+ // set is the tight one -- nothing here may traverse a path.
3859
+ function safeClientMessageId(clientId) {
3860
+ if (typeof clientId !== 'string')
3861
+ return null;
3862
+ return /^[A-Za-z0-9_-]{1,32}$/.test(clientId) ? clientId : null;
3863
+ }
3864
+ function handleQueueUserMessage(ctx, text, images, clientId) {
3857
3865
  const { state } = ctx;
3858
3866
  // Mid-run: queue (don't interrupt). It shows as a queued row in the
3859
3867
  // composer and flushes when the current turn settles. Idle: start now.
3860
3868
  // Persist at enqueue (assign the final message id, save attachments to
3861
3869
  // disk, mirror the queue to pending-sends.json) so a restart before the
3862
3870
  // queue drains can recover the send instead of silently dropping it.
3863
- const id = nanoid(8);
3871
+ //
3872
+ // The id is the SENDER's when it supplied one: the client shows the message
3873
+ // the moment you hit send, and reusing that row's id means the echo lands on
3874
+ // the same row rather than beside it as a duplicate.
3875
+ const id = safeClientMessageId(clientId) ?? nanoid(8);
3864
3876
  const attachments = saveAttachments(ctx.attachmentsDir, id, images);
3865
3877
  state.pendingSends.push({ id, text, attachments });
3866
3878
  persistPendingSends(ctx);
@@ -3947,7 +3959,7 @@ function createRouterQueue(deps) {
3947
3959
  persistPendingSends(ctx);
3948
3960
  maybeStartRouterQueueTurn(ctx);
3949
3961
  return {
3950
- handleUserMessage: (text, images) => handleQueueUserMessage(ctx, text, images),
3962
+ handleUserMessage: (text, images, clientId) => handleQueueUserMessage(ctx, text, images, clientId),
3951
3963
  interruptRouter: () => interruptRouterQueue(ctx),
3952
3964
  cancelQueued: (index) => cancelQueuedSend(ctx, index),
3953
3965
  isRunning: () => ctx.state.routerRunning,
@@ -4087,6 +4099,21 @@ export function createAgentServer(opts) {
4087
4099
  taskStore.acknowledge(id, rejected);
4088
4100
  }
4089
4101
  const wss = new WebSocketServer({ noServer: true });
4102
+ // Ids of user messages already accepted, so a client's resend after a lost
4103
+ // ack is dropped rather than asked twice. Bounded and insertion-ordered: a
4104
+ // resend follows within a reconnect or two, never thousands of messages
4105
+ // later, so the oldest entries are safe to forget.
4106
+ const seenClientIds = new Set();
4107
+ const MAX_SEEN_CLIENT_IDS = 500;
4108
+ function rememberClientId(clientId) {
4109
+ seenClientIds.add(clientId);
4110
+ while (seenClientIds.size > MAX_SEEN_CLIENT_IDS) {
4111
+ const oldest = seenClientIds.values().next().value;
4112
+ if (oldest === undefined)
4113
+ break;
4114
+ seenClientIds.delete(oldest);
4115
+ }
4116
+ }
4090
4117
  function attachClient(socket) {
4091
4118
  clients.add(socket);
4092
4119
  const hello = {
@@ -4123,7 +4150,21 @@ export function createAgentServer(opts) {
4123
4150
  const hasText = typeof msg.text === 'string' && msg.text.trim() !== '';
4124
4151
  const hasImages = Array.isArray(msg.images) && msg.images.length > 0;
4125
4152
  if (msg.type === 'user-message' && (hasText || hasImages)) {
4126
- routerQueue.handleUserMessage(typeof msg.text === 'string' ? msg.text.trim() : '', msg.images);
4153
+ // A client resends anything it never saw acknowledged -- an ack lost
4154
+ // with the socket must not cost the message. So the id is what makes a
4155
+ // resend idempotent: seen once, dropped after, always acknowledged.
4156
+ const clientId = safeClientMessageId(msg.clientId);
4157
+ const duplicate = clientId !== null && seenClientIds.has(clientId);
4158
+ if (!duplicate) {
4159
+ if (clientId !== null)
4160
+ rememberClientId(clientId);
4161
+ routerQueue.handleUserMessage(typeof msg.text === 'string' ? msg.text.trim() : '', msg.images, clientId ?? undefined);
4162
+ }
4163
+ if (clientId !== null) {
4164
+ if (socket.readyState === socket.OPEN) {
4165
+ socket.send(JSON.stringify({ type: 'user-message-ack', clientId }));
4166
+ }
4167
+ }
4127
4168
  }
4128
4169
  else if (msg.type === 'interrupt') {
4129
4170
  routerQueue.interruptRouter();
package/dist/init.js CHANGED
@@ -39,7 +39,7 @@ const DEFAULT_KIT = 'physics-2d';
39
39
  // Registry version of castle-web-sdk to inject when scaffolding from a
40
40
  // globally-installed castle-web (not from inside the workspace). Bumped
41
41
  // alongside cli/sdk version bumps.
42
- const PUBLISHED_SDK_VERSION = '0.4.15';
42
+ const PUBLISHED_SDK_VERSION = '0.4.16';
43
43
  // The account the first-party kits are (to be) published under, so a kit
44
44
  // imported from the CLI's copy is named the same as one fetched from the server.
45
45
  const KIT_AUTHOR = 'castle';
package/dist/serve.js CHANGED
@@ -585,6 +585,16 @@ function startWSServer(port, projectDir, logFile, screenshotsDir, viteHolder) {
585
585
  ? raw.toString('utf8')
586
586
  : Buffer.from(raw).toString('utf8');
587
587
  const msg = JSON.parse(text);
588
+ // Liveness. A client asks so it can tell a live connection from one that
589
+ // died without a close -- the state a sleeping laptop or a dropped
590
+ // network leaves behind, which `readyState` reports as OPEN forever.
591
+ // Answered here rather than with a protocol-level ping because the
592
+ // browser's WebSocket API never exposes those to page JS.
593
+ if (msg.type === 'ping') {
594
+ if (ws.readyState === WebSocket.OPEN)
595
+ ws.send(JSON.stringify({ type: 'pong' }));
596
+ return;
597
+ }
588
598
  if (msg.type === 'restart') {
589
599
  // Fire-and-forget: reload every open tab.
590
600
  invalidateModuleCaches(viteHolder.vite);