castle-web-cli 0.4.56 → 0.4.58

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.
@@ -1,6 +1,7 @@
1
1
  export interface PromptMessage {
2
2
  role: "user" | "assistant";
3
3
  text: string;
4
+ interrupted?: boolean;
4
5
  }
5
6
  export interface PromptTask {
6
7
  id: string;
@@ -16,7 +17,7 @@ export declare function buildRouterPrompt(opts: {
16
17
  instruction: string;
17
18
  }): string;
18
19
  export declare function userTurnInstruction(opts: {
19
- text: string;
20
+ messages: string[];
20
21
  interruptedDraft?: string;
21
22
  attachments?: string[];
22
23
  }): string;
@@ -61,7 +61,14 @@ function renderTranscript(messages) {
61
61
  if (recent.length === 0)
62
62
  return "(no conversation yet)";
63
63
  return recent
64
- .map((m) => `${m.role === "user" ? "user" : "you"}: ${m.text}`)
64
+ .map((m) => {
65
+ if (m.role === "user")
66
+ return `user: ${m.text}`;
67
+ const label = m.interrupted
68
+ ? "you (interrupted draft -- not a complete reply)"
69
+ : "you";
70
+ return `${label}: ${m.text}`;
71
+ })
65
72
  .join("\n\n");
66
73
  }
67
74
  function renderTasks(tasks) {
@@ -94,9 +101,17 @@ Reply now, as "you" in the conversation. Plain reply text only -- no role prefix
94
101
  export function userTurnInstruction(opts) {
95
102
  const parts = [];
96
103
  if (opts.interruptedDraft?.trim()) {
97
- parts.push(`Your previous reply was interrupted mid-stream by the user's new message. Its draft so far (already shown to the user; NO tasks were spawned from it):\n\n${opts.interruptedDraft.trim()}\n\nContinue that line of work AND address the new message -- do both in this one reply, spawning whatever tasks are needed for either.`);
104
+ parts.push(`Your previous reply was interrupted mid-stream by the user's new message. Its draft so far (already shown to the user; NO tasks were spawned from it):\n\n${opts.interruptedDraft.trim()}\n\nContinue that line of work AND address the new message(s) -- do it all in this one reply, spawning whatever tasks are needed.`);
105
+ }
106
+ const msgs = opts.messages.filter((m) => m.trim());
107
+ if (msgs.length <= 1) {
108
+ parts.push(`The user just said:\n\n${msgs[0] ?? ""}`);
109
+ }
110
+ else {
111
+ parts.push(`The user sent these messages -- the earlier ones arrived while you were mid-reply and have NOT been answered yet. Address ALL of them in this one reply, in order. Exception: if a later message clearly clarifies, corrects, or takes back an earlier one, follow the later intent and don't redo work the user reversed. By default, cover every message:\n\n${msgs
112
+ .map((t, i) => `${i + 1}. ${t}`)
113
+ .join("\n\n")}`);
98
114
  }
99
- parts.push(`The user just said:\n\n${opts.text}`);
100
115
  if (opts.attachments && opts.attachments.length > 0) {
101
116
  parts.push(`The user attached image file(s), saved in the deck at: ${opts.attachments.join(", ")}. Open them with your read tool and take them into account; pass the paths along to task agents that need them.`);
102
117
  }
package/dist/agent.js CHANGED
@@ -78,6 +78,10 @@ function buildAgentInvocation(backend, role, prompt, claudeModel) {
78
78
  const ROUTER_TIMEOUT_MS = 3 * 60_000;
79
79
  const TASK_TIMEOUT_MS = 30 * 60_000;
80
80
  const MAX_TASK_ATTEMPTS = 3;
81
+ // Cap on task agents running at once -- keeps us under e2b / provider rate
82
+ // limits. Over-cap tasks stay queued ('waiting') and start, earliest-created
83
+ // first, as running ones finish. Conservative default; override via env.
84
+ const MAX_CONCURRENT_TASKS = Number(process.env.CASTLE_MAX_CONCURRENT_TASKS) || 4;
81
85
  const TASK_POLL_MS = 1_000;
82
86
  const FENCE_HOLDBACK = '```castle-';
83
87
  const RESULT_SUMMARY_CHARS = 600;
@@ -497,9 +501,21 @@ function createTaskStore(opts) {
497
501
  return !dep || isTerminal(dep.status);
498
502
  });
499
503
  }
504
+ function runningCount() {
505
+ let n = 0;
506
+ for (const t of tasks.values())
507
+ if (t.status === 'running')
508
+ n++;
509
+ return n;
510
+ }
500
511
  function maybeStart(task) {
501
512
  if (task.status !== 'waiting' || task.acknowledged || !depsAreSettled(task))
502
513
  return;
514
+ // Concurrency cap: at most MAX_CONCURRENT_TASKS agents run at once. Over-cap
515
+ // tasks stay 'waiting' and are restarted -- earliest-created first -- by the
516
+ // onFinished sweep below when a running task frees a slot.
517
+ if (runningCount() >= MAX_CONCURRENT_TASKS)
518
+ return;
503
519
  start(task);
504
520
  }
505
521
  function start(task) {
@@ -540,7 +556,9 @@ function createTaskStore(opts) {
540
556
  : `${result.error ?? 'failed'}\n${result.finalText.slice(-RESULT_SUMMARY_CHARS)}`;
541
557
  touch(task);
542
558
  opts.onFinished(task);
543
- for (const waiting of tasks.values())
559
+ // A slot just freed -- restart eligible waiting tasks, earliest-created
560
+ // first, up to the concurrency cap.
561
+ for (const waiting of sorted())
544
562
  maybeStart(waiting);
545
563
  });
546
564
  }
@@ -775,7 +793,11 @@ function runRouterTurnIn(ctx, instruction) {
775
793
  deckLabel: ctx.deckLabel,
776
794
  messages: ctx.log.messages
777
795
  .filter((m) => m.role !== 'log' && m.id !== message.id && m.status !== 'streaming')
778
- .map((m) => ({ role: m.role, text: m.text })),
796
+ .map((m) => ({
797
+ role: m.role,
798
+ text: m.text,
799
+ interrupted: m.interrupted,
800
+ })),
779
801
  // Only the live board -- match what the user sees. Hide tasks that are
780
802
  // BOTH acknowledged AND finished; an active (running/waiting) task always
781
803
  // shows even if somehow acked, so nothing can ever go invisible mid-work.
@@ -1018,6 +1040,20 @@ export function createAgentServer(opts) {
1018
1040
  claudeModel: () => settings.claudeModel,
1019
1041
  }, instruction);
1020
1042
  }
