@octopi-agent/engine 0.65.0 → 0.70.7

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 (99) hide show
  1. package/dist/config-schema/engine.d.ts +84 -0
  2. package/dist/config-schema/engine.js +4 -1
  3. package/dist/config-schema/substrate.d.ts +86 -0
  4. package/dist/config-schema/substrate.js +114 -0
  5. package/dist/config.d.ts +71 -0
  6. package/dist/harness/agent/builder.d.ts +1 -1
  7. package/dist/harness/agent/builder.js +27 -2
  8. package/dist/harness/agent/config-bridge.js +17 -4
  9. package/dist/harness/context/capabilities/summary/gate.js +2 -2
  10. package/dist/harness/context/capabilities/summary/policies.js +5 -5
  11. package/dist/harness/context/capabilities/summary/registry.js +29 -24
  12. package/dist/harness/context/layer-health.js +3 -2
  13. package/dist/harness/context/layers.d.ts +27 -13
  14. package/dist/harness/context/layers.js +76 -66
  15. package/dist/harness/context/system-prompt-assembler.d.ts +3 -0
  16. package/dist/harness/context/system-prompt-assembler.js +11 -1
  17. package/dist/harness/extension/plugin-ecosystem/client-tools/index.d.ts +4 -0
  18. package/dist/harness/extension/plugin-ecosystem/client-tools/index.js +3 -0
  19. package/dist/harness/extension/plugin-ecosystem/client-tools/invoke.d.ts +56 -0
  20. package/dist/harness/extension/plugin-ecosystem/client-tools/invoke.js +281 -0
  21. package/dist/harness/extension/plugin-ecosystem/client-tools/registry.d.ts +98 -0
  22. package/dist/harness/extension/plugin-ecosystem/client-tools/registry.js +250 -0
  23. package/dist/harness/extension/plugin-ecosystem/client-tools/stream.d.ts +124 -0
  24. package/dist/harness/extension/plugin-ecosystem/client-tools/stream.js +372 -0
  25. package/dist/harness/extension/plugin-ecosystem/client-tools/types.d.ts +109 -0
  26. package/dist/harness/extension/plugin-ecosystem/client-tools/types.js +8 -0
  27. package/dist/harness/extension/plugin-ecosystem/index.d.ts +1 -0
  28. package/dist/harness/extension/plugin-ecosystem/index.js +1 -0
  29. package/dist/harness/extension/plugin-ecosystem/tools/builtin.js +1 -1
  30. package/dist/harness/extension/plugin-ecosystem/tools/memory.d.ts +11 -1
  31. package/dist/harness/extension/plugin-ecosystem/tools/memory.js +14 -0
  32. package/dist/harness/index.d.ts +4 -3
  33. package/dist/harness/index.js +2 -1
  34. package/dist/harness/knowledge/query-service.js +5 -5
  35. package/dist/harness/knowledge/retriever.js +1 -1
  36. package/dist/harness/knowledge/source-store.d.ts +17 -2
  37. package/dist/harness/knowledge/source-store.js +6 -5
  38. package/dist/harness/memory/cognition-decay.d.ts +36 -0
  39. package/dist/harness/memory/cognition-decay.js +55 -0
  40. package/dist/harness/memory/cognition-gates.d.ts +60 -0
  41. package/dist/harness/memory/cognition-gates.js +191 -0
  42. package/dist/harness/memory/cognition-trigger.d.ts +30 -0
  43. package/dist/harness/memory/cognition-trigger.js +41 -0
  44. package/dist/harness/memory/cognition-types.d.ts +1 -1
  45. package/dist/harness/memory/cognition.d.ts +40 -8
  46. package/dist/harness/memory/cognition.js +432 -79
  47. package/dist/harness/memory/conceptualizer.d.ts +66 -0
  48. package/dist/harness/memory/conceptualizer.js +246 -0
  49. package/dist/harness/memory/index.d.ts +16 -2
  50. package/dist/harness/memory/index.js +11 -1
  51. package/dist/harness/memory/sqlite/agent-db.d.ts +9 -0
  52. package/dist/harness/memory/sqlite/agent-db.js +118 -15
  53. package/dist/harness/memory/sqlite/cognition-store.d.ts +55 -16
  54. package/dist/harness/memory/sqlite/cognition-store.js +591 -144
  55. package/dist/harness/memory/sqlite/wisdom-store.d.ts +33 -14
  56. package/dist/harness/memory/sqlite/wisdom-store.js +257 -44
  57. package/dist/harness/memory/types.d.ts +331 -16
  58. package/dist/harness/memory/types.js +49 -0
  59. package/dist/harness/memory/wisdom-formation.d.ts +101 -0
  60. package/dist/harness/memory/wisdom-formation.js +304 -0
  61. package/dist/harness/memory/wisdom-gates.d.ts +50 -0
  62. package/dist/harness/memory/wisdom-gates.js +204 -0
  63. package/dist/harness/memory/wisdom-policy.d.ts +107 -0
  64. package/dist/harness/memory/wisdom-policy.js +220 -0
  65. package/dist/harness/memory/wisdom-types.d.ts +3 -2
  66. package/dist/harness/memory/wisdom-types.js +2 -2
  67. package/dist/harness/memory/wisdom.d.ts +27 -0
  68. package/dist/harness/memory/wisdom.js +181 -0
  69. package/dist/harness/run/agent/agent.d.ts +9 -0
  70. package/dist/harness/run/agent/agent.js +19 -0
  71. package/dist/harness/run/reliability/run-agent.js +6 -2
  72. package/dist/harness/run/runner.js +2 -1
  73. package/dist/harness/session/index.d.ts +1 -0
  74. package/dist/harness/session/index.js +1 -0
  75. package/dist/harness/session/title.d.ts +86 -0
  76. package/dist/harness/session/title.js +268 -0
  77. package/dist/harness/shared/events/agent-event-map.d.ts +8 -0
  78. package/dist/harness/shared/events/agent-event-map.js +1 -0
  79. package/dist/index.d.ts +3 -1
  80. package/dist/index.js +1 -0
  81. package/dist/subsystems/cognition-steward/conceptualize/SUBSYSTEM.md +43 -0
  82. package/dist/subsystems/cognition-steward/conceptualize/config.yaml +50 -0
  83. package/dist/subsystems/cognition-steward/conceptualize/handler.d.ts +9 -0
  84. package/dist/subsystems/cognition-steward/conceptualize/handler.js +125 -0
  85. package/dist/subsystems/cognition-steward/consolidate/SUBSYSTEM.md +6 -0
  86. package/dist/subsystems/cognition-steward/consolidate/config.yaml +54 -0
  87. package/dist/subsystems/cognition-steward/consolidate/handler.d.ts +12 -0
  88. package/dist/subsystems/cognition-steward/consolidate/handler.js +93 -0
  89. package/dist/subsystems/wisdom-steward/evaluate/config.yaml +50 -0
  90. package/dist/subsystems/wisdom-steward/evaluate/handler.d.ts +18 -0
  91. package/dist/subsystems/wisdom-steward/evaluate/handler.js +91 -0
  92. package/dist/subsystems/wisdom-steward/formation/SUBSYSTEM.md +42 -0
  93. package/dist/subsystems/wisdom-steward/formation/config.yaml +54 -0
  94. package/dist/subsystems/wisdom-steward/formation/handler.d.ts +18 -0
  95. package/dist/subsystems/wisdom-steward/formation/handler.js +212 -0
  96. package/dist/subsystems/wisdom-steward/govern/config.yaml +55 -0
  97. package/dist/subsystems/wisdom-steward/govern/handler.d.ts +17 -0
  98. package/dist/subsystems/wisdom-steward/govern/handler.js +68 -0
  99. package/package.json +2 -2
