@thready-web/dsh-bot-plugin 0.1.0-beta.2 → 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.
@@ -1,19 +1,177 @@
1
1
  /** Admission and host budgets. Neither function calls a model or reorders an inbox. */
2
+ /**
3
+ * Main-session caps. The main bot may still call host tools; these caps are a
4
+ * runaway guard. Worker parallelism is a separate slot/queue policy: `perBot`
5
+ * and `global` limit running workers, and `perBotQueue` / `globalQueue` cap
6
+ * tickets waiting for a slot. `needs_user` does not occupy a running slot.
7
+ * `main*` is the cap after a dispatch or a host tool and is never looser than
8
+ * `chat*`.
9
+ */
10
+ /** Hard ceiling so a typo cannot raise a slot cap without bound. */
11
+ export const WORK_SLOT_MAX = 64;
12
+ /** Hard ceiling so a typo cannot raise a queue cap without bound. */
13
+ export const WORK_QUEUE_MAX = 256;
2
14
  export const DEFAULT_WORK_BUDGET = Object.freeze({
3
- mainMaxSteps: 2,
4
- mainWallMs: 20_000,
5
- chatMaxSteps: 4,
6
- chatWallMs: 120_000,
15
+ mainMaxSteps: 80,
16
+ mainWallMs: 30 * 60 * 1000,
17
+ chatMaxSteps: 80,
18
+ chatWallMs: 30 * 60 * 1000,
7
19
  holdMs: 15_000,
8
20
  workerMaxSteps: 8,
9
21
  workerWallMs: 10 * 60 * 1000,
10
22
  workerMaxTokens: 400_000,
11
- perBot: 1,
12
- global: 2,
23
+ perBot: 2,
24
+ global: 4,
25
+ perBotQueue: 8,
26
+ globalQueue: 16,
13
27
  leaseMs: 60_000,
14
28
  starveMs: 30_000,
15
29
  mergeWindowMs: 1_000,
16
30
  });
