@fullstackhouse/cerber 0.40.0 → 0.41.0-pr.76.2fef79e

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.
@@ -340,6 +340,8 @@ export const ChatTurnSchema = z.object({
340
340
  /** Edits this turn asked for but the artifact would not accept. */
341
341
  refused: z.array(RefusalSchema).default([]),
342
342
  costUsd: z.number().nullable().default(null),
343
+ /** A user turn sent while the previous one was still being answered. */
344
+ steer: z.boolean().optional(),
343
345
  });
344
346
  /**
345
347
  * The chat turn being answered right now — or the one that failed.
@@ -361,6 +363,8 @@ export const PendingChatSchema = z.object({
361
363
  * only honest account of them; it is thrown away when the answer lands.
362
364
  */
363
365
  progress: z.array(z.string()).default([]),
366
+ /** What the user added while it worked — each one went into the live run. */
367
+ steers: z.array(z.string()).default([]),
364
368
  /** Why the turn failed. Null while it is still running. */
365
369
  error: z.string().nullable().default(null),
366
370
  });
@@ -75,6 +75,25 @@ async function isCheckedOut(dir) {
75
75
  .then((s) => s.isDirectory())
76
76
  .catch(() => false);
77
77
  }
78
+ /** The live turns that can still take a message, by artifact id. */
79
+ const steerable = new Map();
80
+ /**
81
+ * Add a message to the turn being answered for this review, mid-run.
82
+ *
83
+ * False when there is no turn to add it to — none running, it is retrying its
84
+ * JSON, or it has already answered — so the caller can say "send it as the next
85
+ * message" rather than claim it landed.
86
+ */
87
+ export function steerChatTurn(artifactId, message) {
88
+ return steerable.get(artifactId)?.(message) ?? false;
89
+ }
90
+ function steerPrompt(message) {
91
+ return `The user adds, while you work on this turn:
92
+
93
+ ${message}
94
+
95
+ Take it into account. Your answer is still the one JSON object described earlier, with "reply" and "revisions" — and the reply should say what you did about this.`;
96
+ }
78
97
  /**
79
98
  * Append the user's message and the reviewer's answer, applying whatever the
80
99
  * answer revised. Claims the artifact's inflight slot for the duration: the
@@ -142,11 +161,36 @@ async function turn(artifact, message, opts) {
142
161
  isolateWorkspace: !(source && trusted),
143
162
  resumeSessionId,
144
163
  };
164
+ // Steers in the order they went in, stamped as they did: each is its own user
165
+ // turn in the transcript, between the question and the answer.
166
+ const steers = [];
167
+ const onSteer = (send) => {
168
+ if (!send) {
169
+ steerable.delete(artifact.id);
170
+ return;
171
+ }
172
+ steerable.set(artifact.id, (text) => {
173
+ if (!send(steerPrompt(text)))
174
+ return false;
175
+ steers.push({
176
+ id: newId(),
177
+ role: "user",
178
+ at: now(),
179
+ body: text,
180
+ refs: [],
181
+ revisions: [],
182
+ refused: [],
183
+ costUsd: null,
184
+ steer: true,
185
+ });
186
+ return true;
187
+ });
188
+ };
145
189
  let reply;
146
190
  let costUsd;
147
191
  let sessionId;
148
192
  try {
149
- const first = await run(prompt, claudeOpts);
193
+ const first = await run(prompt, { ...claudeOpts, onSteer });
150
194
  try {
151
195
  reply = AiChatTurnSchema.parse(extractJson(first.text));
152
196
  costUsd = first.costUsd;
@@ -168,6 +212,7 @@ async function turn(artifact, message, opts) {
168
212
  }
169
213
  }
170
214
  finally {
215
+ steerable.delete(artifact.id);
171
216
  await removeRunDir(empty);
172
217
  }
173
218
  // The first turn snapshots the review, so "reset" always has somewhere to go
@@ -204,7 +249,7 @@ async function turn(artifact, message, opts) {
204
249
  ...revised.artifact,
205
250
  updatedAt: assistantTurn.at,
206
251
  preChat,
207
- chat: [...artifact.chat, userTurn, assistantTurn],
252
+ chat: [...artifact.chat, userTurn, ...steers, assistantTurn],
208
253
  run: artifact.run ? { ...artifact.run, sessionId: sessionId ?? artifact.run.sessionId } : null,
209
254
  },
210
255
  applied: revised.applied,
@@ -46,6 +46,10 @@ export function buildClaudeArgs(opts) {
46
46
  // and everything before it is the run saying what it is doing. `--verbose` is
47
47
  // not optional — the CLI refuses the combination without it.
48
48
  const args = ["-p", "--output-format", "stream-json", "--verbose"];
49
+ // Echoing our own messages back is how a run says it took a steer — the only
50
+ // way to tell one that went in from one still sitting in the pipe.
51
+ if (opts.onSteer)
52
+ args.push("--input-format", "stream-json", "--replay-user-messages");
49
53
  if (opts.resumeSessionId)
50
54
  args.push("--resume", opts.resumeSessionId);
51
55
  if (opts.model)
@@ -126,14 +130,40 @@ export function runClaude(prompt, opts = {}) {
126
130
  child.stdout.destroy();
127
131
  child.stderr.destroy();
128
132
  child.stdin.destroy();
133
+ stopSteering();
129
134
  settle(() => reject(new Error(`claude did not finish within ${Math.round(timeoutMs / 60_000)} minutes — run killed.`)));
130
135
  }, timeoutMs);
136
+ // Messages written vs echoed back. stdin closes on a result only once every
137
+ // steer has been taken: one that lands as the answer is being written starts
138
+ // a second pass, and closing on the first result would end the run with the
139
+ // steer unread. The last result is the answer either way.
140
+ let written = 0;
141
+ let echoed = 0;
142
+ const steering = Boolean(opts.onSteer);
143
+ const userMessage = (text) => JSON.stringify({ type: "user", message: { role: "user", content: text } }) + "\n";
144
+ let open = steering;
145
+ const stopSteering = () => {
146
+ if (!open)
147
+ return;
148
+ open = false;
149
+ opts.onSteer?.(null);
150
+ };
151
+ const closeInput = () => {
152
+ if (!open)
153
+ return;
154
+ stopSteering();
155
+ child.stdin.end();
156
+ };
131
157
  const ingest = (chunk) => {
132
158
  const read = readEvents(unread + chunk);
133
159
  unread = read.rest;
134
160
  for (const event of read.events) {
135
161
  if (event.type === "result")
136
162
  result = event;
163
+ if (steering && event.type === "user" && isEchoedMessage(event))
164
+ echoed++;
165
+ if (steering && event.type === "result" && echoed >= written)
166
+ closeInput();
137
167
  // A throwing listener must not take the run down with it: the answer is
138
168
  // the point, the narration is not.
139
169
  try {
@@ -150,9 +180,11 @@ export function runClaude(prompt, opts = {}) {
150
180
  });
151
181
  child.stderr.on("data", (d) => (stderr += d));
152
182
  child.on("error", (err) => {
183
+ stopSteering();
153
184
  settle(() => reject(new Error(`Failed to start claude CLI: ${err.message}. Is Claude Code installed and on PATH?`)));
154
185
  });
155
186
  child.on("close", (code) => {
187
+ stopSteering();
156
188
  if (settled)
157
189
  return;
158
190
  clearTimeout(timer);
@@ -177,10 +209,32 @@ export function runClaude(prompt, opts = {}) {
177
209
  sessionId: typeof wrapper.session_id === "string" ? wrapper.session_id : null,
178
210
  });
179
211
  });
180
- child.stdin.write(prompt);
181
- child.stdin.end();
212
+ // A run that dies early closes the pipe under a write; the close handler
213
+ // says why, so the EPIPE itself has nothing to add.
214
+ child.stdin.on("error", () => { });
215
+ if (!steering) {
216
+ child.stdin.write(prompt);
217
+ child.stdin.end();
218
+ return;
219
+ }
220
+ written++;
221
+ child.stdin.write(userMessage(prompt));
222
+ opts.onSteer((text) => {
223
+ if (!open)
224
+ return false;
225
+ written++;
226
+ child.stdin.write(userMessage(text));
227
+ return true;
228
+ });
182
229
  });
183
230
  }
231
+ /** A user event carrying our own words back, as opposed to a tool's result. */
232
+ function isEchoedMessage(event) {
233
+ const content = event.message?.content;
234
+ if (typeof content === "string")
235
+ return true;
236
+ return Array.isArray(content) && content.length > 0 && content.every((b) => b?.type === "text");
237
+ }
184
238
  /** Extract a JSON object from model output that may be wrapped in fences or prose. */
