dsh-workplan 3.0.3 → 3.0.6
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 +54 -0
- package/README.en.md +15 -7
- package/README.md +18 -8
- package/README.zh.md +8 -7
- package/client.js +181 -27
- package/docs/release-notes-3.0.3.md +7 -0
- package/lib/approval-gate.js +79 -3
- package/lib/client.js +181 -27
- package/lib/comm-format.js +1 -1
- package/lib/dept-journal.js +223 -6
- package/lib/host.js +199 -7
- package/lib/lessons.js +83 -15
- package/lib/llm-rules.js +3 -2
- package/lib/notice-ledger.js +238 -0
- package/lib/plan.js +120 -3
- package/lib/runtime.js +440 -28
- package/lib/three-phase.js +6 -2
- package/lib/understanding.js +85 -6
- package/lib/v3-contract.js +45 -12
- package/package.json +1 -1
- package/src/approval-gate.ts +77 -3
- package/src/client.ts +178 -24
- package/src/comm-format.ts +1 -1
- package/src/dept-journal.ts +219 -7
- package/src/host.ts +199 -7
- package/src/lessons.ts +88 -15
- package/src/llm-rules.ts +3 -2
- package/src/notice-ledger.ts +251 -0
- package/src/plan.ts +124 -3
- package/src/runtime.ts +482 -26
- package/src/three-phase.ts +6 -2
- package/src/understanding.ts +100 -6
- package/src/v3-contract.ts +46 -13
package/lib/host.js
CHANGED
|
@@ -14,6 +14,7 @@ import { fileURLToPath } from 'node:url';
|
|
|
14
14
|
import { applyHardeningHeaders, cancelToolOutputSchema, createWorkplanRuntime, dispatchToolOutputSchema, plannerOutputSchema, replanToolOutputSchema, statusToolOutputSchema, updateToolOutputSchema, } from './runtime.js';
|
|
15
15
|
import { DEPARTMENT_IDS, isRecord } from './plan.js';
|
|
16
16
|
import { autoResumeEnabledFrom } from './auto-resume.js';
|
|
17
|
+
import { pruneJournal } from './dept-journal.js';
|
|
17
18
|
/*
|
|
18
19
|
* [1.8.14] Identity of the modules this process actually loaded (N1).
|
|
19
20
|
*
|
|
@@ -54,6 +55,13 @@ const LOADED_BUILD = (() => {
|
|
|
54
55
|
* "restarted after the fix" from "same process, rebuilt files on disk".
|
|
55
56
|
*/
|
|
56
57
|
const PROCESS_STARTED_AT = Date.now() - Math.round(process.uptime() * 1000);
|
|
58
|
+
/*
|
|
59
|
+
* [3.0.6][第 2 批] 静态图表资产只读一次。
|
|
60
|
+
* 旧实现每次请求都 readFileSync 210KB(看板每次加载都拉一次),在请求路径上
|
|
61
|
+
* 做磁盘 I/O 没有必要:vendor 文件随插件发布、运行期不会变。缓存 Buffer 后
|
|
62
|
+
* 后续请求直接复用;读取失败不缓存,下次仍会重试。
|
|
63
|
+
*/
|
|
64
|
+
let chartVendorBytes = null;
|
|
57
65
|
function phaseLabel(phase) {
|
|
58
66
|
if (phase === 'paused')
|
|
59
67
|
return '本轮预算用尽,可续跑';
|
|
@@ -85,7 +93,7 @@ const PROMPT_SECTION = [
|
|
|
85
93
|
'3.0.0 任务书质量要求(用户要求 3/6):插件派给部门负责人的每个子任务都携带 qualityRequirements(结果质量要求),部门负责人派给专家的每条任务也必须携带 qualityRequirements。负责人→专家缺它平台直接拒绝派发(DELEGATE_QUALITY_REQUIREMENTS_MISSING);主 Agent→部门负责人缺它则该子任务在派发时被直接拒发(DISPATCH_QUALITY_REQUIREMENTS_MISSING,不启动部门 Agent),须 replan 补齐后重派。你审阅计划时先看每个子任务是否写了清楚的质量要求。',
|
|
86
94
|
'3.0.0 极简任务(用户要求 5,用户裁决 A=硬门槛):部门负责人派出的每条任务必须简单到像 1+1=2——一个动作、一个主产物、一项可独立验收,单句、≤300 字符、不含「并且/同时/另外/还要/以及/分别/顺便」等并列词;不满足即被拒绝并给出拆分提示(DELEGATE_TASK_NOT_ATOMIC)。',
|
|
87
95
|
'3.0.0 角色与汇报(用户要求 7/8/9/10/11/12):部门组员角色已取消,只保留部门负责人与全球顶尖专家(role 只允许 expert);专家完成后必须以 JSON 汇报(证据块里的 report:keyResults/keyInfo/importantInfo/leadRequirements,精简、禁止重复,F10);部门负责人必须全面汇总本部门全部专家汇报后,以 JSON 交回主 Agent(departmentSummary:keyResults/keyInfo/importantInfo/sources,精简、禁止累赘重复,F11);等所有部门的汇总都交回后,由你再全面、深入整理汇总成最终成果交付给用户(用户要求 12)。',
|
|
88
|
-
'3.0.
|
|
96
|
+
'3.0.6 版本(用户要求 13):本插件当前版本为 3.0.6。涉及版本号的对外表述一律写 3.0.6。',
|
|
89
97
|
/*
|
|
90
98
|
* [2.5.1] 主 Agent 两轮深度思考协议(用户 2026-11-11 补充要求):部门三层
|
|
91
99
|
* 执行者已有结算/再分配双门禁,主 Agent 自己也必须做到同一协议——提示词
|
|
@@ -664,8 +672,9 @@ function createEscalateTool(runtime) {
|
|
|
664
672
|
name: 'dsh_workplan_escalate',
|
|
665
673
|
description: '子 Agent 遇阻时向父会话上报请示,并把该子任务置为「待指示」。'
|
|
666
674
|
+ '第三层专家(叶子)同样可用:请示会经 delegate 登记落到所属部门行的第三层台账,由部门负责人用 dsh_workplan_instruct 答复。'
|
|
675
|
+
+ '3.0.5 硬规则:第三层专家上报后,部门负责人禁止另派给其他专家,必须梳理该问题与本部门全部任务执行状态、研究方案后,用 dsh_workplan_instruct 答复原专家,原专家继续执行。'
|
|
667
676
|
+ '遇阻(缺权限/缺输入/依赖产物缺失/需求自相矛盾/无法验证/同一命令连续两次失败/需要主 Agent 决定方向)时,'
|
|
668
|
-
+ '第一动作是调用本工具,然后停止推进、只做只读核对,等待主 Agent
|
|
677
|
+
+ '第一动作是调用本工具,然后停止推进、只做只读核对,等待主 Agent/负责人指示。'
|
|
669
678
|
+ '不需要外部决定时不要调用;调用后不要自行选定一个选项继续。',
|
|
670
679
|
parameters: {
|
|
671
680
|
type: 'object',
|
|
@@ -718,7 +727,7 @@ function createEscalateTool(runtime) {
|
|
|
718
727
|
return textContent(`${String(result.subtaskId ?? '')} 已登记为「待指示」`
|
|
719
728
|
+ (result.alreadyEscalated ? '(此前已登记,未重复提交)' : '')
|
|
720
729
|
+ `:${String(result.question ?? '')}`
|
|
721
|
-
+ '|现在停止推进,只做只读核对;等待主 Agent
|
|
730
|
+
+ '|现在停止推进,只做只读核对;等待主 Agent/部门负责人指示后再继续。第三层请示在答复前禁止改派给其他专家。');
|
|
722
731
|
},
|
|
723
732
|
},
|
|
724
733
|
async execute(args, exec) {
|
|
@@ -767,7 +776,8 @@ function createDelegateTool(runtime) {
|
|
|
767
776
|
+ '平台按当前模型家族合法集合自动钳制越界档并留痕,等级在其任务全生命周期生效。'
|
|
768
777
|
+ 'action="status" 查看梳理登记与下级进度。第三层专家叶子用 action="self_effort" 自配推理等级并提交执行前两轮深度思考产物'
|
|
769
778
|
+ '(先配置+先思考后开工:phase-0 以 low 启动,唯一职责=评估自身任务复杂度并提交 reasoningEffort+complexityRationale+deepThink{problemAnalysis,approach,approachBasis}+开工前预估 timeNotice{declaredMinutes:正整数分钟,reason?};'
|
|
770
|
-
+ '缺交/不合格 deepThink 硬拒 DEEP_THINK_MISSING/DEEP_THINK_INCOMPLETE、不接续;两项齐全登记后平台立即以自配档接续同一任务进入 phase-1,正式执行必须按第二轮方法流程步骤进行、全程少走弯路;成员自配为权威、分工表档仅建议)。'
|
|
779
|
+
+ '缺交/不合格 deepThink 硬拒 DEEP_THINK_MISSING/DEEP_THINK_INCOMPLETE、不接续;两项齐全登记后平台立即以自配档接续同一任务进入 phase-1,正式执行必须按第二轮方法流程步骤进行、全程少走弯路;成员自配为权威、分工表档仅建议)。'
|
|
780
|
+
+ '3.0.5 硬规则:任一第三层成员处于待指示(escalated)时,review/dispatch/direct 一律拒绝改派、重派或降级;负责人必须梳理该成员问题与本部门全部任务状态,研究方案后用 dsh_workplan_instruct 答复原成员,原成员继续执行。',
|
|
771
781
|
parameters: {
|
|
772
782
|
type: 'object',
|
|
773
783
|
properties: {
|
|
@@ -1115,6 +1125,7 @@ function createInstructTool(runtime) {
|
|
|
1115
1125
|
name: 'dsh_workplan_instruct',
|
|
1116
1126
|
description: '向一条「待指示」子任务下发决定:投递给对应子会话,并把该行恢复为执行中(closure=instructed)。'
|
|
1117
1127
|
+ '根会话可答复第一层部门行;部门负责人可答复自己派出的第三层专家(subtaskId 填自己的部门行 ID 或省略,用 member 或 sessionId 指定目标,二者至少其一)。'
|
|
1128
|
+
+ '3.0.5 硬规则:第三层只能答复已经提交请示的原专家,投递成功后必须原专家继续执行;待指示期间负责人不得 review/dispatch/direct 改派。'
|
|
1118
1129
|
+ '只有目标子会话的直接父会话可调用,非直系一律拒绝;根路径只接受 closure=escalated 的行。'
|
|
1119
1130
|
+ '投递失败时状态不变(仍是待指示),按返回的 hint 重试或改用 dsh_workplan_update 明确放弃。',
|
|
1120
1131
|
parameters: {
|
|
@@ -1743,7 +1754,47 @@ export function apply(ctx, config = {}) {
|
|
|
1743
1754
|
* 运行上下文(提供 `headlessStartup` 服务)才硬失败,交互式会话不受影响。
|
|
1744
1755
|
*/
|
|
1745
1756
|
failClosedOnIncomplete: failClosedOnIncompleteFrom(config.failClosedOnIncomplete, settingEnv('DSH_WORKPLAN_FAIL_CLOSED_ON_INCOMPLETE')),
|
|
1757
|
+
turnStoppingMaxWaitMs: numericSetting(config.turnStoppingMaxWaitMs, 'DSH_WORKPLAN_TURN_STOPPING_MAX_WAIT_MS', true),
|
|
1746
1758
|
});
|
|
1759
|
+
/*
|
|
1760
|
+
* [3.0.6][第 2 批] 可选留痕清理:默认关闭(0 = 永久保留证据)。
|
|
1761
|
+
* 只有显式配置正数时才启用;启动先跑一次,之后每 24 小时一次。
|
|
1762
|
+
*/
|
|
1763
|
+
const journalRetentionDays = (() => {
|
|
1764
|
+
const configured = numericSetting(config.journalRetentionDays, 'DSH_WORKPLAN_JOURNAL_RETENTION_DAYS', true);
|
|
1765
|
+
return configured !== undefined && configured > 0 ? Math.min(configured, 3650) : 0;
|
|
1766
|
+
})();
|
|
1767
|
+
if (journalRetentionDays > 0) {
|
|
1768
|
+
const prune = () => {
|
|
1769
|
+
const result = pruneJournal({ retentionDays: journalRetentionDays, excludePlanIds: activePlanIdSet(runtime) });
|
|
1770
|
+
if (!result.ok)
|
|
1771
|
+
ctx.logger?.warn?.(`[dsh-workplan] journal 清理失败:${result.error}`);
|
|
1772
|
+
// 删除失败逐条 warn(ok 仍为 true,errors[] 是弱错误通道,不得静默)。
|
|
1773
|
+
if (Array.isArray(result.errors) && result.errors.length > 0) {
|
|
1774
|
+
for (const line of result.errors)
|
|
1775
|
+
console.warn(`[dsh-workplan] journal 清理异常:${String(line)}`);
|
|
1776
|
+
}
|
|
1777
|
+
return result;
|
|
1778
|
+
};
|
|
1779
|
+
ctx.effect(() => {
|
|
1780
|
+
/*
|
|
1781
|
+
* [复审修复] 首次清理从激活同步路径挪到 10s 后:激活是同步路径,不能
|
|
1782
|
+
* 当场扫盘(readdir/stat 大目录会拖住插件注册)。卸载时清除——
|
|
1783
|
+
* journalRetentionTimer 同款的 ctx.effect 清理模式。
|
|
1784
|
+
*/
|
|
1785
|
+
const firstPruneTimer = setTimeout(prune, 10_000);
|
|
1786
|
+
if (typeof firstPruneTimer?.unref === 'function')
|
|
1787
|
+
firstPruneTimer.unref();
|
|
1788
|
+
return () => clearTimeout(firstPruneTimer);
|
|
1789
|
+
});
|
|
1790
|
+
ctx.effect(() => {
|
|
1791
|
+
// 卫生类:不触发只少一次清理,不得拖住正在关闭的宿主。
|
|
1792
|
+
const journalRetentionTimer = setInterval(prune, 24 * 60 * 60 * 1000);
|
|
1793
|
+
if (typeof journalRetentionTimer?.unref === 'function')
|
|
1794
|
+
journalRetentionTimer.unref();
|
|
1795
|
+
return () => clearInterval(journalRetentionTimer);
|
|
1796
|
+
});
|
|
1797
|
+
}
|
|
1747
1798
|
ctx.effect(() => ctx.systemPrompt.section({
|
|
1748
1799
|
name: 'dsh-workplan',
|
|
1749
1800
|
order: PROMPT_ORDER,
|
|
@@ -1847,7 +1898,10 @@ export function apply(ctx, config = {}) {
|
|
|
1847
1898
|
return;
|
|
1848
1899
|
}
|
|
1849
1900
|
try {
|
|
1850
|
-
|
|
1901
|
+
if (!chartVendorBytes) {
|
|
1902
|
+
chartVendorBytes = readFileSync(new URL('../vendor/chart.umd.js', import.meta.url));
|
|
1903
|
+
}
|
|
1904
|
+
const body = chartVendorBytes;
|
|
1851
1905
|
res.statusCode = 200;
|
|
1852
1906
|
res.setHeader('content-type', 'text/javascript; charset=utf-8');
|
|
1853
1907
|
res.setHeader('cache-control', 'public, max-age=3600');
|
|
@@ -1862,10 +1916,16 @@ export function apply(ctx, config = {}) {
|
|
|
1862
1916
|
}));
|
|
1863
1917
|
});
|
|
1864
1918
|
ctx.effect(() => () => runtime.dispose());
|
|
1919
|
+
/*
|
|
1920
|
+
* [3.0.6][复审修复] 把 runtime 句柄交还给调用方:宿主契约不消费本返回值
|
|
1921
|
+
* (清理一律经上方 effect),测试与宿主编排需要观察 state.records /
|
|
1922
|
+
* 替换 startAutoDispatch 时可直接拿到同一实例,不必从工具闭包里挖。
|
|
1923
|
+
*/
|
|
1924
|
+
return runtime;
|
|
1865
1925
|
}
|
|
1866
1926
|
export const meta = {
|
|
1867
1927
|
name,
|
|
1868
|
-
version: '3.0.
|
|
1928
|
+
version: '3.0.6',
|
|
1869
1929
|
promptOrder: PROMPT_ORDER,
|
|
1870
1930
|
routes: ['/dsh-workplan/plans', '/dsh-workplan/vendor/chart.umd.js'],
|
|
1871
1931
|
};
|
|
@@ -1878,7 +1938,14 @@ export { PROMPT_SECTION, createCancelTool, createDelegateTool, createDispatchToo
|
|
|
1878
1938
|
* 取第一个非空原因,互不覆盖。
|
|
1879
1939
|
*/
|
|
1880
1940
|
export function fileWriteGuardReason(exec, runtime) {
|
|
1881
|
-
|
|
1941
|
+
/*
|
|
1942
|
+
* [复审修复] bash 写保护绕过:shell 类工具的 command 字符串(重定向/heredoc/
|
|
1943
|
+
* tee/sed -i/cp/mv)此前因参数形状不认(无 file_path/path)而整类绕过闸门。
|
|
1944
|
+
* 先解析出 shell 写目标(非 shell 调用为 null),形状闸门与 shell 闸门至少
|
|
1945
|
+
* 命中其一才继续。
|
|
1946
|
+
*/
|
|
1947
|
+
const shellTargets = shellWriteTargets(exec);
|
|
1948
|
+
if (!isWriteShapedCall(exec) && shellTargets === null)
|
|
1882
1949
|
return undefined;
|
|
1883
1950
|
// The three historical write tools pass their arguments through untouched, so
|
|
1884
1951
|
// their behaviour stays exactly as before (argument-shape matching is only an
|
|
@@ -1894,8 +1961,48 @@ export function fileWriteGuardReason(exec, runtime) {
|
|
|
1894
1961
|
if (typeof extra === 'string')
|
|
1895
1962
|
return extra;
|
|
1896
1963
|
}
|
|
1964
|
+
/*
|
|
1965
|
+
* [复审修复] shell 写目标:与上面同一判定出口——只拦「写入已交付文件」,
|
|
1966
|
+
* 纯读命令(cat/grep/less 该路径)解析不出写目标,天然放行。
|
|
1967
|
+
* 启发式局限(保守模式):只认 > / >> 重定向、tee、sed -i、mv/cp/install/dd
|
|
1968
|
+
* 的目标;命令拼接变量(cmd=path; $cmd)、eval、find -delete 等动态写法
|
|
1969
|
+
* 不在解析范围内——见 shellWriteTargets 注释。
|
|
1970
|
+
*/
|
|
1971
|
+
if (shellTargets) {
|
|
1972
|
+
for (const target of shellTargets) {
|
|
1973
|
+
const extra = runtime.presentedWriteDenialReason(exec?.agent, { file_path: target });
|
|
1974
|
+
if (typeof extra === 'string')
|
|
1975
|
+
return extra;
|
|
1976
|
+
}
|
|
1977
|
+
}
|
|
1897
1978
|
return undefined;
|
|
1898
1979
|
}
|
|
1980
|
+
/*
|
|
1981
|
+
* [复审修复] 活跃 planId 集合:pruneJournal 只看 mtime,「活跃但安静」的计划
|
|
1982
|
+
* (久无新留痕、mtime 变旧)会被误删;把当前活跃 planId 传给 excludePlanIds
|
|
1983
|
+
* 即可保住。planning 在途时尚未铸出 planId(planId 在规划完成后才生成,占位
|
|
1984
|
+
* 记录 planId=null),因此 runtime.state.records 就是全部活跃 planId 的事实源。
|
|
1985
|
+
* 导出以便直接单测;读取失败返回空集——宁可少排除,也不让清理路径抛错。
|
|
1986
|
+
*/
|
|
1987
|
+
export function activePlanIdSet(runtime) {
|
|
1988
|
+
const ids = new Set();
|
|
1989
|
+
try {
|
|
1990
|
+
const records = runtime?.state?.records;
|
|
1991
|
+
if (records instanceof Map) {
|
|
1992
|
+
for (const record of records.values()) {
|
|
1993
|
+
const direct = typeof record?.planId === 'string' ? record.planId.trim() : '';
|
|
1994
|
+
const nested = typeof record?.plan?.planId === 'string' ? record.plan.planId.trim() : '';
|
|
1995
|
+
const id = direct !== '' ? direct : (nested !== '' ? nested : null);
|
|
1996
|
+
if (id)
|
|
1997
|
+
ids.add(id);
|
|
1998
|
+
}
|
|
1999
|
+
}
|
|
2000
|
+
}
|
|
2001
|
+
catch {
|
|
2002
|
+
// 同上:名单读取失败不阻断清理。
|
|
2003
|
+
}
|
|
2004
|
+
return ids;
|
|
2005
|
+
}
|
|
1899
2006
|
/**
|
|
1900
2007
|
* [1.12.2] G2: recognise write calls by their argument shape, not by a tool-name
|
|
1901
2008
|
* allow-list. The 1.8.18 version named write/edit/str_replace_editor only, so a
|
|
@@ -1925,3 +2032,88 @@ function isWriteShapedCall(exec) {
|
|
|
1925
2032
|
// Every other write form names a single target: file_path or path.
|
|
1926
2033
|
return typeof args.file_path === 'string' || typeof args.path === 'string';
|
|
1927
2034
|
}
|
|
2035
|
+
/*
|
|
2036
|
+
* [复审修复] shell 类工具的识别:exec.name 为 bash/shell/exec,或以它们为
|
|
2037
|
+
* 前缀且前缀后是 [-_] 分隔符或结尾(bash_exec、shell-tool 等宿主工具名变体)。
|
|
2038
|
+
* 要求分隔符是为了避免 'executor' 这类普通名字被误判进 shell 闸门。
|
|
2039
|
+
*/
|
|
2040
|
+
function isShellLikeToolName(name) {
|
|
2041
|
+
if (typeof name !== 'string' || name === '')
|
|
2042
|
+
return false;
|
|
2043
|
+
if (name === 'bash' || name === 'shell' || name === 'exec')
|
|
2044
|
+
return true;
|
|
2045
|
+
return /^(?:bash|shell|exec)(?:[-_]|$)/.test(name);
|
|
2046
|
+
}
|
|
2047
|
+
/*
|
|
2048
|
+
* [复审修复] 从 shell 调用的 command 字符串解析「写目标路径」。
|
|
2049
|
+
*
|
|
2050
|
+
* 返回 null = 这不是 shell 类调用(或没有 command 参数),调用方据此跳过
|
|
2051
|
+
* shell 闸门;返回数组则可能为空(shell 调用但不含写形态,如纯读的
|
|
2052
|
+
* cat/grep/less)——空数组同样放行。多提取无害:每个候选最终仍走
|
|
2053
|
+
* presentedWriteDenialReason 的「已交付且计划未就绪」判定,普通路径直接
|
|
2054
|
+
* 放行;要防的是漏提取,所以解析宁可宽进。
|
|
2055
|
+
*
|
|
2056
|
+
* 保守模式只认这些强写信号:
|
|
2057
|
+
* - `>` / `>>` 重定向(含 heredoc:`cat <<EOF > path` 的重定向部分);
|
|
2058
|
+
* - `tee [-a] files...`(tee 是纯写管道,必须整组检查);
|
|
2059
|
+
* - `sed -i[.bak] ... files`(段内 -i 之后的全部非选项 token);
|
|
2060
|
+
* - `mv/cp/install 源... 目标`(只取最后一个非选项 token——源是读,只有
|
|
2061
|
+
* 目标是写,全取会把「复制自受保护文件」误拦成写);
|
|
2062
|
+
* - `dd of=path`。
|
|
2063
|
+
*
|
|
2064
|
+
* 启发式局限(有意保守,备查):
|
|
2065
|
+
* - 变量间接(cmd="> $f"; eval、sh -c 嵌套命令)不做展开;
|
|
2066
|
+
* - find -delete、perl -i、crontab 等少见写法不覆盖;
|
|
2067
|
+
* - 引号内带空格的路径按空白截断,不做完整 shell 词法分析;
|
|
2068
|
+
* - 超长命令只解析前 20k 字符。
|
|
2069
|
+
*/
|
|
2070
|
+
const SHELL_COMMAND_PARSE_LIMIT = 20_000;
|
|
2071
|
+
function shellWriteTargets(exec) {
|
|
2072
|
+
if (!isShellLikeToolName(exec?.name))
|
|
2073
|
+
return null;
|
|
2074
|
+
const command = exec?.arguments?.command;
|
|
2075
|
+
if (typeof command !== 'string' || command.trim() === '')
|
|
2076
|
+
return null;
|
|
2077
|
+
const text = command.length > SHELL_COMMAND_PARSE_LIMIT ? command.slice(0, SHELL_COMMAND_PARSE_LIMIT) : command;
|
|
2078
|
+
const targets = new Set();
|
|
2079
|
+
const stripQuotes = (token) => token.replace(/^['"]+|['"]+$/g, '');
|
|
2080
|
+
const isOption = (token) => token === '-' || token.startsWith('-') || token === '--';
|
|
2081
|
+
const add = (token) => {
|
|
2082
|
+
const cleaned = stripQuotes(String(token ?? '')).trim();
|
|
2083
|
+
if (cleaned !== '')
|
|
2084
|
+
targets.add(cleaned);
|
|
2085
|
+
};
|
|
2086
|
+
// > / >> 重定向(兼容 fd 前缀 2>file、1> file;&> 合并重定向跳过——歧义大)。
|
|
2087
|
+
for (const match of text.matchAll(/(?<![&\d])\d*>>?\s*([^\s;|&<>]+)/g))
|
|
2088
|
+
add(match[1]);
|
|
2089
|
+
// 按命令段分隔:; | &&。单 &(2>&1 的合并目标)不拆,避免把 fd 描述切碎。
|
|
2090
|
+
for (const segment of text.split(/&&|[;|]/)) {
|
|
2091
|
+
const trimmed = segment.trim();
|
|
2092
|
+
if (trimmed === '')
|
|
2093
|
+
continue;
|
|
2094
|
+
// tee [-a] files...:tee 之后到段尾的全部 token 都是写目标。
|
|
2095
|
+
const tee = /(?:^|\s)tee(?:\s+-[a-zA-Z]+)*\s+(\S(?:[\s\S]*?))?$/.exec(trimmed);
|
|
2096
|
+
if (tee && /\btee\b/.test(trimmed)) {
|
|
2097
|
+
for (const token of (tee[1] ?? '').split(/\s+/))
|
|
2098
|
+
if (!isOption(token))
|
|
2099
|
+
add(token);
|
|
2100
|
+
}
|
|
2101
|
+
// sed -i[.bak] ...:段内含 sed 且带 -i 选项时,取其后的全部非选项 token。
|
|
2102
|
+
const sed = /(?:^|\s)sed(?:\s+-[a-zA-Z]+)*\s+(-i\S*)(?:\s+([\s\S]*))?$/.exec(trimmed);
|
|
2103
|
+
if (sed) {
|
|
2104
|
+
for (const token of (sed[2] ?? '').split(/\s+/))
|
|
2105
|
+
if (!isOption(token))
|
|
2106
|
+
add(token);
|
|
2107
|
+
}
|
|
2108
|
+
// mv/cp/install 源... 目标:只取最后一个非选项 token(目标是写、源是读)。
|
|
2109
|
+
const mover = /(?:^|\s)(?:mv|cp|install)(?:\s+-[a-zA-Z]+)*\s+([\s\S]+)$/.exec(trimmed);
|
|
2110
|
+
if (mover) {
|
|
2111
|
+
const tokens = (mover[1] ?? '').split(/\s+/).filter((token) => !isOption(token));
|
|
2112
|
+
add(tokens[tokens.length - 1]);
|
|
2113
|
+
}
|
|
2114
|
+
// dd of=path(of= 后到空白为止)。
|
|
2115
|
+
for (const match of trimmed.matchAll(/(?:^|\s)of=([^\s;|&]+)/g))
|
|
2116
|
+
add(match[1]);
|
|
2117
|
+
}
|
|
2118
|
+
return [...targets];
|
|
2119
|
+
}
|
package/lib/lessons.js
CHANGED
|
@@ -18,7 +18,7 @@
|
|
|
18
18
|
* 本模块不依赖 harness 服务,只做纯函数 + 同步文件读写,便于离线回放与测试。
|
|
19
19
|
*/
|
|
20
20
|
import { createHash } from 'node:crypto';
|
|
21
|
-
import { appendFileSync, existsSync, mkdirSync, readFileSync, renameSync, writeFileSync } from 'node:fs';
|
|
21
|
+
import { appendFileSync, chmodSync, copyFileSync, existsSync, lstatSync, mkdirSync, readFileSync, renameSync, writeFileSync } from 'node:fs';
|
|
22
22
|
import { homedir } from 'node:os';
|
|
23
23
|
import { dirname, join } from 'node:path';
|
|
24
24
|
export const LESSON_VERSION = 1;
|
|
@@ -141,17 +141,18 @@ export function resolveLessonsFile(env = process.env) {
|
|
|
141
141
|
return override;
|
|
142
142
|
return join(homedir(), '.dsh', 'workplan', 'lessons.jsonl');
|
|
143
143
|
}
|
|
144
|
-
function
|
|
144
|
+
function readJsonLinesSnapshot(file) {
|
|
145
145
|
if (!existsSync(file))
|
|
146
|
-
return [];
|
|
146
|
+
return { rows: [], corrupt: [], readError: null };
|
|
147
147
|
let text = '';
|
|
148
148
|
try {
|
|
149
149
|
text = readFileSync(file, 'utf8');
|
|
150
150
|
}
|
|
151
|
-
catch {
|
|
152
|
-
return [];
|
|
151
|
+
catch (readError) {
|
|
152
|
+
return { rows: [], corrupt: [], readError };
|
|
153
153
|
}
|
|
154
154
|
const rows = [];
|
|
155
|
+
const corrupt = [];
|
|
155
156
|
for (const line of text.split('\n')) {
|
|
156
157
|
const trimmed = line.trim();
|
|
157
158
|
if (!trimmed)
|
|
@@ -160,12 +161,18 @@ function readJsonLines(file) {
|
|
|
160
161
|
const parsed = JSON.parse(trimmed);
|
|
161
162
|
if (isRecord(parsed))
|
|
162
163
|
rows.push(parsed);
|
|
164
|
+
else
|
|
165
|
+
corrupt.push(trimmed);
|
|
163
166
|
}
|
|
164
167
|
catch {
|
|
165
|
-
//
|
|
168
|
+
// 单行损坏不能拖垮整份记忆:读端跳过该行,写端原样保留。
|
|
169
|
+
corrupt.push(trimmed);
|
|
166
170
|
}
|
|
167
171
|
}
|
|
168
|
-
return rows;
|
|
172
|
+
return { rows, corrupt, readError: null };
|
|
173
|
+
}
|
|
174
|
+
function readJsonLines(file) {
|
|
175
|
+
return readJsonLinesSnapshot(file).rows;
|
|
169
176
|
}
|
|
170
177
|
function coerceRecord(raw) {
|
|
171
178
|
if (!isRecord(raw))
|
|
@@ -196,12 +203,54 @@ export function readLessons(file = resolveLessonsFile()) {
|
|
|
196
203
|
.map(coerceRecord)
|
|
197
204
|
.filter((item) => item !== null);
|
|
198
205
|
}
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
206
|
+
/**
|
|
207
|
+
* [3.0.3] 防符号链接投毒:写入前确认目标不是符号链接,否则抛错拒绝
|
|
208
|
+
* (风格同 LESSONS_READ_FAILED)。攻击者把教训文件或临时文件路径换成
|
|
209
|
+
* 符号链接后,一次「原子重写」就会被重定向去覆盖无关文件。
|
|
210
|
+
*/
|
|
211
|
+
function assertLessonsWriteTarget(file, what) {
|
|
212
|
+
let stat;
|
|
213
|
+
try {
|
|
214
|
+
stat = lstatSync(file);
|
|
215
|
+
}
|
|
216
|
+
catch {
|
|
217
|
+
return; // 不存在:正常新写。
|
|
218
|
+
}
|
|
219
|
+
if (stat.isSymbolicLink()) {
|
|
220
|
+
throw new Error(`LESSONS_WRITE_REFUSED: 拒绝向符号链接${what}写入 ${file},以免被重定向覆盖无关文件`);
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
function writeLessonsAtomic(file, records, corrupt = []) {
|
|
224
|
+
assertLessonsWriteTarget(file, '');
|
|
225
|
+
assertLessonsWriteTarget(`${file}.bak`, '备份');
|
|
226
|
+
mkdirSync(dirname(file), { recursive: true, mode: 0o700 });
|
|
227
|
+
const lines = [
|
|
228
|
+
...records.map((item) => JSON.stringify(item)),
|
|
229
|
+
// 坏行原样留在文件末尾:下次读端仍会跳过,但绝不会因为一次重写就消失。
|
|
230
|
+
...corrupt,
|
|
231
|
+
];
|
|
232
|
+
const body = lines.length > 0 ? `${lines.join('\n')}\n` : '';
|
|
233
|
+
// 覆盖前留一代备份;备份失败不阻断主写入(主写入仍走临时文件 + 原子改名)。
|
|
234
|
+
if (existsSync(file)) {
|
|
235
|
+
try {
|
|
236
|
+
copyFileSync(file, `${file}.bak`);
|
|
237
|
+
}
|
|
238
|
+
catch {
|
|
239
|
+
// 备份是尽力而为:权限不足等情况下,主写入的失败会由调用方显式上报。
|
|
240
|
+
}
|
|
241
|
+
}
|
|
202
242
|
const tmp = `${file}.tmp-${process.pid}`;
|
|
203
|
-
|
|
243
|
+
assertLessonsWriteTarget(tmp, '临时文件');
|
|
244
|
+
writeFileSync(tmp, body, { encoding: 'utf8', mode: 0o600 });
|
|
204
245
|
renameSync(tmp, file);
|
|
246
|
+
// 已存在文件收紧到 0o600(rename 覆盖了同名符号链接之外的目标时,
|
|
247
|
+
// 权限可能带着旧文件的宽松位):best-effort,文件系统不支持时不断写。
|
|
248
|
+
try {
|
|
249
|
+
chmodSync(file, 0o600);
|
|
250
|
+
}
|
|
251
|
+
catch {
|
|
252
|
+
// best-effort。
|
|
253
|
+
}
|
|
205
254
|
}
|
|
206
255
|
/**
|
|
207
256
|
* 把一次复盘写入永久记忆。
|
|
@@ -221,7 +270,22 @@ export function recordLesson(input) {
|
|
|
221
270
|
problems: retro.problems,
|
|
222
271
|
prevention: retro.prevention,
|
|
223
272
|
});
|
|
224
|
-
const
|
|
273
|
+
const snapshot = readJsonLinesSnapshot(file);
|
|
274
|
+
/*
|
|
275
|
+
* 数据安全红线:读失败 ≠ 没有历史。
|
|
276
|
+
* 以前这里把 EACCES/EMFILE/EIO 当成空数组,随后「读-改-写」会把整份永久记忆
|
|
277
|
+
* 覆盖成刚写入的这一条。现在读失败直接抛错,由 runtime 的 catch 记录到状态里;
|
|
278
|
+
* 坏行则原样带回写端,重写时不会丢。
|
|
279
|
+
*/
|
|
280
|
+
if (snapshot.readError) {
|
|
281
|
+
const message = snapshot.readError instanceof Error
|
|
282
|
+
? snapshot.readError.message
|
|
283
|
+
: String(snapshot.readError);
|
|
284
|
+
throw new Error(`LESSONS_READ_FAILED: 无法读取 ${file},已拒绝写入以免覆盖历史教训(${message})`);
|
|
285
|
+
}
|
|
286
|
+
const records = snapshot.rows
|
|
287
|
+
.map(coerceRecord)
|
|
288
|
+
.filter((item) => item !== null);
|
|
225
289
|
const sameRow = records.findIndex((item) => item.planId === input.planId && item.subtaskId === input.subtaskId);
|
|
226
290
|
const sameIssue = records.findIndex((item) => item.fingerprint === fingerprint && item.department === input.department);
|
|
227
291
|
const base = {
|
|
@@ -262,11 +326,15 @@ export function recordLesson(input) {
|
|
|
262
326
|
// 时间正序保留最近 N 条;id 冲突(同一毫秒重复)时无需特殊处理,读端按行。
|
|
263
327
|
const kept = records.slice(Math.max(0, records.length - MAX_LESSON_RECORDS));
|
|
264
328
|
try {
|
|
265
|
-
writeLessonsAtomic(file, kept);
|
|
329
|
+
writeLessonsAtomic(file, kept, snapshot.corrupt);
|
|
266
330
|
}
|
|
267
|
-
catch {
|
|
331
|
+
catch (error) {
|
|
332
|
+
// 符号链接投毒是拒绝不是故障:不上 append 兜底,直接向上抛。
|
|
333
|
+
if (error instanceof Error && error.message.startsWith('LESSONS_WRITE_REFUSED'))
|
|
334
|
+
throw error;
|
|
268
335
|
try {
|
|
269
|
-
mkdirSync(dirname(file), { recursive: true });
|
|
336
|
+
mkdirSync(dirname(file), { recursive: true, mode: 0o700 });
|
|
337
|
+
assertLessonsWriteTarget(file, '');
|
|
270
338
|
appendFileSync(file, `${JSON.stringify(base)}\n`, 'utf8');
|
|
271
339
|
}
|
|
272
340
|
catch {
|
package/lib/llm-rules.js
CHANGED
|
@@ -111,11 +111,12 @@ export function buildLlmRulesBrief(info = {}) {
|
|
|
111
111
|
* 与代码门禁同源(planDelegations 的 checkAtomicAssignment、comm-format 的
|
|
112
112
|
* F10/F11),提示词只是把代码已经在执行的规则提前说清楚。
|
|
113
113
|
*/
|
|
114
|
-
lines.push('- 极简任务硬门槛(用户要求 5,用户裁决 A=硬门槛):你派出的每一条任务必须简单到像 1+1=2
|
|
114
|
+
lines.push('- 极简任务硬门槛(用户要求 5,用户裁决 A=硬门槛):你派出的每一条任务必须简单到像 1+1=2——一个动作、一个主产物、一项可独立验收;出现「并且/并/同时/另外/还要/以及/分别/顺便/然后/接着/再」或分号并列、或一个任务里写了多句(含英文句点分隔的多句)、或超过 300 字符,平台直接拒绝派发(DELEGATE_TASK_NOT_ATOMIC)并给出拆分提示,实例:不要派「分析并修复并写测试」,要拆成「读 X 定位原因」「按原因改 Y」「写 Z 用例」三条。');
|
|
115
115
|
lines.push('- 质量要求硬门槛(用户要求 6):每一条分派都必须带 qualityRequirements(至少 1 条、每条 ≤400 字符、最多 8 条),写清「成果必须达到什么要求、什么算合格、怎么算不合格」;缺它平台拒绝派发(DELEGATE_QUALITY_REQUIREMENTS_MISSING)。任务书会把这块原文交给专家。');
|
|
116
116
|
lines.push('- 专家汇报必须精简且是 JSON(用户要求 7):专家完成后在证据块里给出 report(keyResults/keyInfo/importantInfo/leadRequirements 四个字符串数组,每数组 ≤6 条、每条 ≤200 字符,禁止重复);你收到后逐条核对,缺项或重复会被格式门禁退回(F10)。');
|
|
117
117
|
lines.push('- 你的汇总必须精简且是 JSON(用户要求 8/10/11):汇总本部门全部专家汇报后,在证据块里给出 departmentSummary(keyResults/keyInfo/importantInfo/sources 四个字符串数组,sources 必须覆盖全部已派发专家,禁止累赘重复);不合格会被退回(F11)。汇总完成后直接交回主 Agent,不需要另建文件。');
|
|
118
118
|
lines.push('- 执行时间与验收:你和每位全球顶尖专家均没有总时长、单次时长、空闲或回执硬截止。开工预估及其汇总只用于协调与告知,未报齐时如实显示部分和,但不构成分钟判据;不得以耗时、预估超额或未经时间批准为由拒绝 done,extendMs 已停用。完成只由真实结果、产物、结构化证据和质量验收决定;用户显式取消与真实错误仍然有效。');
|
|
119
|
+
lines.push('- 专家遇阻处置(3.0.5 硬规则,机制已强制):任何专家调用 dsh_workplan_escalate 后,禁止用 review/dispatch/direct 把该问题改派、重派或降级给其他专家;你必须先用 action="status" 全面梳理该成员的问题与本部门全部 delegates 执行状态,再研究形成解决方案,最后用 dsh_workplan_instruct 答复原 member;平台在该成员待指示期间会硬拒一切改派动作,投递成功后强制原专家继续执行。');
|
|
119
120
|
}
|
|
120
121
|
return lines.join('\n');
|
|
121
122
|
}
|
|
@@ -449,7 +450,7 @@ export function buildDelegateePrompt(input) {
|
|
|
449
450
|
'- 开工前必须先预估本人任务耗时(整数分钟)并经 self_effort 的 timeNotice={ declaredMinutes: 正整数分钟, reason? } 上报部门负责人,未上报不接续(T6/D9 硬门禁:ESTIMATE_MISSING/ESTIMATE_INVALID);该上报只是协调信息,必须如实预估。你和部门负责人执行任务均没有总时长、单次时长、空闲或回执硬截止;预估或实际耗时不会触发延期、超时接手,也不影响 done 验收。',
|
|
450
451
|
'- 必须在确保自身任务完成品质非常高的前提下,完美、超高效率地执行;严禁低效执行与浪费 token。',
|
|
451
452
|
'- 你是第三层叶子执行者:不得派生任何第四层 Agent/子会话;无法完成时在结论首行写 BLOCKED: 原因 并说明缺什么,不要空转。',
|
|
452
|
-
'-
|
|
453
|
+
'- 遇阻上报通道(3.0.5 四步闭环):遇阻(缺权限/缺输入/依赖产物缺失/需求自相矛盾/无法验证/需要负责人决定方向)时,第一动作是调用 dsh_workplan_escalate({ "question": "需要负责人回答的单个问题", "options": ["可选方向"], "needs": ["需要谁提供什么"] })。流程必须严格为:你先向部门负责人报告具体问题→负责人全面梳理你的问题与本部门全部已派发任务执行状态→负责人研究形成解决方案→负责人用 dsh_workplan_instruct 把方案告诉你→你继续执行原任务。平台禁止负责人在你待指示期间把你改派给其他专家;你必须是继续执行的人。调用 escalate 成功后停止推进、只做只读核对,不要自行选定一个选项继续。',
|
|
453
454
|
'- 完成后把结论与逐项证据(命令输出/文件/引用)用简洁中文回报给部门负责人会话;同时把同一份结构化结论写入 reports/wp-member-<你的成员名>-<任务代号>.json(相对工作目录,目录不存在就创建)作为结算兜底。',
|
|
454
455
|
`- 结果块契约:以 ${EVIDENCE_MARKER_BEGIN} 开头、${EVIDENCE_MARKER_END} 结尾,内含合法 JSON(status ∈ done/blocked/partial、phases 三阶段、acceptance 逐条带证据)。`,
|
|
455
456
|
'',
|