dsh-taskbridge 1.3.2 → 1.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/mcp/wait.js CHANGED
@@ -14,9 +14,9 @@
14
14
  // means "nothing changed yet"; it is consumed here and never surfaced, because a
15
15
  // host that saw heartbeats would have a polling loop with extra steps.
16
16
  // 3. **A deadline is not a result.** When the caller's own time is up the answer
17
- // says `wait-limit` and keeps the task identity, so the host can immediately
18
- // wait again on the SAME task. It never means "review it", "resubmit it" or
19
- // "it failed".
17
+ // says `wait-limit`, `actionable: false` and keeps the task identity, so the host
18
+ // can immediately wait again on the SAME task. It never means "review it",
19
+ // "resubmit it" or "it failed".
20
20
  //
21
21
  // Metrics are counted HERE and nowhere else: `observationRequests` is the number of
22
22
  // internal HTTP observations, while `waitReturns` is 1 — the number of answers the
@@ -41,6 +41,10 @@ import {
41
41
  WAIT_LIMIT_REASON,
42
42
  isPermissionDecidable,
43
43
  } from './server.js';
44
+ import { REPEATED_FAILURE_KIND } from '../jobs/diagnostics.js';
45
+
46
+ /** Re-exported so a caller can assert the discriminator without importing the engine. */
47
+ export { REPEATED_FAILURE_KIND };
44
48
 
45
49
  /**
46
50
  * Reasons on which the wait loop keeps waiting instead of answering.
@@ -100,6 +104,19 @@ const TASK_FIELDS = Object.freeze([
100
104
  */
101
105
  const TERMINAL_REASONS = new Set(['ready', 'failed', 'cancelled', 'interrupted']);
102
106
 