185
239
  export function extractJson(text) {
186
240
  const trimmed = text.trim();
@@ -21,7 +21,7 @@ import { checkAlreadyRaised, claimRaisedCheck, finishRaisedCheck, isRaisedCheckR
21
21
  import { listArtifacts, loadArtifact, loadArtifactByKey, noteHistory, saveArtifact, updateArtifactByKey, } from "../core/state.js";
22
22
  import { isReviewRunning } from "../runner/inflight.js";
23
23
  import { reviewPr } from "../runner/review.js";
24
- import { runChatTurn } from "../runner/chat.js";
24
+ import { runChatTurn, steerChatTurn } from "../runner/chat.js";
25
25
  import { mergeConcurrentEdits, restoreReview } from "../core/revise.js";
26
26
  import { progressWriter } from "./progress.js";
27
27
  import { ChatTurnSchema } from "../core/artifact.js";
@@ -731,6 +731,7 @@ export async function buildApp(opts) {
731
731
  refs: request.refs ?? [],
732
732
  startedAt: new Date().toISOString(),
733
733
  progress: [],
734
+ steers: [],
734
735
  error: null,
735
736
  },
736
737
  }));
@@ -781,6 +782,29 @@ export async function buildApp(opts) {
781
782
  });
782
783
  return c.json(pending, 202);
