@octopi-agent/engine 0.86.38 → 0.90.11

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.
Files changed (83) hide show
  1. package/dist/config-schema/agent.js +12 -5
  2. package/dist/config-schema/context.d.ts +1 -1
  3. package/dist/config-schema/engine.d.ts +4 -4
  4. package/dist/config-schema/governance.js +3 -2
  5. package/dist/config-schema/models.d.ts +2 -2
  6. package/dist/config-schema/shared.d.ts +3 -0
  7. package/dist/config-schema/shared.js +12 -0
  8. package/dist/config-schema/substrate.d.ts +1 -1
  9. package/dist/config.d.ts +31 -9
  10. package/dist/config.js +66 -74
  11. package/dist/harness/agent/builder.d.ts +3 -2
  12. package/dist/harness/agent/builder.js +11 -7
  13. package/dist/harness/agent/config-bridge.js +4 -1
  14. package/dist/harness/agent/tool-purpose.d.ts +9 -1
  15. package/dist/harness/agent/tool-purpose.js +11 -2
  16. package/dist/harness/agent/wire-agent-builder.js +9 -2
  17. package/dist/harness/collaboration/autonomous-subsystem/boundary/validator.d.ts +11 -1
  18. package/dist/harness/collaboration/autonomous-subsystem/boundary/validator.js +11 -1
  19. package/dist/harness/collaboration/autonomous-subsystem/loader.d.ts +13 -0
  20. package/dist/harness/collaboration/autonomous-subsystem/loader.js +16 -1
  21. package/dist/harness/collaboration/autonomous-subsystem/runtime.d.ts +4 -0
  22. package/dist/harness/collaboration/autonomous-subsystem/runtime.js +3 -1
  23. package/dist/harness/context/default-context-engine.js +4 -4
  24. package/dist/harness/context/layer-types.d.ts +36 -0
  25. package/dist/harness/context/layer-types.js +38 -0
  26. package/dist/harness/context/root-instructions.js +1 -1
  27. package/dist/harness/context/system-prompt-assembler.js +10 -4
  28. package/dist/harness/context/tool-result-aging.js +2 -2
  29. package/dist/harness/context/turn-envelope.d.ts +1 -1
  30. package/dist/harness/context/turn-envelope.js +1 -1
  31. package/dist/harness/extension/execution-environment/effect-policy.d.ts +11 -1
  32. package/dist/harness/extension/execution-environment/effect-policy.js +24 -0
  33. package/dist/harness/extension/execution-environment/index.d.ts +1 -1
  34. package/dist/harness/extension/execution-environment/index.js +1 -1
  35. package/dist/harness/extension/execution-environment/isolation.js +5 -1
  36. package/dist/harness/extension/execution-environment/sandbox.js +36 -8
  37. package/dist/harness/extension/plugin-ecosystem/tools/allowlisted-run.d.ts +6 -0
  38. package/dist/harness/extension/plugin-ecosystem/tools/allowlisted-run.js +17 -0
  39. package/dist/harness/extension/plugin-ecosystem/tools/file-search.js +10 -17
  40. package/dist/harness/extension/plugin-ecosystem/tools/git-runner.d.ts +31 -0
  41. package/dist/harness/extension/plugin-ecosystem/tools/git-runner.js +104 -0
  42. package/dist/harness/extension/plugin-ecosystem/tools/git-status-snapshot.d.ts +111 -0
  43. package/dist/harness/extension/plugin-ecosystem/tools/git-status-snapshot.js +197 -0
  44. package/dist/harness/extension/plugin-ecosystem/tools/git-tools.d.ts +8 -8
  45. package/dist/harness/extension/plugin-ecosystem/tools/git-tools.js +9 -100
  46. package/dist/harness/extension/plugin-ecosystem/tools/index.d.ts +4 -0
  47. package/dist/harness/extension/plugin-ecosystem/tools/index.js +2 -0
  48. package/dist/harness/index.d.ts +2 -0
  49. package/dist/harness/index.js +1 -0
  50. package/dist/harness/knowledge/file-identity.d.ts +8 -0
  51. package/dist/harness/knowledge/file-identity.js +26 -1
  52. package/dist/harness/knowledge/grounding.js +5 -5
  53. package/dist/harness/knowledge/index-store.d.ts +31 -4
  54. package/dist/harness/knowledge/index-store.js +121 -39
  55. package/dist/harness/knowledge/ingest.d.ts +115 -115
  56. package/dist/harness/knowledge/ingest.js +150 -150
  57. package/dist/harness/knowledge/knowledge-serve-child.js +1 -1
  58. package/dist/harness/knowledge/purge.js +4 -1
  59. package/dist/harness/memory/backfill-coverage.js +3 -1
  60. package/dist/harness/observability/observer/hub.js +30 -1
  61. package/dist/harness/observability/observer/types.d.ts +23 -0
  62. package/dist/harness/run/runner.d.ts +16 -0
  63. package/dist/harness/run/runner.js +139 -26
  64. package/dist/harness/session/history/port.js +15 -5
  65. package/dist/harness/session/in-memory-store.d.ts +5 -0
  66. package/dist/harness/session/in-memory-store.js +33 -2
  67. package/dist/harness/session/title.js +25 -9
  68. package/dist/harness/shared/events/agent-event-map.d.ts +26 -0
  69. package/dist/harness/shared/events/agent-event-map.js +2 -0
  70. package/dist/index.d.ts +4 -2
  71. package/dist/index.js +3 -2
  72. package/dist/integration/observability/run-telemetry.d.ts +8 -0
  73. package/dist/integration/observability/run-telemetry.js +31 -8
  74. package/dist/integration/observability/trace-collector.js +26 -4
  75. package/dist/integration/observability/trace-events.d.ts +1 -0
  76. package/dist/integration/observability/trace-events.js +2 -0
  77. package/dist/integration/observability/trace-logger.js +27 -17
  78. package/dist/integration/storage/jsonl.d.ts +19 -1
  79. package/dist/integration/storage/jsonl.js +96 -3
  80. package/dist/paths.d.ts +19 -0
  81. package/dist/paths.js +38 -1
  82. package/dist/subsystems/safety-guard/config.yaml +3 -1
  83. package/package.json +2 -2
