Skip to content

Commit b921763

Browse files
authored
Merge pull request #595 from Oscar-Williams/fix/choro-tool-errors
fix(choro): surface tool errors and incomplete searches
2 parents 79d49d2 + 3d708c7 commit b921763

10 files changed

Lines changed: 267 additions & 143 deletions

File tree

‎dsh-mneme/CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
## [Unreleased]
44

5+
- **修复 #579**:仓库地志工具会提示登记、仓库选择和读页错误。检索遇到不可读的页面时会标明遗漏,并保留已找到的命中。
56
- **修复 #593 留下的两处旁路:显式钉住的行仍会被文档升格吸收静默归档;`memory_pin` 回执在 `pinnedInjectBudget=0`(默认关闭)时仍报 `injectable: true`**。#593 把 pin 从「按类型推断」改成显式存储(`pinned_memories` 表),但两处消费方没跟上:吸收只拿得到记忆行、拿不到 pin 表,于是退回按类型猜;回执只看了遗忘/归档/暂停作用域,没看预算开关。
67

78
- **判据①:新增 `store.isPinned(memoryId)` 单行点查**(`pinned_memories` 的唯一读侧谓词)。`src/document.js` 的 evidence 吸收循环改为两条并列判据:`store.isPinned(row.id)`(显式钉住)**或** `pinnedTypes.has(row.type)`(#249 逐字保真池类型 constraint/preference)——任一成立就不归档。上一版只有后者,所以一条被钉的 `pattern` / `decision` 在文档升格时照样 `setArchived`:用户钉过的行被悄悄收走,pin 语义当场失效。`store.isPinned` 缺席时自动退回类型口径(fail-safe,不因新谓词缺失而放宽)。

‎dsh-mneme/README.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ dsh web
6868
| `memory_organize` | 主动整理(#231,**要 `organizeEnabled` 打开才注册**):`mode=dryRun` 先出比对报告(每条候选 `exact` / `near` / `new` + 命中行,**不写一行库**、只留审计回执),agent 判断后 `mode=apply` 引用那份报告的 `run_id` 落地。两种「不要它」要分清——**`discard`** = 这条候选不写,什么都不动(只进回执);**`archive`** = 把报告标为被取代的那条**既有行**归档(可恢复,绝不物理删除)。没有比对过的候选写不进库,一份报告也只许落地一次 |
6969
| `memory_runtime` | 本地推理运行时的状态 / 供给 / 校验(v0.7.32,自管运行时三档取件) |
7070

71+
开启 `chorographyEnabled` 后可用仓库地志工具。多个仓库已登记时请传仓库 ID。若搜索只读到部分页面,结果会标明漏查的页。
72+
7173
### 工作流(M.O.,v0.9 beta)
7274

7375
记忆库答的是「这件事**为什么**这么办」,答不了「这件事**到底怎么跑**」。v0.9 把后者从记忆库里的一行文字变成项目里可提交、可 review、可被 CI 读的一份文件——一条工作流一个 JSON:

‎dsh-mneme/lib/api.js‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2750,13 +2750,14 @@ export function createApi(ctx, service, settings, commands, embedder, semantic =
27502750
return;
27512751
}
27522752
if (page.error) {
2753-
// 索引还在、盘上没了:409 让面板知道「重跑一次 T0 就能好」,不是 404 那种
2754-
// 「这条路本来就不存在」。
2753+
// 409 保留已登记的页与未知 slug 的区别,恢复方式取决于文件状态。
27552754
sendJson(res, 409, {
27562755
error: page.error,
27572756
slug: page.slug,
27582757
relPath: page.relPath,
2759-
hint: "run `dsh-mneme-choro <repo> --apply` to rebuild"
2758+
hint: page.error === "page-missing"
2759+
? "run `dsh-mneme-choro <repo> --apply` to rebuild"
2760+
: "check the local file and its permissions"
27602761
});
27612762
return;
27622763
}

‎dsh-mneme/lib/tools.js‎

Lines changed: 36 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import { normalizeScopeStateDimension, normalizeScopeStateValue, normalizeScopeS
1111
import { lastUserQuery } from "./inject.js";
1212

1313
const TEXT_OUTPUT = (text) => [{ type: "text", text }];
14+
const incompleteChoroSearch = (pages) =>
15+
`Search was incomplete because these pages could not be read (${pages.join(", ")}). Check the local checkout and page permissions.`;
1416
// Per-registry tool-name registry: guards against duplicate registration on
1517
// live patch reload (DSH Desktop `patchReload: "live"`) where a plugin may be
1618
// re-applied on the same tools registry without an intervening unregister.
@@ -92,14 +94,17 @@ export function createTools(ctx, service, config, embedder, sessionGate = null,
9294
// flag 关闭时恒返回 null——写入不标注,检索不加权,行为与 A1 前完全一致。
9395
// logger 透传:registry 反查失败时 warnOnce 才有出口(否则静默降级无观测)。
9496
const resolveSessionScope = createScopeResolver({ ctx, config, logger: ctx.logger });
95-
// choro 工具共用的 repo 解析(M1):不带 repo 且只有一个登记仓时自动收敛;
96-
// 多仓歧义时把可选清单带回给模型(render 层会把它转成人话)。三个 execute 里
97-
// 都先走这里,错误形状统一 { error, ... },output.schema 容忍多余键由
98-
// additionalProperties:false + render 兜底——错误分支直接透传给渲染层判。
97+
// 宿主在 render 前按成功 schema 校验返回值,仓库解析失败要走工具错误通道。
9998
const resolveChoroRepo = (ref) => {
100-
const row = choro ? choro.resolveRepo(ref) : null;
101-
if (!row) return { error: "no-repos", hint: "run `dsh-mneme-choro <repo> --apply` first (registers the repository)" };
102-
if (row.ambiguous) return { error: "repo-ambiguous", repos: row.ambiguous, hint: "pass repo id or name" };
99+
const row = choro.resolveRepo(ref);
100+
if (!row) {
101+
const reason = ref ? `Repository ${JSON.stringify(ref)} is not registered.` : "No repository is registered for chorography.";
102+
throw new Error(`${reason} Run \`dsh-mneme-choro <repo> --apply\` to register a repository.`);
103+
}
104+
if (row.ambiguous) {
105+
const choices = row.ambiguous.map(({ id, name }) => `${id} (${name})`).join(", ");
106+
throw new Error(`More than one repository is registered. Pass one of these repository IDs (${choices}).`);
107+
}
103108
return row;
104109
};
105110
// #249 能力说明(injectGuidanceEnabled):开启时给「何时用」最有歧义的两个
@@ -1185,7 +1190,6 @@ export function createTools(ctx, service, config, embedder, sessionGate = null,
11851190
},
11861191
async execute(args) {
11871192
const repo = resolveChoroRepo(args?.repo);
1188-
if (repo.error) return repo;
11891193
const { modules, edges } = choro.moduleMap(repo.id);
11901194
return { repo: repo.name, modules, edges };
11911195
}
@@ -1211,10 +1215,16 @@ export function createTools(ctx, service, config, embedder, sessionGate = null,
12111215
},
12121216
async execute(args) {
12131217
const repo = resolveChoroRepo(args?.repo);
1214-
if (repo.error) return repo;
12151218
const page = choro.readPage(repo.id, args?.slug);
1216-
if (!page) return { error: "page-not-found", slug: String(args?.slug ?? ""), hint: "use choro_module_map to list slugs" };
1217-
if (page.error) return page;
1219+
if (!page) {
1220+
throw new Error(`Page ${JSON.stringify(args?.slug ?? "")} was not found. Use choro_module_map to list page slugs.`);
1221+
}
1222+
if (page.error === "page-missing") {
1223+
throw new Error(`Page ${JSON.stringify(page.slug)} is missing from disk (${page.relPath}). Check the local checkout or run \`dsh-mneme-choro <repo> --apply\` to rebuild it.`);
1224+
}
1225+
if (page.error) {
1226+
throw new Error(`Page ${JSON.stringify(page.slug)} could not be read (${page.relPath}). Check the local file and its permissions.`);
1227+
}
12181228
return { slug: page.slug, content: page.content };
12191229
}
12201230
}),
@@ -1233,26 +1243,29 @@ export function createTools(ctx, service, config, embedder, sessionGate = null,
12331243
type: "object",
12341244
additionalProperties: false,
12351245
properties: {
1236-
hits: { type: "array", required: true, items: { type: "object", additionalProperties: true } }
1246+
hits: { type: "array", required: true, items: { type: "object", additionalProperties: true } },
1247+
unreadablePages: { type: "array", items: { type: "string" }, description: "Page slugs that could not be read during the search" }
12371248
}
12381249
},
12391250
render: (_args, value) => {
1240-
const hits = value.hits ?? [];
1251+
const { hits, unreadablePages } = value;
12411252
if (hits.length === 0) return TEXT_OUTPUT("0 hits.");
1242-
return TEXT_OUTPUT(hits
1243-
.map((hit) => `${hit.slug} · ${hit.section} · L${hit.line}: ${hit.excerpt}`)
1244-
.join("\n"));
1253+
const lines = hits.map((hit) => `${hit.slug} · ${hit.section} · L${hit.line}: ${hit.excerpt}`);
1254+
if (unreadablePages?.length) lines.push("", incompleteChoroSearch(unreadablePages));
1255+
return TEXT_OUTPUT(lines.join("\n"));
12451256
}
12461257
},
12471258
async execute(args) {
12481259
const repo = resolveChoroRepo(args?.repo);
1249-
if (repo.error) return repo;
1250-
return {
1251-
hits: choro.search(repo.id, args?.query, {
1252-
moduleSlug: args?.module ?? null,
1253-
limit: args?.limit
1254-
})
1255-
};
1260+
const unreadablePages = [];
1261+
const hits = choro.search(repo.id, args?.query, {
1262+
moduleSlug: args?.module ?? null,
1263+
limit: args?.limit,
1264+
onUnreadablePage: (slug) => unreadablePages.push(slug)
1265+
});
1266+
if (unreadablePages.length === 0) return { hits };
1267+
if (hits.length === 0) throw new Error(incompleteChoroSearch(unreadablePages));
1268+
return { hits, unreadablePages };
12561269
}
12571270
})
12581271
];

