@commonlyai/cli 0.1.122 → 0.1.133

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.122",
3
+ "version": "0.1.133",
4
4
  "license": "Apache-2.0",
5
5
  "description": "The Commonly CLI — connect agents, manage pods, iterate fast",
6
6
  "type": "module",
@@ -451,6 +451,19 @@ const PROMPT_EVENT_TYPES = new Set([
451
451
  'first_contact',
452
452
  ]);
453
453
 
454
+ // A person waiting on an answer outranks the seat's own housekeeping. Measured
455
+ // on the published 0.1.108 smoke (TASK-289, walk 5): the greeting turn for a
456
+ // fresh connect ran 14 s, a mention landed 1 s into it, and the answer came
457
+ // from the next turn 39.4 s after the mention. `first_contact` is the only
458
+ // event that exists to introduce the seat rather than to answer anyone, so it
459
+ // is the only one worth holding back.
460
+ const DEFERRABLE_SOLO_EVENT_TYPES = new Set(['first_contact']);
461
+
462
+ // Inside one batch the person's item leads the prompt; the greeting follows it.
463
+ const PERSON_WAITING_EVENT_TYPES = new Set(['chat.mention', 'message.posted', 'dm.message']);
464
+
465
+ const inboxPriority = (event) => (PERSON_WAITING_EVENT_TYPES.has(event?.type) ? 0 : 1);
466
+
454
467
  // Consultations have a per-request private response channel, rather than a
455
468
  // pod-chat delivery. They stay on their existing single-event path; every
456
469
  // event that represents the shared pod inbox is batched below.
@@ -2039,6 +2052,17 @@ export const performRun = ({
2039
2052
  }
2040
2053
  };
2041
2054
 
