@in-the-loop-labs/pair-review 5.3.2 → 5.3.4

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.
@@ -76,6 +76,12 @@ class PiBridge extends EventEmitter {
76
76
  // Accumulate text across streaming deltas for each turn
77
77
  this._accumulatedText = '';
78
78
  this._inMessage = false;
79
+ // OMP pause tracking (see _handleAgentEnd). _continuing: a non-terminal
80
+ // agent_end arrived and the run's terminal agent_end has not, so the agent
81
+ // still owns the turn. _paused: the part of that window before the resumed
82
+ // run's agent_start, while OMP has no agent loop running.
83
+ this._continuing = false;
84
+ this._paused = false;
79
85
  // Pending callbacks for RPC responses keyed by command type
80
86
  this._pendingCallbacks = new Map();
81
87
  }
@@ -133,11 +139,18 @@ class PiBridge extends EventEmitter {
133
139
 
134
140
  if (!this._closing) {
135
141
  logger.warn(`[${this.logName}] Process exited unexpectedly (code=${code}, signal=${signal})`);
142
+ // A paused run's reply up to the pause is finished text that OMP
143
+ // reported with an agent_end; save it rather than lose it with the
144
+ // process. No terminal agent_end can follow now.
145
+ if (this._continuing) this._handleAgentEnd({});
136
146
  this.emit('error', { error: new Error(`${this.cliName} process exited (code=${code}, signal=${signal})`) });
137
147
  } else {
138
148
  logger.info(`[${this.logName}] Process exited (code=${code}, signal=${signal})`);
139
149
  }
140
150
 
151
+ this._inMessage = false;
152
+ this._continuing = false;
153
+ this._paused = false;
141
154
  this.emit('close');
142
155
  });
143
156
 
@@ -206,6 +219,14 @@ class PiBridge extends EventEmitter {
206
219
  const command = JSON.stringify({ type: 'abort' });
207
220
  logger.debug(`[${this.logName}] Sending abort`);
208
221
  this._write(command);
222
+ // OMP answers an abort during a pause with no agent_end: no agent loop is
223
+ // running for it to stop. End the paused run here so its reply so far is
224
+ // saved and 'complete' fires. Once the run has resumed (agent_start), OMP
225
+ // ends the aborted loop with its own terminal agent_end instead.
226
+ if (this._paused) {
227
+ logger.debug(`[${this.logName}] Abort during a pause; ending the run`);
228
+ this._handleAgentEnd({});
229
+ }
209
230
  }
210
231
 
211
232
  /**
@@ -216,6 +237,10 @@ class PiBridge extends EventEmitter {
216
237
  if (!this._process) return;
217
238
 
218
239
  this._closing = true;
240
+ // Emit the paused reply while the session manager is still listening,
241
+ // so its complete handler saves it to chat_messages. The process close
242
+ // event cannot: listeners are gone by then, and _closing skips it.
243
+ if (this._continuing) this._handleAgentEnd({});
219
244
  this.removeAllListeners();
220
245
 
221
246
  // Try to abort any in-flight work first
@@ -262,11 +287,14 @@ class PiBridge extends EventEmitter {
262
287
  }
263
288
 
264
289
  /**
265
- * Check if the bridge is currently processing a message.
290
+ * Check if the bridge is currently processing a message. A run paused by a
291
+ * non-terminal agent_end stays busy until its terminal agent_end: a prompt
292
+ * sent in the gap would start a new turn and drop the reply accumulated so
293
+ * far, which has not been saved yet.
266
294
  * @returns {boolean}
267
295
  */
268
296
  isBusy() {
269
- return this._inMessage;
297
+ return this._inMessage || this._continuing;
270
298
  }
271
299
 
272
300
  // ---------------------------------------------------------------------------
