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

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.
@@ -3,6 +3,10 @@ import { join } from 'node:path';
3
3
  import { randomUUID } from 'node:crypto';
4
4
  import { sha256 } from "./fs-safe.js";
5
5
  export const ACTIVE_TICKET = Object.freeze(['accepted', 'enqueued', 'started', 'needs_user']);
6
+ /** Tickets that occupy a running execution slot, plus reserved starts stored as slot=held. */
7
+ export const SLOT_TICKET = Object.freeze(['enqueued', 'started']);
8
+ /** Cap on how long an accepted/needs_user ticket may occupy a reserved start slot. */
9
+ export const RESERVE_WALL_MS = 10 * 60 * 1000;
6
10
  const TERMINAL_KIND = new Set(['question', 'result', 'failure']);
7
11
  const OPEN_NOTICE_FLUSH = new Set(['intent', 'flushing', 'ambiguous']);
8
12
  /**
@@ -14,10 +18,23 @@ const OPEN_NOTICE_FLUSH = new Set(['intent', 'flushing', 'ambiguous']);
14
18
  */
15
19
  export function createTicketBook(db, root, now = () => new Date()) {
16
20
  const iso = () => now().toISOString();
21
+ const SLOT_SQL = `status IN ('enqueued','started')
22
+ OR (status IN ('accepted','needs_user') AND json_extract(budget_json, '$.slot') = 'held')`;
23
+ const QUEUE_SQL = `status = 'accepted'
24
+ AND (json_extract(budget_json, '$.slot') IS NULL OR json_extract(budget_json, '$.slot') != 'held')`;
17
25
  return {
18
26
  iso,
19
27
  countActive,
20
28
  countGlobal,
29
+ countSlots,
30
+ countGlobalSlots,
31
+ countQueued,
32
+ countGlobalQueued,
33
+ slotHeld,
34
+ reserveSlot,
35
+ releaseSlot,
36
+ nextPromotable,
37
+ heldStarts,
21
38
  ticket: (id) => row('SELECT * FROM work_tickets WHERE id = ?', id),
22
39
  attempt: (id) => row('SELECT * FROM work_attempts WHERE id = ?', id),
23
40
  byIdempotency,
@@ -98,6 +115,98 @@ export function createTicketBook(db, root, now = () => new Date()) {
98
115
  `)
99
116
  .get().n;
100
117
  }
118
+ /**
119
+ * Running execution slots for one bot. needs_user does not count unless a
120
+ * resume has already reserved a slot.
121
+ * @param {string} owner
122
+ */
123
+ function countSlots(owner) {
124
+ return db.prepare(`SELECT COUNT(*) AS n FROM work_tickets WHERE owner_session_id = ? AND (${SLOT_SQL})`).get(owner)
125
+ .n;
126
+ }
127
+ function countGlobalSlots() {
128
+ return db.prepare(`SELECT COUNT(*) AS n FROM work_tickets WHERE ${SLOT_SQL}`).get().n;
129
+ }
130
+ /**
131
+ * Accepted tickets waiting for a slot. A reserved start is not queued.
132
+ * @param {string} owner
133
+ */
134
+ function countQueued(owner) {
135
+ return db.prepare(`SELECT COUNT(*) AS n FROM work_tickets WHERE owner_session_id = ? AND ${QUEUE_SQL}`).get(owner).n;
136
+ }
137
+ function countGlobalQueued() {
138
+ return db.prepare(`SELECT COUNT(*) AS n FROM work_tickets WHERE ${QUEUE_SQL}`).get().n;
139
+ }
140
+ /**
141
+ * @param {{ budget_json?: string } | null | undefined} ticket
142
+ */
143
+ function slotHeld(ticket) {
144
+ return budgetObject(ticket ?? {}).slot === 'held';
145
+ }
146
+ /**
147
+ * Reserve a running slot on an accepted or needs_user ticket. Returns null
148
+ * when the ticket is missing or already terminal.
149
+ * @param {string} ticketId
150
+ */
151
+ function reserveSlot(ticketId) {
152
+ const ticket = row('SELECT * FROM work_tickets WHERE id = ?', ticketId);
153
+ if (!ticket)
154
+ return null;
155
+ if (ticket.status === 'cancelled' || ticket.status === 'failed' || ticket.status === 'delivered')
156
+ return ticket;
157
+ if (ticket.status === 'enqueued' || ticket.status === 'started' || slotHeld(ticket))
158
+ return ticket;
159
+ const budget = { ...budgetObject(ticket), slot: 'held' };
160
+ db.prepare(`UPDATE work_tickets SET budget_json = ?, updated_at = ? WHERE id = ?`).run(JSON.stringify(budget), iso(), ticketId);
161
+ return row('SELECT * FROM work_tickets WHERE id = ?', ticketId);
162
+ }
163
+ /**
164
+ * @param {string} ticketId
165
+ */
166
+ function releaseSlot(ticketId) {
167
+ const ticket = row('SELECT * FROM work_tickets WHERE id = ?', ticketId);
168
+ if (!ticket || !slotHeld(ticket))
169
+ return ticket;
170
+ const budget = budgetObject(ticket);
171
+ delete budget.slot;
172
+ db.prepare(`UPDATE work_tickets SET budget_json = ?, updated_at = ? WHERE id = ?`).run(JSON.stringify(budget), iso(), ticketId);
173
+ return row('SELECT * FROM work_tickets WHERE id = ?', ticketId);
174
+ }
175
+ /**
176
+ * Oldest ticket that can take a newly freed slot: a queued accept, a reserved
177
+ * start that has not reached enqueued, or a needs_user resume with unflushed input.
178
+ * FIFO is created_at, then SQLite rowid (insertion order), not ticket id.
179
+ * The caller still checks per-bot and global slot caps before reserveSlot.
180
+ */
181
+ function nextPromotable() {
182
+ return db
183
+ .prepare(`
184
+ SELECT t.* FROM work_tickets t
185
+ LEFT JOIN work_attempts a ON a.id = t.active_attempt_id
186
+ LEFT JOIN work_inputs i ON i.id = a.current_input_id
187
+ WHERE t.status = 'accepted'
188
+ OR (t.status IN ('accepted','needs_user') AND json_extract(t.budget_json, '$.slot') = 'held')
189
+ OR (
190
+ t.status = 'needs_user'
191
+ AND t.child_session_id IS NOT NULL
192
+ AND i.flush_status IN ('intent','ambiguous')
193
+ )
194
+ ORDER BY t.created_at ASC, t.rowid ASC
195
+ `)
196
+ .all();
197
+ }
198
+ /**
199
+ * Reserved starts that still need create/flush after a crash or a mid-start return.
200
+ */
201
+ function heldStarts() {
202
+ return db
203
+ .prepare(`
204
+ SELECT * FROM work_tickets
205
+ WHERE status IN ('accepted','needs_user') AND json_extract(budget_json, '$.slot') = 'held'
206
+ ORDER BY created_at ASC, rowid ASC
207
+ `)
208
+ .all();
209
+ }
101
210
  /**
102
211
  * @param {string} owner
103
212
  * @param {string | null} key
@@ -192,17 +301,34 @@ export function createTicketBook(db, root, now = () => new Date()) {
192
301
  * @param {string} ticketId
193
302
  * @param {string} childSessionId
194
303
  */
195
- function setChildIfEmpty(ticketId, childSessionId) {
304
+ function setChildIfEmpty(ticketId, childSessionId, expected) {
196
305
  const ticket = row('SELECT * FROM work_tickets WHERE id = ?', ticketId);
197
- if (!ticket || ticket.child_session_id)
198
- return ticket;
306
+ if (!ticket)
307
+ return { ticket: null, attached: false };
308
+ if (ticket.status === 'cancelled' || ticket.status === 'failed' || ticket.status === 'delivered') {
309
+ return { ticket, attached: false };
310
+ }
311
+ if (ticket.child_session_id) {
312
+ return { ticket, attached: ticket.child_session_id === childSessionId };
313
+ }
314
+ const attempt = ticket.active_attempt_id
315
+ ? row('SELECT * FROM work_attempts WHERE id = ?', ticket.active_attempt_id)
316
+ : null;
317
+ if (expected?.attemptId && attempt?.id !== expected.attemptId)
318
+ return { ticket, attached: false };
319
+ if (expected?.fencingToken != null && attempt && attempt.fencing_token !== expected.fencingToken) {
320
+ return { ticket, attached: false };
321
+ }
199
322
  db.prepare(`
200
323
  UPDATE work_tickets SET child_session_id = ?, updated_at = ? WHERE id = ? AND child_session_id IS NULL
201
324
  `).run(childSessionId, iso(), ticketId);
202
- db.prepare(`
203
- UPDATE work_attempts SET child_session_id = ? WHERE id = ? AND child_session_id IS NULL
204
- `).run(childSessionId, ticket.active_attempt_id);
205
- return row('SELECT * FROM work_tickets WHERE id = ?', ticketId);
325
+ if (ticket.active_attempt_id) {
326
+ db.prepare(`
327
+ UPDATE work_attempts SET child_session_id = ? WHERE id = ? AND child_session_id IS NULL
328
+ `).run(childSessionId, ticket.active_attempt_id);
329
+ }
330
+ const fresh = row('SELECT * FROM work_tickets WHERE id = ?', ticketId);
331
+ return { ticket: fresh, attached: fresh?.child_session_id === childSessionId };
206
332
  }
207
333
  /**
208
334
  * @param {{
@@ -396,14 +522,17 @@ export function createTicketBook(db, root, now = () => new Date()) {
396
522
  const token = (previous?.fencing_token ?? 0) + 1;
397
523
  const attemptId = randomUUID();
398
524
  const next = ticket.revision + 1;
525
+ const retryBudget = budgetObject(ticket);
526
+ delete retryBudget.quota;
527
+ delete retryBudget.extension;
399
528
  const info = db
400
529
  .prepare(`
401
530
  UPDATE work_tickets
402
- SET revision = ?, briefing_revision = 1, status = 'accepted', review_status = 'none',
531
+ SET budget_json = ?, revision = ?, briefing_revision = 1, status = 'accepted', review_status = 'none',
403
532
  active_attempt_id = ?, cancel_requested_revision = NULL, updated_at = ?
404
533
  WHERE id = ? AND revision = ?
405
534
  `)
406
- .run(next, attemptId, iso(), ticketId, expected);
535
+ .run(JSON.stringify(retryBudget), next, attemptId, iso(), ticketId, expected);
407
536
  if (info.changes !== 1)
408
537
  return { error: 'CAS_CONFLICT' };
409
538
  if (previous) {
@@ -488,7 +617,9 @@ export function createTicketBook(db, root, now = () => new Date()) {
488
617
  db.prepare(`UPDATE work_events SET apply_status = 'applied', turn_end_reason = ? WHERE event_id = ?`).run(reasonKind, eventId);
489
618
  const stamp = iso();
490
619
  if (found.kind === 'question') {
491
- db.prepare(`UPDATE work_tickets SET status = 'needs_user', updated_at = ? WHERE id = ?`).run(stamp, ticket.id);
620
+ const budget = budgetObject(ticket);
621
+ delete budget.slot;
622
+ db.prepare(`UPDATE work_tickets SET status = 'needs_user', budget_json = ?, updated_at = ? WHERE id = ?`).run(JSON.stringify(budget), stamp, ticket.id);
492
623
  }
493
624
  else if (found.kind === 'result') {
494
625
  const review = ticket.review_status === 'rejected' ? 'rejected' : 'candidate';
@@ -555,7 +686,20 @@ export function createTicketBook(db, root, now = () => new Date()) {
555
686
  if (wallMs === beforeWall && maxTokens === beforeTokens) {
556
687
  return { error: 'VALIDATION', message: 'Nothing more can be granted. The host ceiling is already used.' };
557
688
  }
558
- const next = { ...budget, wallMs, maxTokens, extension: null };
689
+ const quota = budget.quota;
690
+ const enough = !quota?.waiting ||
691
+ (maxTokens > quota.tokens &&
692
+ wallMs > quota.activeMs &&
693
+ quota.tokens < ceilings.maxTokens &&
694
+ quota.activeMs < ceilings.wallMs);
695
+ const next = {
696
+ ...budget,
697
+ wallMs,
698
+ maxTokens,
699
+ extension: enough ? null : 'open',
700
+ // Only actual loop admission clears durable waiting. A crash after this
701
+ // grant transaction must not remove expiry protection before resumption.
702
+ };
559
703
  db.prepare(`UPDATE work_tickets SET budget_json = ?, updated_at = ? WHERE id = ?`).run(JSON.stringify(next), iso(), ticketId);
560
704
  return { ticket: row('SELECT * FROM work_tickets WHERE id = ?', ticketId), budget: next };
561
705
  }
@@ -605,11 +749,20 @@ export function createTicketBook(db, root, now = () => new Date()) {
605
749
  }
606
750
  return row('SELECT * FROM work_tickets WHERE id = ?', ticketId);
607
751
  }
752
+ function extensionIsOpen(budgetJson) {
753
+ try {
754
+ return JSON.parse(budgetJson ?? '')?.extension === 'open';
755
+ }
756
+ catch {
757
+ return false;
758
+ }
759
+ }
608
760
  function expireQuiet() {
609
761
  const failed = [];
610
762
  const running = db
611
763
  .prepare(`
612
- SELECT t.id AS ticket_id, t.budget_json, t.status AS ticket_status, a.id AS attempt_id, a.status AS attempt_status,
764
+ SELECT t.id AS ticket_id, t.budget_json, t.status AS ticket_status, t.child_session_id,
765
+ a.id AS attempt_id, a.status AS attempt_status,
613
766
  a.lease_until, a.current_input_id
614
767
  FROM work_tickets t
615
768
  JOIN work_attempts a ON a.id = t.active_attempt_id
@@ -617,12 +770,17 @@ export function createTicketBook(db, root, now = () => new Date()) {
617
770
  `)
618
771
  .all();
619
772
  for (const item of running) {
773
+ if (budgetObject(item).quota?.waiting)
774
+ continue;
620
775
  if (item.ticket_status === 'started' &&
621
776
  item.attempt_status === 'started' &&
622
777
  item.lease_until &&
623
778
  Date.parse(item.lease_until) <= now().getTime()) {
779
+ // The host already asked main to grant, not to mark the ticket failed.
780
+ if (extensionIsOpen(item.budget_json))
781
+ continue;
624
782
  failRunning(item.ticket_id, 'lease');
625
- failed.push(item.ticket_id);
783
+ failed.push({ ticketId: item.ticket_id, reason: 'lease', childSessionId: item.child_session_id ?? null });
626
784
  continue;
627
785
  }
628
786
  if (item.ticket_status === 'enqueued' && item.attempt_status === 'enqueued' && item.current_input_id) {
@@ -636,10 +794,42 @@ export function createTicketBook(db, root, now = () => new Date()) {
636
794
  }
637
795
  if (input && input.flush_status === 'accepted' && Date.parse(input.updated_at) + wall <= now().getTime()) {
638
796
  failRunning(item.ticket_id, 'enqueue-wall');
639
- failed.push(item.ticket_id);
797
+ failed.push({
798
+ ticketId: item.ticket_id,
799
+ reason: 'enqueue-wall',
800
+ childSessionId: item.child_session_id ?? null,
801
+ });
640
802
  }
641
803
  }
642
804
  }
805
+ const reserved = db
806
+ .prepare(`
807
+ SELECT t.id AS ticket_id, t.budget_json, t.updated_at, t.child_session_id
808
+ FROM work_tickets t
809
+ WHERE t.status IN ('accepted','needs_user')
810
+ AND json_extract(t.budget_json, '$.slot') = 'held'
811
+ `)
812
+ .all();
813
+ for (const item of reserved) {
814
+ let wall = RESERVE_WALL_MS;
815
+ try {
816
+ const parsed = JSON.parse(item.budget_json);
817
+ const ticketWall = Number(parsed?.wallMs);
818
+ if (Number.isFinite(ticketWall) && ticketWall > 0)
819
+ wall = Math.min(ticketWall, RESERVE_WALL_MS);
820
+ }
821
+ catch {
822
+ /* host default */
823
+ }
824
+ if (Date.parse(item.updated_at) + wall <= now().getTime()) {
825
+ failRunning(item.ticket_id, 'reserve-wall');
826
+ failed.push({
827
+ ticketId: item.ticket_id,
828
+ reason: 'reserve-wall',
829
+ childSessionId: item.child_session_id ?? null,
830
+ });
831
+ }
832
+ }
643
833
  return failed;
644
834
  }
645
835
  /**
@@ -737,6 +927,7 @@ export function createTicketBook(db, root, now = () => new Date()) {
737
927
  return visibleTickets(owner).map(item => ({
738
928
  id: item.id,
739
929
  status: item.status,
930
+ waiting_budget: (item.status === 'started' || item.status === 'enqueued') && budgetObject(item).quota?.waiting === true,
740
931
  briefing_revision: item.briefing_revision,
741
932
  child_session_id: item.child_session_id,
742
933
  summary: readBriefing(item.briefing_hash).replace(/\s+/g, ' ').trim().slice(0, 80),
@@ -767,7 +958,7 @@ export function createTicketBook(db, root, now = () => new Date()) {
767
958
  function signalMap() {
768
959
  const rows = db
769
960
  .prepare(`
770
- SELECT owner_session_id, status FROM work_tickets
961
+ SELECT owner_session_id, status, budget_json FROM work_tickets
771
962
  WHERE status != 'delivered' AND status != 'cancelled'
772
963
  ORDER BY updated_at DESC
773
964
  `)
@@ -777,7 +968,9 @@ export function createTicketBook(db, root, now = () => new Date()) {
777
968
  for (const item of rows) {
778
969
  const owner = item.owner_session_id;
779
970
  const list = signals[owner] ?? [];
780
- list.push(item.status);
971
+ list.push((item.status === 'started' || item.status === 'enqueued') && budgetObject(item).quota?.waiting
972
+ ? 'waiting_budget'
973
+ : item.status);
781
974
  signals[owner] = list;
782
975
  }
783
976
  return signals;
@@ -849,6 +1042,7 @@ export function createTicketBook(db, root, now = () => new Date()) {
849
1042
  ticket: {
850
1043
  id: item.id,
851
1044
  status: item.status,
1045
+ waiting_budget: (item.status === 'started' || item.status === 'enqueued') && budgetObject(item).quota?.waiting === true,
852
1046
  briefing_revision: item.briefing_revision,
853
1047
  child_session_id: item.child_session_id,
854
1048
  active_attempt_id: item.active_attempt_id,
@@ -1120,6 +1314,8 @@ function hostFailureText(reason) {
1120
1314
  return 'The worker lease expired and this round has stopped.';
1121
1315
  if (reason === 'enqueue-wall')
1122
1316
  return 'The task entered the queue and did not start before the time limit.';
1317
+ if (reason === 'reserve-wall')
1318
+ return 'The reserved start did not create a worker before the time limit, so the slot was released.';
1123
1319
  if (reason === 'steps')
1124
1320
  return 'The worker hit its step limit and this round has stopped.';
1125
1321
  return 'The worker has stopped.';
@@ -17,7 +17,7 @@ const OUTPUT = {
17
17
  */
18
18
  export function workToolDefinitions(work, engine) {
19
19
  return [
20
- tool('foundations_dispatch', 'Hand a long job to a worker session. Available only on a bot. status=accepted means received, not yet dispatched. status=enqueued means it entered the execution queue, not that it has started. The return value is never started. ownerSessionId is ignored.', {
20
+ tool('foundations_dispatch', 'Hand a non-trivial job to a worker session. Prefer this over doing the work with bash, write, edit, or an official subagent in the main session. Available only on a bot. status=accepted means received, and may wait for an execution slot. status=enqueued means it entered the execution queue, not that it has started. queued=true means it is waiting for a slot and will start automatically when one is free. The return value is never started. A BUDGET error means both the slot cap and the queue cap are full; do not start the job in the main session. ownerSessionId is ignored.', {
21
21
  type: 'object',
22
22
  properties: {
23
23
  briefing: { type: 'string' },
@@ -65,7 +65,7 @@ export function workToolDefinitions(work, engine) {
65
65
  ownerSessionId: { type: 'string', description: 'Ignored.' },
66
66
  },
67
67
  }, (args, hostId) => work.cancelTicket(hostId, args), 'bot'),
68
- tool('foundations_grant', 'Grant more time or tokens to an unfinished ticket. wallMs and maxTokens are amounts to add, not a new total. A budget-exhausted notice is not a failure. deny=true refuses the grant and stops this round.', {
68
+ tool('foundations_grant', 'Grant more active runtime or tokens to an unfinished ticket. wallMs and maxTokens are amounts to ADD, not totals. Exhausted workers wait at the next safe loop boundary without expiry. Use current usage/required totals and headroom in the notice/result: insufficient grants remain paused, host ceilings cannot be raised. deny=true stops this round; do not redispatch a replacement.', {
69
69
  type: 'object',
70
70
  properties: {
71
71
  ticketId: { type: 'string' },