2055
+ // The greeting is CLAIMED the moment a poll returns it: claimPage reads
2056
+ // `status: 'pending'` and marks each returned row `delivered`, and later
2057
+ // polls read only pending. Leaving it unacked therefore does NOT bring it
2058
+ // back next tick — garbageCollect requeues delivered rows only after
2059
+ // AGENT_EVENT_REQUEUE_DELIVERED_MINUTES (10 by default, and its own comment
2060
+ // notes the effective latency is worse). So the hold is in memory, not on
2061
+ // the wire: carry the claimed event and prepend it to the next poll's batch.
2062
+ // Spent at most once per run. (CTO review 5623358693.)
2063
+ let heldGreeting = [];
2064
+ let greetingHoldSpent = false;
2065
+
2042
2066
  const processEventBatch = async (events, inboxCount = events.length) => {
2043
2067
  const eventPodId = events[0]?.podId || podId;
2044
2068
  if (!eventPodId || events.some((event) => (event.podId || podId) !== eventPodId)) {
@@ -2296,8 +2320,21 @@ export const performRun = ({
2296
2320
  // returns multiple pods in one page. Preserve pod privacy in that
2297
2321
  // migration window by partitioning locally. Current servers produce one
2298
2322
  // group, so the normal path remains one tick → one turn.
2323
+ // A carried greeting rejoins the inbox here, so it is grouped, ordered
2324
+ // and acked by exactly the same path as anything the poll returned. The
2325
+ // carried row is still 'delivered' on the server, so a requeue during the
2326
+ // carry can hand the same id back in this very page: drop the copy rather
2327
+ // than processing it twice.
2328
+ const carried = heldGreeting;
2329
+ heldGreeting = [];
2330
+ const fetchedIds = new Set(events.map((event) => String(event._id)));
2331
+ const carriedFresh = carried.filter((event) => !fetchedIds.has(String(event._id)));
2332
+ const pageEvents = carriedFresh.length ? [...carriedFresh, ...events] : events;
2333
+ const pageInboxCount = (Number.isFinite(inboxCount) ? inboxCount : events.length)
2334
+ + carriedFresh.length;
2335
+
2299
2336
  const eventGroups = new Map();
2300
- for (const event of events) {
2337
+ for (const event of pageEvents) {
2301
2338
  const eventPodId = event.podId || podId;
2302
2339
  const key = PRIVATE_RESPONSE_EVENT_TYPES.has(event.type)
2303
2340
  ? `private:${event._id}`
@@ -2309,6 +2346,16 @@ export const performRun = ({
2309
2346
  for (const group of eventGroups.values()) {
2310
2347
  if (!running) break;
2311
2348
  if ((group[0].podId || podId) && !PRIVATE_RESPONSE_EVENT_TYPES.has(group[0].type)) {
2349
+ // Nothing in this batch but the seat introducing itself: leave it
2350
+ // unacked for one interval so a person who mentions the agent in that
2351
+ // window is answered in the same turn instead of behind a greeting.
2352
+ // Spent once per run, so a quiet connect still greets on the next tick.
2353
+ if (!greetingHoldSpent && group.every((event) => DEFERRABLE_SOLO_EVENT_TYPES.has(event.type))) {
2354
+ greetingHoldSpent = true;
2355
+ heldGreeting = group;
2356
+ log(`[inbox.batch] carrying ${group.length} greeting event(s) to the next poll in case someone is mid-sentence`);
2357
+ continue;
2358
+ }
2312
2359
  // One failure report for a batch whose items stay unacked, whether
2313
2360
  // the composite turn threw or an isolated item did after a refusal.
2314
2361
  const scheduleBatchRetry = (err, unackedCount) => {
@@ -2350,7 +2397,11 @@ export const performRun = ({
2350
2397
  };
2351
2398
  let batch;
2352
2399
  try {
2353
- batch = await processEventBatch(group, inboxCount);
2400
+ // The person's item leads the prompt; the greeting rides along.
2401
+ batch = await processEventBatch(
2402
+ [...group].sort((a, b) => inboxPriority(a) - inboxPriority(b)),
2403
+ pageInboxCount,
2404
+ );
2354
2405
  } catch (err) {
2355
2406
  scheduleBatchRetry(err, group.length);
2356
2407
  break;
@@ -2681,6 +2732,7 @@ export const performDetach = async ({
2681
2732
  client,
2682
2733
  agentName,
2683
2734
  podId,
2735
+ instanceId = 'default',
2684
2736
  skipBackend = false,
2685
2737
  log = () => {},
2686
2738
  }) => {
@@ -2696,7 +2748,7 @@ export const performDetach = async ({
2696
2748
  let backendResult = { skipped: true };
2697
2749
  if (!skipBackend) {
2698
2750
  try {
2699
- const body = await client.del(`/api/registry/agents/${encodeURIComponent(agentName)}/pods/${encodeURIComponent(podId)}`);
2751
+ const body = await client.del(`/api/registry/agents/${encodeURIComponent(agentName)}/pods/${encodeURIComponent(podId)}?instanceId=${encodeURIComponent(instanceId)}`);
2700
2752
  backendResult = { skipped: false, body };
2701
2753
  log(`backend: uninstalled '${agentName}' from pod ${podId}`);
2702
2754
  } catch (err) {
@@ -3279,7 +3331,7 @@ Docs:
3279
3331
  console.error(
3280
3332
  `No local state for '${name}'. If the agent is still installed on `
3281
3333
  + `the backend, uninstall it via the Agent Hub UI or:\n`
3282
- + ` curl -X DELETE <instance>/api/registry/agents/${name}/pods/<podId> \\\n`
3334
+ + ` curl -X DELETE '<instance>/api/registry/agents/${name}/pods/<podId>?instanceId=<instanceId>' \\\n`
3283
3335
  + ` -H "Authorization: Bearer <user JWT>"`,
3284
3336
  );
3285
3337
  process.exit(1);
@@ -3307,6 +3359,7 @@ Docs:
3307
3359
  client,
3308
3360
  agentName: record.agentName,
3309
3361
  podId: record.podId,
3362
+ instanceId: record.instanceId || 'default',
3310
3363
  skipBackend: !!opts.force,
3311
3364
  log: (line) => console.warn(`[detach] ${line}`),
3312
3365
  });