107
+ /**
108
+ * Reasons that ask the host to DO something about the task.
109
+ *
110
+ * `wait-limit` is deliberately absent: it says only that this call's own budget ran
111
+ * out while nothing changed, so the correct next action is to wait again — not to
112
+ * review, decide, resubmit or cancel. Reporting it as actionable would invite a host
113
+ * to treat its own deadline as a business event, which is exactly the confusion this
114
+ * field exists to remove. The flag is a description of the answer, not a guarantee
115
+ * that a host will or will not surface it to its own model.
116
+ * @type {ReadonlySet<string>}
117
+ */
118
+ const ACTIONABLE_ANSWER_REASONS = new Set(['attention', 'permission', 'ready', 'failed', 'cancelled', 'interrupted']);
119
+
103
120
  /**
104
121
  * Task states a live run can be in.
105
122
  *
@@ -147,6 +164,24 @@ export function compactTask(task) {
147
164
  for (const key of TASK_FIELDS) {
148
165
  if (task[key] !== undefined) out[key] = task[key];
149
166
  }
167
+ // A wait NEVER carries the review note body: the conclusion's state, time and length are
168
+ // what a host needs to act, and the body is read on demand through `delegate_result`.
169
+ // The object is kept (with `noteOmitted`) so an old client cannot read "no note" as
170
+ // "not reviewed", and `reviewPersistence` states whether the conclusion is durable yet.
171
+ const acceptance = out.hostAcceptance;
172
+ if (acceptance !== null && typeof acceptance === 'object' && !Array.isArray(acceptance)) {
173
+ out.hostAcceptance = {
174
+ accepted: acceptance.accepted === true,
175
+ state: acceptance.state ?? null,
176
+ acceptedAt: acceptance.acceptedAt ?? null,
177
+ by: acceptance.by ?? null,
178
+ // Counted from the body when it is here, so the summary states the REAL length even
179
+ // on a view that carries the full note.
180
+ noteChars: acceptance.noteChars ?? (typeof acceptance.note === 'string' ? Array.from(acceptance.note).length : 0),
181
+ noteOmitted: true,
182
+ };
183
+ }
184
+ if (task.reviewPersistence !== undefined) out.reviewPersistence = task.reviewPersistence;
150
185
  // Activity and self-check are small, bounded summaries the engine produced for
151
186
  // exactly this purpose; they are carried whole rather than re-projected here.
152
187
  if (task.activity !== undefined) out.activity = task.activity;
@@ -185,16 +220,23 @@ function resolvedTaskState(task, reason) {
185
220
  function instructionFor(reason, facts = {}) {
186
221
  switch (reason) {
187
222
  case WAIT_LIMIT_REASON:
188
- return 'Nothing changed before this deadline. Call delegate_wait again with the same taskId; '
189
- + 'do not review, resubmit, restart or cancel the task on account of this answer.';
223
+ return 'This call\'s own wait budget ran out and NOTHING changed about the task. It is not a result and '
224
+ + 'no decision is needed. Call delegate_wait again with the same taskId (omit waitSeconds); do not '
225
+ + 'review, resubmit, restart or cancel the task on account of this answer.';
190
226
  case 'permission':
191
227
  return 'A per-call permission request is pending. Decide it with delegate_permission_decide using '
192
228
  + 'permission.requestId (and permission.fingerprint for allow-once), or reject it. The task is still '
193
229
  + 'running and waiting.';
194
230
  case 'attention':
195
- return 'No activity has been observed for about ten minutes while the task is still running. The run may '
196
- + 'be stuck. Check the current tool and idle time; cancel or follow up deliberately rather than '
197
- + 'assuming completion.';
231
+ if (facts.failureKind === REPEATED_FAILURE_KIND) {
232
+ return 'The worker has repeated the SAME tool operation and failed the same way several times in a row, '
233
+ + 'while the task is still running. See `failure` (toolName, failureCode, consecutive). This is a '
234
+ + 'question, not a verdict: decide deliberately whether to keep waiting, cancel, or follow up. Nothing '
235
+ + 'was cancelled or changed automatically, and the run is still going.';
236
+ }
237
+ return 'No activity has been observed for the configured inactivity threshold (ten minutes by default) '
238
+ + 'while the task is still running. The run may be stuck. Check the current tool and idle time; cancel '
239
+ + 'or follow up deliberately rather than assuming completion.';
198
240
  case 'ready':
199
241
  return 'The run ended and is awaiting host review. Read delegate_result for the diff and evidence; '
200
242
  + 'completion is not host acceptance.';
@@ -424,20 +466,35 @@ export async function waitForTask(options) {
424
466
  const state = facts.resolved === true
425
467
  ? (facts.resolvedState ?? null)
426
468
  : resolvedTaskState(task, reason);
469
+ // The repeated-failure diagnostic, when THAT is what the reminder was about. It is
470
+ // what lets a host tell "silent for ten minutes" from "kept failing the same call",
471
+ // which need different decisions. Only the bounded fields the engine produced travel.
472
+ const failure = reason === 'attention' && observation?.failure !== null
473
+ && typeof observation?.failure === 'object'
474
+ ? observation.failure
475
+ : null;
427
476
  const out = {
428
477
  taskId: typeof task.taskId === 'string' ? task.taskId : taskId,
429
478
  state,
430
479
  phase: task.phase ?? null,
431
480
  reason,
432
- actionable: true,
481
+ // `wait-limit` is NOT actionable: it reports this call's own deadline, not a
482
+ // task fact, and the only correct response is to wait again on the same task.
483
+ actionable: ACTIONABLE_ANSWER_REASONS.has(reason),
433
484
  // `terminal` is honest about the TASK: `wait-limit` and `attention` are
434
485
  // reasons to act, but the run is still going, which a host must not confuse
435
486
  // with a finished task. An interruption carries the flag its resolution decided.
436
487
  terminal: facts.resolved === true ? facts.terminal === true : TERMINAL_REASONS.has(reason),
437
- instruction: instructionFor(reason, { resolved: facts.resolved === true, taskState: state, kind: facts.kind }),
488
+ instruction: instructionFor(reason, {
489
+ resolved: facts.resolved === true,
490
+ taskState: state,
491
+ kind: facts.kind,
492
+ failureKind: failure?.kind ?? null,
493
+ }),
438
494
  task,
439
495
  metrics: metrics(1),
440
496
  };
497
+ if (failure !== null) out.failure = failure;
441
498
  // The permission block is carried WHOLE — identity, fingerprint, completeness
442
499
  // flags and the redacted operation — because a host that had to read
443
500
  // `delegate_result` first would be doing two round trips to answer one question,