@commonlyai/cli 0.1.132 → 0.1.134
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 +4 -1
- package/src/commands/agent.js +53 -2
- package/src/lib/daemon-supervisor.js +31 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@commonlyai/cli",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.134",
|
|
4
4
|
"license": "Apache-2.0",
|
|
5
5
|
"description": "The Commonly CLI — connect agents, manage pods, iterate fast",
|
|
6
6
|
"type": "module",
|
|
@@ -45,6 +45,9 @@
|
|
|
45
45
|
"setupFiles": [
|
|
46
46
|
"<rootDir>/jest.setup.cjs"
|
|
47
47
|
],
|
|
48
|
+
"setupFilesAfterEnv": [
|
|
49
|
+
"<rootDir>/jest.teardown.cjs"
|
|
50
|
+
],
|
|
48
51
|
"transform": {},
|
|
49
52
|
"testMatch": [
|
|
50
53
|
"**/__tests__/**/*.[jt]s?(x)",
|
package/src/commands/agent.js
CHANGED
|
@@ -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
|
|
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
|
-
|
|
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;
|
|
@@ -208,6 +208,37 @@ export const createDaemonSupervisor = ({
|
|
|
208
208
|
|
|
209
209
|
const existing = loadToken(row.agentName);
|
|
210
210
|
if (existing) {
|
|
211
|
+
// Local records are still keyed by agent name. Never rotate or launch
|
|
212
|
+
// another instance through a same-name record: that would overwrite a
|
|
213
|
+
// different seat's bearer until instance-scoped local records land.
|
|
214
|
+
if ((existing.instanceId || 'default') !== (row.instanceId || 'default')) {
|
|
215
|
+
log(`[${row.agentName}] local token belongs to instance '${existing.instanceId || 'default'}'; refusing '${row.instanceId || 'default'}'`);
|
|
216
|
+
return false;
|
|
217
|
+
}
|
|
218
|
+
// A human owner can request a fresh key from Settings after revocation.
|
|
219
|
+
// The daemon owns its local token file, so the browser only stamps the
|
|
220
|
+
// directive; this host rotates the key and restarts its child once.
|
|
221
|
+
const requestedAt = row.reconnectRequestedAt && new Date(row.reconnectRequestedAt).getTime();
|
|
222
|
+
const appliedAt = existing.reconnectRequestAppliedAt
|
|
223
|
+
&& new Date(existing.reconnectRequestAppliedAt).getTime();
|
|
224
|
+
if (Number.isFinite(requestedAt) && requestedAt > (appliedAt || 0)) {
|
|
225
|
+
try {
|
|
226
|
+
const minted = await client.post('/api/agent-binding/runtime-token', {
|
|
227
|
+
agentName: row.agentName, instanceId: row.instanceId, rotate: true,
|
|
228
|
+
});
|
|
229
|
+
if (!minted?.token) throw new Error('mint returned no token');
|
|
230
|
+
saveToken(row.agentName, {
|
|
231
|
+
...existing,
|
|
232
|
+
runtimeToken: minted.token,
|
|
233
|
+
reconnectRequestAppliedAt: row.reconnectRequestedAt,
|
|
234
|
+
});
|
|
235
|
+
log(`[${row.agentName}] owner-requested key rotation applied; restarting the seat`);
|
|
236
|
+
return 'changed';
|
|
237
|
+
} catch (error) {
|
|
238
|
+
log(`[${row.agentName}] owner-requested key rotation failed: ${error.message}`);
|
|
239
|
+
return false;
|
|
240
|
+
}
|
|
241
|
+
}
|
|
211
242
|
// A model changed in the UI reaches the seat here: update the record,
|
|
212
243
|
// and let the caller restart the child (`agent run` reads its record
|
|
213
244
|
// once at boot). A row with NO declared model leaves the record alone —
|