@commonlyai/cli 0.1.68 → 0.1.69

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.68",
3
+ "version": "0.1.69",
4
4
  "license": "Apache-2.0",
5
5
  "description": "The Commonly CLI — connect agents, manage pods, iterate fast",
6
6
  "type": "module",
@@ -39,6 +39,7 @@ import {
39
39
  formatPodFocusFrame,
40
40
  readPodFocus,
41
41
  } from '../lib/pod-focus.js';
42
+ import { claimReleaseFor, ackResultFor } from '../lib/claim-outcome.js';
42
43
  import { detectBwrap } from '../lib/sandbox/bwrap.js';
43
44
  import { resolvePublicSandboxMode } from '../lib/sandbox/mode.js';
44
45
  import { detectSeatbelt } from '../lib/sandbox/seatbelt.js';
@@ -1045,6 +1046,7 @@ export const performRun = ({
1045
1046
  // goes in a sibling field instead of being spelled into the reason.
1046
1047
  return {
1047
1048
  outcome: 'no_action',
1049
+ refused: { reason: 'cascade-cap' },
1048
1050
  reason: 'cascade-cap',
1049
1051
  details: {
1050
1052
  // The id of what was dropped. `reason` and `streak` say a refusal
@@ -1139,16 +1141,19 @@ export const performRun = ({
1139
1141
  } finally {
1140
1142
  // A silent, normally completed human wake is an explicit decline, not
1141
1143
  // a successful answer. Tell the kernel so it can hand the message to
1142
- // exactly one remaining original listener. Any posted reply, refusal
1143
- // with a reason, or thrown spawn retains completion/legacy semantics:
1144
- // re-offering those would duplicate a visible response or defeat normal
1145
- // at-least-once redelivery after an infrastructure failure.
1146
- const claimOutcome = event.type === 'message.posted'
1147
- && event.payload?.senderIsHuman === true
1148
- && turnResult?.outcome === 'no_action' && !turnResult?.reason
1149
- ? 'declined'
1150
- : (turnResult ? 'completed' : undefined);
1151
- await claimKeeper?.release(claimOutcome);
1144
+ // exactly one remaining original listener. A refusal — the upstream
1145
+ // route down, or the server refusing the post — is NAMED (TASK-099): it
1146
+ // carries its class so the kernel's record can tell "answered" from
1147
+ // "never ran", and on a human wake the kernel hands it on exactly as it
1148
+ // does a decline, because a failure local to THIS seat must not make a
1149
+ // human's message disappear. A thrown spawn keeps the legacy release, so
1150
+ // normal at-least-once redelivery still happens after an infrastructure
1151
+ // failure. One decision point, shared with the batch path — see
1152
+ // lib/claim-outcome.js.
1153
+ const release = claimReleaseFor(event, turnResult);
1154
+ await claimKeeper?.release(release.outcome, {
1155
+ reason: release.reason, status: release.status,
1156
+ });
1152
1157
  }
1153
1158
  };
1154
1159
 
@@ -1397,6 +1402,7 @@ export const performRun = ({
1397
1402
  if (deliveryRefusal) {
1398
1403
  return {
1399
1404
  outcome: 'no_action',
1405
+ refused: { reason: 'delivery-refused' },
1400
1406
  reason: deliveryRefusal.reason,
1401
1407
  details: {
1402
1408
  mode: deliveryRefusal.mode,
@@ -1409,14 +1415,22 @@ export const performRun = ({
1409
1415
  },
1410
1416
  };
1411
1417
  }
1412
- // A refusal is not a decline. `no_action` with no reason means "the agent
1413
- // chose not to answer" and hands a human wake to one remaining listener;
1414
- // nothing about the agent's choice happened here — the model never ran — so
1415
- // say what did happen, and let the event close rather than fan a
1416
- // route-wide refusal out across the rest of the fleet.
1418
+ // A refusal is not a decline, and it is not a close either. `no_action`
1419
+ // with no reason means "the agent chose not to answer" and hands a human
1420
+ // wake to one remaining listener; nothing about the agent's choice happened
1421
+ // here — the model never ran — so the release says WHICH class of refusal
1422
+ // it was (TASK-099). The kernel routes from there: on a human wake the
1423
+ // message is handed to one remaining listener exactly as a decline is
1424
+ // (this seat's upstream route being down says nothing about theirs), and on
1425
+ // any other wake it is terminal. The free-text detail stays in the seat
1426
+ // log and the event ack; the kernel records the class.
1417
1427
  if (!delivered && upstream) {
1418
1428
  return {
1419
1429
  outcome: 'no_action',
1430
+ refused: {
1431
+ reason: 'upstream-refused',
1432
+ ...(Number.isInteger(upstream.status) ? { status: upstream.status } : {}),
1433
+ },
1420
1434
  reason: `upstream-refused-${upstream.status}`,
1421
1435
  details: {
1422
1436
  status: upstream.status,
@@ -1461,6 +1475,7 @@ export const performRun = ({
1461
1475
 
1462
1476
  const batchAdmissionResult = (event, admission) => ({
1463
1477
  outcome: 'no_action',
1478
+ refused: { reason: 'cascade-cap' },
1464
1479
  reason: 'cascade-cap',
1465
1480
  details: {
1466
1481
  messageId: event?.payload?.messageId || null,
@@ -1646,14 +1661,14 @@ export const performRun = ({
1646
1661
  .filter((entry) => entry.claimKeeper)
1647
1662
  .map(async (entry) => {
1648
1663
  // Preserve ADR-018 D6.1 per binding message: a silent human
1649
- // broadcast may be handed to one remaining listener, while every
1650
- // other completed batch item closes normally.
1651
- const claimOutcome = entry.event.type === 'message.posted'
1652
- && entry.event.payload?.senderIsHuman === true
1653
- && turnResult?.outcome === 'no_action' && !turnResult?.reason
1654
- ? 'declined'
1655
- : (turnResult ? 'completed' : undefined);
1656
- await entry.claimKeeper.release(claimOutcome);
1664
+ // broadcast may be handed to one remaining listener, a refusal
1665
+ // closes with its reason, and every other completed batch item
1666
+ // closes normally. Same decision point as the single-event path.
1667
+ // Same decision point as the single-event path.
1668
+ const release = claimReleaseFor(entry.event, turnResult);
1669
+ await entry.claimKeeper.release(release.outcome, {
1670
+ reason: release.reason, status: release.status,
1671
+ });
1657
1672
  }));
1658
1673
  }
1659
1674
  };
@@ -1743,7 +1758,7 @@ export const performRun = ({
1743
1758
  try {
1744
1759
  const deliveryId = event.payload?.deliveryId;
1745
1760
  await client.post(`/api/agents/runtime/events/${event._id}/ack`, {
1746
- result: entry.result,
1761
+ result: ackResultFor(entry.result),
1747
1762
  ...(typeof deliveryId === 'string' && deliveryId ? { deliveryId } : {}),
1748
1763
  });
1749
1764
  } catch (ackErr) {
@@ -1843,7 +1858,7 @@ export const performRun = ({
1843
1858
  try {
1844
1859
  const deliveryId = event.payload?.deliveryId;
1845
1860
  await client.post(`/api/agents/runtime/events/${event._id}/ack`, {
1846
- result,
1861
+ result: ackResultFor(result),
1847
1862
  ...(typeof deliveryId === 'string' && deliveryId ? { deliveryId } : {}),
1848
1863
  });
1849
1864
  } catch (ackErr) {
@@ -0,0 +1,120 @@
1
+ /**
2
+ * claimReleaseFor — the ONE decision about how a finished turn releases its
3
+ * ADR-018 attention claim.
4
+ *
5
+ * It lives here, extracted from two byte-identical copies in the run loop
6
+ * (single-event and batch release), for the reason a second copy is always a
7
+ * risk: the batch path is the one `message.posted` actually reaches, so a
8
+ * change applied to only one of them would pass review and do nothing.
9
+ *
10
+ * Three outcomes, and the KERNEL decides what each one does with the wake
11
+ * (TASK-099 ruling, corrected at 71194/71195/71210):
12
+ *
13
+ * - `refused` — the lease holder could not deliver. Nothing about the
14
+ * agent's CHOICE happened: the model never ran (an upstream
15
+ * 429/502/401, named by TASK-096) or the server refused the
16
+ * post. On a HUMAN wake the kernel hands the message to one
17
+ * remaining listener exactly as it does for a decline — a
18
+ * per-seat budget failure must not make a human's message
19
+ * disappear — and records the refuser so the chain stays
20
+ * finite. On any other wake it is terminal. The routing is
21
+ * the kernel's: this module only names the outcome.
22
+ * - `declined` — a silent, normally completed human broadcast. The agent
23
+ * chose not to answer, so the kernel hands it to exactly one
24
+ * remaining original listener.
25
+ * - `completed` — everything else that produced a result: a posted reply, or
26
+ * a skip with a reason (`no-prompt`, `claim-held`,
27
+ * `duplicate-delivery`) where nothing was refused.
28
+ *
29
+ * The refusal CLASS is an enum, not free text: `upstream-refused` (with the
30
+ * HTTP status), `cascade-cap`, `delivery-refused`. The kernel stores the class
31
+ * so refusals are countable, and rejects anything else with a 400. A producer
32
+ * that cannot classify itself maps to `delivery-refused` before sending — the
33
+ * mapping is here, in one place, rather than in each producer.
34
+ *
35
+ * A thrown turn returns no turnResult and releases with no outcome — the
36
+ * legacy holder-only DELETE whose at-least-once redelivery is the correct
37
+ * behaviour for a crashed spawn (the lid closed, the process died).
38
+ */
39
+
40
+ /** The classes the kernel accepts. Mirrors `REFUSAL_REASONS` in agentsRuntime. */
41
+ export const REFUSAL_REASONS = ['upstream-refused', 'cascade-cap', 'delivery-refused'];
42
+
43
+ /** What an unclassifiable refusal is recorded as, rather than a fourth class. */
44
+ export const DEFAULT_REFUSAL_REASON = 'delivery-refused';
45
+
46
+ /**
47
+ * The range the kernel accepts for a refusal's `status` — its
48
+ * `MIN_UPSTREAM_STATUS` / `MAX_UPSTREAM_STATUS` in `messageClaimService`.
49
+ * A number outside it is not a refusal status the kernel will record: it
50
+ * answers 400, and the 400 fallback in `enforcement.release` re-releases as
51
+ * `completed` — which is terminal. So forwarding an out-of-range number does
52
+ * not merely lose the number, it loses the handoff: an upstream refusal on a
53
+ * human wake would be recorded as a clean completion. The sender's rule has to
54
+ * be the receiver's rule, which is why the bound lives here as well as there.
55
+ */
56
+ export const MIN_UPSTREAM_STATUS = 400;
57
+ export const MAX_UPSTREAM_STATUS = 599;
58
+
59
+ /**
60
+ * @param {{type?: string, payload?: {senderIsHuman?: boolean}}} event
61
+ * @param {{refused?: {reason?: string, status?: number}|true, outcome?: string, reason?: string}|undefined} turnResult
62
+ * @returns {{outcome?: 'refused'|'declined'|'completed', reason?: string, status?: number}}
63
+ */
64
+ export const claimReleaseFor = (event, turnResult) => {
65
+ if (!turnResult) return { outcome: undefined };
66
+ // `refused: true` is still honoured: it is what an older producer shape (and
67
+ // a third-party wrapper following TASK-096's docs) sends, and it means the
68
+ // same thing — `delivery-refused`, the class that claims the least.
69
+ if (turnResult.refused) {
70
+ const declared = typeof turnResult.refused === 'object' ? turnResult.refused : {};
71
+ const reason = REFUSAL_REASONS.includes(declared.reason)
72
+ ? declared.reason
73
+ : DEFAULT_REFUSAL_REASON;
74
+ // The status is the instance of the class, and only the upstream class has
75
+ // one: a cap refusal or a refused post never saw an upstream HTTP status,
76
+ // so sending its number would put a field on the record that cannot mean
77
+ // what a reader would assume. It is also bounded by what the kernel
78
+ // accepts — an out-of-range number keeps the class and drops only the
79
+ // number, so the refusal still hands off instead of falling back to
80
+ // `completed`.
81
+ const status = reason === 'upstream-refused'
82
+ && Number.isInteger(declared.status)
83
+ && declared.status >= MIN_UPSTREAM_STATUS
84
+ && declared.status <= MAX_UPSTREAM_STATUS
85
+ ? declared.status
86
+ : undefined;
87
+ return {
88
+ outcome: 'refused',
89
+ reason,
90
+ ...(status !== undefined ? { status } : {}),
91
+ };
92
+ }
93
+ if (event?.type === 'message.posted'
94
+ && event.payload?.senderIsHuman === true
95
+ && turnResult.outcome === 'no_action'
96
+ && !turnResult.reason) {
97
+ return { outcome: 'declined' };
98
+ }
99
+ return { outcome: 'completed' };
100
+ };
101
+
102
+ /**
103
+ * `refused` is a LOCAL release marker: the producer sets it so the claim
104
+ * release can name the outcome (see `claimReleaseFor`), and nothing else reads
105
+ * it. The event ack is a wire payload with its own readers, and the information
106
+ * that belongs there already reaches them as `reason` — so the marker is
107
+ * dropped at the ack boundary rather than widening the ack with an internal
108
+ * flag. Returns the result unchanged when there is no marker, so the common
109
+ * path never sees a rebuilt object.
110
+ *
111
+ * @param {object|undefined} turnResult
112
+ */
113
+ export const ackResultFor = (turnResult) => {
114
+ if (!turnResult || !turnResult.refused) return turnResult;
115
+ // ts-eslint's `arg`/`vars` ignore pattern is `^_`, and ignoreRestSiblings is
116
+ // off in this repo's config — so the dropped key is renamed rather than
117
+ // left to trip no-unused-vars.
118
+ const { refused: _refused, ...rest } = turnResult;
119
+ return rest;
120
+ };
@@ -434,7 +434,7 @@ export const createClaimKeeper = (client, {
434
434
  if (timer && typeof timer.unref === 'function') timer.unref();
435
435
  },
436
436
 
437
- async release(outcome) {
437
+ async release(outcome, { reason, status } = {}) {
438
438
  stopRenewal();
439
439
  if (!acquired || lost) return;
440
440
  try {
@@ -442,7 +442,35 @@ export const createClaimKeeper = (client, {
442
442
  // outcome. Besides keeping old clients' DELETE shape intact, callers
443
443
  // that assert their transport arguments must not see a synthetic
444
444
  // `undefined` body. An explicit outcome is the new D6.1 contract.
445
- await (outcome ? client.del(path, { outcome }) : client.del(path));
445
+ //
446
+ // `reason` and `status` ride only with a refusal (TASK-099) and only
447
+ // when the producer named them: the kernel stores the class as the
448
+ // record of why a delivery never happened, so an absent reason must
449
+ // stay absent rather than become the string "undefined".
450
+ if (!outcome) {
451
+ await client.del(path);
452
+ return;
453
+ }
454
+ const body = {
455
+ outcome,
456
+ ...(reason ? { reason } : {}),
457
+ ...(status !== undefined ? { status } : {}),
458
+ };
459
+ try {
460
+ await client.del(path, body);
461
+ } catch (err) {
462
+ // An older kernel validates the outcome against its own
463
+ // decline/completed enum and answers 400 — as it does for a reason
464
+ // it does not recognise. A CLI that ships before (or runs against a
465
+ // server earlier than) the third outcome must still release its
466
+ // lease, or the message stays claimed until it expires: one retry as
467
+ // `completed`, which is exactly what the old release did. Bounded to
468
+ // a 400 — a 500 or a timeout is not turned into a second write, and
469
+ // a second 400 is not retried again.
470
+ if (outcome !== 'refused' || err?.status !== 400) throw err;
471
+ log(`claim on message ${messageId}: kernel rejected the refused outcome (400) — releasing as completed`);
472
+ await client.del(path, { outcome: 'completed' });
473
+ }
446
474
  } catch {
447
475
  // Best-effort: a miss just means the lease already expired.
448
476
  }