Skip to content

Commit 5eb0dc8

Browse files
committed
feat(mo): /pipeline 端点接观察旁挂——fix-step-tool 提议从数据里长出来
readPipelineDoc 的 observationsById 参数在上一刀已备好但没人喂。本刀接上: /pipeline 端点读 <memoryDir>/observations/<项目键>/ 下各工作流的旁挂 → 压成 { id: { latest: { activities } } } 传给 readPipelineDoc → suggestRetirements 的 fix-step-tool 从此有真实数据 为此给 observations 模块补了批量读 loadObservationsById(单工作流的 loadObservations 保留原样):末条记录即最近一次确认,没有观察的 id 不进 结果——读侧 `?? null` 降级,不制造空壳。 接线纪律与 /candidates 同款:memoryDir 同源同默认;读旁挂失败 try/catch 降级成「没有观察」——旁挂是可再生的辅助数据,不能连累图(与 P2 时 「旁挂写失败不连累记账」是同一条纪律的读侧半边)。 全量 2103/2102 pass/0 fail(无新增测试:端点接的是既有函数的组合, 数据形状由 observations 模块的 10 条锁守着)。
1 parent 6211544 commit 5eb0dc8

4 files changed

Lines changed: 86 additions & 2 deletions

File tree

‎dsh-mneme/lib/api.js‎

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ import { classify, fetchLatestVersion, PACKAGE_VERSION } from "./version-check.j
1515
import { parseReclaimQuery, parseReclaimJson, readReclaimFields, reclaimReply, RECLAIM_UNAVAILABLE } from "./reclaim-http.js";
1616
import { readPipelineDoc, loadPipelines, projectPipelineDir } from "./pipeline.js";
1717
import { loadRunsById, runsDir, RUNS_SUFFIX } from "./pipeline-runs.js";
18+
import { observationDir, loadObservationsById } from "./pipeline-observations.js";
19+
import { projectKey } from "./candidates.js";
1820
import { readCandidateDoc } from "./candidates.js";
1921

2022
// headers:少数端点(/export 附件下载)需要追加 Content-Disposition 等响应头。
@@ -1739,7 +1741,24 @@ export function createApi(ctx, service, settings, commands, embedder, semantic =
17391741
}
17401742
const { runsById, errors: runsErrors, statesById } = runsResult;
17411743

1742-
const doc = readPipelineDoc({ projectRoot: project, id, runsById });
1744+
// P3 投影的燃料:观察旁挂(与 /candidates 的 memoryDir 同源同默认)。
1745+
// 失败不连累图——旁挂是可再生的辅助数据,读不到就当没有,提议少一类而已。
1746+
let observationsById = {};
1747+
if (project !== null) {
1748+
try {
1749+
const memoryDir = (config && typeof config.memoryDir === "string" && config.memoryDir.trim() !== "")
1750+
? config.memoryDir
1751+
: "~/.dsh/memory";
1752+
const odir = observationDir({ memoryDir, projectKey: projectKey(project) });
1753+
if (odir) {
1754+
observationsById = loadObservationsById(odir, ids.filter(Boolean));
1755+
}
1756+
} catch {
1757+
// 读旁挂失败不算账本问题:如实降级成「没有观察」,fix-step-tool 少发而已
1758+
}
1759+
}
1760+
1761+
const doc = readPipelineDoc({ projectRoot: project, id, runsById, observationsById });
17431762

17441763
// 「读不出来」与「还没跑过」在节点状态上完全同形(都是全体 candidate),而且
17451764
// **不带 id 的默认视图没有兜底**——那正是用户打开时看到的那一屏。所以状态按

‎dsh-mneme/lib/pipeline-observations.js‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,29 @@ export function loadObservations(dir, pipelineId) {
105105
return { records, unreadable };
106106
}
107107

