dsh-taskbridge 1.8.0 → 1.8.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 +12 -0
- package/README.md +616 -619
- package/antigravity/agents/dsh-taskbridge.md +12 -11
- package/antigravity/skills/dsh-taskbridge/SKILL.md +15 -15
- package/codex/dsh-taskbridge.toml +22 -11
- package/docs/INTRODUCTION.md +18 -15
- package/docs/INTRODUCTION.zh.md +19 -19
- package/docs/codex-integration.md +2 -2
- package/docs/conversation-placement.md +7 -6
- package/docs/releases/1.8.1.zh.md +24 -0
- package/docs/runtime-compatibility.md +1 -1
- package/docs/taskbridge-upgrade.md +1 -1
- package/lib/client.d.ts +72 -1
- package/lib/client.js +7 -7
- package/locale/en.json +1 -1
- package/locale/zh.json +1 -1
- package/package.json +4 -3
- package/src/antigravity/installer.js +23 -1
- package/src/client/api.js +123 -3
- package/src/client/index.jsx +111 -8
- package/src/client/task-cleanup.js +130 -5
- package/src/codex/installer.js +3 -0
- package/src/config/defaults.js +11 -15
- package/src/http/user-plane.js +11 -1
- package/src/jobs/engine.js +1 -0
- package/src/jobs/history.js +1 -1
- package/src/jobs/plan-store.js +83 -0
- package/src/jobs/record-cleanup.js +452 -7
- package/src/mcp/server.js +30 -26
- package/src/mcp/wait.js +34 -15
- package/src/runtime/agent-runner.js +1 -0
- package/src/runtime/session-placement.js +36 -1
- package/src/runtime/worker-prompt.js +2 -1
- package/src/workspace/worktrees.js +155 -1
|
@@ -1,8 +1,11 @@
|
|
|
1
1
|
import { buildTaskTree } from './task-tree.js';
|
|
2
2
|
import { HISTORY_CODES, HISTORY_DELETE_MAX_TASKS } from './history.js';
|
|
3
|
+
import { sessionPathKey } from '../runtime/session-placement.js';
|
|
3
4
|
|
|
4
5
|
export const MAX_CLEANUP_CHAINS = 100;
|
|
5
|
-
const FINISHED = new Set(['awaiting-host-review', 'failed', 'cancelled']);
|
|
6
|
+
const FINISHED = new Set(['awaiting-host-review', 'failed', 'cancelled', 'interrupted']);
|
|
7
|
+
const ALLOWED_CLEANUP_KEYS = new Set(['chains', 'confirm', 'archiveSessions', 'removeWorktrees', 'cleanPlans']);
|
|
8
|
+
const ALLOWED_ORPHAN_KEYS = new Set(['confirm', 'archiveSessions', 'removeWorktrees']);
|
|
6
9
|
|
|
7
10
|
export const CLEANUP_CODES = Object.freeze({
|
|
8
11
|
invalid: 'CLEANUP_INVALID',
|
|
@@ -40,10 +43,15 @@ function isId(value) {
|
|
|
40
43
|
}
|
|
41
44
|
|
|
42
45
|
export function validateCleanupRequest(body) {
|
|
43
|
-
if (!isObject(body) || Object.keys(body).some((key) => key
|
|
46
|
+
if (!isObject(body) || Object.keys(body).some((key) => !ALLOWED_CLEANUP_KEYS.has(key))) {
|
|
44
47
|
invalid('expected only chains and explicit confirmation');
|
|
45
48
|
}
|
|
46
49
|
if (body.confirm !== true) invalid('cleanup requires explicit confirmation', { field: 'confirm' });
|
|
50
|
+
for (const flag of ['archiveSessions', 'removeWorktrees', 'cleanPlans']) {
|
|
51
|
+
if (body[flag] !== undefined && typeof body[flag] !== 'boolean') {
|
|
52
|
+
invalid(`${flag} must be a boolean`, { field: flag });
|
|
53
|
+
}
|
|
54
|
+
}
|
|
47
55
|
if (!Array.isArray(body.chains) || body.chains.length === 0 || body.chains.length > MAX_CLEANUP_CHAINS) {
|
|
48
56
|
invalid('select a bounded list of execution chains', { field: 'chains', limit: MAX_CLEANUP_CHAINS });
|
|
49
57
|
}
|
|
@@ -63,7 +71,14 @@ export function validateCleanupRequest(body) {
|
|
|
63
71
|
}
|
|
64
72
|
return { chainId: chain.chainId, taskIds: [...chain.taskIds] };
|
|
65
73
|
});
|
|
66
|
-
return {
|
|
74
|
+
return {
|
|
75
|
+
chains,
|
|
76
|
+
options: {
|
|
77
|
+
archiveSessions: body.archiveSessions === true,
|
|
78
|
+
removeWorktrees: body.removeWorktrees === true,
|
|
79
|
+
cleanPlans: body.cleanPlans === true,
|
|
80
|
+
},
|
|
81
|
+
};
|
|
67
82
|
}
|
|
68
83
|
|
|
69
84
|
function planStore(deps) {
|
|
@@ -152,7 +167,7 @@ function checkBatch(chains, context) {
|
|
|
152
167
|
}
|
|
153
168
|
|
|
154
169
|
export async function runCleanup(deps, body) {
|
|
155
|
-
const { chains } = validateCleanupRequest(body);
|
|
170
|
+
const { chains, options } = validateCleanupRequest(body);
|
|
156
171
|
const store = deps.history;
|
|
157
172
|
const plans = planStore(deps);
|
|
158
173
|
const engine = deps.engine;
|
|
@@ -160,6 +175,9 @@ export async function runCleanup(deps, body) {
|
|
|
160
175
|
throw new CleanupError(CLEANUP_CODES.unavailable, 'task record cleanup is unavailable');
|
|
161
176
|
}
|
|
162
177
|
const ids = chains.flatMap((chain) => chain.taskIds);
|
|
178
|
+
const idsSet = new Set(ids);
|
|
179
|
+
let deletedRowsSnapshot = [];
|
|
180
|
+
let remainingRowsSnapshot = [];
|
|
163
181
|
const deleteRecords = async (planNodes) => {
|
|
164
182
|
let release;
|
|
165
183
|
try {
|
|
@@ -167,7 +185,11 @@ export async function runCleanup(deps, body) {
|
|
|
167
185
|
beforeWrite(archived) {
|
|
168
186
|
// Read the latest engine state inside the history queue. No asynchronous
|
|
169
187
|
// work may run between checking relationships and reserving these ids.
|
|
170
|
-
|
|
188
|
+
const context = cleanupContext(engine, archived, planNodes);
|
|
189
|
+
checkBatch(chains, context);
|
|
190
|
+
const allTasks = new Map([...context.archivedById, ...context.liveById]);
|
|
191
|
+
deletedRowsSnapshot = ids.map((id) => allTasks.get(id)).filter(Boolean);
|
|
192
|
+
remainingRowsSnapshot = [...allTasks.values()].filter((row) => !idsSet.has(row.taskId));
|
|
171
193
|
release = engine?.reserveCleanupIds(ids);
|
|
172
194
|
},
|
|
173
195
|
afterWrite(deletedIds) {
|
|
@@ -187,10 +209,433 @@ export async function runCleanup(deps, body) {
|
|
|
187
209
|
: error?.code === HISTORY_CODES.writeFailed ? CLEANUP_CODES.failed : CLEANUP_CODES.unavailable;
|
|
188
210
|
throw new CleanupError(code, 'the task records could not be removed', { deleted: 0 });
|
|
189
211
|
}
|
|
212
|
+
|
|
213
|
+
const hasLinkedCleanup = options.archiveSessions || options.removeWorktrees || options.cleanPlans;
|
|
214
|
+
if (!hasLinkedCleanup) {
|
|
215
|
+
return {
|
|
216
|
+
chains: chains.length,
|
|
217
|
+
tasks: result.deletedTaskIds.length,
|
|
218
|
+
...result,
|
|
219
|
+
retained: { sessions: true, workspaces: true, worktrees: true, sourceFiles: true, plans: true },
|
|
220
|
+
};
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
const linked = await performLinkedCleanup(deps, {
|
|
224
|
+
options,
|
|
225
|
+
deletedTaskIds: result.deletedTaskIds,
|
|
226
|
+
deletedRows: deletedRowsSnapshot,
|
|
227
|
+
remainingRows: remainingRowsSnapshot,
|
|
228
|
+
plans,
|
|
229
|
+
});
|
|
230
|
+
|
|
190
231
|
return {
|
|
191
232
|
chains: chains.length,
|
|
192
233
|
tasks: result.deletedTaskIds.length,
|
|
193
234
|
...result,
|
|
194
|
-
retained: {
|
|
235
|
+
retained: {
|
|
236
|
+
sessions: !options.archiveSessions,
|
|
237
|
+
workspaces: !options.removeWorktrees,
|
|
238
|
+
worktrees: !options.removeWorktrees,
|
|
239
|
+
sourceFiles: true,
|
|
240
|
+
plans: !options.cleanPlans,
|
|
241
|
+
},
|
|
242
|
+
cleaned: linked,
|
|
195
243
|
};
|
|
196
|
-
}
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
function firstNonEmpty(...values) {
|
|
247
|
+
for (const val of values) {
|
|
248
|
+
if (typeof val === 'string' && val.trim() !== '') return val.trim();
|
|
249
|
+
}
|
|
250
|
+
return null;
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
function extractTaskFacts(row, sessionRow = null) {
|
|
254
|
+
const isolation = firstNonEmpty(
|
|
255
|
+
row?.isolation,
|
|
256
|
+
row?.effective?.worker?.isolation,
|
|
257
|
+
sessionRow?.isolation,
|
|
258
|
+
);
|
|
259
|
+
const sourcePath = firstNonEmpty(
|
|
260
|
+
sessionRow?.sourcePath,
|
|
261
|
+
row?.sourcePath,
|
|
262
|
+
row?.execution?.sourcePath,
|
|
263
|
+
row?.conversation?.sourcePath,
|
|
264
|
+
);
|
|
265
|
+
const executionPath = firstNonEmpty(
|
|
266
|
+
sessionRow?.executionPath,
|
|
267
|
+
row?.executionWorkspacePath,
|
|
268
|
+
row?.worktree?.workspacePath,
|
|
269
|
+
row?.execution?.workspacePath,
|
|
270
|
+
row?.workspacePath,
|
|
271
|
+
);
|
|
272
|
+
const workspaceId = firstNonEmpty(
|
|
273
|
+
sessionRow?.workspaceId,
|
|
274
|
+
row?.workspaceId,
|
|
275
|
+
row?.execution?.workspaceId,
|
|
276
|
+
row?.execution?.conversation?.workspaceId,
|
|
277
|
+
row?.conversation?.workspaceId,
|
|
278
|
+
);
|
|
279
|
+
const sessionId = firstNonEmpty(
|
|
280
|
+
sessionRow?.sessionId,
|
|
281
|
+
row?.sessionId,
|
|
282
|
+
row?.execution?.sessionId,
|
|
283
|
+
row?.execution?.conversation?.sessionId,
|
|
284
|
+
row?.conversation?.sessionId,
|
|
285
|
+
);
|
|
286
|
+
return {
|
|
287
|
+
taskId: firstNonEmpty(row?.taskId, sessionRow?.taskId),
|
|
288
|
+
isolation,
|
|
289
|
+
sourcePath,
|
|
290
|
+
executionPath,
|
|
291
|
+
workspaceId,
|
|
292
|
+
sessionId,
|
|
293
|
+
planNodeId: firstNonEmpty(row?.planNodeId),
|
|
294
|
+
parentNodeId: firstNonEmpty(row?.parentNodeId),
|
|
295
|
+
};
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
function resolveController(serviceOrAccessor) {
|
|
299
|
+
if (serviceOrAccessor === null || serviceOrAccessor === undefined) return null;
|
|
300
|
+
if (typeof serviceOrAccessor === 'object' && 'current' in serviceOrAccessor) {
|
|
301
|
+
return serviceOrAccessor.current ?? null;
|
|
302
|
+
}
|
|
303
|
+
return serviceOrAccessor;
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
async function tryArchiveHostSession(deps, sessionId) {
|
|
307
|
+
const ws = resolveController(deps?.workspaces);
|
|
308
|
+
if (typeof ws?.archiveSession === 'function') {
|
|
309
|
+
try {
|
|
310
|
+
await ws.archiveSession(sessionId);
|
|
311
|
+
return true;
|
|
312
|
+
} catch {
|
|
313
|
+
try {
|
|
314
|
+
await ws.archiveSession({ sessionId });
|
|
315
|
+
return true;
|
|
316
|
+
} catch {
|
|
317
|
+
// Ignore host controller error; browser client also reconciles via client workspaces service.
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
const sc = resolveController(deps?.sessionController);
|
|
322
|
+
if (typeof sc?.archiveSession === 'function') {
|
|
323
|
+
try {
|
|
324
|
+
await sc.archiveSession(sessionId);
|
|
325
|
+
return true;
|
|
326
|
+
} catch {
|
|
327
|
+
// Ignore
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
return false;
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
async function tryDeleteHostWorkspace(deps, workspaceId) {
|
|
334
|
+
const ws = resolveController(deps?.workspaces);
|
|
335
|
+
if (typeof ws?.delete === 'function') {
|
|
336
|
+
try {
|
|
337
|
+
await ws.delete(workspaceId);
|
|
338
|
+
return true;
|
|
339
|
+
} catch {
|
|
340
|
+
try {
|
|
341
|
+
await ws.delete({ workspaceId });
|
|
342
|
+
return true;
|
|
343
|
+
} catch {
|
|
344
|
+
// Ignore
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
return false;
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
async function performLinkedCleanup(deps, { options, deletedTaskIds, deletedRows, remainingRows, plans }) {
|
|
352
|
+
const deletedSet = new Set(deletedTaskIds);
|
|
353
|
+
let sessionRows = [];
|
|
354
|
+
if (typeof deps?.sessionStore?.list === 'function') {
|
|
355
|
+
try {
|
|
356
|
+
sessionRows = await deps.sessionStore.list();
|
|
357
|
+
} catch {
|
|
358
|
+
sessionRows = [];
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
const sessionByTask = new Map(sessionRows.map((row) => [row.taskId, row]));
|
|
362
|
+
|
|
363
|
+
const activeSessionIds = new Set();
|
|
364
|
+
const activeWorkspaceIds = new Set();
|
|
365
|
+
const activeExecKeys = new Set();
|
|
366
|
+
for (const row of remainingRows) {
|
|
367
|
+
const facts = extractTaskFacts(row, sessionByTask.get(row.taskId));
|
|
368
|
+
if (facts.sessionId !== null) activeSessionIds.add(facts.sessionId);
|
|
369
|
+
if (facts.workspaceId !== null) activeWorkspaceIds.add(facts.workspaceId);
|
|
370
|
+
const execKey = sessionPathKey(facts.executionPath);
|
|
371
|
+
if (execKey !== null) activeExecKeys.add(execKey);
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
const deletedFacts = deletedTaskIds.map((taskId) => {
|
|
375
|
+
const row = deletedRows.find((item) => item?.taskId === taskId) ?? { taskId };
|
|
376
|
+
return extractTaskFacts(row, sessionByTask.get(taskId));
|
|
377
|
+
});
|
|
378
|
+
|
|
379
|
+
const archivedSessionIds = [];
|
|
380
|
+
const archivedSet = new Set();
|
|
381
|
+
if (options.archiveSessions) {
|
|
382
|
+
for (const facts of deletedFacts) {
|
|
383
|
+
if (facts.sessionId !== null && !activeSessionIds.has(facts.sessionId) && !archivedSet.has(facts.sessionId)) {
|
|
384
|
+
archivedSet.add(facts.sessionId);
|
|
385
|
+
archivedSessionIds.push(facts.sessionId);
|
|
386
|
+
await tryArchiveHostSession(deps, facts.sessionId);
|
|
387
|
+
}
|
|
388
|
+
}
|
|
389
|
+
if (typeof deps?.sessionStore?.forgetMany === 'function') {
|
|
390
|
+
await deps.sessionStore.forgetMany(deletedSet).catch(() => []);
|
|
391
|
+
} else if (typeof deps?.sessionStore?.forget === 'function') {
|
|
392
|
+
for (const id of deletedSet) {
|
|
393
|
+
await deps.sessionStore.forget(id).catch(() => false);
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
const removedWorktreePaths = [];
|
|
399
|
+
const removedWorktreeSet = new Set();
|
|
400
|
+
const removedWorkspaceIds = [];
|
|
401
|
+
const removedWorkspaceSet = new Set();
|
|
402
|
+
|
|
403
|
+
if (options.removeWorktrees) {
|
|
404
|
+
for (const facts of deletedFacts) {
|
|
405
|
+
// Strictly operate ONLY on worktree-isolated tasks; never touch shared directories or shared workspaces.
|
|
406
|
+
if (facts.isolation !== 'worktree') continue;
|
|
407
|
+
const execKey = sessionPathKey(facts.executionPath);
|
|
408
|
+
const sourceKey = sessionPathKey(facts.sourcePath);
|
|
409
|
+
if (execKey === null || execKey === sourceKey || activeExecKeys.has(execKey)) continue;
|
|
410
|
+
|
|
411
|
+
let worktreeEligibleForWorkspaceRemoval = false;
|
|
412
|
+
if (typeof deps?.worktrees?.removeManagedTree === 'function') {
|
|
413
|
+
if (!removedWorktreeSet.has(execKey)) {
|
|
414
|
+
const outcome = await deps.worktrees.removeManagedTree({
|
|
415
|
+
taskId: facts.taskId,
|
|
416
|
+
sourcePath: facts.sourcePath,
|
|
417
|
+
workspacePath: facts.executionPath,
|
|
418
|
+
});
|
|
419
|
+
if (outcome.removed && outcome.workspacePath) {
|
|
420
|
+
removedWorktreeSet.add(execKey);
|
|
421
|
+
removedWorktreePaths.push(outcome.workspacePath);
|
|
422
|
+
worktreeEligibleForWorkspaceRemoval = true;
|
|
423
|
+
} else if (outcome.reason === 'already-absent') {
|
|
424
|
+
removedWorktreeSet.add(execKey);
|
|
425
|
+
worktreeEligibleForWorkspaceRemoval = true;
|
|
426
|
+
}
|
|
427
|
+
} else {
|
|
428
|
+
worktreeEligibleForWorkspaceRemoval = true;
|
|
429
|
+
}
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
if (
|
|
433
|
+
worktreeEligibleForWorkspaceRemoval
|
|
434
|
+
&& facts.workspaceId !== null
|
|
435
|
+
&& !activeWorkspaceIds.has(facts.workspaceId)
|
|
436
|
+
&& !removedWorkspaceSet.has(facts.workspaceId)
|
|
437
|
+
) {
|
|
438
|
+
removedWorkspaceSet.add(facts.workspaceId);
|
|
439
|
+
removedWorkspaceIds.push(facts.workspaceId);
|
|
440
|
+
await tryDeleteHostWorkspace(deps, facts.workspaceId);
|
|
441
|
+
}
|
|
442
|
+
}
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
let removedPlanNodeIds = [];
|
|
446
|
+
if (options.cleanPlans && typeof plans?.deleteEmptyAncestors === 'function') {
|
|
447
|
+
const seedPlanIds = new Set();
|
|
448
|
+
for (const facts of deletedFacts) {
|
|
449
|
+
if (facts.planNodeId !== null) seedPlanIds.add(facts.planNodeId);
|
|
450
|
+
if (facts.parentNodeId !== null && facts.parentNodeId.startsWith('plan-')) {
|
|
451
|
+
seedPlanIds.add(facts.parentNodeId);
|
|
452
|
+
}
|
|
453
|
+
}
|
|
454
|
+
if (seedPlanIds.size > 0) {
|
|
455
|
+
try {
|
|
456
|
+
removedPlanNodeIds = await plans.deleteEmptyAncestors(seedPlanIds, remainingRows);
|
|
457
|
+
} catch {
|
|
458
|
+
removedPlanNodeIds = [];
|
|
459
|
+
}
|
|
460
|
+
}
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
return {
|
|
464
|
+
archivedSessionIds,
|
|
465
|
+
removedWorktreePaths,
|
|
466
|
+
removedWorkspaceIds,
|
|
467
|
+
removedPlanNodeIds,
|
|
468
|
+
};
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
export function validateOrphanCleanupRequest(body) {
|
|
472
|
+
if (!isObject(body) || Object.keys(body).some((key) => !ALLOWED_ORPHAN_KEYS.has(key))) {
|
|
473
|
+
invalid('expected explicit confirmation for orphan cleanup');
|
|
474
|
+
}
|
|
475
|
+
if (body.confirm !== true) invalid('orphan cleanup requires explicit confirmation', { field: 'confirm' });
|
|
476
|
+
for (const flag of ['archiveSessions', 'removeWorktrees']) {
|
|
477
|
+
if (body[flag] !== undefined && typeof body[flag] !== 'boolean') {
|
|
478
|
+
invalid(`${flag} must be a boolean`, { field: flag });
|
|
479
|
+
}
|
|
480
|
+
}
|
|
481
|
+
return {
|
|
482
|
+
archiveSessions: body.archiveSessions !== false,
|
|
483
|
+
removeWorktrees: body.removeWorktrees !== false,
|
|
484
|
+
};
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
/**
|
|
488
|
+
* Sweep residual worktrees and dispatched session identities whose task records
|
|
489
|
+
* were already deleted or rotated out of `history.json`.
|
|
490
|
+
*
|
|
491
|
+
* Strictly preserves any session, workspace or directory still referenced by a live
|
|
492
|
+
* or retained historical task, and never touches a shared checkout or shared project
|
|
493
|
+
* Workspace.
|
|
494
|
+
* @param {object} deps - runtime dependencies.
|
|
495
|
+
* @param {object} body - `{ confirm: true, archiveSessions?: boolean, removeWorktrees?: boolean }`.
|
|
496
|
+
* @returns {Promise<object>} sweep outcome.
|
|
497
|
+
*/
|
|
498
|
+
export async function runOrphanCleanup(deps, body) {
|
|
499
|
+
const options = validateOrphanCleanupRequest(body);
|
|
500
|
+
const store = deps?.history;
|
|
501
|
+
if (typeof store?.load !== 'function') {
|
|
502
|
+
throw new CleanupError(CLEANUP_CODES.unavailable, 'task history is unavailable');
|
|
503
|
+
}
|
|
504
|
+
let archived = [];
|
|
505
|
+
try {
|
|
506
|
+
archived = await store.load();
|
|
507
|
+
} catch {
|
|
508
|
+
throw new CleanupError(CLEANUP_CODES.unavailable, 'task history could not be read');
|
|
509
|
+
}
|
|
510
|
+
const live = deps?.engine?.list?.() ?? [];
|
|
511
|
+
const allExisting = [...archived, ...live];
|
|
512
|
+
const existingTaskIds = new Set(allExisting.map((row) => row?.taskId).filter(Boolean));
|
|
513
|
+
|
|
514
|
+
let sessionRows = [];
|
|
515
|
+
if (typeof deps?.sessionStore?.list === 'function') {
|
|
516
|
+
try {
|
|
517
|
+
sessionRows = await deps.sessionStore.list();
|
|
518
|
+
} catch {
|
|
519
|
+
sessionRows = [];
|
|
520
|
+
}
|
|
521
|
+
}
|
|
522
|
+
const sessionByTask = new Map(sessionRows.map((row) => [row.taskId, row]));
|
|
523
|
+
|
|
524
|
+
const activeSessionIds = new Set();
|
|
525
|
+
const activeWorkspaceIds = new Set();
|
|
526
|
+
const activeExecKeys = new Set();
|
|
527
|
+
const activeExecPaths = [];
|
|
528
|
+
const knownSourcePaths = new Set();
|
|
529
|
+
|
|
530
|
+
for (const row of allExisting) {
|
|
531
|
+
const facts = extractTaskFacts(row, sessionByTask.get(row.taskId));
|
|
532
|
+
if (facts.sessionId !== null) activeSessionIds.add(facts.sessionId);
|
|
533
|
+
if (facts.workspaceId !== null) activeWorkspaceIds.add(facts.workspaceId);
|
|
534
|
+
if (facts.sourcePath !== null) knownSourcePaths.add(facts.sourcePath);
|
|
535
|
+
if (facts.executionPath !== null) {
|
|
536
|
+
activeExecPaths.push(facts.executionPath);
|
|
537
|
+
const key = sessionPathKey(facts.executionPath);
|
|
538
|
+
if (key !== null) activeExecKeys.add(key);
|
|
539
|
+
}
|
|
540
|
+
}
|
|
541
|
+
|
|
542
|
+
const orphanRows = sessionRows.filter((row) => typeof row?.taskId === 'string' && !existingTaskIds.has(row.taskId));
|
|
543
|
+
const orphanTaskIds = orphanRows.map((row) => row.taskId);
|
|
544
|
+
|
|
545
|
+
const archivedSessionIds = [];
|
|
546
|
+
const archivedSet = new Set();
|
|
547
|
+
const removedWorktreePaths = [];
|
|
548
|
+
const removedWorktreeSet = new Set();
|
|
549
|
+
const removedWorkspaceIds = [];
|
|
550
|
+
const removedWorkspaceSet = new Set();
|
|
551
|
+
|
|
552
|
+
for (const row of orphanRows) {
|
|
553
|
+
const facts = extractTaskFacts(null, row);
|
|
554
|
+
if (facts.sourcePath !== null) knownSourcePaths.add(facts.sourcePath);
|
|
555
|
+
|
|
556
|
+
if (options.archiveSessions && facts.sessionId !== null) {
|
|
557
|
+
if (!activeSessionIds.has(facts.sessionId) && !archivedSet.has(facts.sessionId)) {
|
|
558
|
+
archivedSet.add(facts.sessionId);
|
|
559
|
+
archivedSessionIds.push(facts.sessionId);
|
|
560
|
+
await tryArchiveHostSession(deps, facts.sessionId);
|
|
561
|
+
}
|
|
562
|
+
}
|
|
563
|
+
|
|
564
|
+
if (options.removeWorktrees && facts.isolation === 'worktree') {
|
|
565
|
+
const execKey = sessionPathKey(facts.executionPath);
|
|
566
|
+
const sourceKey = sessionPathKey(facts.sourcePath);
|
|
567
|
+
if (execKey !== null && execKey !== sourceKey && !activeExecKeys.has(execKey)) {
|
|
568
|
+
let canRemoveWs = false;
|
|
569
|
+
if (typeof deps?.worktrees?.removeManagedTree === 'function') {
|
|
570
|
+
if (!removedWorktreeSet.has(execKey)) {
|
|
571
|
+
const wt = await deps.worktrees.removeManagedTree({
|
|
572
|
+
taskId: facts.taskId,
|
|
573
|
+
sourcePath: facts.sourcePath,
|
|
574
|
+
workspacePath: facts.executionPath,
|
|
575
|
+
});
|
|
576
|
+
if (wt.removed && wt.workspacePath) {
|
|
577
|
+
removedWorktreeSet.add(execKey);
|
|
578
|
+
removedWorktreePaths.push(wt.workspacePath);
|
|
579
|
+
canRemoveWs = true;
|
|
580
|
+
} else if (wt.reason === 'already-absent') {
|
|
581
|
+
removedWorktreeSet.add(execKey);
|
|
582
|
+
canRemoveWs = true;
|
|
583
|
+
}
|
|
584
|
+
} else {
|
|
585
|
+
canRemoveWs = true;
|
|
586
|
+
}
|
|
587
|
+
}
|
|
588
|
+
if (
|
|
589
|
+
canRemoveWs
|
|
590
|
+
&& facts.workspaceId !== null
|
|
591
|
+
&& !activeWorkspaceIds.has(facts.workspaceId)
|
|
592
|
+
&& !removedWorkspaceSet.has(facts.workspaceId)
|
|
593
|
+
) {
|
|
594
|
+
removedWorkspaceSet.add(facts.workspaceId);
|
|
595
|
+
removedWorkspaceIds.push(facts.workspaceId);
|
|
596
|
+
await tryDeleteHostWorkspace(deps, facts.workspaceId);
|
|
597
|
+
}
|
|
598
|
+
}
|
|
599
|
+
}
|
|
600
|
+
}
|
|
601
|
+
|
|
602
|
+
if (options.archiveSessions && orphanTaskIds.length > 0) {
|
|
603
|
+
if (typeof deps?.sessionStore?.forgetMany === 'function') {
|
|
604
|
+
await deps.sessionStore.forgetMany(orphanTaskIds).catch(() => []);
|
|
605
|
+
} else if (typeof deps?.sessionStore?.forget === 'function') {
|
|
606
|
+
for (const id of orphanTaskIds) {
|
|
607
|
+
await deps.sessionStore.forget(id).catch(() => false);
|
|
608
|
+
}
|
|
609
|
+
}
|
|
610
|
+
}
|
|
611
|
+
|
|
612
|
+
if (options.removeWorktrees && typeof deps?.worktrees?.sweepOrphanTrees === 'function') {
|
|
613
|
+
try {
|
|
614
|
+
const swept = await deps.worktrees.sweepOrphanTrees({
|
|
615
|
+
activePaths: activeExecPaths,
|
|
616
|
+
sourcePaths: [...knownSourcePaths],
|
|
617
|
+
});
|
|
618
|
+
for (const wtPath of Array.isArray(swept?.removedWorktreePaths) ? swept.removedWorktreePaths : []) {
|
|
619
|
+
const key = sessionPathKey(wtPath);
|
|
620
|
+
if (key !== null && !removedWorktreeSet.has(key)) {
|
|
621
|
+
removedWorktreeSet.add(key);
|
|
622
|
+
removedWorktreePaths.push(wtPath);
|
|
623
|
+
}
|
|
624
|
+
}
|
|
625
|
+
} catch {
|
|
626
|
+
// Best-effort disk sweep.
|
|
627
|
+
}
|
|
628
|
+
}
|
|
629
|
+
|
|
630
|
+
return {
|
|
631
|
+
orphanSessionsForgotten: options.archiveSessions ? orphanTaskIds.length : 0,
|
|
632
|
+
cleaned: {
|
|
633
|
+
archivedSessionIds,
|
|
634
|
+
removedWorktreePaths,
|
|
635
|
+
removedWorkspaceIds,
|
|
636
|
+
},
|
|
637
|
+
retained: {
|
|
638
|
+
sourceFiles: true,
|
|
639
|
+
},
|
|
640
|
+
};
|
|
641
|
+
}
|
package/src/mcp/server.js
CHANGED
|
@@ -473,9 +473,12 @@ export const TOOLS = Object.freeze([
|
|
|
473
473
|
title: 'Wait for a task to need you',
|
|
474
474
|
description: 'Follow ONE task this connection submitted and return as soon as it needs the host. '
|
|
475
475
|
+ 'A single call blocks for up to waitSeconds (default 3600) and returns a COMPACT answer: '
|
|
476
|
-
+ '`reason` is `permission` (a per-call request is pending), `
|
|
477
|
-
+ '
|
|
478
|
-
+ '
|
|
476
|
+
+ '`reason` is `permission` (a per-call request is pending), `question` (the worker asked a native '
|
|
477
|
+
+ 'question that needs `delegate_question_answer`), `attention` (a progress check because nothing new was '
|
|
478
|
+
+ 'returned to the host for the operator-configured delivery interval in DSH settings — 40 minutes by '
|
|
479
|
+
+ 'default; internal worker activity does not reset this clock and it does NOT imply the run is stuck — '
|
|
480
|
+
+ 'or a `repeated-tool-failure` reminder), `ready` (the run ended and is awaiting host review), `failed`, '
|
|
481
|
+
+ '`cancelled` or `interrupted`. '
|
|
479
482
|
+ 'An `attention` answer may instead carry a `failure` block (kind `repeated-tool-failure`, with '
|
|
480
483
|
+ 'a stable `eventId`, toolName/failureCode/consecutive): the worker repeated the SAME operation and failed the same way '
|
|
481
484
|
+ 'several times in a row. It is a question for the host, and it is REDELIVERED until you acknowledge it with '
|
|
@@ -1401,19 +1404,16 @@ function compactJson(value) {
|
|
|
1401
1404
|
}
|
|
1402
1405
|
|
|
1403
1406
|
/**
|
|
1404
|
-
* Whether a projected value fits the tool-text bound
|
|
1407
|
+
* Whether a projected value fits the tool-text bound when serialized compactly.
|
|
1405
1408
|
*
|
|
1406
|
-
*
|
|
1407
|
-
*
|
|
1408
|
-
*
|
|
1409
|
-
* push past {@link MAX_TOOL_TEXT_CHARS}.
|
|
1409
|
+
* Compact JSON (no indentation whitespace) is what {@link boundedJson} publishes to MCP
|
|
1410
|
+
* clients so wait, permission, and result turns avoid wasting commander context tokens
|
|
1411
|
+
* on formatting spaces.
|
|
1410
1412
|
* @param {unknown} value - projected JSON value.
|
|
1411
1413
|
* @returns {boolean} true when the value fits.
|
|
1412
1414
|
*/
|
|
1413
1415
|
function fitsBound(value) {
|
|
1414
|
-
|
|
1415
|
-
if (compact.length > MAX_TOOL_TEXT_CHARS) return false;
|
|
1416
|
-
return JSON.stringify(value, null, 2).length <= MAX_TOOL_TEXT_CHARS;
|
|
1416
|
+
return compactJson(value).length <= MAX_TOOL_TEXT_CHARS;
|
|
1417
1417
|
}
|
|
1418
1418
|
|
|
1419
1419
|
/**
|
|
@@ -2300,7 +2300,7 @@ function boundedJson(value) {
|
|
|
2300
2300
|
} catch {
|
|
2301
2301
|
return refuseResult('MCP_RESULT_UNSERIALIZABLE', 'the delegated call answered with a value that could not be serialized');
|
|
2302
2302
|
}
|
|
2303
|
-
if (fitsBound(value)) return { text: JSON.stringify(redactDeepStrings(value)
|
|
2303
|
+
if (fitsBound(value)) return { text: JSON.stringify(redactDeepStrings(value)), isError: false };
|
|
2304
2304
|
|
|
2305
2305
|
const stages = [
|
|
2306
2306
|
{ executionText: TRUNCATED_FIELD_CAPS.executionTextChars, reviewText: TRUNCATED_FIELD_CAPS.reviewTextChars, lists: 50, permissionEvidence: 8 },
|
|
@@ -2343,7 +2343,7 @@ function boundedJson(value) {
|
|
|
2343
2343
|
projected.questionAnswerable = isQuestionAnswerable(projected.question);
|
|
2344
2344
|
projected.grantsAccess = false;
|
|
2345
2345
|
}
|
|
2346
|
-
return JSON.stringify(redactDeepStrings(projected)
|
|
2346
|
+
return JSON.stringify(redactDeepStrings(projected));
|
|
2347
2347
|
};
|
|
2348
2348
|
for (const stage of stages) {
|
|
2349
2349
|
const text = apply(stage);
|
|
@@ -2493,8 +2493,6 @@ function rescueQuestion(value, fullTextChars) {
|
|
|
2493
2493
|
payload.failureAck = questionRescueAck;
|
|
2494
2494
|
}
|
|
2495
2495
|
const redacted = redactDeepStrings(payload);
|
|
2496
|
-
const pretty = JSON.stringify(redacted, null, 2);
|
|
2497
|
-
if (pretty.length <= MAX_TOOL_TEXT_CHARS) return { text: pretty, isError: false };
|
|
2498
2496
|
const compact = JSON.stringify(redacted);
|
|
2499
2497
|
if (compact.length <= MAX_TOOL_TEXT_CHARS) return { text: compact, isError: false };
|
|
2500
2498
|
}
|
|
@@ -2613,7 +2611,7 @@ function permissionRescue(value, fullTextChars) {
|
|
|
2613
2611
|
permissionDecidable: false,
|
|
2614
2612
|
truncation: truncationBlock(records, fullTextChars, entries),
|
|
2615
2613
|
});
|
|
2616
|
-
return JSON.stringify(redactDeepStrings(payload)
|
|
2614
|
+
return JSON.stringify(redactDeepStrings(payload));
|
|
2617
2615
|
}
|
|
2618
2616
|
if (entries.length === 0) {
|
|
2619
2617
|
// A RECEIPT-ONLY rescue: no live request survived to publish, but the decision must
|
|
@@ -2627,7 +2625,7 @@ function permissionRescue(value, fullTextChars) {
|
|
|
2627
2625
|
actionable: true,
|
|
2628
2626
|
truncation: truncationBlock(records, fullTextChars, entries),
|
|
2629
2627
|
});
|
|
2630
|
-
return JSON.stringify(redactDeepStrings(payload)
|
|
2628
|
+
return JSON.stringify(redactDeepStrings(payload));
|
|
2631
2629
|
}
|
|
2632
2630
|
const tasks = entries.map((entry) => {
|
|
2633
2631
|
const pending = render(entry);
|
|
@@ -2643,7 +2641,7 @@ function permissionRescue(value, fullTextChars) {
|
|
|
2643
2641
|
const payload = withReceipt(entries.length === 1 && entries[0].path === 'task'
|
|
2644
2642
|
? { ok: value.ok, task: tasks[0], truncation: truncationBlock(records, fullTextChars, entries) }
|
|
2645
2643
|
: { ok: value.ok, tasks, truncation: truncationBlock(records, fullTextChars, entries) });
|
|
2646
|
-
return JSON.stringify(redactDeepStrings(payload)
|
|
2644
|
+
return JSON.stringify(redactDeepStrings(payload));
|
|
2647
2645
|
};
|
|
2648
2646
|
}
|
|
2649
2647
|
|
|
@@ -2702,13 +2700,13 @@ function refuseResult(code, message, extra = {}) {
|
|
|
2702
2700
|
ok: false,
|
|
2703
2701
|
error: { code, message, ...extra },
|
|
2704
2702
|
truncation: { truncated: true, fullResultIncluded: false },
|
|
2705
|
-
})
|
|
2703
|
+
}));
|
|
2706
2704
|
if (text.length <= MAX_TOOL_TEXT_CHARS) return { text, isError: true };
|
|
2707
2705
|
// The refusal itself is larger than the bound, which only happens if the bound
|
|
2708
2706
|
// is configured absurdly small. This minimal form is still valid JSON and still
|
|
2709
2707
|
// says the answer was refused rather than delivered.
|
|
2710
2708
|
return {
|
|
2711
|
-
text: JSON.stringify({ ok: false, error: { code, truncated: true }, truncation: { truncated: true, fullResultIncluded: false } }
|
|
2709
|
+
text: JSON.stringify({ ok: false, error: { code, truncated: true }, truncation: { truncated: true, fullResultIncluded: false } }),
|
|
2712
2710
|
isError: true,
|
|
2713
2711
|
};
|
|
2714
2712
|
}
|
|
@@ -3362,8 +3360,9 @@ export function createMcpServer(options) {
|
|
|
3362
3360
|
+ 'When an APPLIED decision\'s follow-up wait fails or is cancelled, the call still succeeds with that '
|
|
3363
3361
|
+ 'receipt, `reason: null` and a `waitError` block: only the waiting ended, the execution was not '
|
|
3364
3362
|
+ 'stopped, and the decision must not be resent. To follow a task without polling, use '
|
|
3365
|
-
+ 'delegate_wait({ taskId }): one call blocks until the task needs you (permission,
|
|
3366
|
-
+ 'cancelled, interrupted) or until its own one-hour deadline, in which
|
|
3363
|
+
+ 'delegate_wait({ taskId }): one call blocks until the task needs you (permission, question, '
|
|
3364
|
+
+ 'attention, ready, failed, cancelled, interrupted) or until its own one-hour deadline, in which '
|
|
3365
|
+
+ 'case it answers reason '
|
|
3367
3366
|
+ '`wait-limit` with `actionable: false` and you call it again with the same taskId instead of '
|
|
3368
3367
|
+ 'reviewing or resubmitting anything. Omit waitSeconds for normal waiting: the default already '
|
|
3369
3368
|
+ 'covers an hour, it cannot be raised above 3600, and a shorter value is only for a one-off '
|
|
@@ -3379,9 +3378,13 @@ export function createMcpServer(options) {
|
|
|
3379
3378
|
+ 'time limit is an OUTER limit: the observation did not end and the worker did not stop, so continue '
|
|
3380
3379
|
+ 'the SAME exec cell and ask for the longest yield the host supports (roughly 1200000 ms when a '
|
|
3381
3380
|
+ 'yield/pragmas parameter exists, not a minute) instead of re-issuing every second. (c) `attention` '
|
|
3382
|
-
+ 'is
|
|
3383
|
-
+ '
|
|
3384
|
-
+ '
|
|
3381
|
+
+ 'is either a run progress check (nothing new was delivered back to the host for the operator-configured '
|
|
3382
|
+
+ 'delivery interval in DSH settings — 40 minutes by default; internal worker activity, heartbeats, '
|
|
3383
|
+
+ 'permission/question waits and the local self-check do not reset it, and it does NOT imply the run is '
|
|
3384
|
+
+ 'stuck) or a `repeated-tool-failure` reminder while the task still runs. (d) There is NO default total '
|
|
3385
|
+
+ 'execution timeout: an execution timeout (`JOB_TIMEOUT`, reported as a terminal state) only applies when '
|
|
3386
|
+
+ 'an explicit cap is supplied via `overrides.timeoutSeconds` — it is not `wait-limit`, not `attention` '
|
|
3387
|
+
+ 'and not a client-side event. A real '
|
|
3385
3388
|
+ 'failure is worth one de-duplicated reminder; an ordinary single failing test inside the worker is '
|
|
3386
3389
|
+ 'the worker\'s job to fix and is not a host event. '
|
|
3387
3390
|
+ '`interrupted` never means the execution stopped by itself: this call returns it at once '
|
|
@@ -3404,7 +3407,8 @@ export function createMcpServer(options) {
|
|
|
3404
3407
|
+ 'PROGRAM, not the host model: the host\'s own turns, token usage and cost are reported as '
|
|
3405
3408
|
+ '`not-integrated` because this process cannot observe them. '
|
|
3406
3409
|
+ 'When a task reaches `ready`/`awaiting-host-review`, finish YOUR review and record it with '
|
|
3407
|
-
+ 'delegate_review ({ taskId, decision: "accepted" | "changes-requested", note }):
|
|
3410
|
+
+ 'delegate_review ({ taskId, decision: "accepted" | "changes-requested", note, completionSummary? }): '
|
|
3411
|
+
+ 'that call is the '
|
|
3408
3412
|
+ 'only thing that records host acceptance, and until it is made the task stays unreviewed. '
|
|
3409
3413
|
+ 'Completion is never acceptance on its own, and a review cannot approve permissions, change '
|
|
3410
3414
|
+ 'settings or models, merge the diff or create work. The first decision is final for that task; '
|