@commonlyai/cli 0.1.265 → 0.1.267
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 +1 -1
- package/src/commands/agent.js +309 -45
package/package.json
CHANGED
package/src/commands/agent.js
CHANGED
|
@@ -781,6 +781,34 @@ const DELIVERY_RENEW_INTERVAL_MS = 60_000;
|
|
|
781
781
|
// point where spawnRetryPolicy stops treating an unknown failure as transient.
|
|
782
782
|
const HELD_RETRY_MAX_FAILURES = 3;
|
|
783
783
|
|
|
784
|
+
// F226: a turn shows at most the batch limit, so a carried item on top of a full
|
|
785
|
+
// page can sit past the cap. Such an item is not acked: it keeps its lease and
|
|
786
|
+
// rejoins the next turn. At this many turns passed over the carry lets it go,
|
|
787
|
+
// the way HELD_RETRY_MAX_FAILURES does, so an ambient item under a steady stream
|
|
788
|
+
// of people, or anything piling up during an outage, can't be held forever. The
|
|
789
|
+
// server redelivers it under its cap, and may retire it with the TASK-656 line,
|
|
790
|
+
// which is skipped if the agent has posted in the pod since.
|
|
791
|
+
const PAST_CAP_MAX_CARRIES = 5;
|
|
792
|
+
|
|
793
|
+
// Who gets a slot when a batch is past the cap. 'unknown' counts as a person:
|
|
794
|
+
// the snapshot behind `trigger` holds 10 messages and a page holds up to 50, and
|
|
795
|
+
// a wrong guess costs an ambient item one turn. Within a rank the older row
|
|
796
|
+
// wins, and carried rows lead the page, so new arrivals can't overtake them.
|
|
797
|
+
const capRank = (entry) => {
|
|
798
|
+
const { type, payload } = entry.event;
|
|
799
|
+
// A reply to the seat is direct address on a broadcast's transport, as the
|
|
800
|
+
// claim path treats it.
|
|
801
|
+
const direct = ADDRESSED_EVENT_TYPES.has(type)
|
|
802
|
+
|| ((payload?.repliesToYourMessage === true || payload?.addressed === true)
|
|
803
|
+
&& payload?.senderIsHuman !== false);
|
|
804
|
+
return direct && entry.trigger !== 'agent' && entry.trigger !== 'kernel'
|
|
805
|
+
? 0
|
|
806
|
+
: 1 + inboxPriority(entry.event);
|
|
807
|
+
};
|
|
808
|
+
|
|
809
|
+
// The wake whose ack settles the digest (agentEventService.acknowledge).
|
|
810
|
+
const holdsDigest = (event) => Boolean(event.digestSnapshotAt || event.payload?.digestRollup);
|
|
811
|
+
|
|
784
812
|
const deliveryIdOf = (event) => {
|
|
785
813
|
const deliveryId = event?.payload?.deliveryId;
|
|
786
814
|
return typeof deliveryId === 'string' && deliveryId ? deliveryId : null;
|
|
@@ -1174,11 +1202,20 @@ export const resolveAttachSandbox = ({
|
|
|
1174
1202
|
// used to fail closed on, refusing `internal` beside `mode: 'workspace'`. It is
|
|
1175
1203
|
// pure — a mode and an adapter name — so it belongs with the derivation, and
|
|
1176
1204
|
// its witnesses live beside it.
|
|
1177
|
-
|
|
1178
|
-
|
|
1179
|
-
|
|
1180
|
-
|
|
1181
|
-
|
|
1205
|
+
// Two different things can be missing, so the refusal names the one that is:
|
|
1206
|
+
// the D247 walk read "requires public trust" on a seat whose trust WAS public.
|
|
1207
|
+
if (mode === 'workspace' || mode === 'read-only') {
|
|
1208
|
+
if (!confined) {
|
|
1209
|
+
throw new Error(
|
|
1210
|
+
`sandbox.mode=${mode} needs sandbox.trust=public. Add it, or remove sandbox.mode to run without a sandbox.`,
|
|
1211
|
+
);
|
|
1212
|
+
}
|
|
1213
|
+
if (!ADAPTERS_WITH_SANDBOX_ENFORCEMENT.has(adapterName)) {
|
|
1214
|
+
throw new Error(
|
|
1215
|
+
`The ${adapterName || 'selected'} adapter can't enforce sandbox.mode=${mode}. `
|
|
1216
|
+
+ `On macOS, these adapters can: ${[...ADAPTERS_WITH_SANDBOX_ENFORCEMENT].sort().join(', ')}.`,
|
|
1217
|
+
);
|
|
1218
|
+
}
|
|
1182
1219
|
}
|
|
1183
1220
|
return { mode, trust };
|
|
1184
1221
|
};
|
|
@@ -1516,7 +1553,14 @@ const FINAL_TEXT_IS_POSTED = 'Your final text is posted to the pod unless it is
|
|
|
1516
1553
|
// threadRootId set to that message, so the answer sat in a collapsed thread
|
|
1517
1554
|
// under it. The kernel's mention cue teaches threadRootId as a verb but never
|
|
1518
1555
|
// says which messages it is for, so each top-level mention says where its
|
|
1519
|
-
// answer goes, beside the message it answers.
|
|
1556
|
+
// answer goes, beside the message it answers. replyToMessageId alone threads
|
|
1557
|
+
// too, because the kernel derives the root from the reply edge, so the line
|
|
1558
|
+
// names both. This is advice: the guarantee is the kernel's answerStaysTopLevel.
|
|
1559
|
+
//
|
|
1560
|
+
// Only for a person's message. The line sends a plain post, which the kernel
|
|
1561
|
+
// cannot thread back, so on a peer's mention it would put an agent exchange in
|
|
1562
|
+
// the channel at every hop. A missing `senderIsHuman` means no line: the kernel
|
|
1563
|
+
// still keeps the answer top-level for a person.
|
|
1520
1564
|
//
|
|
1521
1565
|
// `chat.mention` alone is not "top-level": the kernel sends a chat-thread
|
|
1522
1566
|
// mention as chat.mention with `payload.threadRootId`, a 1:1 DM as chat.mention
|
|
@@ -1542,6 +1586,7 @@ export const answersInChannel = (entry) => {
|
|
|
1542
1586
|
return entry?.event?.type === 'chat.mention' && !entry.readOnly
|
|
1543
1587
|
&& entry.result?.reason !== 'duplicate-delivery'
|
|
1544
1588
|
&& Boolean(payload.messageId)
|
|
1589
|
+
&& payload.senderIsHuman === true
|
|
1545
1590
|
&& (payload.threadRootId === undefined || payload.threadRootId === null || payload.threadRootId === '')
|
|
1546
1591
|
&& payload.source !== 'dm' && !payload.dmKind;
|
|
1547
1592
|
};
|
|
@@ -1702,6 +1747,7 @@ export const performRun = ({
|
|
|
1702
1747
|
log = () => {},
|
|
1703
1748
|
onError,
|
|
1704
1749
|
setTimeoutImpl = setTimeout,
|
|
1750
|
+
clearTimeoutImpl = clearTimeout,
|
|
1705
1751
|
setIntervalImpl = setInterval,
|
|
1706
1752
|
clearIntervalImpl = clearInterval,
|
|
1707
1753
|
// The host's bubblewrap probe, for a turn placed public; tests pin it.
|
|
@@ -2941,20 +2987,26 @@ export const performRun = ({
|
|
|
2941
2987
|
};
|
|
2942
2988
|
|
|
2943
2989
|
// ADR-024 D3: the batch header states the real inbox count even when we cap
|
|
2944
|
-
// copied bodies.
|
|
2945
|
-
//
|
|
2990
|
+
// copied bodies. A claimed item past the cap (`pastCap`, set in
|
|
2991
|
+
// handleEventBatch) is left out and named in the prompt, never acked as seen;
|
|
2992
|
+
// the next turn shows it. Additional pending
|
|
2946
2993
|
// events stay in the kernel queue and are cued in the header for a later
|
|
2947
2994
|
// pull, rather than being silently consumed here.
|
|
2948
2995
|
const buildInboxPrompt = (entries, inboxCount = entries.length) => {
|
|
2949
2996
|
const BODY_LIMIT = batchLimit;
|
|
2950
|
-
const wakeEntries = entries.filter((entry) => entry.event.deliveryMode !== 'digest');
|
|
2951
|
-
const digestEntries = entries.filter((entry) => entry.event.deliveryMode === 'digest');
|
|
2952
|
-
|
|
2997
|
+
const wakeEntries = entries.filter((entry) => entry.event.deliveryMode !== 'digest' && !entry.pastCap);
|
|
2998
|
+
const digestEntries = entries.filter((entry) => entry.event.deliveryMode === 'digest' && !entry.pastCap);
|
|
2999
|
+
// What the turn acts on always fits. An item already settled (a peer's, a
|
|
3000
|
+
// duplicate) is context, and takes only what's left.
|
|
3001
|
+
const context = wakeEntries.filter((entry) => entry.result);
|
|
3002
|
+
const contextShown = new Set(context.slice(0, Math.max(0, BODY_LIMIT - wakeEntries.length + context.length)));
|
|
3003
|
+
const shownEntries = wakeEntries.filter((entry) => !entry.result || contextShown.has(entry));
|
|
3004
|
+
const heldBack = entries.filter((entry) => entry.pastCap).length;
|
|
2953
3005
|
const answersPerson = shownEntries.some(answersAPerson);
|
|
2954
3006
|
const bodies = shownEntries.map((entry, index) => {
|
|
2955
3007
|
const messageId = entry.event.payload?.messageId;
|
|
2956
3008
|
const prefix = [
|
|
2957
|
-
`Event ${index + 1}/${
|
|
3009
|
+
`Event ${index + 1}/${shownEntries.length}: ${entry.event.type}`,
|
|
2958
3010
|
messageId ? `message ${messageId}` : null,
|
|
2959
3011
|
].filter(Boolean).join(' — ');
|
|
2960
3012
|
const claimContext = entry.readOnly
|
|
@@ -2984,6 +3036,10 @@ export const performRun = ({
|
|
|
2984
3036
|
+ 'Never checkpoint unfinished work. Only a successful checkpoint prevents replay after timeout. '
|
|
2985
3037
|
+ 'If the tool is unavailable, continue normally; completion will acknowledge the batch.',
|
|
2986
3038
|
...bodies,
|
|
3039
|
+
...(heldBack > 0
|
|
3040
|
+
? [`[${heldBack} more event${heldBack === 1 ? '' : 's'} in this inbox did not fit this turn. `
|
|
3041
|
+
+ 'They are unread and still queued, and come in a later turn. Don\'t answer as if you saw them.]']
|
|
3042
|
+
: []),
|
|
2987
3043
|
...(digestFrame ? [digestFrame] : []),
|
|
2988
3044
|
...(omitted > 0
|
|
2989
3045
|
? [`[${omitted} more event${omitted === 1 ? '' : 's'} arrived. Their count is real; pull pod context if needed.]`]
|
|
@@ -3089,8 +3145,19 @@ export const performRun = ({
|
|
|
3089
3145
|
// 2026-10-25): a scheduled retry either fires, re-running the held events,
|
|
3090
3146
|
// or releases them; it never leaves a live process idle on unacked work.
|
|
3091
3147
|
let heldRetry = [];
|
|
3148
|
+
// Claimed events the latest tick neither acked nor carried: a digest left
|
|
3149
|
+
// unread, or a later group a failure or a stale ack skipped. A running seat
|
|
3150
|
+
// leaves these to the requeue as it always has. A stop hands them back on
|
|
3151
|
+
// every planned exit from the tick; an unexpected throw leaves the page to
|
|
3152
|
+
// the requeue. One list, so a new exit has one thing to add to (F189).
|
|
3153
|
+
let tickUnsettled = [];
|
|
3092
3154
|
// eventId → consecutive local failures, for HELD_RETRY_MAX_FAILURES.
|
|
3093
3155
|
const heldRetryFailures = new Map();
|
|
3156
|
+
// eventId → turns passed over, for PAST_CAP_MAX_CARRIES.
|
|
3157
|
+
const pastCapCarries = new Map();
|
|
3158
|
+
// The current batch's items past the cap. Kept out here because the tick needs
|
|
3159
|
+
// them when the turn throws, and a throw returns no entries.
|
|
3160
|
+
let pastCapEvents = [];
|
|
3094
3161
|
let heldRenewTimer = null;
|
|
3095
3162
|
const clearHeldRenewal = () => {
|
|
3096
3163
|
if (heldRenewTimer !== null) clearIntervalImpl(heldRenewTimer);
|
|
@@ -3132,8 +3199,11 @@ export const performRun = ({
|
|
|
3132
3199
|
// Decide what a failed turn's retry carries. `failedEvents` were attempted
|
|
3133
3200
|
// and count toward HELD_RETRY_MAX_FAILURES; `untriedEvents` are later groups
|
|
3134
3201
|
// from the same page that the failure stopped before they ran (#782), and
|
|
3135
|
-
// are just as claimed, so they ride along without a strike.
|
|
3136
|
-
|
|
3202
|
+
// are just as claimed, so they ride along without a strike. `pastCap` events
|
|
3203
|
+
// were left out of a turn, which is no failure of theirs: no strike, and
|
|
3204
|
+
// PAST_CAP_MAX_CARRIES bounds them instead. A later group's hold in the same
|
|
3205
|
+
// tick adds to the carry, never replaces it.
|
|
3206
|
+
const holdForRetry = async (failedEvents, untriedEvents = [], pastCap = []) => {
|
|
3137
3207
|
const released = [];
|
|
3138
3208
|
const carry = [];
|
|
3139
3209
|
const seen = new Set();
|
|
@@ -3142,7 +3212,9 @@ export const performRun = ({
|
|
|
3142
3212
|
if (!seen.has(id)) {
|
|
3143
3213
|
seen.add(id);
|
|
3144
3214
|
const failures = (heldRetryFailures.get(id) || 0) + 1;
|
|
3145
|
-
|
|
3215
|
+
// A stop wins over the local limit: the sweep would make this event
|
|
3216
|
+
// wait 10–20 minutes, and the server's ceiling bounds a hand-back.
|
|
3217
|
+
if (running && failures >= HELD_RETRY_MAX_FAILURES) released.push(event);
|
|
3146
3218
|
else {
|
|
3147
3219
|
heldRetryFailures.set(id, failures);
|
|
3148
3220
|
carry.push(event);
|
|
@@ -3156,23 +3228,113 @@ export const performRun = ({
|
|
|
3156
3228
|
carry.push(event);
|
|
3157
3229
|
}
|
|
3158
3230
|
}
|
|
3231
|
+
const passedOver = [];
|
|
3232
|
+
for (const event of pastCap) {
|
|
3233
|
+
const id = String(event._id);
|
|
3234
|
+
if (!seen.has(id)) {
|
|
3235
|
+
seen.add(id);
|
|
3236
|
+
const carries = (pastCapCarries.get(id) || 0) + 1;
|
|
3237
|
+
// As above: a stop hands the item back rather than leave it to the sweep.
|
|
3238
|
+
if (running && carries >= PAST_CAP_MAX_CARRIES) passedOver.push(event);
|
|
3239
|
+
else {
|
|
3240
|
+
pastCapCarries.set(id, carries);
|
|
3241
|
+
carry.push(event);
|
|
3242
|
+
}
|
|
3243
|
+
}
|
|
3244
|
+
}
|
|
3245
|
+
if (passedOver.length) {
|
|
3246
|
+
log(
|
|
3247
|
+
`[inbox.batch] releasing ${countEvents(passedOver.length)} after ${PAST_CAP_MAX_CARRIES} turns past the prompt cap `
|
|
3248
|
+
+ 'to the server\'s requeue (redelivered in ~10–20 min)',
|
|
3249
|
+
);
|
|
3250
|
+
}
|
|
3159
3251
|
// A stopping run carries nothing and renews nothing: extending the lease
|
|
3160
3252
|
// of a process on its way out only delays the replacement's redelivery.
|
|
3161
|
-
|
|
3162
|
-
|
|
3163
|
-
|
|
3164
|
-
|
|
3165
|
-
|
|
3166
|
-
|
|
3253
|
+
// Its carry is handed back to the server when the tick ends instead
|
|
3254
|
+
// (F189), so the next run answers it on its first poll.
|
|
3255
|
+
if (!running) {
|
|
3256
|
+
heldRetry = [...heldRetry, ...carry];
|
|
3257
|
+
heldRetryFailures.clear();
|
|
3258
|
+
pastCapCarries.clear();
|
|
3259
|
+
return { held: [], released, handedBack: carry };
|
|
3260
|
+
}
|
|
3261
|
+
const confirmed = carry.length ? await renewHeldDeliveries(carry) : null;
|
|
3262
|
+
// The stop landed during that renewal, after the limits were applied.
|
|
3263
|
+
if (!running) tickUnsettled.push(...released, ...passedOver);
|
|
3264
|
+
const held = carry.filter((event) => stillHeld(event, confirmed));
|
|
3265
|
+
const dropped = carry.filter((event) => !stillHeld(event, confirmed));
|
|
3266
|
+
heldRetry = [...heldRetry, ...held];
|
|
3267
|
+
const heldIds = new Set(heldRetry.map((event) => String(event._id)));
|
|
3268
|
+
for (const counts of [heldRetryFailures, pastCapCarries]) {
|
|
3269
|
+
for (const id of [...counts.keys()]) {
|
|
3270
|
+
if (!heldIds.has(id)) counts.delete(id);
|
|
3271
|
+
}
|
|
3167
3272
|
}
|
|
3168
|
-
heldRetry = held;
|
|
3169
3273
|
if (dropped.length) {
|
|
3170
3274
|
log(
|
|
3171
3275
|
`[inbox.batch] ${countEvents(dropped.length)} no longer held by this runner on the server `
|
|
3172
3276
|
+ '(checkpointed, requeued or retired) — not retried',
|
|
3173
3277
|
);
|
|
3174
3278
|
}
|
|
3175
|
-
return { held, released };
|
|
3279
|
+
return { held, released, handedBack: [] };
|
|
3280
|
+
};
|
|
3281
|
+
|
|
3282
|
+
// F189/F182: hand claimed events this run will never run back to the
|
|
3283
|
+
// server as pending, so the next run claims them on its first poll. Without
|
|
3284
|
+
// this they stay `delivered` to a dead process until the requeue sweep
|
|
3285
|
+
// (10–20 min, one attempt spent per lap): a walk on 2026-11-05 restarted a
|
|
3286
|
+
// circuit-open seat five times and each restart's mention waited on that
|
|
3287
|
+
// sweep. Nonce-bound on the server, so nothing a replacement holds can be
|
|
3288
|
+
// released, and an event this run released can no longer be acked by it.
|
|
3289
|
+
// The server refuses an event at its redelivery ceiling (`atCeiling`); it
|
|
3290
|
+
// stays delivered and the requeue retires it, which bounds a poison event.
|
|
3291
|
+
// Best effort: an older server without the route, or a failed request,
|
|
3292
|
+
// leaves them to the requeue as before, and says so.
|
|
3293
|
+
const handBackDeliveries = async (events, why) => {
|
|
3294
|
+
const seen = new Set();
|
|
3295
|
+
const deliveries = [];
|
|
3296
|
+
for (const event of events) {
|
|
3297
|
+
const id = String(event._id);
|
|
3298
|
+
if (deliveryIdOf(event) && !seen.has(id)) {
|
|
3299
|
+
seen.add(id);
|
|
3300
|
+
deliveries.push({ eventId: id, deliveryId: deliveryIdOf(event) });
|
|
3301
|
+
}
|
|
3302
|
+
}
|
|
3303
|
+
if (deliveries.length === 0) return [];
|
|
3304
|
+
const released = [];
|
|
3305
|
+
const atCeiling = [];
|
|
3306
|
+
try {
|
|
3307
|
+
for (let offset = 0; offset < deliveries.length; offset += 50) {
|
|
3308
|
+
// eslint-disable-next-line no-await-in-loop
|
|
3309
|
+
const response = await client.post('/api/agents/runtime/events/release', {
|
|
3310
|
+
events: deliveries.slice(offset, offset + 50),
|
|
3311
|
+
});
|
|
3312
|
+
if (Array.isArray(response?.released)) released.push(...response.released.map(String));
|
|
3313
|
+
if (Array.isArray(response?.atCeiling)) atCeiling.push(...response.atCeiling.map(String));
|
|
3314
|
+
}
|
|
3315
|
+
} catch (error) {
|
|
3316
|
+
log(
|
|
3317
|
+
`[inbox.batch] could not hand back ${countEvents(deliveries.length - released.length)} on ${why} `
|
|
3318
|
+
+ `(${error.message}) — the server's requeue redelivers them in ~10–20 min`,
|
|
3319
|
+
);
|
|
3320
|
+
return released;
|
|
3321
|
+
}
|
|
3322
|
+
log(`[inbox.batch] handed back ${countEvents(released.length)} held on ${why}, for the next run to answer`);
|
|
3323
|
+
if (atCeiling.length) {
|
|
3324
|
+
log(`[inbox.batch] ${countEvents(atCeiling.length)} left to the requeue (at the redelivery ceiling)`);
|
|
3325
|
+
}
|
|
3326
|
+
return released;
|
|
3327
|
+
};
|
|
3328
|
+
// Everything a stopped run still carries. Taken once: the arrays are emptied
|
|
3329
|
+
// before the request, so a second caller finds nothing to send.
|
|
3330
|
+
const handBackCarryOnStop = () => {
|
|
3331
|
+
const carry = [...heldRetry, ...heldGreeting, ...tickUnsettled];
|
|
3332
|
+
heldRetry = [];
|
|
3333
|
+
heldGreeting = [];
|
|
3334
|
+
tickUnsettled = [];
|
|
3335
|
+
heldRetryFailures.clear();
|
|
3336
|
+
clearHeldRenewal();
|
|
3337
|
+
return carry.length ? handBackDeliveries(carry, 'stop') : Promise.resolve([]);
|
|
3176
3338
|
};
|
|
3177
3339
|
|
|
3178
3340
|
// Keep a long wait's leases alive. A wait shorter than one renewal interval
|
|
@@ -3202,9 +3364,13 @@ export const performRun = ({
|
|
|
3202
3364
|
};
|
|
3203
3365
|
|
|
3204
3366
|
// What the retry will actually do, for the failure report.
|
|
3205
|
-
const describeHold = ({ held, released }, retry) => {
|
|
3367
|
+
const describeHold = ({ held, released, handedBack = [] }, retry) => {
|
|
3206
3368
|
const parts = [];
|
|
3207
3369
|
if (retry.circuitOpen) parts.push('circuit open');
|
|
3370
|
+
if (handedBack.length) {
|
|
3371
|
+
return [...parts, `stopping: handing ${countEvents(handedBack.length)} back to the server for the next run`]
|
|
3372
|
+
.join('; ');
|
|
3373
|
+
}
|
|
3208
3374
|
if (released.length) {
|
|
3209
3375
|
parts.push(
|
|
3210
3376
|
`releasing ${countEvents(released.length)} after ${HELD_RETRY_MAX_FAILURES} failed attempts `
|
|
@@ -3218,6 +3384,7 @@ export const performRun = ({
|
|
|
3218
3384
|
};
|
|
3219
3385
|
|
|
3220
3386
|
const handleEventBatch = async (events, inboxCount = events.length) => {
|
|
3387
|
+
pastCapEvents = [];
|
|
3221
3388
|
const eventPodId = events[0]?.podId || podId;
|
|
3222
3389
|
if (!eventPodId || events.some((event) => (event.podId || podId) !== eventPodId)) {
|
|
3223
3390
|
throw new Error('Inbox batch crossed pod boundaries; refusing to mix private pod context');
|
|
@@ -3275,6 +3442,22 @@ export const performRun = ({
|
|
|
3275
3442
|
? new Set(preSpawn.map((message) => String(message._id || message.id)))
|
|
3276
3443
|
: null;
|
|
3277
3444
|
const activeEntries = entries.filter((entry) => !entry.result);
|
|
3445
|
+
// The server settles a digest on its holder's ack, up to the holder's
|
|
3446
|
+
// snapshot. A holder this turn shows settles what it shows, so every digest
|
|
3447
|
+
// in hand goes in its prompt, whoever else waits. With no holder shown, a
|
|
3448
|
+
// holder that waits takes them along, so they settle on a turn that shows
|
|
3449
|
+
// them. A holder with a result settles nothing here: its reason doesn't count
|
|
3450
|
+
// as reading a digest.
|
|
3451
|
+
const carryPastCap = () => {
|
|
3452
|
+
const isHolder = (entry) => holdsDigest(entry.event);
|
|
3453
|
+
const shownHolder = wakingEntries.some((entry) => !entry.pastCap && !entry.result && isHolder(entry));
|
|
3454
|
+
if (!shownHolder && wakingEntries.some((entry) => entry.pastCap && isHolder(entry))) {
|
|
3455
|
+
for (const entry of activeEntries) {
|
|
3456
|
+
if (entry.event.deliveryMode === 'digest') entry.pastCap = true;
|
|
3457
|
+
}
|
|
3458
|
+
}
|
|
3459
|
+
pastCapEvents = entries.filter((entry) => entry.pastCap).map((entry) => entry.event);
|
|
3460
|
+
};
|
|
3278
3461
|
// Ambient digest entries provide context for this turn, but must never
|
|
3279
3462
|
// determine its admission or count as the trigger that woke the seat.
|
|
3280
3463
|
const wakingEntries = activeEntries.filter((entry) => entry.event.deliveryMode !== 'digest');
|
|
@@ -3306,9 +3489,18 @@ export const performRun = ({
|
|
|
3306
3489
|
admissionEvent.event.payload,
|
|
3307
3490
|
);
|
|
3308
3491
|
if (!admission.allowed) {
|
|
3492
|
+
// An agent row refuses the page, and that shouldn't cost a row nobody
|
|
3493
|
+
// can place: a person's message that scrolled off the snapshot reads
|
|
3494
|
+
// `unknown`. It is carried, past-cap bound included, and alone in its
|
|
3495
|
+
// next page it can't be refused, because only 'agent' ever is. A row
|
|
3496
|
+
// stamped as an agent's is placed, so it stays refused, or it would walk
|
|
3497
|
+
// past this brake. A legacy row has no stamp, and still carries.
|
|
3309
3498
|
for (const entry of wakingEntries) {
|
|
3310
|
-
entry.
|
|
3499
|
+
if (entry.trigger === 'unknown' && entry.event.payload?.messageId
|
|
3500
|
+
&& entry.event.payload?.senderIsHuman !== false) entry.pastCap = true;
|
|
3501
|
+
else entry.result = batchAdmissionResult(entry.event, admission);
|
|
3311
3502
|
}
|
|
3503
|
+
carryPastCap();
|
|
3312
3504
|
return { entries };
|
|
3313
3505
|
}
|
|
3314
3506
|
}
|
|
@@ -3325,10 +3517,9 @@ export const performRun = ({
|
|
|
3325
3517
|
}
|
|
3326
3518
|
}
|
|
3327
3519
|
|
|
3328
|
-
|
|
3329
|
-
if (entry.event.deliveryMode === 'digest') continue;
|
|
3520
|
+
const claimEntry = async (entry) => {
|
|
3330
3521
|
const messageId = entry.event.payload?.messageId;
|
|
3331
|
-
if (!messageId || !CLAIMABLE_EVENT_TYPES.has(entry.event.type))
|
|
3522
|
+
if (!messageId || !CLAIMABLE_EVENT_TYPES.has(entry.event.type)) return;
|
|
3332
3523
|
const claimKeeper = createClaimKeeper(client, {
|
|
3333
3524
|
messageId,
|
|
3334
3525
|
podId: eventPodId,
|
|
@@ -3352,7 +3543,7 @@ export const performRun = ({
|
|
|
3352
3543
|
// read-only so a generic broadcast cannot widen itself after CAS.
|
|
3353
3544
|
if (entry.event.payload?.repliesToYourMessage === true || entry.event.payload?.addressed === true) {
|
|
3354
3545
|
entry.peerFrame = peerHoldsFrame(claim.holder, messageId);
|
|
3355
|
-
|
|
3546
|
+
return;
|
|
3356
3547
|
}
|
|
3357
3548
|
// D3a's partition: no later model judgement can widen this item's
|
|
3358
3549
|
// authority after a peer won the binding CAS.
|
|
@@ -3365,9 +3556,23 @@ export const performRun = ({
|
|
|
3365
3556
|
} else {
|
|
3366
3557
|
log(`[${entry.event.type}] claim unavailable (${claim.error?.message || 'unknown error'}) — proceeding unguarded`);
|
|
3367
3558
|
}
|
|
3559
|
+
};
|
|
3560
|
+
|
|
3561
|
+
// The cap is spent here, not when the prompt is built: a lost claim gives its
|
|
3562
|
+
// slot back, and an item past the cap is never claimed, because a seat that
|
|
3563
|
+
// isn't reading a message must not hold a peer off it.
|
|
3564
|
+
let slotsLeft = batchLimit;
|
|
3565
|
+
for (const entry of [...wakingEntries].sort((a, b) => capRank(a) - capRank(b))) {
|
|
3566
|
+
if (slotsLeft === 0) entry.pastCap = true;
|
|
3567
|
+
else {
|
|
3568
|
+
// eslint-disable-next-line no-await-in-loop
|
|
3569
|
+
await claimEntry(entry);
|
|
3570
|
+
if (!entry.result) slotsLeft -= 1;
|
|
3571
|
+
}
|
|
3368
3572
|
}
|
|
3573
|
+
carryPastCap();
|
|
3369
3574
|
|
|
3370
|
-
const actionEntries = wakingEntries.filter((entry) => !entry.result);
|
|
3575
|
+
const actionEntries = wakingEntries.filter((entry) => !entry.result && !entry.pastCap);
|
|
3371
3576
|
if (actionEntries.length === 0) return { entries };
|
|
3372
3577
|
|
|
3373
3578
|
const claimKeepers = actionEntries
|
|
@@ -3383,8 +3588,8 @@ export const performRun = ({
|
|
|
3383
3588
|
type: 'inbox.batch',
|
|
3384
3589
|
payload: {
|
|
3385
3590
|
...actionEntries[0].event.payload,
|
|
3386
|
-
batchEventIds: entries.map((entry) => String(entry.event._id)),
|
|
3387
|
-
hasHeartbeat: entries.some((entry) => entry.event.type === 'heartbeat'),
|
|
3591
|
+
batchEventIds: entries.filter((entry) => !entry.pastCap).map((entry) => String(entry.event._id)),
|
|
3592
|
+
hasHeartbeat: entries.some((entry) => !entry.pastCap && entry.event.type === 'heartbeat'),
|
|
3388
3593
|
},
|
|
3389
3594
|
};
|
|
3390
3595
|
// One item, run as its own turn. Used only after the provider refused a
|
|
@@ -3438,7 +3643,7 @@ export const performRun = ({
|
|
|
3438
3643
|
|| (turnResult?.outcome === 'no_action'
|
|
3439
3644
|
&& (!turnResult?.reason || turnResult.reason === 'silent_or_empty'))) {
|
|
3440
3645
|
for (const entry of activeEntries) {
|
|
3441
|
-
if (entry.event.deliveryMode === 'digest') {
|
|
3646
|
+
if (entry.event.deliveryMode === 'digest' && !entry.pastCap) {
|
|
3442
3647
|
entry.result = { outcome: 'no_action', reason: 'digest-read' };
|
|
3443
3648
|
}
|
|
3444
3649
|
}
|
|
@@ -3537,16 +3742,37 @@ export const performRun = ({
|
|
|
3537
3742
|
try { return await turn(); } finally { pollLiveness.arm(); }
|
|
3538
3743
|
};
|
|
3539
3744
|
|
|
3745
|
+
// The scheduled next tick, so stop() can cancel it: a stopped run idling in
|
|
3746
|
+
// a 15-minute circuit wait otherwise stays alive until the timer fires,
|
|
3747
|
+
// which is why a walk on 2026-11-05 needed repeated signals to end it.
|
|
3748
|
+
let tickTimer = null;
|
|
3749
|
+
let tickInFlight = false;
|
|
3750
|
+
// stop() returns this: it resolves once the hand-back has answered, whether
|
|
3751
|
+
// the idle stop or the in-flight tick's end performs it.
|
|
3752
|
+
let stopped = null;
|
|
3753
|
+
let resolveStopped = () => {};
|
|
3540
3754
|
const tick = async () => {
|
|
3755
|
+
tickTimer = null;
|
|
3541
3756
|
if (!running) return;
|
|
3757
|
+
tickInFlight = true;
|
|
3758
|
+
try { await runTick(); } finally {
|
|
3759
|
+
tickInFlight = false;
|
|
3760
|
+
if (!running) {
|
|
3761
|
+
try { await handBackCarryOnStop(); } finally { resolveStopped(); }
|
|
3762
|
+
}
|
|
3763
|
+
}
|
|
3764
|
+
};
|
|
3765
|
+
|
|
3766
|
+
const runTick = async () => {
|
|
3767
|
+
tickUnsettled = [];
|
|
3542
3768
|
let nextPollDelayMs = intervalMs;
|
|
3543
3769
|
let completedModelTurn = false;
|
|
3544
3770
|
let retryScheduled = false;
|
|
3545
3771
|
try {
|
|
3546
3772
|
// ADR-024 D3: this is an inbox fetch, not a stream of interrupts. The
|
|
3547
|
-
// backend returns one pod's oldest pending batch, so
|
|
3548
|
-
//
|
|
3549
|
-
//
|
|
3773
|
+
// backend returns one pod's oldest pending batch, so the rows below belong
|
|
3774
|
+
// to one composite turn, and what doesn't fit its prompt is carried to the
|
|
3775
|
+
// next (F226). The service selects the pod before claiming any row;
|
|
3550
3776
|
// do not reintroduce a mixed-pod `limit: 10` here.
|
|
3551
3777
|
// TASK-317: name this CLI for the chat chip. The server keeps it as a
|
|
3552
3778
|
// display-only label (runtime.reportedCli) and never as a binding.
|
|
@@ -3567,8 +3793,9 @@ export const performRun = ({
|
|
|
3567
3793
|
});
|
|
3568
3794
|
pollLiveness.arm();
|
|
3569
3795
|
// A stall fired while this request hung: the loop has already given up on
|
|
3570
|
-
// it
|
|
3571
|
-
//
|
|
3796
|
+
// it, and `agent run` exits in onStall, so its page is left to the requeue.
|
|
3797
|
+
// A plain stop() falls through instead, to the group loop's own `running`
|
|
3798
|
+
// check, which hands the page back.
|
|
3572
3799
|
if (pollStalled) return;
|
|
3573
3800
|
const { events = [], inboxCount } = page;
|
|
3574
3801
|
consecutiveAuthErrors = 0;
|
|
@@ -3611,8 +3838,8 @@ export const performRun = ({
|
|
|
3611
3838
|
eventGroups.set(key, group);
|
|
3612
3839
|
}
|
|
3613
3840
|
// A carried greeting must not push a full page past the prompt's body
|
|
3614
|
-
// cap
|
|
3615
|
-
//
|
|
3841
|
+
// cap (CodeRabbit on #3511). It goes back to the greeting hold instead
|
|
3842
|
+
// of the past-cap carry.
|
|
3616
3843
|
for (const [key, group] of eventGroups) {
|
|
3617
3844
|
const wakeCount = group.filter((event) => event.deliveryMode !== 'digest').length;
|
|
3618
3845
|
if (wakeCount <= batchLimit) continue;
|
|
@@ -3626,18 +3853,30 @@ export const performRun = ({
|
|
|
3626
3853
|
// The greeting window is counted once per poll, however many pods'
|
|
3627
3854
|
// greetings this page carries (gate review of #3511).
|
|
3628
3855
|
let greetingCarriedThisPoll = false;
|
|
3629
|
-
// The
|
|
3630
|
-
//
|
|
3856
|
+
// The events a failure at `groupIndex` stops before they run. A later
|
|
3857
|
+
// digest-only group never starts a turn of its own, so a retry does not
|
|
3858
|
+
// carry it.
|
|
3631
3859
|
const untriedAfter = (groupIndex) => groups.slice(groupIndex + 1)
|
|
3632
3860
|
.filter((later) => !later.every((event) => event.deliveryMode === 'digest'))
|
|
3633
3861
|
.flat();
|
|
3862
|
+
// Every later group a stop must hand back. The server claims a digest
|
|
3863
|
+
// alongside a wake on the same page, and a private wake groups apart
|
|
3864
|
+
// from it, so a digest-only group here can be `delivered` to this run.
|
|
3865
|
+
const claimedAfter = (groupIndex) => groups.slice(groupIndex + 1).flat();
|
|
3634
3866
|
for (let groupIndex = 0; groupIndex < groups.length; groupIndex += 1) {
|
|
3635
3867
|
const group = groups[groupIndex];
|
|
3636
|
-
if (!running)
|
|
3868
|
+
if (!running) {
|
|
3869
|
+
// Claimed by this tick's poll (or carried into it) and never run:
|
|
3870
|
+
// a stop that arrived mid-poll or mid-page (F182). Handed back when
|
|
3871
|
+
// the tick ends.
|
|
3872
|
+
heldRetry = [...heldRetry, ...group, ...claimedAfter(groupIndex)];
|
|
3873
|
+
break;
|
|
3874
|
+
}
|
|
3637
3875
|
// A current kernel never claims a digest-only page. Guard a mixed
|
|
3638
3876
|
// version rollout too: an ambient batch must not start a model turn.
|
|
3639
3877
|
if (group.every((event) => event.deliveryMode === 'digest')) {
|
|
3640
3878
|
log(`[inbox.batch] digest-only page (${group.length}) — no spawn or ack`);
|
|
3879
|
+
tickUnsettled.push(...group);
|
|
3641
3880
|
continue;
|
|
3642
3881
|
}
|
|
3643
3882
|
if ((group[0].podId || podId) && !PRIVATE_RESPONSE_EVENT_TYPES.has(group[0].type)) {
|
|
@@ -3685,7 +3924,12 @@ export const performRun = ({
|
|
|
3685
3924
|
});
|
|
3686
3925
|
nextPollDelayMs = retry.delayMs;
|
|
3687
3926
|
retryScheduled = true;
|
|
3688
|
-
|
|
3927
|
+
tickUnsettled.push(...claimedAfter(groupIndex));
|
|
3928
|
+
const hold = await holdForRetry(
|
|
3929
|
+
failedEvents.filter((event) => !pastCapEvents.includes(event)),
|
|
3930
|
+
untriedAfter(groupIndex),
|
|
3931
|
+
pastCapEvents,
|
|
3932
|
+
);
|
|
3689
3933
|
keepHeldLeases(retry.delayMs);
|
|
3690
3934
|
const event = group[0];
|
|
3691
3935
|
const wrapped = new Error(
|
|
@@ -3769,6 +4013,13 @@ export const performRun = ({
|
|
|
3769
4013
|
);
|
|
3770
4014
|
break;
|
|
3771
4015
|
}
|
|
4016
|
+
// Items a stop cut off before they ran (refusal isolation checks
|
|
4017
|
+
// `running`), and any digest this batch left unread.
|
|
4018
|
+
tickUnsettled.push(...batch.entries.filter((entry) => !entry.result).map((entry) => entry.event));
|
|
4019
|
+
if (pastCapEvents.length) {
|
|
4020
|
+
await holdForRetry([], [], pastCapEvents);
|
|
4021
|
+
keepHeldLeases(nextPollDelayMs);
|
|
4022
|
+
}
|
|
3772
4023
|
continue;
|
|
3773
4024
|
}
|
|
3774
4025
|
const [event] = group;
|
|
@@ -3802,6 +4053,7 @@ export const performRun = ({
|
|
|
3802
4053
|
retryScheduled = true;
|
|
3803
4054
|
// Carried to the retry like a failed batch: the event is claimed,
|
|
3804
4055
|
// so the next poll would not return it.
|
|
4056
|
+
tickUnsettled.push(...claimedAfter(groupIndex));
|
|
3805
4057
|
// eslint-disable-next-line no-await-in-loop
|
|
3806
4058
|
const hold = await holdForRetry([event], untriedAfter(groupIndex));
|
|
3807
4059
|
keepHeldLeases(retry.delayMs);
|
|
@@ -3877,6 +4129,7 @@ export const performRun = ({
|
|
|
3877
4129
|
if (terminalError) {
|
|
3878
4130
|
running = false;
|
|
3879
4131
|
onError?.(terminalError);
|
|
4132
|
+
tickUnsettled.push(...claimedAfter(groupIndex));
|
|
3880
4133
|
return;
|
|
3881
4134
|
}
|
|
3882
4135
|
onError?.(new Error(`Ack failed for ${event._id}: ${ackErr.message}`));
|
|
@@ -3929,15 +4182,26 @@ export const performRun = ({
|
|
|
3929
4182
|
// names it, and a sibling's completed turn must not shorten the backoff.
|
|
3930
4183
|
// A wait the loop schedules itself always ends, so it is not a stall.
|
|
3931
4184
|
pollLiveness.pause();
|
|
3932
|
-
if (running) setTimeoutImpl(tick, completedModelTurn && !retryScheduled ? 3000 : nextPollDelayMs);
|
|
4185
|
+
if (running) tickTimer = setTimeoutImpl(tick, completedModelTurn && !retryScheduled ? 3000 : nextPollDelayMs);
|
|
3933
4186
|
};
|
|
3934
4187
|
|
|
3935
4188
|
tick();
|
|
3936
4189
|
return {
|
|
4190
|
+
// Resolves once anything held has been handed back. A tick in flight hands
|
|
4191
|
+
// back its own carry when it ends; an idle run does it here. A second
|
|
4192
|
+
// stop() returns the same promise.
|
|
3937
4193
|
stop: () => {
|
|
4194
|
+
if (stopped) return stopped;
|
|
4195
|
+
stopped = new Promise((resolve) => { resolveStopped = resolve; });
|
|
3938
4196
|
running = false;
|
|
3939
4197
|
pollLiveness.stop();
|
|
3940
4198
|
clearHeldRenewal();
|
|
4199
|
+
if (tickTimer !== null) {
|
|
4200
|
+
clearTimeoutImpl(tickTimer);
|
|
4201
|
+
tickTimer = null;
|
|
4202
|
+
}
|
|
4203
|
+
if (!tickInFlight) handBackCarryOnStop().finally(() => resolveStopped());
|
|
4204
|
+
return stopped;
|
|
3941
4205
|
},
|
|
3942
4206
|
};
|
|
3943
4207
|
};
|