31
+ const BUDGET_KEYS = Object.keys(DEFAULT_WORK_BUDGET);
32
+ const INT_MAX = {
33
+ perBot: WORK_SLOT_MAX,
34
+ global: WORK_SLOT_MAX,
35
+ perBotQueue: WORK_QUEUE_MAX,
36
+ globalQueue: WORK_QUEUE_MAX,
37
+ };
38
+ /**
39
+ * A single bot cannot run more workers than the global cap, even if `perBot`
40
+ * is larger. The same applies to the queue caps. Existing running workers are
41
+ * not cancelled when a later process start uses a lower cap.
42
+ * @param {WorkBudget} budget
43
+ */
44
+ export function effectiveWorkLimits(budget) {
45
+ return {
46
+ perBot: Math.min(budget.perBot, budget.global),
47
+ global: budget.global,
48
+ perBotQueue: Math.min(budget.perBotQueue, budget.globalQueue),
49
+ globalQueue: budget.globalQueue,
50
+ };
51
+ }
52
+ /** Environment variable the launcher uses to pass `--work-budget` to the plugin. */
53
+ export const WORK_BUDGET_ENV = 'DSH_BOT_WORK_BUDGET';
54
+ /**
55
+ * Milliseconds from a number or a `90s` / `30m` / `2h` / `1500ms` string.
56
+ * @param {unknown} value
57
+ */
58
+ function durationMs(value) {
59
+ if (typeof value === 'number')
60
+ return value;
61
+ if (typeof value !== 'string')
62
+ return Number.NaN;
63
+ const match = /^\s*(\d+(?:\.\d+)?)\s*(ms|s|m|h)?\s*$/i.exec(value);
64
+ if (!match)
65
+ return Number.NaN;
66
+ const unit = (match[2] ?? 'ms').toLowerCase();
67
+ const scale = unit === 'h' ? 3_600_000 : unit === 'm' ? 60_000 : unit === 's' ? 1000 : 1;
68
+ return Number(match[1]) * scale;
69
+ }
70
+ /**
71
+ * Keep only known, positive, finite budget fields. Unknown keys and bad values
72
+ * are dropped (and listed in `ignored`) so a typo never zeroes a cap or raises
73
+ * it to Infinity. Slot and queue integers above WORK_SLOT_MAX / WORK_QUEUE_MAX
74
+ * are clamped (and listed in `clamped`). Non-integer slot/queue/step values
75
+ * are ignored.
76
+ * Shorthands: `steps` sets mainMaxSteps and chatMaxSteps; `wall` sets mainWallMs and chatWallMs.
77
+ * Fields ending in `Ms` accept duration strings such as `30m`.
78
+ * @param {unknown} input
79
+ */
80
+ export function normalizeWorkBudget(input) {
81
+ const budget = {};
82
+ const ignored = [];
83
+ const clamped = [];
84
+ if (input == null || typeof input !== 'object' || Array.isArray(input))
85
+ return { budget, ignored, clamped };
86
+ const raw = { ...input };
87
+ const expanded = {};
88
+ if ('steps' in raw) {
89
+ expanded.mainMaxSteps = raw.steps;
90
+ expanded.chatMaxSteps = raw.steps;
91
+ delete raw.steps;
92
+ }
93
+ if ('wall' in raw) {
94
+ expanded.mainWallMs = raw.wall;
95
+ expanded.chatWallMs = raw.wall;
96
+ delete raw.wall;
97
+ }
98
+ Object.assign(expanded, raw);
99
+ for (const [key, value] of Object.entries(expanded)) {
100
+ if (!BUDGET_KEYS.includes(key)) {
101
+ ignored.push(key);
102
+ continue;
103
+ }
104
+ const duration = key.endsWith('Ms');
105
+ const number = duration ? durationMs(value) : Number(value);
106
+ if (!Number.isFinite(number) || number <= 0) {
107
+ ignored.push(key);
108
+ continue;
109
+ }
110
+ if (!duration && !Number.isInteger(number)) {
111
+ ignored.push(key);
112
+ continue;
113
+ }
114
+ const max = INT_MAX[key];
115
+ if (max != null && number > max) {
116
+ budget[key] = max;
117
+ clamped.push(key);
118
+ continue;
119
+ }
120
+ budget[key] = duration ? Math.floor(number) : number;
121
+ }
122
+ return { budget, ignored, clamped };
123
+ }
124
+ /**
125
+ * Parse `--work-budget` / DSH_BOT_WORK_BUDGET: JSON (`{"steps":80,"wall":"30m"}`)
126
+ * or comma-separated `key=value` (`steps=80,wall=30m,mainMaxSteps=10`).
127
+ * @param {string | undefined} text
128
+ */
129
+ export function parseWorkBudget(text) {
130
+ const source = typeof text === 'string' ? text.trim() : '';
131
+ if (source === '')
132
+ return { budget: {}, ignored: [], clamped: [] };
133
+ if (source.startsWith('{')) {
134
+ try {
135
+ return normalizeWorkBudget(JSON.parse(source));
136
+ }
137
+ catch {
138
+ return { budget: {}, ignored: [source], clamped: [] };
139
+ }
140
+ }
141
+ const object = {};
142
+ const ignored = [];
143
+ for (const part of source.split(',')) {
144
+ const piece = part.trim();
145
+ if (piece === '')
146
+ continue;
147
+ const eq = piece.indexOf('=');
148
+ if (eq <= 0) {
149
+ ignored.push(piece);
150
+ continue;
151
+ }
152
+ object[piece.slice(0, eq).trim()] = piece.slice(eq + 1).trim();
153
+ }
154
+ const normalized = normalizeWorkBudget(object);
155
+ return { budget: normalized.budget, ignored: [...ignored, ...normalized.ignored], clamped: normalized.clamped };
156
+ }
157
+ /**
158
+ * Defaults, then the environment (launcher `--work-budget` / `DSH_BOT_WORK_BUDGET`),
159
+ * then plugin config `work`. Limits are applied when the work runtime is created
160
+ * (process start or plugin load). They are not hot-reloaded into an already
161
+ * running runtime. Lowering a cap never cancels a worker that is already running;
162
+ * later dispatch and promotion use the new caps.
163
+ * @param {unknown} config
164
+ * @param {string | undefined} envText
165
+ */
166
+ export function resolveWorkBudget(config, envText) {
167
+ const fromEnv = parseWorkBudget(envText);
168
+ const fromConfig = normalizeWorkBudget(config);
169
+ return {
170
+ budget: { ...DEFAULT_WORK_BUDGET, ...fromEnv.budget, ...fromConfig.budget },
171
+ ignored: [...fromEnv.ignored, ...fromConfig.ignored],
172
+ clamped: [...fromEnv.clamped, ...fromConfig.clamped],
173
+ };
174
+ }
17
175
  /**
18
176
  * Pending next-turn ids whose source is not a ticket notice and not a delivery.
19
177
  * @param {string[]} pendingIds
@@ -101,6 +259,8 @@ export function estimateWorkerBudget(briefing, suggestion, defaults = DEFAULT_WO
101
259
  maxTokens: clampInt(suggestion?.maxTokens, 1000, defaults.workerMaxTokens) ?? guessedTokens,
102
260
  perBot: defaults.perBot,
103
261
  global: defaults.global,
262
+ perBotQueue: defaults.perBotQueue,
263
+ globalQueue: defaults.globalQueue,
104
264
  };
105
265
  }
106
266
  /**
@@ -1,9 +1,12 @@
1
1
  /**
2
2
  * File tools for a worker. Plugin-created agents do not inherit the web preset,
3
3
  * so their request header otherwise lists only host tools.
4
- * This preset is tool-fs only: read, write, and edit. It does not add bash,
5
- * and it does not change the sandbox mode or add writable roots.
6
- * The session cwd copied at create remains the workspace-write boundary.
4
+ * This preset mounts official file tools and official command tools
5
+ * (`@deepseek-ai/dsh-tool-bash`, `@deepseek-ai/dsh-tool-jobs`). It does not
6
+ * change the host sandbox mode, add writable roots, or mount the web preset
7
+ * (no web_fetch, subagent, or other profile tools). The session cwd copied
8
+ * at create remains the workspace-write boundary; bash uses the already
9
+ * composed host bash-sandbox and approval policy.
7
10
  */