@@ -462,6 +490,10 @@ class PiBridge extends EventEmitter {
462
490
  callback(event);
463
491
  } else if (!event.success) {
464
492
  logger.error(`[${this.logName}] Command failed: ${event.error}`);
493
+ // OMP reports a failed prompt after the run's last agent_end. If
494
+ // that was a pause, nothing may resume it, and a run left paused
495
+ // keeps the bridge busy for good.
496
+ if (event.command === 'prompt' && this._paused) this._handleAgentEnd({});
465
497
  this.emit('error', { error: new Error(event.error || 'Unknown command error') });
466
498
  } else {
467
499
  logger.debug(`[${this.logName}] Command acknowledged: ${JSON.stringify(event).substring(0, 200)}`);
@@ -469,6 +501,11 @@ class PiBridge extends EventEmitter {
469
501
  break;
470
502
 
471
503
  case 'agent_start':
504
+ // A paused OMP run resumes in a new agent loop (see _handleAgentEnd)
505
+ this._paused = false;
506
+ logger.debug(`[${this.logName}] ${type}`);
507
+ this.emit('status', { status: 'working' });
508
+ break;
472
509
  case 'turn_start':
473
510
  logger.debug(`[${this.logName}] ${type}`);
474
511
  this.emit('status', { status: 'working' });
@@ -486,10 +523,20 @@ class PiBridge extends EventEmitter {
486
523
  break;
487
524
 
488
525
  default:
489
- logger.debug(`[${this.logName}] Unhandled event type: ${type}`);
526
+ this._handleOtherEvent(event);
490
527
  }
491
528
  }
492
529
 
530
+ /**
531
+ * Handle an event type the shared switch in _handleLine does not recognize.
532
+ * Subclasses override this for CLI-specific events (see OmpBridge) and fall
533
+ * back to super for anything they do not recognize either.
534
+ * @param {Object} event - The parsed event
535
+ */
536
+ _handleOtherEvent(event) {
537
+ logger.debug(`[${this.logName}] Unhandled event type: ${event.type}`);
538
+ }
539
+
493
540
  /**
494
541
  * Handle a message_update event containing streaming content deltas.
495
542
  * @param {Object} event - The message_update event
@@ -547,12 +594,32 @@ class PiBridge extends EventEmitter {
547
594
  /**
548
595
  * Handle agent_end event which signals the completion of the agent's work.
549
596
  * Emits 'complete' with the full accumulated text.
550
- * @param {Object} _event - The agent_end event
597
+ *
598
+ * OMP marks a scheduling pause with `isTerminal: false` — an async job
599
+ * result, a queued advisor steer, or a compaction/retry continuation will
600
+ * resume the loop, and a terminal agent_end follows. Completing on the pause
601
+ * would persist a truncated reply and finalize the chat UI while the agent
602
+ * keeps working, so keep accumulating instead. Pi never sets the field, so
603
+ * every Pi agent_end is terminal.
604
+ *
605
+ * The pause stays open (isBusy) until the terminal agent_end, or until
606
+ * abort(), a failed prompt, close(), or process exit ends it through this
607
+ * method.
608
+ * @param {Object} event - The agent_end event
551
609
  */
552
- _handleAgentEnd(_event) {
610
+ _handleAgentEnd(event) {
611
+ if (event?.isTerminal === false) {
612
+ this._continuing = true;
613
+ this._paused = true;
614
+ logger.debug(`[${this.logName}] Non-terminal agent_end; waiting for the agent to resume`);
615
+ return;
616
+ }
617
+
553
618
  const fullText = this._accumulatedText;
554
619
  this._accumulatedText = '';
555
620
  this._inMessage = false;
621
+ this._continuing = false;
622
+ this._paused = false;
556
623
 
557
624
  logger.debug(`[${this.logName}] Agent ended, accumulated ${fullText.length} chars`);
558
625
  this.emit('complete', { fullText });
@@ -33,6 +33,9 @@ const CHAT_TOOLS = 'read,bash,grep,find,ls';
33
33
  // comment block in src/ai/omp-provider.js for detail.
34
34
  const OMP_CHAT_TOOLS = 'read,bash,grep,glob,task';
35
35
 
36
+ /** Error code sendMessage() sets when the agent still owns the previous turn. */
37
+ const SESSION_BUSY = 'SESSION_BUSY';
38
+
36
39
  class ChatSessionManager {
37
40
  /**
38
41
  * @param {Database} db - better-sqlite3 database instance
@@ -165,7 +168,10 @@ class ChatSessionManager {
165
168
  }
166
169
 
167
170
  if (session.bridge.isBusy()) {
168
- throw new Error(`Session ${sessionId} is currently processing a message`);
171
+ // Checked before the message is stored, so a rejected send leaves no trace.
172
+ const busy = new Error(`Session ${sessionId} is currently processing a message`);
173
+ busy.code = SESSION_BUSY;
174
+ throw busy;
169
175
  }
170
176
 
171
177
  // Build the message for the agent: initialContext > context > userMessage
@@ -844,3 +850,4 @@ class ChatSessionManager {
844
850
  }
845
851
 
846
852
  module.exports = ChatSessionManager;
853
+ module.exports.SESSION_BUSY = SESSION_BUSY;
package/src/main.js CHANGED
@@ -231,9 +231,8 @@ OPTIONS:
231
231
  Available models: opus, sonnet, haiku (Claude Code);
232
232
  also: fable-5.1-xhigh, fable-5.1-high,
233
233
  fable-5-xhigh, fable-5-high, opus-5.5-xhigh,
234
- opus-5.5-high, opus-5-xhigh, opus-5-high,
235
- opus-4.8-xhigh, opus-4.8-high, opus-4.7-xhigh,
236
- opus-4.7-high, opus-4.6-high, opus-4.6-1m,
234
+ opus-5.5-high, opus-5.5-medium, opus-5.5-low,
235
+ opus-5-xhigh, opus-5-high,
237
236
  sonnet-5-xhigh, sonnet-5-high, sonnet-4.6
238
237
  (default is opus-5.5-high; opus is Opus 5.5 XHigh;
239
238
  a saved default_model wins over the default)
@@ -23,6 +23,7 @@ const { buildChatStartedPayload, buildChatResumedPayload, buildChatHookContext,
23
23
  const { resolveFormat } = require('../utils/comment-formatter');
24
24
  const { getAllChatProviders, getAllCachedChatAvailability } = require('../chat/chat-providers');
25
25
  const { getChatModelCatalog, canonicalChatModel } = require('../chat/chat-models');
26
+ const { SESSION_BUSY } = require('../chat/session-manager');
26
27
  const { resolveLoadSkills } = require('../config');
27
28
 
28
29
  /**
@@ -511,6 +512,15 @@ router.post('/api/chat/session/:id/message', async (req, res) => {
511
512
  logger.debug(`[ChatRoute] Message stored as ID ${result.id}, awaiting agent response via WebSocket`);
512
513
  res.json({ data: { messageId: result.id } });
513
514
  } catch (error) {
515
+ if (error.code === SESSION_BUSY) {
516
+ // The agent still owns its previous turn — e.g. an OMP run paused on a
517
+ // background job, seen from a page reloaded mid-reply. The message was
518
+ // not stored, so the client can send it again once the reply finishes.
519
+ logger.warn(`Chat message rejected: ${error.message}`);
520
+ return res.status(409).json({
521
+ error: 'The agent is still working on its previous reply. Wait for it to finish, then send your message again.'
522
+ });
523
+ }
514
524
  logger.error(`Error sending chat message: ${error.message}`);
515
525
  res.status(500).json({ error: 'Failed to send message' });
516
526
  }