@stmcw1/dacw 1.0.2 → 1.1.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/README.md +128 -21
- package/README_EN.md +125 -20
- package/SAFETY.md +59 -6
- package/SAFETY_EN.md +59 -6
- package/cordis.patch.yml +12 -0
- package/dist/index.js +643 -14
- package/dist/plugin.js +444 -67
- package/dist/runner.js +159 -92
- package/dist/sandbox.js +297 -0
- package/dist/secrets.js +42 -0
- package/dist/state_manager.js +13 -11
- package/package.json +2 -5
package/dist/plugin.js
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
import * as fs from 'fs/promises';
|
|
2
2
|
import * as os from 'os';
|
|
3
3
|
import * as path from 'path';
|
|
4
|
-
import { apply as createEngine } from './index.js';
|
|
5
|
-
|
|
4
|
+
import { apply as createEngine, isValidWorkspaceMarker, resolveEngineOptions, rollbackTask } from './index.js';
|
|
5
|
+
import { DEFAULT_SANDBOX_CONFIG, normalizeSandboxConfig, parseSandboxConsentAnswer } from './sandbox.js';
|
|
6
|
+
const PLUGIN_VERSION = '1.1.0';
|
|
6
7
|
const WRITER_PRESET_ID = 'dacw-writer';
|
|
7
8
|
const REVIEWER_PRESET_ID = 'dacw-reviewer';
|
|
8
9
|
const WORKSPACE_MARKER = '.dacw-workspace';
|
|
9
10
|
const WORKSPACE_MARKER_MANAGED_BY = '@stmcw1/dacw';
|
|
10
|
-
const LEGACY_WORKSPACE_MARKER_MANAGED_BY = '@local/dacw';
|
|
11
11
|
const WORKSPACE_MARKER_CONTENT = `{"managedBy":"${WORKSPACE_MARKER_MANAGED_BY}","version":"${PLUGIN_VERSION}",` + `"note":"DACW resets non-foundation files in this directory every round. Remove this marker to revoke that."}\n`;
|
|
12
12
|
const MAX_TASK_CHARS = 20_000;
|
|
13
13
|
const MAX_TASK_ID_CHARS = 128;
|
|
@@ -33,7 +33,10 @@ export const DEFAULT_PLUGIN_CONFIG = {
|
|
|
33
33
|
writerOutputMaxChars: 2_000_000,
|
|
34
34
|
manifestMaxEntries: 400,
|
|
35
35
|
allowInheritedPresets: false,
|
|
36
|
-
enforceChildToolIsolation: true
|
|
36
|
+
enforceChildToolIsolation: true,
|
|
37
|
+
sandbox: DEFAULT_SANDBOX_CONFIG,
|
|
38
|
+
allowTruncatedReview: false,
|
|
39
|
+
secretScanEnabled: true
|
|
37
40
|
};
|
|
38
41
|
function positiveIntOr(value, fallback, warnings, field) {
|
|
39
42
|
if (value === undefined || value === null) return fallback;
|
|
@@ -90,6 +93,8 @@ function normalizeModelConfig(value, warnings, field) {
|
|
|
90
93
|
export function normalizePluginConfig(raw) {
|
|
91
94
|
const warnings = [];
|
|
92
95
|
const input = raw && typeof raw === 'object' && !Array.isArray(raw) ? raw : {};
|
|
96
|
+
const rawSandbox = input.sandbox;
|
|
97
|
+
const sandboxExplicit = !!rawSandbox && typeof rawSandbox === 'object' && !Array.isArray(rawSandbox) && rawSandbox.mode === 'host';
|
|
93
98
|
let toolName = nonEmptyStringOr(input.toolName, DEFAULT_PLUGIN_CONFIG.toolName, warnings, 'toolName');
|
|
94
99
|
if (!/^[a-z][a-z0-9_]*$/.test(toolName)) {
|
|
95
100
|
warnings.push(`config.toolName 非法(需匹配 /^[a-z][a-z0-9_]*$/),已回退默认值 ${DEFAULT_PLUGIN_CONFIG.toolName}`);
|
|
@@ -112,7 +117,10 @@ export function normalizePluginConfig(raw) {
|
|
|
112
117
|
writerOutputMaxChars: positiveIntOr(input.writerOutputMaxChars, DEFAULT_PLUGIN_CONFIG.writerOutputMaxChars, warnings, 'writerOutputMaxChars'),
|
|
113
118
|
manifestMaxEntries: positiveIntOr(input.manifestMaxEntries, DEFAULT_PLUGIN_CONFIG.manifestMaxEntries, warnings, 'manifestMaxEntries'),
|
|
114
119
|
allowInheritedPresets: input.allowInheritedPresets === true,
|
|
115
|
-
enforceChildToolIsolation: input.enforceChildToolIsolation !== false
|
|
120
|
+
enforceChildToolIsolation: input.enforceChildToolIsolation !== false,
|
|
121
|
+
sandbox: normalizeSandboxConfig(input.sandbox, warnings, 'config.sandbox'),
|
|
122
|
+
allowTruncatedReview: input.allowTruncatedReview === true,
|
|
123
|
+
secretScanEnabled: input.secretScanEnabled !== false
|
|
116
124
|
};
|
|
117
125
|
if (config.chatTimeoutMaxMs < config.chatTimeoutBaseMs) {
|
|
118
126
|
warnings.push('config.chatTimeoutMaxMs 小于 chatTimeoutBaseMs,已把 chatTimeoutMaxMs 提升到 base');
|
|
@@ -124,9 +132,19 @@ export function normalizePluginConfig(raw) {
|
|
|
124
132
|
if (!config.enforceChildToolIsolation) {
|
|
125
133
|
warnings.push('config.enforceChildToolIsolation=false:不再强制剥离子代理的宿主全局工具');
|
|
126
134
|
}
|
|
135
|
+
if (sandboxExplicit) {
|
|
136
|
+
warnings.push('config.sandbox.mode="host":测试将以宿主用户全权限直接执行写手产出的代码(无任何隔离)。' + '仅在你显式信任写手输出与工作区内容时使用。');
|
|
137
|
+
}
|
|
138
|
+
if (config.allowTruncatedReview) {
|
|
139
|
+
warnings.push('config.allowTruncatedReview=true:审查输入被截断或锁文件仅送摘要时仍会返回 success:true,' + '安全保证降低(默认 fail-closed 拒绝)。');
|
|
140
|
+
}
|
|
141
|
+
if (!config.secretScanEnabled) {
|
|
142
|
+
warnings.push('config.secretScanEnabled=false:写手输出不再做凭据模式扫描,密钥可能进入代码/审查上下文(安全保证降低)。');
|
|
143
|
+
}
|
|
127
144
|
return {
|
|
128
145
|
config,
|
|
129
|
-
warnings
|
|
146
|
+
warnings,
|
|
147
|
+
sandboxExplicit
|
|
130
148
|
};
|
|
131
149
|
}
|
|
132
150
|
export function extractTextBlocks(output) {
|
|
@@ -421,64 +439,74 @@ async function resolveRoleModel(ctx, spec, role, logger) {
|
|
|
421
439
|
source: 'config'
|
|
422
440
|
};
|
|
423
441
|
}
|
|
424
|
-
function restrictChildTools(ctx, agent, isolation, logger) {
|
|
442
|
+
export function restrictChildTools(ctx, agent, isolation, logger) {
|
|
425
443
|
const fail = (reason)=>{
|
|
426
444
|
isolation.failures += 1;
|
|
427
445
|
isolation.lastError = reason;
|
|
428
446
|
logger.error(`DACW: 子代理工具隔离失败:${reason}`);
|
|
429
447
|
throw new Error(`[DACW] 子代理工具隔离失败:${reason}`);
|
|
430
448
|
};
|
|
449
|
+
const failUnverified = (reason)=>{
|
|
450
|
+
logger.error(`DACW: 子代理 ${String(agent && agent.id)} 隔离已应用但无法验证(${reason})`);
|
|
451
|
+
return fail(reason);
|
|
452
|
+
};
|
|
431
453
|
if (!agent || !agent.ctx || !agent.ctx.tools || typeof agent.ctx.tools.restrict !== 'function') {
|
|
432
454
|
fail('子代理上下文缺少 tools.restrict');
|
|
433
455
|
}
|
|
434
456
|
const childId = String(agent.id);
|
|
435
457
|
let dispose = null;
|
|
458
|
+
let restrictApplied = false;
|
|
436
459
|
let strategy = '';
|
|
437
460
|
try {
|
|
438
461
|
dispose = agent.ctx.tools.restrict({
|
|
439
462
|
allow: []
|
|
440
463
|
});
|
|
464
|
+
restrictApplied = true;
|
|
441
465
|
strategy = 'allow:[]';
|
|
442
466
|
} catch {
|
|
467
|
+
if (typeof ctx.tools.schemas !== 'function') fail('无法枚举宿主全局工具(ctx.tools.schemas 缺失),无法安全回退');
|
|
443
468
|
let names = [];
|
|
444
469
|
try {
|
|
445
|
-
if (typeof ctx.tools.schemas !== 'function') fail('无法枚举宿主全局工具(ctx.tools.schemas 缺失),无法安全回退');
|
|
446
470
|
names = ctx.tools.schemas().map((schema)=>schema && schema.name).filter((name)=>typeof name === 'string' && name);
|
|
447
471
|
} catch (error) {
|
|
448
472
|
fail(`读取宿主全局工具列表失败:${errorMessage(error)}`);
|
|
449
473
|
}
|
|
450
474
|
if (names.length === 0) {
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
}
|
|
475
|
+
fail('宿主全局工具列表为空且 allow:[] 不可用,无法应用工具隔离');
|
|
476
|
+
}
|
|
477
|
+
try {
|
|
478
|
+
dispose = agent.ctx.tools.restrict({
|
|
479
|
+
deny: names
|
|
480
|
+
});
|
|
481
|
+
restrictApplied = true;
|
|
482
|
+
strategy = `deny:${names.join(',')}`;
|
|
483
|
+
} catch (error) {
|
|
484
|
+
fail(`ctx.tools.restrict 执行失败:${errorMessage(error)}`);
|
|
462
485
|
}
|
|
463
486
|
}
|
|
487
|
+
if (!restrictApplied) failUnverified('restrict 未成功应用');
|
|
488
|
+
if (typeof agent.ctx.tools.schemas !== 'function') {
|
|
489
|
+
failUnverified('子代理上下文缺少 tools.schemas,无法验证隔离生效');
|
|
490
|
+
}
|
|
491
|
+
let visible;
|
|
464
492
|
try {
|
|
465
|
-
|
|
466
|
-
const visible = agent.ctx.tools.schemas(agent);
|
|
467
|
-
if (Array.isArray(visible) && visible.length > 0) {
|
|
468
|
-
const names = visible.map((schema)=>schema && schema.name).filter(Boolean).join(', ');
|
|
469
|
-
fail(`隔离后子代理仍可见 ${visible.length} 个工具(${names})`);
|
|
470
|
-
}
|
|
471
|
-
}
|
|
493
|
+
visible = agent.ctx.tools.schemas(agent);
|
|
472
494
|
} catch (error) {
|
|
473
|
-
|
|
474
|
-
|
|
495
|
+
failUnverified(`调用 tools.schemas(agent) 失败:${errorMessage(error)}`);
|
|
496
|
+
}
|
|
497
|
+
if (!Array.isArray(visible)) {
|
|
498
|
+
failUnverified('tools.schemas(agent) 未返回工具数组,无法验证隔离生效');
|
|
499
|
+
}
|
|
500
|
+
if (visible.length > 0) {
|
|
501
|
+
const names = visible.map((schema)=>schema && schema.name).filter(Boolean).join(', ');
|
|
502
|
+
fail(`隔离后子代理仍可见 ${visible.length} 个工具(${names})`);
|
|
475
503
|
}
|
|
476
504
|
if (typeof dispose === 'function') isolation.disposers.set(childId, dispose);
|
|
477
505
|
isolation.restricted.add(childId);
|
|
478
506
|
isolation.applied += 1;
|
|
479
507
|
logger.info(`DACW: 子代理 ${childId} 工具隔离已生效(策略 ${strategy},隔离后可见工具 0 个)`);
|
|
480
508
|
}
|
|
481
|
-
async function runSubagentChat(options) {
|
|
509
|
+
export async function runSubagentChat(options) {
|
|
482
510
|
const { subagents, config, presets, isolation, agent, workspaceDir, signal, role, text, dacwChildren, logger, roleModel } = options;
|
|
483
511
|
const presetId = presets.ready ? role === 'writer' ? WRITER_PRESET_ID : REVIEWER_PRESET_ID : null;
|
|
484
512
|
if (signal && signal.aborted) throw abortError();
|
|
@@ -500,11 +528,24 @@ async function runSubagentChat(options) {
|
|
|
500
528
|
throw new Error(`[DACW] ${role} 子代理启动失败:${errorMessage(error)}`);
|
|
501
529
|
}
|
|
502
530
|
const childId = activation && activation.childId ? String(activation.childId) : '';
|
|
503
|
-
|
|
531
|
+
const disposeActivation = async ()=>{
|
|
504
532
|
try {
|
|
505
533
|
if (activation && typeof activation.dispose === 'function') await activation.dispose();
|
|
506
534
|
} catch {}
|
|
507
|
-
|
|
535
|
+
};
|
|
536
|
+
if (config.enforceChildToolIsolation) {
|
|
537
|
+
if (!presets.ready) {
|
|
538
|
+
await disposeActivation();
|
|
539
|
+
throw new Error(`[DACW] enforceChildToolIsolation=true 但写手/审查者专用 preset 不可用(${presets.reason || '未知原因'}),` + '无法确认子代理运行在零工具 preset 上:拒绝运行。' + '请修复 preset 注册,或在明确接受风险后设置 config.enforceChildToolIsolation=false。');
|
|
540
|
+
}
|
|
541
|
+
if (!childId) {
|
|
542
|
+
await disposeActivation();
|
|
543
|
+
throw new Error('[DACW] provider 未返回有效 childId,无法确认隔离生效:' + '拒绝在写手/审查者可能继承宿主工具的情况下运行。' + '可修复宿主环境,或在明确接受风险后设置 config.enforceChildToolIsolation=false。');
|
|
544
|
+
}
|
|
545
|
+
if (!isolation.restricted.has(childId)) {
|
|
546
|
+
await disposeActivation();
|
|
547
|
+
throw new Error(`[DACW] 子代理 ${childId} 的工具隔离未生效(${isolation.lastError || '未知原因'}):` + `拒绝在写手/审查者可能继承宿主工具的情况下运行。可修复宿主环境,或在明确接受风险后设置 config.enforceChildToolIsolation=false。`);
|
|
548
|
+
}
|
|
508
549
|
}
|
|
509
550
|
if (childId) dacwChildren.add(childId);
|
|
510
551
|
let abortListener = null;
|
|
@@ -556,17 +597,7 @@ const PROTECTED_WORKSPACE_ENTRIES = new Set([
|
|
|
556
597
|
'node_modules',
|
|
557
598
|
'.git'
|
|
558
599
|
]);
|
|
559
|
-
export
|
|
560
|
-
if (typeof content !== 'string' || content.trim() === '') return false;
|
|
561
|
-
try {
|
|
562
|
-
const parsed = JSON.parse(content);
|
|
563
|
-
if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed)) return false;
|
|
564
|
-
const managedBy = parsed.managedBy;
|
|
565
|
-
return managedBy === WORKSPACE_MARKER_MANAGED_BY || managedBy === LEGACY_WORKSPACE_MARKER_MANAGED_BY;
|
|
566
|
-
} catch {
|
|
567
|
-
return false;
|
|
568
|
-
}
|
|
569
|
-
}
|
|
600
|
+
export { isValidWorkspaceMarker };
|
|
570
601
|
function isSameOrInside(parent, child) {
|
|
571
602
|
const relative = path.relative(parent, child);
|
|
572
603
|
return relative === '' || !relative.startsWith('..') && !path.isAbsolute(relative);
|
|
@@ -648,6 +679,9 @@ function renderToolResultText(value) {
|
|
|
648
679
|
lines.push(`DACW 结果:${value.success ? '机器闸门通过' : '未通过'}`);
|
|
649
680
|
lines.push(`状态:${value.status}${value.status === 'NEED_HUMAN' ? '(需要人工终审,未经确认不要合入)' : ''}`);
|
|
650
681
|
lines.push(`工作区:${value.workspace}`);
|
|
682
|
+
if (typeof value.task_id === 'string') {
|
|
683
|
+
lines.push(`回滚:${value.rollback_available === true ? '可用' : '不可用'}(task_id: ${value.task_id},调用 dacw_rollback 回滚到任务开始前)`);
|
|
684
|
+
}
|
|
651
685
|
if (value.models && typeof value.models.writer === 'string' && typeof value.models.reviewer === 'string') {
|
|
652
686
|
lines.push(`模型:✍️ 写手 ${value.models.writer} | 🔍 审查者 ${value.models.reviewer}`);
|
|
653
687
|
}
|
|
@@ -666,25 +700,153 @@ function renderToolResultText(value) {
|
|
|
666
700
|
}
|
|
667
701
|
return lines.join('\n');
|
|
668
702
|
}
|
|
669
|
-
|
|
703
|
+
function renderRollbackResultText(value) {
|
|
704
|
+
const lines = [];
|
|
705
|
+
lines.push(`DACW 回滚:${value.success ? '成功' : '未成功'}`);
|
|
706
|
+
if (value.mode) {
|
|
707
|
+
lines.push(`模式:${value.mode === 'git-hint' ? 'git 提示(DACW 未执行 git 命令)' : '工作区备份恢复'}`);
|
|
708
|
+
}
|
|
709
|
+
if (typeof value.task_id === 'string') lines.push(`任务:${value.task_id}`);
|
|
710
|
+
if (typeof value.workspace === 'string') lines.push(`工作区:${value.workspace}`);
|
|
711
|
+
lines.push('');
|
|
712
|
+
lines.push(`说明:${String(value.message ?? '')}`);
|
|
713
|
+
if (value.record && typeof value.record.hint === 'string' && value.record.hint) {
|
|
714
|
+
lines.push('');
|
|
715
|
+
lines.push('手动回滚命令(含风险提示,请先阅读):');
|
|
716
|
+
lines.push(value.record.hint);
|
|
717
|
+
}
|
|
718
|
+
return lines.join('\n');
|
|
719
|
+
}
|
|
720
|
+
const ROLLBACK_TOOL_DESCRIPTION = '调用时必须传 confirm: true 表示有意调用。' + '回滚 DACW 任务到任务开始前。git 工作区返回回滚指令(DACW 不执行 git 命令);' + '非 git 工作区从备份恢复。回滚需人工触发,执行前会通过宿主 userQuestions 请求人类确认;' + '宿主无交互能力且未显式设置 DACW_ROLLBACK_TOKEN 时拒绝执行(fail-closed),回滚后任务终止。' + '必须与执行该任务的同一个 DACW 插件实例/状态配置一起使用' + '(状态文件按 task_id + 工作区路径定位;编程调用方若注入自定义 stateChecksumFn,回滚必须使用同一函数)。';
|
|
721
|
+
const ROLLBACK_TOKEN_ENV = 'DACW_ROLLBACK_TOKEN';
|
|
722
|
+
const ROLLBACK_CONFIRM_QUESTION_ID = 'dacw-rollback-confirm';
|
|
723
|
+
const ROLLBACK_CONFIRM_ACCEPT_LABEL = '确认回滚';
|
|
724
|
+
const ROLLBACK_CONFIRM_CANCEL_LABEL = '取消';
|
|
725
|
+
async function askRollbackConfirmation(api, exec, info) {
|
|
726
|
+
const modeText = info.mode === 'git-hint' ? 'git 工作区:仅返回回滚指令,DACW 不执行任何 git 命令' : '非 git 工作区:从任务开始前的备份恢复文件';
|
|
727
|
+
const question = [
|
|
728
|
+
`即将回滚任务 ${info.taskId} 的工作区到任务开始前。`,
|
|
729
|
+
`模式:${modeText}`,
|
|
730
|
+
`工作区:${info.workspaceDir}`,
|
|
731
|
+
`回滚点:${info.rollbackPoint.ref}(捕获于 ${info.rollbackPoint.capturedAt})`,
|
|
732
|
+
'回滚后任务终止,且工作区内容将与任务开始前一致。确认继续?'
|
|
733
|
+
].join('\n');
|
|
734
|
+
let answer;
|
|
735
|
+
try {
|
|
736
|
+
answer = await api.ask({
|
|
737
|
+
questions: [
|
|
738
|
+
{
|
|
739
|
+
id: ROLLBACK_CONFIRM_QUESTION_ID,
|
|
740
|
+
header: '确认回滚',
|
|
741
|
+
question,
|
|
742
|
+
options: [
|
|
743
|
+
{
|
|
744
|
+
label: ROLLBACK_CONFIRM_ACCEPT_LABEL,
|
|
745
|
+
description: '执行回滚(高影响操作,回滚后任务终止)'
|
|
746
|
+
},
|
|
747
|
+
{
|
|
748
|
+
label: ROLLBACK_CONFIRM_CANCEL_LABEL,
|
|
749
|
+
description: '取消:不执行任何回滚,工作区保持不变'
|
|
750
|
+
}
|
|
751
|
+
]
|
|
752
|
+
}
|
|
753
|
+
],
|
|
754
|
+
...exec && exec.agent ? {
|
|
755
|
+
agent: exec.agent
|
|
756
|
+
} : {},
|
|
757
|
+
...exec && exec.signal ? {
|
|
758
|
+
signal: exec.signal
|
|
759
|
+
} : {}
|
|
760
|
+
});
|
|
761
|
+
} catch (error) {
|
|
762
|
+
if (exec && exec.signal && exec.signal.aborted) throw abortError();
|
|
763
|
+
if (isAbortLike(error)) throw abortError();
|
|
764
|
+
throw new Error(`[DACW] 回滚确认交互失败:${errorMessage(error)}`);
|
|
765
|
+
}
|
|
766
|
+
const answers = Array.isArray(answer && answer.answers) ? answer.answers : [];
|
|
767
|
+
const first = answers.find((item)=>item && item.id === ROLLBACK_CONFIRM_QUESTION_ID);
|
|
768
|
+
const selected = first && Array.isArray(first.selected) ? first.selected.map((value)=>String(value)) : [];
|
|
769
|
+
return selected.includes(ROLLBACK_CONFIRM_ACCEPT_LABEL);
|
|
770
|
+
}
|
|
771
|
+
const TOOL_DESCRIPTION = '运行 DACW(防御性智能体编程工作流)闭环:写手子代理按增量协议产出代码变更 → 确定性运行机执行项目自带测试 → ' + '独立审查者子代理审计全量文件并给出 JSON 裁决 → 通过后停在人工闸门(NEED_HUMAN),绝不自动合入。' + '测试在任何审查之前执行;默认 host(执行隔离默认关闭,测试以宿主用户权限运行写手产出的代码)。' + '如需容器隔离可显式配置 sandbox.mode:"auto":Docker 可用时在一次性容器内跑测试(无网络、只读根文件系统、' + '非 root、资源受限);Docker 不可用时通过 ctx.userQuestions 交互询问是否降级,拒绝或无法确认即 fail-closed 停止;' + 'sandbox.mode:"docker" 强制容器,不可用时拒绝运行。审查者审计是侦探性控制,不是执行前屏障。' + '当需要"让模型写代码、让真实测试验证、让独立审查者把关、最后人工确认"时使用本工具。' + '注意:DACW 每轮都会把工作区内容重置为产出文件集(仅保留 package.json 等地基文件与 .git/node_modules),' + '因此必须指向专用空目录(工具会写入 .dacw-workspace 标记);不要在存放重要文件的目录上运行。' + '模型调用发生在独立子代理会话里,不污染当前会话;子代理在创建时被剥除全部宿主工具。' + '写手/审查者可分别配置模型:支持 "provider/model"、纯 "model"(按 DSH 已配置模型目录自动解析 provider)或 inherit;' + '运行日志首行与结果 models 字段显示实际生效的写手/审查者模型。' + '结果含 task_id 与 rollback_available;需要撤销本次任务时用 dacw_rollback(人工触发,回滚后任务终止)。';
|
|
772
|
+
function hostIncompatible(capability, detail) {
|
|
773
|
+
const reason = detail ? `(${detail};DSH 版本可能低于 0.2.1-rc.5 或 API 已变更)` : '(DSH 版本可能低于 0.2.1-rc.5 或 API 已变更)';
|
|
774
|
+
return new Error(`[DACW] 宿主不兼容:缺少 ${capability}${reason}。\n请确认 DSH 版本,或联系插件维护者。`);
|
|
775
|
+
}
|
|
776
|
+
function assertHostCompatibility(ctx, config, logger) {
|
|
777
|
+
const tools = (()=>{
|
|
778
|
+
try {
|
|
779
|
+
return ctx && ctx.tools;
|
|
780
|
+
} catch {
|
|
781
|
+
return undefined;
|
|
782
|
+
}
|
|
783
|
+
})();
|
|
784
|
+
if (!tools || typeof tools.register !== 'function') {
|
|
785
|
+
throw hostIncompatible('ctx.tools.register');
|
|
786
|
+
}
|
|
787
|
+
if (config.enforceChildToolIsolation && typeof tools.restrict !== 'function') {
|
|
788
|
+
throw hostIncompatible('ctx.tools.restrict', 'config.enforceChildToolIsolation=true 时必需');
|
|
789
|
+
}
|
|
790
|
+
const getService = (name)=>{
|
|
791
|
+
try {
|
|
792
|
+
return ctx && typeof ctx.get === 'function' ? ctx.get(name) : undefined;
|
|
793
|
+
} catch {
|
|
794
|
+
return undefined;
|
|
795
|
+
}
|
|
796
|
+
};
|
|
797
|
+
const agentPresets = getService('agentPresets');
|
|
798
|
+
if (!agentPresets || typeof agentPresets.register !== 'function') {
|
|
799
|
+
logger.warn("DACW: 激活时 ctx.get('agentPresets') 尚不可用;将等待该服务挂载后再注册 preset。" + '若始终不可用,enforceChildToolIsolation=true 时 dacw_run 会 fail-closed 拒绝。');
|
|
800
|
+
}
|
|
801
|
+
const subagents = getService('subagents');
|
|
802
|
+
if (!subagents || typeof subagents.getProvider !== 'function' || typeof subagents.startActivation !== 'function') {
|
|
803
|
+
logger.warn("DACW: 激活时 ctx.get('subagents') 尚不可用(getProvider/startActivation);将在 dacw_run 调用时再次检查并拒绝。");
|
|
804
|
+
}
|
|
805
|
+
const providerName = String(config.subagentProvider || 'spawn');
|
|
806
|
+
let provider;
|
|
807
|
+
try {
|
|
808
|
+
provider = subagents && typeof subagents.getProvider === 'function' ? subagents.getProvider(providerName) : undefined;
|
|
809
|
+
} catch {
|
|
810
|
+
provider = undefined;
|
|
811
|
+
}
|
|
812
|
+
if (!provider || typeof provider !== 'object') {
|
|
813
|
+
logger.warn(`DACW: provider "${providerName}" 在激活时尚未注册(可能是加载顺序问题);将在 dacw_run 调用时再次检查。`);
|
|
814
|
+
}
|
|
815
|
+
}
|
|
670
816
|
export function apply(ctx, rawConfig) {
|
|
671
|
-
const { config, warnings } = normalizePluginConfig(rawConfig);
|
|
817
|
+
const { config, warnings, sandboxExplicit } = normalizePluginConfig(rawConfig);
|
|
672
818
|
const logger = makeLogger(ctx);
|
|
819
|
+
assertHostCompatibility(ctx, config, logger);
|
|
673
820
|
for (const warning of warnings)logger.warn(`DACW: ${warning}`);
|
|
821
|
+
const resolvedEngineOptions = resolveEngineOptions({
|
|
822
|
+
maxRounds: config.maxRounds,
|
|
823
|
+
chatTimeoutBaseMs: config.chatTimeoutBaseMs,
|
|
824
|
+
chatTimeoutPer10kCharsMs: config.chatTimeoutPer10kCharsMs,
|
|
825
|
+
chatTimeoutMaxMs: config.chatTimeoutMaxMs,
|
|
826
|
+
chatMaxAttempts: config.chatMaxAttempts,
|
|
827
|
+
writerOutputMaxChars: config.writerOutputMaxChars,
|
|
828
|
+
manifestMaxEntries: config.manifestMaxEntries,
|
|
829
|
+
stateDir: config.stateDir || null,
|
|
830
|
+
sandbox: config.sandbox,
|
|
831
|
+
sandboxExplicit,
|
|
832
|
+
allowTruncatedReview: config.allowTruncatedReview,
|
|
833
|
+
secretScanEnabled: config.secretScanEnabled
|
|
834
|
+
});
|
|
674
835
|
const presets = {
|
|
675
836
|
ready: false,
|
|
676
|
-
reason: '
|
|
837
|
+
reason: '尚未注册(等待 agentPresets 服务)',
|
|
677
838
|
disposers: []
|
|
678
839
|
};
|
|
679
|
-
|
|
840
|
+
let agentPresetsService = (()=>{
|
|
680
841
|
try {
|
|
681
842
|
return ctx.get('agentPresets');
|
|
682
843
|
} catch {
|
|
683
844
|
return undefined;
|
|
684
845
|
}
|
|
685
846
|
})();
|
|
686
|
-
|
|
687
|
-
|
|
847
|
+
const registerPresets = (service, ownerCtx)=>{
|
|
848
|
+
agentPresetsService = service;
|
|
849
|
+
ownerCtx.effect(()=>{
|
|
688
850
|
let disposed = false;
|
|
689
851
|
void (async ()=>{
|
|
690
852
|
try {
|
|
@@ -693,7 +855,7 @@ export function apply(ctx, rawConfig) {
|
|
|
693
855
|
presetDefinition(REVIEWER_PRESET_ID, 'DACW Reviewer', 'DACW 审查者子代理:只输出 JSON 裁决,创建时剥除全部宿主工具。', 92, REVIEWER_PERSONA)
|
|
694
856
|
];
|
|
695
857
|
for (const definition of definitions){
|
|
696
|
-
const dispose = await
|
|
858
|
+
const dispose = await service.register(definition);
|
|
697
859
|
if (disposed) {
|
|
698
860
|
try {
|
|
699
861
|
await dispose();
|
|
@@ -701,8 +863,8 @@ export function apply(ctx, rawConfig) {
|
|
|
701
863
|
continue;
|
|
702
864
|
}
|
|
703
865
|
if (typeof dispose === 'function') presets.disposers.push(dispose);
|
|
704
|
-
if (typeof
|
|
705
|
-
const detail = await
|
|
866
|
+
if (typeof service.resolve === 'function') {
|
|
867
|
+
const detail = await service.resolve(definition.id).catch(()=>null);
|
|
706
868
|
if (detail && typeof detail.broken === 'string' && detail.broken) {
|
|
707
869
|
throw new Error(`${definition.id} 激活失败:${detail.broken}`);
|
|
708
870
|
}
|
|
@@ -726,8 +888,31 @@ export function apply(ctx, rawConfig) {
|
|
|
726
888
|
}
|
|
727
889
|
};
|
|
728
890
|
});
|
|
891
|
+
};
|
|
892
|
+
if (agentPresetsService && typeof agentPresetsService.register === 'function') {
|
|
893
|
+
registerPresets(agentPresetsService, ctx);
|
|
894
|
+
} else if (typeof ctx.inject === 'function') {
|
|
895
|
+
presets.reason = '等待 agentPresets 服务出现(尚未注册)';
|
|
896
|
+
ctx.inject([
|
|
897
|
+
'agentPresets'
|
|
898
|
+
], (child)=>{
|
|
899
|
+
const service = (()=>{
|
|
900
|
+
try {
|
|
901
|
+
return child.get('agentPresets');
|
|
902
|
+
} catch {
|
|
903
|
+
return undefined;
|
|
904
|
+
}
|
|
905
|
+
})();
|
|
906
|
+
if (service && typeof service.register === 'function') {
|
|
907
|
+
registerPresets(service, child);
|
|
908
|
+
} else {
|
|
909
|
+
presets.ready = false;
|
|
910
|
+
presets.reason = 'ctx.inject 已触发但 agentPresets 不可用(宿主未注册该服务)';
|
|
911
|
+
logger.warn(`DACW: ${presets.reason}`);
|
|
912
|
+
}
|
|
913
|
+
});
|
|
729
914
|
} else {
|
|
730
|
-
presets.reason = '宿主未提供 agentPresets
|
|
915
|
+
presets.reason = '宿主未提供 agentPresets 服务,且 Context 不支持 inject';
|
|
731
916
|
logger.warn('DACW: 宿主未提供 agentPresets 服务,无法注册写手/审查者 preset');
|
|
732
917
|
}
|
|
733
918
|
const isolation = {
|
|
@@ -740,8 +925,8 @@ export function apply(ctx, rawConfig) {
|
|
|
740
925
|
};
|
|
741
926
|
const composedPresetOf = (agent)=>{
|
|
742
927
|
try {
|
|
743
|
-
if (
|
|
744
|
-
return
|
|
928
|
+
if (agentPresetsService && typeof agentPresetsService.composedPreset === 'function' && agent && agent.ctx) {
|
|
929
|
+
return agentPresetsService.composedPreset(agent.ctx);
|
|
745
930
|
}
|
|
746
931
|
} catch {}
|
|
747
932
|
return undefined;
|
|
@@ -864,6 +1049,12 @@ export function apply(ctx, rawConfig) {
|
|
|
864
1049
|
},
|
|
865
1050
|
elapsed_ms: {
|
|
866
1051
|
type: 'integer'
|
|
1052
|
+
},
|
|
1053
|
+
task_id: {
|
|
1054
|
+
type: 'string'
|
|
1055
|
+
},
|
|
1056
|
+
rollback_available: {
|
|
1057
|
+
type: 'boolean'
|
|
867
1058
|
}
|
|
868
1059
|
},
|
|
869
1060
|
required: [
|
|
@@ -875,7 +1066,9 @@ export function apply(ctx, rawConfig) {
|
|
|
875
1066
|
'round',
|
|
876
1067
|
'files',
|
|
877
1068
|
'logs',
|
|
878
|
-
'elapsed_ms'
|
|
1069
|
+
'elapsed_ms',
|
|
1070
|
+
'task_id',
|
|
1071
|
+
'rollback_available'
|
|
879
1072
|
],
|
|
880
1073
|
additionalProperties: false
|
|
881
1074
|
},
|
|
@@ -910,14 +1103,17 @@ export function apply(ctx, rawConfig) {
|
|
|
910
1103
|
const available = typeof subagents.list === 'function' ? subagents.list().join(', ') : '未知';
|
|
911
1104
|
throw new Error(`[DACW] 子代理 provider "${providerName}" 未注册(可用:${available})`);
|
|
912
1105
|
}
|
|
1106
|
+
if (typeof subagents.startActivation !== 'function') {
|
|
1107
|
+
throw new Error('[DACW] 宿主 subagents 服务缺少 startActivation:无法创建写手/审查者子代理,拒绝运行。请确认 DSH 版本(0.2.1-rc.5 起应提供该入口)。');
|
|
1108
|
+
}
|
|
913
1109
|
if (presets.ready && provider.capabilities && provider.capabilities.agentPreset === false) {
|
|
914
|
-
throw new Error(`[DACW] 子代理 provider "${providerName}" 不支持 agentPreset(独立 preset 子代理),` + `无法保证写手/审查者运行在专用零工具 preset 上;请改用 spawn(@deepseek-ai/dsh-subagent-spawn-in-process),` +
|
|
1110
|
+
throw new Error(`[DACW] 子代理 provider "${providerName}" 不支持 agentPreset(独立 preset 子代理),` + `无法保证写手/审查者运行在专用零工具 preset 上;请改用 spawn(@deepseek-ai/dsh-subagent-spawn-in-process),` + `或在明确接受风险后同时设置 config.allowInheritedPresets: true 与 config.enforceChildToolIsolation: false。`);
|
|
915
1111
|
}
|
|
916
1112
|
const agent = exec && exec.agent ? exec.agent : null;
|
|
917
1113
|
if (!agent) throw new Error('[DACW] 只能由 Agent 调用:执行上下文缺少调用方 Agent');
|
|
918
1114
|
if (isDacwChild(agent)) throw new Error('[DACW] 禁止在 DACW 写手/审查者子代理内再次运行 dacw_run(防递归)');
|
|
919
1115
|
if (!presets.ready && !config.allowInheritedPresets) {
|
|
920
|
-
throw new Error(`[DACW] 写手/审查者专用 preset 不可用(${presets.reason}),拒绝让子代理继承父会话工具运行。` + `请修复 preset 注册(需要 @deepseek-ai/dsh-persona 与 agentPresets 服务),` + `或在明确接受风险后设置 config.allowInheritedPresets: true。`);
|
|
1116
|
+
throw new Error(`[DACW] 写手/审查者专用 preset 不可用(${presets.reason}),拒绝让子代理继承父会话工具运行。` + `请修复 preset 注册(需要 @deepseek-ai/dsh-persona 与 agentPresets 服务),` + `或在明确接受风险后设置 config.allowInheritedPresets: true 与 config.enforceChildToolIsolation: false。`);
|
|
921
1117
|
}
|
|
922
1118
|
const roleModels = {
|
|
923
1119
|
writer: await resolveRoleModel(ctx, config.writerModel, 'writer', logger),
|
|
@@ -955,17 +1151,53 @@ export function apply(ctx, rawConfig) {
|
|
|
955
1151
|
roleModel: role === 'writer' ? roleModels.writer : roleModels.reviewer
|
|
956
1152
|
});
|
|
957
1153
|
},
|
|
1154
|
+
async askUserQuestion (request) {
|
|
1155
|
+
const userQuestions = (()=>{
|
|
1156
|
+
try {
|
|
1157
|
+
return ctx.get('userQuestions');
|
|
1158
|
+
} catch {
|
|
1159
|
+
return undefined;
|
|
1160
|
+
}
|
|
1161
|
+
})();
|
|
1162
|
+
if (!userQuestions || typeof userQuestions.ask !== 'function') {
|
|
1163
|
+
return {
|
|
1164
|
+
decision: 'unavailable',
|
|
1165
|
+
reason: '宿主未提供 ctx.userQuestions 交互服务(无法弹出沙盒确认)'
|
|
1166
|
+
};
|
|
1167
|
+
}
|
|
1168
|
+
try {
|
|
1169
|
+
const answer = await userQuestions.ask({
|
|
1170
|
+
questions: [
|
|
1171
|
+
{
|
|
1172
|
+
id: 'dacw-sandbox-consent',
|
|
1173
|
+
question: request.message,
|
|
1174
|
+
header: request.title,
|
|
1175
|
+
options: request.options.map((option)=>({
|
|
1176
|
+
label: option.label,
|
|
1177
|
+
description: option.description
|
|
1178
|
+
}))
|
|
1179
|
+
}
|
|
1180
|
+
],
|
|
1181
|
+
...agent ? {
|
|
1182
|
+
agent
|
|
1183
|
+
} : {},
|
|
1184
|
+
...signal ? {
|
|
1185
|
+
signal
|
|
1186
|
+
} : {}
|
|
1187
|
+
});
|
|
1188
|
+
return parseSandboxConsentAnswer(answer, request);
|
|
1189
|
+
} catch (error) {
|
|
1190
|
+
if (signal && signal.aborted || isAbortLike(error)) throw abortError();
|
|
1191
|
+
return {
|
|
1192
|
+
decision: 'unavailable',
|
|
1193
|
+
reason: `沙盒确认交互失败:${errorMessage(error)}`
|
|
1194
|
+
};
|
|
1195
|
+
}
|
|
1196
|
+
},
|
|
958
1197
|
log
|
|
959
1198
|
};
|
|
960
1199
|
const executeTask = createEngine(engineContext, {
|
|
961
|
-
|
|
962
|
-
chatTimeoutBaseMs: config.chatTimeoutBaseMs,
|
|
963
|
-
chatTimeoutPer10kCharsMs: config.chatTimeoutPer10kCharsMs,
|
|
964
|
-
chatTimeoutMaxMs: config.chatTimeoutMaxMs,
|
|
965
|
-
chatMaxAttempts: config.chatMaxAttempts,
|
|
966
|
-
writerOutputMaxChars: config.writerOutputMaxChars,
|
|
967
|
-
manifestMaxEntries: config.manifestMaxEntries,
|
|
968
|
-
stateDir: config.stateDir || null,
|
|
1200
|
+
...resolvedEngineOptions,
|
|
969
1201
|
roleModels: roleModelLabels
|
|
970
1202
|
});
|
|
971
1203
|
const rawTaskId = taskIdArg || `dacw-${Date.now().toString(36)}`;
|
|
@@ -979,7 +1211,9 @@ export function apply(ctx, rawConfig) {
|
|
|
979
1211
|
round: typeof result.round === 'number' ? result.round : 0,
|
|
980
1212
|
files: Array.isArray(result.files) ? result.files.map(String) : [],
|
|
981
1213
|
logs: tailLogs(logs, 60, 8000),
|
|
982
|
-
elapsed_ms: Date.now() - startedAt
|
|
1214
|
+
elapsed_ms: Date.now() - startedAt,
|
|
1215
|
+
task_id: rawTaskId,
|
|
1216
|
+
rollback_available: result.rollbackPointCaptured === true
|
|
983
1217
|
};
|
|
984
1218
|
if (!value.success) {
|
|
985
1219
|
const failure = new Error(`[DACW] 工作流未通过(status=${value.status},round=${value.round}):${value.message}\n` + `工作区:${value.workspace}\n\n${renderToolResultText(value)}`);
|
|
@@ -989,6 +1223,149 @@ export function apply(ctx, rawConfig) {
|
|
|
989
1223
|
return value;
|
|
990
1224
|
}
|
|
991
1225
|
};
|
|
1226
|
+
const rollbackToolDefinition = {
|
|
1227
|
+
name: 'dacw_rollback',
|
|
1228
|
+
description: ROLLBACK_TOOL_DESCRIPTION,
|
|
1229
|
+
parameters: {
|
|
1230
|
+
type: 'object',
|
|
1231
|
+
properties: {
|
|
1232
|
+
task_id: {
|
|
1233
|
+
type: 'string',
|
|
1234
|
+
description: '要回滚的任务 ID(dacw_run 返回的 task_id)。'
|
|
1235
|
+
},
|
|
1236
|
+
workspace: {
|
|
1237
|
+
type: 'string',
|
|
1238
|
+
description: '可选,dacw_run 时使用的工作区;省略则用配置的 workspace,再退回当前会话工作目录。'
|
|
1239
|
+
},
|
|
1240
|
+
reason: {
|
|
1241
|
+
type: 'string',
|
|
1242
|
+
description: '可选,回滚原因,记录到审计,限 500 字符。'
|
|
1243
|
+
},
|
|
1244
|
+
confirm: {
|
|
1245
|
+
type: 'boolean',
|
|
1246
|
+
description: '必填。传 true 表示有意调用回滚(高影响操作,必须显式确认)。'
|
|
1247
|
+
}
|
|
1248
|
+
},
|
|
1249
|
+
required: [
|
|
1250
|
+
'task_id',
|
|
1251
|
+
'confirm'
|
|
1252
|
+
],
|
|
1253
|
+
additionalProperties: false
|
|
1254
|
+
},
|
|
1255
|
+
output: {
|
|
1256
|
+
schema: {
|
|
1257
|
+
type: 'object',
|
|
1258
|
+
properties: {
|
|
1259
|
+
success: {
|
|
1260
|
+
type: 'boolean'
|
|
1261
|
+
},
|
|
1262
|
+
mode: {
|
|
1263
|
+
oneOf: [
|
|
1264
|
+
{
|
|
1265
|
+
type: 'string'
|
|
1266
|
+
},
|
|
1267
|
+
{
|
|
1268
|
+
type: 'null'
|
|
1269
|
+
}
|
|
1270
|
+
]
|
|
1271
|
+
},
|
|
1272
|
+
message: {
|
|
1273
|
+
type: 'string'
|
|
1274
|
+
},
|
|
1275
|
+
task_id: {
|
|
1276
|
+
type: 'string'
|
|
1277
|
+
},
|
|
1278
|
+
workspace: {
|
|
1279
|
+
type: 'string'
|
|
1280
|
+
},
|
|
1281
|
+
record: {
|
|
1282
|
+
oneOf: [
|
|
1283
|
+
{
|
|
1284
|
+
type: 'object'
|
|
1285
|
+
},
|
|
1286
|
+
{
|
|
1287
|
+
type: 'null'
|
|
1288
|
+
}
|
|
1289
|
+
]
|
|
1290
|
+
}
|
|
1291
|
+
},
|
|
1292
|
+
required: [
|
|
1293
|
+
'success',
|
|
1294
|
+
'mode',
|
|
1295
|
+
'message',
|
|
1296
|
+
'task_id',
|
|
1297
|
+
'workspace',
|
|
1298
|
+
'record'
|
|
1299
|
+
],
|
|
1300
|
+
additionalProperties: false
|
|
1301
|
+
},
|
|
1302
|
+
render: (_args, value)=>[
|
|
1303
|
+
{
|
|
1304
|
+
type: 'text',
|
|
1305
|
+
text: renderRollbackResultText(value)
|
|
1306
|
+
}
|
|
1307
|
+
]
|
|
1308
|
+
},
|
|
1309
|
+
async execute (rawArgs, exec) {
|
|
1310
|
+
const args = rawArgs && typeof rawArgs === 'object' ? rawArgs : {};
|
|
1311
|
+
const taskId = typeof args.task_id === 'string' ? args.task_id.trim() : '';
|
|
1312
|
+
if (!taskId) throw new Error('[DACW] 参数 task_id 必须是非空字符串');
|
|
1313
|
+
if (taskId.length > MAX_TASK_ID_CHARS) throw new Error(`[DACW] task_id 过长(${taskId.length} > ${MAX_TASK_ID_CHARS})`);
|
|
1314
|
+
if (args.confirm !== true) {
|
|
1315
|
+
throw new Error('[DACW] 回滚是高影响操作。请传 confirm: true 表示有意调用。\n' + 'git 工作区:返回回滚指令,不修改文件。\n' + '非 git 工作区:执行前会请求人工交互确认;宿主无交互能力时需设置 DACW_ROLLBACK_TOKEN。');
|
|
1316
|
+
}
|
|
1317
|
+
const reason = typeof args.reason === 'string' ? args.reason.trim() : '';
|
|
1318
|
+
if (reason.length > 500) throw new Error(`[DACW] reason 过长(${reason.length} > 500)`);
|
|
1319
|
+
const workspaceArg = typeof args.workspace === 'string' ? args.workspace.trim() : '';
|
|
1320
|
+
if (workspaceArg.length > MAX_WORKSPACE_ARG_CHARS) throw new Error(`[DACW] workspace 过长(${workspaceArg.length} > ${MAX_WORKSPACE_ARG_CHARS})`);
|
|
1321
|
+
const agent = exec && exec.agent ? exec.agent : null;
|
|
1322
|
+
if (!agent) throw new Error('[DACW] 只能由 Agent 调用:执行上下文缺少调用方 Agent');
|
|
1323
|
+
if (isDacwChild(agent)) throw new Error('[DACW] 禁止在 DACW 子代理内调用 dacw_rollback');
|
|
1324
|
+
const signal = exec && exec.signal ? exec.signal : undefined;
|
|
1325
|
+
const userQuestionsApi = (()=>{
|
|
1326
|
+
try {
|
|
1327
|
+
const svc = typeof ctx.get === 'function' ? ctx.get('userQuestions') : undefined;
|
|
1328
|
+
return svc && typeof svc.ask === 'function' ? svc : null;
|
|
1329
|
+
} catch {
|
|
1330
|
+
return null;
|
|
1331
|
+
}
|
|
1332
|
+
})();
|
|
1333
|
+
const baseDir = await callerWorkingDirectory(agent, signal);
|
|
1334
|
+
const requested = workspaceArg || config.workspace;
|
|
1335
|
+
const workspaceDir = await prepareWorkspace(baseDir, requested, config.workspacePolicy);
|
|
1336
|
+
const onBeforeExecute = async (info)=>{
|
|
1337
|
+
if (userQuestionsApi) {
|
|
1338
|
+
const confirmed = await askRollbackConfirmation(userQuestionsApi, exec, info);
|
|
1339
|
+
if (!confirmed) throw new Error('[DACW] 用户未确认回滚,已取消执行(工作区未改动)');
|
|
1340
|
+
return;
|
|
1341
|
+
}
|
|
1342
|
+
const token = String(process.env[ROLLBACK_TOKEN_ENV] ?? '').trim();
|
|
1343
|
+
if (!token) {
|
|
1344
|
+
throw new Error(`[DACW] 宿主未提供 userQuestions 交互能力,且未设置 ${ROLLBACK_TOKEN_ENV}:拒绝执行回滚(fail-closed)`);
|
|
1345
|
+
}
|
|
1346
|
+
logger.info(`DACW: 宿主无交互能力,使用 ${ROLLBACK_TOKEN_ENV} 显式授权执行回滚(task_id=${taskId})`);
|
|
1347
|
+
};
|
|
1348
|
+
const stateDir = config.stateDir || path.join(os.tmpdir(), 'dacw-state');
|
|
1349
|
+
const result = await rollbackTask(stateDir, taskId, workspaceDir, reason || undefined, resolvedEngineOptions.stateChecksumFn ?? undefined, onBeforeExecute);
|
|
1350
|
+
const value = {
|
|
1351
|
+
success: result.success,
|
|
1352
|
+
mode: result.mode,
|
|
1353
|
+
message: result.message,
|
|
1354
|
+
task_id: taskId,
|
|
1355
|
+
workspace: workspaceDir,
|
|
1356
|
+
record: result.record ?? null
|
|
1357
|
+
};
|
|
1358
|
+
if (!result.success) {
|
|
1359
|
+
const failure = new Error(`[DACW] 回滚未成功:${result.message}`);
|
|
1360
|
+
failure.dacwRollback = value;
|
|
1361
|
+
throw failure;
|
|
1362
|
+
}
|
|
1363
|
+
logger.info(`DACW: 回滚完成(task_id=${taskId}, mode=${value.mode}, workspace=${workspaceDir})`);
|
|
1364
|
+
return value;
|
|
1365
|
+
}
|
|
1366
|
+
};
|
|
992
1367
|
ctx.effect(()=>ctx.tools.register(toolDefinition));
|
|
993
|
-
|
|
1368
|
+
ctx.effect(()=>ctx.tools.register(rollbackToolDefinition));
|
|
1369
|
+
logger.info(`DACW: 工具 ${config.toolName} 已注册(workspacePolicy=${config.workspacePolicy}, sandbox=${config.sandbox.mode}, ` + `provider=${config.subagentProvider}, enforceChildToolIsolation=${config.enforceChildToolIsolation})`);
|
|
1370
|
+
logger.info('DACW: 工具 dacw_rollback 已注册(回滚到任务开始前;git 工作区仅返回命令提示)');
|
|
994
1371
|
}
|