dsh-taskbridge 1.3.2 → 1.4.0

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/server.js CHANGED
@@ -2,7 +2,7 @@
2
2
  //
3
3
  // Design rule taken from SPEC §10–11: an external MCP client may *exercise* an
4
4
  // authorization the operator already created, and may do nothing else. So this
5
- // server exposes seven tools and maps each one onto one execution-plane route:
5
+ // server exposes eight tools and maps each one onto one execution-plane route:
6
6
  //
7
7
  // delegate_status → GET /dsh-delegate/x/status
8
8
  // delegate_submit → POST /dsh-delegate/x/tasks
@@ -10,6 +10,7 @@
10
10
  // delegate_wait → POST /dsh-delegate/x/tasks/observe (repeated)
11
11
  // delegate_cancel → POST /dsh-delegate/x/tasks/cancel
12
12
  // delegate_permission_decide → POST /dsh-delegate/x/tasks/permission
13
+ // delegate_review → POST /dsh-delegate/x/tasks/review
13
14
  // delegate_preflight → POST /dsh-delegate/x/workspace/preflight
14
15
  //
15
16
  // `delegate_wait` is a read like `delegate_result`, and it is the one tool whose
@@ -33,6 +34,14 @@
33
34
  // a real tool-surface restriction, but NOT a claim that a process running as the same OS
34
35
  // user could not reach the shared local-MCP endpoint directly. See `runtime/permissions.js`.
35
36
  //
37
+ // `delegate_review` is the second narrow write, and it is the ONLY thing that records a
38
+ // task's `hostAcceptance`: the commander's own delivery conclusion (`accepted` or
39
+ // `changes-requested`). A finished run stays `awaiting-host-review` — that underlying
40
+ // state is what keeps older clients and `continueFromTaskId` compatible — and the review
41
+ // lives in its own field. It cannot approve a permission, change the switch, a provider,
42
+ // a model, the isolation or a credential, merge a diff, or create work; it also cannot
43
+ // be reached by the worker's own catalog, for the same reason as above.
44
+ //
36
45
  // `delegate_preflight` is the one addition that is not an authorization at all: it
37
46
  // is a read-only measurement of a workspace, and it exists so a dispatcher can
38
47
  // learn before submitting that a checkout is dirty, is not a repository, or would
