@thready-web/dsh-bot-plugin 0.1.0-beta.3 → 0.1.0-beta.5

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.
@@ -2,7 +2,7 @@ import { randomUUID } from 'node:crypto';
2
2
  import { sha256 } from "./fs-safe.js";
3
3
  import { toolError } from "./errors.js";
4
4
  import { createTicketBook } from "./work-tickets.js";
5
- import { decideAdmission, budgetTrip, estimateWorkerBudget, DEFAULT_WORK_BUDGET } from "./work-admit.js";
5
+ import { decideAdmission, budgetTrip, estimateWorkerBudget, DEFAULT_WORK_BUDGET, effectiveWorkLimits, } from "./work-admit.js";
6
6
  import { messageIdOf, pushInput, pushNotice, reasonKindOf, reconcileInputs, reconcileNotices, renderNotice, } from "./work-ledger.js";
7
7
  const KINDS = new Set(['progress', 'question', 'artifact', 'result', 'failure']);
8
8
  const LABEL = {
@@ -26,10 +26,15 @@ export const BUDGET_NOTICE_PREFIX = 'foundations-budget:';
26
26
  export function createWorkRuntime(store, engine, options = {}) {
27
27
  const now = options.now ?? (() => new Date());
28
28
  const limits = { ...DEFAULT_WORK_BUDGET, ...options.budget };
29
+ const slots = effectiveWorkLimits(limits);
29
30
  const pid = options.pid ?? process.pid;
30
31
  const resident = options.resident !== false;
31
32
  const book = createTicketBook(store.db, store.root, now);
32
33
  const inflight = new Set();
34
+ /** Ticket ids currently in ensureChild / flush, so a tick cannot start the same ticket twice. */
35
+ const starting = new Set();
36
+ /** In-flight startReserved promises, so a later promoteQueued can join them. */
37
+ const startingPromises = new Map();
33
38
  /** @type {Map<string, { messageId: string, turn: number | null, seq: number | null }>} */
34
39
  const turns = new Map();
35
40
  /** @type {Map<string, string>} */
@@ -38,6 +43,8 @@ export function createWorkRuntime(store, engine, options = {}) {
38
43
  const arms = new Map();
39
44
  /** @type {Map<string, number>} */
40
45
  const holds = new Map();
46
+ const admitting = new Set();
47
+ const budgetWaiters = new Map();
41
48
  const schedule = options.schedule ??
42
49
  ((fn, ms) => {
43
50
  const timer = setTimeout(fn, ms);
@@ -75,9 +82,11 @@ export function createWorkRuntime(store, engine, options = {}) {
75
82
  releaseHold,
76
83
  tick,
77
84
  admit,
85
+ promoteQueued,
78
86
  observeSession,
79
87
  observeClaimed,
80
88
  gateStep,
89
+ waitForBudget,
81
90
  isWorkerSession,
82
91
  summaries: (owner) => book.summaries(owner),
83
92
  detail: (id) => book.detail(id),
@@ -92,6 +101,171 @@ export function createWorkRuntime(store, engine, options = {}) {
92
101
  function isWorkerSession(sessionId) {
93
102
  return book.attemptForChild(sessionId) != null;
94
103
  }
104
+ /**
105
+ * A running slot is free when both the per-bot and global running caps have room.
106
+ * needs_user does not occupy a running slot.
107
+ * @param {string} owner
108
+ */
109
+ function hasSlot(owner) {
110
+ return book.countSlots(owner) < slots.perBot && book.countGlobalSlots() < slots.global;
111
+ }
112
+ /**
113
+ * Queue room is separate from running slots. The combined unfinished cap is
114
+ * slots + queue, so parked needs_user tickets cannot grow without bound.
115
+ * @param {string} owner
116
+ */
117
+ function canQueue(owner) {
118
+ return (book.countQueued(owner) < slots.perBotQueue &&
119
+ book.countGlobalQueued() < slots.globalQueue &&
120
+ book.countActive(owner) < slots.perBot + slots.perBotQueue &&
121
+ book.countGlobal() < slots.global + slots.globalQueue);
122
+ }
123
+ /**
124
+ * @param {string} owner
125
+ * @returns {'start' | 'queue' | 'refuse'}
126
+ */
127
+ function underCombinedCap(owner) {
128
+ return (book.countActive(owner) < slots.perBot + slots.perBotQueue &&
129
+ book.countGlobal() < slots.global + slots.globalQueue);
130
+ }
131
+ /**
132
+ * @param {string} owner
133
+ * @returns {'start' | 'queue' | 'refuse'}
134
+ */
135
+ function decideDispatch(owner) {
136
+ // needs_user does not occupy a running slot, but it still counts as unfinished.
137
+ // A free slot must not admit a new ticket once the combined cap is full.
138
+ if (!underCombinedCap(owner))
139
+ return 'refuse';
140
+ if (hasSlot(owner))
141
+ return 'start';
142
+ if (canQueue(owner))
143
+ return 'queue';
144
+ return 'refuse';
145
+ }
146
+ /**
147
+ * Create the child if needed and flush the current briefing or supply.
148
+ * @param {Record<string, any>} ticket
149
+ */
150
+ async function startReserved(ticket) {
151
+ if (!resident) {
152
+ return { ...publicDispatch(ticket), flush: 'pending', resident: false, message: 'Only a resident host continues' };
153
+ }
154
+ if (starting.has(ticket.id)) {
155
+ return startingPromises.get(ticket.id) ?? publicDispatch(book.ticket(ticket.id) ?? ticket, { flush: 'pending' });
156
+ }
157
+ starting.add(ticket.id);
158
+ const run = (async () => {
159
+ const current = book.ticket(ticket.id);
160
+ if (!current)
161
+ return toolError('NOT_FOUND', 'No such ticket.');
162
+ if (current.status === 'cancelled' || current.status === 'failed' || current.status === 'delivered') {
163
+ return publicDispatch(current);
164
+ }
165
+ const created = await ensureChild(current);
166
+ if (created.error)
167
+ return created.error;
168
+ if (created.rejected)
169
+ return publicDispatch(created.ticket ?? book.ticket(ticket.id) ?? current);
170
+ const liveAfterStart = book.ticket(ticket.id);
171
+ if (!liveAfterStart ||
172
+ liveAfterStart.status === 'cancelled' ||
173
+ liveAfterStart.status === 'failed' ||
174
+ liveAfterStart.status === 'delivered') {
175
+ return publicDispatch(liveAfterStart ?? current);
176
+ }
177
+ const input = await store.write('work', () => {
178
+ const live = book.ticket(ticket.id);
179
+ const liveAttempt = book.attempt(live.active_attempt_id);
180
+ if (liveAttempt?.current_input_id) {
181
+ const existing = book.input(liveAttempt.current_input_id);
182
+ if (existing)
183
+ return existing;
184
+ }
185
+ const text = book.readBriefing(live.briefing_hash);
186
+ return book.insertInput({
187
+ ticketId: live.id,
188
+ attemptId: liveAttempt.id,
189
+ revision: live.briefing_revision,
190
+ kind: 'briefing',
191
+ text,
192
+ });
193
+ });
194
+ if (input.flush_status === 'accepted') {
195
+ return publicDispatch(book.ticket(ticket.id), { flush: 'accepted', nativeMessageId: input.native_message_id });
196
+ }
197
+ const flushed = await pushInput(book, input, deps);
198
+ return publicDispatch(book.ticket(ticket.id), {
199
+ flush: flushWord(flushed),
200
+ nativeMessageId: input.native_message_id,
201
+ });
202
+ })();
203
+ startingPromises.set(ticket.id, run);
204
+ try {
205
+ return await run;
206
+ }
207
+ finally {
208
+ starting.delete(ticket.id);
209
+ startingPromises.delete(ticket.id);
210
+ }
211
+ }
212
+ /**
213
+ * Start waiting tickets that now fit a slot. Awaits only starts this call
214
+ * launched, so a hung createChild from an earlier call cannot deadlock
215
+ * later ticks or cancel. The earlier promise stays owned until it finishes
216
+ * and is then attached or cleaned up.
217
+ */
218
+ async function promoteQueued() {
219
+ if (!resident)
220
+ return { started: [] };
221
+ const started = [];
222
+ const launched = [];
223
+ const launch = (ticket) => {
224
+ if (!ticket?.id || starting.has(ticket.id))
225
+ return;
226
+ started.push(ticket.id);
227
+ launched.push(startReserved(ticket).catch(error => {
228
+ options.log?.('warn', `session-foundations: startReserved failed ticket=${ticket.id} ${error instanceof Error ? error.message : String(error)}`);
229
+ return error;
230
+ }));
231
+ };
232
+ for (const ticket of book.heldStarts())
233
+ launch(ticket);
234
+ for (;;) {
235
+ const claimed = await store.write('work', () => {
236
+ for (const ticket of book.nextPromotable()) {
237
+ if (book.slotHeld(ticket))
238
+ continue;
239
+ if (ticket.status !== 'accepted' && ticket.status !== 'needs_user')
240
+ continue;
241
+ if (!hasSlot(ticket.owner_session_id))
242
+ continue;
243
+ return book.reserveSlot(ticket.id);
244
+ }
245
+ return null;
246
+ });
247
+ if (!claimed)
248
+ break;
249
+ launch(claimed);
250
+ }
251
+ await Promise.all(launched);
252
+ // Join in-flight starts that are still allowed to attach. Do not join a
253
+ // hung createChild after the ticket has already been cancelled, failed, or
254
+ // expired, so a later tick can free the slot and start the next waiter.
255
+ const pending = [...startingPromises.entries()]
256
+ .filter(([id]) => {
257
+ const ticket = book.ticket(id);
258
+ return (ticket != null && ticket.status !== 'failed' && ticket.status !== 'cancelled' && ticket.status !== 'delivered');
259
+ })
260
+ .map(([, promise]) => promise);
261
+ await Promise.all(pending);
262
+ return { started };
263
+ }
264
+ function schedulePromote() {
265
+ void promoteQueued().catch(error => {
266
+ options.log?.('warn', `session-foundations: promoteQueued failed ${error instanceof Error ? error.message : String(error)}`);
267
+ });
268
+ }
95
269
  /**
96
270
  * @param {string} owner
97
271
  * @param {Record<string, unknown>} args
@@ -119,50 +293,40 @@ export function createWorkRuntime(store, engine, options = {}) {
119
293
  return { replay: existing };
120
294
  }
121
295
  }
122
- if (book.countActive(owner) >= limits.perBot || book.countGlobal() >= limits.global) {
296
+ const decision = decideDispatch(owner);
297
+ if (decision === 'refuse') {
123
298
  return {
124
- error: toolError('BUDGET', 'Unfinished tickets already hit the host concurrency limit, so no row was created.'),
299
+ error: toolError('BUDGET', 'Unfinished tickets already hit the host slot and queue limits, so no row was created.'),
125
300
  };
126
301
  }
127
- return {
128
- created: book.insertAccepted({
129
- owner,
130
- briefing,
131
- idempotencyKey: key,
132
- correlationId: correlationOf(owner, args.correlationId),
133
- budget,
134
- }),
135
- };
302
+ const created = book.insertAccepted({
303
+ owner,
304
+ briefing,
305
+ idempotencyKey: key,
306
+ correlationId: correlationOf(owner, args.correlationId),
307
+ budget,
308
+ });
309
+ if (decision === 'start')
310
+ book.reserveSlot(created.ticket.id);
311
+ return { created, start: decision === 'start' };
136
312
  });
137
313
  if (prepared.error)
138
314
  return prepared.error;
139
315
  if (prepared.replay)
140
316
  return publicDispatch(prepared.replay, { replayed: true });
141
317
  const ticket = prepared.created.ticket;
318
+ if (!prepared.start) {
319
+ return publicDispatch(book.ticket(ticket.id), {
320
+ flush: 'pending',
321
+ queued: true,
322
+ childSessionId: null,
323
+ nativeMessageId: null,
324
+ });
325
+ }
142
326
  if (!resident) {
143
327
  return { ...publicDispatch(ticket), flush: 'pending', resident: false, message: 'Only a resident host continues' };
144
328
  }
145
- const created = await ensureChild(ticket);
146
- if (created.error)
147
- return created.error;
148
- const input = await store.write('work', () => {
149
- const current = book.ticket(ticket.id);
150
- const attempt = book.attempt(current.active_attempt_id);
151
- if (attempt?.current_input_id)
152
- return book.input(attempt.current_input_id);
153
- return book.insertInput({
154
- ticketId: current.id,
155
- attemptId: attempt.id,
156
- revision: current.briefing_revision,
157
- kind: 'briefing',
158
- text: briefing,
159
- });
160
- });
161
- const flushed = await pushInput(book, input, deps);
162
- return publicDispatch(book.ticket(ticket.id), {
163
- flush: flushWord(flushed),
164
- nativeMessageId: input.native_message_id,
165
- });
329
+ return startReserved(ticket);
166
330
  }
167
331
  /**
168
332
  * @param {string} owner
@@ -193,9 +357,28 @@ export function createWorkRuntime(store, engine, options = {}) {
193
357
  if (found.error)
194
358
  return found.error;
195
359
  const expected = revisionOf(args.expectedRevision);
196
- const advanced = await store.write('work', () => book.advanceGeneration(found.ticket.id, expected, kind, text));
360
+ const advanced = await store.write('work', () => {
361
+ const result = book.advanceGeneration(found.ticket.id, expected, kind, text);
362
+ if (result.error)
363
+ return result;
364
+ // A supply into needs_user must take a running slot. If none is free, keep
365
+ // the input durable and start it when promoteQueued reserves one.
366
+ if (kind === 'supply' && result.ticket.status === 'needs_user' && !hasSlot(owner)) {
367
+ return { ...result, queued: true };
368
+ }
369
+ if (kind === 'supply' && result.ticket.status === 'needs_user')
370
+ book.reserveSlot(result.ticket.id);
371
+ return result;
372
+ });
197
373
  if (advanced.error)
198
374
  return toolError(advanced.error, advanced.message ?? 'Changing the generation failed.');
375
+ if (advanced.queued) {
376
+ return publicDispatch(book.ticket(found.ticket.id), {
377
+ flush: 'pending',
378
+ queued: true,
379
+ nativeMessageId: advanced.input.native_message_id,
380
+ });
381
+ }
199
382
  if (!resident)
200
383
  return { ...publicDispatch(advanced.ticket), flush: 'pending', message: 'Only a resident host continues' };
201
384
  const flushed = await pushInput(book, advanced.input, deps);
@@ -220,12 +403,15 @@ export function createWorkRuntime(store, engine, options = {}) {
220
403
  if (cancelled.error)
221
404
  return toolError(cancelled.error, cancelled.message ?? 'Cancel failed.');
222
405
  const child = cancelled.ticket.child_session_id;
406
+ if (child)
407
+ closeBudgetWait(child);
223
408
  if (resident && child && options.cancel) {
224
409
  await options.cancel(child, { keepInbox: note !== '', reason: 'ticket-cancel' });
225
410
  }
226
411
  if (resident && note !== '' && cancelled.input)
227
412
  await pushInput(book, cancelled.input, deps);
228
413
  await removeUnclaimed(owner, cancelled.ticket.id);
414
+ schedulePromote();
229
415
  return {
230
416
  ok: true,
231
417
  ticketId: cancelled.ticket.id,
@@ -243,33 +429,99 @@ export function createWorkRuntime(store, engine, options = {}) {
243
429
  const found = resolveTicket(owner, args.ticketId, null);
244
430
  if (found.error)
245
431
  return found.error;
432
+ // The model cannot kill a worker by refusing budget. A deny adds another
433
+ // full slice. Grant count is not a reason to stop.
434
+ let refusalIgnored = false;
246
435
  if (args.deny === true) {
247
- const failed = await store.write('work', () => book.failRunning(found.ticket.id, 'grant-denied'));
248
- const child = failed?.child_session_id;
249
- const state = child ? arms.get(child) : null;
250
- if (state) {
251
- state.closing = true;
252
- state.clear?.();
253
- }
254
- if (resident && child && options.cancel)
255
- await options.cancel(child, { keepInbox: false, reason: 'grant-denied' });
256
- return { ok: true, ticketId: found.ticket.id, ticketStatus: 'failed', denied: true };
436
+ refusalIgnored = true;
437
+ args = {
438
+ ticketId: found.ticket.id,
439
+ maxTokens: limits.workerMaxTokens,
440
+ wallMs: limits.workerWallMs,
441
+ };
257
442
  }
443
+ const live = found.ticket.child_session_id ? arms.get(found.ticket.child_session_id) : null;
444
+ if (live?.role === 'worker')
445
+ saveQuota(found.ticket.child_session_id, live);
258
446
  const added = await store.write('work', () => book.extendBudget(found.ticket.id, {
259
447
  wallMs: typeof args.wallMs === 'number' ? args.wallMs : undefined,
260
448
  maxTokens: typeof args.maxTokens === 'number' ? args.maxTokens : undefined,
261
449
  }, { wallMs: limits.workerWallMs, maxTokens: limits.workerMaxTokens }));
262
- if (added.error)
263
- return toolError(added.error, added.message ?? 'No budget was added.');
450
+ if (added.error) {
451
+ return {
452
+ ...toolError(added.error, added.message ?? 'No budget was added.'),
453
+ ...quotaFeedback(found.ticket.id),
454
+ ...(refusalIgnored ? { denied: false } : {}),
455
+ };
456
+ }
264
457
  const child = added.ticket.child_session_id;
265
458
  if (child)
266
459
  releaseArm(child, added.budget);
460
+ let resumedInput = null;
461
+ const quota = quotaOf(added.ticket.id);
462
+ if (child &&
463
+ !arms.has(child) &&
464
+ quota?.waiting &&
465
+ options.resumePaused &&
466
+ resident &&
467
+ options.push &&
468
+ ['started', 'enqueued'].includes(added.ticket.status) &&
469
+ added.budget.maxTokens > quota.tokens &&
470
+ added.budget.wallMs > quota.activeMs &&
471
+ !(await isChildLive(child))) {
472
+ resumedInput = await store.write('work', () => {
473
+ const fresh = book.ticket(added.ticket.id);
474
+ if (!fresh ||
475
+ !['started', 'enqueued'].includes(fresh.status) ||
476
+ fresh.active_attempt_id !== added.ticket.active_attempt_id)
477
+ return null;
478
+ const current = quotaOf(fresh.id);
479
+ if (!current?.waiting)
480
+ return null;
481
+ if (current.resumeInputId)
482
+ return book.input(current.resumeInputId);
483
+ // A detached native turn ended (possibly disposed). Advance only the
484
+ // input generation, not the attempt/fence/session, so replay of that
485
+ // old turn/end cannot finalize a new pending result.
486
+ store.db
487
+ .prepare('UPDATE work_tickets SET revision = revision + 1, briefing_revision = briefing_revision + 1, updated_at = ? WHERE id = ?')
488
+ .run(now().toISOString(), fresh.id);
489
+ const input = book.insertInput({
490
+ ticketId: fresh.id,
491
+ attemptId: fresh.active_attempt_id,
492
+ revision: fresh.briefing_revision + 1,
493
+ kind: 'supply',
494
+ text: '[foundations:budget-resume] Budget was explicitly granted. Continue the SAME ticket from durable history; do not repeat completed effects. Inspect current state before continuing. This is a new host turn after detachment, not a new task.',
495
+ });
496
+ writeQuota(fresh.id, { ...current, resumeInputId: input.id });
497
+ return input;
498
+ });
499
+ if (resumedInput)
500
+ await pushInput(book, resumedInput, deps);
501
+ }
502
+ const feedback = quotaFeedback(added.ticket.id);
267
503
  return {
268
504
  ok: true,
269
505
  ticketId: added.ticket.id,
270
- ticketStatus: added.ticket.status,
506
+ ticketStatus: book.ticket(added.ticket.id)?.status ?? added.ticket.status,
507
+ briefingRevision: book.ticket(added.ticket.id)?.briefing_revision,
271
508
  wallMs: added.budget.wallMs,
272
509
  maxTokens: added.budget.maxTokens,
510
+ ...feedback,
511
+ ...(refusalIgnored
512
+ ? {
513
+ denied: false,
514
+ refusalIgnored: true,
515
+ message: feedback.waitingBudget
516
+ ? 'Budget refusal was not applied. Another full slice was granted, and the worker is still paused because usage is past that grant. Grant another slice. Do not cancel unless the user asks to stop. Do not take over the task.'
517
+ : 'Budget refusal was not applied. Another full slice was granted and the worker continues. Do not take over the task.',
518
+ }
519
+ : {}),
520
+ continuation: resumedInput
521
+ ? 'same-session-new-turn'
522
+ : child && arms.has(child)
523
+ ? 'same-turn'
524
+ : 'awaiting-host-turn',
273
525
  };
274
526
  }
275
527
  async function retry(owner, args) {
@@ -279,19 +531,27 @@ export function createWorkRuntime(store, engine, options = {}) {
279
531
  if (found.error)
280
532
  return found.error;
281
533
  const expected = revisionOf(args.expectedRevision);
282
- const retried = await store.write('work', () => book.insertRetry(found.ticket.id, expected));
534
+ const retried = await store.write('work', () => {
535
+ const result = book.insertRetry(found.ticket.id, expected);
536
+ if (result.error)
537
+ return result;
538
+ if (!hasSlot(owner))
539
+ return { ...result, queued: true };
540
+ book.reserveSlot(result.ticket.id);
541
+ return result;
542
+ });
283
543
  if (retried.error)
284
544
  return toolError(retried.error, retried.message ?? 'Retry failed.');
545
+ if (retried.queued) {
546
+ return publicDispatch(book.ticket(retried.ticket.id), {
547
+ flush: 'pending',
548
+ queued: true,
549
+ nativeMessageId: retried.input?.native_message_id ?? null,
550
+ });
551
+ }
285
552
  if (!resident)
286
553
  return { ...publicDispatch(retried.ticket), flush: 'pending', message: 'Only a resident host continues' };
287
- const ensured = await ensureChild(retried.ticket);
288
- if (ensured.error)
289
- return ensured.error;
290
- const flushed = await pushInput(book, retried.input, deps);
291
- return publicDispatch(book.ticket(retried.ticket.id), {
292
- flush: flushWord(flushed),
293
- nativeMessageId: retried.input.native_message_id,
294
- });
554
+ return startReserved(book.ticket(retried.ticket.id));
295
555
  }
296
556
  /**
297
557
  * @param {string} owner
@@ -428,9 +688,9 @@ export function createWorkRuntime(store, engine, options = {}) {
428
688
  async function tick() {
429
689
  await reconcileInputs(book, deps);
430
690
  await reconcileNotices(book, deps);
431
- await store.write('work', () => {
432
- book.expireQuiet();
433
- });
691
+ await renewLiveLeases();
692
+ const expired = await store.write('work', () => book.expireQuiet());
693
+ await stopExpiredChildren(expired);
434
694
  for (const owner of book.ownerIds()) {
435
695
  if (engine.isBot(owner))
436
696
  await admit(owner);
@@ -439,11 +699,137 @@ export function createWorkRuntime(store, engine, options = {}) {
439
699
  await reconcileChild(child);
440
700
  for (const owner of book.ownerIds())
441
701
  await reconcileOwner(owner);
702
+ await promoteQueued();
703
+ }
704
+ /**
705
+ * Renew the lease of a started worker that is still alive, so a silent
706
+ * model or tool turn longer than leaseMs does not false-expire it.
707
+ */
708
+ async function renewLiveLeases() {
709
+ const renewals = [];
710
+ for (const child of book.childIds()) {
711
+ const attempt = book.attemptForChild(child);
712
+ if (!attempt || (attempt.status !== 'started' && attempt.status !== 'enqueued'))
713
+ continue;
714
+ if (!(await isChildLive(child)))
715
+ continue;
716
+ renewals.push(attempt.id);
717
+ }
718
+ if (renewals.length === 0)
719
+ return;
720
+ await store.write('work', () => {
721
+ const until = new Date(now().getTime() + limits.leaseMs).toISOString();
722
+ for (const id of renewals) {
723
+ const attempt = book.attempt(id);
724
+ if (!attempt || (attempt.status !== 'started' && attempt.status !== 'enqueued'))
725
+ continue;
726
+ book.touchLease(id, until, typeof attempt.lease_observed_seq === 'number' ? attempt.lease_observed_seq : null);
727
+ }
728
+ });
729
+ }
730
+ /**
731
+ * @param {string} sessionId
732
+ */
733
+ async function isChildLive(sessionId) {
734
+ if (options.childPresent) {
735
+ try {
736
+ if (!(await options.childPresent(sessionId)))
737
+ return false;
738
+ }
739
+ catch {
740
+ return false;
741
+ }
742
+ }
743
+ else if (!options.snapshot) {
744
+ return false;
745
+ }
746
+ if (!options.snapshot)
747
+ return true;
748
+ try {
749
+ const events = await options.snapshot(sessionId);
750
+ return !latestTurnEnded(events);
751
+ }
752
+ catch {
753
+ return false;
754
+ }
755
+ }
756
+ /**
757
+ * @param {Array<{ ticketId: string, reason: string, childSessionId?: string | null }>} expired
758
+ */
759
+ async function stopExpiredChildren(expired) {
760
+ if (!Array.isArray(expired) || expired.length === 0)
761
+ return;
762
+ for (const item of expired) {
763
+ const child = item.childSessionId;
764
+ if (!child || !options.cancel)
765
+ continue;
766
+ const state = arms.get(child);
767
+ if (state) {
768
+ state.closing = true;
769
+ state.clear?.();
770
+ }
771
+ try {
772
+ await options.cancel(child, { keepInbox: false, reason: `worker-expire:${item.reason}` });
773
+ }
774
+ catch {
775
+ /* best-effort stop; the ledger already failed the ticket */
776
+ }
777
+ }
778
+ }
779
+ /**
780
+ * @param {Record<string, any>} event
781
+ */
782
+ function eventText(event) {
783
+ if (typeof event.text === 'string' && event.text !== '')
784
+ return event.text;
785
+ return book.detail(event.ticket_id)?.events?.find(item => item.event_id === event.event_id)?.text ?? '';
786
+ }
787
+ /**
788
+ * @param {Record<string, any>} event
789
+ */
790
+ function isExtensionAsk(event) {
791
+ return event.kind === 'progress' && eventText(event).startsWith('[foundations:extension]');
792
+ }
793
+ /**
794
+ * Extension asks are decision-required: they may starve-bypass notice-open,
795
+ * user-pending, and delivery-open. Ordinary progress does not.
796
+ * @param {Record<string, any>} event
797
+ */
798
+ function isDecisionRequiredEvent(event) {
799
+ return event.kind === 'question' || event.kind === 'result' || event.kind === 'failure' || isExtensionAsk(event);
800
+ }
801
+ /**
802
+ * @param {Record<string, any>} ticket
803
+ */
804
+ function isFinishedTicket(ticket) {
805
+ const status = String(ticket.status);
806
+ return status === 'failed' || status === 'cancelled' || status === 'delivered';
442
807
  }
443
808
  /**
444
809
  * @param {string} owner
445
810
  */
446
811
  async function admit(owner) {
812
+ if (admitting.has(owner))
813
+ return { admitted: false, reason: 'admission-inflight' };
814
+ admitting.add(owner);
815
+ try {
816
+ return await admitOnce(owner);
817
+ }
818
+ finally {
819
+ admitting.delete(owner);
820
+ }
821
+ }
822
+ function currentExtension(event) {
823
+ const ticket = book.ticket(event.ticket_id);
824
+ if (!ticket || !['started', 'enqueued'].includes(ticket.status))
825
+ return false;
826
+ const budget = JSON.parse(ticket.budget_json);
827
+ if (budget.extension !== 'open')
828
+ return false;
829
+ const latest = book.detail(ticket.id)?.events.filter(isExtensionAsk).at(-1);
830
+ return latest?.event_id === event.event_id;
831
+ }
832
+ async function admitOnce(owner) {
447
833
  const wake = book.pendingWake(owner);
448
834
  if (wake.events.length === 0 && wake.cancels.length === 0)
449
835
  return { admitted: false, reason: 'empty' };
@@ -456,15 +842,17 @@ export function createWorkRuntime(store, engine, options = {}) {
456
842
  }
457
843
  const terminalTimes = [];
458
844
  for (const event of wake.events) {
459
- if ((event.kind === 'question' || event.kind === 'result' || event.kind === 'failure') &&
460
- event.apply_status === 'applied') {
845
+ if (event.apply_status === 'applied' &&
846
+ isDecisionRequiredEvent(event) &&
847
+ (!isExtensionAsk(event) || currentExtension(event))) {
461
848
  terminalTimes.push(Date.parse(event.created_at));
462
849
  }
463
850
  }
464
851
  for (const ticket of wake.cancels)
465
852
  terminalTimes.push(Date.parse(ticket.updated_at));
853
+ const urgent = wake.events.filter(event => isExtensionAsk(event) && currentExtension(event));
466
854
  const heldUntil = holds.get(owner);
467
- if (heldUntil != null && now().getTime() < heldUntil)
855
+ if (!urgent.length && heldUntil != null && now().getTime() < heldUntil)
468
856
  return { admitted: false, reason: 'interrupt-hold' };
469
857
  if (heldUntil != null)
470
858
  holds.delete(owner);
@@ -479,21 +867,24 @@ export function createWorkRuntime(store, engine, options = {}) {
479
867
  terminalSince: terminalTimes.length === 0 ? null : Math.min(...terminalTimes),
480
868
  starveMs: limits.starveMs,
481
869
  });
482
- if (!decision.admit)
870
+ if (!urgent.length && !decision.admit)
483
871
  return { admitted: false, ...decision };
484
872
  const nativeId = randomUUID();
485
873
  const items = [];
486
- for (const event of wake.events) {
874
+ for (const event of urgent.length ? urgent : wake.events) {
487
875
  const ticket = book.ticket(event.ticket_id);
488
876
  if (!ticket)
489
877
  continue;
878
+ // A grant cannot revive a finished ticket, so drop a stale extension ask.
879
+ if (isExtensionAsk(event) && !currentExtension(event))
880
+ continue;
490
881
  items.push({
491
882
  ticketId: ticket.id,
492
883
  eventId: event.event_id,
493
884
  line: noticeLine(ticket, event),
494
885
  });
495
886
  }
496
- for (const ticket of wake.cancels) {
887
+ for (const ticket of urgent.length ? [] : wake.cancels) {
497
888
  items.push({
498
889
  ticketId: ticket.id,
499
890
  eventId: `cancel:${ticket.cancel_requested_revision}`,
@@ -503,10 +894,24 @@ export function createWorkRuntime(store, engine, options = {}) {
503
894
  if (items.length === 0)
504
895
  return { admitted: false, reason: 'empty' };
505
896
  await store.write('work', () => {
506
- book.insertNotices(nativeId, owner, items);
897
+ // Revalidate after the write queue: a grant/cancel may have raced admission.
898
+ const valid = items.filter(item => item.eventId.startsWith('cancel:') ||
899
+ (!book.event(item.eventId)?.notice_id &&
900
+ (!isExtensionAsk(book.event(item.eventId) ?? {}) || currentExtension(book.event(item.eventId)))));
901
+ items.splice(0, items.length, ...valid);
902
+ if (items.length)
903
+ book.insertNotices(nativeId, owner, items);
507
904
  });
905
+ if (!items.length)
906
+ return { admitted: false, reason: 'superseded' };
508
907
  const flushed = await pushNotice(book, nativeId, owner, renderNotice(items.map(item => item.line)), 0, deps);
509
- return { admitted: true, nativeMessageId: nativeId, flush: flushWord(flushed), ...decision };
908
+ return {
909
+ ...decision,
910
+ admitted: true,
911
+ reason: urgent.length ? 'budget-decision' : decision.reason,
912
+ nativeMessageId: nativeId,
913
+ flush: flushWord(flushed),
914
+ };
510
915
  }
511
916
  /**
512
917
  * Progress stays a status line. Result and failure carry the body so the
@@ -519,6 +924,8 @@ export function createWorkRuntime(store, engine, options = {}) {
519
924
  const head = `${ticket.id} ${status} (${LABEL[status] ?? status}) rev ${ticket.briefing_revision} ${event.kind}`;
520
925
  const text = book.detail(ticket.id)?.events?.find(item => item.event_id === event.event_id)?.text ?? '';
521
926
  if (text.startsWith('[foundations:extension]')) {
927
+ if (isFinishedTicket(ticket))
928
+ return head;
522
929
  return [head, `eventId: ${event.event_id}`, text].join('\n');
523
930
  }
524
931
  if (event.kind !== 'result' && event.kind !== 'failure')
@@ -526,7 +933,7 @@ export function createWorkRuntime(store, engine, options = {}) {
526
933
  const body = text.length > 8000 ? `${text.slice(0, 8000)}\n…` : text;
527
934
  const ask = event.kind === 'result'
528
935
  ? 'This is a successful result. Call foundations_publish now. Use the eventId below as resultEventId and the words you want the user to see as text. After the tool returns, write that same text as the reply to the user. Do not only restate the status, and do not ask the user to paste the body back.'
529
- : 'This is a failed result. Now tell the user the reason and the next step in your own words. Do not only restate the status, and do not ask the user to paste the body back.';
936
+ : 'This is a failed result. Tell the user the reason in your own words, then stop. Do not only restate the status, and do not ask the user to paste the body back. Do not take over the task, and do not dispatch a replacement unless the user explicitly asks.';
530
937
  return [head, `eventId: ${event.event_id}`, ask, '---', body].join('\n');
531
938
  }
532
939
  /**
@@ -563,8 +970,15 @@ export function createWorkRuntime(store, engine, options = {}) {
563
970
  wakeTurns.set(`${sessionId}:${event.data.turn}`, nativeId);
564
971
  }
565
972
  }
566
- if (event.type === 'turn/end')
973
+ if (event.type === 'turn/end') {
974
+ if (isWorkerSession(sessionId) && event.data?.reason?.reason?.kind === 'user') {
975
+ const attempt = book.attemptForChild(sessionId);
976
+ if (attempt)
977
+ book.markCancelled(attempt.ticket_id, null, 'Worker cancelled by user.');
978
+ closeBudgetWait(sessionId);
979
+ }
567
980
  applyTurnEnd(sessionId, event.data?.turn ?? null, reasonKindOf(event));
981
+ }
568
982
  if (isWorkerSession(sessionId) && typeof event.seq === 'number') {
569
983
  const attempt = book.attemptForChild(sessionId);
570
984
  if (attempt && (attempt.status === 'started' || attempt.status === 'enqueued')) {
@@ -581,6 +995,8 @@ export function createWorkRuntime(store, engine, options = {}) {
581
995
  if (book.inputByNative(messageId))
582
996
  turns.set(sessionId, { messageId, turn, seq: null });
583
997
  book.setNoticeObserve(messageId, 'claimed');
998
+ if (turn != null && book.noticeIds(sessionId).has(messageId))
999
+ wakeTurns.set(`${sessionId}:${turn}`, messageId);
584
1000
  }
585
1001
  /**
586
1002
  * @param {Record<string, any>} ticket
@@ -591,17 +1007,29 @@ export function createWorkRuntime(store, engine, options = {}) {
591
1007
  if (!options.createChild)
592
1008
  return { error: publicDispatch(ticket, { flush: 'pending' }) };
593
1009
  const sessionId = `worker-${ticket.id}`;
1010
+ const attemptId = ticket.active_attempt_id;
1011
+ const fencingToken = book.attempt(attemptId)?.fencing_token ?? 1;
594
1012
  try {
595
1013
  const created = await options.createChild({
596
1014
  sessionId,
597
1015
  ownerSessionId: ticket.owner_session_id,
598
1016
  ticketId: ticket.id,
599
- fencingToken: book.attempt(ticket.active_attempt_id)?.fencing_token ?? 1,
1017
+ fencingToken,
600
1018
  });
601
1019
  const child = typeof created === 'string' && created !== '' ? created : sessionId;
1020
+ const saved = await store.write('work', () => book.setChildIfEmpty(ticket.id, child, { attemptId, fencingToken }));
1021
+ if (!saved?.attached) {
1022
+ try {
1023
+ if (options.cancel)
1024
+ await options.cancel(child, { keepInbox: false, reason: 'stale-start' });
1025
+ }
1026
+ catch {
1027
+ /* best-effort cleanup of a late or fenced child */
1028
+ }
1029
+ return { rejected: true, ticket: saved?.ticket ?? book.ticket(ticket.id) ?? ticket };
1030
+ }
602
1031
  engine.ensureSession(child);
603
- const saved = await store.write('work', () => book.setChildIfEmpty(ticket.id, child));
604
- return { ticket: saved };
1032
+ return { ticket: saved.ticket };
605
1033
  }
606
1034
  catch {
607
1035
  return {
@@ -678,11 +1106,13 @@ export function createWorkRuntime(store, engine, options = {}) {
678
1106
  briefingRevision: fresh.briefing_revision,
679
1107
  status: dispatched && flush === 'accepted' ? 'enqueued' : 'accepted',
680
1108
  ticketStatus: fresh.status,
1109
+ ...quotaFeedback(fresh.id),
681
1110
  childSessionId: extra.childSessionId === null ? null : fresh.child_session_id,
682
1111
  fencingToken: attempt?.fencing_token ?? null,
683
1112
  nativeMessageId: extra.nativeMessageId === undefined ? (input?.native_message_id ?? null) : extra.nativeMessageId,
684
1113
  flush,
685
1114
  replayed: extra.replayed === true,
1115
+ queued: extra.queued === true || (fresh.status === 'accepted' && !book.slotHeld(fresh)),
686
1116
  };
687
1117
  }
688
1118
  function project(ticket) {
@@ -721,6 +1151,7 @@ export function createWorkRuntime(store, engine, options = {}) {
721
1151
  const found = book.detail(input.ticket_id);
722
1152
  if (!found)
723
1153
  return;
1154
+ let released = false;
724
1155
  for (const event of found.events) {
725
1156
  if (event.attempt_id !== input.attempt_id)
726
1157
  continue;
@@ -729,7 +1160,10 @@ export function createWorkRuntime(store, engine, options = {}) {
729
1160
  if (event.apply_status !== 'pending_completion')
730
1161
  continue;
731
1162
  book.applyTerminal(event.event_id, reason);
1163
+ released = true;
732
1164
  }
1165
+ if (released)
1166
+ void promoteQueued().catch(() => { });
733
1167
  }
734
1168
  /**
735
1169
  * @param {string} sessionId
@@ -760,7 +1194,7 @@ export function createWorkRuntime(store, engine, options = {}) {
760
1194
  const nativeId = byTurn.get(event.data?.turn);
761
1195
  if (!nativeId)
762
1196
  continue;
763
- turns.set(sessionId, { messageId: nativeId, turn: event.data?.turn ?? null, seq: null });
1197
+ // Historical turn endings must not overwrite the latest live input binding.
764
1198
  const input = book.inputByNative(nativeId);
765
1199
  if (!input)
766
1200
  continue;
@@ -874,6 +1308,7 @@ export function createWorkRuntime(store, engine, options = {}) {
874
1308
  state.tokens += total;
875
1309
  if (seq != null)
876
1310
  state.lastSeq = seq;
1311
+ saveQuota(sessionId, state);
877
1312
  consider(sessionId, state, false);
878
1313
  }
879
1314
  return;
@@ -1028,11 +1463,12 @@ export function createWorkRuntime(store, engine, options = {}) {
1028
1463
  }
1029
1464
  : workerCaps(sessionId);
1030
1465
  const state = {
1466
+ attemptId: role === 'worker' ? (book.attemptForChild(sessionId)?.id ?? null) : null,
1031
1467
  turn,
1032
- startedAt: previous?.startedAt ?? now().getTime(),
1468
+ startedAt: role === 'worker' ? now().getTime() : (previous?.startedAt ?? now().getTime()),
1033
1469
  steps: 0,
1034
- tokens: previous?.tokens ?? 0,
1035
- lastSeq: previous?.lastSeq ?? null,
1470
+ tokens: role === 'worker' ? 0 : (previous?.tokens ?? 0),
1471
+ lastSeq: role === 'worker' ? null : (previous?.lastSeq ?? null),
1036
1472
  role,
1037
1473
  tight: false,
1038
1474
  limits: caps,
@@ -1040,18 +1476,37 @@ export function createWorkRuntime(store, engine, options = {}) {
1040
1476
  budgetNotice: false,
1041
1477
  extensionOpen: false,
1042
1478
  closing: false,
1479
+ pausedAt: null,
1480
+ atBoundary: false,
1043
1481
  clear: () => { },
1044
1482
  };
1045
- const elapsed = Math.max(0, now().getTime() - state.startedAt);
1483
+ if (role === 'worker') {
1484
+ const attempt = book.attemptForChild(sessionId);
1485
+ const quota = attempt ? quotaOf(attempt.ticket_id) : null;
1486
+ if (quota) {
1487
+ state.tokens = quota.tokens ?? 0;
1488
+ state.lastSeq = quota.lastSeq ?? null;
1489
+ const elapsed = (quota.activeMs ?? 0) + (quota.runningSince == null ? 0 : Math.max(0, now().getTime() - quota.runningSince));
1490
+ state.startedAt = now().getTime() - elapsed;
1491
+ if (quota.waiting) {
1492
+ state.pausedAt = now().getTime();
1493
+ state.atBoundary = true;
1494
+ }
1495
+ }
1496
+ }
1497
+ const elapsed = Math.max(0, (state.pausedAt != null && state.atBoundary ? state.pausedAt : now().getTime()) - state.startedAt);
1046
1498
  state.extensionOpen = previous?.extensionOpen === true || caps.extensionOpen === true;
1047
- const target = state.extensionOpen ? caps.ceilingWall : caps.wallMs;
1048
- state.clear = schedule(() => {
1049
- const live = arms.get(sessionId);
1050
- if (!live || live !== state || live.closing)
1051
- return;
1052
- consider(sessionId, live, true);
1053
- }, Math.max(1, target - elapsed));
1499
+ const target = caps.wallMs;
1500
+ if (state.pausedAt == null)
1501
+ state.clear = schedule(() => {
1502
+ const live = arms.get(sessionId);
1503
+ if (!live || live !== state || live.closing)
1504
+ return;
1505
+ consider(sessionId, live, true);
1506
+ }, Math.max(1, target - elapsed));
1054
1507
  arms.set(sessionId, state);
1508
+ if (role === 'worker')
1509
+ saveQuota(sessionId, state);
1055
1510
  return state;
1056
1511
  }
1057
1512
  /**
@@ -1067,21 +1522,21 @@ export function createWorkRuntime(store, engine, options = {}) {
1067
1522
  try {
1068
1523
  const parsed = JSON.parse(ticket.budget_json);
1069
1524
  if (Number.isFinite(parsed.wallMs))
1070
- wallMs = Math.min(limits.workerWallMs, Math.max(1000, parsed.wallMs));
1525
+ wallMs = Math.max(1000, parsed.wallMs);
1071
1526
  if (Number.isFinite(parsed.maxTokens))
1072
- maxTokens = Math.min(limits.workerMaxTokens, Math.max(1000, parsed.maxTokens));
1527
+ maxTokens = Math.max(1000, parsed.maxTokens);
1073
1528
  extensionOpen = parsed.extension === 'open';
1074
1529
  }
1075
1530
  catch {
1076
- /* host ceiling */
1531
+ /* ticket budget */
1077
1532
  }
1078
1533
  }
1079
1534
  return {
1080
1535
  maxSteps: Number.POSITIVE_INFINITY,
1081
1536
  wallMs,
1082
1537
  maxTokens,
1083
- ceilingWall: limits.workerWallMs,
1084
- ceilingTokens: limits.workerMaxTokens,
1538
+ ceilingWall: Number.POSITIVE_INFINITY,
1539
+ ceilingTokens: Number.POSITIVE_INFINITY,
1085
1540
  extensionOpen,
1086
1541
  };
1087
1542
  }
@@ -1104,26 +1559,14 @@ export function createWorkRuntime(store, engine, options = {}) {
1104
1559
  Promise.resolve(options.cancel?.(sessionId, { keepInbox: true, reason: trip.cause })).catch(() => { });
1105
1560
  return;
1106
1561
  }
1107
- const elapsed = Math.max(0, now().getTime() - state.startedAt);
1108
- if (state.tokens >= state.limits.ceilingTokens || elapsed >= state.limits.ceilingWall) {
1109
- finishWorker(sessionId, state, state.tokens >= state.limits.ceilingTokens && elapsed < state.limits.ceilingWall ? 'tokens' : 'wall');
1110
- return;
1111
- }
1112
- if (state.extensionOpen)
1562
+ if (state.pausedAt != null)
1113
1563
  return;
1564
+ const elapsed = Math.max(0, now().getTime() - state.startedAt);
1114
1565
  const overWall = fromTimer || elapsed >= state.limits.wallMs;
1115
1566
  const overTokens = state.tokens >= state.limits.maxTokens;
1116
1567
  if (!overWall && !overTokens)
1117
1568
  return;
1118
- if (overWall && state.limits.wallMs < state.limits.ceilingWall) {
1119
- askForQuota(sessionId, state, 'wall', elapsed);
1120
- return;
1121
- }
1122
- if (overTokens && state.limits.maxTokens < state.limits.ceilingTokens) {
1123
- askForQuota(sessionId, state, 'tokens', elapsed);
1124
- return;
1125
- }
1126
- finishWorker(sessionId, state, overTokens ? 'tokens' : 'wall');
1569
+ askForQuota(sessionId, state, overTokens ? 'tokens' : 'wall', elapsed);
1127
1570
  }
1128
1571
  /**
1129
1572
  * @param {string} sessionId
@@ -1134,13 +1577,15 @@ export function createWorkRuntime(store, engine, options = {}) {
1134
1577
  function askForQuota(sessionId, state, reason, elapsed) {
1135
1578
  state.extensionOpen = true;
1136
1579
  state.clear?.();
1137
- watchHostCeiling(sessionId, state);
1580
+ state.pausedAt = now().getTime();
1581
+ state.atBoundary = false;
1582
+ saveQuota(sessionId, state, reason);
1138
1583
  const attempt = book.attemptForChild(sessionId);
1139
1584
  if (!attempt)
1140
1585
  return;
1141
1586
  const ticket = book.ticket(attempt.ticket_id);
1142
1587
  if (!ticket || ticket.status === 'failed' || ticket.status === 'cancelled' || ticket.status === 'delivered') {
1143
- finishWorker(sessionId, state, reason);
1588
+ closeBudgetWait(sessionId);
1144
1589
  return;
1145
1590
  }
1146
1591
  const marked = book.markExtensionOpen(ticket.id);
@@ -1153,7 +1598,10 @@ export function createWorkRuntime(store, engine, options = {}) {
1153
1598
  `granted wall ms: ${state.limits.wallMs}`,
1154
1599
  `tokens used: ${state.tokens}`,
1155
1600
  `granted tokens: ${state.limits.maxTokens}`,
1156
- 'Do not mark the ticket failed. Call foundations_grant to add budget. wallMs and maxTokens are amounts to add. Pass deny=true to refuse the grant.',
1601
+ `required total tokens: ${state.tokens + 1}; required total active wall ms: ${elapsed + 1}`,
1602
+ `standard slice tokens: ${limits.workerMaxTokens}; standard slice wall ms: ${limits.workerWallMs}`,
1603
+ 'Worker is waiting for budget at the next safe loop boundary. It will not dispatch another step until sufficient budget is granted. Budget wait does not expire.',
1604
+ "Call foundations_grant with amounts to ADD. Add at least one standard slice, and more if current usage is already past granted plus that slice. There is no limit on how many times you grant and no cumulative ceiling. Do not refuse because this ticket has already been extended. deny does not stop the worker. Do not cancel unless the user asks to stop. Do not redispatch a replacement ticket. Do not take over the task: do not inspect, edit, or build the worker's files, and do not run its commands.",
1157
1605
  ].join('\n');
1158
1606
  book.insertEvent({
1159
1607
  ticketId: ticket.id,
@@ -1165,74 +1613,185 @@ export function createWorkRuntime(store, engine, options = {}) {
1165
1613
  applyStatus: 'applied',
1166
1614
  });
1167
1615
  }
1168
- /**
1169
- * After a quota request, the worker keeps running until the host ceiling.
1170
- * An early timer is rearmed. It does not fail the ticket by itself.
1171
- * @param {string} sessionId
1172
- * @param {{ startedAt: number, limits: { ceilingWall: number }, closing: boolean, clear: () => void }} state
1173
- */
1174
- function watchHostCeiling(sessionId, state) {
1175
- const elapsed = Math.max(0, now().getTime() - state.startedAt);
1176
- state.clear = schedule(() => {
1177
- const live = arms.get(sessionId);
1178
- if (!live || live !== state || live.closing)
1179
- return;
1180
- const at = Math.max(0, now().getTime() - live.startedAt);
1181
- if (at >= live.limits.ceilingWall) {
1182
- finishWorker(sessionId, live, 'wall');
1183
- return;
1184
- }
1185
- watchHostCeiling(sessionId, live);
1186
- }, Math.max(1000, state.limits.ceilingWall - elapsed));
1187
- }
1188
- /**
1189
- * @param {string} sessionId
1190
- * @param {{ closing: boolean, clear: () => void }} state
1191
- * @param {string} reason
1192
- */
1193
- function finishWorker(sessionId, state, reason) {
1194
- if (state.closing)
1195
- return;
1196
- state.closing = true;
1197
- state.clear?.();
1198
- Promise.resolve(options.cancel?.(sessionId, { keepInbox: false, reason: `worker-budget:${reason}` })).catch(() => { });
1199
- const attempt = book.attemptForChild(sessionId);
1200
- if (attempt)
1201
- book.failRunning(attempt.ticket_id, reason);
1202
- }
1203
1616
  /**
1204
1617
  * @param {string} sessionId
1205
1618
  * @param {{ wallMs: number, maxTokens: number }} budget
1206
1619
  */
1207
1620
  function releaseArm(sessionId, budget) {
1208
1621
  const state = arms.get(sessionId);
1209
- if (!state || state.role !== 'worker' || state.closing)
1622
+ const attempt = book.attemptForChild(sessionId);
1623
+ if (!attempt)
1210
1624
  return;
1211
- state.extensionOpen = false;
1625
+ const quota = quotaOf(attempt.ticket_id);
1626
+ if (!state || state.role !== 'worker' || state.closing) {
1627
+ // A detached turn stays parked until the grant path queues a durable continuation.
1628
+ if (quota)
1629
+ writeQuota(attempt.ticket_id, { ...quota, waiting: true, runningSince: null });
1630
+ return;
1631
+ }
1212
1632
  state.limits.wallMs = budget.wallMs;
1213
1633
  state.limits.maxTokens = budget.maxTokens;
1634
+ const elapsed = Math.max(0, (state.pausedAt != null && state.atBoundary ? state.pausedAt : now().getTime()) - state.startedAt);
1635
+ if (state.tokens >= budget.maxTokens || elapsed >= budget.wallMs) {
1636
+ state.extensionOpen = true;
1637
+ if (state.pausedAt == null)
1638
+ state.pausedAt = now().getTime();
1639
+ state.clear();
1640
+ saveQuota(sessionId, state);
1641
+ book.markExtensionOpen(attempt.ticket_id);
1642
+ return;
1643
+ }
1644
+ state.extensionOpen = false;
1645
+ state.startedAt = now().getTime() - elapsed;
1646
+ state.pausedAt = null;
1647
+ state.atBoundary = false;
1214
1648
  state.clear?.();
1215
- const elapsed = Math.max(0, now().getTime() - state.startedAt);
1649
+ saveQuota(sessionId, state);
1216
1650
  state.clear = schedule(() => {
1217
- const live = arms.get(sessionId);
1218
- if (!live || live !== state || live.closing)
1219
- return;
1220
- consider(sessionId, live, true);
1651
+ if (arms.get(sessionId) === state && !state.closing)
1652
+ consider(sessionId, state, true);
1221
1653
  }, Math.max(1, budget.wallMs - elapsed));
1222
- if (state.tokens >= budget.maxTokens || elapsed >= budget.wallMs)
1223
- consider(sessionId, state, false);
1654
+ for (const resolve of budgetWaiters.get(sessionId) ?? [])
1655
+ resolve(true);
1656
+ budgetWaiters.delete(sessionId);
1657
+ }
1658
+ function quotaOf(ticketId) {
1659
+ try {
1660
+ return JSON.parse(book.ticket(ticketId)?.budget_json ?? '{}').quota ?? null;
1661
+ }
1662
+ catch {
1663
+ return null;
1664
+ }
1665
+ }
1666
+ function writeQuota(ticketId, quota) {
1667
+ const ticket = book.ticket(ticketId);
1668
+ if (!ticket)
1669
+ return;
1670
+ const budget = JSON.parse(ticket.budget_json);
1671
+ budget.quota = quota;
1672
+ store.db
1673
+ .prepare('UPDATE work_tickets SET budget_json = ?, updated_at = ? WHERE id = ?')
1674
+ .run(JSON.stringify(budget), now().toISOString(), ticketId);
1675
+ }
1676
+ function saveQuota(sessionId, state, reason) {
1677
+ const attempt = book.attemptForChild(sessionId);
1678
+ if (!attempt || attempt.id !== state.attemptId)
1679
+ return;
1680
+ const activeMs = Math.max(0, (state.pausedAt != null && state.atBoundary ? state.pausedAt : now().getTime()) - state.startedAt);
1681
+ writeQuota(attempt.ticket_id, {
1682
+ tokens: state.tokens,
1683
+ lastSeq: state.lastSeq,
1684
+ activeMs,
1685
+ runningSince: state.pausedAt == null ? now().getTime() : null,
1686
+ waiting: state.pausedAt != null,
1687
+ phase: state.atBoundary ? 'waiting' : 'requested',
1688
+ reason: state.pausedAt == null ? undefined : (reason ?? quotaOf(attempt.ticket_id)?.reason),
1689
+ });
1690
+ }
1691
+ function quotaFeedback(ticketId) {
1692
+ const quota = quotaOf(ticketId);
1693
+ const budget = JSON.parse(book.ticket(ticketId)?.budget_json ?? '{}');
1694
+ return {
1695
+ waitingBudget: quota?.waiting === true,
1696
+ budgetReason: quota?.reason ?? null,
1697
+ usedTokens: quota?.tokens ?? 0,
1698
+ activeElapsedMs: quota?.activeMs ?? 0,
1699
+ requiredMaxTokens: (quota?.tokens ?? 0) + 1,
1700
+ requiredWallMs: (quota?.activeMs ?? 0) + 1,
1701
+ minimumAdditionalTokens: Math.max(0, (quota?.tokens ?? 0) + 1 - (budget.maxTokens ?? 0)),
1702
+ minimumAdditionalWallMs: Math.max(0, (quota?.activeMs ?? 0) + 1 - (budget.wallMs ?? 0)),
1703
+ sliceTokens: limits.workerMaxTokens,
1704
+ sliceWallMs: limits.workerWallMs,
1705
+ };
1706
+ }
1707
+ function closeBudgetWait(sessionId) {
1708
+ const state = arms.get(sessionId);
1709
+ if (state) {
1710
+ state.closing = true;
1711
+ state.clear();
1712
+ }
1713
+ for (const resolve of budgetWaiters.get(sessionId) ?? [])
1714
+ resolve(false);
1715
+ budgetWaiters.delete(sessionId);
1716
+ }
1717
+ /** Awaiting this official pre-step waterfall suspends admission, not the turn. */
1718
+ async function waitForBudget(sessionId, signal) {
1719
+ if (!isWorkerSession(sessionId))
1720
+ return true;
1721
+ const attempt = book.attemptForChild(sessionId);
1722
+ const ticket = attempt ? book.ticket(attempt.ticket_id) : null;
1723
+ if (!ticket || isFinishedTicket(ticket) || signal?.aborted)
1724
+ return false;
1725
+ const state = arms.get(sessionId) ?? arm(sessionId, 0, 'worker');
1726
+ if (state.closing)
1727
+ return false;
1728
+ consider(sessionId, state, false);
1729
+ if (state.pausedAt == null)
1730
+ return true;
1731
+ if (!state.atBoundary) {
1732
+ // The already-admitted request/tool batch may finish; account for that
1733
+ // active time before freezing the clock at this safe loop boundary.
1734
+ state.pausedAt = now().getTime();
1735
+ state.atBoundary = true;
1736
+ saveQuota(sessionId, state);
1737
+ }
1738
+ const caps = workerCaps(sessionId);
1739
+ releaseArm(sessionId, caps);
1740
+ if (state.pausedAt == null)
1741
+ return true;
1742
+ return new Promise(resolve => {
1743
+ const waiters = budgetWaiters.get(sessionId) ?? new Set();
1744
+ const done = (allowed) => {
1745
+ signal?.removeEventListener('abort', aborted);
1746
+ waiters.delete(done);
1747
+ if (!waiters.size)
1748
+ budgetWaiters.delete(sessionId);
1749
+ resolve(allowed);
1750
+ };
1751
+ const aborted = () => done(false);
1752
+ waiters.add(done);
1753
+ budgetWaiters.set(sessionId, waiters);
1754
+ signal?.addEventListener('abort', aborted, { once: true });
1755
+ if (signal?.aborted)
1756
+ done(false);
1757
+ });
1224
1758
  }
1225
1759
  /**
1226
1760
  * @param {string} sessionId
1227
1761
  */
1228
1762
  function clearBudget(sessionId) {
1229
1763
  const state = arms.get(sessionId);
1764
+ if (state?.role === 'worker')
1765
+ saveQuota(sessionId, state);
1230
1766
  if (!state)
1231
1767
  return;
1232
1768
  state.clear?.();
1233
1769
  arms.delete(sessionId);
1234
1770
  }
1235
1771
  }
1772
+ /**
1773
+ * A later turn/end for every started turn means the child is idle.
1774
+ * No turn at all, or an open turn, means it may still be running.
1775
+ * @param {Array<Record<string, any>>} events
1776
+ */
1777
+ function latestTurnEnded(events) {
1778
+ const open = new Set();
1779
+ let sawTurn = false;
1780
+ for (const event of events) {
1781
+ const turn = event.data?.turn;
1782
+ if (turn == null)
1783
+ continue;
1784
+ if (event.type === 'turn/start' || event.type === 'user/message') {
1785
+ sawTurn = true;
1786
+ open.add(turn);
1787
+ }
1788
+ else if (event.type === 'turn/end') {
1789
+ sawTurn = true;
1790
+ open.delete(turn);
1791
+ }
1792
+ }
1793
+ return sawTurn && open.size === 0;
1794
+ }
1236
1795
  /**
1237
1796
  * @param {unknown} value
1238
1797
  */