@dommaker/harness 4.0.0 → 4.0.2
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/dist/bootstrap/bootstrap.d.ts +15 -0
- package/dist/bootstrap/bootstrap.js +18 -1
- package/dist/bootstrap/index.d.ts +2 -2
- package/dist/bootstrap/index.js +3 -2
- package/dist/cli/commands/acceptance.d.ts +8 -0
- package/dist/cli/commands/acceptance.js +45 -24
- package/dist/cli/commands/check.d.ts +2 -0
- package/dist/cli/commands/check.js +36 -5
- package/dist/cli/commands/command.js +1 -1
- package/dist/cli/commands/definitions.js +3 -0
- package/dist/cli/commands/init/ci-platform.d.ts +3 -5
- package/dist/cli/commands/init/ci-platform.js +3 -8
- package/dist/cli/commands/init/index.js +78 -11
- package/dist/cli/commands/release.d.ts +11 -3
- package/dist/cli/commands/release.js +159 -52
- package/dist/cli/commands/report.d.ts +2 -0
- package/dist/cli/commands/report.js +54 -6
- package/dist/cli/commands/security.d.ts +3 -3
- package/dist/cli/commands/security.js +3 -2
- package/dist/cli/commands/sync-docs/capabilities-syncer.d.ts +3 -40
- package/dist/cli/commands/sync-docs/capabilities-syncer.js +5 -159
- package/dist/cli/commands/sync-docs/compact.js +2 -2
- package/dist/cli/commands/sync-docs/context-syncer.d.ts +4 -36
- package/dist/cli/commands/sync-docs/context-syncer.js +4 -221
- package/dist/cli/commands/sync-docs/index.d.ts +8 -8
- package/dist/cli/commands/sync-docs/index.js +8 -8
- package/dist/cli/commands/sync-docs/main-flow.d.ts +5 -4
- package/dist/cli/commands/sync-docs/main-flow.js +6 -5
- package/dist/cli/commands/sync-docs/sync-output.d.ts +1 -1
- package/dist/cli/commands/sync-docs/sync-output.js +4 -0
- package/dist/cli/commands/sync-docs/sync-writes.d.ts +1 -1
- package/dist/cli/commands/validate.d.ts +2 -0
- package/dist/cli/commands/validate.js +40 -1
- package/dist/core/command-execution-policy.d.ts +59 -0
- package/dist/core/command-execution-policy.js +167 -0
- package/dist/core/constraints/capabilities-parser.d.ts +2 -1
- package/dist/core/constraints/capabilities-parser.js +2 -1
- package/dist/core/constraints/checker.d.ts +17 -5
- package/dist/core/constraints/checker.js +20 -10
- package/dist/core/constraints/checkers/no-hardcoded-credentials.js +14 -15
- package/dist/core/constraints/checkers/no-test-simplification.js +8 -6
- package/dist/core/constraints/checkers/templated/exec.d.ts +4 -0
- package/dist/core/constraints/checkers/templated/exec.js +11 -48
- package/dist/core/constraints/checkers/templated/file-exists.js +7 -4
- package/dist/core/constraints/checkers/templated/regex-scan.js +11 -15
- package/dist/core/constraints/checkers/types.d.ts +13 -2
- package/dist/core/constraints/checkers/types.js +5 -2
- package/dist/core/constraints/construction-sites.d.ts +1 -1
- package/dist/core/constraints/construction-sites.js +1 -1
- package/dist/core/constraints/injection-writer.d.ts +1 -1
- package/dist/core/constraints/injection-writer.js +1 -1
- package/dist/core/constraints/project-snapshot.d.ts +43 -0
- package/dist/core/constraints/project-snapshot.js +103 -0
- package/dist/core/constraints/run-env.d.ts +35 -0
- package/dist/core/constraints/run-env.js +36 -0
- package/dist/{cli/commands/sync-docs → core/docs-sync}/agents-syncer.d.ts +2 -0
- package/dist/{cli/commands/sync-docs → core/docs-sync}/agents-syncer.js +5 -3
- package/dist/core/docs-sync/capabilities-table.d.ts +46 -0
- package/dist/core/docs-sync/capabilities-table.js +200 -0
- package/dist/core/docs-sync/context-collector.d.ts +40 -0
- package/dist/core/docs-sync/context-collector.js +227 -0
- package/dist/{cli/commands/sync-docs → core/docs-sync}/project-reader.d.ts +5 -1
- package/dist/{cli/commands/sync-docs → core/docs-sync}/project-reader.js +10 -6
- package/dist/{cli/commands/sync-docs → core/docs-sync}/sync-state.d.ts +15 -9
- package/dist/{cli/commands/sync-docs → core/docs-sync}/sync-state.js +26 -29
- package/dist/core/validators/check-handlers/command.js +22 -0
- package/dist/core/validators/check-handlers/output.js +26 -3
- package/dist/core/validators/checkpoint.d.ts +4 -0
- package/dist/core/validators/checkpoint.js +10 -2
- package/dist/gates/acceptance.d.ts +8 -3
- package/dist/gates/acceptance.js +76 -13
- package/dist/gates/command.d.ts +3 -3
- package/dist/gates/command.js +2 -2
- package/dist/gates/contract.d.ts +3 -3
- package/dist/gates/performance.d.ts +10 -3
- package/dist/gates/performance.js +49 -4
- package/dist/gates/review.d.ts +3 -3
- package/dist/gates/security.d.ts +3 -3
- package/dist/gates/security.js +26 -18
- package/dist/gates/types.d.ts +45 -16
- package/dist/knowledge/ingest.d.ts +31 -0
- package/dist/knowledge/ingest.js +192 -52
- package/dist/knowledge/lifecycle.d.ts +15 -0
- package/dist/knowledge/lifecycle.js +62 -0
- package/dist/knowledge/query.d.ts +10 -0
- package/dist/knowledge/query.js +60 -8
- package/dist/knowledge/store.d.ts +56 -3
- package/dist/knowledge/store.js +108 -30
- package/dist/types/checkpoint.d.ts +28 -1
- package/dist/types/command-execution-policy.d.ts +73 -0
- package/dist/types/command-execution-policy.js +16 -0
- package/dist/utils/exec.d.ts +7 -1
- package/dist/utils/exec.js +7 -1
- package/dist/utils/yaml.d.ts +10 -0
- package/dist/utils/yaml.js +11 -0
- package/package.json +1 -1
- package/dist/core/constraints/check-cache.d.ts +0 -43
- package/dist/core/constraints/check-cache.js +0 -77
- package/dist/core/constraints/checkers/templated/glob.d.ts +0 -8
- package/dist/core/constraints/checkers/templated/glob.js +0 -14
- /package/dist/{cli/commands/sync-docs → core/docs-sync}/preserve-block.d.ts +0 -0
- /package/dist/{cli/commands/sync-docs → core/docs-sync}/preserve-block.js +0 -0
package/dist/gates/security.d.ts
CHANGED
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
* fail-fast:扫描输出必须是 npm audit 的 JSON;非 JSON 输出不再有
|
|
10
10
|
* 「正则抠数字」的文本兜底——解析失败直接抛出(外层归一为 deny 报告)。
|
|
11
11
|
*/
|
|
12
|
-
import type { GateResult,
|
|
12
|
+
import type { GateResult, SecurityGateContext, SecurityGateConfig, Gate, GateDecision } from './types';
|
|
13
13
|
/**
|
|
14
14
|
* 安全门禁
|
|
15
15
|
*/
|
|
@@ -21,11 +21,11 @@ export declare class SecurityGate implements Gate {
|
|
|
21
21
|
/**
|
|
22
22
|
* 统一门禁接口(G1):执行细节私有,决策三态由 scan() 报告推导
|
|
23
23
|
*/
|
|
24
|
-
evaluate(context:
|
|
24
|
+
evaluate(context: SecurityGateContext): Promise<GateDecision>;
|
|
25
25
|
/**
|
|
26
26
|
* 扫描安全漏洞
|
|
27
27
|
*/
|
|
28
|
-
scan(context:
|
|
28
|
+
scan(context: SecurityGateContext): Promise<GateResult>;
|
|
29
29
|
/**
|
|
30
30
|
* 检测扫描命令
|
|
31
31
|
*/
|
package/dist/gates/security.js
CHANGED
|
@@ -16,6 +16,13 @@ const exec_1 = require("../utils/exec");
|
|
|
16
16
|
const guards_1 = require("../utils/guards");
|
|
17
17
|
const types_1 = require("./types");
|
|
18
18
|
const decision_1 = require("./decision");
|
|
19
|
+
/**
|
|
20
|
+
* 默认审计固定可信 registry(D4):npm 命令行 `--registry` 旗标优先于项目
|
|
21
|
+
* .npmrc 的 registry 配置,项目 npm 配置不能改变默认审计来源。
|
|
22
|
+
* 仅缺省探测路径(detectScanCommand 第三级)使用;显式可信输入
|
|
23
|
+
* (context.securityScanCommand / 构造器 scanCommand)原样下发,不追加。
|
|
24
|
+
*/
|
|
25
|
+
const TRUSTED_NPM_REGISTRY = 'https://registry.npmjs.org/';
|
|
19
26
|
/** via 的首个来源标题(字符串直取,对象取 title) */
|
|
20
27
|
function viaTitle(via) {
|
|
21
28
|
const first = via?.[0];
|
|
@@ -82,8 +89,8 @@ class SecurityGate {
|
|
|
82
89
|
* 检测扫描命令
|
|
83
90
|
*/
|
|
84
91
|
detectScanCommand(_projectPath) {
|
|
85
|
-
// 默认使用 npm audit
|
|
86
|
-
return
|
|
92
|
+
// 默认使用 npm audit,固定可信 registry(D4,见 TRUSTED_NPM_REGISTRY)
|
|
93
|
+
return `npm audit --json --registry ${TRUSTED_NPM_REGISTRY}`;
|
|
87
94
|
}
|
|
88
95
|
/**
|
|
89
96
|
* 运行扫描
|
|
@@ -135,23 +142,24 @@ class SecurityGate {
|
|
|
135
142
|
if (!(0, guards_1.isRecord)(raw)) {
|
|
136
143
|
throw new Error('安全扫描输出不是 JSON 对象');
|
|
137
144
|
}
|
|
138
|
-
if ((0, guards_1.isRecord)(raw.vulnerabilities)) {
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
const severity = vuln.severity?.toLowerCase() ?? 'low';
|
|
148
|
-
count(severity);
|
|
149
|
-
vulnerabilities.push({
|
|
150
|
-
name,
|
|
151
|
-
severity,
|
|
152
|
-
via: viaTitle(vuln.via) ?? vuln.name ?? 'Unknown',
|
|
153
|
-
});
|
|
145
|
+
if (!(0, guards_1.isRecord)(raw.vulnerabilities)) {
|
|
146
|
+
throw new Error('安全扫描输出缺少有效的 vulnerabilities 对象');
|
|
147
|
+
}
|
|
148
|
+
for (const [name, entry] of Object.entries(raw.vulnerabilities)) {
|
|
149
|
+
// 畸形条目(非对象)跳过并计数——不伪装成空漏洞对象(原 `as NpmVulnerability`
|
|
150
|
+
// 会把字符串/数组条目当成 {severity: undefined} 的低危漏洞计入 total)
|
|
151
|
+
if (!(0, guards_1.isRecord)(entry)) {
|
|
152
|
+
skippedMalformed++;
|
|
153
|
+
continue;
|
|
154
154
|
}
|
|
155
|
+
const vuln = entry;
|
|
156
|
+
const severity = vuln.severity?.toLowerCase() ?? 'low';
|
|
157
|
+
count(severity);
|
|
158
|
+
vulnerabilities.push({
|
|
159
|
+
name,
|
|
160
|
+
severity,
|
|
161
|
+
via: viaTitle(vuln.via) ?? vuln.name ?? 'Unknown',
|
|
162
|
+
});
|
|
155
163
|
}
|
|
156
164
|
return {
|
|
157
165
|
critical,
|
package/dist/gates/types.d.ts
CHANGED
|
@@ -30,6 +30,11 @@ export declare function fromError(gate: string, label: string, error: unknown, s
|
|
|
30
30
|
*
|
|
31
31
|
* 统一的是决策协议(id / 声明式 order / evaluate → 三态决策),
|
|
32
32
|
* 各门禁执行细节(gh pr view / 正则黑名单 / OpenAPI diff / benchmark…)私有。
|
|
33
|
+
*
|
|
34
|
+
* 注意:各实现 evaluate 收窄使用专属输入类型(`ReviewGateContext` 等),
|
|
35
|
+
* 而当前没有经本接口多态调 evaluate 的生产消费者。TS 方法参数双变意味着
|
|
36
|
+
* 若未来新增这种调用,基础 `GateContext` 不会替你校验专属必填字段
|
|
37
|
+
* (如 `CommandGateContext.command`)——届时须先收紧接口形状/调用契约。
|
|
33
38
|
*/
|
|
34
39
|
export interface Gate {
|
|
35
40
|
/** 门禁 id(与 gates/definitions.ts 定义一致,注册表闭环校验) */
|
|
@@ -40,27 +45,55 @@ export interface Gate {
|
|
|
40
45
|
evaluate(ctx: GateContext): Promise<GateDecision> | GateDecision;
|
|
41
46
|
}
|
|
42
47
|
/**
|
|
43
|
-
*
|
|
48
|
+
* 门禁上下文:只带全部门禁真正共享的运行信息(在哪跑 / 跑什么 / 本 run 观察面)
|
|
44
49
|
*
|
|
45
|
-
*
|
|
46
|
-
*
|
|
50
|
+
* P1.4 收窄:各门禁专属输入字段(prNumber / securityScanCommand /
|
|
51
|
+
* performanceThresholds / oldContractPath / newContractPath / command /
|
|
52
|
+
* tasksPath)已迁到各门禁自己的输入类型(`ReviewGateContext` 等),
|
|
53
|
+
* 不再混进共享上下文。配置一律走构造器(`ReviewGateConfig` 等),
|
|
54
|
+
* 不要往这里加字段——本类型曾有 6 个无人读取的配置型字段与构造器
|
|
55
|
+
* 配置平行,填了不生效。
|
|
47
56
|
*/
|
|
48
57
|
export interface GateContext {
|
|
49
58
|
projectPath: string;
|
|
50
59
|
taskId?: string;
|
|
51
|
-
prNumber?: number;
|
|
52
|
-
securityScanCommand?: string;
|
|
53
|
-
performanceThresholds?: PerformanceThresholds;
|
|
54
|
-
oldContractPath?: string;
|
|
55
|
-
newContractPath?: string;
|
|
56
|
-
command?: string;
|
|
57
|
-
tasksPath?: string;
|
|
58
60
|
/**
|
|
59
61
|
* 本 run 的运行级观察面(ADR-0023)。一条守卫链传同一枚 → 项目配置文件整链至多读一次;
|
|
60
62
|
* 不传 = 各门禁自造一枚一次性观察面(判定不变,只是不共享读取)
|
|
61
63
|
*/
|
|
62
64
|
runEnv?: RunEnv;
|
|
63
65
|
}
|
|
66
|
+
/**
|
|
67
|
+
* 审查门禁输入:PR 号存在时走 GitHub API,否则落本地 git 检查
|
|
68
|
+
*/
|
|
69
|
+
export interface ReviewGateContext extends GateContext {
|
|
70
|
+
prNumber?: number;
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* 安全门禁输入:本次扫描命令覆盖(优先级高于构造器 `scanCommand`,见 SecurityGate.scan)
|
|
74
|
+
*/
|
|
75
|
+
export interface SecurityGateContext extends GateContext {
|
|
76
|
+
securityScanCommand?: string;
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* 性能门禁输入:本次阈值覆盖(优先级高于构造器 `thresholds`,见 PerformanceGate.check)
|
|
80
|
+
*/
|
|
81
|
+
export interface PerformanceGateContext extends GateContext {
|
|
82
|
+
performanceThresholds?: PerformanceThresholds;
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* 契约门禁输入:新旧契约路径覆盖(缺省按构造器 `contractPath` 锚 projectPath)
|
|
86
|
+
*/
|
|
87
|
+
export interface ContractGateContext extends GateContext {
|
|
88
|
+
oldContractPath?: string;
|
|
89
|
+
newContractPath?: string;
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* 命令门禁输入:待检命令(必填——evaluate 的判定对象就是它)
|
|
93
|
+
*/
|
|
94
|
+
export interface CommandGateContext extends GateContext {
|
|
95
|
+
command: string;
|
|
96
|
+
}
|
|
64
97
|
/**
|
|
65
98
|
* 审查门禁配置
|
|
66
99
|
*/
|
|
@@ -109,13 +142,9 @@ export interface SpecAcceptanceGateConfig {
|
|
|
109
142
|
projectPath?: string;
|
|
110
143
|
}
|
|
111
144
|
/**
|
|
112
|
-
*
|
|
145
|
+
* 验收标准门禁输入
|
|
113
146
|
*/
|
|
114
|
-
export interface AcceptanceGateContext {
|
|
115
|
-
/** 项目路径 */
|
|
116
|
-
projectPath: string;
|
|
117
|
-
/** 任务 ID */
|
|
118
|
-
taskId?: string;
|
|
147
|
+
export interface AcceptanceGateContext extends GateContext {
|
|
119
148
|
/** tasks.yml 路径;相对值按 projectPath 解析 */
|
|
120
149
|
tasksPath?: string;
|
|
121
150
|
}
|
|
@@ -50,6 +50,14 @@ export declare class KnowledgeIngest {
|
|
|
50
50
|
/**
|
|
51
51
|
* Ingest multiple entries in batch.
|
|
52
52
|
* Returns one result per input (accepted/rejected).
|
|
53
|
+
*
|
|
54
|
+
* D7:批内共享一次磁盘去重视图与虚拟 index 起点(IngestContext,惰性各读一次),
|
|
55
|
+
* 本批已接受的新建/merge 即时纳入视图,保留批内重复语义;逐条 ingestEntry 行为不变。
|
|
56
|
+
*
|
|
57
|
+
* 统一落盘:新建与 merge 的完整终态(含 auto-fix 折好)收入写计划,
|
|
58
|
+
* 批尾一次 saveAll——全批只重写一次 index.json,整批预校验
|
|
59
|
+
* (任一脏枚举全批拒写,不制造已落盘假象);批内同 id 按 saveAll 后写覆盖语义。
|
|
60
|
+
* 空批零读写。单条路径(ingestEntry)写时序不变。
|
|
53
61
|
*/
|
|
54
62
|
ingestBatch(partials: Partial<KnowledgeEntry>[], options: IngestOptions): IngestResult[];
|
|
55
63
|
/**
|
|
@@ -81,6 +89,24 @@ export declare class KnowledgeIngest {
|
|
|
81
89
|
maturity?: string;
|
|
82
90
|
layer?: string;
|
|
83
91
|
}): IngestValidationIssue[];
|
|
92
|
+
/**
|
|
93
|
+
* 单条的批内共用执行体(D7):build → audit.validate → reject 或
|
|
94
|
+
* 视图去重 → merge / 新建 + auto-fix。ingestEntry 每调新建上下文(= 原单条行为);
|
|
95
|
+
* ingestBatch 全批共享一个上下文。
|
|
96
|
+
*
|
|
97
|
+
* `plan` 存在 = 批次写计划模式:新建与 merge 的完整终态收进 plan(不逐条落盘,
|
|
98
|
+
* 由批尾一次 saveAll 统一写入);缺省 = 单条即时写路径(save + 逐 fix update),
|
|
99
|
+
* 写时序与旧实现逐字一致。返回值契约两种模式相同(新建返回 pre-fix entry,
|
|
100
|
+
* merge 返回合并终态)。
|
|
101
|
+
*/
|
|
102
|
+
private ingestOne;
|
|
103
|
+
/**
|
|
104
|
+
* auto-fix 折叠(纯,D7 批次模式用):archive/demote/flag 依 issues 顺序折进条目副本,
|
|
105
|
+
* 与单条路径逐步 update 的终态一致。merge 分支不调用(既有语义)。
|
|
106
|
+
*/
|
|
107
|
+
private foldAutoFixes;
|
|
108
|
+
/** 单条即时写路径的 auto-fix:逐步 store.update(写时序与旧实现一致),返回折好终态供 overlay */
|
|
109
|
+
private persistAutoFixes;
|
|
84
110
|
private buildEntry;
|
|
85
111
|
private generateId;
|
|
86
112
|
private findDuplicate;
|
|
@@ -95,6 +121,11 @@ export declare class KnowledgeIngest {
|
|
|
95
121
|
private getContentPrefix;
|
|
96
122
|
/** Calculate keyword overlap ratio between two normalized titles */
|
|
97
123
|
private titleOverlap;
|
|
124
|
+
/**
|
|
125
|
+
* 合并终态(纯,D7 两种写模式共用):merge 字段口径与来源上限(MAX_SOURCE_REFS)
|
|
126
|
+
* 只此一份。merge 分支不做新条目 auto-fix(既有语义)。
|
|
127
|
+
*/
|
|
128
|
+
private computeMerge;
|
|
98
129
|
private mergeEntries;
|
|
99
130
|
private defaultSourceRef;
|
|
100
131
|
}
|
package/dist/knowledge/ingest.js
CHANGED
|
@@ -44,6 +44,63 @@ function sanitizeExternalContent(content) {
|
|
|
44
44
|
return sanitized;
|
|
45
45
|
}
|
|
46
46
|
// ── Ingest ─────────────────────────────────────────────────
|
|
47
|
+
/**
|
|
48
|
+
* D7 ingest 批内上下文(包内私有):磁盘去重基线与虚拟序号起点惰性各读一次,
|
|
49
|
+
* 本批已接受条目(新建/merge 的终态)即时进 overlay——
|
|
50
|
+
* 去重视图 = 磁盘基线 + overlay;虚拟 index = 起点 + overlay 终态序号记录
|
|
51
|
+
* (auto-fix 后的 maturity 即时生效,复刻旧逐条路径的序号口径)。
|
|
52
|
+
* reject 不入 overlay。
|
|
53
|
+
*/
|
|
54
|
+
class IngestContext {
|
|
55
|
+
store;
|
|
56
|
+
diskCache;
|
|
57
|
+
indexCache;
|
|
58
|
+
overlay = [];
|
|
59
|
+
overlayIndex = [];
|
|
60
|
+
constructor(store) {
|
|
61
|
+
this.store = store;
|
|
62
|
+
}
|
|
63
|
+
/** 共享磁盘去重基线(readEntriesFromDisk 每批至多一次) */
|
|
64
|
+
disk() {
|
|
65
|
+
return (this.diskCache ??= this.store.readEntriesFromDisk());
|
|
66
|
+
}
|
|
67
|
+
/** 虚拟序号/索引状态起点(readIndex 每批至多一次) */
|
|
68
|
+
index() {
|
|
69
|
+
return (this.indexCache ??= this.store.readIndex());
|
|
70
|
+
}
|
|
71
|
+
/** 本批已接受条目的最新状态入列;同 id 替换而非追加(stale 旧态不得残留) */
|
|
72
|
+
accept(entry) {
|
|
73
|
+
const at = this.overlay.findIndex(e => e.id === entry.id);
|
|
74
|
+
if (at >= 0) {
|
|
75
|
+
this.overlay[at] = entry;
|
|
76
|
+
}
|
|
77
|
+
else {
|
|
78
|
+
this.overlay.push(entry);
|
|
79
|
+
}
|
|
80
|
+
const record = { id: entry.id, type: entry.type, maturity: entry.maturity };
|
|
81
|
+
const recAt = this.overlayIndex.findIndex(r => r.id === entry.id);
|
|
82
|
+
if (recAt >= 0) {
|
|
83
|
+
this.overlayIndex[recAt] = record;
|
|
84
|
+
}
|
|
85
|
+
else {
|
|
86
|
+
this.overlayIndex.push(record);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* 去重视图:磁盘基线中被本批更新过的 id 以 overlay 最新状态为准——
|
|
91
|
+
* stale 磁盘条目不得抢在更新态前命中(同批连续 merge 第二次须基于第一次终态);
|
|
92
|
+
* 顺序语义保持盘序在前、本批接受序在后。
|
|
93
|
+
*/
|
|
94
|
+
view() {
|
|
95
|
+
const updatedIds = new Set(this.overlay.map(e => e.id));
|
|
96
|
+
return [...this.disk().filter(e => !updatedIds.has(e.id)), ...this.overlay];
|
|
97
|
+
}
|
|
98
|
+
/** 虚拟 index:起点中被本批更新过的 id 按 overlay 终态替换(每 id 至多计一次序号) */
|
|
99
|
+
virtualIndex() {
|
|
100
|
+
const updatedIds = new Set(this.overlayIndex.map(r => r.id));
|
|
101
|
+
return [...this.index().filter(r => !updatedIds.has(r.id)), ...this.overlayIndex];
|
|
102
|
+
}
|
|
103
|
+
}
|
|
47
104
|
class KnowledgeIngest {
|
|
48
105
|
store;
|
|
49
106
|
constructor(store) {
|
|
@@ -55,53 +112,30 @@ class KnowledgeIngest {
|
|
|
55
112
|
* Returns a discriminated result: accepted = saved (new or merged), rejected = audit gate refusal.
|
|
56
113
|
*/
|
|
57
114
|
ingestEntry(partial, options) {
|
|
58
|
-
|
|
59
|
-
// Quality gate: audit before saving
|
|
60
|
-
const audit = new audit_1.KnowledgeAudit(this.store);
|
|
61
|
-
let issues = audit.validate(entry);
|
|
62
|
-
const critical = issues.filter(i => i.action === 'reject');
|
|
63
|
-
if (critical.length > 0) {
|
|
64
|
-
// Reject: return without saving; caller branches on status / reasons
|
|
65
|
-
return { status: 'rejected', entry, reasons: critical.map(i => i.detail) };
|
|
66
|
-
}
|
|
67
|
-
// If caller explicitly set maturity, skip demote actions from audit.
|
|
68
|
-
// Demote is for auto-inferred maturity that's too high; explicit is a user decision.
|
|
69
|
-
if (options.maturity) {
|
|
70
|
-
issues = issues.filter(i => i.action !== 'demote');
|
|
71
|
-
}
|
|
72
|
-
// Dedup check: same title + same type
|
|
73
|
-
const existing = this.findDuplicate(entry.title, entry.content, entry.type);
|
|
74
|
-
if (existing) {
|
|
75
|
-
// Merge: update existing entry with new content and metadata
|
|
76
|
-
return { status: 'accepted', entry: this.mergeEntries(existing, entry, options) };
|
|
77
|
-
}
|
|
78
|
-
this.store.save(entry);
|
|
79
|
-
// Post-save: apply auto-fixes (archive/demote/flag)
|
|
80
|
-
const fixable = issues.filter(i => i.action !== 'reject');
|
|
81
|
-
if (fixable.length > 0) {
|
|
82
|
-
for (const issue of fixable) {
|
|
83
|
-
if (issue.action === 'archive') {
|
|
84
|
-
this.store.update(entry.id, { maturity: 'archived' });
|
|
85
|
-
}
|
|
86
|
-
else if (issue.action === 'demote') {
|
|
87
|
-
this.store.update(entry.id, { maturity: 'draft' });
|
|
88
|
-
}
|
|
89
|
-
else if (issue.action === 'flag') {
|
|
90
|
-
const saved = this.store.get(entry.id);
|
|
91
|
-
if (saved && !saved.tags.includes('low_quality')) {
|
|
92
|
-
this.store.update(entry.id, { tags: [...saved.tags, 'low_quality'] });
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
return { status: 'accepted', entry };
|
|
115
|
+
return this.ingestOne(partial, options, new IngestContext(this.store));
|
|
98
116
|
}
|
|
99
117
|
/**
|
|
100
118
|
* Ingest multiple entries in batch.
|
|
101
119
|
* Returns one result per input (accepted/rejected).
|
|
120
|
+
*
|
|
121
|
+
* D7:批内共享一次磁盘去重视图与虚拟 index 起点(IngestContext,惰性各读一次),
|
|
122
|
+
* 本批已接受的新建/merge 即时纳入视图,保留批内重复语义;逐条 ingestEntry 行为不变。
|
|
123
|
+
*
|
|
124
|
+
* 统一落盘:新建与 merge 的完整终态(含 auto-fix 折好)收入写计划,
|
|
125
|
+
* 批尾一次 saveAll——全批只重写一次 index.json,整批预校验
|
|
126
|
+
* (任一脏枚举全批拒写,不制造已落盘假象);批内同 id 按 saveAll 后写覆盖语义。
|
|
127
|
+
* 空批零读写。单条路径(ingestEntry)写时序不变。
|
|
102
128
|
*/
|
|
103
129
|
ingestBatch(partials, options) {
|
|
104
|
-
|
|
130
|
+
if (partials.length === 0)
|
|
131
|
+
return [];
|
|
132
|
+
const ctx = new IngestContext(this.store);
|
|
133
|
+
const plan = [];
|
|
134
|
+
const results = partials.map(p => this.ingestOne(p, options, ctx, plan));
|
|
135
|
+
if (plan.length > 0) {
|
|
136
|
+
this.store.saveAll(plan);
|
|
137
|
+
}
|
|
138
|
+
return results;
|
|
105
139
|
}
|
|
106
140
|
/**
|
|
107
141
|
* Ingest external content with sanitization.
|
|
@@ -207,10 +241,105 @@ class KnowledgeIngest {
|
|
|
207
241
|
return issues;
|
|
208
242
|
}
|
|
209
243
|
// ── Internal ───────────────────────────────────────────────
|
|
210
|
-
|
|
244
|
+
/**
|
|
245
|
+
* 单条的批内共用执行体(D7):build → audit.validate → reject 或
|
|
246
|
+
* 视图去重 → merge / 新建 + auto-fix。ingestEntry 每调新建上下文(= 原单条行为);
|
|
247
|
+
* ingestBatch 全批共享一个上下文。
|
|
248
|
+
*
|
|
249
|
+
* `plan` 存在 = 批次写计划模式:新建与 merge 的完整终态收进 plan(不逐条落盘,
|
|
250
|
+
* 由批尾一次 saveAll 统一写入);缺省 = 单条即时写路径(save + 逐 fix update),
|
|
251
|
+
* 写时序与旧实现逐字一致。返回值契约两种模式相同(新建返回 pre-fix entry,
|
|
252
|
+
* merge 返回合并终态)。
|
|
253
|
+
*/
|
|
254
|
+
ingestOne(partial, options, ctx, plan) {
|
|
255
|
+
const entry = this.buildEntry(partial, options, ctx);
|
|
256
|
+
// Quality gate: audit before saving
|
|
257
|
+
const audit = new audit_1.KnowledgeAudit(this.store);
|
|
258
|
+
let issues = audit.validate(entry);
|
|
259
|
+
const critical = issues.filter(i => i.action === 'reject');
|
|
260
|
+
if (critical.length > 0) {
|
|
261
|
+
// Reject: return without saving; caller branches on status / reasons
|
|
262
|
+
return { status: 'rejected', entry, reasons: critical.map(i => i.detail) };
|
|
263
|
+
}
|
|
264
|
+
// If caller explicitly set maturity, skip demote actions from audit.
|
|
265
|
+
// Demote is for auto-inferred maturity that's too high; explicit is a user decision.
|
|
266
|
+
if (options.maturity) {
|
|
267
|
+
issues = issues.filter(i => i.action !== 'demote');
|
|
268
|
+
}
|
|
269
|
+
// Dedup check: same title + same type(共享视图 = 磁盘基线 + 本批已接受终态)
|
|
270
|
+
const existing = this.findDuplicate(entry.title, entry.content, entry.type, ctx.view());
|
|
271
|
+
if (existing) {
|
|
272
|
+
// Merge: update existing entry with new content and metadata(merge 分支不做新条目 auto-fix)
|
|
273
|
+
if (plan) {
|
|
274
|
+
const merged = this.computeMerge(existing, entry, options);
|
|
275
|
+
plan.push(merged);
|
|
276
|
+
ctx.accept(merged);
|
|
277
|
+
return { status: 'accepted', entry: merged };
|
|
278
|
+
}
|
|
279
|
+
const merged = this.mergeEntries(existing, entry, options);
|
|
280
|
+
ctx.accept(merged);
|
|
281
|
+
return { status: 'accepted', entry: merged };
|
|
282
|
+
}
|
|
283
|
+
if (plan) {
|
|
284
|
+
// 批次模式:auto-fix 折进终态收写计划,批尾一次落盘;返回值仍是 pre-fix entry
|
|
285
|
+
const finalEntry = this.foldAutoFixes(entry, issues);
|
|
286
|
+
plan.push(finalEntry);
|
|
287
|
+
ctx.accept(finalEntry);
|
|
288
|
+
return { status: 'accepted', entry };
|
|
289
|
+
}
|
|
290
|
+
this.store.save(entry);
|
|
291
|
+
// Post-save: apply auto-fixes (archive/demote/flag)
|
|
292
|
+
const finalEntry = this.persistAutoFixes(entry, issues);
|
|
293
|
+
ctx.accept(finalEntry);
|
|
294
|
+
return { status: 'accepted', entry };
|
|
295
|
+
}
|
|
296
|
+
/**
|
|
297
|
+
* auto-fix 折叠(纯,D7 批次模式用):archive/demote/flag 依 issues 顺序折进条目副本,
|
|
298
|
+
* 与单条路径逐步 update 的终态一致。merge 分支不调用(既有语义)。
|
|
299
|
+
*/
|
|
300
|
+
foldAutoFixes(entry, issues) {
|
|
301
|
+
let current = entry;
|
|
302
|
+
for (const issue of issues) {
|
|
303
|
+
if (issue.action === 'archive') {
|
|
304
|
+
current = { ...current, maturity: 'archived' };
|
|
305
|
+
}
|
|
306
|
+
else if (issue.action === 'demote') {
|
|
307
|
+
current = { ...current, maturity: 'draft' };
|
|
308
|
+
}
|
|
309
|
+
else if (issue.action === 'flag') {
|
|
310
|
+
if (!current.tags.includes('low_quality')) {
|
|
311
|
+
current = { ...current, tags: [...current.tags, 'low_quality'] };
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
return current;
|
|
316
|
+
}
|
|
317
|
+
/** 单条即时写路径的 auto-fix:逐步 store.update(写时序与旧实现一致),返回折好终态供 overlay */
|
|
318
|
+
persistAutoFixes(entry, issues) {
|
|
319
|
+
let current = entry;
|
|
320
|
+
for (const issue of issues) {
|
|
321
|
+
if (issue.action === 'archive') {
|
|
322
|
+
this.store.update(entry.id, { maturity: 'archived' });
|
|
323
|
+
current = { ...current, maturity: 'archived' };
|
|
324
|
+
}
|
|
325
|
+
else if (issue.action === 'demote') {
|
|
326
|
+
this.store.update(entry.id, { maturity: 'draft' });
|
|
327
|
+
current = { ...current, maturity: 'draft' };
|
|
328
|
+
}
|
|
329
|
+
else if (issue.action === 'flag') {
|
|
330
|
+
if (!current.tags.includes('low_quality')) {
|
|
331
|
+
const tags = [...current.tags, 'low_quality'];
|
|
332
|
+
this.store.update(entry.id, { tags });
|
|
333
|
+
current = { ...current, tags };
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
return current;
|
|
338
|
+
}
|
|
339
|
+
buildEntry(partial, options, ctx) {
|
|
211
340
|
const now = new Date().toISOString();
|
|
212
341
|
const type = partial.type || 'guideline';
|
|
213
|
-
const id = partial.id || this.generateId(type);
|
|
342
|
+
const id = partial.id || this.generateId(type, ctx);
|
|
214
343
|
return {
|
|
215
344
|
id,
|
|
216
345
|
type,
|
|
@@ -233,18 +362,21 @@ class KnowledgeIngest {
|
|
|
233
362
|
skillId: partial.skillId,
|
|
234
363
|
};
|
|
235
364
|
}
|
|
236
|
-
generateId(type) {
|
|
365
|
+
generateId(type, ctx) {
|
|
237
366
|
const prefix = type.toUpperCase().slice(0, 3);
|
|
238
|
-
//
|
|
239
|
-
// 与 list() 默认过滤口径一致,archived/deprecated
|
|
240
|
-
|
|
367
|
+
// 序号取自虚拟 index 计数(harness#107 + D7):不再走 list() 逐条读条目文件;
|
|
368
|
+
// 与 list() 默认过滤口径一致,archived/deprecated 不计入序号。
|
|
369
|
+
// 批内起点 = 批首一次 readIndex,叠加本批已接受条目的终态 maturity——
|
|
370
|
+
// 旧逐条路径里前一条 save 后 auto-fix 的 archived 会立即影响下一条序号,此处同口径复刻。
|
|
371
|
+
const existing = ctx.virtualIndex()
|
|
241
372
|
.filter(e => e.type === type && e.maturity !== 'archived' && e.maturity !== 'deprecated');
|
|
242
373
|
const seq = String(existing.length + 1).padStart(3, '0');
|
|
243
374
|
return `${prefix}-${seq}`;
|
|
244
375
|
}
|
|
245
|
-
findDuplicate(title, content, type) {
|
|
246
|
-
// A1:
|
|
247
|
-
|
|
376
|
+
findDuplicate(title, content, type, candidates) {
|
|
377
|
+
// A1: 共享视图来自盘驱动枚举(readEntriesFromDisk),stale index 不造成去重失效;
|
|
378
|
+
// D7:批内视图含本批已接受终态,重复语义与旧逐条路径一致
|
|
379
|
+
const all = candidates.filter(e => e.type === type);
|
|
248
380
|
// Exact match (case-insensitive)
|
|
249
381
|
const exact = all.find(e => (e.title || '').toLowerCase() === title.toLowerCase());
|
|
250
382
|
if (exact)
|
|
@@ -309,8 +441,13 @@ class KnowledgeIngest {
|
|
|
309
441
|
const overlap = tokensA.filter(t => setB.has(t)).length;
|
|
310
442
|
return overlap / Math.max(tokensA.length, tokensB.length);
|
|
311
443
|
}
|
|
312
|
-
|
|
313
|
-
|
|
444
|
+
/**
|
|
445
|
+
* 合并终态(纯,D7 两种写模式共用):merge 字段口径与来源上限(MAX_SOURCE_REFS)
|
|
446
|
+
* 只此一份。merge 分支不做新条目 auto-fix(既有语义)。
|
|
447
|
+
*/
|
|
448
|
+
computeMerge(existing, incoming, options) {
|
|
449
|
+
return {
|
|
450
|
+
...existing,
|
|
314
451
|
content: incoming.content || existing.content,
|
|
315
452
|
maturity: options.maturity || existing.maturity,
|
|
316
453
|
lastReferenced: new Date().toISOString(),
|
|
@@ -319,6 +456,9 @@ class KnowledgeIngest {
|
|
|
319
456
|
tags: [...new Set([...existing.tags, ...incoming.tags])],
|
|
320
457
|
sourceReferences: [...existing.sourceReferences, ...incoming.sourceReferences].slice(-audit_rules_1.MAX_SOURCE_REFS),
|
|
321
458
|
};
|
|
459
|
+
}
|
|
460
|
+
mergeEntries(existing, incoming, options) {
|
|
461
|
+
const merged = this.computeMerge(existing, incoming, options);
|
|
322
462
|
// fail-fast:刚经 readEntriesFromDisk 读到的条目 update 不到 = 并发删除/索引漂移,不静默吞
|
|
323
463
|
const updated = this.store.update(existing.id, merged);
|
|
324
464
|
if (!updated) {
|
|
@@ -26,6 +26,21 @@ export declare class KnowledgeLifecycle {
|
|
|
26
26
|
* Optionally records execution success/failure.
|
|
27
27
|
*/
|
|
28
28
|
recordReference(entryId: string, contributor?: string, success?: boolean, source?: 'human' | 'auto'): KnowledgeEntry | undefined;
|
|
29
|
+
/**
|
|
30
|
+
* 批量形 recordReference(P1.3):一次 query 的 N 条引用收集进一次 applyAll 落盘,
|
|
31
|
+
* 消除 N 次「全量重写 index.json」。逐项语义与逐条 recordReference 一致:
|
|
32
|
+
* same-day refKey 去重(去重时 success 给定额补记 executionResults)、
|
|
33
|
+
* referencedBy 20 条上限、contributors 去重、未知 id 跳过;
|
|
34
|
+
* 回调保持「不早于持久化成功」语义——批量落盘成功后逐条触发,去重/未知 id 不触发
|
|
35
|
+
* (批量本就改变了落盘粒度,不声称逐条版的部分失败时序原样保留)。
|
|
36
|
+
* 单事件调用方仍用 recordReference()(其「返回更新后条目 + 触发回调」契约不变)。
|
|
37
|
+
*
|
|
38
|
+
* D7 引用复用:可选 `preloaded` 传 query 本次已物化的条目(id → 完整条目)——
|
|
39
|
+
* 条目查线不再 store.get,applyAll 以预载为合并基线(层间零重复读取);
|
|
40
|
+
* 缺省走原取盘路径,原四参公共调用兼容。preloaded 缺项的 id 回退 store.get 取盘,
|
|
41
|
+
* 取不到才按未知 id 跳过。
|
|
42
|
+
*/
|
|
43
|
+
recordReferences(entryIds: string[], contributor?: string, success?: boolean, source?: 'human' | 'auto', preloaded?: ReadonlyMap<string, KnowledgeEntry>): void;
|
|
29
44
|
/**
|
|
30
45
|
* Check if an entry meets promotion criteria.
|
|
31
46
|
* Returns the target maturity level if promotion is warranted, otherwise undefined.
|
|
@@ -72,6 +72,68 @@ class KnowledgeLifecycle {
|
|
|
72
72
|
}
|
|
73
73
|
return updated;
|
|
74
74
|
}
|
|
75
|
+
/**
|
|
76
|
+
* 批量形 recordReference(P1.3):一次 query 的 N 条引用收集进一次 applyAll 落盘,
|
|
77
|
+
* 消除 N 次「全量重写 index.json」。逐项语义与逐条 recordReference 一致:
|
|
78
|
+
* same-day refKey 去重(去重时 success 给定额补记 executionResults)、
|
|
79
|
+
* referencedBy 20 条上限、contributors 去重、未知 id 跳过;
|
|
80
|
+
* 回调保持「不早于持久化成功」语义——批量落盘成功后逐条触发,去重/未知 id 不触发
|
|
81
|
+
* (批量本就改变了落盘粒度,不声称逐条版的部分失败时序原样保留)。
|
|
82
|
+
* 单事件调用方仍用 recordReference()(其「返回更新后条目 + 触发回调」契约不变)。
|
|
83
|
+
*
|
|
84
|
+
* D7 引用复用:可选 `preloaded` 传 query 本次已物化的条目(id → 完整条目)——
|
|
85
|
+
* 条目查线不再 store.get,applyAll 以预载为合并基线(层间零重复读取);
|
|
86
|
+
* 缺省走原取盘路径,原四参公共调用兼容。preloaded 缺项的 id 回退 store.get 取盘,
|
|
87
|
+
* 取不到才按未知 id 跳过。
|
|
88
|
+
*/
|
|
89
|
+
recordReferences(entryIds, contributor, success, source, preloaded) {
|
|
90
|
+
const now = new Date().toISOString();
|
|
91
|
+
const updates = [];
|
|
92
|
+
const events = [];
|
|
93
|
+
const seen = new Set();
|
|
94
|
+
for (const entryId of entryIds) {
|
|
95
|
+
if (seen.has(entryId))
|
|
96
|
+
continue;
|
|
97
|
+
seen.add(entryId);
|
|
98
|
+
const entry = preloaded?.get(entryId) ?? this.store.get(entryId);
|
|
99
|
+
if (!entry)
|
|
100
|
+
continue;
|
|
101
|
+
const contributors = contributor && !entry.contributors.includes(contributor)
|
|
102
|
+
? [...entry.contributors, contributor]
|
|
103
|
+
: entry.contributors;
|
|
104
|
+
const refKey = `${contributor || 'unknown'}:${now.slice(0, 10)}`;
|
|
105
|
+
const execSource = source || (contributor?.startsWith('auto-') ? 'auto' : undefined);
|
|
106
|
+
// B4: same-day, same-contributor 去重(与逐条版同口径)
|
|
107
|
+
if (entry.referencedBy.includes(refKey)) {
|
|
108
|
+
// 去重仍补记执行结果(若给了 success),但不触发回调
|
|
109
|
+
if (success !== undefined) {
|
|
110
|
+
const executionResults = [
|
|
111
|
+
...(entry.executionResults),
|
|
112
|
+
{ contributor: contributor || 'unknown', success, timestamp: now, source: execSource },
|
|
113
|
+
].slice(-MAX_REFERENCED_BY);
|
|
114
|
+
updates.push({ id: entryId, partial: { executionResults } });
|
|
115
|
+
}
|
|
116
|
+
continue;
|
|
117
|
+
}
|
|
118
|
+
const referencedBy = [...entry.referencedBy, refKey].slice(-MAX_REFERENCED_BY);
|
|
119
|
+
const executionResults = success !== undefined
|
|
120
|
+
? [...(entry.executionResults), { contributor: contributor || 'unknown', success, timestamp: now, source: execSource }].slice(-MAX_REFERENCED_BY)
|
|
121
|
+
: entry.executionResults;
|
|
122
|
+
updates.push({ id: entryId, partial: { lastReferenced: now, contributors, referencedBy, executionResults } });
|
|
123
|
+
events.push({ entryId, contributor: contributor || 'unknown', timestamp: now, success });
|
|
124
|
+
}
|
|
125
|
+
// 一次落盘:applyAll 循环内只更新内存索引、结束一次 writeIndex(空批零读写)。
|
|
126
|
+
// 落盘成功前不触发任何回调;抛错则整批中断上抛,由调用方 fail-open 兜底。
|
|
127
|
+
// preloaded 作为合并基线传入:批内条目零重复 get(D7 引用复用)。
|
|
128
|
+
this.store.applyAll(updates, preloaded);
|
|
129
|
+
// 批量落盘成功后才逐条触发消费回调(去重/未知 id 不在 events 内,天然不触发)
|
|
130
|
+
for (const event of events) {
|
|
131
|
+
for (const cb of this.onReferenceCallbacks) {
|
|
132
|
+
// 显式 fail-open(attempt):消费回调是外挂监听,崩了不拖死引用记账主流程
|
|
133
|
+
(0, attempt_1.attempt)(() => cb(event), () => undefined);
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
}
|
|
75
137
|
/**
|
|
76
138
|
* Check if an entry meets promotion criteria.
|
|
77
139
|
* Returns the target maturity level if promotion is warranted, otherwise undefined.
|
|
@@ -75,6 +75,16 @@ export declare class KnowledgeQuery {
|
|
|
75
75
|
* Adds [External Source] warning prefix for external-origin entries.
|
|
76
76
|
*/
|
|
77
77
|
static formatForPrompt(entry: KnowledgeEntry): string;
|
|
78
|
+
/**
|
|
79
|
+
* D7 按需物化路径:候选只经 index 元数据过滤/排序/截断——被过滤排除与
|
|
80
|
+
* budget 截断掉的溢出候选全文零读取,最终返回子集才经 materializeByIndex 回读
|
|
81
|
+
* (漂移错误与 list 同一正本)。tokenCount 写入时已按 estimateTokens 两次 ceil
|
|
82
|
+
* 口径算好,tokensUsed 与兼容路径逐字相等。
|
|
83
|
+
*
|
|
84
|
+
* 返回 undefined = 走兼容路径:store 未提供 materializeByIndex(外部自定义 store)
|
|
85
|
+
* 或过滤后候选存在无 tokenCount 的旧格式条目(readIndex 会打一行 rebuildIndex 提示)。
|
|
86
|
+
*/
|
|
87
|
+
private queryFromIndex;
|
|
78
88
|
private sortEntries;
|
|
79
89
|
private applyBudget;
|
|
80
90
|
private cacheKey;
|