@@ -143,10 +143,15 @@ export declare class KnowledgeSourceStore {
143
143
  /**
144
144
  * 生成 agent 可见 catalog(Tier 0)
145
145
  * 默认返回可见全集(display 截断在 KnowledgeLayer);maxEntries 仅作安全硬顶
146
+ * identity 必须与 UI 挂载查询同口径(tenant/gateway),否则会误命中他方挂载行
146
147
  */
147
148
  catalogFor(agentId: string, opts?: {
148
149
  sessionId?: string;
149
150
  maxEntries?: number;
151
+ identity?: {
152
+ tenantId?: string;
153
+ gatewayId?: string;
154
+ };
150
155
  }): KnowledgeCatalogItem[];
151
156
  /** 规模 + topics(短 TTL 缓存;禁止每轮全表拉 path) */
152
157
  private catalogExtras;
@@ -163,12 +168,22 @@ export declare class KnowledgeSourceStore {
163
168
  */
164
169
  catalogFingerprint(agentId: string, opts?: {
165
170
  sessionId?: string;
171
+ identity?: {
172
+ tenantId?: string;
173
+ gatewayId?: string;
174
+ };
166
175
  }): string;
167
176
  /**
168
177
  * 可见源列表(effective = base ⊕ session overlay;含 hiddenFromCatalog)
169
178
  */
170
- listVisible(agentId: string, sessionId?: string): KnowledgeSource[];
179
+ listVisible(agentId: string, sessionId?: string, identity?: {
180
+ tenantId?: string;
181
+ gatewayId?: string;
182
+ }): KnowledgeSource[];
171
183
  /** Agent 级 base 可见源(不含会话 overlay) */
172
- listBaseVisible(agentId: string, sessionId?: string): KnowledgeSource[];
184
+ listBaseVisible(agentId: string, sessionId?: string, identity?: {
185
+ tenantId?: string;
186
+ gatewayId?: string;
187
+ }): KnowledgeSource[];
173
188
  private upsertRow;
174
189
  }
@@ -528,10 +528,11 @@ export class KnowledgeSourceStore {
528
528
  /**
529
529
  * 生成 agent 可见 catalog(Tier 0)
530
530
  * 默认返回可见全集(display 截断在 KnowledgeLayer);maxEntries 仅作安全硬顶
531
+ * identity 必须与 UI 挂载查询同口径(tenant/gateway),否则会误命中他方挂载行
531
532
  */
532
533
  catalogFor(agentId, opts) {
533
534
  const max = opts?.maxEntries ?? 200;
534
- const visible = this.list().filter((s) => this.isVisible(s, agentId, opts?.sessionId));
535
+ const visible = this.list().filter((s) => this.isVisible(s, agentId, opts?.sessionId, opts?.identity));
535
536
  const sorted = [...visible].sort((a, b) => {
536
537
  const pa = a.catalogPriority ?? 0;
537
538
  const pb = b.catalogPriority ?? 0;
@@ -604,12 +605,12 @@ export class KnowledgeSourceStore {
604
605
  /**
605
606
  * 可见源列表(effective = base ⊕ session overlay;含 hiddenFromCatalog)
606
607
  */
607
- listVisible(agentId, sessionId) {
608
- return this.list().filter((s) => this.isVisible(s, agentId, sessionId));
608
+ listVisible(agentId, sessionId, identity) {
609
+ return this.list().filter((s) => this.isVisible(s, agentId, sessionId, identity));
609
610
  }
610
611
  /** Agent 级 base 可见源(不含会话 overlay) */
611
- listBaseVisible(agentId, sessionId) {
612
- return this.list().filter((s) => this.isBaseVisible(s, agentId, sessionId));
612
+ listBaseVisible(agentId, sessionId, identity) {
613
+ return this.list().filter((s) => this.isBaseVisible(s, agentId, sessionId, identity));
613
614
  }
614
615
  upsertRow(source) {
615
616
  this.db.raw
@@ -0,0 +1,36 @@
1
+ /**
2
+ * Cognition 边衰减曲线 — 按 relationType 的日衰减率
3
+ *
4
+ * @module harness/memory/cognition-decay
5
+ */
6
+ import type { ConceptRelationType, ConceptStatus } from './types.js';
7
+ /** 每日衰减乘数(0–1;越小越快弱) */
8
+ export declare const DEFAULT_EDGE_DECAY_PER_DAY: Record<ConceptRelationType, number>;
9
+ export declare const DEFAULT_EDGE_DECAY_FLOOR = 0.02;
10
+ export type EdgeDecayConfig = Partial<Record<ConceptRelationType, number>>;
11
+ /**
12
+ * 将日衰减率换算到时间间隔后的乘数。
13
+ *
14
+ * @param rate - 每日衰减比例(0.05 表示每日 ×0.95)
15
+ * @param days - 经过的天数
16
+ * @returns 乘数(≥0)
17
+ */
18
+ export declare function decayMultiplier(rate: number, days: number): number;
19
+ /**
20
+ * 下一强度 = max(floor, w × mult)
21
+ */
22
+ export declare function nextEdgeStrength(current: number, relationType: ConceptRelationType, days: number, config?: EdgeDecayConfig, floor?: number): number;
23
+ /**
24
+ * Hebbian 异步累积:w' = 1 - (1-w)(1-η·e)
25
+ *
26
+ * @param current - 当前边权
27
+ * @param eta - 学习率(默认 0.15)
28
+ * @param evidence - 证据强度 0–1
29
+ */
30
+ export declare function hebbianStrengthen(current: number, evidence: number, eta?: number): number;
31
+ /**
32
+ * 反证降权:w' = w × (1-ρ)
33
+ */
34
+ export declare function counterEvidenceWeaken(current: number, rho?: number): number;
35
+ /** Layer 默认只吃 active / strengthened */
36
+ export declare const DEFAULT_RETRIEVE_STATUSES: ConceptStatus[];
@@ -0,0 +1,55 @@
1
+ /**
2
+ * Cognition 边衰减曲线 — 按 relationType 的日衰减率
3
+ *
4
+ * @module harness/memory/cognition-decay
5
+ */
6
+ /** 每日衰减乘数(0–1;越小越快弱) */
7
+ export const DEFAULT_EDGE_DECAY_PER_DAY = {
8
+ related: 0.05,
9
+ similar_to: 0.03,
10
+ opposes: 0.02,
11
+ causes: 0.01,
12
+ part_of: 0.01,
13
+ evolves_to: 0.01,
14
+ };
15
+ export const DEFAULT_EDGE_DECAY_FLOOR = 0.02;
16
+ /**
17
+ * 将日衰减率换算到时间间隔后的乘数。
18
+ *
19
+ * @param rate - 每日衰减比例(0.05 表示每日 ×0.95)
20
+ * @param days - 经过的天数
21
+ * @returns 乘数(≥0)
22
+ */
23
+ export function decayMultiplier(rate, days) {
24
+ if (days <= 0)
25
+ return 1;
26
+ const dailyKeep = Math.max(0, 1 - rate);
27
+ return Math.pow(dailyKeep, days);
28
+ }
29
+ /**
30
+ * 下一强度 = max(floor, w × mult)
31
+ */
32
+ export function nextEdgeStrength(current, relationType, days, config, floor = DEFAULT_EDGE_DECAY_FLOOR) {
33
+ const rate = config?.[relationType] ?? DEFAULT_EDGE_DECAY_PER_DAY[relationType];
34
+ return Math.max(floor, current * decayMultiplier(rate, days));
35
+ }
36
+ /**
37
+ * Hebbian 异步累积:w' = 1 - (1-w)(1-η·e)
38
+ *
39
+ * @param current - 当前边权
40
+ * @param eta - 学习率(默认 0.15)
41
+ * @param evidence - 证据强度 0–1
42
+ */
43
+ export function hebbianStrengthen(current, evidence, eta = 0.15) {
44
+ const e = Math.min(1, Math.max(0, evidence));
45
+ const w = Math.min(1, Math.max(0, current));
46
+ return 1 - (1 - w) * (1 - eta * e);
47
+ }
48
+ /**
49
+ * 反证降权:w' = w × (1-ρ)
50
+ */
51
+ export function counterEvidenceWeaken(current, rho = 0.3) {
52
+ return Math.max(0, current * (1 - Math.min(1, Math.max(0, rho))));
53
+ }
54
+ /** Layer 默认只吃 active / strengthened */
55
+ export const DEFAULT_RETRIEVE_STATUSES = ['active', 'strengthened'];
@@ -0,0 +1,60 @@
1
+ /**
2
+ * Cognition gates — 概念/关系准入执法(结构,不猜语义)
3
+ *
4
+ * 意图判断(是否可复用单元、关系假设)归 LLM;本模块只做:
5
+ * cue 校验、证据等级、保守降级、MDL 出生门槛、伪概念/密钥形态。
6
+ *
7
+ * @module harness/memory/cognition-gates
8
+ */
9
+ import type { AdmitConceptInput, ConceptGateReason, ConceptRelationType, EdgeBasis, EvidenceClass } from './types.js';
10
+ export interface ConceptGateOutcome {
11
+ ok: boolean;
12
+ reason: ConceptGateReason;
13
+ message?: string;
14
+ }
15
+ /** 归一化空白,用于 cue ⊆ evidence 子串校验 */
16
+ export declare function normalizeForCue(text: string): string;
17
+ /**
18
+ * 概念结构门控。
19
+ *
20
+ * @param input - 候选概念
21
+ * @param options - capacityAdmitStop 时禁止新生
22
+ * @returns 是否准入与 reason code
23
+ */
24
+ export declare function evaluateConceptGate(input: AdmitConceptInput, options?: {
25
+ capacityAdmitStop?: boolean;
26
+ }): ConceptGateOutcome;
27
+ export interface EdgeLicenseInput {
28
+ relationType: ConceptRelationType;
29
+ evidenceClass: EvidenceClass;
30
+ cue: string;
31
+ /** evidence + contextSlice 全文;用于 cue 子串校验 */
32
+ evidenceText?: string;
33
+ memoryIds: string[];
34
+ /** 对应 memory 的 status;用于「1 条 strengthened + 失败→修复」例外 */
35
+ memoryStatuses?: Array<'shadow' | 'active' | 'strengthened'>;
36
+ /** 对应 memory 的 channel;strengthened 例外要求 fail_fix */
37
+ memoryChannels?: Array<'user_directive' | 'decision' | 'fail_fix' | 'model_inference' | 'admin'>;
38
+ /** 配置:causes 最少独立命题(默认 2) */
39
+ causesMinIndependentMemories?: number;
40
+ }
41
+ export interface EdgeLicenseOutcome {
42
+ ok: boolean;
43
+ /** 实际应入库的关系类型(可能已降级) */
44
+ relationType: ConceptRelationType;
45
+ status: 'active' | 'shadow' | 'demoted' | 'candidate_only' | 'rejected';
46
+ reason: ConceptGateReason;
47
+ message?: string;
48
+ }
49
+ /**
50
+ * 关系持证门:强边需 cue ⊆ 证据 ∧ 证据形态匹配 ∧ 独立多证据。
51
+ * 任何不满足 → 保守降级为 `related` 或 candidate_only / rejected。
52
+ * **禁止** 共现升格为强类型。
53
+ */
54
+ export declare function licenseEdge(input: EdgeLicenseInput): EdgeLicenseOutcome;
55
+ /**
56
+ * 校验 basis 与许可结果一致;不一致时强制保守。
57
+ */
58
+ export declare function edgeBasisFromLicense(license: EdgeLicenseOutcome, basis: Omit<EdgeBasis, 'licensedAt'> & {
59
+ licensedAt?: number;
60
+ }): EdgeBasis;
@@ -0,0 +1,191 @@
1
+ /**
2
+ * Cognition gates — 概念/关系准入执法(结构,不猜语义)
3
+ *
4
+ * 意图判断(是否可复用单元、关系假设)归 LLM;本模块只做:
5
+ * cue 校验、证据等级、保守降级、MDL 出生门槛、伪概念/密钥形态。
6
+ *
7
+ * @module harness/memory/cognition-gates
8
+ */
9
+ import { CONCEPT_KINDS, CONCEPT_RELATION_TYPES, STRONG_RELATION_TYPES, } from './types.js';
10
+ const SECRET_LIKE = /(?:api[_-]?key|secret|password|token)\s*[:=]\s*\S{8,}/i;
11
+ const STOPWORD = /^(?:the|a|an|this|that|it|is|are|was|were|and|or|of|to|in|for)$/i;
12
+ const CJK = /[一-鿿]/;
13
+ /** 归一化空白,用于 cue ⊆ evidence 子串校验 */
14
+ export function normalizeForCue(text) {
15
+ return text.replace(/\s+/g, ' ').trim().toLowerCase();
16
+ }
17
+ /**
18
+ * 概念结构门控。
19
+ *
20
+ * @param input - 候选概念
21
+ * @param options - capacityAdmitStop 时禁止新生
22
+ * @returns 是否准入与 reason code
23
+ */
24
+ export function evaluateConceptGate(input, options) {
25
+ const name = (input.name ?? '').trim();
26
+ if (!name)
27
+ return { ok: false, reason: 'empty_name' };
28
+ if (!CONCEPT_KINDS.includes(input.kind)) {
29
+ return { ok: false, reason: 'invalid_kind', message: `kind=${input.kind}` };
30
+ }
31
+ if (SECRET_LIKE.test(name) || SECRET_LIKE.test(input.description ?? '')) {
32
+ return { ok: false, reason: 'secret_like' };
33
+ }
34
+ if (STOPWORD.test(name) || /^\d+$/.test(name)) {
35
+ return { ok: false, reason: 'pseudo_concept', message: 'stopword or numeric' };
36
+ }
37
+ // 非 CJK 拉丁名至少 2 字符;CJK 允许单字(锁、库…)
38
+ if (name.length < 2 && !CJK.test(name)) {
39
+ return { ok: false, reason: 'pseudo_concept', message: 'name too short' };
40
+ }
41
+ const support = input.supportCount ?? input.memoryIds?.length ?? 0;
42
+ const highValueKind = ['method', 'constraint', 'construct'];
43
+ if (support < 2 && !(support >= 1 && highValueKind.includes(input.kind))) {
44
+ return {
45
+ ok: false,
46
+ reason: 'mdl_insufficient',
47
+ message: `support=${support} kind=${input.kind}`,
48
+ };
49
+ }
50
+ if (options?.capacityAdmitStop) {
51
+ return { ok: false, reason: 'capacity_admit_stop' };
52
+ }
53
+ return { ok: true, reason: 'ok' };
54
+ }
55
+ // ── 关系持证 ──
56
+ /** relationType → 允许的 evidenceClass */
57
+ const RELATION_EVIDENCE = {
58
+ causes: ['causal'],
59
+ part_of: ['mereonymy'],
60
+ opposes: ['negation'],
61
+ similar_to: ['analogy'],
62
+ evolves_to: ['evolution'],
63
+ related: ['cooccur', 'causal', 'mereonymy', 'negation', 'analogy', 'evolution'],
64
+ };
65
+ /**
66
+ * 关系持证门:强边需 cue ⊆ 证据 ∧ 证据形态匹配 ∧ 独立多证据。
67
+ * 任何不满足 → 保守降级为 `related` 或 candidate_only / rejected。
68
+ * **禁止** 共现升格为强类型。
69
+ */
70
+ export function licenseEdge(input) {
71
+ const rel = input.relationType;
72
+ if (!CONCEPT_RELATION_TYPES.includes(rel)) {
73
+ return {
74
+ ok: false,
75
+ relationType: rel,
76
+ status: 'rejected',
77
+ reason: 'invalid_relation',
78
+ message: `relationType=${rel}`,
79
+ };
80
+ }
81
+ const allowed = RELATION_EVIDENCE[rel] ?? [];
82
+ const classOk = allowed.includes(input.evidenceClass);
83
+ const isStrong = STRONG_RELATION_TYPES.includes(rel);
84
+ // cue 校验(有 evidenceText 时强制)
85
+ if (input.evidenceText != null && input.evidenceText !== '') {
86
+ const hay = normalizeForCue(input.evidenceText);
87
+ const needle = normalizeForCue(input.cue ?? '');
88
+ if (!needle || !hay.includes(needle)) {
89
+ if (isStrong) {
90
+ return {
91
+ ok: true,
92
+ relationType: 'related',
93
+ status: 'demoted',
94
+ reason: 'cue_mismatch',
95
+ message: 'strong relation cue not in evidence; demoted to related',
96
+ };
97
+ }
98
+ return {
99
+ ok: false,
100
+ relationType: 'related',
101
+ status: 'rejected',
102
+ reason: 'cue_mismatch',
103
+ };
104
+ }
105
+ }
106
+ if (!classOk) {
107
+ if (isStrong) {
108
+ return {
109
+ ok: true,
110
+ relationType: 'related',
111
+ status: 'demoted',
112
+ reason: 'demoted_related',
113
+ message: `evidenceClass=${input.evidenceClass} cannot license ${rel}`,
114
+ };
115
+ }
116
+ return {
117
+ ok: false,
118
+ relationType: 'related',
119
+ status: 'rejected',
120
+ reason: 'demoted_related',
121
+ };
122
+ }
123
+ // 强边多证据(调用方应传入同 pair 累积 memoryIds)
124
+ const min = input.causesMinIndependentMemories ?? 2;
125
+ const memCount = new Set(input.memoryIds ?? []).size;
126
+ const hasStrengthened = (input.memoryStatuses ?? []).includes('strengthened');
127
+ const hasFailFix = (input.memoryChannels ?? []).includes('fail_fix');
128
+ if (rel === 'causes') {
129
+ // 2 条独立,或 1 条 strengthened + 失败→修复闭环(fail_fix)
130
+ const closedLoop = memCount >= 1 && hasStrengthened && hasFailFix && input.evidenceClass === 'causal';
131
+ const licensed = memCount >= min || closedLoop;
132
+ if (!licensed) {
133
+ return {
134
+ ok: true,
135
+ relationType: 'related',
136
+ status: 'candidate_only',
137
+ reason: memCount < 1 ? 'cue_mismatch' : 'multi_evidence_required',
138
+ message: `causes needs ≥${min} independent memories (have ${memCount}) or strengthened+fail_fix`,
139
+ };
140
+ }
141
+ }
142
+ else if (rel === 'opposes') {
143
+ // 2 条独立,或 supersede/否定极性(negation 类 1 条可 shadow 持证,2 条才 active)
144
+ if (memCount < 2) {
145
+ return {
146
+ ok: true,
147
+ relationType: rel,
148
+ status: 'shadow',
149
+ reason: 'multi_evidence_required',
150
+ message: `opposes needs ≥2 independent memories (have ${memCount})`,
151
+ };
152
+ }
153
+ }
154
+ else if (isStrong) {
155
+ // part_of / evolves_to:至少 1 条持证件
156
+ if (memCount < 1) {
157
+ return {
158
+ ok: true,
159
+ relationType: 'related',
160
+ status: 'shadow',
161
+ reason: 'multi_evidence_required',
162
+ };
163
+ }
164
+ }
165
+ // related / 共现:永远 shadow,不得因强度升格
166
+ if (rel === 'related' || input.evidenceClass === 'cooccur') {
167
+ return {
168
+ ok: true,
169
+ relationType: 'related',
170
+ status: isStrong ? 'demoted' : 'shadow',
171
+ reason: input.evidenceClass === 'cooccur' && rel !== 'related' ? 'cooccur_no_escalation' : 'ok',
172
+ };
173
+ }
174
+ return {
175
+ ok: true,
176
+ relationType: rel,
177
+ status: 'active',
178
+ reason: 'ok',
179
+ };
180
+ }
181
+ /**
182
+ * 校验 basis 与许可结果一致;不一致时强制保守。
183
+ */
184
+ export function edgeBasisFromLicense(license, basis) {
185
+ return {
186
+ memoryIds: basis.memoryIds ?? [],
187
+ cue: basis.cue ?? '',
188
+ evidenceClass: basis.evidenceClass,
189
+ licensedAt: basis.licensedAt ?? Date.now(),
190
+ };
191
+ }
@@ -0,0 +1,30 @@
1
+ /**
2
+ * Cognition 触发 — 命题落库后 emit 概念化请求
3
+ *
4
+ * 事件类型必须真实存在于 EventBus;sense.source=`eventBus` 才会挂监听。
5
+ *
6
+ * @module harness/memory/cognition-trigger
7
+ */
8
+ import type { EventBus } from '@octopi-agent/core/primitives/event-bus.js';
9
+ import type { MemoryChannel, MemoryStatus, MemoryType } from './types.js';
10
+ /** 概念化请求(cognition.steward.conceptualize 监听) */
11
+ export declare const COGNITIZE_REQUEST_EVENT = "cognition.conceptualize.request";
12
+ export interface ConceptualizeRequestPayload {
13
+ memoryId: string;
14
+ proposition: string;
15
+ evidence: string;
16
+ memoryType: MemoryType;
17
+ memoryStatus?: MemoryStatus;
18
+ channel?: MemoryChannel;
19
+ /** 证据周边语境;缺省由 handler 从 session 补 */
20
+ contextSlice?: string;
21
+ sessionId?: string;
22
+ agentId?: string;
23
+ }
24
+ /**
25
+ * 发射概念化请求。失败静默(不得阻断 memory 写路径)。
26
+ *
27
+ * @param events - EventBus
28
+ * @param payload - 请求载荷
29
+ */
30
+ export declare function emitConceptualizeRequest(events: EventBus | undefined, payload: ConceptualizeRequestPayload): void;
@@ -0,0 +1,41 @@
1
+ /**
2
+ * Cognition 触发 — 命题落库后 emit 概念化请求
3
+ *
4
+ * 事件类型必须真实存在于 EventBus;sense.source=`eventBus` 才会挂监听。
5
+ *
6
+ * @module harness/memory/cognition-trigger
7
+ */
8
+ /** 概念化请求(cognition.steward.conceptualize 监听) */
9
+ export const COGNITIZE_REQUEST_EVENT = 'cognition.conceptualize.request';
10
+ /**
11
+ * 发射概念化请求。失败静默(不得阻断 memory 写路径)。
12
+ *
13
+ * @param events - EventBus
14
+ * @param payload - 请求载荷
15
+ */
16
+ export function emitConceptualizeRequest(events, payload) {
17
+ if (!events)
18
+ return;
19
+ try {
20
+ events.emit({
21
+ type: COGNITIZE_REQUEST_EVENT,
22
+ timestamp: Date.now(),
23
+ agentId: payload.agentId,
24
+ sessionId: payload.sessionId,
25
+ data: {
26
+ memoryId: payload.memoryId,
27
+ proposition: payload.proposition,
28
+ evidence: payload.evidence,
29
+ memoryType: payload.memoryType,
30
+ memoryStatus: payload.memoryStatus,
31
+ channel: payload.channel,
32
+ contextSlice: payload.contextSlice,
33
+ sessionId: payload.sessionId,
34
+ agentId: payload.agentId,
35
+ },
36
+ });
37
+ }
38
+ catch {
39
+ // 总线异常不得拖垮 Memory 写入
40
+ }
41
+ }
@@ -1,4 +1,4 @@
1
1
  /**
2
2
  * Cognition 类型 — 规范定义在 ./types.ts
3
3
  */
4
- export type { ConceptNode, ConceptEdge, ConceptGraph, ConceptGraphStore, } from './types.js';
4
+ export type { ConceptKind, ConceptRelationType, ConceptStatus, EvidenceClass, EdgeBasis, ConceptNode, ConceptEdge, ConceptGraph, MergeCandidate, EdgeAux, AdmitConceptInput, AdmitConceptResult, AdmitEdgeInput, AdmitEdgeResult, ConceptGateReason, SpreadingActivateOptions, ActivatedGraph, ConceptGraphStats, DecayResult, ConceptGraphStore, } from './types.js';
@@ -1,16 +1,48 @@
1
1
  /**
2
- * InMemoryConceptGraph — 内存认知图谱
2
+ * InMemoryConceptGraph — 内存认知图谱(测试/轻量)
3
3
  *
4
- * P0 基础版:基于关键词的简单概念提取和关系建立。
5
- * P1 增强版:LLM 驱动的深度概念提取。
4
+ * 实现与 SqliteConceptGraph 同一 `ConceptGraphStore` 契约:
5
+ * 结构同一性合并、持证边、衰减、扩散激活。无正则 extractFromText。
6
+ *
7
+ * @module harness/memory/cognition
6
8
  */
7
- import type { ConceptGraphStore, ConceptNode, ConceptEdge, ConceptGraph } from './cognition-types.js';
9
+ import type { ActivatedGraph, AdmitConceptInput, AdmitConceptResult, AdmitEdgeInput, AdmitEdgeResult, ConceptGraph, ConceptGraphStats, ConceptGraphStore, DecayResult, MergeCandidate, SpreadingActivateOptions } from './cognition-types.js';
8
10
  export declare class InMemoryConceptGraph implements ConceptGraphStore {
9
11
  private nodes;
10
12
  private edges;
11
- addConcept(concept: Omit<ConceptNode, 'id' | 'frequency' | 'memoryIds'>): Promise<string>;
12
- addEdge(edge: ConceptEdge): Promise<void>;
13
- queryRelated(conceptName: string, depth?: number): Promise<ConceptGraph>;
14
- extractFromText(text: string, memoryId: string): Promise<void>;
13
+ private mergeCandidates;
14
+ private causalCandidates;
15
+ private get capacityStop();
16
+ admitConcept(input: AdmitConceptInput): Promise<AdmitConceptResult>;
17
+ private insertNode;
18
+ /** 义项指纹冲突:kind 不同或论域无交集且双方皆非空 */
19
+ private fingerprintConflict;
20
+ private pushMergeCandidate;
21
+ private collectPairMemoryIds;
22
+ admitEdge(input: AdmitEdgeInput): Promise<AdmitEdgeResult>;
23
+ addCausalCandidate(input: {
24
+ sourceId: string;
25
+ targetId: string;
26
+ memoryIds: string[];
27
+ cue: string;
28
+ note?: string;
29
+ }): Promise<void>;
30
+ listMergeCandidates(limit?: number): Promise<MergeCandidate[]>;
31
+ resolveMerge(id: string, action: 'merge' | 'keep_split' | 'drop'): Promise<void>;
32
+ reinforce(input: {
33
+ nodeIds?: string[];
34
+ edgeIds?: string[];
35
+ evidenceStrength?: number;
36
+ }): Promise<void>;
37
+ counterEvidence(input: {
38
+ edgeId: string;
39
+ memoryId?: string;
40
+ markOpposes?: boolean;
41
+ }): Promise<void>;
42
+ promote(ids: string[], to: 'active' | 'strengthened'): Promise<void>;
43
+ demote(ids: string[], to: 'shadow'): Promise<void>;
44
+ applyDecay(now?: number): Promise<DecayResult>;
45
+ spreadingActivate(seeds: string[], options?: SpreadingActivateOptions): Promise<ActivatedGraph>;
15
46
  getFullGraph(): Promise<ConceptGraph>;
47
+ stats(): Promise<ConceptGraphStats>;
16
48
  }