783
784
  });
785
+ // Add to the turn being answered rather than waiting minutes to say "not that,
786
+ // this". The message goes into the live run; when the turn has already
787
+ // answered (or is only retrying its JSON) there is nothing to add it to, and
788
+ // the cockpit is told so instead of being shown a message that went nowhere.
789
+ const SteerRequestSchema = z.object({ message: z.string().min(1) });
790
+ app.post("/api/reviews/:key/chat/steer", async (c) => {
791
+ const key = c.req.param("key");
792
+ const artifact = await loadArtifactByKey(key);
793
+ if (!artifact)
794
+ return c.json({ error: "not found" }, 404);
795
+ let request;
796
+ try {
797
+ request = SteerRequestSchema.parse(await c.req.json());
798
+ }
799
+ catch (err) {
800
+ return c.json({ error: err instanceof Error ? err.message : String(err) }, 400);
801
+ }
802
+ if (!turnInFlight(artifact) || !steerChatTurn(artifact.id, request.message)) {
803
+ return c.json({ error: "that turn is no longer taking messages — send this as the next one" }, 409);
804
+ }
805
+ const updated = await updateArtifactByKey(key, (a) => a.pendingChat ? { ...a, pendingChat: { ...a.pendingChat, steers: [...a.pendingChat.steers, request.message] } } : a);
806
+ return c.json(updated);
807
+ });
784
808
  // Clear a turn that failed. Only that: a turn still being answered would come
785
809
  // back on the next write and the cockpit would have lied about dropping it.
786
810
  app.delete("/api/reviews/:key/chat/pending", async (c) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fullstackhouse/cerber",
3
- "version": "0.40.0",
3
+ "version": "0.41.0-pr.76.2fef79e",
4
4
  "description": "AI code-review cockpit: Claude reviews your PRs into local artifacts; nothing reaches GitHub until you say so.",
5
5
  "license": "MIT",
6
6
  "keywords": [
@@ -55,6 +55,7 @@
55
55
  "@types/react": "^19.0.4",
56
56
  "@types/react-dom": "^19.0.2",
57
57
  "@vitejs/plugin-react": "^4.3.4",
58
+ "conventional-changelog-conventionalcommits": "^9.3.1",
58
59
  "dompurify": "^3.4.13",
59
60
  "highlight.js": "^11.12.0",
60
61
  "marked": "^18.0.10",