‎dsh-mneme/lib/wiki/queries.js‎

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,8 @@ export function createChoroQueries({ store }) {
8686
}
8787

8888
/**
89-
* 读一页正文。slug 必须命中 DB 的模块行(P12:路径零用户输入);磁盘文件被手删
90-
* 时返回 { error: "page-missing" }(索引还没重算,读侧如实报缺,不伪造内容)。
89+
* 读一页正文。slug 必须命中 DB 的模块行(P12:路径零用户输入);索引仍在但
90+
* 文件缺失或不可读时带回相对路径,让调用方给出对应的恢复提示。
9191
*/
9292
function readPage(repoId, slug) {
9393
const row = db.prepare(
@@ -99,7 +99,13 @@ export function createChoroQueries({ store }) {
9999
if (!row) return null;
100100
const repo = db.prepare("SELECT root_path, name FROM choro_repos WHERE id = ?").get(repoId);
101101
const abs = join(repo.root_path, row.rel_path);
102-
if (!existsSync(abs)) return { error: "page-missing", slug: row.slug, relPath: row.rel_path };
102+
let content;
103+
try {
104+
content = readFileSync(abs, "utf8");
105+
} catch (cause) {
106+
const error = cause.code === "ENOENT" || cause.code === "ENOTDIR" ? "page-missing" : "page-unreadable";
107+
return { error, slug: row.slug, relPath: row.rel_path };
108+
}
103109
return {
104110
slug: row.slug,
105111
title: row.title,
@@ -111,16 +117,17 @@ export function createChoroQueries({ store }) {
111117
humanBlockCount: row.human_block_count,
112118
repoName: repo.name,
113119
relPath: row.rel_path,
114-
content: readFileSync(abs, "utf8"),
120+
content,
115121
};
116122
}
117123

118124
/**
119125
* 关键词检索(P10):按 `## ` 分节做大小写不敏感子串匹配,命中返回页 slug、
120126
* 节标题、起始行号与摘录。只扫 DB 里登记过的页面文件(不是全目录 glob——
121-
* 划分变更后未重算的孤儿文件不该被搜出来)。q 为空返回空数组不报错。
127+
* 划分变更后未重算的孤儿文件不该被搜出来)。需要提示检索不完整的调用方可接收
128+
* 不可读的页 slug。q 为空返回空数组不报错。
122129
*/
123-
function search(repoId, q, { moduleSlug = null, limit = 20 } = {}) {
130+
function search(repoId, q, { moduleSlug = null, limit = 20, onUnreadablePage } = {}) {
124131
const needle = String(q ?? "").trim().toLowerCase();
125132
if (needle === "") return [];
126133
const cap = Math.max(1, Math.min(100, Number(limit) || 20));
@@ -134,11 +141,11 @@ export function createChoroQueries({ store }) {
134141
const hits = [];
135142
for (const page of pages) {
136143
const abs = join(repo.root_path, page.rel_path);
137-
if (!existsSync(abs)) continue;
138144
let text;
139145
try {
140146
text = readFileSync(abs, "utf8");
141147
} catch {
148+
onUnreadablePage?.(page.slug);
142149
continue;
143150
}
144151
const lines = text.split("\n");

‎dsh-mneme/src/api.js‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2750,13 +2750,14 @@ export function createApi(ctx, service, settings, commands, embedder, semantic =
27502750
return;
27512751
}
27522752
if (page.error) {
2753-
// 索引还在、盘上没了:409 让面板知道「重跑一次 T0 就能好」,不是 404 那种
2754-
// 「这条路本来就不存在」。
2753+
// 409 保留已登记的页与未知 slug 的区别,恢复方式取决于文件状态。
27552754
sendJson(res, 409, {
27562755
error: page.error,
27572756
slug: page.slug,
27582757
relPath: page.relPath,
2759-
hint: "run `dsh-mneme-choro <repo> --apply` to rebuild"
2758+
hint: page.error === "page-missing"
2759+
? "run `dsh-mneme-choro <repo> --apply` to rebuild"
2760+
: "check the local file and its permissions"
27602761
});
27612762
return;
27622763
}

‎dsh-mneme/src/tools.js‎

Lines changed: 36 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import { normalizeScopeStateDimension, normalizeScopeStateValue, normalizeScopeS
1111
import { lastUserQuery } from "./inject.js";
1212

1313
const TEXT_OUTPUT = (text) => [{ type: "text", text }];
14+
const incompleteChoroSearch = (pages) =>
15+
`Search was incomplete because these pages could not be read (${pages.join(", ")}). Check the local checkout and page permissions.`;
1416
// Per-registry tool-name registry: guards against duplicate registration on
1517
// live patch reload (DSH Desktop `patchReload: "live"`) where a plugin may be
1618
// re-applied on the same tools registry without an intervening unregister.
@@ -92,14 +94,17 @@ export function createTools(ctx, service, config, embedder, sessionGate = null,
9294
// flag 关闭时恒返回 null——写入不标注,检索不加权,行为与 A1 前完全一致。
9395
// logger 透传:registry 反查失败时 warnOnce 才有出口(否则静默降级无观测)。
9496
const resolveSessionScope = createScopeResolver({ ctx, config, logger: ctx.logger });
95-
// choro 工具共用的 repo 解析(M1):不带 repo 且只有一个登记仓时自动收敛;
96-
// 多仓歧义时把可选清单带回给模型(render 层会把它转成人话)。三个 execute 里
97-
// 都先走这里,错误形状统一 { error, ... },output.schema 容忍多余键由
98-
// additionalProperties:false + render 兜底——错误分支直接透传给渲染层判。
97+
// 宿主在 render 前按成功 schema 校验返回值,仓库解析失败要走工具错误通道。
9998
const resolveChoroRepo = (ref) => {
100-
const row = choro ? choro.resolveRepo(ref) : null;
101-
if (!row) return { error: "no-repos", hint: "run `dsh-mneme-choro <repo> --apply` first (registers the repository)" };
102-
if (row.ambiguous) return { error: "repo-ambiguous", repos: row.ambiguous, hint: "pass repo id or name" };
99+
const row = choro.resolveRepo(ref);
100+
if (!row) {
101+
const reason = ref ? `Repository ${JSON.stringify(ref)} is not registered.` : "No repository is registered for chorography.";
102+
throw new Error(`${reason} Run \`dsh-mneme-choro <repo> --apply\` to register a repository.`);
103+
}
104+
if (row.ambiguous) {
105+
const choices = row.ambiguous.map(({ id, name }) => `${id} (${name})`).join(", ");
106+
throw new Error(`More than one repository is registered. Pass one of these repository IDs (${choices}).`);
107+
}
103108
return row;
104109
};
105110
// #249 能力说明(injectGuidanceEnabled):开启时给「何时用」最有歧义的两个
@@ -1185,7 +1190,6 @@ export function createTools(ctx, service, config, embedder, sessionGate = null,
11851190
},
11861191
async execute(args) {
11871192
const repo = resolveChoroRepo(args?.repo);
1188-
if (repo.error) return repo;
11891193
const { modules, edges } = choro.moduleMap(repo.id);
11901194
return { repo: repo.name, modules, edges };
11911195
}
@@ -1211,10 +1215,16 @@ export function createTools(ctx, service, config, embedder, sessionGate = null,
12111215
},
12121216
async execute(args) {
12131217
const repo = resolveChoroRepo(args?.repo);
1214-
if (repo.error) return repo;
12151218
const page = choro.readPage(repo.id, args?.slug);
1216-
if (!page) return { error: "page-not-found", slug: String(args?.slug ?? ""), hint: "use choro_module_map to list slugs" };
1217-
if (page.error) return page;
1219+
if (!page) {
1220+
throw new Error(`Page ${JSON.stringify(args?.slug ?? "")} was not found. Use choro_module_map to list page slugs.`);
1221+
}
1222+
if (page.error === "page-missing") {
1223+
throw new Error(`Page ${JSON.stringify(page.slug)} is missing from disk (${page.relPath}). Check the local checkout or run \`dsh-mneme-choro <repo> --apply\` to rebuild it.`);
1224+
}
1225+
if (page.error) {
1226+
throw new Error(`Page ${JSON.stringify(page.slug)} could not be read (${page.relPath}). Check the local file and its permissions.`);
1227+
}
12181228
return { slug: page.slug, content: page.content };
12191229
}
12201230
}),
@@ -1233,26 +1243,29 @@ export function createTools(ctx, service, config, embedder, sessionGate = null,
12331243
type: "object",
12341244
additionalProperties: false,
12351245
properties: {
1236-
hits: { type: "array", required: true, items: { type: "object", additionalProperties: true } }
1246+
hits: { type: "array", required: true, items: { type: "object", additionalProperties: true } },
1247+
unreadablePages: { type: "array", items: { type: "string" }, description: "Page slugs that could not be read during the search" }
12371248
}
12381249
},
12391250
render: (_args, value) => {
1240-
const hits = value.hits ?? [];
1251+
const { hits, unreadablePages } = value;
12411252
if (hits.length === 0) return TEXT_OUTPUT("0 hits.");
1242-
return TEXT_OUTPUT(hits
1243-
.map((hit) => `${hit.slug} · ${hit.section} · L${hit.line}: ${hit.excerpt}`)
1244-
.join("\n"));
1253+
const lines = hits.map((hit) => `${hit.slug} · ${hit.section} · L${hit.line}: ${hit.excerpt}`);
1254+
if (unreadablePages?.length) lines.push("", incompleteChoroSearch(unreadablePages));
1255+
return TEXT_OUTPUT(lines.join("\n"));
12451256
}
12461257
},
12471258
async execute(args) {
12481259
const repo = resolveChoroRepo(args?.repo);
1249-
if (repo.error) return repo;
1250-
return {
1251-
hits: choro.search(repo.id, args?.query, {
1252-
moduleSlug: args?.module ?? null,
1253-
limit: args?.limit
1254-
})
1255-
};
1260+
const unreadablePages = [];
1261+
const hits = choro.search(repo.id, args?.query, {
1262+
moduleSlug: args?.module ?? null,
1263+
limit: args?.limit,
1264+
onUnreadablePage: (slug) => unreadablePages.push(slug)
1265+
});
1266+
if (unreadablePages.length === 0) return { hits };
1267+
if (hits.length === 0) throw new Error(incompleteChoroSearch(unreadablePages));
1268+
return { hits, unreadablePages };
12561269
}
12571270
})
12581271
];

0 commit comments

Comments
 (0)