@commonlyai/cli 0.1.277 → 0.1.278

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@commonlyai/cli",
3
- "version": "0.1.277",
3
+ "version": "0.1.278",
4
4
  "license": "Apache-2.0",
5
5
  "description": "The Commonly CLI — connect agents, manage pods, iterate fast",
6
6
  "type": "module",
@@ -788,28 +788,28 @@ const parseEvent = (line) => {
788
788
  }
789
789
  };
790
790
 
791
- // The reply is the `result` event's text. A turn that reaches no result event
792
- // (killed mid-stream, exit 0) gets the text of its assistant events, never the
793
- // raw NDJSON. Output with no JSON lines at all is plain text and returned as it
794
- // arrived. A result flagged `is_error` is a failed turn, whatever its exit code,
795
- // so it is raised here, outside the parse guard, and never returned as a reply.
791
+ const STOPPED_TURN = 'the turn stopped before it finished';
792
+
793
+ // The reply is the `result` event's text. A turn with no result event stopped
794
+ // before it finished: SIGINT exits 0 without printing one (claude 2.1.318), with
795
+ // or without assistant text, and before startup it prints nothing at all. That
796
+ // text isn't an answer, since each assistant event may be an interim message on
797
+ // the way to a tool, and empty text would read as a turn with nothing to say.
798
+ // Other output with no JSON lines is plain text and returned as it arrived. A
799
+ // result flagged `is_error` is a failed turn, whatever its exit code, so it is
800
+ // raised here, outside the parse guard, and never returned as a reply.
796
801
  export const claudeResultText = (stdout, { credentials = [] } = {}) => {
797
802
  const text = String(stdout || '');
798
803
  const events = text.split('\n').map(parseEvent);
799
- if (!events.some(Boolean)) return text.trim();
804
+ if (text.trim() && !events.some(Boolean)) return text.trim();
800
805
  for (let i = events.length - 1; i >= 0; i -= 1) {
801
806
  if (events[i]?.type !== 'result') continue;
802
807
  const reply = String(events[i].result ?? '').trim();
803
- if (events[i].is_error === true) throw adapterFailure('claude', reply, { credentials });
808
+ // An interrupted request reports error_during_execution with empty text.
809
+ if (events[i].is_error === true) throw adapterFailure('claude', reply || STOPPED_TURN, { credentials });
804
810
  return reply;
805
811
  }
806
- return events
807
- .filter((event) => event?.type === 'assistant')
808
- .flatMap((event) => event.message?.content || [])
809
- .filter((block) => block?.type === 'text')
810
- .map((block) => block.text)
811
- .join('')
812
- .trim();
812
+ throw adapterFailure('claude', STOPPED_TURN, { credentials });
813
813
  };
814
814
 
815
815
  // TASK-256: what a public seat's own stream says about its Commonly MCP server.
@@ -52,6 +52,7 @@ import { homedir } from 'os';
52
52
  import { dirname, isAbsolute, join } from 'path';
53
53
  import { fileURLToPath } from 'url';
54
54
  import {
55
+ adapterFailure,
55
56
  readUpstreamRefusal,
56
57
  spawnCredentials,
57
58
  } from '../upstream-refusal.js';
@@ -450,10 +451,18 @@ export const buildArgs = ({
450
451
  prompt,
451
452
  ];
452
453
 
453
- /** The reply is the last assistant `message_end`'s text parts; tool calls are not text. */
454
+ /**
455
+ * The reply is the last assistant `message_end`'s text parts; tool calls are not
456
+ * text. `finished` means pi ended the turn on an answer: `agent_end`, not retried,
457
+ * after an assistant message that stopped with `stop`. An overflow's `agent_end`
458
+ * says `willRetry: false` and then pi compacts and continues, so `agent_end` after
459
+ * an `error` or `length` stop doesn't count; the continuation's message decides.
460
+ */
454
461
  export const extractReply = (stdout) => {
455
462
  let text = '';
456
463
  let sawAssistant = false;
464
+ let finished = false;
465
+ let lastStop = null;
457
466
  const errors = [];
458
467
  for (const line of String(stdout).split('\n')) {
459
468
  if (!line.trim()) continue;
@@ -462,10 +471,33 @@ export const extractReply = (stdout) => {
462
471
  if (event?.type === 'message_end' && event.message?.role === 'assistant') {
463
472
  const parts = (event.message.content || []).filter((c) => c?.type === 'text').map((c) => c.text || '');
464
473
  if (parts.length) { text = parts.join('\n').trim(); sawAssistant = true; }
474
+ lastStop = event.message.stopReason;
475
+ finished = false;
465
476
  }
477
+ if (event?.type === 'agent_end') finished = event.willRetry !== true && lastStop === 'stop';
466
478
  if (event?.type === 'error') errors.push(String(event.message || event.error || 'error'));
467
479
  }
468
- return { text, sawAssistant, errors };
480
+ return { text, sawAssistant, finished, errors };
481
+ };
482
+
483
+ // A turn that printed an interim message and then ended without finishing. A
484
+ // crash keeps its cause, so a quota line in stderr or a refusal on the stream
485
+ // still classifies. A signal (143, 129, or a null code) has neither: stderr then
486
+ // holds pi's startup warnings, and a refusal on the stream may be one pi was
487
+ // recovering from, like an overflow's 400.
488
+ const stoppedTurnFailure = (reply, {
489
+ code, signal, stderr, credentials,
490
+ }) => {
491
+ const killed = Boolean(signal) || code === 143 || code === 129;
492
+ const upstream = killed ? null : reply.upstream;
493
+ const failure = adapterFailure(
494
+ 'pi',
495
+ reply.errors.join(' | ') || (killed ? '' : stderr.trim().slice(-600)) || upstream?.detail
496
+ || 'the turn stopped before it finished',
497
+ { credentials, exitCode: code, limit: 600 },
498
+ );
499
+ if (upstream) failure.status ??= upstream.status;
500
+ return failure;
469
501
  };
470
502
 
471
503
  /**
@@ -538,7 +570,7 @@ const runPi = ({
538
570
  });
539
571
  proc.stderr.on('data', (chunk) => { stderr += chunk.toString(); });
540
572
  proc.on('error', (err) => { clearTimeout(timer); reject(err); });
541
- proc.on('close', (code) => {
573
+ proc.on('close', (code, signal) => {
542
574
  clearTimeout(timer);
543
575
  if (hookError) return reject(hookError);
544
576
  if (timedOut) return reject(new Error(`pi timed out after ${timeoutMs}ms`));
@@ -548,7 +580,16 @@ const runPi = ({
548
580
  // on the stream rather than on the exit code (TASK-096, measured 2026-09-22).
549
581
  const upstream = readUpstreamRefusal(stdout, { credentials });
550
582
  if (upstream) reply.upstream = upstream;
551
- if (code !== 0 && !reply.sawAssistant) {
583
+ // Without `finished`, the text pi printed was an interim message, so posting
584
+ // it would answer with half a turn and acknowledge what it held. An error
585
+ // event fails the turn even after `finished`, which could otherwise read as
586
+ // silent. A tool-only turn that finished stays silent, whatever the exit.
587
+ if (code !== 0 && (!reply.finished || reply.errors.length)) {
588
+ if (reply.sawAssistant) {
589
+ return reject(stoppedTurnFailure(reply, {
590
+ code, signal, stderr, credentials,
591
+ }));
592
+ }
552
593
  const tail = (reply.errors.join(' | ') || stderr).trim().slice(-600);
553
594
  return reject(new Error(`pi exited ${code}: ${tail}`));
554
595
  }