@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.
- package/dist/config-schema/engine.d.ts +84 -0
- package/dist/config-schema/engine.js +4 -1
- package/dist/config-schema/substrate.d.ts +86 -0
- package/dist/config-schema/substrate.js +114 -0
- package/dist/config.d.ts +71 -0
- package/dist/harness/agent/builder.d.ts +1 -1
- package/dist/harness/agent/builder.js +27 -2
- package/dist/harness/agent/config-bridge.js +17 -4
- package/dist/harness/context/capabilities/summary/gate.js +2 -2
- package/dist/harness/context/capabilities/summary/policies.js +5 -5
- package/dist/harness/context/capabilities/summary/registry.js +29 -24
- package/dist/harness/context/layer-health.js +3 -2
- package/dist/harness/context/layers.d.ts +27 -13
- package/dist/harness/context/layers.js +76 -66
- package/dist/harness/context/system-prompt-assembler.d.ts +3 -0
- package/dist/harness/context/system-prompt-assembler.js +11 -1
- package/dist/harness/extension/plugin-ecosystem/client-tools/index.d.ts +4 -0
- package/dist/harness/extension/plugin-ecosystem/client-tools/index.js +3 -0
- package/dist/harness/extension/plugin-ecosystem/client-tools/invoke.d.ts +56 -0
- package/dist/harness/extension/plugin-ecosystem/client-tools/invoke.js +281 -0
- package/dist/harness/extension/plugin-ecosystem/client-tools/registry.d.ts +98 -0
- package/dist/harness/extension/plugin-ecosystem/client-tools/registry.js +250 -0
- package/dist/harness/extension/plugin-ecosystem/client-tools/stream.d.ts +124 -0
- package/dist/harness/extension/plugin-ecosystem/client-tools/stream.js +372 -0
- package/dist/harness/extension/plugin-ecosystem/client-tools/types.d.ts +109 -0
- package/dist/harness/extension/plugin-ecosystem/client-tools/types.js +8 -0
- package/dist/harness/extension/plugin-ecosystem/index.d.ts +1 -0
- package/dist/harness/extension/plugin-ecosystem/index.js +1 -0
- package/dist/harness/extension/plugin-ecosystem/tools/builtin.js +1 -1
- package/dist/harness/extension/plugin-ecosystem/tools/memory.d.ts +11 -1
- package/dist/harness/extension/plugin-ecosystem/tools/memory.js +14 -0
- package/dist/harness/index.d.ts +4 -3
- package/dist/harness/index.js +2 -1
- package/dist/harness/knowledge/query-service.js +5 -5
- package/dist/harness/knowledge/retriever.js +1 -1
- package/dist/harness/knowledge/source-store.d.ts +17 -2
- package/dist/harness/knowledge/source-store.js +6 -5
- package/dist/harness/memory/cognition-decay.d.ts +36 -0
- package/dist/harness/memory/cognition-decay.js +55 -0
- package/dist/harness/memory/cognition-gates.d.ts +60 -0
- package/dist/harness/memory/cognition-gates.js +191 -0
- package/dist/harness/memory/cognition-trigger.d.ts +30 -0
- package/dist/harness/memory/cognition-trigger.js +41 -0
- package/dist/harness/memory/cognition-types.d.ts +1 -1
- package/dist/harness/memory/cognition.d.ts +40 -8
- package/dist/harness/memory/cognition.js +432 -79
- package/dist/harness/memory/conceptualizer.d.ts +66 -0
- package/dist/harness/memory/conceptualizer.js +246 -0
- package/dist/harness/memory/index.d.ts +16 -2
- package/dist/harness/memory/index.js +11 -1
- package/dist/harness/memory/sqlite/agent-db.d.ts +9 -0
- package/dist/harness/memory/sqlite/agent-db.js +118 -15
- package/dist/harness/memory/sqlite/cognition-store.d.ts +55 -16
- package/dist/harness/memory/sqlite/cognition-store.js +591 -144
- package/dist/harness/memory/sqlite/wisdom-store.d.ts +33 -14
- package/dist/harness/memory/sqlite/wisdom-store.js +257 -44
- package/dist/harness/memory/types.d.ts +331 -16
- package/dist/harness/memory/types.js +49 -0
- package/dist/harness/memory/wisdom-formation.d.ts +101 -0
- package/dist/harness/memory/wisdom-formation.js +304 -0
- package/dist/harness/memory/wisdom-gates.d.ts +50 -0
- package/dist/harness/memory/wisdom-gates.js +204 -0
- package/dist/harness/memory/wisdom-policy.d.ts +107 -0
- package/dist/harness/memory/wisdom-policy.js +220 -0
- package/dist/harness/memory/wisdom-types.d.ts +3 -2
- package/dist/harness/memory/wisdom-types.js +2 -2
- package/dist/harness/memory/wisdom.d.ts +27 -0
- package/dist/harness/memory/wisdom.js +181 -0
- package/dist/harness/run/agent/agent.d.ts +9 -0
- package/dist/harness/run/agent/agent.js +19 -0
- package/dist/harness/run/reliability/run-agent.js +6 -2
- package/dist/harness/run/runner.js +2 -1
- package/dist/harness/session/index.d.ts +1 -0
- package/dist/harness/session/index.js +1 -0
- package/dist/harness/session/title.d.ts +86 -0
- package/dist/harness/session/title.js +268 -0
- package/dist/harness/shared/events/agent-event-map.d.ts +8 -0
- package/dist/harness/shared/events/agent-event-map.js +1 -0
- package/dist/index.d.ts +3 -1
- package/dist/index.js +1 -0
- package/dist/subsystems/cognition-steward/conceptualize/SUBSYSTEM.md +43 -0
- package/dist/subsystems/cognition-steward/conceptualize/config.yaml +50 -0
- package/dist/subsystems/cognition-steward/conceptualize/handler.d.ts +9 -0
- package/dist/subsystems/cognition-steward/conceptualize/handler.js +125 -0
- package/dist/subsystems/cognition-steward/consolidate/SUBSYSTEM.md +6 -0
- package/dist/subsystems/cognition-steward/consolidate/config.yaml +54 -0
- package/dist/subsystems/cognition-steward/consolidate/handler.d.ts +12 -0
- package/dist/subsystems/cognition-steward/consolidate/handler.js +93 -0
- package/dist/subsystems/wisdom-steward/evaluate/config.yaml +50 -0
- package/dist/subsystems/wisdom-steward/evaluate/handler.d.ts +18 -0
- package/dist/subsystems/wisdom-steward/evaluate/handler.js +91 -0
- package/dist/subsystems/wisdom-steward/formation/SUBSYSTEM.md +42 -0
- package/dist/subsystems/wisdom-steward/formation/config.yaml +54 -0
- package/dist/subsystems/wisdom-steward/formation/handler.d.ts +18 -0
- package/dist/subsystems/wisdom-steward/formation/handler.js +212 -0
- package/dist/subsystems/wisdom-steward/govern/config.yaml +55 -0
- package/dist/subsystems/wisdom-steward/govern/handler.d.ts +17 -0
- package/dist/subsystems/wisdom-steward/govern/handler.js +68 -0
- package/package.json +2 -2
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Wisdom policy — 评估/治理确定性策略
|
|
3
|
+
*
|
|
4
|
+
* 弱归因贝叶斯更新、utility 淘汰、priority 衰减、状态迁移规划。
|
|
5
|
+
* 代码执法;不调用 LLM。规范:arch/wisdom-layer-formation.md §5–§8。
|
|
6
|
+
*
|
|
7
|
+
* @module harness/memory/wisdom-policy
|
|
8
|
+
*/
|
|
9
|
+
export const DEFAULT_WISDOM_EVALUATION = {
|
|
10
|
+
trialAlpha: 0.18,
|
|
11
|
+
trialBeta: 0.22,
|
|
12
|
+
activeAlpha: 0.1,
|
|
13
|
+
activeBeta: 0.14,
|
|
14
|
+
strongAlpha: 0.05,
|
|
15
|
+
strongBeta: 0.07,
|
|
16
|
+
minAppliesForActive: 3,
|
|
17
|
+
contestRate: 0.5,
|
|
18
|
+
counterevidenceWeight: 2.5,
|
|
19
|
+
strengthenMinApplies: 12,
|
|
20
|
+
strengthenMaxContestRate: 0.15,
|
|
21
|
+
};
|
|
22
|
+
export const DEFAULT_WISDOM_GOVERN = {
|
|
23
|
+
softCap: 50,
|
|
24
|
+
hardCap: 80,
|
|
25
|
+
zeroApplyIdleMs: 30 * 24 * 3600_000,
|
|
26
|
+
priorityDecayStep: 5,
|
|
27
|
+
priorityFloor: 5,
|
|
28
|
+
dryRun: false,
|
|
29
|
+
};
|
|
30
|
+
/**
|
|
31
|
+
* 结局事件折叠到条目计数。
|
|
32
|
+
*
|
|
33
|
+
* @param entry - 当前条目(不修改)
|
|
34
|
+
* @param events - 该条相关信号
|
|
35
|
+
* @returns 更新后的 outcomes 计数与时间戳
|
|
36
|
+
*/
|
|
37
|
+
export function foldOutcomeEvents(entry, events) {
|
|
38
|
+
const o = {
|
|
39
|
+
applied: entry.outcomes.applied,
|
|
40
|
+
cited: entry.outcomes.cited,
|
|
41
|
+
assisted: entry.outcomes.assisted,
|
|
42
|
+
contested: entry.outcomes.contested,
|
|
43
|
+
lastAppliedAt: entry.outcomes.lastAppliedAt,
|
|
44
|
+
lastOutcomeAt: entry.outcomes.lastOutcomeAt,
|
|
45
|
+
evalAssisted: entry.outcomes.evalAssisted ?? 0,
|
|
46
|
+
evalContested: entry.outcomes.evalContested ?? 0,
|
|
47
|
+
};
|
|
48
|
+
let maxAt = o.lastOutcomeAt;
|
|
49
|
+
for (const e of events) {
|
|
50
|
+
const at = e.at ?? Date.now();
|
|
51
|
+
if (maxAt == null || at > maxAt)
|
|
52
|
+
maxAt = at;
|
|
53
|
+
switch (e.signal) {
|
|
54
|
+
case 'applied':
|
|
55
|
+
o.applied += 1;
|
|
56
|
+
o.lastAppliedAt = at;
|
|
57
|
+
break;
|
|
58
|
+
case 'cited':
|
|
59
|
+
o.cited += 1;
|
|
60
|
+
break;
|
|
61
|
+
case 'assisted':
|
|
62
|
+
o.assisted += 1;
|
|
63
|
+
break;
|
|
64
|
+
case 'contested':
|
|
65
|
+
o.contested += 1;
|
|
66
|
+
break;
|
|
67
|
+
case 'ignored':
|
|
68
|
+
break;
|
|
69
|
+
default:
|
|
70
|
+
break;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
o.lastOutcomeAt = maxAt;
|
|
74
|
+
return { outcomes: o };
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* 贝叶斯风格 confidence 弱更新 + 状态迁移规划。
|
|
78
|
+
*
|
|
79
|
+
* **只对增量生效**:`outcomes.assisted/contested` 终身累计,与水位
|
|
80
|
+
* `evalAssisted/evalContested` 之差才是本轮增量。空跑 pulse 不得再拉 confidence。
|
|
81
|
+
* applied=0 只影响 priority(govern),不减 confidence。
|
|
82
|
+
*
|
|
83
|
+
* @param entry - 条目
|
|
84
|
+
* @param cfg - 评估参数
|
|
85
|
+
* @param now - 当前时间
|
|
86
|
+
* @returns 是否需要写回与新值(含水位);无需写回时 null
|
|
87
|
+
*/
|
|
88
|
+
export function planConfidenceUpdate(entry, cfg = {}, now = Date.now()) {
|
|
89
|
+
const c = { ...DEFAULT_WISDOM_EVALUATION, ...cfg };
|
|
90
|
+
const { assisted, contested, applied } = entry.outcomes;
|
|
91
|
+
const trial = entry.status === 'trial';
|
|
92
|
+
const strong = entry.status === 'strengthened';
|
|
93
|
+
const alpha = trial ? c.trialAlpha : strong ? c.strongAlpha : c.activeAlpha;
|
|
94
|
+
const beta = trial ? c.trialBeta : strong ? c.strongBeta : c.activeBeta;
|
|
95
|
+
// 增量:只吃尚未并入 confidence 的部分
|
|
96
|
+
const dPos = Math.max(0, assisted - (entry.outcomes.evalAssisted ?? 0));
|
|
97
|
+
const dNeg = Math.max(0, contested - (entry.outcomes.evalContested ?? 0));
|
|
98
|
+
let confidence = entry.confidence;
|
|
99
|
+
if (dPos > 0) {
|
|
100
|
+
confidence = confidence + alpha * (1 - confidence) * Math.min(dPos, 5);
|
|
101
|
+
}
|
|
102
|
+
if (dNeg > 0) {
|
|
103
|
+
confidence = confidence - beta * confidence * Math.min(dNeg, 5);
|
|
104
|
+
}
|
|
105
|
+
confidence = Math.min(1, Math.max(0, confidence));
|
|
106
|
+
let status = entry.status;
|
|
107
|
+
let reason = 'noop';
|
|
108
|
+
const ceWeight = (entry.counterevidence ?? []).reduce((s, x) => s + (x.weight || 0), 0);
|
|
109
|
+
const contestRate = applied > 0 ? contested / applied : 0;
|
|
110
|
+
if (status !== 'superseded' && status !== 'retired') {
|
|
111
|
+
if (ceWeight >= c.counterevidenceWeight || (applied >= 3 && contestRate >= c.contestRate && contested >= 2)) {
|
|
112
|
+
if (status !== 'contested') {
|
|
113
|
+
status = 'contested';
|
|
114
|
+
reason = 'contest_threshold';
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
else if (status === 'trial' && applied >= c.minAppliesForActive && assisted > contested) {
|
|
118
|
+
status = 'active';
|
|
119
|
+
reason = 'trial_to_active';
|
|
120
|
+
}
|
|
121
|
+
else if ((status === 'active' || status === 'strengthened') &&
|
|
122
|
+
applied >= c.strengthenMinApplies &&
|
|
123
|
+
contestRate <= c.strengthenMaxContestRate &&
|
|
124
|
+
assisted > contested) {
|
|
125
|
+
if (status !== 'strengthened') {
|
|
126
|
+
status = 'strengthened';
|
|
127
|
+
reason = 'strengthened';
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
else if (status === 'contested' && contestRate < c.contestRate / 2 && assisted > contested) {
|
|
131
|
+
status = 'trial';
|
|
132
|
+
reason = 'contested_repair';
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
const nextEvalAssisted = assisted;
|
|
136
|
+
const nextEvalContested = contested;
|
|
137
|
+
const watermarkMoved = (entry.outcomes.evalAssisted ?? 0) !== nextEvalAssisted ||
|
|
138
|
+
(entry.outcomes.evalContested ?? 0) !== nextEvalContested;
|
|
139
|
+
const changed = Math.abs(confidence - entry.confidence) > 1e-6 || status !== entry.status || watermarkMoved;
|
|
140
|
+
if (!changed)
|
|
141
|
+
return null;
|
|
142
|
+
return {
|
|
143
|
+
id: entry.id,
|
|
144
|
+
confidence,
|
|
145
|
+
status,
|
|
146
|
+
reason,
|
|
147
|
+
evalAssisted: nextEvalAssisted,
|
|
148
|
+
evalContested: nextEvalContested,
|
|
149
|
+
};
|
|
150
|
+
}
|
|
151
|
+
/**
|
|
152
|
+
* 容量演化 + 零应用衰减规划。
|
|
153
|
+
*
|
|
154
|
+
* @param entries - 存活条目(不含 retired/superseded)
|
|
155
|
+
* @param cfg - 治理参数
|
|
156
|
+
* @param now - 当前时间
|
|
157
|
+
* @returns 治理动作列表
|
|
158
|
+
*/
|
|
159
|
+
export function planWisdomGovern(entries, cfg = {}, now = Date.now()) {
|
|
160
|
+
const c = { ...DEFAULT_WISDOM_GOVERN, ...cfg };
|
|
161
|
+
const plan = [];
|
|
162
|
+
const alive = entries.filter((e) => e.status !== 'retired' && e.status !== 'superseded');
|
|
163
|
+
// 零应用衰减(保护 strengthened / factory 不轻易退休,但仍降 priority)
|
|
164
|
+
for (const e of alive) {
|
|
165
|
+
const last = e.outcomes.lastAppliedAt ?? e.updatedAt;
|
|
166
|
+
const idle = now - last;
|
|
167
|
+
if (idle >= c.zeroApplyIdleMs && e.outcomes.applied === 0) {
|
|
168
|
+
const next = Math.max(c.priorityFloor, e.priority - c.priorityDecayStep);
|
|
169
|
+
if (next < e.priority) {
|
|
170
|
+
plan.push({
|
|
171
|
+
id: e.id,
|
|
172
|
+
action: 'decay_priority',
|
|
173
|
+
reason: 'zero_apply_idle',
|
|
174
|
+
priority: next,
|
|
175
|
+
});
|
|
176
|
+
}
|
|
177
|
+
// factory 核心不因零应用退休
|
|
178
|
+
if (e.origin !== 'factory' && e.status !== 'strengthened' && next <= c.priorityFloor) {
|
|
179
|
+
plan.push({ id: e.id, action: 'retire', reason: 'zero_apply_floor' });
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
// 容量:超 softCap 淘汰最低 utility(保护 factory / strengthened)
|
|
184
|
+
const living = alive.filter((e) => !plan.some((p) => p.id === e.id && p.action === 'retire'));
|
|
185
|
+
if (living.length > c.softCap) {
|
|
186
|
+
const ranked = [...living].sort((a, b) => wisdomUtility(a) - wisdomUtility(b));
|
|
187
|
+
const excess = living.length - c.softCap;
|
|
188
|
+
let dropped = 0;
|
|
189
|
+
for (const e of ranked) {
|
|
190
|
+
if (dropped >= excess)
|
|
191
|
+
break;
|
|
192
|
+
if (e.origin === 'factory' || e.status === 'strengthened')
|
|
193
|
+
continue;
|
|
194
|
+
plan.push({ id: e.id, action: 'retire', reason: 'capacity' });
|
|
195
|
+
dropped++;
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
return plan;
|
|
199
|
+
}
|
|
200
|
+
/**
|
|
201
|
+
* utility = confidence × (0.5 + 0.5·normalizedApplies) × (priority/100)
|
|
202
|
+
*
|
|
203
|
+
* @param e - 条目
|
|
204
|
+
* @returns 淘汰排序分
|
|
205
|
+
*/
|
|
206
|
+
export function wisdomUtility(e) {
|
|
207
|
+
const applies = Math.min(1, e.outcomes.applied / 20);
|
|
208
|
+
return e.confidence * (0.5 + 0.5 * applies) * (e.priority / 100);
|
|
209
|
+
}
|
|
210
|
+
/**
|
|
211
|
+
* 注入选择打分。
|
|
212
|
+
*
|
|
213
|
+
* @param entry - 条目
|
|
214
|
+
* @param matchScore - 场景匹配分 [0,1];core 用 1
|
|
215
|
+
* @returns 注入排序分
|
|
216
|
+
*/
|
|
217
|
+
export function wisdomInjectScore(entry, matchScore) {
|
|
218
|
+
const statusBoost = entry.status === 'strengthened' ? 1.15 : entry.status === 'active' ? 1 : 0.75;
|
|
219
|
+
return wisdomUtility(entry) * statusBoost * (0.35 + 0.65 * matchScore);
|
|
220
|
+
}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Wisdom 类型 — 规范定义在 ./types.ts
|
|
2
|
+
* Wisdom 类型 — 规范定义在 ./types.ts(maxim;见 arch/wisdom-layer-formation.md)
|
|
3
3
|
*/
|
|
4
|
-
export type { WisdomEntry, WisdomStore, } from './types.js';
|
|
4
|
+
export type { WisdomStatus, WisdomOrigin, WisdomKind, WisdomRetireReason, WisdomScenario, WisdomEffect, WisdomDerivation, WisdomCounterevidence, WisdomOutcomes, WisdomEntry, WisdomGateReason, WisdomGateOutcome, AdmitWisdomInput, AdmitWisdomAction, AdmitWisdomResult, WisdomInjectQuery, WisdomInjectPick, WisdomOutcomeSignal, WisdomOutcomeEvent, WisdomStats, WisdomStore, } from './types.js';
|
|
5
|
+
export { WISDOM_STATUSES, WISDOM_INJECTABLE_STATUSES, WISDOM_ORIGINS, WISDOM_KINDS, } from './types.js';
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* InMemoryWisdomStore — 测试/开发用内存实现
|
|
3
|
+
*
|
|
4
|
+
* @module harness/memory/wisdom
|
|
5
|
+
*/
|
|
6
|
+
import type { AdmitWisdomInput, AdmitWisdomResult, WisdomEntry, WisdomInjectPick, WisdomInjectQuery, WisdomRetireReason, WisdomStats, WisdomStore, WisdomOutcomeEvent } from './types.js';
|
|
7
|
+
export declare class InMemoryWisdomStore implements WisdomStore {
|
|
8
|
+
readonly name = "in-memory-wisdom";
|
|
9
|
+
private entries;
|
|
10
|
+
private seq;
|
|
11
|
+
admit(input: AdmitWisdomInput, options?: {
|
|
12
|
+
allowedMemoryIds?: ReadonlySet<string>;
|
|
13
|
+
}): Promise<AdmitWisdomResult>;
|
|
14
|
+
get(id: string): Promise<WisdomEntry | null>;
|
|
15
|
+
selectForInjection(query: WisdomInjectQuery): Promise<WisdomInjectPick[]>;
|
|
16
|
+
listForGovern(filter?: {
|
|
17
|
+
includeRetired?: boolean;
|
|
18
|
+
}): Promise<WisdomEntry[]>;
|
|
19
|
+
update(id: string, patch: Partial<WisdomEntry>): Promise<void>;
|
|
20
|
+
softRetire(id: string, meta: {
|
|
21
|
+
by: string;
|
|
22
|
+
reason: WisdomRetireReason | string;
|
|
23
|
+
}): Promise<void>;
|
|
24
|
+
recordOutcomes(events: WisdomOutcomeEvent[]): Promise<void>;
|
|
25
|
+
touchApplied(ids: string[]): Promise<void>;
|
|
26
|
+
stats(): Promise<WisdomStats>;
|
|
27
|
+
}
|
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* InMemoryWisdomStore — 测试/开发用内存实现
|
|
3
|
+
*
|
|
4
|
+
* @module harness/memory/wisdom
|
|
5
|
+
*/
|
|
6
|
+
import { WISDOM_STATUSES } from './types.js';
|
|
7
|
+
import { evaluateWisdomGate, resolveInitialWisdomStatus } from './wisdom-gates.js';
|
|
8
|
+
import { foldOutcomeEvents, planConfidenceUpdate } from './wisdom-policy.js';
|
|
9
|
+
import { pickForInjection } from './wisdom-formation.js';
|
|
10
|
+
export class InMemoryWisdomStore {
|
|
11
|
+
name = 'in-memory-wisdom';
|
|
12
|
+
entries = new Map();
|
|
13
|
+
seq = 0;
|
|
14
|
+
async admit(input, options) {
|
|
15
|
+
const existing = [...this.entries.values()]
|
|
16
|
+
.filter((e) => e.status !== 'retired')
|
|
17
|
+
.map((e) => ({ id: e.id, statement: e.statement, status: e.status, kind: e.kind }));
|
|
18
|
+
const gate = evaluateWisdomGate(input, {
|
|
19
|
+
existing,
|
|
20
|
+
allowedMemoryIds: options?.allowedMemoryIds,
|
|
21
|
+
});
|
|
22
|
+
if (!gate.ok) {
|
|
23
|
+
return { action: 'rejected', reason: gate.reason, message: gate.message };
|
|
24
|
+
}
|
|
25
|
+
const now = Date.now();
|
|
26
|
+
const status = resolveInitialWisdomStatus(input);
|
|
27
|
+
const id = `wis_${++this.seq}`;
|
|
28
|
+
if (input.supersedesId) {
|
|
29
|
+
const prev = this.entries.get(input.supersedesId);
|
|
30
|
+
if (!prev) {
|
|
31
|
+
return {
|
|
32
|
+
action: 'rejected',
|
|
33
|
+
reason: 'supersedes_target_missing',
|
|
34
|
+
message: `supersedesId=${input.supersedesId}`,
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
this.entries.set(prev.id, {
|
|
38
|
+
...prev,
|
|
39
|
+
status: 'superseded',
|
|
40
|
+
supersededBy: id,
|
|
41
|
+
updatedAt: now,
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
this.entries.set(id, {
|
|
45
|
+
id,
|
|
46
|
+
statement: input.statement.trim(),
|
|
47
|
+
rationale: input.rationale,
|
|
48
|
+
scenario: input.scenario,
|
|
49
|
+
effect: input.effect,
|
|
50
|
+
status,
|
|
51
|
+
confidence: input.confidence ?? (status === 'active' ? 0.7 : 0.5),
|
|
52
|
+
priority: input.priority ?? (status === 'active' ? 50 : 30),
|
|
53
|
+
derivedFrom: input.derivedFrom,
|
|
54
|
+
exceptions: input.exceptions,
|
|
55
|
+
counterevidence: [],
|
|
56
|
+
outcomes: emptyOutcomes(),
|
|
57
|
+
origin: input.origin ?? 'distilled',
|
|
58
|
+
kind: input.kind,
|
|
59
|
+
createdAt: now,
|
|
60
|
+
updatedAt: now,
|
|
61
|
+
});
|
|
62
|
+
return { action: input.supersedesId ? 'superseded' : 'created', id };
|
|
63
|
+
}
|
|
64
|
+
async get(id) {
|
|
65
|
+
return this.entries.get(id) ?? null;
|
|
66
|
+
}
|
|
67
|
+
async selectForInjection(query) {
|
|
68
|
+
const entries = [...this.entries.values()];
|
|
69
|
+
return pickForInjection(entries, query.text ?? '', {
|
|
70
|
+
coreMaxItems: query.coreMaxItems,
|
|
71
|
+
scenarioMaxItems: query.scenarioMaxItems,
|
|
72
|
+
includeTrial: query.includeTrial,
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
async listForGovern(filter) {
|
|
76
|
+
return [...this.entries.values()].filter((e) => filter?.includeRetired || e.status !== 'retired');
|
|
77
|
+
}
|
|
78
|
+
async update(id, patch) {
|
|
79
|
+
const cur = this.entries.get(id);
|
|
80
|
+
if (!cur)
|
|
81
|
+
throw new Error(`wisdom_update_not_found: ${id}`);
|
|
82
|
+
this.entries.set(id, {
|
|
83
|
+
...cur,
|
|
84
|
+
...patch,
|
|
85
|
+
id: cur.id,
|
|
86
|
+
createdAt: cur.createdAt,
|
|
87
|
+
updatedAt: Date.now(),
|
|
88
|
+
outcomes: { ...cur.outcomes, ...(patch.outcomes ?? {}) },
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
async softRetire(id, meta) {
|
|
92
|
+
const cur = this.entries.get(id);
|
|
93
|
+
if (!cur || cur.status === 'superseded')
|
|
94
|
+
return;
|
|
95
|
+
await this.update(id, {
|
|
96
|
+
status: 'retired',
|
|
97
|
+
exceptions: [...(cur.exceptions ?? []), `retired:${meta.reason}:${meta.by}`],
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
async recordOutcomes(events) {
|
|
101
|
+
const byId = new Map();
|
|
102
|
+
for (const e of events) {
|
|
103
|
+
const list = byId.get(e.wisdomId) ?? [];
|
|
104
|
+
list.push(e);
|
|
105
|
+
byId.set(e.wisdomId, list);
|
|
106
|
+
}
|
|
107
|
+
for (const [id, evs] of byId) {
|
|
108
|
+
const cur = this.entries.get(id);
|
|
109
|
+
if (!cur)
|
|
110
|
+
continue;
|
|
111
|
+
const folded = foldOutcomeEvents(cur, evs);
|
|
112
|
+
let next = { ...cur, ...folded, updatedAt: Date.now() };
|
|
113
|
+
const plan = planConfidenceUpdate(next);
|
|
114
|
+
if (plan) {
|
|
115
|
+
next = {
|
|
116
|
+
...next,
|
|
117
|
+
confidence: plan.confidence,
|
|
118
|
+
status: plan.status,
|
|
119
|
+
outcomes: {
|
|
120
|
+
...next.outcomes,
|
|
121
|
+
evalAssisted: plan.evalAssisted,
|
|
122
|
+
evalContested: plan.evalContested,
|
|
123
|
+
},
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
const counter = evs.filter((e) => e.signal === 'contested' && (e.ref || e.note));
|
|
127
|
+
if (counter.length) {
|
|
128
|
+
const ce = [...(next.counterevidence ?? [])];
|
|
129
|
+
for (const c of counter) {
|
|
130
|
+
ce.push({ ref: c.ref ?? c.wisdomId, note: c.note ?? 'contested', at: c.at ?? Date.now(), weight: 1 });
|
|
131
|
+
}
|
|
132
|
+
next = { ...next, counterevidence: ce };
|
|
133
|
+
}
|
|
134
|
+
await this.update(id, next);
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
async touchApplied(ids) {
|
|
138
|
+
const now = Date.now();
|
|
139
|
+
for (const id of ids) {
|
|
140
|
+
const cur = this.entries.get(id);
|
|
141
|
+
if (!cur)
|
|
142
|
+
continue;
|
|
143
|
+
this.entries.set(id, {
|
|
144
|
+
...cur,
|
|
145
|
+
outcomes: {
|
|
146
|
+
...cur.outcomes,
|
|
147
|
+
applied: cur.outcomes.applied + 1,
|
|
148
|
+
lastAppliedAt: now,
|
|
149
|
+
},
|
|
150
|
+
updatedAt: now,
|
|
151
|
+
});
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
async stats() {
|
|
155
|
+
const list = [...this.entries.values()];
|
|
156
|
+
const byStatus = Object.fromEntries(WISDOM_STATUSES.map((s) => [s, 0]));
|
|
157
|
+
let conf = 0;
|
|
158
|
+
let prio = 0;
|
|
159
|
+
let applied = 0;
|
|
160
|
+
let contested = 0;
|
|
161
|
+
for (const e of list) {
|
|
162
|
+
byStatus[e.status] = (byStatus[e.status] ?? 0) + 1;
|
|
163
|
+
conf += e.confidence;
|
|
164
|
+
prio += e.priority;
|
|
165
|
+
applied += e.outcomes.applied;
|
|
166
|
+
contested += e.outcomes.contested;
|
|
167
|
+
}
|
|
168
|
+
const n = list.length || 1;
|
|
169
|
+
return {
|
|
170
|
+
total: list.length,
|
|
171
|
+
byStatus,
|
|
172
|
+
avgConfidence: conf / n,
|
|
173
|
+
avgPriority: prio / n,
|
|
174
|
+
totalApplied: applied,
|
|
175
|
+
totalContested: contested,
|
|
176
|
+
};
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
function emptyOutcomes() {
|
|
180
|
+
return { applied: 0, cited: 0, assisted: 0, contested: 0, evalAssisted: 0, evalContested: 0 };
|
|
181
|
+
}
|
|
@@ -54,6 +54,8 @@ export declare class Agent {
|
|
|
54
54
|
/** sessionId → 压缩状态快照;Runner 从 Session 播种,assemble 后回写 Session */
|
|
55
55
|
private _sessionCompactStates;
|
|
56
56
|
private _onAfterTurn?;
|
|
57
|
+
/** Client Tool 会话可见性;undefined = 全部可见 */
|
|
58
|
+
private _toolVisibilityFilter?;
|
|
57
59
|
constructor(options: AgentOptions);
|
|
58
60
|
get context(): AgentContext;
|
|
59
61
|
get messages(): Message[];
|
|
@@ -67,6 +69,13 @@ export declare class Agent {
|
|
|
67
69
|
get harness(): ReliabilityHarness | undefined;
|
|
68
70
|
registerTool(tool: AgentTool): void;
|
|
69
71
|
setTools(tools: AgentTool[]): void;
|
|
72
|
+
/**
|
|
73
|
+
* 会话级 tool 面可见性(Client Tool:无 live client 则不进 LLM tool 面)。
|
|
74
|
+
* 返回 false 的 tool 对该 session 的 run 隐藏;server tool 不受影响。
|
|
75
|
+
*/
|
|
76
|
+
setToolVisibilityFilter(filter: ((sessionId: string, toolName: string) => boolean) | undefined): void;
|
|
77
|
+
/** 本 session 的 tool 列表(含可见性过滤) */
|
|
78
|
+
toolsForSession(sessionId: string): AgentTool[];
|
|
70
79
|
setModel(model: ModelProvider): void;
|
|
71
80
|
setSystemPrompt(prompt: string): void;
|
|
72
81
|
setHarness(harness: ReliabilityHarness): void;
|
|
@@ -26,6 +26,8 @@ export class Agent {
|
|
|
26
26
|
/** sessionId → 压缩状态快照;Runner 从 Session 播种,assemble 后回写 Session */
|
|
27
27
|
_sessionCompactStates = new Map();
|
|
28
28
|
_onAfterTurn;
|
|
29
|
+
/** Client Tool 会话可见性;undefined = 全部可见 */
|
|
30
|
+
_toolVisibilityFilter;
|
|
29
31
|
constructor(options) {
|
|
30
32
|
this._context = {
|
|
31
33
|
systemPrompt: options.systemPrompt ?? '',
|
|
@@ -71,6 +73,21 @@ export class Agent {
|
|
|
71
73
|
setTools(tools) {
|
|
72
74
|
this._context.tools = [...tools];
|
|
73
75
|
}
|
|
76
|
+
/**
|
|
77
|
+
* 会话级 tool 面可见性(Client Tool:无 live client 则不进 LLM tool 面)。
|
|
78
|
+
* 返回 false 的 tool 对该 session 的 run 隐藏;server tool 不受影响。
|
|
79
|
+
*/
|
|
80
|
+
setToolVisibilityFilter(filter) {
|
|
81
|
+
this._toolVisibilityFilter = filter;
|
|
82
|
+
}
|
|
83
|
+
/** 本 session 的 tool 列表(含可见性过滤) */
|
|
84
|
+
toolsForSession(sessionId) {
|
|
85
|
+
const tools = this.tools;
|
|
86
|
+
const filter = this._toolVisibilityFilter;
|
|
87
|
+
if (!filter)
|
|
88
|
+
return tools;
|
|
89
|
+
return tools.filter((t) => filter(sessionId, t.name));
|
|
90
|
+
}
|
|
74
91
|
// ── 动态配置 ──
|
|
75
92
|
setModel(model) {
|
|
76
93
|
this._config = { ...this._config, model };
|
|
@@ -164,7 +181,9 @@ export class Agent {
|
|
|
164
181
|
const runScope = {
|
|
165
182
|
sessionId: options?.runScope?.sessionId ?? this._contextSessionId,
|
|
166
183
|
agentId: options?.runScope?.agentId ?? harness.agentId ?? 'default',
|
|
184
|
+
runId: options?.runScope?.runId,
|
|
167
185
|
systemPrompt: options?.runScope?.systemPrompt ?? context.systemPrompt,
|
|
186
|
+
grounding: options?.runScope?.grounding,
|
|
168
187
|
toolRuntime: options?.runScope?.toolRuntime,
|
|
169
188
|
agentRevision: options?.runScope?.agentRevision,
|
|
170
189
|
};
|
|
@@ -576,8 +576,12 @@ export async function* runAgentWithReliability(context, config, harness, signal)
|
|
|
576
576
|
}
|
|
577
577
|
return 'abort';
|
|
578
578
|
}
|
|
579
|
-
// 默认:重试 rate_limit / timeout / server(最多 3 次,避免无界重试)
|
|
580
|
-
|
|
579
|
+
// 默认:重试 rate_limit / timeout / network / server(最多 3 次,避免无界重试)
|
|
580
|
+
// network 含对端断连(undici terminated / socket hang up),重试常可自愈
|
|
581
|
+
if (classified.reason === 'rate_limit' ||
|
|
582
|
+
classified.reason === 'timeout' ||
|
|
583
|
+
classified.reason === 'network' ||
|
|
584
|
+
classified.reason === 'server') {
|
|
581
585
|
if (attempt < 3) {
|
|
582
586
|
const delayMs = classified.retryAfterMs ?? (attempt + 1) * 1000;
|
|
583
587
|
if (delayMs > 0) {
|
|
@@ -535,7 +535,8 @@ export class SessionAwareRunner {
|
|
|
535
535
|
const runContext = {
|
|
536
536
|
systemPrompt: '',
|
|
537
537
|
messages: session.messages,
|
|
538
|
-
|
|
538
|
+
// 会话级 tool 面:Client Tool 无 live client 时不进 LLM 列表
|
|
539
|
+
tools: this.agent.toolsForSession(sessionId),
|
|
539
540
|
};
|
|
540
541
|
// 播种压缩状态(E4 键 = sessionId × agentId);供 convertToLlm / 重启重建
|
|
541
542
|
this.agent.setSessionCompactState(sessionId, _agentId, readSessionCompact(session, _agentId));
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 会话标题策略 — 临时截断 / 小模型摘要触发条件
|
|
3
|
+
*
|
|
4
|
+
* 目标:历史列表一眼可认;「你好」类碎消息不浪费 LLM 调用。
|
|
5
|
+
*/
|
|
6
|
+
import type { Message, SessionMeta } from '@octopi-agent/core/types.js';
|
|
7
|
+
import type { SessionData } from './types.js';
|
|
8
|
+
/** 首条有效用户消息达到该长度才可直接摘要 */
|
|
9
|
+
export declare const TITLE_MIN_FIRST_USER_CHARS = 16;
|
|
10
|
+
/** 累计有效用户内容达到该长度(且至少 1 轮完成)可摘要 */
|
|
11
|
+
export declare const TITLE_MIN_TOTAL_USER_CHARS = 40;
|
|
12
|
+
/** 临时截断标题最大长度 */
|
|
13
|
+
export declare const TITLE_SNIPPET_MAX_CHARS = 24;
|
|
14
|
+
/** 生成标题最大长度 */
|
|
15
|
+
export declare const TITLE_MAX_CHARS = 40;
|
|
16
|
+
/**
|
|
17
|
+
* 压缩 URL / 本地路径,避免路径噪声占满 snippet 与摘要 prompt。
|
|
18
|
+
*
|
|
19
|
+
* 长 percent-encoded file:// 路径会把 24 字 snippet 截成「帮我评估一下, file:///…」,
|
|
20
|
+
* 也会把送入小模型的前 400 字预算吃光,导致摘要失败或弱标题。
|
|
21
|
+
*
|
|
22
|
+
* @param text - 原始消息文本
|
|
23
|
+
* @returns 路径替换为文件名后的文本
|
|
24
|
+
*/
|
|
25
|
+
export declare function compactTitleText(text: string): string;
|
|
26
|
+
/** 收集标题相关的用户文本(按时间序,已压缩路径) */
|
|
27
|
+
export declare function collectTitleUserTexts(messages: Message[]): string[];
|
|
28
|
+
/**
|
|
29
|
+
* 是否已具备生成摘要标题的语义信号。
|
|
30
|
+
*
|
|
31
|
+
* @param session - 会话数据(用 messages 判断)
|
|
32
|
+
* @returns 首条/最新用户消息够长,或累计用户内容够长且已有助手回复
|
|
33
|
+
*/
|
|
34
|
+
export declare function isTitleSignalReady(session: Pick<SessionData, 'messages'>): boolean;
|
|
35
|
+
/**
|
|
36
|
+
* 是否应尝试更新标题(尊重手动重命名与已生成有效 auto)。
|
|
37
|
+
*
|
|
38
|
+
* @param meta - 会话元数据
|
|
39
|
+
* @returns true = 允许写入 snippet 或重新生成 auto
|
|
40
|
+
*/
|
|
41
|
+
export declare function shouldUpdateTitle(meta: Pick<SessionMeta, 'title' | 'titleSource'>): boolean;
|
|
42
|
+
/**
|
|
43
|
+
* 是否像代码 / 标记 / 工具载荷——不适合当会话标题。
|
|
44
|
+
*
|
|
45
|
+
* 只拦「本身就是载荷」的开头,不误伤「npm 依赖冲突」这类正常主题。
|
|
46
|
+
*
|
|
47
|
+
* @param text - 候选标题或消息正文
|
|
48
|
+
* @returns true = 应跳过或视为弱标题
|
|
49
|
+
*/
|
|
50
|
+
export declare function looksLikeCodeOrMarkup(text: string): boolean;
|
|
51
|
+
/**
|
|
52
|
+
* 构建临时标题:优先首条「有语义」的用户消息,否则取最长一条。
|
|
53
|
+
*
|
|
54
|
+
* 避免「你好」占位后复杂提问也一直显示「你好」;路径已压缩为文件名。
|
|
55
|
+
*
|
|
56
|
+
* @param messages - 会话消息
|
|
57
|
+
* @returns 截断后的 snippet;无有效用户消息时返回 null
|
|
58
|
+
*/
|
|
59
|
+
export declare function buildSnippetTitle(messages: Message[]): string | null;
|
|
60
|
+
/**
|
|
61
|
+
* 规范化模型输出的标题(去引号/换行、限长)。
|
|
62
|
+
*
|
|
63
|
+
* @param raw - 模型原始输出
|
|
64
|
+
* @returns 单行短标题;空输出返回 null
|
|
65
|
+
*/
|
|
66
|
+
export declare function normalizeGeneratedTitle(raw: string): string | null;
|
|
67
|
+
/**
|
|
68
|
+
* 弱标题(寒暄/过短/无信息量)——即使已标成 auto 也允许再升级。
|
|
69
|
+
*
|
|
70
|
+
* @param title - 当前标题
|
|
71
|
+
* @returns true = 不应视为最终摘要标题
|
|
72
|
+
*/
|
|
73
|
+
export declare function isWeakTitle(title: string | undefined): boolean;
|
|
74
|
+
/**
|
|
75
|
+
* 构建摘要标题的 LLM 消息。
|
|
76
|
+
*
|
|
77
|
+
* 标题语义 = 「这段对话面向的是什么事」(主题标签),不是对话内容复述。
|
|
78
|
+
* 正文先压缩 URL/路径,避免 percent-encoded 路径吃光上下文预算。
|
|
79
|
+
*
|
|
80
|
+
* @param messages - 会话消息(跳过寒暄与命令留痕)
|
|
81
|
+
* @returns 送给 ModelProvider.chat 的 messages
|
|
82
|
+
*/
|
|
83
|
+
export declare function buildTitlePrompt(messages: Message[]): Array<{
|
|
84
|
+
role: 'system' | 'user';
|
|
85
|
+
content: string;
|
|
86
|
+
}>;
|