8
11
  export const WORKER_FILE_PRESET = 'foundations-files';
9
12
  /**
@@ -42,6 +45,8 @@ export function ensureWorkerFilePreset(ctx) {
42
45
  name: '@deepseek-ai/dsh-tool-fs',
43
46
  config: { applyPatch: false, readImage: false },
44
47
  },
48
+ { id: 'tool-bash', name: '@deepseek-ai/dsh-tool-bash' },
49
+ { id: 'tool-jobs', name: '@deepseek-ai/dsh-tool-jobs' },
45
50
  ],
46
51
  })
47
52
  .then(() => WORKER_FILE_PRESET)
package/dist/work-http.js CHANGED
@@ -141,12 +141,36 @@ export function openPageLive(req, res, source) {
141
141
  const intervalMs = source.intervalMs ?? 1000;
142
142
  /** @type {ReturnType<typeof setInterval> | null} */
143
143
  let timer = null;
144
+ let interactionTimer = null;
145
+ let lastInteractions = '';
146
+ let interactionFailed = false;
147
+ const pumpInteractions = () => {
148
+ if (stopped || !source.interactions)
149
+ return;
150
+ try {
151
+ const text = JSON.stringify(source.interactions());
152
+ if (text === lastInteractions && !interactionFailed)
153
+ return;
154
+ interactionFailed = false;
155
+ lastInteractions = text;
156
+ res.write(`event: interactions\ndata: ${text}\n\n`);
157
+ }
158
+ catch {
159
+ // Downgrade the client to REST fallback without closing desk/work updates.
160
+ if (!interactionFailed) {
161
+ interactionFailed = true;
162
+ res.write('event: interaction-error\ndata: {}\n\n');
163
+ }
164
+ }
165
+ };
144
166
  const stop = () => {
145
167
  if (stopped)
146
168
  return;
147
169
  stopped = true;
148
170
  if (timer)
149
171
  clearInterval(timer);
172
+ if (interactionTimer)
173
+ clearInterval(interactionTimer);
150
174
  if (!res.writableEnded)
151
175
  res.end();
152
176
  };
@@ -201,6 +225,11 @@ export function openPageLive(req, res, source) {
201
225
  busy = false;
202
226
  }
203
227
  };
228
+ pumpInteractions();
229
+ if (source.interactions) {
230
+ interactionTimer = setInterval(pumpInteractions, intervalMs);
231
+ interactionTimer.unref?.();
232
+ }
204
233
  void pump();
205
234
  timer = setInterval(() => {
206
235
  void pump();
@@ -136,6 +136,13 @@ export async function pushInput(book, input, deps) {
136
136
  const ticket = book.ticket(input.ticket_id);
137
137
  if (!attempt?.child_session_id || !ticket)
138
138
  return { ok: false, status: 'pending' };
139
+ if (input.kind !== 'cancel_note' &&
140
+ (['failed', 'cancelled', 'delivered'].includes(ticket.status) ||
141
+ ticket.active_attempt_id !== input.attempt_id ||
142
+ ticket.briefing_revision !== input.briefing_revision)) {
143
+ book.markInputFlush(input.id, 'dead', deps.pid, 'ticket-finished-or-superseded', null, false);
144
+ return { ok: false, status: 'dead' };
145
+ }
139
146
  let budget = { wallMs: 600_000, maxTokens: 400_000 };
140
147
  try {
141
148
  budget = { ...budget, ...JSON.parse(ticket.budget_json) };
@@ -23,6 +23,7 @@ export async function startWorkMock(options) {
23
23
  pendingIds: sessionId => world.pendingIds(sessionId),
24
24
  cancel: (sessionId, cancelOptions) => world.cancel(sessionId, cancelOptions),
25
25
  snapshot: sessionId => world.snapshot(sessionId),
26
+ childPresent: sessionId => world.hasSession(sessionId),
26
27
  now,
27
28
  pid: process.pid,
28
29
  });
package/dist/work-mock.js CHANGED
@@ -97,11 +97,21 @@ export function createMockWorld() {
97
97
  return true;
98
98
  },
99
99
  async cancel(sessionId, options) {
100
- cancels.push({ sessionId, keepInbox: options?.keepInbox === true });
100
+ cancels.push({
101
+ sessionId,
102
+ keepInbox: options?.keepInbox === true,
103
+ ...(typeof options?.reason === 'string' ? { reason: options.reason } : {}),
104
+ });
101
105
  },
102
106
  async snapshot(sessionId) {
103
107
  return session(sessionId).events.map(event => ({ ...event, data: event.data }));
104
108
  },
109
+ hasSession(sessionId) {
110
+ return sessions.has(sessionId);
111
+ },
112
+ dropChild(sessionId) {
113
+ sessions.delete(sessionId);
114
+ },
105
115
  };
106
116
  /**
107
117
  * @param {string} sessionId
@@ -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.';