@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
|
@@ -1,23 +1,42 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* SqliteWisdomStore — SQLite
|
|
2
|
+
* SqliteWisdomStore — 判断范式(maxim)SQLite 存储
|
|
3
|
+
*
|
|
4
|
+
* 写入只经 admit(门控在 wisdom-gates)。无旧 content 形态兼容。
|
|
3
5
|
*
|
|
4
6
|
* @module
|
|
5
7
|
*/
|
|
6
|
-
import type {
|
|
8
|
+
import type { AdmitWisdomInput, AdmitWisdomResult, WisdomEntry, WisdomInjectPick, WisdomInjectQuery, WisdomOutcomeEvent, WisdomRetireReason, WisdomStats, WisdomStore } from '../types.js';
|
|
7
9
|
import { AgentDatabase } from './agent-db.js';
|
|
10
|
+
export interface SqliteWisdomStoreOptions {
|
|
11
|
+
/** admit 硬顶(存活面);默认 80 */
|
|
12
|
+
hardCap?: number;
|
|
13
|
+
}
|
|
8
14
|
export declare class SqliteWisdomStore implements WisdomStore {
|
|
15
|
+
readonly name = "sqlite-wisdom";
|
|
9
16
|
private db;
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
17
|
+
private readonly hardCapLimit;
|
|
18
|
+
constructor(db: AgentDatabase, options?: SqliteWisdomStoreOptions);
|
|
19
|
+
admit(input: AdmitWisdomInput, options?: {
|
|
20
|
+
allowedMemoryIds?: ReadonlySet<string>;
|
|
21
|
+
}): Promise<AdmitWisdomResult>;
|
|
22
|
+
get(id: string): Promise<WisdomEntry | null>;
|
|
23
|
+
selectForInjection(query: WisdomInjectQuery): Promise<WisdomInjectPick[]>;
|
|
24
|
+
listForGovern(filter?: {
|
|
25
|
+
includeRetired?: boolean;
|
|
26
|
+
}): Promise<WisdomEntry[]>;
|
|
27
|
+
update(id: string, patch: Partial<WisdomEntry>): Promise<void>;
|
|
28
|
+
softRetire(id: string, meta: {
|
|
29
|
+
by: string;
|
|
30
|
+
reason: WisdomRetireReason | string;
|
|
31
|
+
}): Promise<void>;
|
|
32
|
+
recordOutcomes(events: WisdomOutcomeEvent[]): Promise<void>;
|
|
33
|
+
touchApplied(ids: string[]): Promise<void>;
|
|
34
|
+
stats(): Promise<WisdomStats>;
|
|
35
|
+
/** 存活面计数(candidate/trial/active/strengthened/contested) */
|
|
36
|
+
countLive(): number;
|
|
37
|
+
private hardCap;
|
|
38
|
+
private livingCount;
|
|
39
|
+
private listMinimal;
|
|
40
|
+
private getSync;
|
|
22
41
|
private rowToEntry;
|
|
23
42
|
}
|
|
@@ -1,64 +1,277 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* SqliteWisdomStore — SQLite
|
|
2
|
+
* SqliteWisdomStore — 判断范式(maxim)SQLite 存储
|
|
3
|
+
*
|
|
4
|
+
* 写入只经 admit(门控在 wisdom-gates)。无旧 content 形态兼容。
|
|
3
5
|
*
|
|
4
6
|
* @module
|
|
5
7
|
*/
|
|
8
|
+
import { WISDOM_STATUSES } from '../types.js';
|
|
9
|
+
import { evaluateWisdomGate, resolveInitialWisdomStatus, } from '../wisdom-gates.js';
|
|
10
|
+
import { foldOutcomeEvents, planConfidenceUpdate } from '../wisdom-policy.js';
|
|
11
|
+
import { pickForInjection } from '../wisdom-formation.js';
|
|
6
12
|
import { AgentDatabase } from './agent-db.js';
|
|
7
13
|
export class SqliteWisdomStore {
|
|
14
|
+
name = 'sqlite-wisdom';
|
|
8
15
|
db;
|
|
9
|
-
|
|
16
|
+
hardCapLimit;
|
|
17
|
+
constructor(db, options) {
|
|
10
18
|
this.db = db;
|
|
19
|
+
this.hardCapLimit = options?.hardCap ?? 80;
|
|
11
20
|
}
|
|
12
|
-
async
|
|
21
|
+
async admit(input, options) {
|
|
22
|
+
const existing = this.listMinimal();
|
|
23
|
+
const gate = evaluateWisdomGate(input, {
|
|
24
|
+
existing,
|
|
25
|
+
allowedMemoryIds: options?.allowedMemoryIds,
|
|
26
|
+
capacityAdmitStop: this.livingCount() >= this.hardCap(),
|
|
27
|
+
});
|
|
28
|
+
if (!gate.ok) {
|
|
29
|
+
return { action: 'rejected', reason: gate.reason, message: gate.message };
|
|
30
|
+
}
|
|
31
|
+
const now = Date.now();
|
|
32
|
+
const status = resolveInitialWisdomStatus(input);
|
|
13
33
|
const id = AgentDatabase.generateId('wis');
|
|
34
|
+
if (input.supersedesId) {
|
|
35
|
+
const prev = this.getSync(input.supersedesId);
|
|
36
|
+
if (!prev) {
|
|
37
|
+
return {
|
|
38
|
+
action: 'rejected',
|
|
39
|
+
reason: 'supersedes_target_missing',
|
|
40
|
+
message: `supersedesId=${input.supersedesId}`,
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
this.db.raw
|
|
44
|
+
.prepare(`UPDATE wisdom SET status = 'superseded', superseded_by = ?, updated_at = ? WHERE id = ?`)
|
|
45
|
+
.run(id, now, prev.id);
|
|
46
|
+
}
|
|
47
|
+
this.db.raw
|
|
48
|
+
.prepare(`INSERT INTO wisdom (
|
|
49
|
+
id, statement, rationale, scenario_json, effect_json, status, confidence, priority,
|
|
50
|
+
derived_from_json, exceptions_json, counterevidence_json, outcomes_json,
|
|
51
|
+
origin, kind, superseded_by, created_at, updated_at
|
|
52
|
+
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`)
|
|
53
|
+
.run(id, input.statement.trim(), input.rationale?.trim() || null, JSON.stringify(input.scenario), JSON.stringify(input.effect), status, clamp01(input.confidence ?? defaultConfidence(status, input.origin ?? 'distilled')), input.priority ?? defaultPriority(status, input.origin ?? 'distilled'), JSON.stringify(input.derivedFrom), JSON.stringify(input.exceptions ?? []), JSON.stringify([]), JSON.stringify(emptyOutcomes()), input.origin ?? 'distilled', input.kind, null, now, now);
|
|
54
|
+
return {
|
|
55
|
+
action: input.supersedesId ? 'superseded' : 'created',
|
|
56
|
+
id,
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
async get(id) {
|
|
60
|
+
const row = this.db.raw.prepare(`SELECT * FROM wisdom WHERE id = ?`).get(id);
|
|
61
|
+
return row ? this.rowToEntry(row) : null;
|
|
62
|
+
}
|
|
63
|
+
async selectForInjection(query) {
|
|
64
|
+
const rows = this.db.raw
|
|
65
|
+
.prepare(`SELECT * FROM wisdom WHERE status IN ('trial','active','strengthened') ORDER BY priority DESC, confidence DESC`)
|
|
66
|
+
.all();
|
|
67
|
+
const entries = rows.map((r) => this.rowToEntry(r));
|
|
68
|
+
return pickForInjection(entries, query.text ?? '', {
|
|
69
|
+
coreMaxItems: query.coreMaxItems,
|
|
70
|
+
scenarioMaxItems: query.scenarioMaxItems,
|
|
71
|
+
includeTrial: query.includeTrial,
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
async listForGovern(filter) {
|
|
75
|
+
const includeRetired = filter?.includeRetired ?? false;
|
|
76
|
+
const rows = (includeRetired
|
|
77
|
+
? this.db.raw.prepare(`SELECT * FROM wisdom ORDER BY updated_at DESC`).all()
|
|
78
|
+
: this.db.raw
|
|
79
|
+
.prepare(`SELECT * FROM wisdom WHERE status NOT IN ('retired') ORDER BY updated_at DESC`)
|
|
80
|
+
.all());
|
|
81
|
+
return rows.map((r) => this.rowToEntry(r));
|
|
82
|
+
}
|
|
83
|
+
async update(id, patch) {
|
|
84
|
+
const current = this.getSync(id);
|
|
85
|
+
if (!current)
|
|
86
|
+
throw new Error(`wisdom_update_not_found: ${id}`);
|
|
87
|
+
const merged = {
|
|
88
|
+
...current,
|
|
89
|
+
...patch,
|
|
90
|
+
id: current.id,
|
|
91
|
+
createdAt: current.createdAt,
|
|
92
|
+
updatedAt: Date.now(),
|
|
93
|
+
outcomes: { ...current.outcomes, ...(patch.outcomes ?? {}) },
|
|
94
|
+
};
|
|
95
|
+
this.db.raw
|
|
96
|
+
.prepare(`UPDATE wisdom SET
|
|
97
|
+
statement = ?, rationale = ?, scenario_json = ?, effect_json = ?,
|
|
98
|
+
status = ?, confidence = ?, priority = ?, derived_from_json = ?,
|
|
99
|
+
exceptions_json = ?, counterevidence_json = ?, outcomes_json = ?,
|
|
100
|
+
origin = ?, kind = ?, superseded_by = ?, updated_at = ?
|
|
101
|
+
WHERE id = ?`)
|
|
102
|
+
.run(merged.statement, merged.rationale ?? null, JSON.stringify(merged.scenario), JSON.stringify(merged.effect), merged.status, clamp01(merged.confidence), merged.priority, JSON.stringify(merged.derivedFrom), JSON.stringify(merged.exceptions ?? []), JSON.stringify(merged.counterevidence ?? []), JSON.stringify(merged.outcomes), merged.origin, merged.kind, merged.supersededBy ?? null, merged.updatedAt, id);
|
|
103
|
+
}
|
|
104
|
+
async softRetire(id, meta) {
|
|
105
|
+
const current = this.getSync(id);
|
|
106
|
+
if (!current)
|
|
107
|
+
return;
|
|
108
|
+
if (current.status === 'superseded')
|
|
109
|
+
return;
|
|
110
|
+
await this.update(id, {
|
|
111
|
+
status: 'retired',
|
|
112
|
+
exceptions: [...(current.exceptions ?? []), `retired:${meta.reason}:${meta.by}`],
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
async recordOutcomes(events) {
|
|
116
|
+
if (events.length === 0)
|
|
117
|
+
return;
|
|
118
|
+
const byId = new Map();
|
|
119
|
+
for (const e of events) {
|
|
120
|
+
const list = byId.get(e.wisdomId) ?? [];
|
|
121
|
+
list.push(e);
|
|
122
|
+
byId.set(e.wisdomId, list);
|
|
123
|
+
}
|
|
124
|
+
for (const [id, evs] of byId) {
|
|
125
|
+
const current = this.getSync(id);
|
|
126
|
+
if (!current)
|
|
127
|
+
continue;
|
|
128
|
+
const folded = foldOutcomeEvents(current, evs);
|
|
129
|
+
let next = { ...current, ...folded, updatedAt: Date.now() };
|
|
130
|
+
const plan = planConfidenceUpdate(next);
|
|
131
|
+
if (plan) {
|
|
132
|
+
next = {
|
|
133
|
+
...next,
|
|
134
|
+
confidence: plan.confidence,
|
|
135
|
+
status: plan.status,
|
|
136
|
+
outcomes: {
|
|
137
|
+
...next.outcomes,
|
|
138
|
+
evalAssisted: plan.evalAssisted,
|
|
139
|
+
evalContested: plan.evalContested,
|
|
140
|
+
},
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
const counter = evs.filter((e) => e.signal === 'contested' && (e.ref || e.note));
|
|
144
|
+
if (counter.length) {
|
|
145
|
+
const ce = [...(next.counterevidence ?? [])];
|
|
146
|
+
for (const c of counter) {
|
|
147
|
+
ce.push({
|
|
148
|
+
ref: c.ref ?? c.wisdomId,
|
|
149
|
+
note: c.note ?? 'contested',
|
|
150
|
+
at: c.at ?? Date.now(),
|
|
151
|
+
weight: 1,
|
|
152
|
+
});
|
|
153
|
+
}
|
|
154
|
+
next = { ...next, counterevidence: ce };
|
|
155
|
+
}
|
|
156
|
+
await this.update(id, next);
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
async touchApplied(ids) {
|
|
160
|
+
if (ids.length === 0)
|
|
161
|
+
return;
|
|
14
162
|
const now = Date.now();
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
163
|
+
for (const id of ids) {
|
|
164
|
+
const cur = this.getSync(id);
|
|
165
|
+
if (!cur)
|
|
166
|
+
continue;
|
|
167
|
+
const outcomes = {
|
|
168
|
+
...cur.outcomes,
|
|
169
|
+
applied: cur.outcomes.applied + 1,
|
|
170
|
+
lastAppliedAt: now,
|
|
171
|
+
};
|
|
172
|
+
this.db.raw
|
|
173
|
+
.prepare(`UPDATE wisdom SET outcomes_json = ?, updated_at = ? WHERE id = ?`)
|
|
174
|
+
.run(JSON.stringify(outcomes), now, id);
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
async stats() {
|
|
178
|
+
const rows = this.db.raw.prepare(`SELECT * FROM wisdom`).all();
|
|
179
|
+
const byStatus = Object.fromEntries(WISDOM_STATUSES.map((s) => [s, 0]));
|
|
180
|
+
let confSum = 0;
|
|
181
|
+
let prioSum = 0;
|
|
182
|
+
let applied = 0;
|
|
183
|
+
let contested = 0;
|
|
184
|
+
for (const r of rows) {
|
|
185
|
+
const st = r.status ?? 'active';
|
|
186
|
+
byStatus[st] = (byStatus[st] ?? 0) + 1;
|
|
187
|
+
confSum += r.confidence;
|
|
188
|
+
prioSum += r.priority;
|
|
189
|
+
const o = safeParse(r.outcomes_json, emptyOutcomes());
|
|
190
|
+
applied += o.applied;
|
|
191
|
+
contested += o.contested;
|
|
192
|
+
}
|
|
193
|
+
const n = rows.length || 1;
|
|
194
|
+
return {
|
|
195
|
+
total: rows.length,
|
|
196
|
+
byStatus,
|
|
197
|
+
avgConfidence: confSum / n,
|
|
198
|
+
avgPriority: prioSum / n,
|
|
199
|
+
totalApplied: applied,
|
|
200
|
+
totalContested: contested,
|
|
201
|
+
};
|
|
202
|
+
}
|
|
203
|
+
/** 存活面计数(candidate/trial/active/strengthened/contested) */
|
|
204
|
+
countLive() {
|
|
205
|
+
return this.livingCount();
|
|
206
|
+
}
|
|
207
|
+
hardCap() {
|
|
208
|
+
return this.hardCapLimit;
|
|
209
|
+
}
|
|
210
|
+
livingCount() {
|
|
211
|
+
const row = this.db.raw
|
|
212
|
+
.prepare(`SELECT COUNT(*) as c FROM wisdom WHERE status IN ('candidate','trial','active','strengthened','contested')`)
|
|
213
|
+
.get();
|
|
214
|
+
return row.c;
|
|
215
|
+
}
|
|
216
|
+
listMinimal() {
|
|
217
|
+
const rows = this.db.raw
|
|
218
|
+
.prepare(`SELECT id, statement, status, kind FROM wisdom WHERE status NOT IN ('retired')`)
|
|
219
|
+
.all();
|
|
220
|
+
return rows;
|
|
221
|
+
}
|
|
222
|
+
getSync(id) {
|
|
223
|
+
const row = this.db.raw.prepare(`SELECT * FROM wisdom WHERE id = ?`).get(id);
|
|
224
|
+
return row ? this.rowToEntry(row) : null;
|
|
53
225
|
}
|
|
54
226
|
rowToEntry(row) {
|
|
55
227
|
return {
|
|
56
228
|
id: row.id,
|
|
57
|
-
|
|
58
|
-
|
|
229
|
+
statement: row.statement,
|
|
230
|
+
rationale: row.rationale ?? undefined,
|
|
231
|
+
scenario: safeParse(row.scenario_json, { problemTypes: [] }),
|
|
232
|
+
effect: safeParse(row.effect_json, {}),
|
|
233
|
+
status: (WISDOM_STATUSES.includes(row.status)
|
|
234
|
+
? row.status
|
|
235
|
+
: 'active'),
|
|
236
|
+
confidence: row.confidence,
|
|
59
237
|
priority: row.priority,
|
|
238
|
+
derivedFrom: safeParse(row.derived_from_json, { memoryIds: [] }),
|
|
239
|
+
exceptions: safeParse(row.exceptions_json, []),
|
|
240
|
+
counterevidence: safeParse(row.counterevidence_json, []),
|
|
241
|
+
outcomes: safeParse(row.outcomes_json, emptyOutcomes()),
|
|
242
|
+
origin: row.origin,
|
|
243
|
+
kind: row.kind,
|
|
244
|
+
supersededBy: row.superseded_by ?? undefined,
|
|
60
245
|
createdAt: row.created_at,
|
|
61
|
-
|
|
246
|
+
updatedAt: row.updated_at,
|
|
62
247
|
};
|
|
63
248
|
}
|
|
64
249
|
}
|
|
250
|
+
function emptyOutcomes() {
|
|
251
|
+
return { applied: 0, cited: 0, assisted: 0, contested: 0, evalAssisted: 0, evalContested: 0 };
|
|
252
|
+
}
|
|
253
|
+
function clamp01(n) {
|
|
254
|
+
return Math.min(1, Math.max(0, n));
|
|
255
|
+
}
|
|
256
|
+
function defaultConfidence(status, origin) {
|
|
257
|
+
if (origin === 'factory' || status === 'active')
|
|
258
|
+
return 0.7;
|
|
259
|
+
return 0.5;
|
|
260
|
+
}
|
|
261
|
+
function defaultPriority(status, origin) {
|
|
262
|
+
if (origin === 'factory')
|
|
263
|
+
return 80;
|
|
264
|
+
if (status === 'active')
|
|
265
|
+
return 50;
|
|
266
|
+
return 30;
|
|
267
|
+
}
|
|
268
|
+
function safeParse(raw, fallback) {
|
|
269
|
+
if (!raw)
|
|
270
|
+
return fallback;
|
|
271
|
+
try {
|
|
272
|
+
return JSON.parse(raw);
|
|
273
|
+
}
|
|
274
|
+
catch {
|
|
275
|
+
return fallback;
|
|
276
|
+
}
|
|
277
|
+
}
|