108+
/**
109+
* 批量读多个工作流的观察,压成 readPipelineDoc 要的形状:
110+
* { [pipelineId]: { latest: { activities } } }
111+
*
112+
* `latest` 取最后一条记录(confirmAt 升序写入,末条即最近一次确认)。
113+
* 没有观察的 id 不出现在结果里——读侧用 `?? null` 降级,不制造空壳。
114+
*/
115+
export function loadObservationsById(dir, pipelineIds) {
116+
const out = {};
117+
for (const id of pipelineIds || []) {
118+
if (typeof id !== "string" || id.trim() === "") continue;
119+
const { records, unreadable } = loadObservations(dir, id);
120+
if (records.length === 0) {
121+
// 有文件但全坏也如实记一条空 latest?——不。空 = 没有观察,与坏文件同形处理,
122+
// unreadable 的计数只在单工作流读取时暴露,批量视图里降级为「没有」。
123+
continue;
124+
}
125+
const last = records[records.length - 1];
126+
out[id] = { latest: { activities: last.activities }, unreadable };
127+
}
128+
return out;
129+
}
130+
108131
/**
109132
* 🔴 核心判据:从「人确认的执行集」与「本次观察到的动作」里算出 tool 不符。
110133
*

‎dsh-mneme/src/api.js‎

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ import { classify, fetchLatestVersion, PACKAGE_VERSION } from "./version-check.j
1515
import { parseReclaimQuery, parseReclaimJson, readReclaimFields, reclaimReply, RECLAIM_UNAVAILABLE } from "./reclaim-http.js";
1616
import { readPipelineDoc, loadPipelines, projectPipelineDir } from "./pipeline.js";
1717
import { loadRunsById, runsDir, RUNS_SUFFIX } from "./pipeline-runs.js";
18+
import { observationDir, loadObservationsById } from "./pipeline-observations.js";
19+
import { projectKey } from "./candidates.js";
1820
import { readCandidateDoc } from "./candidates.js";
1921

2022
// headers:少数端点(/export 附件下载)需要追加 Content-Disposition 等响应头。
@@ -1739,7 +1741,24 @@ export function createApi(ctx, service, settings, commands, embedder, semantic =
17391741
}
17401742
const { runsById, errors: runsErrors, statesById } = runsResult;
17411743

1742-
const doc = readPipelineDoc({ projectRoot: project, id, runsById });
1744+
// P3 投影的燃料:观察旁挂(与 /candidates 的 memoryDir 同源同默认)。
1745+
// 失败不连累图——旁挂是可再生的辅助数据,读不到就当没有,提议少一类而已。
1746+
let observationsById = {};
1747+
if (project !== null) {
1748+
try {
1749+
const memoryDir = (config && typeof config.memoryDir === "string" && config.memoryDir.trim() !== "")
1750+
? config.memoryDir
1751+
: "~/.dsh/memory";
1752+
const odir = observationDir({ memoryDir, projectKey: projectKey(project) });
1753+
if (odir) {
1754+
observationsById = loadObservationsById(odir, ids.filter(Boolean));
1755+
}
1756+
} catch {
1757+
// 读旁挂失败不算账本问题:如实降级成「没有观察」,fix-step-tool 少发而已
1758+
}
1759+
}
1760+
1761+
const doc = readPipelineDoc({ projectRoot: project, id, runsById, observationsById });
17431762

17441763
// 「读不出来」与「还没跑过」在节点状态上完全同形(都是全体 candidate),而且
17451764
// **不带 id 的默认视图没有兜底**——那正是用户打开时看到的那一屏。所以状态按

‎dsh-mneme/src/pipeline-observations.js‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,29 @@ export function loadObservations(dir, pipelineId) {
105105
return { records, unreadable };
106106
}
107107

108+
/**
109+
* 批量读多个工作流的观察,压成 readPipelineDoc 要的形状:
110+
* { [pipelineId]: { latest: { activities } } }
111+
*
112+
* `latest` 取最后一条记录(confirmAt 升序写入,末条即最近一次确认)。
113+
* 没有观察的 id 不出现在结果里——读侧用 `?? null` 降级,不制造空壳。
114+
*/
115+
export function loadObservationsById(dir, pipelineIds) {
116+
const out = {};
117+
for (const id of pipelineIds || []) {
118+
if (typeof id !== "string" || id.trim() === "") continue;
119+
const { records, unreadable } = loadObservations(dir, id);
120+
if (records.length === 0) {
121+
// 有文件但全坏也如实记一条空 latest?——不。空 = 没有观察,与坏文件同形处理,
122+
// unreadable 的计数只在单工作流读取时暴露,批量视图里降级为「没有」。
123+
continue;
124+
}
125+
const last = records[records.length - 1];
126+
out[id] = { latest: { activities: last.activities }, unreadable };
127+
}
128+
return out;
129+
}
130+
108131
/**
109132
* 🔴 核心判据:从「人确认的执行集」与「本次观察到的动作」里算出 tool 不符。
110133
*

0 commit comments

Comments
 (0)