dsh-cohub 0.10.1 → 0.10.3
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 +341 -98
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# dsh-cohub
|
|
2
2
|
|
|
3
|
-
> 本文对应 **v0.10.
|
|
3
|
+
> 本文对应 **v0.10.3**;完整变更历史见 [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
|
@@ -5384,6 +5384,60 @@ var STATUS_TITLE = {
|
|
|
5384
5384
|
interrupted: "interrupted(被取消,结论不完整)",
|
|
5385
5385
|
failed: "failed(分析失败,本轮没有模型结论,原因见下方「分析错误说明」)"
|
|
5386
5386
|
};
|
|
5387
|
+
function inferSessionsTruncated(sessionsTruncated, lineageTotal, sessionsAnalyzed) {
|
|
5388
|
+
if (typeof sessionsTruncated === "boolean")
|
|
5389
|
+
return sessionsTruncated;
|
|
5390
|
+
if (lineageTotal !== null && sessionsAnalyzed !== null)
|
|
5391
|
+
return sessionsAnalyzed < lineageTotal;
|
|
5392
|
+
return;
|
|
5393
|
+
}
|
|
5394
|
+
function ruleText(note) {
|
|
5395
|
+
return note.length > 0 && note !== DASH ? note : "";
|
|
5396
|
+
}
|
|
5397
|
+
function ruleSuffix(note) {
|
|
5398
|
+
const rule = ruleText(note);
|
|
5399
|
+
return rule.length > 0 ? `(${rule})` : "";
|
|
5400
|
+
}
|
|
5401
|
+
function samplingKind(note) {
|
|
5402
|
+
const rule = ruleText(note);
|
|
5403
|
+
const sampled = rule.includes("取最新");
|
|
5404
|
+
const readFailed = rule.includes("读取失败");
|
|
5405
|
+
if (sampled && readFailed)
|
|
5406
|
+
return "both";
|
|
5407
|
+
if (sampled)
|
|
5408
|
+
return "sampled";
|
|
5409
|
+
if (readFailed)
|
|
5410
|
+
return "readFailed";
|
|
5411
|
+
return "unknown";
|
|
5412
|
+
}
|
|
5413
|
+
function sessionsCoverageText(sessionsTruncatedRaw, lineageTotal, sessionsAnalyzed, samplingNote, childCount) {
|
|
5414
|
+
const sessionsTruncated = inferSessionsTruncated(sessionsTruncatedRaw, lineageTotal, sessionsAnalyzed);
|
|
5415
|
+
const analyzed = sessionsAnalyzed ?? (sessionsTruncated === false ? childCount : null);
|
|
5416
|
+
if (sessionsTruncated === true) {
|
|
5417
|
+
if (lineageTotal !== null && analyzed !== null) {
|
|
5418
|
+
return `已分析 ${analyzed} / 共 ${lineageTotal} 个子会话${ruleSuffix(samplingNote)}`;
|
|
5419
|
+
}
|
|
5420
|
+
if (lineageTotal !== null)
|
|
5421
|
+
return `共 ${lineageTotal} 个子会话(已分析数未记录)`;
|
|
5422
|
+
if (analyzed !== null)
|
|
5423
|
+
return `已分析 ${analyzed} 个子会话(总数未知)`;
|
|
5424
|
+
return "已取样截断(总数未记录)";
|
|
5425
|
+
}
|
|
5426
|
+
if (sessionsTruncated === false) {
|
|
5427
|
+
if (analyzed !== null)
|
|
5428
|
+
return `全部 ${analyzed} 个子会话`;
|
|
5429
|
+
if (lineageTotal !== null)
|
|
5430
|
+
return `全部 ${lineageTotal} 个子会话`;
|
|
5431
|
+
return "全部子会话(数量未记录)";
|
|
5432
|
+
}
|
|
5433
|
+
return "未知(本报告由旧版本生成,未记录子会话总数)";
|
|
5434
|
+
}
|
|
5435
|
+
function childCountFromSessions(report) {
|
|
5436
|
+
const list = Array.isArray(report?.sessions) ? report.sessions : [];
|
|
5437
|
+
if (list.length === 0)
|
|
5438
|
+
return null;
|
|
5439
|
+
return Math.max(0, list.length - 1);
|
|
5440
|
+
}
|
|
5387
5441
|
function renderMarkdown(report, opts = {}) {
|
|
5388
5442
|
const r = report ?? {};
|
|
5389
5443
|
const excerptLimit = typeof opts.excerptLimit === "number" && opts.excerptLimit > 0 ? Math.floor(opts.excerptLimit) : 120;
|
|
@@ -5394,12 +5448,18 @@ function renderMarkdown(report, opts = {}) {
|
|
|
5394
5448
|
const coverage = r.coverage ?? { throughSeq: {}, complete: false, gaps: [], inputTruncated: false };
|
|
5395
5449
|
const gaps = Array.isArray(coverage.gaps) ? coverage.gaps : [];
|
|
5396
5450
|
const complete = coverage.complete === true;
|
|
5451
|
+
const sessionsTruncated = coverage.sessionsTruncated;
|
|
5452
|
+
const lineageTotal = typeof coverage.lineageTotal === "number" && Number.isFinite(coverage.lineageTotal) ? coverage.lineageTotal : null;
|
|
5453
|
+
const sessionsAnalyzedRaw = typeof coverage.sessionsAnalyzed === "number" && Number.isFinite(coverage.sessionsAnalyzed) ? coverage.sessionsAnalyzed : null;
|
|
5454
|
+
const sessionsTruncatedEffective = inferSessionsTruncated(sessionsTruncated, lineageTotal, sessionsAnalyzedRaw);
|
|
5455
|
+
const samplingNote = text(coverage.samplingNote);
|
|
5397
5456
|
lines.push("# 会话复盘报告", "");
|
|
5398
5457
|
lines.push(`- 项目 cwd:${text(r.project?.cwd)}`);
|
|
5399
5458
|
lines.push(`- 项目键:${text(r.project?.key)}`);
|
|
5400
5459
|
lines.push(`- root session id:${text(r.rootSessionId)}`);
|
|
5401
5460
|
lines.push(`- 生成时间:${new Date().toISOString()}`);
|
|
5402
|
-
lines.push(`-
|
|
5461
|
+
lines.push(`- 事件流完整性:${complete ? "complete(已分析会话内部事件连续无缺口)" : "不完整(已分析会话内存在事件缺口,结论需降级)"}`);
|
|
5462
|
+
lines.push(`- 会话集合覆盖:${sessionsCoverageText(sessionsTruncated, lineageTotal, sessionsAnalyzedRaw, samplingNote, childCountFromSessions(r))}`);
|
|
5403
5463
|
lines.push(`- 分析输入是否被裁剪:${coverage.inputTruncated ? "是(结论不得外推到未读区间)" : "否"}`);
|
|
5404
5464
|
const analysis = r.analysis ?? {};
|
|
5405
5465
|
const statusRaw = typeof analysis.status === "string" && analysis.status.length > 0 ? analysis.status : "";
|
|
@@ -5419,6 +5479,22 @@ function renderMarkdown(report, opts = {}) {
|
|
|
5419
5479
|
lines.push("## 覆盖范围", "");
|
|
5420
5480
|
const throughSeq = coverage.throughSeq ?? {};
|
|
5421
5481
|
const throughEntries = Object.entries(throughSeq);
|
|
5482
|
+
const rootSid = typeof r.rootSessionId === "string" ? r.rootSessionId : "";
|
|
5483
|
+
const rootSeq = rootSid.length > 0 ? throughSeq[rootSid] : undefined;
|
|
5484
|
+
if (typeof rootSeq === "number" && Number.isFinite(rootSeq)) {
|
|
5485
|
+
lines.push(`> 本报告是快照:根会话截至 seq ${rootSeq}。此后的新 turn 与委派不在本报告范围内,需要重新生成。`, "");
|
|
5486
|
+
} else {
|
|
5487
|
+
lines.push(`> 本报告是快照:根会话的已读 seq ${DASH}(未记录)。此后的新 turn 与委派不在本报告范围内,需要重新生成。`, "");
|
|
5488
|
+
}
|
|
5489
|
+
if (sessionsTruncatedEffective === true) {
|
|
5490
|
+
const totalLabel = lineageTotal !== null ? `${lineageTotal} 个` : `${DASH}(未记录)个`;
|
|
5491
|
+
const analyzedLabel = sessionsAnalyzedRaw !== null ? `${sessionsAnalyzedRaw} 个` : `${DASH}(未记录)个`;
|
|
5492
|
+
if (samplingKind(samplingNote) === "readFailed") {
|
|
5493
|
+
lines.push(`⚠️ 部分子会话读取失败:共 ${totalLabel},成功分析 ${analyzedLabel}。失败子会话的消耗与问题不在本报告内。`, "");
|
|
5494
|
+
} else {
|
|
5495
|
+
lines.push(`⚠️ 子会话取样截断:共 ${totalLabel},只分析了 ${analyzedLabel}${ruleSuffix(samplingNote)}。未分析的子会话的消耗与问题不在本报告内。`, "");
|
|
5496
|
+
}
|
|
5497
|
+
}
|
|
5422
5498
|
if (throughEntries.length === 0) {
|
|
5423
5499
|
lines.push(`- 无覆盖记录(throughSeq 为空,${DASH})`);
|
|
5424
5500
|
} else {
|
|
@@ -5524,21 +5600,31 @@ function renderMarkdown(report, opts = {}) {
|
|
|
5524
5600
|
lines.push("## 限制说明", "");
|
|
5525
5601
|
const noConclusion = verified.length === 0;
|
|
5526
5602
|
const analysisError = typeof r.analysis?.error === "string" && r.analysis.error.length > 0 ? String(r.analysis.error) : null;
|
|
5527
|
-
const needsReview = gaps.length > 0 || unverified.length > 0 || !complete;
|
|
5603
|
+
const needsReview = gaps.length > 0 || unverified.length > 0 || !complete || coverage.inputTruncated === true || sessionsTruncatedEffective === true;
|
|
5528
5604
|
if (needsReview) {
|
|
5529
5605
|
lines.push("本报告存在未覆盖部分,结论需人工复核。", "");
|
|
5606
|
+
if (coverage.inputTruncated === true) {
|
|
5607
|
+
lines.push("- 分析输入被裁剪:模型摘要超过 12000 字被裁剪(coverage.inputTruncated),结论不得外推到未读区间。");
|
|
5608
|
+
}
|
|
5609
|
+
if (sessionsTruncatedEffective === true) {
|
|
5610
|
+
const totalLabel = lineageTotal !== null ? `${lineageTotal}` : `${DASH}`;
|
|
5611
|
+
const analyzedLabel = sessionsAnalyzedRaw !== null ? `${sessionsAnalyzedRaw}` : `${DASH}`;
|
|
5612
|
+
if (samplingKind(samplingNote) === "readFailed") {
|
|
5613
|
+
lines.push(`- 部分子会话读取失败:共 ${totalLabel} 个,成功分析 ${analyzedLabel} 个,失败子会话不在结论范围内。`);
|
|
5614
|
+
} else {
|
|
5615
|
+
lines.push(`- 子会话取样截断:共 ${totalLabel} 个,只分析了 ${analyzedLabel} 个${ruleSuffix(samplingNote)},未分析的子会话不在结论范围内。`);
|
|
5616
|
+
}
|
|
5617
|
+
}
|
|
5530
5618
|
if (gaps.length > 0)
|
|
5531
5619
|
lines.push(`- 采集缺口 ${gaps.length} 处,相关区间的事件未被读到,不应据此下结论。`);
|
|
5620
|
+
if (!complete)
|
|
5621
|
+
lines.push("- coverage.complete 为 false,已分析会话内存在事件缺口,统计数字不完整。");
|
|
5532
5622
|
if (unverified.length > 0) {
|
|
5533
5623
|
lines.push(`- ${unverified.length} 条结论缺少证据,已移入「未验证」节,未计入正式分组。`);
|
|
5534
5624
|
}
|
|
5535
5625
|
if (noConclusion) {
|
|
5536
5626
|
lines.push("- 本次复盘**没有产出任何带锚点的结论**(findings 为空),不存在可复核的结论条目。");
|
|
5537
5627
|
}
|
|
5538
|
-
if (!complete)
|
|
5539
|
-
lines.push("- coverage.complete 为 false,统计数字不完整。");
|
|
5540
|
-
if (coverage.inputTruncated)
|
|
5541
|
-
lines.push("- 分析输入被裁剪,结论不得外推到未读区间。");
|
|
5542
5628
|
if (analysisError)
|
|
5543
5629
|
lines.push(`- 分析失败/降级原因:${analysisError}`);
|
|
5544
5630
|
lines.push("");
|
|
@@ -5685,6 +5771,10 @@ function updateIndex(entry) {
|
|
|
5685
5771
|
};
|
|
5686
5772
|
if (typeof entry.note === "string" && entry.note.length > 0)
|
|
5687
5773
|
lastReport.note = entry.note;
|
|
5774
|
+
if (Number.isFinite(entry.lineageTotal))
|
|
5775
|
+
lastReport.lineageTotal = entry.lineageTotal;
|
|
5776
|
+
if (typeof entry.sessionsTruncated === "boolean")
|
|
5777
|
+
lastReport.sessionsTruncated = entry.sessionsTruncated;
|
|
5688
5778
|
index.projects[key] = {
|
|
5689
5779
|
cwd: slashed(cwd) ?? prev?.cwd ?? "",
|
|
5690
5780
|
name: cwd !== null ? basename(cwd) : prev?.name ?? "(未知项目)",
|
|
@@ -5717,6 +5807,21 @@ function sortedProjects(index) {
|
|
|
5717
5807
|
});
|
|
5718
5808
|
return rows;
|
|
5719
5809
|
}
|
|
5810
|
+
function sessionsCoverage(r) {
|
|
5811
|
+
const total = typeof r.lineageTotal === "number" && Number.isFinite(r.lineageTotal) ? r.lineageTotal : null;
|
|
5812
|
+
const analyzed = Number.isFinite(r.sessions) && r.sessions > 0 ? Math.max(0, r.sessions - 1) : null;
|
|
5813
|
+
const truncated = typeof r.sessionsTruncated === "boolean" ? r.sessionsTruncated : total !== null && analyzed !== null ? total > analyzed : null;
|
|
5814
|
+
if (truncated === true) {
|
|
5815
|
+
if (total !== null && analyzed !== null)
|
|
5816
|
+
return `(子会话 ${analyzed}/${total})`;
|
|
5817
|
+
if (total !== null)
|
|
5818
|
+
return `(子会话共 ${total},已分析数未知)`;
|
|
5819
|
+
return "(子会话取样截断,总数未知)";
|
|
5820
|
+
}
|
|
5821
|
+
if (truncated === false)
|
|
5822
|
+
return "(子会话全量)";
|
|
5823
|
+
return "(子会话总数未知)";
|
|
5824
|
+
}
|
|
5720
5825
|
function renderIndexMarkdown(index) {
|
|
5721
5826
|
const rows = sortedProjects(index);
|
|
5722
5827
|
const lines = [];
|
|
@@ -5736,7 +5841,7 @@ function renderIndexMarkdown(index) {
|
|
|
5736
5841
|
lines.push("| --- | --- | --- | --- | --- |");
|
|
5737
5842
|
for (const [, p] of rows) {
|
|
5738
5843
|
const r = p.lastReport;
|
|
5739
|
-
lines.push("| " + p.name + " | " + r.status + " | " + r.findings + " | " + r.sessions + " | " + formatLocalTime(r.at) + " |");
|
|
5844
|
+
lines.push("| " + p.name + " | " + r.status + " | " + r.findings + " | " + r.sessions + sessionsCoverage(r) + " | " + formatLocalTime(r.at) + " |");
|
|
5740
5845
|
}
|
|
5741
5846
|
lines.push("");
|
|
5742
5847
|
for (const [key, p] of rows) {
|
|
@@ -5745,7 +5850,7 @@ function renderIndexMarkdown(index) {
|
|
|
5745
5850
|
lines.push("");
|
|
5746
5851
|
lines.push("- 项目路径:`" + (p.cwd.length > 0 ? p.cwd : "(未知)") + "`");
|
|
5747
5852
|
lines.push("- 最近报告:" + formatLocalTime(r.at) + "(本地时间)");
|
|
5748
|
-
lines.push("- 状态:" + r.status + " findings:" + r.findings + " 会话数:" + r.sessions);
|
|
5853
|
+
lines.push("- 状态:" + r.status + " findings:" + r.findings + " 会话数:" + r.sessions + sessionsCoverage(r));
|
|
5749
5854
|
lines.push("- 会话:" + r.sessionId);
|
|
5750
5855
|
lines.push("- 报告(DSH):`" + (r.jsonPath.length > 0 ? r.jsonPath : "(无)") + "`");
|
|
5751
5856
|
lines.push("- 报告 MD:`" + (r.mdPath ?? "(无)") + "`");
|
|
@@ -5763,15 +5868,15 @@ var DIGEST_LIMIT_CHARS = 12000;
|
|
|
5763
5868
|
var ANALYSIS_MAX_TOKENS = 5000;
|
|
5764
5869
|
var RAW_OUTPUT_KEEP_CHARS = 2000;
|
|
5765
5870
|
var MAX_CHILD_SESSIONS = 50;
|
|
5766
|
-
var
|
|
5871
|
+
var MAX_LINEAGE_WALK_DEPTH = 64;
|
|
5767
5872
|
var ANALYSIS_TIMEOUT_MS = 60000;
|
|
5768
5873
|
var MAX_ROOT_ASCEND = 32;
|
|
5769
5874
|
function idOf(rec) {
|
|
5770
|
-
const v = rec?.session?.id ?? rec?.id ?? rec?.sessionId;
|
|
5875
|
+
const v = rec?.session?.id ?? rec?.id ?? rec?.sessionId ?? rec?.header?.id ?? rec?.session?.header?.id;
|
|
5771
5876
|
return typeof v === "string" && v.length > 0 ? v : undefined;
|
|
5772
5877
|
}
|
|
5773
5878
|
function parentOf(rec) {
|
|
5774
|
-
const v = rec?.session?.parentSession ?? rec?.parentSession ?? rec?.parentSessionId;
|
|
5879
|
+
const v = rec?.session?.parentSession ?? rec?.parentSession ?? rec?.parentSessionId ?? rec?.header?.parentSession ?? rec?.session?.header?.parentSession;
|
|
5775
5880
|
return typeof v === "string" && v.length > 0 ? v : undefined;
|
|
5776
5881
|
}
|
|
5777
5882
|
function toSessionEvents(read, fallbackId) {
|
|
@@ -5793,92 +5898,123 @@ function toSessionEvents(read, fallbackId) {
|
|
|
5793
5898
|
events
|
|
5794
5899
|
};
|
|
5795
5900
|
}
|
|
5796
|
-
function
|
|
5797
|
-
|
|
5901
|
+
function createdAtOf(rec) {
|
|
5902
|
+
const v = rec?.header?.createdAt ?? rec?.session?.header?.createdAt ?? rec?.createdAt;
|
|
5903
|
+
return typeof v === "number" && Number.isFinite(v) ? v : 0;
|
|
5904
|
+
}
|
|
5905
|
+
function normalizeSessionId(id) {
|
|
5906
|
+
const s = typeof id === "string" ? id : "";
|
|
5907
|
+
return s.startsWith("session-") ? s.slice("session-".length) : s;
|
|
5908
|
+
}
|
|
5909
|
+
function collectLineageNodes(node, out, depth = 0) {
|
|
5910
|
+
if (node === null || typeof node !== "object" || depth > MAX_LINEAGE_WALK_DEPTH)
|
|
5798
5911
|
return;
|
|
5799
5912
|
if (Array.isArray(node)) {
|
|
5800
5913
|
for (const item of node)
|
|
5801
|
-
|
|
5914
|
+
collectLineageNodes(item, out, depth + 1);
|
|
5802
5915
|
return;
|
|
5803
5916
|
}
|
|
5804
5917
|
const direct = idOf(node);
|
|
5805
|
-
if (direct)
|
|
5806
|
-
out.
|
|
5918
|
+
if (direct && !out.has(direct))
|
|
5919
|
+
out.set(direct, { id: direct, createdAt: createdAtOf(node) });
|
|
5807
5920
|
for (const value of Object.values(node)) {
|
|
5808
5921
|
if (value !== null && typeof value === "object")
|
|
5809
|
-
|
|
5922
|
+
collectLineageNodes(value, out, depth + 1);
|
|
5923
|
+
}
|
|
5924
|
+
}
|
|
5925
|
+
function sampleNewest(nodes, limit) {
|
|
5926
|
+
const sorted = [...nodes].sort((a, b) => b.createdAt - a.createdAt || a.id.localeCompare(b.id));
|
|
5927
|
+
return sorted.slice(0, Math.max(0, limit)).map((n) => n.id);
|
|
5928
|
+
}
|
|
5929
|
+
function toDiscovery(nodes, source) {
|
|
5930
|
+
const total = nodes.size;
|
|
5931
|
+
const ids = sampleNewest(nodes.values(), MAX_CHILD_SESSIONS);
|
|
5932
|
+
return { ids, total, truncated: total > ids.length, source };
|
|
5933
|
+
}
|
|
5934
|
+
function applyDiscoveryCoverage(coverage, discovery, childReadFailed = 0) {
|
|
5935
|
+
const failed = Math.max(0, childReadFailed);
|
|
5936
|
+
const sampled = discovery.ids.length;
|
|
5937
|
+
const sessionsAnalyzed = Math.max(0, sampled - failed);
|
|
5938
|
+
coverage.lineageTotal = discovery.total;
|
|
5939
|
+
coverage.sessionsAnalyzed = sessionsAnalyzed;
|
|
5940
|
+
coverage.sessionsTruncated = discovery.total > sessionsAnalyzed;
|
|
5941
|
+
const sampledOut = sampled < discovery.total;
|
|
5942
|
+
const readFailed = failed > 0;
|
|
5943
|
+
if (sampledOut && readFailed) {
|
|
5944
|
+
coverage.samplingNote = "按创建时间倒序取最新,且存在读取失败的子会话";
|
|
5945
|
+
} else if (sampledOut) {
|
|
5946
|
+
coverage.samplingNote = "按创建时间倒序取最新";
|
|
5947
|
+
} else if (readFailed) {
|
|
5948
|
+
coverage.samplingNote = "存在读取失败的子会话";
|
|
5949
|
+
} else {
|
|
5950
|
+
coverage.samplingNote = undefined;
|
|
5951
|
+
}
|
|
5952
|
+
}
|
|
5953
|
+
function throwIfAborted(signal) {
|
|
5954
|
+
if (signal?.aborted) {
|
|
5955
|
+
const reason = signal.reason;
|
|
5956
|
+
throw reason instanceof Error ? reason : new Error("分析已取消(" + String(reason ?? "aborted") + ")");
|
|
5810
5957
|
}
|
|
5811
5958
|
}
|
|
5812
5959
|
async function findChildIds(ctx, rootId, signal) {
|
|
5813
|
-
const ids = new Set;
|
|
5814
5960
|
try {
|
|
5815
5961
|
const trace = ctx?.sessionQuery?.traceSession;
|
|
5816
5962
|
if (typeof trace === "function") {
|
|
5817
5963
|
const tree = await trace.call(ctx.sessionQuery, rootId, signal);
|
|
5818
|
-
|
|
5819
|
-
|
|
5820
|
-
|
|
5821
|
-
|
|
5822
|
-
|
|
5823
|
-
|
|
5824
|
-
|
|
5825
|
-
ctx.logger?.warn?.("[cohub review] traceSession 失败:" + String(error?.message ?? error));
|
|
5826
|
-
}
|
|
5827
|
-
if (ids.size > 0)
|
|
5828
|
-
return [...ids].slice(0, MAX_CHILD_SESSIONS);
|
|
5829
|
-
try {
|
|
5830
|
-
const filter = ctx?.sessionQuery?.filterSessions;
|
|
5831
|
-
if (typeof filter === "function") {
|
|
5832
|
-
const queue = [rootId];
|
|
5833
|
-
let calls = 0;
|
|
5834
|
-
while (queue.length > 0 && ids.size < MAX_CHILD_SESSIONS && calls < MAX_CHILD_FILTER_CALLS) {
|
|
5835
|
-
const parentId = queue.shift();
|
|
5836
|
-
calls++;
|
|
5837
|
-
const listed = await filter.call(ctx.sessionQuery, { parentSessionId: parentId }, signal);
|
|
5838
|
-
const records = Array.isArray(listed) ? listed : [];
|
|
5839
|
-
for (const rec of records) {
|
|
5840
|
-
const id = idOf(rec);
|
|
5841
|
-
if (!id || id === rootId || ids.has(id))
|
|
5842
|
-
continue;
|
|
5843
|
-
ids.add(id);
|
|
5844
|
-
queue.push(id);
|
|
5845
|
-
if (ids.size >= MAX_CHILD_SESSIONS)
|
|
5846
|
-
break;
|
|
5964
|
+
if (tree !== null && typeof tree === "object") {
|
|
5965
|
+
const found = new Map;
|
|
5966
|
+
collectLineageNodes(tree?.descendants, found);
|
|
5967
|
+
const rootKey = normalizeSessionId(rootId);
|
|
5968
|
+
for (const id of [...found.keys()]) {
|
|
5969
|
+
if (id === rootId || normalizeSessionId(id) === rootKey)
|
|
5970
|
+
found.delete(id);
|
|
5847
5971
|
}
|
|
5972
|
+
return toDiscovery(found, "trace");
|
|
5848
5973
|
}
|
|
5849
5974
|
}
|
|
5850
5975
|
} catch (error) {
|
|
5851
|
-
|
|
5976
|
+
throwIfAborted(signal);
|
|
5977
|
+
ctx.logger?.warn?.("[cohub review] traceSession 失败:" + String(error?.message ?? error));
|
|
5852
5978
|
}
|
|
5853
|
-
if (ids.size > 0)
|
|
5854
|
-
return [...ids].slice(0, MAX_CHILD_SESSIONS);
|
|
5855
5979
|
try {
|
|
5856
5980
|
const list = await ctx.sessionQuery.listSessions(signal);
|
|
5857
5981
|
const records = Array.isArray(list) ? list : [];
|
|
5858
5982
|
const byParent = new Map;
|
|
5859
5983
|
for (const rec of records) {
|
|
5860
|
-
const id = idOf(rec);
|
|
5861
5984
|
const parent = parentOf(rec);
|
|
5862
|
-
|
|
5985
|
+
const key = normalizeSessionId(parent);
|
|
5986
|
+
if (!key)
|
|
5863
5987
|
continue;
|
|
5864
|
-
const bucket = byParent.get(
|
|
5865
|
-
bucket.push(
|
|
5866
|
-
byParent.set(
|
|
5988
|
+
const bucket = byParent.get(key) ?? [];
|
|
5989
|
+
bucket.push(rec);
|
|
5990
|
+
byParent.set(key, bucket);
|
|
5867
5991
|
}
|
|
5868
|
-
const
|
|
5869
|
-
|
|
5992
|
+
const found = new Map;
|
|
5993
|
+
const rootKey = normalizeSessionId(rootId);
|
|
5994
|
+
const queue = [rootKey];
|
|
5995
|
+
const seenParents = new Set([rootKey]);
|
|
5996
|
+
while (queue.length > 0) {
|
|
5870
5997
|
const cur = queue.shift();
|
|
5871
|
-
for (const
|
|
5872
|
-
|
|
5998
|
+
for (const rec of byParent.get(cur) ?? []) {
|
|
5999
|
+
const id = idOf(rec);
|
|
6000
|
+
if (!id)
|
|
6001
|
+
continue;
|
|
6002
|
+
const idKey = normalizeSessionId(id);
|
|
6003
|
+
if (idKey === rootKey || found.has(id))
|
|
5873
6004
|
continue;
|
|
5874
|
-
|
|
5875
|
-
|
|
6005
|
+
found.set(id, { id, createdAt: createdAtOf(rec) });
|
|
6006
|
+
if (!seenParents.has(idKey)) {
|
|
6007
|
+
seenParents.add(idKey);
|
|
6008
|
+
queue.push(idKey);
|
|
6009
|
+
}
|
|
5876
6010
|
}
|
|
5877
6011
|
}
|
|
6012
|
+
return toDiscovery(found, "list");
|
|
5878
6013
|
} catch (error) {
|
|
6014
|
+
throwIfAborted(signal);
|
|
5879
6015
|
ctx.logger?.warn?.("[cohub review] listSessions 失败:" + String(error?.message ?? error));
|
|
5880
6016
|
}
|
|
5881
|
-
return [
|
|
6017
|
+
return { ids: [], total: 0, truncated: false, source: "list" };
|
|
5882
6018
|
}
|
|
5883
6019
|
function writeTextAtomic2(file, text) {
|
|
5884
6020
|
const tmp = file + ".tmp";
|
|
@@ -5901,7 +6037,10 @@ function appendErrorLog(cwd, sessionId, message) {
|
|
|
5901
6037
|
mkdirSync5(dirname5(logPath), { recursive: true });
|
|
5902
6038
|
appendFileSync(logPath, new Date().toISOString() + "\t" + sessionId + "\t" + message.replace(/\s+/g, " ").slice(0, 500) + `
|
|
5903
6039
|
`, "utf8");
|
|
5904
|
-
|
|
6040
|
+
return logPath;
|
|
6041
|
+
} catch {
|
|
6042
|
+
return null;
|
|
6043
|
+
}
|
|
5905
6044
|
}
|
|
5906
6045
|
function writeErrorRecord(cwd, sessionId, error) {
|
|
5907
6046
|
const stamp = new Date().toISOString().replace(/[:.]/g, "-");
|
|
@@ -6236,6 +6375,7 @@ async function readRootSession(ctx, sessionId, signal) {
|
|
|
6236
6375
|
try {
|
|
6237
6376
|
read = await ctx.sessionQuery.readSession(current, signal);
|
|
6238
6377
|
} catch (error) {
|
|
6378
|
+
throwIfAborted(signal);
|
|
6239
6379
|
failed = "根会话读取失败(" + String(error?.message ?? error) + ")";
|
|
6240
6380
|
ctx.logger?.warn?.("[cohub review] " + failed + ",降级为空事件继续出报告");
|
|
6241
6381
|
read = { session: { id: current }, events: [] };
|
|
@@ -6251,6 +6391,7 @@ async function readRootSession(ctx, sessionId, signal) {
|
|
|
6251
6391
|
try {
|
|
6252
6392
|
parentRead = await ctx.sessionQuery.readSession(parent, signal);
|
|
6253
6393
|
} catch (error) {
|
|
6394
|
+
throwIfAborted(signal);
|
|
6254
6395
|
ctx.logger?.warn?.("[cohub review] 上溯父会话失败,以 " + current + " 为根:" + String(error?.message ?? error));
|
|
6255
6396
|
break;
|
|
6256
6397
|
}
|
|
@@ -6260,15 +6401,25 @@ async function readRootSession(ctx, sessionId, signal) {
|
|
|
6260
6401
|
}
|
|
6261
6402
|
return { sessionId: current, read, failed };
|
|
6262
6403
|
}
|
|
6263
|
-
async function runAnalysis(
|
|
6404
|
+
async function runAnalysis(req) {
|
|
6405
|
+
const { ctx, agent, sessionId, cwdHint, withLlm } = req;
|
|
6406
|
+
const external = req.signal;
|
|
6264
6407
|
const controller = new AbortController;
|
|
6265
|
-
const
|
|
6266
|
-
const
|
|
6408
|
+
const onExternalAbort = () => controller.abort(external?.reason);
|
|
6409
|
+
const timer = setTimeout(() => controller.abort(new Error("分析超时(" + Math.round(ANALYSIS_TIMEOUT_MS / 1000) + " 秒)")), ANALYSIS_TIMEOUT_MS);
|
|
6410
|
+
if (external) {
|
|
6411
|
+
if (external.aborted)
|
|
6412
|
+
onExternalAbort();
|
|
6413
|
+
else
|
|
6414
|
+
external.addEventListener("abort", onExternalAbort, { once: true });
|
|
6415
|
+
}
|
|
6267
6416
|
try {
|
|
6268
|
-
await runAnalysisInner(ctx, agent, sessionId, cwdHint, withLlm, signal);
|
|
6417
|
+
return { ok: true, ...await runAnalysisInner(ctx, agent, sessionId, cwdHint, withLlm, controller.signal) };
|
|
6269
6418
|
} catch (error) {
|
|
6419
|
+
const reason = String(error?.message ?? error);
|
|
6270
6420
|
const errorPath = writeErrorRecord(cwdHint, sessionId, error);
|
|
6271
|
-
|
|
6421
|
+
const logPath = appendErrorLog(cwdHint, sessionId, reason);
|
|
6422
|
+
ctx.logger?.warn?.("[cohub review] 分析失败(error-*.json " + (errorPath !== null ? "已" : "未") + "落盘):" + reason);
|
|
6272
6423
|
if (errorPath !== null) {
|
|
6273
6424
|
try {
|
|
6274
6425
|
updateIndex({
|
|
@@ -6280,34 +6431,41 @@ async function runAnalysis(ctx, agent, sessionId, cwdHint, withLlm) {
|
|
|
6280
6431
|
jsonPath: errorPath,
|
|
6281
6432
|
mdPath: null,
|
|
6282
6433
|
projectCopyPath: null,
|
|
6283
|
-
note: "本次复盘整体失败,仅落盘 error 记录:" +
|
|
6434
|
+
note: "本次复盘整体失败,仅落盘 error 记录:" + reason.slice(0, 200),
|
|
6435
|
+
lineageTotal: undefined,
|
|
6436
|
+
sessionsTruncated: undefined
|
|
6284
6437
|
});
|
|
6285
6438
|
} catch {}
|
|
6286
6439
|
}
|
|
6287
|
-
|
|
6440
|
+
return { ok: false, reason, errorPath, logPath };
|
|
6288
6441
|
} finally {
|
|
6289
6442
|
clearTimeout(timer);
|
|
6443
|
+
external?.removeEventListener("abort", onExternalAbort);
|
|
6290
6444
|
}
|
|
6291
6445
|
}
|
|
6292
6446
|
async function runAnalysisInner(ctx, agent, sessionId, cwdHint, withLlm, signal) {
|
|
6293
6447
|
const { sessionId: rootId, read: rootRead, failed: rootFailed } = await readRootSession(ctx, sessionId, signal);
|
|
6294
6448
|
const root = toSessionEvents(rootRead, rootId);
|
|
6295
|
-
const
|
|
6449
|
+
const discovery = await findChildIds(ctx, root.sessionId, signal);
|
|
6296
6450
|
const children = [];
|
|
6297
6451
|
const gaps = [];
|
|
6452
|
+
let childReadFailed = 0;
|
|
6298
6453
|
if (rootFailed)
|
|
6299
6454
|
gaps.push(rootId + ": " + rootFailed);
|
|
6300
|
-
for (const childId of
|
|
6455
|
+
for (const childId of discovery.ids) {
|
|
6301
6456
|
try {
|
|
6302
6457
|
const read = await ctx.sessionQuery.readSession(childId, signal);
|
|
6303
6458
|
children.push(toSessionEvents(read, childId));
|
|
6304
6459
|
} catch (error) {
|
|
6460
|
+
throwIfAborted(signal);
|
|
6461
|
+
childReadFailed++;
|
|
6305
6462
|
gaps.push(childId + ": 子会话读取失败(" + String(error?.message ?? error) + ")");
|
|
6306
6463
|
}
|
|
6307
6464
|
}
|
|
6308
6465
|
const report = buildReport({ root, children });
|
|
6466
|
+
applyDiscoveryCoverage(report.coverage, discovery, childReadFailed);
|
|
6309
6467
|
report.coverage.gaps.push(...gaps);
|
|
6310
|
-
if (
|
|
6468
|
+
if (childReadFailed > 0 || rootFailed)
|
|
6311
6469
|
report.coverage.complete = false;
|
|
6312
6470
|
const cwd = root.cwd ?? cwdHint;
|
|
6313
6471
|
const { text: digest, digest: inputDigest, truncated } = buildDigest(report);
|
|
@@ -6352,6 +6510,7 @@ async function runAnalysisInner(ctx, agent, sessionId, cwdHint, withLlm, signal)
|
|
|
6352
6510
|
analysis.status = "failed";
|
|
6353
6511
|
analysis.error = result.reason;
|
|
6354
6512
|
ctx.logger?.warn?.("[cohub review] LLM 分析失败:" + result.reason);
|
|
6513
|
+
throwIfAborted(signal);
|
|
6355
6514
|
}
|
|
6356
6515
|
} else {
|
|
6357
6516
|
analysis.status = "completed";
|
|
@@ -6385,51 +6544,135 @@ async function runAnalysisInner(ctx, agent, sessionId, cwdHint, withLlm, signal)
|
|
|
6385
6544
|
jsonPath,
|
|
6386
6545
|
mdPath: mdOk ? mdPath : null,
|
|
6387
6546
|
projectCopyPath: copy.path,
|
|
6388
|
-
note: copy.note ?? undefined
|
|
6547
|
+
note: copy.note ?? undefined,
|
|
6548
|
+
lineageTotal: report.coverage.lineageTotal,
|
|
6549
|
+
sessionsTruncated: report.coverage.sessionsTruncated
|
|
6389
6550
|
});
|
|
6390
6551
|
if (!indexed)
|
|
6391
6552
|
ctx.logger?.warn?.("[cohub review] 复盘索引更新失败(不影响报告)");
|
|
6392
6553
|
} catch (error) {
|
|
6393
6554
|
ctx.logger?.warn?.("[cohub review] 复盘索引更新异常:" + String(error?.message ?? error));
|
|
6394
6555
|
}
|
|
6556
|
+
return {
|
|
6557
|
+
report,
|
|
6558
|
+
jsonPath,
|
|
6559
|
+
mdPath: mdOk ? mdPath : null,
|
|
6560
|
+
projectCopyPath: copy.path,
|
|
6561
|
+
persisted: jsonOk && mdOk
|
|
6562
|
+
};
|
|
6395
6563
|
}
|
|
6396
6564
|
function cwdHintOf(inv) {
|
|
6397
6565
|
const cwd = inv?.agent?.session?.header?.cwd ?? inv?.agent?.session?.meta?.cwd ?? inv?.agent?.options?.cwd ?? null;
|
|
6398
6566
|
return typeof cwd === "string" && cwd.length > 0 ? cwd : null;
|
|
6399
6567
|
}
|
|
6400
6568
|
var inFlight = new Set;
|
|
6569
|
+
function formatElapsed(ms) {
|
|
6570
|
+
const safe = Number.isFinite(ms) && ms >= 0 ? ms : 0;
|
|
6571
|
+
return safe < 1000 ? (safe / 1000).toFixed(1) + "s" : Math.round(safe / 1000) + "s";
|
|
6572
|
+
}
|
|
6573
|
+
function kindBreakdown(report) {
|
|
6574
|
+
const order = ["issue", "praise", "observation", "suggestion"];
|
|
6575
|
+
const counts = new Map;
|
|
6576
|
+
for (const f of report.findings ?? []) {
|
|
6577
|
+
const kind = typeof f?.kind === "string" ? f.kind : "observation";
|
|
6578
|
+
counts.set(kind, (counts.get(kind) ?? 0) + 1);
|
|
6579
|
+
}
|
|
6580
|
+
const parts = [];
|
|
6581
|
+
for (const kind of order) {
|
|
6582
|
+
const n = counts.get(kind);
|
|
6583
|
+
if (n !== undefined && n > 0)
|
|
6584
|
+
parts.push(kind + " " + n);
|
|
6585
|
+
}
|
|
6586
|
+
for (const [kind, n] of counts) {
|
|
6587
|
+
if (!order.includes(kind))
|
|
6588
|
+
parts.push(kind + " " + n);
|
|
6589
|
+
}
|
|
6590
|
+
return parts.length > 0 ? "(" + parts.join(" / ") + ")" : "";
|
|
6591
|
+
}
|
|
6592
|
+
function formatSuccessText(art, elapsedMs, withLlm) {
|
|
6593
|
+
const report = art.report;
|
|
6594
|
+
const lines = [];
|
|
6595
|
+
const status = report.analysis?.status ?? "completed";
|
|
6596
|
+
const head = status === "completed" ? "报告已生成" : "报告已生成(分析未完整完成)";
|
|
6597
|
+
lines.push(head + "(用时 " + formatElapsed(elapsedMs) + (withLlm ? "" : ",仅统计,跳过 LLM") + ")");
|
|
6598
|
+
const findings = Array.isArray(report.findings) ? report.findings : [];
|
|
6599
|
+
lines.push("- 结论:" + findings.length + " 条" + kindBreakdown(report));
|
|
6600
|
+
const cov = report.coverage;
|
|
6601
|
+
const analyzed = typeof cov?.sessionsAnalyzed === "number" ? cov.sessionsAnalyzed : undefined;
|
|
6602
|
+
const total = typeof cov?.lineageTotal === "number" ? cov.lineageTotal : undefined;
|
|
6603
|
+
if (analyzed !== undefined && total !== undefined) {
|
|
6604
|
+
const note = cov.samplingNote ? "(" + cov.samplingNote + ")" : "";
|
|
6605
|
+
lines.push("- 覆盖:已分析 " + analyzed + " / 共 " + total + " 个子会话" + note);
|
|
6606
|
+
} else {
|
|
6607
|
+
lines.push("- 覆盖:已读取 " + (Array.isArray(report.sessions) ? report.sessions.length : 0) + " 个会话(子会话总数未知;根会话 + 子会话)");
|
|
6608
|
+
}
|
|
6609
|
+
const reportPath = art.projectCopyPath ?? art.mdPath ?? art.jsonPath;
|
|
6610
|
+
lines.push("- 报告:" + reportPath);
|
|
6611
|
+
if (art.projectCopyPath === null && art.mdPath !== null) {
|
|
6612
|
+
lines.push("- 说明:项目内副本未写入(" + (report.coverage?.gaps?.length ? "见报告缺口" : "cwd 不可用或 .gitignore 冲突") + "),以上为全局目录路径");
|
|
6613
|
+
}
|
|
6614
|
+
if (!art.persisted) {
|
|
6615
|
+
lines.push("- 注意:报告落盘不完整(JSON/MD 至少一个写失败),路径可能不可读");
|
|
6616
|
+
}
|
|
6617
|
+
if (cov?.sessionsTruncated === true) {
|
|
6618
|
+
lines.push("- 提示:**未分析的子会话不在结论内**(结论不得外推到这些子会话)");
|
|
6619
|
+
}
|
|
6620
|
+
if (cov?.inputTruncated === true) {
|
|
6621
|
+
lines.push("- 提示:喂给模型的输入被裁剪过,结论不得外推到未读区间");
|
|
6622
|
+
}
|
|
6623
|
+
if (status !== "completed") {
|
|
6624
|
+
const reason = typeof report.analysis?.error === "string" && report.analysis.error.length > 0 ? report.analysis.error : "原因未记录(详见报告 analysis 段)";
|
|
6625
|
+
lines.push("- 状态:" + status + " —— " + reason);
|
|
6626
|
+
}
|
|
6627
|
+
return lines.join(`
|
|
6628
|
+
`);
|
|
6629
|
+
}
|
|
6630
|
+
function formatErrorText(outcome, elapsedMs) {
|
|
6631
|
+
const lines = ["分析失败:" + outcome.reason + "(用时 " + formatElapsed(elapsedMs) + ")"];
|
|
6632
|
+
if (outcome.errorPath !== null)
|
|
6633
|
+
lines.push("- 错误详情:" + outcome.errorPath);
|
|
6634
|
+
else
|
|
6635
|
+
lines.push("- 错误详情:未落盘(写 error-*.json 失败)");
|
|
6636
|
+
if (outcome.logPath !== null)
|
|
6637
|
+
lines.push("- 错误日志:" + outcome.logPath);
|
|
6638
|
+
else
|
|
6639
|
+
lines.push("- 错误日志:未落盘(写 _errors.log 失败)");
|
|
6640
|
+
return lines.join(`
|
|
6641
|
+
`);
|
|
6642
|
+
}
|
|
6401
6643
|
function registerReviewCommand(ctx) {
|
|
6402
|
-
const handler = (inv) => {
|
|
6644
|
+
const handler = async (inv) => {
|
|
6645
|
+
const startedAt = Date.now();
|
|
6646
|
+
let sessionId = null;
|
|
6647
|
+
let cwdHint = null;
|
|
6648
|
+
let claimed = false;
|
|
6403
6649
|
try {
|
|
6404
|
-
const
|
|
6405
|
-
if (typeof
|
|
6650
|
+
const sid = inv?.agent?.session?.id;
|
|
6651
|
+
if (typeof sid !== "string" || sid.length === 0) {
|
|
6406
6652
|
return { kind: "error", text: "无法确定当前会话 id,已跳过复盘。" };
|
|
6407
6653
|
}
|
|
6408
|
-
|
|
6409
|
-
|
|
6654
|
+
sessionId = sid;
|
|
6655
|
+
if (inFlight.has(sid)) {
|
|
6656
|
+
return { kind: "error", text: "上一次分析仍在进行中,请稍候(会话 " + sid + ")。" };
|
|
6410
6657
|
}
|
|
6411
6658
|
const rawInput = typeof inv?.rawInput === "string" ? inv.rawInput : "";
|
|
6412
6659
|
const withLlm = !/(^|\s)--no-llm(\s|$)/.test(rawInput);
|
|
6413
|
-
|
|
6414
|
-
inFlight.add(
|
|
6415
|
-
|
|
6416
|
-
|
|
6417
|
-
|
|
6418
|
-
|
|
6419
|
-
|
|
6420
|
-
|
|
6421
|
-
ctx.logger?.warn?.("[cohub review] 报告生成失败:" + message);
|
|
6422
|
-
} finally {
|
|
6423
|
-
inFlight.delete(sessionId);
|
|
6424
|
-
}
|
|
6425
|
-
})();
|
|
6426
|
-
const dir = reviewStorePath(cwdHint, sessionId, "").replace(/[\\/]$/, "");
|
|
6427
|
-
return {
|
|
6428
|
-
kind: "success",
|
|
6429
|
-
text: "正在生成报告…(" + (withLlm ? "含一次 LLM 分析" : "仅统计,跳过 LLM") + ")输出目录:" + dir
|
|
6430
|
-
};
|
|
6660
|
+
cwdHint = cwdHintOf(inv);
|
|
6661
|
+
inFlight.add(sid);
|
|
6662
|
+
claimed = true;
|
|
6663
|
+
const outcome = await runAnalysis({ ctx, agent: inv?.agent, sessionId: sid, cwdHint, withLlm, signal: inv?.signal });
|
|
6664
|
+
if (outcome.ok) {
|
|
6665
|
+
return { kind: "success", text: formatSuccessText(outcome, Date.now() - startedAt, withLlm) };
|
|
6666
|
+
}
|
|
6667
|
+
return { kind: "error", text: formatErrorText(outcome, Date.now() - startedAt) };
|
|
6431
6668
|
} catch (error) {
|
|
6432
|
-
|
|
6669
|
+
const message = String(error?.message ?? error);
|
|
6670
|
+
if (sessionId !== null)
|
|
6671
|
+
appendErrorLog(cwdHint, sessionId, message);
|
|
6672
|
+
return { kind: "error", text: "复盘命令执行异常:" + message };
|
|
6673
|
+
} finally {
|
|
6674
|
+
if (claimed && sessionId !== null)
|
|
6675
|
+
inFlight.delete(sessionId);
|
|
6433
6676
|
}
|
|
6434
6677
|
};
|
|
6435
6678
|
try {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-cohub",
|
|
3
|
-
"version": "0.10.
|
|
3
|
+
"version": "0.10.3",
|
|
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",
|