@@ -76,7 +85,7 @@ export const SUPPORTED_PROTOCOL_VERSIONS = Object.freeze([
76
85
  export const LATEST_PROTOCOL_VERSION = SUPPORTED_PROTOCOL_VERSIONS[0];
77
86
 
78
87
  /** Identifies this server in the handshake. */
79
- export const SERVER_INFO = Object.freeze({ name: 'dsh-delegate', version: '1.3.2' });
88
+ export const SERVER_INFO = Object.freeze({ name: 'dsh-delegate', version: '1.4.0' });
80
89
 
81
90
  /** Bound on a tool result's text. A task view can be large; a model's context cannot. */
82
91
  export const MAX_TOOL_TEXT_CHARS = 20_000;
@@ -102,6 +111,14 @@ export const MAX_WAIT_SECONDS = 3_600;
102
111
  /** Shortest deadline one `delegate_wait` call may ask for, in seconds. */
103
112
  export const MIN_WAIT_SECONDS = 1;
104
113
 
114
+ /**
115
+ * Longest `delegate_review` note, in characters.
116
+ *
117
+ * The same bound the execution plane and the engine enforce, declared here so the
118
+ * advertised schema and the refusals cannot drift apart.
119
+ */
120
+ export const MAX_REVIEW_NOTE_CHARS = 1_000;
121
+
105
122
  /**
106
123
  * How many requests may be in flight at once on the stdio transport.
107
124
  *
@@ -282,11 +299,23 @@ export const TOOLS = Object.freeze([
282
299
  title: 'Wait for a task to need you',
283
300
  description: 'Follow ONE task this connection submitted and return as soon as it needs the host. '
284
301
  + 'A single call blocks for up to waitSeconds (default 3600) and returns a COMPACT answer: '
285
- + '`reason` is `permission` (a per-call request is pending), `attention` (no activity for about ten '
286
- + 'minutes, so the run may be stuck), `ready` (the run ended and is awaiting host review), `failed`, '
287
- + '`cancelled` or `interrupted`. '
288
- + '`reason: "wait-limit"` means the deadline was reached and NOTHING changed: call delegate_wait again '
289
- + 'with the same taskId — do not review, resubmit or restart the task. '
302
+ + '`reason` is `permission` (a per-call request is pending), `attention` (the run has been silent for '
303
+ + 'the configured inactivity threshold, ten minutes by default, so it may be stuck), `ready` (the run '
304
+ + 'ended and is awaiting host review), `failed`, `cancelled` or `interrupted`. '
305
+ + 'An `attention` answer may instead carry a `failure` block (kind `repeated-tool-failure`, with '
306
+ + 'toolName/failureCode/consecutive): the worker repeated the SAME operation and failed the same way '
307
+ + 'several times in a row. It is a question for the host, reported once; nothing is cancelled, retimed '
308
+ + 'or reconfigured automatically. '
309
+ + '`reason: "wait-limit"` means only that this call\u2019s own budget ran out with nothing changed: it is '
310
+ + 'reported with `actionable: false` and `terminal: false`. Call delegate_wait again with the same '
311
+ + 'taskId and omit waitSeconds — do not review, resubmit, restart or cancel the task. '
312
+ + 'Normal use is delegate_wait({ taskId }) with no second argument: the default already covers a full '
313
+ + 'hour and the call re-issues its own internal observations, so there is no reason to shorten it or to '
314
+ + 'poll delegate_result or delegate_status while a task runs. A shorter waitSeconds is accepted for a '
315
+ + 'one-off diagnostic you were explicitly asked for. '
316
+ + 'The internal observation cadence (about one bounded request every 25 seconds) belongs to THIS '
317
+ + 'program; it is not a polling rate for the agent, and nothing about it requires action while nothing '
318
+ + 'has changed. '
290
319
  + 'Heartbeats are internal to this call and are never returned. '
291
320
  + 'This tool reads; it never submits, cancels or decides anything, and it does not fetch the task\u2019s '
292
321
  + 'diff or full result (use delegate_result for that, after an actionable reason). '
@@ -299,7 +328,21 @@ export const TOOLS = Object.freeze([
299
328
  + '`terminal: true` with `state: "interrupted"` means the task record itself is a restart history '
300
329
  + 'and will not resume. If the reason carried a settled state it is reported as that outcome '
301
330
  + '(`ready`/`failed`/`cancelled`). A missing or unrecognized state is reported as '
302
- + '`terminal: false` with nothing asserted: check before deciding.',
331
+ + '`terminal: false` with nothing asserted: check before deciding. '
332
+ + 'This call ending, or your own client tool timeout firing, says NOTHING about the execution: the task '
333
+ + 'may still be running. Keep its taskId, and if you need to know more read one compact fact with '
334
+ + 'delegate_result rather than assuming either way; never submit a duplicate, restart or cancel it '
335
+ + 'because a wait or a client timeout ended. '
336
+ + 'A host may hand a wait back early on its own tool/exec time limit even though this call asked for an '
337
+ + 'hour. That outer event is not `wait-limit` and is not a result: the MCP observation did not end and the '
338
+ + 'worker did not stop. Continue the SAME exec cell (do not open a second wait, read delegate_result, poll '
339
+ + 'delegate_status or submit a duplicate) and ask for the longest yield the host supports — when a yield or '
340
+ + 'pragmas parameter exists, an ordinary wait should request roughly 1200000 ms (20 minutes), not a minute, '
341
+ + 'and re-issuing every second is polling rather than waiting. A one-minute return is a caller choice, not a '
342
+ + 'documented hard limit, so do not present it as the client maximum and do not claim every client offers '
343
+ + 'the same yield. This plugin cannot create a background waiter or wake the agent: if the available yield '
344
+ + 'is genuinely short with no automatic continuation, a long wait costs one resumed turn per yield window, '
345
+ + 'and that limit must be reported honestly rather than worked around.',
303
346
  // A LONG read, and that is the point: `readOnlyHint` is what tells a
304
347
  // concurrency-aware client this call may be held for an hour without being a
305
348
  // mutating operation. A serialising host still ignores it; the independent
@@ -319,8 +362,10 @@ export const TOOLS = Object.freeze([
319
362
  type: 'integer',
320
363
  minimum: MIN_WAIT_SECONDS,
321
364
  maximum: MAX_WAIT_SECONDS,
322
- description: 'How long this one call may wait, in seconds (default 3600). '
323
- + 'A returned `wait-limit` means this deadline was reached, not that the task stalled.',
365
+ description: 'OPTIONAL. Omit it for normal waiting: the default is 3600 seconds (one hour). '
366
+ + 'Set it only for a shorter, one-off diagnostic you were explicitly asked to make. It cannot '
367
+ + 'extend a wait beyond 3600. A returned `wait-limit` with `actionable: false` means this '
368
+ + 'deadline was reached, not that the task stalled or ended.',
324
369
  },
325
370
  },
326
371
  required: ['taskId'],
@@ -399,6 +444,55 @@ export const TOOLS = Object.freeze([
399
444
  additionalProperties: false,
400
445
  }),
401
446
  }),
447
+ Object.freeze({
448
+ name: 'delegate_review',
449
+ title: 'Record the host review of a delivered task',
450
+ description: 'Record YOUR OWN delivery review of one task this connection submitted. A finished run stays '
451
+ + '\u0060awaiting-host-review\u0060 until you say what you concluded: this tool records that conclusion, and it '
452
+ + 'is the only thing that does. '
453
+ + '\u0060decision: "accepted"\u0060 means you reviewed the evidence with \u0060delegate_result\u0060 and it is right; '
454
+ + '\u0060decision: "changes-requested"\u0060 means it needs correction. Either way \u0060note\u0060 is required and '
455
+ + 'bounded (at most 1000 characters) — it is the recorded conclusion, not a place for secrets. '
456
+ + 'This cannot approve a permission request, change the switch, a provider, a model, the isolation or a '
457
+ + 'credential, merge or apply the diff, or create another task. Execution completion is never accepted '
458
+ + 'automatically: until this is called the task stays unreviewed. '
459
+ + 'Only a task awaiting host review (execution finished) can be reviewed; a running, failed, cancelled or '
460
+ + 'interrupted task, or another connection\u2019s task, is refused. '
461
+ + 'The first decision is final for this task: repeating the SAME decision with the SAME note is an '
462
+ + 'idempotent no-op, and a DIFFERENT conclusion is refused \u2014 send a follow-up task with '
463
+ + '\u0060continueFromTaskId\u0060 instead, so the original conclusion is preserved.',
464
+ // It records a conclusion on an existing task. It can neither read a workspace nor
465
+ // start, stop or re-configure anything, and a replayed identical decision changes
466
+ // nothing — but a DIFFERENT second decision is a refusal, so it is not fully
467
+ // idempotent either.
468
+ annotations: Object.freeze({
469
+ title: 'Record the host review of a delivered task',
470
+ readOnlyHint: false,
471
+ destructiveHint: false,
472
+ idempotentHint: false,
473
+ openWorldHint: false,
474
+ }),
475
+ inputSchema: Object.freeze({
476
+ type: 'object',
477
+ properties: {
478
+ taskId: { type: 'string', description: 'Task identity returned by delegate_submit.' },
479
+ decision: {
480
+ type: 'string',
481
+ enum: ['accepted', 'changes-requested'],
482
+ description: 'accepted records that you reviewed the result and accept it; changes-requested records '
483
+ + 'that it needs correction.',
484
+ },
485
+ note: {
486
+ type: 'string',
487
+ maxLength: MAX_REVIEW_NOTE_CHARS,
488
+ description: 'Required non-empty bounded conclusion (at most 1000 characters), stored redacted. '
489
+ + 'It is evidence of your review, never a grant and never a place for credentials.',
490
+ },
491
+ },
492
+ required: ['taskId', 'decision', 'note'],
493
+ additionalProperties: false,
494
+ }),
495
+ }),
402
496
  Object.freeze({
403
497
  name: 'delegate_preflight',
404
498
  title: 'Inspect workspace before dispatch',
@@ -439,6 +533,7 @@ const TOOL_ROUTES = Object.freeze({
439
533
  delegate_wait: 'observe',
440
534
  delegate_cancel: 'cancel',
441
535
  delegate_permission_decide: 'permission',
536
+ delegate_review: 'review',
442
537
  delegate_preflight: 'preflight',
443
538
  });
444
539
 
@@ -479,6 +574,11 @@ function validateArgs(tool, args) {
479
574
  const field = value[key];
480
575
  if (declared.type === 'string') {
481
576
  if (typeof field !== 'string') throw new McpError('MCP_ARGUMENT_TYPE', `${tool.name}.${key} must be a string`);
577
+ // A declared bound is enforced, not merely advertised: a note over the limit is
578
+ // refused with the field named instead of reaching the plane and being cut there.
579
+ if (typeof declared.maxLength === 'number' && field.length > declared.maxLength) {
580
+ throw new McpError('MCP_ARGUMENT_RANGE', `${tool.name}.${key} must be at most ${declared.maxLength} characters`);
581
+ }
482
582
  out[key] = field;
483
583
  } else if (declared.type === 'boolean') {
484
584
  // Strict: a model that sends the string "true" gets a refusal naming the
@@ -1554,16 +1654,42 @@ export function createMcpServer(options) {
1554
1654
  + 'task stays `running` with phase `awaiting-permission`. Answer exactly one such request with '
1555
1655
  + 'delegate_permission_decide (`allow-once` or `reject`). That decision covers only that call, is '
1556
1656
  + 'never a standing grant, and cannot approve anything else. To follow a task without polling, use '
1557
- + 'delegate_wait: one call blocks until the task needs you (permission, ready, failed, cancelled, '
1558
- + 'interrupted) or until its deadline, in which case it answers reason "wait-limit" and you call '
1559
- + 'it again with the same taskId instead of reviewing or resubmitting anything. '
1657
+ + 'delegate_wait({ taskId }): one call blocks until the task needs you (permission, ready, failed, '
1658
+ + 'cancelled, interrupted) or until its own one-hour deadline, in which case it answers reason '
1659
+ + '`wait-limit` with `actionable: false` and you call it again with the same taskId instead of '
1660
+ + 'reviewing or resubmitting anything. Omit waitSeconds for normal waiting: the default already '
1661
+ + 'covers an hour, it cannot be raised above 3600, and a shorter value is only for a one-off '
1662
+ + 'diagnostic you were explicitly asked for. The approximately 25-second observation cadence '
1663
+ + 'inside delegate_wait is internal to this program, not a polling rate: while nothing has '
1664
+ + 'changed, do not read intermediate code or diff, do not research or implement in parallel, and '
1665
+ + 'do not narrate unchanged snapshots — wait, and review once on delivery. A wait ending, or your '
1666
+ + 'own client tool timeout firing, does not prove the execution stopped or that it is alive: keep '
1667
+ + 'the taskId and check one compact fact if you need to, and never duplicate, restart or cancel a '
1668
+ + 'task merely because a wait or a client timeout ended. '
1560
1669
  + '`interrupted` never means the execution stopped by itself: this call returns it at once '
1561
1670
  + 'with `terminal: false` while the task is still pending or running (wait again on the same '
1562
1671
  + 'taskId); only a task record that is itself interrupted (a restart history) is `terminal: true`. '
1672
+ + 'A host that hands the wait back on its own tool/exec time limit is reporting an OUTER limit, '
1673
+ + 'not a result: the MCP observation did not end and the worker did not stop. Continue the SAME '
1674
+ + 'exec cell on the taskId you hold, ask for the longest yield the host supports (roughly 1200000 '
1675
+ + 'ms when a yield/pragmas parameter exists, not a minute) rather than re-issuing every second, and '
1676
+ + 'do not open a second wait, read delegate_result or submit a duplicate. A one-minute return is a '
1677
+ + 'caller choice, not a documented hard limit, and not every client offers the same yield. This '
1678
+ + 'plugin cannot create a background waiter or wake the agent, so a client with only a short yield '
1679
+ + 'and no automatic continuation costs one resumed turn per yield window — state that plainly '
1680
+ + 'instead of inventing a wake-up. An `attention` answer that carries a `failure` '
1681
+ + 'block means the worker repeated the same failed operation several times while still running: '
1682
+ + 'it is a question for the host, sent once, and nothing is cancelled or reconfigured for it. '
1563
1683
  + 'If a host serialises tool calls so that this '
1564
1684
  + 'connection is blocked by a long wait, the independently installed `dsh-delegate-control` '
1565
1685
  + 'binary can report or cancel one task from a separate process; it cannot submit, configure or '
1566
- + 'change the switch.',
1686
+ + 'change the switch. '
1687
+ + 'When a task reaches `ready`/`awaiting-host-review`, finish YOUR review and record it with '
1688
+ + 'delegate_review ({ taskId, decision: "accepted" | "changes-requested", note }): that call is the '
1689
+ + 'only thing that records host acceptance, and until it is made the task stays unreviewed. '
1690
+ + 'Completion is never acceptance on its own, and a review cannot approve permissions, change '
1691
+ + 'settings or models, merge the diff or create work. The first decision is final for that task; '
1692
+ + 'to request a correction, send a follow-up with `continueFromTaskId`.',
1567
1693
  });
1568
1694
  }
1569
1695
  case 'notifications/initialized':
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
  *
@@ -185,16 +202,23 @@ function resolvedTaskState(task, reason) {
185
202
  function instructionFor(reason, facts = {}) {
186
203
  switch (reason) {
187
204
  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.';
205
+ return 'This call\'s own wait budget ran out and NOTHING changed about the task. It is not a result and '
206
+ + 'no decision is needed. Call delegate_wait again with the same taskId (omit waitSeconds); do not '
207
+ + 'review, resubmit, restart or cancel the task on account of this answer.';
190
208
  case 'permission':
191
209
  return 'A per-call permission request is pending. Decide it with delegate_permission_decide using '
192
210
  + 'permission.requestId (and permission.fingerprint for allow-once), or reject it. The task is still '
193
211
  + 'running and waiting.';
194
212
  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.';
213
+ if (facts.failureKind === REPEATED_FAILURE_KIND) {
214
+ return 'The worker has repeated the SAME tool operation and failed the same way several times in a row, '
215
+ + 'while the task is still running. See `failure` (toolName, failureCode, consecutive). This is a '
216
+ + 'question, not a verdict: decide deliberately whether to keep waiting, cancel, or follow up. Nothing '
217
+ + 'was cancelled or changed automatically, and the run is still going.';
218
+ }
219
+ return 'No activity has been observed for the configured inactivity threshold (ten minutes by default) '
220
+ + 'while the task is still running. The run may be stuck. Check the current tool and idle time; cancel '
221
+ + 'or follow up deliberately rather than assuming completion.';
198
222
  case 'ready':
199
223
  return 'The run ended and is awaiting host review. Read delegate_result for the diff and evidence; '
200
224
  + 'completion is not host acceptance.';
@@ -424,20 +448,35 @@ export async function waitForTask(options) {
424
448
  const state = facts.resolved === true
425
449
  ? (facts.resolvedState ?? null)
426
450
  : resolvedTaskState(task, reason);
451
+ // The repeated-failure diagnostic, when THAT is what the reminder was about. It is
452
+ // what lets a host tell "silent for ten minutes" from "kept failing the same call",
453
+ // which need different decisions. Only the bounded fields the engine produced travel.
454
+ const failure = reason === 'attention' && observation?.failure !== null
455
+ && typeof observation?.failure === 'object'
456
+ ? observation.failure
457
+ : null;
427
458
  const out = {
428
459
  taskId: typeof task.taskId === 'string' ? task.taskId : taskId,
429
460
  state,
430
461
  phase: task.phase ?? null,
431
462
  reason,
432
- actionable: true,
463
+ // `wait-limit` is NOT actionable: it reports this call's own deadline, not a
464
+ // task fact, and the only correct response is to wait again on the same task.
465
+ actionable: ACTIONABLE_ANSWER_REASONS.has(reason),
433
466
  // `terminal` is honest about the TASK: `wait-limit` and `attention` are
434
467
  // reasons to act, but the run is still going, which a host must not confuse
435
468
  // with a finished task. An interruption carries the flag its resolution decided.
436
469
  terminal: facts.resolved === true ? facts.terminal === true : TERMINAL_REASONS.has(reason),
437
- instruction: instructionFor(reason, { resolved: facts.resolved === true, taskState: state, kind: facts.kind }),
470
+ instruction: instructionFor(reason, {
471
+ resolved: facts.resolved === true,
472
+ taskState: state,
473
+ kind: facts.kind,
474
+ failureKind: failure?.kind ?? null,
475
+ }),
438
476
  task,
439
477
  metrics: metrics(1),
440
478
  };
479
+ if (failure !== null) out.failure = failure;
441
480
  // The permission block is carried WHOLE — identity, fingerprint, completeness
442
481
  // flags and the redacted operation — because a host that had to read
443
482
  // `delegate_result` first would be doing two round trips to answer one question,