@@ -47,6 +47,7 @@ export { CapabilityRegistry } from './extension/plugin-ecosystem/plugins/capabil
47
47
  export { validateManifest, parseManifest } from './extension/plugin-ecosystem/plugins/manifest.js';
48
48
  export { DefaultToolBus } from './extension/plugin-ecosystem/tools/tool-bus.js';
49
49
  export { getBuiltinTools, createShellTool, createFileReadTool, createFileWriteTool, createFileDeleteTool, createFileListTool } from './extension/plugin-ecosystem/tools/builtin.js';
50
+ export { readGitStatusSnapshot, parsePorcelainV2 } from './extension/plugin-ecosystem/tools/git-status-snapshot.js';
50
51
  export { DefaultSkillManager, FileSystemSkillSource } from './extension/plugin-ecosystem/skills/manager.js';
51
52
  export { IssueRegistry } from './observability/diagnostics/index.js';
52
53
  export { CommandRouter, parseCommand, normalizeCommandName, createBuiltinCommands, createClientCatalogCommand, skillCommandsFromManager, loadUserCommandDefs, pluginCommandsFromManager, } from './extension/plugin-ecosystem/commands/index.js';
@@ -28,6 +28,14 @@ export interface StatLike {
28
28
  export declare function normalizePathLexical(input: string): string;
29
29
  /** 有则 realpath,否则词法 */
30
30
  export declare function normalizeFsPath(input: string): Promise<string>;
31
+ /**
32
+ * 真实文件身份键(同步):realpath 归一(解析 symlink 与 `/var`↔`/private/var` 等平台别名)。
33
+ * 供 Membership 匹配层做「同一真实文件」判定,勿按 cwd 猜身份。
34
+ *
35
+ * @param input - 路径(词法归一后必须是绝对路径)
36
+ * @returns realpath 归一形态;相对路径或盘上不存在时返回 null(此时调用方只信词法形态)
37
+ */
38
+ export declare function realIdentitySync(input: string): string | null;
31
39
  export declare function foldPathForCompare(p: string, kind: 'win' | 'posix'): string;
32
40
  export declare function pathCompareKind(normalized: string): 'win' | 'posix';
33
41
  /**
@@ -6,6 +6,7 @@
6
6
  */
7
7
  import { stat } from 'node:fs/promises';
8
8
  import { realpath } from 'node:fs/promises';
9
+ import { realpathSync } from 'node:fs';
9
10
  import path from 'node:path';
10
11
  /**
11
12
  * Windows / POSIX 路径词法归一(§7)。不要求存在。
@@ -68,6 +69,30 @@ export async function normalizeFsPath(input) {
68
69
  return lexical;
69
70
  }
70
71
  }
72
+ /**
73
+ * 真实文件身份键(同步):realpath 归一(解析 symlink 与 `/var`↔`/private/var` 等平台别名)。
74
+ * 供 Membership 匹配层做「同一真实文件」判定,勿按 cwd 猜身份。
75
+ *
76
+ * @param input - 路径(词法归一后必须是绝对路径)
77
+ * @returns realpath 归一形态;相对路径或盘上不存在时返回 null(此时调用方只信词法形态)
78
+ */
79
+ export function realIdentitySync(input) {
80
+ let lexical;
81
+ try {
82
+ lexical = normalizePathLexical(input);
83
+ }
84
+ catch {
85
+ // 相对路径:拒绝按 cwd 解析(会把「old.md」猜成无关文件的身份)
86
+ return null;
87
+ }
88
+ try {
89
+ return normalizePathLexical(realpathSync(lexical));
90
+ }
91
+ catch {
92
+ // 盘上不存在(已删文件/合成路径):无别名可解析
93
+ return null;
94
+ }
95
+ }
71
96
  export function foldPathForCompare(p, kind) {
72
97
  return kind === 'win' ? p.toLowerCase() : p;
73
98
  }
@@ -88,7 +113,7 @@ export function isPathPrefix(prefix, full) {
88
113
  export function canonicalizeUrl(raw) {
89
114
  const u = new URL(raw);
90
115
  u.hash = '';
91
- // **保留 query**:?id=1 / ?id=2 大概率是不同文档(契约修订)
116
+ // **保留 query**:`?id=1` / `?id=2` 大概率是不同文档(契约修订)
92
117
  u.hostname = u.hostname.toLowerCase();
93
118
  if ((u.protocol === 'http:' && u.port === '80') ||
94
119
  (u.protocol === 'https:' && u.port === '443')) {
@@ -6,14 +6,14 @@
6
6
  import { getTextContent } from '@octopi-agent/core/types.js';
7
7
  /** 消息 metadata.source 标记 */
8
8
  export const KNOWLEDGE_GROUNDING_SOURCE = 'knowledgeGrounding';
9
- /** 引擎载体 user 插槽(快照/grounding/附件/摘要)不是用户发言,query 不采信(谓词见 context/layer-types) */
10
- import { isEngineCarrierSource } from '../context/layer-types.js';
9
+ /** 真实用户发言判定(载体插槽不是用户发言,query 不采信;谓词见 context/layer-types) */
10
+ import { isHumanSpeech } from '../context/layer-types.js';
11
11
  /**
12
12
  * v1 query:最近 user + 可选前 1–2 条 user;不用 assistant 长文
13
13
  */
14
14
  export function resolveGroundingQuery(messages, opts) {
15
15
  const prior = Math.max(0, opts?.includePriorUserTurns ?? 1);
16
- const users = messages.filter((m) => m.role === 'user' && !isEngineCarrierSource(m.metadata?.source));
16
+ const users = messages.filter((m) => isHumanSpeech(m));
17
17
  const recent = users.slice(-1 - prior);
18
18
  const parts = recent.map((m) => getTextContent(m.content).trim()).filter(Boolean);
19
19
  const joined = parts.join('\n');
@@ -62,10 +62,10 @@ export function formatKnowledgeGroundingMessage(pack) {
62
62
  function estimateTokens(text) {
63
63
  return Math.ceil(text.length / 4);
64
64
  }
65
- /** 最近一条 user 的文本(skipIf 启发式只看本轮) */
65
+ /** 最近一条真实用户发言的文本(skipIf 启发式只看本轮;载体插槽不计) */
66
66
  function latestUserText(messages) {
67
67
  for (let i = messages.length - 1; i >= 0; i--) {
68
- if (messages[i].role === 'user') {
68
+ if (isHumanSpeech(messages[i])) {
69
69
  return getTextContent(messages[i].content);
70
70
  }
71
71
  }
@@ -141,15 +141,32 @@ export declare class KnowledgeIndexStore {
141
141
  ext?: string;
142
142
  max?: number;
143
143
  }): string[];
144
- /** 解绑 path;File 仅在零认领时 purge(单文件同步语义,供测试/轻量路径) */
145
- removeFile(sourceId: KnowledgeSourceId | string, path: string): void;
144
+ /**
145
+ * Membership 视图解析:query 的 logical 形态 → 该真实文件在本 source 下的全部 logical_path。
146
+ * 快路径字面命中;未命中再按真实文件身份(realpath)比对——symlink / `/var`↔`/private/var`
147
+ * 别名视为同一 path(合规 purge 不得因路径形态不同而漏删)。
148
+ *
149
+ * @param sourceId - 源 id
150
+ * @param logical - 查询路径(已 slash 归一)
151
+ * @returns 命中的存储字面 logical_path 列表(可能含多种拼写形态)
152
+ */
153
+ private resolveMembershipPaths;
154
+ /**
155
+ * 解绑 path;File 仅在零认领时 purge(单文件同步语义,供测试/轻量路径)。
156
+ * 按真实文件身份匹配(见 resolveMembershipPaths)。
157
+ *
158
+ * @returns 实际解绑的存储字面 logical_path(供 hit log 按真实字面清痕)
159
+ */
160
+ removeFile(sourceId: KnowledgeSourceId | string, path: string): string[];
146
161
  /**
147
162
  * 解绑 path + 可能的 purge(异步让出)。
148
163
  * 单文件 purge 也可能删数千 chunk——必须让出,否则 Engine 调度不了 HTTP 回包。
164
+ *
165
+ * @returns 实际解绑的存储字面 logical_path(供 hit log 按真实字面清痕)
149
166
  */
150
167
  removeFileAsync(sourceId: KnowledgeSourceId | string, path: string, opts?: {
151
168
  yieldEvery?: number;
152
- }): Promise<void>;
169
+ }): Promise<string[]>;
153
170
  /**
154
171
  * 批量解绑:每 `yieldEvery` 条让出事件循环。
155
172
  * 删大目录(数百文件)时同步连环 DELETE 会堵死 Engine,HTTP/Query 回包都无法调度。
@@ -158,6 +175,15 @@ export declare class KnowledgeIndexStore {
158
175
  yieldEvery?: number;
159
176
  }): Promise<number>;
160
177
  removeFiles(sourceId: KnowledgeSourceId | string, paths: string[]): void;
178
+ /**
179
+ * 子树成员收集:前缀 SQL 命中 ∪ 真实身份落在子树内的别名形态
180
+ * (防 `/var`↔`/private/var`、symlink 前缀漏删)。
181
+ *
182
+ * @param sourceId - 源 id
183
+ * @param prefix - 子树前缀(已 slash 归一、去尾斜杠)
184
+ * @returns 命中的存储字面 logical_path 列表(去重)
185
+ */
186
+ private collectTreeMembershipPaths;
161
187
  /**
162
188
  * 删除 path 及子树:先收集 logical_path,再分批异步解绑 + purge。
163
189
  */
@@ -172,7 +198,8 @@ export declare class KnowledgeIndexStore {
172
198
  yieldEvery?: number;
173
199
  }): Promise<number>;
174
200
  clearSource(sourceId: KnowledgeSourceId | string): void;
175
- purgePath(sourceId: KnowledgeSourceId | string, path: string): void;
201
+ /** 单文件 purge:透传真实身份匹配到的存储字面(供 hit log 清痕) */
202
+ purgePath(sourceId: KnowledgeSourceId | string, path: string): string[];
176
203
  /** 按 fileId 物理删除(membership 已清) */
177
204
  purgeFile(fileId: string): void;
178
205
  purgeFileAsync(fileId: string, opts?: {
@@ -10,7 +10,7 @@ import { KnowledgeFts } from './fts.js';
10
10
  import { vectorBucket } from './vector-ann.js';
11
11
  import { toVecBlob } from '../memory/sqlite/sqlite-vec.js';
12
12
  import { asChunkId, asSourceId } from './types.js';
13
- import { normalizePathLexical } from './file-identity.js';
13
+ import { normalizePathLexical, realIdentitySync } from './file-identity.js';
14
14
  const EMBEDDABLE_WHERE = "(f.adapter_id IS NULL OR f.adapter_id NOT IN ('code-tree'))";
15
15
  function defaultIdentityKey(path) {
16
16
  try {
@@ -494,42 +494,100 @@ export class KnowledgeIndexStore {
494
494
  out = out.slice(0, filter.max);
495
495
  return out;
496
496
  }
497
- /** 解绑 path;File 仅在零认领时 purge(单文件同步语义,供测试/轻量路径) */
497
+ /**
498
+ * Membership 视图解析:query 的 logical 形态 → 该真实文件在本 source 下的全部 logical_path。
499
+ * 快路径字面命中;未命中再按真实文件身份(realpath)比对——symlink / `/var`↔`/private/var`
500
+ * 别名视为同一 path(合规 purge 不得因路径形态不同而漏删)。
501
+ *
502
+ * @param sourceId - 源 id
503
+ * @param logical - 查询路径(已 slash 归一)
504
+ * @returns 命中的存储字面 logical_path 列表(可能含多种拼写形态)
505
+ */
506
+ resolveMembershipPaths(sourceId, logical) {
507
+ const exact = this.db.raw
508
+ .prepare(`SELECT logical_path FROM knowledge_memberships WHERE source_id = ? AND logical_path = ?`)
509
+ .all(sourceId, logical);
510
+ if (exact.length > 0)
511
+ return [...new Set(exact.map((r) => r.logical_path))];
512
+ const queryReal = realIdentitySync(logical);
513
+ if (queryReal === null)
514
+ return [];
515
+ const rows = this.db.raw
516
+ .prepare('SELECT DISTINCT logical_path FROM knowledge_memberships WHERE source_id = ?')
517
+ .all(sourceId);
518
+ const cache = new Map();
519
+ return rows
520
+ .filter((r) => {
521
+ let id = cache.get(r.logical_path);
522
+ if (id === undefined) {
523
+ id = realIdentitySync(r.logical_path);
524
+ cache.set(r.logical_path, id);
525
+ }
526
+ return id !== null && id === queryReal;
527
+ })
528
+ .map((r) => r.logical_path);
529
+ }
530
+ /**
531
+ * 解绑 path;File 仅在零认领时 purge(单文件同步语义,供测试/轻量路径)。
532
+ * 按真实文件身份匹配(见 resolveMembershipPaths)。
533
+ *
534
+ * @returns 实际解绑的存储字面 logical_path(供 hit log 按真实字面清痕)
535
+ */
498
536
  removeFile(sourceId, path) {
499
537
  const logical = path.replace(/\\/g, '/');
500
- const row = this.db.raw
501
- .prepare(`SELECT file_id FROM knowledge_memberships WHERE source_id = ? AND logical_path = ?`)
502
- .get(String(sourceId), logical);
503
- this.db.raw
504
- .prepare('DELETE FROM knowledge_memberships WHERE source_id = ? AND logical_path = ?')
505
- .run(String(sourceId), logical);
506
- if (!row)
507
- return;
508
- const n = this.db.raw
509
- .prepare('SELECT COUNT(*) AS n FROM knowledge_memberships WHERE file_id = ?')
510
- .get(row.file_id).n;
511
- if (n === 0)
512
- this.purgeFileRow(row.file_id);
538
+ const matched = this.resolveMembershipPaths(String(sourceId), logical);
539
+ if (matched.length === 0)
540
+ return [];
541
+ const fileIds = new Set();
542
+ for (const stored of matched) {
543
+ const row = this.db.raw
544
+ .prepare(`SELECT file_id FROM knowledge_memberships WHERE source_id = ? AND logical_path = ?`)
545
+ .get(String(sourceId), stored);
546
+ if (row)
547
+ fileIds.add(row.file_id);
548
+ this.db.raw
549
+ .prepare('DELETE FROM knowledge_memberships WHERE source_id = ? AND logical_path = ?')
550
+ .run(String(sourceId), stored);
551
+ }
552
+ for (const fileId of fileIds) {
553
+ const n = this.db.raw
554
+ .prepare('SELECT COUNT(*) AS n FROM knowledge_memberships WHERE file_id = ?')
555
+ .get(fileId).n;
556
+ if (n === 0)
557
+ this.purgeFileRow(fileId);
558
+ }
559
+ return matched;
513
560
  }
514
561
  /**
515
562
  * 解绑 path + 可能的 purge(异步让出)。
516
563
  * 单文件 purge 也可能删数千 chunk——必须让出,否则 Engine 调度不了 HTTP 回包。
564
+ *
565
+ * @returns 实际解绑的存储字面 logical_path(供 hit log 按真实字面清痕)
517
566
  */
518
567
  async removeFileAsync(sourceId, path, opts) {
519
568
  const logical = path.replace(/\\/g, '/');
520
- const row = this.db.raw
521
- .prepare(`SELECT file_id FROM knowledge_memberships WHERE source_id = ? AND logical_path = ?`)
522
- .get(String(sourceId), logical);
523
- this.db.raw
524
- .prepare('DELETE FROM knowledge_memberships WHERE source_id = ? AND logical_path = ?')
525
- .run(String(sourceId), logical);
526
- if (!row)
527
- return;
528
- const n = this.db.raw
529
- .prepare('SELECT COUNT(*) AS n FROM knowledge_memberships WHERE file_id = ?')
530
- .get(row.file_id).n;
531
- if (n === 0)
532
- await this.purgeFileRowAsync(row.file_id, opts);
569
+ const matched = this.resolveMembershipPaths(String(sourceId), logical);
570
+ if (matched.length === 0)
571
+ return [];
572
+ const fileIds = new Set();
573
+ for (const stored of matched) {
574
+ const row = this.db.raw
575
+ .prepare(`SELECT file_id FROM knowledge_memberships WHERE source_id = ? AND logical_path = ?`)
576
+ .get(String(sourceId), stored);
577
+ if (row)
578
+ fileIds.add(row.file_id);
579
+ this.db.raw
580
+ .prepare('DELETE FROM knowledge_memberships WHERE source_id = ? AND logical_path = ?')
581
+ .run(String(sourceId), stored);
582
+ }
583
+ for (const fileId of fileIds) {
584
+ const n = this.db.raw
585
+ .prepare('SELECT COUNT(*) AS n FROM knowledge_memberships WHERE file_id = ?')
586
+ .get(fileId).n;
587
+ if (n === 0)
588
+ await this.purgeFileRowAsync(fileId, opts);
589
+ }
590
+ return matched;
533
591
  }
534
592
  /**
535
593
  * 批量解绑:每 `yieldEvery` 条让出事件循环。
@@ -552,24 +610,47 @@ export class KnowledgeIndexStore {
552
610
  this.removeFile(sourceId, p);
553
611
  }
554
612
  /**
555
- * 删除 path 及子树:先收集 logical_path,再分批异步解绑 + purge。
613
+ * 子树成员收集:前缀 SQL 命中 ∪ 真实身份落在子树内的别名形态
614
+ * (防 `/var`↔`/private/var`、symlink 前缀漏删)。
615
+ *
616
+ * @param sourceId - 源 id
617
+ * @param prefix - 子树前缀(已 slash 归一、去尾斜杠)
618
+ * @returns 命中的存储字面 logical_path 列表(去重)
556
619
  */
557
- async removePathTreeAsync(sourceId, path, opts) {
558
- const prefix = path.replace(/\\/g, '/').replace(/\/+$/, '');
620
+ collectTreeMembershipPaths(sourceId, prefix) {
559
621
  const rows = this.db.raw
560
622
  .prepare(`SELECT logical_path FROM knowledge_memberships
561
623
  WHERE source_id = ? AND (logical_path = ? OR logical_path LIKE ? ESCAPE '\\')`)
562
624
  .all(String(sourceId), prefix, `${prefix.replace(/[\\%_]/g, (c) => `\\${c}`)}/%`);
563
- return this.removeFilesAsync(sourceId, rows.map((r) => r.logical_path), opts);
625
+ const found = new Set(rows.map((r) => r.logical_path));
626
+ const prefixReal = realIdentitySync(prefix);
627
+ if (prefixReal !== null) {
628
+ const all = this.db.raw
629
+ .prepare('SELECT DISTINCT logical_path FROM knowledge_memberships WHERE source_id = ?')
630
+ .all(String(sourceId));
631
+ for (const r of all) {
632
+ if (found.has(r.logical_path))
633
+ continue;
634
+ const id = realIdentitySync(r.logical_path);
635
+ if (id !== null && (id === prefixReal || id.startsWith(`${prefixReal}/`))) {
636
+ found.add(r.logical_path);
637
+ }
638
+ }
639
+ }
640
+ return [...found];
641
+ }
642
+ /**
643
+ * 删除 path 及子树:先收集 logical_path,再分批异步解绑 + purge。
644
+ */
645
+ async removePathTreeAsync(sourceId, path, opts) {
646
+ const prefix = path.replace(/\\/g, '/').replace(/\/+$/, '');
647
+ return this.removeFilesAsync(sourceId, this.collectTreeMembershipPaths(String(sourceId), prefix), opts);
564
648
  }
565
649
  removePathTree(sourceId, path) {
566
650
  const prefix = path.replace(/\\/g, '/').replace(/\/+$/, '');
567
- const rows = this.db.raw
568
- .prepare(`SELECT logical_path FROM knowledge_memberships
569
- WHERE source_id = ? AND (logical_path = ? OR logical_path LIKE ? ESCAPE '\\')`)
570
- .all(String(sourceId), prefix, `${prefix.replace(/[\\%_]/g, (c) => `\\${c}`)}/%`);
571
- for (const r of rows)
572
- this.removeFile(sourceId, r.logical_path);
651
+ for (const stored of this.collectTreeMembershipPaths(String(sourceId), prefix)) {
652
+ this.removeFile(sourceId, stored);
653
+ }
573
654
  }
574
655
  /**
575
656
  * 清空源认领:分批 purge,避免一次清数千 File 堵死事件循环。
@@ -613,8 +694,9 @@ export class KnowledgeIndexStore {
613
694
  this.purgeFileRow(r.file_id);
614
695
  }
615
696
  }
697
+ /** 单文件 purge:透传真实身份匹配到的存储字面(供 hit log 清痕) */
616
698
  purgePath(sourceId, path) {
617
- this.removeFile(sourceId, path);
699
+ return this.removeFile(sourceId, path);
618
700
  }
619
701
  /** 按 fileId 物理删除(membership 已清) */
620
702
  purgeFile(fileId) {