@commonlyai/cli 0.1.267 → 0.1.268

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.267",
3
+ "version": "0.1.268",
4
4
  "license": "Apache-2.0",
5
5
  "description": "The Commonly CLI — connect agents, manage pods, iterate fast",
6
6
  "type": "module",
@@ -130,6 +130,7 @@ import {
130
130
  refusedItemRef,
131
131
  sanitizeHandoffAgent,
132
132
  } from '../lib/content-refusal.js';
133
+ import { CANT_ANSWER_NOTICE_WINDOW_MS, cantAnswerNotice } from '../lib/turn-failure-notice.js';
133
134
  import {
134
135
  ADDRESSED_EVENT_TYPES,
135
136
  CASCADE_DEFAULTS,
@@ -742,6 +743,12 @@ const PROMPT_EVENT_TYPES = new Set([
742
743
  // event that exists to introduce the seat rather than to answer anyone, so it
743
744
  // is the only one worth holding back.
744
745
  const DEFERRABLE_SOLO_EVENT_TYPES = new Set(['first_contact']);
746
+ // Someone asked this seat for an answer: by type, by the server's stamp, or by
747
+ // replying to it. A batch turn carries the same answer for its items.
748
+ const someoneIsWaiting = (event) => ADDRESSED_EVENT_TYPES.has(event.type)
749
+ || event.payload?.addressed === true
750
+ || event.payload?.repliesToYourMessage === true
751
+ || event.payload?.someoneWaiting === true;
745
752
 
746
753
  // How long a fresh seat's greeting waits for a person before it runs alone.
747
754
  // One poll lost the race on the 0.1.257 walk (2026-11-05): the mention landed
@@ -2369,6 +2376,48 @@ export const performRun = ({
2369
2376
  }
2370
2377
  };
2371
2378
 
2379
+ // A failure only a person can fix, or only time can, leaves a mention
2380
+ // unanswered for as long as it lasts, so each pod with someone waiting is told
2381
+ // why, at most once per cause per pod per hour (persisted, so a restart does
2382
+ // not re-post). A later pod the failed turn stopped before reaching is told
2383
+ // too: a key or an allowance fails every pod alike, and its own turn could be
2384
+ // 45 minutes away. A notice that fails to post is logged and tried at the next
2385
+ // failure; the retry goes ahead either way.
2386
+ const noticeCantAnswer = async (error, waitingEvents) => {
2387
+ const notice = cantAnswerNotice(error);
2388
+ if (!notice || modelUnavailableFailure(error)) return;
2389
+ const waitingPods = new Set(waitingEvents
2390
+ .filter(someoneIsWaiting)
2391
+ .map((event) => event.podId || podId)
2392
+ .filter(Boolean));
2393
+ for (const waitingPod of waitingPods) {
2394
+ const key = `cant-answer:${notice.cause}:${waitingPod}`;
2395
+ try {
2396
+ // eslint-disable-next-line no-continue
2397
+ if (!noticeDue(agentName, key, CANT_ANSWER_NOTICE_WINDOW_MS)) continue;
2398
+ // eslint-disable-next-line no-await-in-loop
2399
+ const delivery = await deliverChatReply({
2400
+ client,
2401
+ podId: waitingPod,
2402
+ text: notice.text,
2403
+ limit: chatCharLimit,
2404
+ maxChunks: maxChatChunks,
2405
+ uploadName: `${agentName}-cant-answer.md`,
2406
+ log: (line) => log(`[inbox.batch] ${line}`),
2407
+ });
2408
+ if (delivery.skipped || delivery.refused) {
2409
+ log(`[inbox.batch] pod ${waitingPod} was not told why this agent can't answer: `
2410
+ + `${delivery.reason || 'the notice was not posted'}`);
2411
+ } else {
2412
+ recordNotice(agentName, key);
2413
+ log(`[inbox.batch] told pod ${waitingPod} why this agent can't answer (${notice.cause})`);
2414
+ }
2415
+ } catch (noticeError) {
2416
+ log(`[inbox.batch] pod ${waitingPod} was not told why this agent can't answer: ${noticeError.message}`);
2417
+ }
2418
+ }
2419
+ };
2420
+
2372
2421
  const runTurnBody = async ({
2373
2422
  event, eventPodId, prompt, preSpawnIds, snapshotMessages, claimKeeper, trigger, turn,
2374
2423
  }) => {
@@ -2963,6 +3012,8 @@ export const performRun = ({
2963
3012
  // any other wake it is terminal. The free-text detail stays in the seat
2964
3013
  // log and the event ack; the kernel records the class.
2965
3014
  if (!delivered && upstream) {
3015
+ // pi exits 0 on a refused route, so no failure reaches the retry path.
3016
+ await noticeCantAnswer(new Error(describeUpstreamRefusal(upstream)), [event]);
2966
3017
  return {
2967
3018
  outcome: 'no_action',
2968
3019
  refused: {
@@ -3590,6 +3641,7 @@ export const performRun = ({
3590
3641
  ...actionEntries[0].event.payload,
3591
3642
  batchEventIds: entries.filter((entry) => !entry.pastCap).map((entry) => String(entry.event._id)),
3592
3643
  hasHeartbeat: entries.some((entry) => !entry.pastCap && entry.event.type === 'heartbeat'),
3644
+ someoneWaiting: entries.some((entry) => !entry.pastCap && someoneIsWaiting(entry.event)),
3593
3645
  },
3594
3646
  };
3595
3647
  // One item, run as its own turn. Used only after the provider refused a
@@ -3606,6 +3658,7 @@ export const performRun = ({
3606
3658
  ...entry.event.payload,
3607
3659
  batchEventIds: [String(entry.event._id)],
3608
3660
  hasHeartbeat: entry.event.type === 'heartbeat',
3661
+ someoneWaiting: someoneIsWaiting(entry.event),
3609
3662
  },
3610
3663
  },
3611
3664
  eventPodId,
@@ -3959,6 +4012,7 @@ export const performRun = ({
3959
4012
  });
3960
4013
  if (onError) onError(wrapped);
3961
4014
  else log(`[inbox.batch] ${wrapped.message}`);
4015
+ await noticeCantAnswer(err, [...failedEvents, ...untriedAfter(groupIndex)]);
3962
4016
  };
3963
4017
  let batch;
3964
4018
  try {
@@ -103,6 +103,12 @@ const CONTENT_REFUSED_RE = /(?:flagged for possible cybersecurity risk|this cont
103
103
  // adjacent `.`+digit is rare enough in provider copy to accept. The test below
104
104
  // pins what the pattern ACTUALLY has — a space after the period ends the match —
105
105
  // rather than the stronger property it does not have.
106
+ // The gateway's two budget refusals. A key over its budget answers 422 with this
107
+ // exact text (walked on a real key, 2026-11-06); a model group over its monthly
108
+ // pool answers 429 "crossed budget" (our gateway config). Case-sensitive, so a
109
+ // "thinking budget has been exceeded" stays out. Unmatched, the 422 is RUNTIME
110
+ // and the seat respawns every few seconds against a key that stays refused.
111
+ export const GATEWAY_BUDGET_RE = /Budget has been exceeded!|crossed budget/;
106
112
  const QUOTA_RE = /(?:quota|usage limit|session limit|weekly limit|credit balance|out of credits|billing|insufficient[_ -]?quota|resource exhausted|spending limit)/i;
107
113
  const QUOTA_POSSESSIVE_RE = /you'?ve (?:hit|reached) your (?!rate[ -]?limit)(?:[^.\n]|\.(?=\d)){0,40}?limit/i;
108
114
  // The status code is not a throttle wording: "429: You've reached your Fable 5
@@ -139,7 +145,7 @@ export const classifySpawnFailure = (error) => {
139
145
  // Provider APIs commonly report an exhausted account quota as HTTP 429.
140
146
  // Prefer the more specific body/message over the generic status code so a
141
147
  // hard quota failure gets the long cooldown rather than a one-minute probe.
142
- if (QUOTA_RE.test(text)) return SPAWN_FAILURE_CLASS.QUOTA;
148
+ if (QUOTA_RE.test(text) || GATEWAY_BUDGET_RE.test(text)) return SPAWN_FAILURE_CLASS.QUOTA;
143
149
  // The possessive shape is deliberately loose about what sits between "your"
144
150
  // and "limit", and its `(?!rate[ -]?limit)` lookahead only guards the word
145
151
  // IMMEDIATELY after "your" — so "you've hit your API rate limit" walked past
@@ -0,0 +1,73 @@
1
+ /**
2
+ * A seat that can't run a turn goes quiet (2026-11-06).
3
+ *
4
+ * The turn fails, the run loop opens the circuit and retries in 15 minutes, and
5
+ * the only trace is a line in a terminal the person in the pod may not own.
6
+ * They see their @mention go unanswered, with no reason. The wrapper, not the
7
+ * model, posts one plain line instead, since no model turn can run. It names
8
+ * the cause and who can fix it. It never quotes the error text, which is vendor
9
+ * output and can carry key material.
10
+ *
11
+ * The failure classes in spawn-retry.js are too loose for this. They only pick a
12
+ * backoff, so a stray "401" or "billing" in a stack trace costs a slower retry.
13
+ * Here the match becomes a statement to a person, so each line below fires only
14
+ * on a wording a seat has logged, a walk has produced, or the wrapper or our own
15
+ * gateway config writes. A failure that matches none of them posts nothing,
16
+ * which is what it did before.
17
+ */
18
+ import { PUBLIC_MCP_NOT_READY_CODE, PUBLIC_TOOL_FAILED_CODE } from './public-tool-error.js';
19
+ import { classifySpawnFailure, GATEWAY_BUDGET_RE, SPAWN_FAILURE_CLASS } from './spawn-retry.js';
20
+
21
+ /** How long one cause stays posted in a pod before the seat may post it again. */
22
+ export const CANT_ANSWER_NOTICE_WINDOW_MS = 60 * 60 * 1000;
23
+
24
+ const LOGIN_FAILED_WORDINGS = [
25
+ // Not `(upstream 401)` alone: adapterFailure writes that for any "status 401"
26
+ // in a tail, including a tool's stderr. A failed Codex turn is its own report.
27
+ /^codex turn failed \(upstream 401\)/,
28
+ /\bupstream refused 401\b/, // the same refusal, read from pi's stream
29
+ /^\w+ exited(?: with code)? \d+: 401\b/, // pi's text, which names no upstream
30
+ /workspace routing discovery unauthorized/,
31
+ /(?:^|: | \| )Not logged in · Please run \/login/,
32
+ /API Error: 400 This API key is not scoped to a workspace/,
33
+ // The wrapper's own refusals of a Claude login (adapters/claude.js).
34
+ /the Claude login on this machine expires in/,
35
+ /refusing a Claude login/,
36
+ ];
37
+ const WONT_START_WORDINGS = [
38
+ /^spawn \S+ ENOENT/,
39
+ /^\w+ adapter: \S+ (?:is not set|is missing)/, // the seat has no provider key
40
+ ];
41
+ // Not the whole of QUOTA_RE, which also holds "billing" and "quota" for backoff.
42
+ const OUT_OF_ALLOWANCE_WORDINGS = [
43
+ /(?:^|: | \| )you['’]?ve (?:hit|reached) your [^\n]{0,40}?limit/i,
44
+ /\bout of credits\b/i,
45
+ GATEWAY_BUDGET_RE,
46
+ ];
47
+
48
+ const AGAIN = 'After that, send your message again if I still haven’t answered.';
49
+
50
+ export const LOGIN_NOTICE = 'I can’t answer right now: the key or login on the computer I run on isn’t working. '
51
+ + `Whoever runs me needs to fix it and start me again. ${AGAIN}`;
52
+ export const WONT_START_NOTICE = 'I can’t answer right now: the tool I run on wouldn’t start on my computer. '
53
+ + `Whoever runs me needs to fix that and start me again. ${AGAIN}`;
54
+ export const OUT_OF_ALLOWANCE_NOTICE = 'I can’t answer right now: the AI account I run on has used up its allowance. '
55
+ + `It needs to reset, or whoever runs me needs to add more. ${AGAIN}`;
56
+
57
+ const matches = (wordings, text) => wordings.some((wording) => wording.test(text));
58
+
59
+ /** The cause a pod should hear about, and its line, or null when it should hear nothing. */
60
+ export const cantAnswerNotice = (error) => {
61
+ // A public seat's withheld turn quotes a TOOL's error, not its own provider's.
62
+ if (error?.code === PUBLIC_TOOL_FAILED_CODE || error?.code === PUBLIC_MCP_NOT_READY_CODE) return null;
63
+ // A crashed claude turn keeps assistant and tool JSON lines in its tail, and
64
+ // those are the model's words, or a tool's.
65
+ const text = String(error?.message ?? '').split(/(?:^|: | \| |\n)\{"type":/)[0];
66
+ // Before the allowance check: a 401 is settled, whatever else its body says.
67
+ if (matches(LOGIN_FAILED_WORDINGS, text)) return { cause: 'login', text: LOGIN_NOTICE };
68
+ if (matches(WONT_START_WORDINGS, text)) return { cause: 'wont-start', text: WONT_START_NOTICE };
69
+ if (classifySpawnFailure(error) === SPAWN_FAILURE_CLASS.QUOTA && matches(OUT_OF_ALLOWANCE_WORDINGS, text)) {
70
+ return { cause: 'out-of-allowance', text: OUT_OF_ALLOWANCE_NOTICE };
71
+ }
72
+ return null;
73
+ };