dsh-taskbridge 1.4.2 → 1.5.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/CHANGELOG.md +34 -2
- package/README.md +10 -8
- package/bin/dsh-delegate-mcp.d.ts +20 -3
- package/bin/dsh-delegate-mcp.js +15 -0
- package/codex/dsh-taskbridge.toml +180 -16
- package/docs/INTRODUCTION.md +2 -2
- package/docs/INTRODUCTION.zh.md +2 -2
- package/docs/codex-integration.md +11 -13
- package/docs/conversation-placement.md +44 -7
- package/docs/plugin-market.md +1 -1
- package/docs/redaction-contract.md +47 -4
- package/docs/releases/1.5.0-token-benchmark.zh.md +73 -0
- package/docs/releases/1.5.0.zh.md +51 -0
- package/docs/releases/1.5.1.zh.md +70 -0
- package/docs/runtime-compatibility.md +4 -4
- package/docs/silent-wait.md +157 -3
- package/docs/taskbridge-upgrade.md +1 -1
- package/docs/worker-permissions.md +154 -17
- package/lib/client.js +298 -103
- package/package.json +9 -3
- package/src/client/api.js +315 -34
- package/src/client/index.jsx +151 -67
- package/src/codex/installer.js +251 -11
- package/src/config/redact.js +72 -11
- package/src/http/execution-plane.js +124 -10
- package/src/index.d.ts +347 -0
- package/src/index.js +88 -5
- package/src/jobs/activity.js +102 -9
- package/src/jobs/continuation-identity.js +283 -0
- package/src/jobs/engine.js +491 -55
- package/src/jobs/history.js +96 -0
- package/src/mcp/continuation.js +317 -0
- package/src/mcp/metrics.js +366 -0
- package/src/mcp/server.js +1203 -53
- package/src/mcp/wait.js +338 -73
- package/src/review/prompts.js +145 -126
- package/src/runtime/agent-runner.js +330 -0
- package/src/runtime/permissions.js +192 -2
- package/src/runtime/question-call.js +162 -0
- package/src/runtime/questions.js +909 -0
- package/src/runtime/sandbox.js +363 -358
- package/src/runtime/session-placement.js +1207 -1121
- package/src/version.js +121 -0
- package/src/workspace/worktrees.js +70 -4
|
@@ -97,6 +97,11 @@ export const RUN_CODES = Object.freeze({
|
|
|
97
97
|
// bridge while installing `never` would look like the commander could decide,
|
|
98
98
|
// and nothing would ever reach it.
|
|
99
99
|
permissionBridgeUnavailable: 'RUN_PERMISSION_BRIDGE_UNAVAILABLE',
|
|
100
|
+
// A question bridge was handed to the run but the answerer could not be registered on
|
|
101
|
+
// its Agent. The run still starts: unlike an approval bridge, an uninstalled question
|
|
102
|
+
// answerer cannot misrepresent a grant — it only means questions cannot be surfaced,
|
|
103
|
+
// which the tool surface already governs.
|
|
104
|
+
questionBridgeUnavailable: 'RUN_QUESTION_BRIDGE_UNAVAILABLE',
|
|
100
105
|
// The conversation's own model API (`agent.model`)
|
|
101
106
|
// is present but unusable: it threw, answered nothing, answered a route
|
|
102
107
|
// this run did not select, or exposed a surface that cannot be selected through
|
|
@@ -291,6 +296,23 @@ export function userMessage(text) {
|
|
|
291
296
|
return createUserMessage({ content: [{ type: 'text', text }], source: { kind: 'user' } });
|
|
292
297
|
}
|
|
293
298
|
|
|
299
|
+
/**
|
|
300
|
+
* The production message factory a bridge should be handed.
|
|
301
|
+
*
|
|
302
|
+
* Exported so the plugin's own composition can pass the REAL `createUserMessage` to a
|
|
303
|
+
* bridge instead of relying on that bridge's local fallback: the native helper normalises
|
|
304
|
+
* ids, roles and content, and a hand-built object is only ever a stand-in. The source kind
|
|
305
|
+
* (`'user-approval'`) is the one the native approval service already uses for its policy
|
|
306
|
+
* notices, so the message is a recognised, natively-supported shape rather than an
|
|
307
|
+
* invented one.
|
|
308
|
+
* @param {string} text - message body.
|
|
309
|
+
* @param {object} [source] - a `MessageSourceMap` entry; defaults to `{ kind: 'user-approval' }`.
|
|
310
|
+
* @returns {object} frozen user message.
|
|
311
|
+
*/
|
|
312
|
+
export function approvalFeedbackMessage(text, source = { kind: 'user-approval' }) {
|
|
313
|
+
return createUserMessage({ content: [{ type: 'text', text }], source });
|
|
314
|
+
}
|
|
315
|
+
|
|
294
316
|
/**
|
|
295
317
|
* One run's execution deadline, with a real pause and resume.
|
|
296
318
|
*
|
|
@@ -631,6 +653,9 @@ export async function driveAgent(deps, request) {
|
|
|
631
653
|
const state = {
|
|
632
654
|
abortRequested: false,
|
|
633
655
|
abortCause: null,
|
|
656
|
+
// The caller's own cancellation, so a pre-prompt step (the Agent Teams selection) can
|
|
657
|
+
// pass a real AbortSignal to the native service and honour the run's deadline/cancel.
|
|
658
|
+
signal: request?.signal ?? null,
|
|
634
659
|
handle: null,
|
|
635
660
|
handleDisposed: false,
|
|
636
661
|
// A resolved Agent is driven through the same handle shape a created one
|
|
@@ -667,9 +692,15 @@ export async function driveAgent(deps, request) {
|
|
|
667
692
|
// an armed permission bridge is why the Session asks instead of rejecting.
|
|
668
693
|
// Reported as a verified fact, never as a configuration value.
|
|
669
694
|
approval: null,
|
|
695
|
+
// The Agent Teams selection this run applied to its own root Session, or null when
|
|
696
|
+
// the caller made no team choice at all.
|
|
697
|
+
team: null,
|
|
670
698
|
// Removes the `approval/request` answerer when the run ends. Null when the run
|
|
671
699
|
// had no bridge, which is the ordinary worker run.
|
|
672
700
|
permissionAnswerer: null,
|
|
701
|
+
// Removes the `user-questions/request` answerer when the run ends. Null when the
|
|
702
|
+
// run had no question bridge or the harness exposes no such seam.
|
|
703
|
+
questionAnswerer: null,
|
|
673
704
|
// The execution deadline this run owns. It is PAUSED around every permission
|
|
674
705
|
// wait, so the wait is not charged to the operator's execution budget.
|
|
675
706
|
deadline: null,
|
|
@@ -830,6 +861,37 @@ export async function driveAgent(deps, request) {
|
|
|
830
861
|
return finish(state, state.abortCause === 'timeout' ? RUN_OUTCOMES.timedOut : RUN_OUTCOMES.cancelled);
|
|
831
862
|
}
|
|
832
863
|
|
|
864
|
+
// Agent Teams selection, applied to THIS run's own Session before any model step.
|
|
865
|
+
//
|
|
866
|
+
// DSH owns the switch: `ctx.agentTeams.setEnabled(rootAgent, enabled, signal)` appends
|
|
867
|
+
// the Session's `team/mode` selection and flushes it, which is exactly what the model
|
|
868
|
+
// menu's "Use Agent Teams" control does. The plugin never guesses the value (no
|
|
869
|
+
// keyword heuristics) and never opens the global default.
|
|
870
|
+
//
|
|
871
|
+
// A REQUESTED team mode that cannot be applied STOPS the run here, before the prompt:
|
|
872
|
+
// continuing with the Session's old mode would silently do the opposite of what the
|
|
873
|
+
// commander asked. Only an ABSENT choice (`null`) keeps the Session as it is.
|
|
874
|
+
state.team = await applyTeamSelection(deps, handle.agent, request?.agentTeams ?? null, state, { aborted });
|
|
875
|
+
if (state.team !== null && typeof state.team.refuseReason === 'string') {
|
|
876
|
+
// A requested mode that could not be applied is a REFUSAL, not a downgrade: the
|
|
877
|
+
// worker is released without ever receiving the prompt, so nothing runs under the
|
|
878
|
+
// Session's old mode while the commander believes teams are on. The failure is
|
|
879
|
+
// stated on the run so the task record explains WHY nothing executed. A run that
|
|
880
|
+
// was cancelled or timed out while waiting ends as cancelled/timedOut instead.
|
|
881
|
+
if (state.team.refuseReason === 'agent-teams-aborted') {
|
|
882
|
+
await releaseHandle(state, handle);
|
|
883
|
+
return finish(state, state.abortCause === 'timeout' ? RUN_OUTCOMES.timedOut : RUN_OUTCOMES.cancelled);
|
|
884
|
+
}
|
|
885
|
+
state.error = {
|
|
886
|
+
code: 'RUN_AGENT_TEAMS_UNAVAILABLE',
|
|
887
|
+
message: `Agent Teams could not be applied before the prompt (${state.team.refuseReason}); `
|
|
888
|
+
+ 'the worker was not started under the Session\u2019s previous mode',
|
|
889
|
+
details: { team: state.team },
|
|
890
|
+
};
|
|
891
|
+
await releaseHandle(state, handle);
|
|
892
|
+
return finish(state, RUN_OUTCOMES.failed);
|
|
893
|
+
}
|
|
894
|
+
|
|
833
895
|
handle.agent.followup(userMessage(prompt));
|
|
834
896
|
await Promise.race([handle.agent.whenIdle(), aborted]);
|
|
835
897
|
|
|
@@ -874,6 +936,19 @@ export async function driveAgent(deps, request) {
|
|
|
874
936
|
// The owning fiber disposes the listener on unload; this is best effort.
|
|
875
937
|
}
|
|
876
938
|
}
|
|
939
|
+
// The QUESTION answerer is removed on the SAME every-exit path, for the same reason:
|
|
940
|
+
// a finished run must not keep a listener that could answer a question for an Agent it
|
|
941
|
+
// no longer drives. Removing it here (not only at dispose) is what lets the ordinary
|
|
942
|
+
// UI answerer take that Agent's questions again as soon as the run is over.
|
|
943
|
+
if (state.questionAnswerer !== null) {
|
|
944
|
+
const remove = state.questionAnswerer;
|
|
945
|
+
state.questionAnswerer = null;
|
|
946
|
+
try {
|
|
947
|
+
remove();
|
|
948
|
+
} catch {
|
|
949
|
+
// Best effort, exactly like the approval answerer above.
|
|
950
|
+
}
|
|
951
|
+
}
|
|
877
952
|
}
|
|
878
953
|
|
|
879
954
|
/**
|
|
@@ -965,6 +1040,11 @@ export async function driveAgent(deps, request) {
|
|
|
965
1040
|
policy: bridged?.approvalPolicy ?? null,
|
|
966
1041
|
bridged: bridged?.bridged === true,
|
|
967
1042
|
};
|
|
1043
|
+
// The QUESTION bridge is independent of the approval stance: asking a question
|
|
1044
|
+
// is not granting anything, so a run whose approval policy is `never` may still
|
|
1045
|
+
// ask. Its answerer is registered after the composition exists and before the
|
|
1046
|
+
// prompt, so no turn can ask with nothing listening.
|
|
1047
|
+
bindQuestionBridge(agent, runRequest);
|
|
968
1048
|
} catch (error) {
|
|
969
1049
|
// A bridge refusal keeps its own code: it is a different operator problem from
|
|
970
1050
|
// a policy that could not be installed, and reporting it as one would send an
|
|
@@ -988,6 +1068,41 @@ export async function driveAgent(deps, request) {
|
|
|
988
1068
|
}
|
|
989
1069
|
}
|
|
990
1070
|
|
|
1071
|
+
/**
|
|
1072
|
+
* Arm the run's QUESTION bridge and register its answerer for this Agent.
|
|
1073
|
+
*
|
|
1074
|
+
* A run with no question bridge is the ordinary worker run: nothing is registered and
|
|
1075
|
+
* the native chain falls through as before. A supplied bridge that cannot be armed is
|
|
1076
|
+
* a REFUSAL rather than a silent downgrade, because the caller believes questions are
|
|
1077
|
+
* surfaced; the operator would otherwise see a task sit in `execute` while its worker
|
|
1078
|
+
* waits for an answer nobody can give.
|
|
1079
|
+
* @param {object} agent - the live native Agent the run will drive.
|
|
1080
|
+
* @param {object} runRequest - the run request carrying the optional bridge.
|
|
1081
|
+
* @returns {void}
|
|
1082
|
+
* @throws {RunError} when a supplied question bridge cannot be armed or registered.
|
|
1083
|
+
*/
|
|
1084
|
+
function bindQuestionBridge(agent, runRequest) {
|
|
1085
|
+
const bridge = runRequest?.questionBridge ?? null;
|
|
1086
|
+
if (bridge === null || bridge === undefined) return;
|
|
1087
|
+
if (typeof bridge.arm !== 'function' || typeof bridge.answer !== 'function') {
|
|
1088
|
+
throw new RunError(
|
|
1089
|
+
RUN_CODES.questionBridgeUnavailable,
|
|
1090
|
+
'the requested question bridge cannot be used for this run, so the worker was not started',
|
|
1091
|
+
{ reason: 'the bridge exposes no arm()/answer()' },
|
|
1092
|
+
);
|
|
1093
|
+
}
|
|
1094
|
+
try {
|
|
1095
|
+
bridge.arm(agent);
|
|
1096
|
+
} catch (error) {
|
|
1097
|
+
throw new RunError(
|
|
1098
|
+
RUN_CODES.questionBridgeUnavailable,
|
|
1099
|
+
'the question bridge could not be bound to the delegated session, so the worker was not started',
|
|
1100
|
+
{ reason: typeof error?.details?.reason === 'string' ? error.details.reason : 'the bridge refused this agent' },
|
|
1101
|
+
);
|
|
1102
|
+
}
|
|
1103
|
+
installQuestionAnswerer(agent?.ctx ?? null, bridge);
|
|
1104
|
+
}
|
|
1105
|
+
|
|
991
1106
|
/**
|
|
992
1107
|
* Arm the run's permission bridge for the Agent this run will drive.
|
|
993
1108
|
*
|
|
@@ -1118,6 +1233,93 @@ export async function driveAgent(deps, request) {
|
|
|
1118
1233
|
};
|
|
1119
1234
|
}
|
|
1120
1235
|
|
|
1236
|
+
/**
|
|
1237
|
+
* Register this run's QUESTION bridge as the answerer for its own Agent.
|
|
1238
|
+
*
|
|
1239
|
+
* The seam is the native, agent-scoped waterfall `'user-questions/request'`
|
|
1240
|
+
* (`UserQuestionService.ask()` dispatches
|
|
1241
|
+
* `ctx.waterfall(scopeTarget(agent, agent), 'user-questions/request', …)`); a listener
|
|
1242
|
+
* on the AGENT's own context therefore sees only that Agent's questions, so an
|
|
1243
|
+
* ordinary DSH session keeps whatever answerer it already has.
|
|
1244
|
+
*
|
|
1245
|
+
* `prepend: true` is REQUIRED for the same reason the approval answerer needs it: the
|
|
1246
|
+
* waterfall gives the request to the FIRST listener that returns a value, and this run
|
|
1247
|
+
* exists to surface the question to the COMMANDER. Registered plainly, a UI answerer
|
|
1248
|
+
* that was composed earlier would claim it first and the commander would never learn
|
|
1249
|
+
* the worker was asking — which is exactly the acceptance failure (the task sat in
|
|
1250
|
+
* `execute` for ten minutes). The listener still falls through for every other agent.
|
|
1251
|
+
*
|
|
1252
|
+
* A question that cannot be answered (an approval intent, a malformed or oversized
|
|
1253
|
+
* set) makes the bridge THROW: the native seam turns a thrown answerer into a failed
|
|
1254
|
+
* tool result carrying the code, so the worker learns why immediately instead of
|
|
1255
|
+
* waiting out the run.
|
|
1256
|
+
* @param {object} agentCtx - the agent's scoped context (listener target).
|
|
1257
|
+
* @param {object} bridge - the armed question bridge.
|
|
1258
|
+
* @returns {() => void} disposer removing the listener.
|
|
1259
|
+
* @throws {RunError} when the context cannot register a question answerer.
|
|
1260
|
+
*/
|
|
1261
|
+
function installQuestionAnswerer(agentCtx, bridge) {
|
|
1262
|
+
if (agentCtx === null || agentCtx === undefined || typeof agentCtx.on !== 'function') {
|
|
1263
|
+
// A context that cannot register a listener has NOT been wired: returning a no-op
|
|
1264
|
+
// would let the run report a bridged question channel it does not have, and the
|
|
1265
|
+
// worker's question would then wait for an answerer that was never installed.
|
|
1266
|
+
throw new RunError(
|
|
1267
|
+
RUN_CODES.questionBridgeUnavailable,
|
|
1268
|
+
'the delegated agent exposes no context a question answerer could be registered on, so the worker was not started',
|
|
1269
|
+
{ reason: 'agent context unavailable' },
|
|
1270
|
+
);
|
|
1271
|
+
}
|
|
1272
|
+
let disposer;
|
|
1273
|
+
try {
|
|
1274
|
+
disposer = agentCtx.on('user-questions/request', async (request, next) => {
|
|
1275
|
+
if (bridge.owns(request) !== true) {
|
|
1276
|
+
// Not this Agent's question: leave the chain untouched, so an ordinary
|
|
1277
|
+
// session keeps the answerer it actually has.
|
|
1278
|
+
return await next();
|
|
1279
|
+
}
|
|
1280
|
+
// PAUSE the run's execution deadline for the duration of the wait, exactly as
|
|
1281
|
+
// the approval answerer does: a worker waiting for a human answer is not
|
|
1282
|
+
// consuming its work budget, and the pause is repaid on every exit.
|
|
1283
|
+
const deadline = state.deadline;
|
|
1284
|
+
const pausedAt = Date.now();
|
|
1285
|
+
deadline?.pause();
|
|
1286
|
+
try {
|
|
1287
|
+
return await bridge.answer(request);
|
|
1288
|
+
} finally {
|
|
1289
|
+
if (deadline !== null) {
|
|
1290
|
+
deadline.notePaused(Date.now() - pausedAt);
|
|
1291
|
+
deadline.resume();
|
|
1292
|
+
}
|
|
1293
|
+
}
|
|
1294
|
+
}, { prepend: true });
|
|
1295
|
+
} catch (error) {
|
|
1296
|
+
throw new RunError(
|
|
1297
|
+
RUN_CODES.questionBridgeUnavailable,
|
|
1298
|
+
'the question answerer could not be registered for the delegated session',
|
|
1299
|
+
{ reason: typeof error?.code === 'string' ? error.code : 'agent context refused the answerer' },
|
|
1300
|
+
);
|
|
1301
|
+
}
|
|
1302
|
+
if (typeof disposer !== 'function') {
|
|
1303
|
+
// A registration that returns no disposer cannot be removed, so it would outlive the
|
|
1304
|
+
// run and keep answering that Agent's questions from a finished task.
|
|
1305
|
+
throw new RunError(
|
|
1306
|
+
RUN_CODES.questionBridgeUnavailable,
|
|
1307
|
+
'the question answerer registration returned no disposer, so it could not be removed when the run ended',
|
|
1308
|
+
{ reason: 'no disposer' },
|
|
1309
|
+
);
|
|
1310
|
+
}
|
|
1311
|
+
state.questionAnswerer = disposer;
|
|
1312
|
+
return () => {
|
|
1313
|
+
const remove = state.questionAnswerer;
|
|
1314
|
+
state.questionAnswerer = null;
|
|
1315
|
+
try {
|
|
1316
|
+
remove?.();
|
|
1317
|
+
} catch {
|
|
1318
|
+
// Best effort: the owning fiber disposes it on unload.
|
|
1319
|
+
}
|
|
1320
|
+
};
|
|
1321
|
+
}
|
|
1322
|
+
|
|
1121
1323
|
/**
|
|
1122
1324
|
* Release one handle at the end of a run.
|
|
1123
1325
|
*
|
|
@@ -1248,9 +1450,137 @@ export async function driveAgent(deps, request) {
|
|
|
1248
1450
|
// armed bridge is the answerer behind it. `null` for a run that never had a
|
|
1249
1451
|
// delegated policy installed (the review, an ordinary session).
|
|
1250
1452
|
approval: target.approval ?? null,
|
|
1453
|
+
// The Agent Teams selection this run asked for and what actually happened.
|
|
1454
|
+
// `requested: null` means the caller made no choice (the run keeps whatever the
|
|
1455
|
+
// Session already had); `applied: true` is only claimed after the native service
|
|
1456
|
+
// confirmed the Session's saved selection. `refuseReason` is INTERNAL (the runner
|
|
1457
|
+
// branches on it before the prompt) and never leaves the run.
|
|
1458
|
+
team: state.team === null || state.team === undefined
|
|
1459
|
+
? null
|
|
1460
|
+
: {
|
|
1461
|
+
requested: state.team.requested,
|
|
1462
|
+
supported: state.team.supported,
|
|
1463
|
+
applied: state.team.applied,
|
|
1464
|
+
enabled: state.team.enabled,
|
|
1465
|
+
reason: state.team.reason,
|
|
1466
|
+
},
|
|
1251
1467
|
};
|
|
1252
1468
|
}
|
|
1253
1469
|
|
|
1470
|
+
/**
|
|
1471
|
+
* Apply the commander's Agent Teams choice to THIS run's own root Session.
|
|
1472
|
+
*
|
|
1473
|
+
* The switch is DSH's, not this plugin's: `ctx.agentTeams.setEnabled(agent, enabled)`
|
|
1474
|
+
* appends the Session's `team/mode` selection and flushes it — the same call the model
|
|
1475
|
+
* menu's「Use Agent Teams」control makes through `remote.agentTeams.setEnabled`. So:
|
|
1476
|
+
*
|
|
1477
|
+
* * the value is NEVER guessed. `null`/absent means "no choice": the Session keeps
|
|
1478
|
+
* whatever selection it already has and the run reports `requested: null`;
|
|
1479
|
+
* * nothing global is opened. Only this run's own root Session is written, so no
|
|
1480
|
+
* other conversation and no future task inherits it;
|
|
1481
|
+
* * a REQUESTED mode that cannot be applied stops the run BEFORE the prompt (the
|
|
1482
|
+
* caller treats `refuseReason` as terminal); an ABSENT choice never does;
|
|
1483
|
+
* * the wait is BOUNDED by the run itself: it races the caller's own `aborted` latch
|
|
1484
|
+
* (internal deadline or cancellation), so a service that never resolves cannot hold
|
|
1485
|
+
* the run past its budget, and a late settle never reaches the prompt.
|
|
1486
|
+
* @param {object} deps - run dependencies (`policyServices`).
|
|
1487
|
+
* @param {object} agent - the live root Agent this run drives.
|
|
1488
|
+
* @param {boolean | null} requested - the commander's choice, or null.
|
|
1489
|
+
* @param {object} state - the run state carrying the abort latch.
|
|
1490
|
+
* @param {{ aborted: Promise<string> }} hooks - the run's own abort latch promise.
|
|
1491
|
+
* @returns {Promise<object>} `{ requested, supported, applied, enabled, reason, refuseReason }`
|
|
1492
|
+
* where `refuseReason` is non-null exactly when the run must NOT proceed.
|
|
1493
|
+
*/
|
|
1494
|
+
async function applyTeamSelection(deps, agent, requested, state, hooks = {}) {
|
|
1495
|
+
const base = {
|
|
1496
|
+
requested: requested === true ? true : requested === false ? false : null,
|
|
1497
|
+
supported: false,
|
|
1498
|
+
applied: false,
|
|
1499
|
+
enabled: null,
|
|
1500
|
+
reason: null,
|
|
1501
|
+
refuseReason: null,
|
|
1502
|
+
};
|
|
1503
|
+
// An ABSENT choice is fully compatible: nothing is written and the run proceeds with
|
|
1504
|
+
// the Session's own selection.
|
|
1505
|
+
if (base.requested === null) return { ...base, reason: 'not-requested' };
|
|
1506
|
+
const refuse = (reason, extra = {}) => ({
|
|
1507
|
+
...base,
|
|
1508
|
+
...extra,
|
|
1509
|
+
// The same word in both fields: `reason` is what a reader displays, `refuseReason`
|
|
1510
|
+
// is what the runner branches on. Keeping them identical avoids a record that says
|
|
1511
|
+
// "unavailable" in one place and nothing in the other.
|
|
1512
|
+
reason,
|
|
1513
|
+
refuseReason: reason,
|
|
1514
|
+
});
|
|
1515
|
+
let service = null;
|
|
1516
|
+
try {
|
|
1517
|
+
const services = typeof deps?.policyServices === 'function' ? deps.policyServices() : null;
|
|
1518
|
+
service = services?.agentTeams ?? null;
|
|
1519
|
+
} catch {
|
|
1520
|
+
service = null;
|
|
1521
|
+
}
|
|
1522
|
+
if (service === null || typeof service.setEnabled !== 'function') {
|
|
1523
|
+
return refuse('agent-teams-unavailable');
|
|
1524
|
+
}
|
|
1525
|
+
if (state.abortRequested === true) return refuse('agent-teams-aborted');
|
|
1526
|
+
// The real service accepts an AbortSignal. Both the caller's signal and the run's own
|
|
1527
|
+
// latch (internal deadline, disposal) abort it, so a slow persist is told to stop.
|
|
1528
|
+
const controller = new AbortController();
|
|
1529
|
+
const onAbort = () => { try { controller.abort(); } catch { /* idempotent */ } };
|
|
1530
|
+
const signal = state.signal;
|
|
1531
|
+
if (signal !== null && signal !== undefined) signal.addEventListener?.('abort', onAbort, { once: true });
|
|
1532
|
+
const latchPromise = hooks?.aborted;
|
|
1533
|
+
const latched = typeof latchPromise?.then === 'function' ? latchPromise.then(() => 'latched') : null;
|
|
1534
|
+
// NEVER leave a rejection unhandled: a late reject from a losing timer is swallowed
|
|
1535
|
+
// here, and the winner is decided by which of the two settles first.
|
|
1536
|
+
const settled = Promise.resolve()
|
|
1537
|
+
.then(() => service.setEnabled(agent, base.requested, controller.signal))
|
|
1538
|
+
.then(() => ({ ok: true }), (error) => ({ ok: false, error }));
|
|
1539
|
+
try {
|
|
1540
|
+
const winner = latched === null
|
|
1541
|
+
? await settled
|
|
1542
|
+
: await Promise.race([settled, latched]);
|
|
1543
|
+
if (winner === 'latched') {
|
|
1544
|
+
// The run's own deadline or cancellation ended while the write was outstanding:
|
|
1545
|
+
// the native call is told to abort and this run must not continue to the prompt.
|
|
1546
|
+
onAbort();
|
|
1547
|
+
// Keep the abandoned promise observed so a late settle cannot become an
|
|
1548
|
+
// unhandled rejection; its result is ignored on purpose.
|
|
1549
|
+
void Promise.resolve(settled).catch(() => {});
|
|
1550
|
+
return refuse('agent-teams-aborted');
|
|
1551
|
+
}
|
|
1552
|
+
if (winner.ok === false) {
|
|
1553
|
+
if (state.abortRequested === true) return refuse('agent-teams-aborted');
|
|
1554
|
+
return refuse(`agent-teams-failed: ${diagnostic(winner.error?.message ?? String(winner.error), 200) ?? 'error'}`, {
|
|
1555
|
+
supported: true,
|
|
1556
|
+
});
|
|
1557
|
+
}
|
|
1558
|
+
} finally {
|
|
1559
|
+
signal?.removeEventListener?.('abort', onAbort);
|
|
1560
|
+
}
|
|
1561
|
+
// A run cancelled while the selection was being written must not continue either: the
|
|
1562
|
+
// caller already asked for it to stop, and the prompt must not be delivered late.
|
|
1563
|
+
if (state.abortRequested === true) return refuse('agent-teams-aborted');
|
|
1564
|
+
// Read back from the SERVICE, not from what we just asked for: `isEnabled` resolves
|
|
1565
|
+
// the runtime root and returns the saved projection, so this is the applied fact.
|
|
1566
|
+
let enabled = null;
|
|
1567
|
+
try {
|
|
1568
|
+
enabled = typeof service.isEnabled === 'function' ? service.isEnabled(agent) === true : null;
|
|
1569
|
+
} catch {
|
|
1570
|
+
enabled = null;
|
|
1571
|
+
}
|
|
1572
|
+
if (enabled === null) {
|
|
1573
|
+
return refuse('agent-teams-unconfirmed', { supported: true });
|
|
1574
|
+
}
|
|
1575
|
+
if (enabled !== base.requested) {
|
|
1576
|
+
return refuse(`agent-teams-mismatch: requested ${String(base.requested)}, session reports ${String(enabled)}`, {
|
|
1577
|
+
supported: true,
|
|
1578
|
+
enabled,
|
|
1579
|
+
});
|
|
1580
|
+
}
|
|
1581
|
+
return { ...base, supported: true, applied: true, enabled, reason: null };
|
|
1582
|
+
}
|
|
1583
|
+
|
|
1254
1584
|
/**
|
|
1255
1585
|
* Classify the turn the agent loop actually finished.
|
|
1256
1586
|
*
|
|
@@ -198,12 +198,24 @@ export class PermissionError extends Error {
|
|
|
198
198
|
/**
|
|
199
199
|
* Bounds on what one request keeps, so a hostile argument blob cannot become a
|
|
200
200
|
* memory or history problem. Every bound is applied to a REDACTED value.
|
|
201
|
+
*
|
|
202
|
+
* The two text caps are set from the EVIDENCE, not from a round number. An allow is
|
|
203
|
+
* only possible on a complete operation, so any cap that fires turns a legitimate
|
|
204
|
+
* one-line request into an unapprovable one. A Windows absolute path plus a
|
|
205
|
+
* read-back command is ~400 characters on its own — that size was refused by an
|
|
206
|
+
* earlier `argumentValueChars: 400`, and a real platform refusal was the result. The
|
|
207
|
+
* caps below hold roughly an 8 KiB command field and a 16 KiB argument object: far
|
|
208
|
+
* more than any single tool call this surface reviews, and still a hard, stated
|
|
209
|
+
* limit — a value past it is `truncated`, `argumentsComplete: false`, and cannot be
|
|
210
|
+
* allowed (see {@link PermissionBridge#allowRefusalReason}). The protection the
|
|
211
|
+
* caps provide is the same as before; only the point where a normal request stops
|
|
212
|
+
* fitting moved.
|
|
201
213
|
*/
|
|
202
214
|
export const PERMISSION_BOUNDS = Object.freeze({
|
|
203
215
|
/** Maximum characters retained for the redacted JSON arguments. */
|
|
204
|
-
argumentsChars:
|
|
216
|
+
argumentsChars: 16_384,
|
|
205
217
|
/** Maximum characters retained for any single redacted argument scalar. */
|
|
206
|
-
argumentValueChars:
|
|
218
|
+
argumentValueChars: 8_192,
|
|
207
219
|
/** Maximum depth retained when projecting arguments. */
|
|
208
220
|
argumentDepth: 6,
|
|
209
221
|
/** Maximum argument keys retained per object. */
|
|
@@ -593,6 +605,62 @@ export function openTurnAt(session, fromSeq) {
|
|
|
593
605
|
return null;
|
|
594
606
|
}
|
|
595
607
|
|
|
608
|
+
/**
|
|
609
|
+
* The text sent back into the worker after its call was rejected.
|
|
610
|
+
*
|
|
611
|
+
* It is written for the MODEL, not for a log, and every sentence is load-bearing:
|
|
612
|
+
*
|
|
613
|
+
* * the decision is final — the call was rejected and stays rejected;
|
|
614
|
+
* * nothing was widened — not the sandbox, not the grant, not the mode;
|
|
615
|
+
* * the note is not an authorization — a later attempt needs its own approval;
|
|
616
|
+
* * what to do: rewrite the operation to fit the stated reason and ask again.
|
|
617
|
+
*
|
|
618
|
+
* The reason is already redacted and bounded by the caller, so this function only
|
|
619
|
+
* composes. The operation is named by tool and call id so the worker can tell WHICH
|
|
620
|
+
* call was refused when several are in flight.
|
|
621
|
+
* @param {object} pending - the settled request.
|
|
622
|
+
* @param {string} reason - the commander's reason, redacted and bounded.
|
|
623
|
+
* @returns {string} the message body.
|
|
624
|
+
*/
|
|
625
|
+
export function rejectionFeedbackText(pending, reason) {
|
|
626
|
+
const tool = typeof pending?.toolName === 'string' && pending.toolName !== '' ? pending.toolName : 'the requested tool call';
|
|
627
|
+
const callId = typeof pending?.callId === 'string' && pending.callId !== '' ? pending.callId : 'unknown';
|
|
628
|
+
const requestId = typeof pending?.requestId === 'string' && pending.requestId !== '' ? pending.requestId : 'unknown';
|
|
629
|
+
const taskId = typeof pending?.taskId === 'string' && pending.taskId !== '' ? pending.taskId : 'unknown';
|
|
630
|
+
const stated = typeof reason === 'string' && reason.trim() !== ''
|
|
631
|
+
? reason.trim()
|
|
632
|
+
: 'no reason was given';
|
|
633
|
+
return [
|
|
634
|
+
`The commander REJECTED your ${tool} call (task ${taskId}, request ${requestId}, call id ${callId}).`,
|
|
635
|
+
`Reason for rejecting this one operation: ${stated}`,
|
|
636
|
+
'The rejection stands: this call was not approved and will not run.',
|
|
637
|
+
'Nothing was granted or widened. Sandbox permissions, the delegation switch, the session policy and your standing sandbox mode are unchanged.',
|
|
638
|
+
'This note is NOT a new authorization and grants nothing by itself. If you still need the operation, change it to fit the reason above and request approval again; the new request will be decided on its own.',
|
|
639
|
+
].join(' ');
|
|
640
|
+
}
|
|
641
|
+
|
|
642
|
+
/**
|
|
643
|
+
* The message shape an Agent's inbox accepts, built without the native LLM package.
|
|
644
|
+
*
|
|
645
|
+
* `agent.inject()` takes a user message object; the native helper is `createUserMessage`
|
|
646
|
+
* from `@deepseek-ai/dsh-llm`, which this module deliberately does not import (the stdio
|
|
647
|
+
* executable links it). This default therefore produces the same minimum shape, and the
|
|
648
|
+
* plugin's own composition injects the REAL factory so production never depends on it.
|
|
649
|
+
*
|
|
650
|
+
* The source kind is `'user-approval'` — the value the native approval service already
|
|
651
|
+
* uses for its own policy notices — so the message is a recognised producer's message, not
|
|
652
|
+
* an invented source. Nothing here claims to be a user instruction.
|
|
653
|
+
* @param {string} text - message body.
|
|
654
|
+
* @returns {{ role: 'user', content: { type: 'text', text: string }[], source: { kind: string } }} message.
|
|
655
|
+
*/
|
|
656
|
+
export function defaultUserMessage(text) {
|
|
657
|
+
return {
|
|
658
|
+
role: 'user',
|
|
659
|
+
content: [{ type: 'text', text }],
|
|
660
|
+
source: { kind: 'user-approval' },
|
|
661
|
+
};
|
|
662
|
+
}
|
|
663
|
+
|
|
596
664
|
/**
|
|
597
665
|
* The permission bridge for ONE delegated run.
|
|
598
666
|
*
|
|
@@ -648,6 +716,16 @@ export class PermissionBridge {
|
|
|
648
716
|
// `this.session` so a replaced `agent.session` is detectable rather than silently
|
|
649
717
|
// followed (see {@link liveBindingRefusal}).
|
|
650
718
|
this.boundSessionId = null;
|
|
719
|
+
// Request id -> the id of the message that carried its rejection reason. One entry
|
|
720
|
+
// per delivered note, so a repeated reject, a retried decision or a restart cannot
|
|
721
|
+
// deliver the same feedback twice.
|
|
722
|
+
this.feedbackSent = new Map();
|
|
723
|
+
// The message factory. Injected so this module stays free of a hard dependency on the
|
|
724
|
+
// native LLM package (the stdio executable imports this file); the plugin passes the
|
|
725
|
+
// real `createUserMessage`, and the default below builds the same shape.
|
|
726
|
+
this.createMessage = typeof options.createUserMessage === 'function'
|
|
727
|
+
? options.createUserMessage
|
|
728
|
+
: defaultUserMessage;
|
|
651
729
|
}
|
|
652
730
|
|
|
653
731
|
/** Maximum settled requests retained on one run. */
|
|
@@ -1231,6 +1309,19 @@ export class PermissionBridge {
|
|
|
1231
1309
|
}
|
|
1232
1310
|
this.history.push(this.publicView(pending));
|
|
1233
1311
|
if (this.history.length > PermissionBridge.HISTORY_BOUND) this.history.shift();
|
|
1312
|
+
// The reason is delivered to the worker BEFORE the paused tool call is released, so
|
|
1313
|
+
// the note is already in the Agent's inbox by the time the model can take its next
|
|
1314
|
+
// step. It is delivered only for a REJECT: an allow needs no explanation, and a
|
|
1315
|
+
// cancellation is not a decision about the operation.
|
|
1316
|
+
let feedback = null;
|
|
1317
|
+
if (pending.settledOutcome === 'rejected' && pending.decidedBy === 'commander') {
|
|
1318
|
+
feedback = this.deliverRejectionFeedback(pending, pending.decisionReason);
|
|
1319
|
+
// Recorded on the settled view as a verified fact, so "the worker was told why" is
|
|
1320
|
+
// never claimed merely because a reason was stored.
|
|
1321
|
+
const last = this.history.at(-1);
|
|
1322
|
+
if (last !== null && last !== undefined) last.rejectionFeedback = feedback;
|
|
1323
|
+
pending.rejectionFeedback = feedback;
|
|
1324
|
+
}
|
|
1234
1325
|
const resolvePromise = pending.resolve;
|
|
1235
1326
|
pending.resolve = null;
|
|
1236
1327
|
if (typeof resolvePromise === 'function') resolvePromise(pending.settledOutcome);
|
|
@@ -1243,6 +1334,105 @@ export class PermissionBridge {
|
|
|
1243
1334
|
return true;
|
|
1244
1335
|
}
|
|
1245
1336
|
|
|
1337
|
+
/**
|
|
1338
|
+
* Send the commander's REJECTION REASON back into the same Agent.
|
|
1339
|
+
*
|
|
1340
|
+
* The native approval protocol cannot carry prose: `ApprovalService.request()`
|
|
1341
|
+
* resolves one of four closed outcomes, so the reason a commander typed never reached
|
|
1342
|
+
* the worker — it was recorded in history and the tool received a bare `rejected`.
|
|
1343
|
+
* The acceptance report is exactly that: the worker could not know it should narrow
|
|
1344
|
+
* its request and ask again.
|
|
1345
|
+
*
|
|
1346
|
+
* The real channel for prose is the Agent's own inbox. The native approval service
|
|
1347
|
+
* itself uses it for its policy notices
|
|
1348
|
+
* (`agent.inject(createUserMessage({ content, source }))`), and `dsh-user-questions`
|
|
1349
|
+
* uses the same call to steer a late answer. So this method injects ONE user message
|
|
1350
|
+
* describing the rejection.
|
|
1351
|
+
*
|
|
1352
|
+
* What the message says, and what it must never be read as:
|
|
1353
|
+
*
|
|
1354
|
+
* * WHY this one call was rejected, quoted from the decision;
|
|
1355
|
+
* * that the rejection STANDS — the call is not approved, and nothing was widened;
|
|
1356
|
+
* * that this is NOT a new authorization and grants nothing by itself;
|
|
1357
|
+
* * what to do next: rewrite the operation to fit and request approval again.
|
|
1358
|
+
*
|
|
1359
|
+
* The reason is redacted and bounded before it is sent, by the same rules the stored
|
|
1360
|
+
* evidence uses. A send failure is returned as a bounded outcome and NEVER changes the
|
|
1361
|
+
* settlement: `rejected` already happened, and reporting a failed notice as success,
|
|
1362
|
+
* or un-rejecting the call because the notice failed, would both be lies.
|
|
1363
|
+
*
|
|
1364
|
+
* Delivery is tracked per request id so a repeated reject, a retried decision or a
|
|
1365
|
+
* plugin restart cannot deliver the same feedback twice. Only an ACTUAL injection on
|
|
1366
|
+
* the bound Agent counts as delivered; anything else stays `not-sent` and is reported.
|
|
1367
|
+
* @param {object} pending - the request that was just settled.
|
|
1368
|
+
* @param {string} reason - the commander's decision reason (already redacted and bounded).
|
|
1369
|
+
* @returns {{ delivered: boolean, requestId: string, callId: string | null,
|
|
1370
|
+
* messageId: string | null, reason: string }} the outcome, never a rejection.
|
|
1371
|
+
*/
|
|
1372
|
+
deliverRejectionFeedback(pending, reason) {
|
|
1373
|
+
const requestId = typeof pending?.requestId === 'string' ? pending.requestId : '';
|
|
1374
|
+
// One delivery per request, ever. A duplicate reject and a restart both land here.
|
|
1375
|
+
if (this.feedbackSent.has(requestId)) {
|
|
1376
|
+
return {
|
|
1377
|
+
delivered: false,
|
|
1378
|
+
requestId,
|
|
1379
|
+
callId: typeof pending?.callId === 'string' ? pending.callId : null,
|
|
1380
|
+
messageId: this.feedbackSent.get(requestId),
|
|
1381
|
+
reason: 'already-delivered',
|
|
1382
|
+
};
|
|
1383
|
+
}
|
|
1384
|
+
const agent = this.agent;
|
|
1385
|
+
const sessionRefusal = this.liveBindingRefusal();
|
|
1386
|
+
if (agent === null || typeof agent?.inject !== 'function' || sessionRefusal !== null) {
|
|
1387
|
+
return {
|
|
1388
|
+
delivered: false,
|
|
1389
|
+
requestId,
|
|
1390
|
+
callId: typeof pending?.callId === 'string' ? pending.callId : null,
|
|
1391
|
+
messageId: null,
|
|
1392
|
+
reason: sessionRefusal ?? 'no-injectable-agent',
|
|
1393
|
+
};
|
|
1394
|
+
}
|
|
1395
|
+
const text = rejectionFeedbackText(pending, reason);
|
|
1396
|
+
let message;
|
|
1397
|
+
try {
|
|
1398
|
+
message = this.createMessage(text);
|
|
1399
|
+
} catch (error) {
|
|
1400
|
+
return {
|
|
1401
|
+
delivered: false,
|
|
1402
|
+
requestId,
|
|
1403
|
+
callId: typeof pending?.callId === 'string' ? pending.callId : null,
|
|
1404
|
+
messageId: null,
|
|
1405
|
+
reason: typeof error?.code === 'string' ? error.code : 'message-unavailable',
|
|
1406
|
+
};
|
|
1407
|
+
}
|
|
1408
|
+
try {
|
|
1409
|
+
agent.inject(message);
|
|
1410
|
+
} catch (error) {
|
|
1411
|
+
// The rejection is already in force. This only reports that the NOTE did not reach
|
|
1412
|
+
// the worker, which is a different, smaller fact than the decision itself.
|
|
1413
|
+
this.logger?.warn?.('dsh-delegate: the rejection reason could not be delivered', String(error?.code ?? ''));
|
|
1414
|
+
return {
|
|
1415
|
+
delivered: false,
|
|
1416
|
+
requestId,
|
|
1417
|
+
callId: typeof pending?.callId === 'string' ? pending.callId : null,
|
|
1418
|
+
messageId: null,
|
|
1419
|
+
reason: 'inject-failed',
|
|
1420
|
+
};
|
|
1421
|
+
}
|
|
1422
|
+
const messageId = typeof message?.id === 'string' ? message.id : null;
|
|
1423
|
+
this.feedbackSent.set(requestId, messageId);
|
|
1424
|
+
return {
|
|
1425
|
+
delivered: true,
|
|
1426
|
+
requestId,
|
|
1427
|
+
callId: typeof pending?.callId === 'string' ? pending.callId : null,
|
|
1428
|
+
messageId,
|
|
1429
|
+
reason: 'delivered',
|
|
1430
|
+
// The record states what was said, so "feedback happened" is auditable rather than
|
|
1431
|
+
// claimed: the exact text is the evidence.
|
|
1432
|
+
text,
|
|
1433
|
+
};
|
|
1434
|
+
}
|
|
1435
|
+
|
|
1246
1436
|
/**
|
|
1247
1437
|
* Clear the pending slot of an answered request.
|
|
1248
1438
|
* @param {object} pending - pending record.
|