dsh-cohub 0.9.0 → 0.9.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/README.md +1 -1
- package/lib/index.js +160 -19
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# dsh-cohub
|
|
2
2
|
|
|
3
|
-
> 本文对应 **v0.9.
|
|
3
|
+
> 本文对应 **v0.9.1**;完整变更历史见 [CHANGELOG.md](./CHANGELOG.md)。
|
|
4
4
|
|
|
5
5
|
DeepSeek Harness 版(对照 [OpenCode 版](../oh-my-opencode-cohub/)):中文智能体编排插件。
|
|
6
6
|
把「纯调度模式 + 13 个技能(12 个代理编排体系 + co-cordis 组合创作)+ 多模型共识 + 运行期调度韧性」移植到 DSH 原生能力之上。
|
package/lib/index.js
CHANGED
|
@@ -3520,6 +3520,20 @@ async function saveStats(cwd, sessionId, stats) {
|
|
|
3520
3520
|
}
|
|
3521
3521
|
|
|
3522
3522
|
// src/review-collector.ts
|
|
3523
|
+
function resolveRootSessionId(sessionId, parentOf, maxDepth = 32) {
|
|
3524
|
+
let current = sessionId;
|
|
3525
|
+
const seen = new Set([current]);
|
|
3526
|
+
for (let depth = 0;depth < maxDepth; depth++) {
|
|
3527
|
+
const parent = parentOf(current);
|
|
3528
|
+
if (typeof parent !== "string" || parent.length === 0)
|
|
3529
|
+
return current;
|
|
3530
|
+
if (seen.has(parent))
|
|
3531
|
+
return current;
|
|
3532
|
+
seen.add(parent);
|
|
3533
|
+
current = parent;
|
|
3534
|
+
}
|
|
3535
|
+
return current;
|
|
3536
|
+
}
|
|
3523
3537
|
function turnKind(reason) {
|
|
3524
3538
|
if (typeof reason === "string" && reason)
|
|
3525
3539
|
return reason;
|
|
@@ -3591,6 +3605,7 @@ function createReviewCollector(ctx, opts = {}) {
|
|
|
3591
3605
|
const enabled = opts.enabled !== false;
|
|
3592
3606
|
const debounceMs = Math.max(0, Math.trunc(opts.debounceMs ?? 200));
|
|
3593
3607
|
const states = new Map;
|
|
3608
|
+
const parents = new Map;
|
|
3594
3609
|
const queues = new Map;
|
|
3595
3610
|
const timers = new Map;
|
|
3596
3611
|
let off = null;
|
|
@@ -3600,6 +3615,11 @@ function createReviewCollector(ctx, opts = {}) {
|
|
|
3600
3615
|
ctx?.logger?.warn?.("[cohub review] " + msg);
|
|
3601
3616
|
} catch {}
|
|
3602
3617
|
};
|
|
3618
|
+
const rerootAll = () => {
|
|
3619
|
+
for (const st of states.values()) {
|
|
3620
|
+
st.rootSessionId = resolveRootSessionId(st.sessionId, (id) => parents.get(id));
|
|
3621
|
+
}
|
|
3622
|
+
};
|
|
3603
3623
|
const stateOf = (sessionId, session) => {
|
|
3604
3624
|
let st = states.get(sessionId);
|
|
3605
3625
|
if (!st) {
|
|
@@ -3608,11 +3628,12 @@ function createReviewCollector(ctx, opts = {}) {
|
|
|
3608
3628
|
const cwd = typeof cwdRaw === "string" && cwdRaw ? cwdRaw : null;
|
|
3609
3629
|
const parentRaw = header?.parentSession;
|
|
3610
3630
|
const parentSessionId = typeof parentRaw === "string" && parentRaw ? parentRaw : null;
|
|
3631
|
+
parents.set(sessionId, parentSessionId);
|
|
3611
3632
|
st = {
|
|
3612
3633
|
sessionId,
|
|
3613
3634
|
cwd,
|
|
3614
3635
|
parentSessionId,
|
|
3615
|
-
rootSessionId:
|
|
3636
|
+
rootSessionId: resolveRootSessionId(sessionId, (id) => parents.get(id)),
|
|
3616
3637
|
seenSeqs: new Set,
|
|
3617
3638
|
turns: new Map,
|
|
3618
3639
|
childSessions: new Set,
|
|
@@ -3646,11 +3667,13 @@ function createReviewCollector(ctx, opts = {}) {
|
|
|
3646
3667
|
const gaps = new Set(existing?.gaps ?? []);
|
|
3647
3668
|
for (const g of st.gaps)
|
|
3648
3669
|
gaps.add(g);
|
|
3670
|
+
const rootSessionId = resolveRootSessionId(st.sessionId, (id) => parents.get(id));
|
|
3671
|
+
st.rootSessionId = rootSessionId;
|
|
3649
3672
|
const stats = {
|
|
3650
3673
|
version: REVIEW_STATS_VERSION,
|
|
3651
3674
|
sessionId: st.sessionId,
|
|
3652
3675
|
cwd: st.cwd,
|
|
3653
|
-
rootSessionId
|
|
3676
|
+
rootSessionId,
|
|
3654
3677
|
gaps: [...gaps],
|
|
3655
3678
|
updatedAt: new Date().toISOString(),
|
|
3656
3679
|
turns: [...merged.values()].sort((a, b) => a.turn - b.turn)
|
|
@@ -3797,6 +3820,7 @@ function createReviewCollector(ctx, opts = {}) {
|
|
|
3797
3820
|
off = ctx.on("session/event", (session, event) => {
|
|
3798
3821
|
try {
|
|
3799
3822
|
consume(session, event);
|
|
3823
|
+
rerootAll();
|
|
3800
3824
|
const sid = session?.id;
|
|
3801
3825
|
const parentId = states.get(sid)?.parentSessionId;
|
|
3802
3826
|
if (typeof sid === "string" && typeof parentId === "string")
|
|
@@ -4154,6 +4178,10 @@ function renderMarkdown(report, opts = {}) {
|
|
|
4154
4178
|
if (typeof analysis.error === "string" && analysis.error.length > 0) {
|
|
4155
4179
|
lines.push(`- 分析错误说明:${analysis.error}`);
|
|
4156
4180
|
}
|
|
4181
|
+
if (typeof analysis.rawOutputLength === "number" && Number.isFinite(analysis.rawOutputLength)) {
|
|
4182
|
+
const kept = typeof analysis.rawOutput === "string" ? analysis.rawOutput.length : 0;
|
|
4183
|
+
lines.push(`- 模型原始输出:全长 ${num2(analysis.rawOutputLength)} 字,报告内留档前 ${num2(kept)} 字(见 JSON 的 analysis.rawOutput)`);
|
|
4184
|
+
}
|
|
4157
4185
|
if (statusRaw === "failed") {
|
|
4158
4186
|
lines.push("- 本轮**没有产生任何模型结论**:findings 为空是分析失败的结果,不代表本次会话没有值得复盘之处。");
|
|
4159
4187
|
}
|
|
@@ -4281,8 +4309,10 @@ function renderMarkdown(report, opts = {}) {
|
|
|
4281
4309
|
|
|
4282
4310
|
// src/review-command.ts
|
|
4283
4311
|
var DIGEST_LIMIT_CHARS = 12000;
|
|
4284
|
-
var ANALYSIS_MAX_TOKENS =
|
|
4312
|
+
var ANALYSIS_MAX_TOKENS = 5000;
|
|
4313
|
+
var RAW_OUTPUT_KEEP_CHARS = 2000;
|
|
4285
4314
|
var MAX_CHILD_SESSIONS = 50;
|
|
4315
|
+
var MAX_ROOT_ASCEND = 32;
|
|
4286
4316
|
function idOf(rec) {
|
|
4287
4317
|
const v = rec?.session?.id ?? rec?.id ?? rec?.sessionId;
|
|
4288
4318
|
return typeof v === "string" && v.length > 0 ? v : undefined;
|
|
@@ -4412,20 +4442,96 @@ function buildDigest(report) {
|
|
|
4412
4442
|
const digest = createHash4("sha256").update(text, "utf8").digest("hex").slice(0, 16);
|
|
4413
4443
|
return { text, digest, truncated };
|
|
4414
4444
|
}
|
|
4415
|
-
function
|
|
4416
|
-
const fenced = /```(?:json)?\s*([\s\S]*?)
|
|
4417
|
-
|
|
4418
|
-
|
|
4419
|
-
|
|
4420
|
-
|
|
4421
|
-
|
|
4445
|
+
function stripFence(text) {
|
|
4446
|
+
const fenced = /```(?:json|JSON)?\s*([\s\S]*?)```/.exec(text);
|
|
4447
|
+
return fenced ? fenced[1] : text;
|
|
4448
|
+
}
|
|
4449
|
+
function scanBalanced(text, start) {
|
|
4450
|
+
let depth = 0;
|
|
4451
|
+
let inString = false;
|
|
4452
|
+
let escape = false;
|
|
4453
|
+
for (let i = start;i < text.length; i++) {
|
|
4454
|
+
const ch = text[i];
|
|
4455
|
+
if (inString) {
|
|
4456
|
+
if (escape)
|
|
4457
|
+
escape = false;
|
|
4458
|
+
else if (ch === "\\")
|
|
4459
|
+
escape = true;
|
|
4460
|
+
else if (ch === '"')
|
|
4461
|
+
inString = false;
|
|
4462
|
+
continue;
|
|
4463
|
+
}
|
|
4464
|
+
if (ch === '"') {
|
|
4465
|
+
inString = true;
|
|
4466
|
+
continue;
|
|
4467
|
+
}
|
|
4468
|
+
if (ch === "[" || ch === "{")
|
|
4469
|
+
depth++;
|
|
4470
|
+
else if (ch === "]" || ch === "}") {
|
|
4471
|
+
depth--;
|
|
4472
|
+
if (depth === 0)
|
|
4473
|
+
return i;
|
|
4474
|
+
}
|
|
4475
|
+
}
|
|
4476
|
+
return -1;
|
|
4477
|
+
}
|
|
4478
|
+
function tryParseArray(text) {
|
|
4422
4479
|
try {
|
|
4423
|
-
const parsed = JSON.parse(
|
|
4480
|
+
const parsed = JSON.parse(text);
|
|
4424
4481
|
return Array.isArray(parsed) ? parsed : null;
|
|
4425
4482
|
} catch {
|
|
4426
4483
|
return null;
|
|
4427
4484
|
}
|
|
4428
4485
|
}
|
|
4486
|
+
function salvageObjects(text, start) {
|
|
4487
|
+
const out = [];
|
|
4488
|
+
let i = start + 1;
|
|
4489
|
+
while (i < text.length) {
|
|
4490
|
+
const ch = text[i];
|
|
4491
|
+
if (ch === "]")
|
|
4492
|
+
break;
|
|
4493
|
+
if (ch === "{") {
|
|
4494
|
+
const end = scanBalanced(text, i);
|
|
4495
|
+
if (end < 0)
|
|
4496
|
+
break;
|
|
4497
|
+
const one = text.slice(i, end + 1);
|
|
4498
|
+
try {
|
|
4499
|
+
out.push(JSON.parse(one));
|
|
4500
|
+
} catch {
|
|
4501
|
+
try {
|
|
4502
|
+
out.push(JSON.parse(one.replace(/,\s*([\]}])/g, "$1")));
|
|
4503
|
+
} catch {}
|
|
4504
|
+
}
|
|
4505
|
+
i = end + 1;
|
|
4506
|
+
continue;
|
|
4507
|
+
}
|
|
4508
|
+
i++;
|
|
4509
|
+
}
|
|
4510
|
+
return out;
|
|
4511
|
+
}
|
|
4512
|
+
function extractJsonArray(text) {
|
|
4513
|
+
const empty = { items: null, truncated: false };
|
|
4514
|
+
if (typeof text !== "string" || text.length === 0)
|
|
4515
|
+
return empty;
|
|
4516
|
+
const body = stripFence(text);
|
|
4517
|
+
const start = body.indexOf("[");
|
|
4518
|
+
if (start < 0)
|
|
4519
|
+
return empty;
|
|
4520
|
+
const end = scanBalanced(body, start);
|
|
4521
|
+
if (end > start) {
|
|
4522
|
+
const candidate = body.slice(start, end + 1);
|
|
4523
|
+
const strict = tryParseArray(candidate);
|
|
4524
|
+
if (strict)
|
|
4525
|
+
return { items: strict, truncated: false };
|
|
4526
|
+
const repaired = tryParseArray(candidate.replace(/,\s*([\]}])/g, "$1"));
|
|
4527
|
+
if (repaired)
|
|
4528
|
+
return { items: repaired, truncated: false };
|
|
4529
|
+
const salvaged = salvageObjects(body, start);
|
|
4530
|
+
return { items: salvaged.length > 0 ? salvaged : null, truncated: false };
|
|
4531
|
+
}
|
|
4532
|
+
const salvaged = salvageObjects(body, start);
|
|
4533
|
+
return { items: salvaged.length > 0 ? salvaged : null, truncated: true };
|
|
4534
|
+
}
|
|
4429
4535
|
function callIdOf(type, data) {
|
|
4430
4536
|
if (type !== "tool/call" && type !== "tool/result")
|
|
4431
4537
|
return null;
|
|
@@ -4607,10 +4713,33 @@ async function callAnalysis(ctx, agent, digest, signal) {
|
|
|
4607
4713
|
};
|
|
4608
4714
|
}
|
|
4609
4715
|
}
|
|
4716
|
+
async function readRootSession(ctx, sessionId, signal) {
|
|
4717
|
+
let current = sessionId;
|
|
4718
|
+
let read = await ctx.sessionQuery.readSession(current, signal);
|
|
4719
|
+
const seen = new Set([current]);
|
|
4720
|
+
for (let depth = 0;depth < MAX_ROOT_ASCEND; depth++) {
|
|
4721
|
+
const parent = parentOf(read);
|
|
4722
|
+
if (typeof parent !== "string" || parent.length === 0)
|
|
4723
|
+
break;
|
|
4724
|
+
if (seen.has(parent))
|
|
4725
|
+
break;
|
|
4726
|
+
let parentRead;
|
|
4727
|
+
try {
|
|
4728
|
+
parentRead = await ctx.sessionQuery.readSession(parent, signal);
|
|
4729
|
+
} catch (error) {
|
|
4730
|
+
ctx.logger?.warn?.("[cohub review] 上溯父会话失败,以 " + current + " 为根:" + String(error?.message ?? error));
|
|
4731
|
+
break;
|
|
4732
|
+
}
|
|
4733
|
+
seen.add(parent);
|
|
4734
|
+
current = parent;
|
|
4735
|
+
read = parentRead;
|
|
4736
|
+
}
|
|
4737
|
+
return { sessionId: current, read };
|
|
4738
|
+
}
|
|
4610
4739
|
async function runAnalysis(ctx, agent, sessionId, cwdHint, withLlm) {
|
|
4611
4740
|
const signal = undefined;
|
|
4612
|
-
const rootRead = await ctx
|
|
4613
|
-
const root = toSessionEvents(rootRead,
|
|
4741
|
+
const { sessionId: rootId, read: rootRead } = await readRootSession(ctx, sessionId, signal);
|
|
4742
|
+
const root = toSessionEvents(rootRead, rootId);
|
|
4614
4743
|
const childIds = await findChildIds(ctx, root.sessionId, signal);
|
|
4615
4744
|
const children = [];
|
|
4616
4745
|
const gaps = [];
|
|
@@ -4640,19 +4769,31 @@ async function runAnalysis(ctx, agent, sessionId, cwdHint, withLlm) {
|
|
|
4640
4769
|
analysis.model = result.model;
|
|
4641
4770
|
if (result.ok) {
|
|
4642
4771
|
analysis.invocations = 1;
|
|
4643
|
-
const
|
|
4644
|
-
|
|
4772
|
+
const extracted = extractJsonArray(result.raw);
|
|
4773
|
+
const rawItems = extracted.items;
|
|
4774
|
+
if (rawItems !== null) {
|
|
4645
4775
|
const index = buildEvidenceIndex([root, ...children]);
|
|
4646
4776
|
report.findings = normalizeFindings(rawItems, index, root.sessionId);
|
|
4647
|
-
analysis.status = "completed";
|
|
4648
4777
|
const dropped = rawItems.length - report.findings.length;
|
|
4649
|
-
if (
|
|
4650
|
-
analysis.
|
|
4778
|
+
if (extracted.truncated) {
|
|
4779
|
+
analysis.status = "interrupted";
|
|
4780
|
+
analysis.error = "模型输出疑似被 maxTokens 截断:已抢救出 " + report.findings.length + " 条完整结论(其余丢失,建议提高 maxTokens 或减少会话数)。" + (dropped > 0 ? "另有 " + dropped + " 条因证据无法校验被丢弃。" : "");
|
|
4781
|
+
} else {
|
|
4782
|
+
analysis.status = "completed";
|
|
4783
|
+
if (dropped > 0) {
|
|
4784
|
+
analysis.error = "模型返回 " + rawItems.length + " 条结论," + dropped + " 条因证据无法校验被丢弃。";
|
|
4785
|
+
}
|
|
4651
4786
|
}
|
|
4652
4787
|
} else {
|
|
4653
|
-
analysis.
|
|
4788
|
+
analysis.status = "failed";
|
|
4789
|
+
analysis.error = "模型输出无法解析为 JSON 数组,findings 留空(原始输出见 analysis.rawOutput)。";
|
|
4654
4790
|
ctx.logger?.warn?.("[cohub review] " + analysis.error);
|
|
4655
4791
|
}
|
|
4792
|
+
if (analysis.status !== "completed") {
|
|
4793
|
+
analysis.rawOutput = result.raw.slice(0, RAW_OUTPUT_KEEP_CHARS);
|
|
4794
|
+
analysis.rawOutputLength = result.raw.length;
|
|
4795
|
+
ctx.logger?.warn?.("[cohub review] 模型输出长度 " + result.raw.length + ",末 40 字:" + result.raw.slice(-40));
|
|
4796
|
+
}
|
|
4656
4797
|
} else {
|
|
4657
4798
|
analysis.status = "failed";
|
|
4658
4799
|
analysis.error = result.reason;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-cohub",
|
|
3
|
-
"version": "0.9.
|
|
3
|
+
"version": "0.9.1",
|
|
4
4
|
"description": "DeepSeek Harness 版 CoHub:中文智能体编排——用户可选 2 种工作模式(co-orchestrator 纯调度 / cohub-cordis 创造模式)+ 13 个专职代理技能 + 多模型共识;另由 cohub-subagent-readonly / cohub-subagent-write 两个支撑 preset 实现子代理读写硬隔离(共 4 个 preset)",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "lib/index.js",
|