@suifei/dsh-ai-team 0.3.90 → 0.3.98
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/index.js +33 -18
- package/lib/skillopt/judge-runner.js +3 -18
- package/lib/skillopt/optimize-wiring.js +7 -5
- package/lib/skillopt/optimizer.js +7 -2
- package/lib/skillopt/subagent-text.js +101 -0
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -46,7 +46,7 @@ export const name = 'ai-team';
|
|
|
46
46
|
/** The workbench only makes sense where the Web carrier exists. */
|
|
47
47
|
export const inject = ['webServer', 'permissionPresets', 'sandboxPolicy', 'tools', 'shell'];
|
|
48
48
|
|
|
49
|
-
const VERSION = '0.3.
|
|
49
|
+
const VERSION = '0.3.98';
|
|
50
50
|
const HUMAN_ID = 'mem_human';
|
|
51
51
|
|
|
52
52
|
const SKILLOPT_BACKENDS = ['mock', 'claude', 'codex', 'copilot', 'cursor', 'pi', 'opencode', 'handoff', 'azure_openai'];
|
|
@@ -753,28 +753,35 @@ export function apply(ctx, config = {}) {
|
|
|
753
753
|
const tools = ctx.get('tools');
|
|
754
754
|
let disposeSkillOpt = () => {};
|
|
755
755
|
try {
|
|
756
|
-
|
|
757
|
-
//
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
756
|
+
// shell 惰性代理:注入时机早于 shell 服务就绪时,注册仍必须完成——
|
|
757
|
+
// 每次调用现取,避免「boot 时 shell 未就绪 → 整条技能链静默跳过」。
|
|
758
|
+
const shellProxy = {
|
|
759
|
+
resolve: (spec) => {
|
|
760
|
+
const service = ctx.get('shell');
|
|
761
|
+
if (service === undefined || service === null) throw new Error('shell service is not available');
|
|
762
|
+
return typeof service.resolve === 'function' ? service.resolve(spec) : spec;
|
|
763
|
+
},
|
|
764
|
+
execute: async (spec) => {
|
|
765
|
+
const service = ctx.get('shell');
|
|
766
|
+
if (service === undefined || service === null) throw new Error('shell service is not available');
|
|
767
|
+
return typeof service.execute === 'function' ? service.execute(spec) : service.run(spec);
|
|
768
|
+
},
|
|
769
|
+
};
|
|
770
|
+
if (!tools?.register) {
|
|
771
|
+
diag.append({ ev: 'skillopt.skip.fail', hasToolsRegister: false, hasShell: Boolean(ctx.get('shell')) });
|
|
772
|
+
console.warn('[ai-team] SkillOpt tools not registered: no tool registry');
|
|
766
773
|
}
|
|
767
|
-
if (tools?.register
|
|
774
|
+
if (tools?.register) {
|
|
768
775
|
// Darwin validation gate(对齐 darwin-skill v2.1):adopt 之前必须
|
|
769
776
|
// 通过 paired 独立 judge 的多数决;绝对 rubric 分只做 triage。
|
|
770
777
|
const engineScript = fileURLToPath(new URL('./lib/vendor/skillopt/scripts/sleep.py', import.meta.url));
|
|
771
|
-
const readPair = makeReadPair({ shell, engineScript, diag });
|
|
778
|
+
const readPair = makeReadPair({ shell: shellProxy, engineScript, diag });
|
|
772
779
|
const judge = makeJudge({
|
|
773
780
|
subagents: ctx.get('subagents'),
|
|
774
781
|
// 惰性解析:ensureParent 在 apply 之后才定义(const TDZ),
|
|
775
782
|
// judge 只在 adopt 时调用,此处不能立即取值。
|
|
776
783
|
ensureParent: () => ensureParent(),
|
|
777
|
-
providerName: () =>
|
|
784
|
+
providerName: () => (ctx.get('subagents')?.list?.() ?? [])[0],
|
|
778
785
|
votes: config.skilloptJudgeVotes ?? 3,
|
|
779
786
|
diag,
|
|
780
787
|
});
|
|
@@ -791,7 +798,7 @@ export function apply(ctx, config = {}) {
|
|
|
791
798
|
// operator can move the skillopt_* tools off the safe mock default.
|
|
792
799
|
disposeSkillOpt = applySkillOpt({
|
|
793
800
|
tools: wrapToolsWithAdoptGate(tools, adoptGate),
|
|
794
|
-
shell,
|
|
801
|
+
shell: shellProxy,
|
|
795
802
|
logger: console,
|
|
796
803
|
}, {
|
|
797
804
|
backend: config.skilloptBackend ?? 'mock',
|
|
@@ -869,7 +876,7 @@ export function apply(ctx, config = {}) {
|
|
|
869
876
|
const skillPath = typeof args?.skillPath === 'string' && args.skillPath !== '' ? args.skillPath : join(project, 'SKILL.md');
|
|
870
877
|
if (!existsSync(skillPath)) return `[skillopt optimize] SKILL.md not found: ${skillPath}`;
|
|
871
878
|
const rounds = Number.isSafeInteger(args?.rounds) ? Math.max(1, Math.min(5, args.rounds)) : 3;
|
|
872
|
-
const vcs = await makeVcs({ shell, projectDir: project });
|
|
879
|
+
const vcs = await makeVcs({ shell: shellProxy, projectDir: project });
|
|
873
880
|
const result = await runOptimizeLoop({
|
|
874
881
|
skillPath,
|
|
875
882
|
rounds,
|
|
@@ -879,14 +886,15 @@ export function apply(ctx, config = {}) {
|
|
|
879
886
|
generate: makeGenerator({
|
|
880
887
|
subagents: ctx.get('subagents'),
|
|
881
888
|
ensureParent: () => ensureParent(),
|
|
882
|
-
providerName: () =>
|
|
889
|
+
providerName: () => (ctx.get('subagents')?.list?.() ?? [])[0],
|
|
883
890
|
cwd: project,
|
|
891
|
+
diag,
|
|
884
892
|
}),
|
|
885
893
|
judge: async ({ before, after }) => {
|
|
886
894
|
const judge = makeJudge({
|
|
887
895
|
subagents: ctx.get('subagents'),
|
|
888
896
|
ensureParent: () => ensureParent(),
|
|
889
|
-
providerName: () =>
|
|
897
|
+
providerName: () => (ctx.get('subagents')?.list?.() ?? [])[0],
|
|
890
898
|
votes: config.skilloptJudgeVotes ?? 3,
|
|
891
899
|
diag,
|
|
892
900
|
});
|
|
@@ -904,6 +912,13 @@ export function apply(ctx, config = {}) {
|
|
|
904
912
|
return lines.join('\n');
|
|
905
913
|
},
|
|
906
914
|
});
|
|
915
|
+
// 汇总释放:引擎工具 + 三个附加工具(顺序无关,各自 try)。
|
|
916
|
+
const disposeExtra = () => {
|
|
917
|
+
try { disposeSkillOpt?.(); } catch { /* already disposed */ }
|
|
918
|
+
try { rubricTool?.(); } catch { /* noop */ }
|
|
919
|
+
try { judgeTool?.(); } catch { /* noop */ }
|
|
920
|
+
try { optimizeTool?.(); } catch { /* noop */ }
|
|
921
|
+
};
|
|
907
922
|
disposeSkillOpt = disposeExtra;
|
|
908
923
|
diag.append({ ev: 'skillopt.registered', backend: config.skilloptBackend ?? 'mock', timeoutMs: config.skilloptTimeoutMs ?? 120_000, adoptGate: true, judgeVotes: config.skilloptJudgeVotes ?? 3, optimize: true });
|
|
909
924
|
}
|
|
@@ -7,22 +7,10 @@
|
|
|
7
7
|
* 汇总成 paired 多数决 verdict。
|
|
8
8
|
*/
|
|
9
9
|
import { buildPairPrompt, decideByMajority, normalizeVoteCount, parseJudgeOutput, voteFromParsed } from './judge.js';
|
|
10
|
+
import { awaitActivationText } from './subagent-text.js';
|
|
10
11
|
|
|
11
12
|
const JUDGE_TIMEOUT_MS = 120_000;
|
|
12
13
|
|
|
13
|
-
/** 从子代理的 result.output 里取纯文本。 */
|
|
14
|
-
function outputText(output) {
|
|
15
|
-
if (typeof output === 'string') return output;
|
|
16
|
-
if (Array.isArray(output)) {
|
|
17
|
-
return output.map((part) => (typeof part === 'string' ? part : part?.text ?? part?.content ?? '')).join('\n');
|
|
18
|
-
}
|
|
19
|
-
if (output && typeof output === 'object') {
|
|
20
|
-
if (typeof output.text === 'string') return output.text;
|
|
21
|
-
if (typeof output.content === 'string') return output.content;
|
|
22
|
-
}
|
|
23
|
-
return '';
|
|
24
|
-
}
|
|
25
|
-
|
|
26
14
|
/**
|
|
27
15
|
* 起 N 个独立 judge 子代理做 paired 比较。
|
|
28
16
|
*
|
|
@@ -61,12 +49,9 @@ export async function judgePair(input) {
|
|
|
61
49
|
label: `skillopt-judge-${index + 1}`,
|
|
62
50
|
request: { cwd, prompt: [{ type: 'text', text: prompt }], parent },
|
|
63
51
|
delivery: 'parent',
|
|
52
|
+
signal: new AbortController().signal,
|
|
64
53
|
});
|
|
65
|
-
const
|
|
66
|
-
Promise.resolve(activation?.result),
|
|
67
|
-
new Promise((resolve) => setTimeout(() => resolve(null), JUDGE_TIMEOUT_MS)),
|
|
68
|
-
]);
|
|
69
|
-
const text = outputText(result?.output ?? result ?? '');
|
|
54
|
+
const text = await awaitActivationText(activation, { timeoutMs: JUDGE_TIMEOUT_MS });
|
|
70
55
|
const parsed = parseJudgeOutput(text);
|
|
71
56
|
const vote = voteFromParsed(parsed === null ? null : { ...parsed, aIsAfter: mapping.aIsAfter });
|
|
72
57
|
if (vote === null) errors.push(`judge ${index + 1}: unparseable output`);
|
|
@@ -5,6 +5,7 @@
|
|
|
5
5
|
import { existsSync, mkdirSync, readFileSync, writeFileSync, copyFileSync } from 'node:fs';
|
|
6
6
|
import { dirname, join } from 'node:path';
|
|
7
7
|
import { runEngineCli, quoteArgv } from './gate-wiring.js';
|
|
8
|
+
import { awaitActivationText } from './subagent-text.js';
|
|
8
9
|
|
|
9
10
|
/** 项目是否为干净 git 仓库(决定 git 棘轮还是文件备份)。 */
|
|
10
11
|
async function isCleanGitRepo(shell, cwd) {
|
|
@@ -59,7 +60,7 @@ export async function makeVcs({ shell, projectDir, now = () => Date.now() }) {
|
|
|
59
60
|
}
|
|
60
61
|
|
|
61
62
|
/** 独立生成器:一次性子代理按 rubric 短板改进,只输出完整新文件。 */
|
|
62
|
-
export function makeGenerator({ subagents, ensureParent, providerName, cwd }) {
|
|
63
|
+
export function makeGenerator({ subagents, ensureParent, providerName, cwd, diag }) {
|
|
63
64
|
return async ({ skillPath, before, dimension, round }) => {
|
|
64
65
|
const parentInfo = await ensureParent();
|
|
65
66
|
const provider = providerName();
|
|
@@ -78,12 +79,13 @@ export function makeGenerator({ subagents, ensureParent, providerName, cwd }) {
|
|
|
78
79
|
label: `skillopt-optimize-${round}`,
|
|
79
80
|
request: { cwd, prompt: [{ type: 'text', text: prompt }], parent: parentInfo?.agent },
|
|
80
81
|
delivery: 'parent',
|
|
82
|
+
signal: new AbortController().signal,
|
|
81
83
|
});
|
|
84
|
+
// 探针(ev 以 .fail 结尾以便在 errors 级 diag 中可见)。
|
|
85
|
+
diag?.append?.({ ev: 'skillopt.generate.probe.fail', kind: activation?.kind ?? null, hasHandle: Boolean(activation?.handle), hasAgent: Boolean(activation?.handle?.agent), hasSnapshot: typeof activation?.handle?.agent?.session?.snapshotEvents === 'function', outputStart: typeof activation?.outputStart, keys: Object.keys(activation ?? {}).join(',') });
|
|
82
86
|
try {
|
|
83
|
-
const
|
|
84
|
-
|
|
85
|
-
? result.output.map((part) => (typeof part === 'string' ? part : part?.text ?? '')).join('\n')
|
|
86
|
-
: String(result?.output ?? '');
|
|
87
|
+
const raw = await awaitActivationText(activation);
|
|
88
|
+
diag?.append?.({ ev: 'skillopt.generate.text.fail', chars: raw.length, preview: raw.slice(0, 160) });
|
|
87
89
|
// 去掉可能包裹的 ```markdown 围栏。
|
|
88
90
|
const fenced = raw.match(/```(?:markdown|md)?\s*\n([\s\S]*?)\n```/);
|
|
89
91
|
return fenced !== null ? fenced[1] : raw.trim();
|
|
@@ -18,9 +18,14 @@
|
|
|
18
18
|
export const DEFAULT_ROUNDS = 3;
|
|
19
19
|
const MAX_GROWTH = 1.5;
|
|
20
20
|
|
|
21
|
-
/**
|
|
21
|
+
/**
|
|
22
|
+
* 最大加权短板(weight × (10 - score) / 10;同分时取原始分更低者)。
|
|
23
|
+
*
|
|
24
|
+
* dim8「实测表现」被排除:它由测试 prompt 的真实运行决定,静态改写无法改进,
|
|
25
|
+
* 选它会让生成器空转(实测暴露的问题)。dim8 的改进走 full_test 流程。
|
|
26
|
+
*/
|
|
22
27
|
export function weakestDimension(rubric) {
|
|
23
|
-
const scored = (rubric?.dimensions ?? []).filter((dimension) => Number.isFinite(dimension?.score));
|
|
28
|
+
const scored = (rubric?.dimensions ?? []).filter((dimension) => Number.isFinite(dimension?.score) && dimension?.id !== 8);
|
|
24
29
|
if (scored.length === 0) return null;
|
|
25
30
|
return scored.reduce((worst, dimension) => {
|
|
26
31
|
const gap = dimension.weight * (10 - dimension.score) / 10;
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 一次性子代理的「等完成 + 取最终文本」——单一份实现,judge 与生成器共用。
|
|
3
|
+
*
|
|
4
|
+
* 取证依据(@deepseek-ai/dsh-subagent/lib/types/assistant-output.js):
|
|
5
|
+
* 子代理的最终输出**不是** `activation.result`,而是
|
|
6
|
+
* `activation.handle.agent.session.snapshotEvents(activation.outputStart)` 中
|
|
7
|
+
* **最后一个非空 `assistant/message`** 的 `data.message.content`;
|
|
8
|
+
* 若无消息,回退到 `assistant/attempt|message` 的 `data.stream` 累积文本。
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
const END_EVENTS = new Set(['subagent/end', 'session/end']);
|
|
12
|
+
|
|
13
|
+
/** 从会话事件里提取最终 assistant 文本。 */
|
|
14
|
+
export function extractAssistantText(events) {
|
|
15
|
+
let message = null;
|
|
16
|
+
const streamPieces = [];
|
|
17
|
+
for (const event of Array.isArray(events) ? events : []) {
|
|
18
|
+
if (event?.type === 'assistant/message') {
|
|
19
|
+
const content = event?.data?.message?.content;
|
|
20
|
+
if (Array.isArray(content) && content.length > 0) message = content;
|
|
21
|
+
}
|
|
22
|
+
if (event?.type === 'assistant/message' || event?.type === 'assistant/attempt') {
|
|
23
|
+
const pieces = event?.data?.stream;
|
|
24
|
+
if (Array.isArray(pieces)) {
|
|
25
|
+
for (const piece of pieces) {
|
|
26
|
+
const text = typeof piece === 'string' ? piece : piece?.text;
|
|
27
|
+
if (typeof text === 'string' && text !== '') streamPieces.push(text);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
if (message === null) return streamPieces.join('');
|
|
33
|
+
return message
|
|
34
|
+
.map((block) => (typeof block === 'string' ? block : block?.text ?? ''))
|
|
35
|
+
.filter((text) => text !== '')
|
|
36
|
+
.join('\n');
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* 等子代理结束并返回最终文本。
|
|
41
|
+
*
|
|
42
|
+
* 两条真实路径(按可用性选择):
|
|
43
|
+
* 1. `activation.result` 是执行结果 Promise(实测返回对象含 childId/result/
|
|
44
|
+
* dispose/messageId)——解析其 output/content/lastAssistantMessage;
|
|
45
|
+
* 2. 若只有 `activation.handle.agent.session`,则轮询 snapshotEvents
|
|
46
|
+
* (见 extractAssistantText 的事件规则)。
|
|
47
|
+
*
|
|
48
|
+
* @param {object} activation startActivation 返回
|
|
49
|
+
* @param {{timeoutMs?: number, pollMs?: number}} [options]
|
|
50
|
+
* @returns {Promise<string>}
|
|
51
|
+
*/
|
|
52
|
+
export async function awaitActivationText(activation, options = {}) {
|
|
53
|
+
const timeoutMs = options.timeoutMs ?? 120_000;
|
|
54
|
+
const pollMs = options.pollMs ?? 400;
|
|
55
|
+
const timeout = new Promise((resolve) => setTimeout(() => resolve(null), timeoutMs));
|
|
56
|
+
|
|
57
|
+
// 路径 1:result Promise(实测的主路径)。
|
|
58
|
+
const pending = activation?.result;
|
|
59
|
+
if (pending !== undefined && pending !== null && typeof pending.then === 'function') {
|
|
60
|
+
const settled = await Promise.race([Promise.resolve(pending).catch(() => null), timeout]);
|
|
61
|
+
const text = textOfResult(settled);
|
|
62
|
+
if (text !== '') return text;
|
|
63
|
+
// 空结果时继续走事件路径(某些 backend 只在会话事件里留输出)。
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// 路径 2:会话事件轮询。
|
|
67
|
+
const agent = activation?.handle?.agent;
|
|
68
|
+
const readEvents = () => {
|
|
69
|
+
try {
|
|
70
|
+
return agent?.session?.snapshotEvents?.(activation?.outputStart) ?? [];
|
|
71
|
+
} catch {
|
|
72
|
+
return [];
|
|
73
|
+
}
|
|
74
|
+
};
|
|
75
|
+
const deadline = Date.now() + timeoutMs;
|
|
76
|
+
const finished = (events) => events.some((event) => END_EVENTS.has(event?.type));
|
|
77
|
+
while (Date.now() < deadline) {
|
|
78
|
+
const events = readEvents();
|
|
79
|
+
if (finished(events) || (agent?.status === 'idle' && events.length > 0)) return extractAssistantText(events);
|
|
80
|
+
await new Promise((resolve) => setTimeout(resolve, pollMs));
|
|
81
|
+
}
|
|
82
|
+
return extractAssistantText(readEvents());
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/** 把 activation.result 的值归一成文本(兼容 string / output 数组 / content)。 */
|
|
86
|
+
export function textOfResult(settled) {
|
|
87
|
+
if (settled === null || settled === undefined) return '';
|
|
88
|
+
if (typeof settled === 'string') return settled;
|
|
89
|
+
const candidates = [settled.output, settled.content, settled.lastAssistantMessage, settled.text];
|
|
90
|
+
for (const candidate of candidates) {
|
|
91
|
+
if (typeof candidate === 'string' && candidate !== '') return candidate;
|
|
92
|
+
if (Array.isArray(candidate)) {
|
|
93
|
+
const text = candidate
|
|
94
|
+
.map((part) => (typeof part === 'string' ? part : part?.text ?? part?.content ?? ''))
|
|
95
|
+
.filter((piece) => piece !== '')
|
|
96
|
+
.join('\n');
|
|
97
|
+
if (text !== '') return text;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
return '';
|
|
101
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@suifei/dsh-ai-team",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.98",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "文渊阁 (Wenyuan Pavilion) — AI Team mode for DeepSeek Harness: concurrent projects, owned tasks, threads, inbox and an append-only team ledger",
|
|
6
6
|
"license": "MIT",
|