1043
+ // User messages awaiting a reply: everything since the last COMPLETED (done,
1044
+ // not interrupted) assistant message. Usually just the latest, but a rapid
1045
+ // burst leaves several queued -- all must be addressed, not only the last.
1046
+ function pendingUserMessages() {
1047
+ let lastAnswered = -1;
1048
+ for (let i = messages.length - 1; i >= 0; i--) {
1049
+ const m = messages[i];
1050
+ if (m.role === 'assistant' && m.status === 'done' && !m.interrupted) {
1051
+ lastAnswered = i;
1052
+ break;
1053
+ }
1054
+ }
1055
+ return messages.slice(lastAnswered + 1).filter((m) => m.role === 'user');
1056
+ }
1021
1057
  function handleUserMessage(text, images) {
1022
1058
  userEpoch += 1;
1023
1059
  const interruptedDraft = interruptRouterRuns();
@@ -1033,10 +1069,15 @@ export function createAgentServer(opts) {
1033
1069
  if (attachments.length > 0)
1034
1070
  message.attachments = attachments;
1035
1071
  log.add(message);
1072
+ // Address every still-unanswered user message (this one plus any earlier
1073
+ // burst messages that interrupted prior turns), not just the latest.
1074
+ const pending = pendingUserMessages();
1036
1075
  runRouterTurn(userTurnInstruction({
1037
- text,
1076
+ messages: pending.map((m) => m.text),
1038
1077
  interruptedDraft: interruptedDraft || undefined,
1039
- attachments: attachments.map((name) => path.join('.castle', 'agent', 'attachments', name)),
1078
+ attachments: pending
1079
+ .flatMap((m) => m.attachments ?? [])
1080
+ .map((name) => path.join('.castle', 'agent', 'attachments', name)),
1040
1081
  }));
1041
1082
  }
1042
1083
  function handleTaskAck(id, rejected) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "castle-web-cli",
3
- "version": "0.4.56",
3
+ "version": "0.4.58",
4
4
  "type": "module",
5
5
  "bin": {
6
6
  "castle-web": "./dist/index.js"