octopi-agent 0.127.4 → 0.130.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/cli/daemon.js +2 -1
- package/dist/cli/doctor/archive-residue.d.ts +77 -0
- package/dist/cli/doctor/archive-residue.js +418 -0
- package/dist/cli/doctor/index.d.ts +2 -1
- package/dist/cli/doctor/index.js +8 -1
- package/dist/config-schema/index.d.ts +20 -0
- package/dist/config.js +1 -0
- package/octopi.example.json +11 -0
- package/octopi.schema.json +36 -0
- package/package.json +5 -5
package/dist/cli/daemon.js
CHANGED
|
@@ -10,6 +10,7 @@ import { setupWizardUrl, readSetupToken } from './helpers.js';
|
|
|
10
10
|
import { loadConfig, toGatewayConfig, resolveConfigFilePath } from '../config.js';
|
|
11
11
|
import { resolveListenConfig } from './listen-config.js';
|
|
12
12
|
import { assembleGlobalTools } from '@octopi-agent/engine/integration/tools/assemble.js';
|
|
13
|
+
import { createArchiveHistorySource } from '@octopi-agent/engine/integration/storage/archives/history-source.js';
|
|
13
14
|
import { createProviderFromConfig, createWireDumpSinkFromConfig } from '@octopi-agent/engine/config.js';
|
|
14
15
|
import { createWsIdentityHooks, agentTenantReader } from '../ws-identity-hooks.js';
|
|
15
16
|
import { startLoopWatchdog } from '@octopi-agent/engine/integration/observability/loop-watchdog.js';
|
|
@@ -979,7 +980,7 @@ async function startGatewayBlocking(configPath, args) {
|
|
|
979
980
|
sessionHistory: {
|
|
980
981
|
store: gatewayStoreForHistory,
|
|
981
982
|
sessionAcl: new SessionAclService(gatewayConfig.sessionAcl),
|
|
982
|
-
|
|
983
|
+
archiveSource: createArchiveHistorySource({ archivesDir: joinPath(home, 'archives') }),
|
|
983
984
|
historyScope: 'participated',
|
|
984
985
|
index: sessionIndex,
|
|
985
986
|
// E6 agent.max:与 Runner agentMaxSessionRights 同源(宿主可按 agent 收紧)
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* doctor 归档/删除残留扫描(P3,arch/session-archive-recycle.md §8 存量孤儿)
|
|
3
|
+
*
|
|
4
|
+
* 判定口径:会话「在世」= OCTOPI_HOME/sessions/ 下存在其 jsonl/state 文件或元索引条目
|
|
5
|
+
* (名字集按 id 的全部已知规范化形态匹配,偏安全方向:拿不准的不报孤儿,绝不误删在世会话)。
|
|
6
|
+
* 不在世会话的 wire dump 目录 / 空会话目录(含 derived/ 残留)/ knowledge session 源与
|
|
7
|
+
* visibility·hits 行 = 残留;--fix 显式清除。旧月度 gz(迁移保留件)与 archives/.staging
|
|
8
|
+
* 只报告不动手(唯一正文可能只在那里 / reconcile 有收尾语义)。本地确定性,不调用 LLM。
|
|
9
|
+
*
|
|
10
|
+
* @module
|
|
11
|
+
*/
|
|
12
|
+
import type { DataFixResult } from './data.js';
|
|
13
|
+
/** 在世会话名册(id + 全部规范化形态;孤儿判定的权威) */
|
|
14
|
+
export interface LiveSessionRoster {
|
|
15
|
+
ids: Set<string>;
|
|
16
|
+
names: Set<string>;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* 收集在世会话名册:sessions.json 元索引 + 磁盘 jsonl/state 文件名(双源取并集,偏安全)
|
|
20
|
+
*
|
|
21
|
+
* @param home - OCTOPI_HOME
|
|
22
|
+
* @returns id 集合与名字集合
|
|
23
|
+
*/
|
|
24
|
+
export declare function collectLiveSessionRoster(home: string): LiveSessionRoster;
|
|
25
|
+
/** 残留清单(detect 与 fix 共用的单源判定) */
|
|
26
|
+
export interface ResidueInventory {
|
|
27
|
+
dumpDirs: Array<{
|
|
28
|
+
path: string;
|
|
29
|
+
bytes: number;
|
|
30
|
+
}>;
|
|
31
|
+
unscopedDump: {
|
|
32
|
+
path: string;
|
|
33
|
+
bytes: number;
|
|
34
|
+
} | null;
|
|
35
|
+
sessionDirs: Array<{
|
|
36
|
+
path: string;
|
|
37
|
+
bytes: number;
|
|
38
|
+
files: number;
|
|
39
|
+
}>;
|
|
40
|
+
knowledgeSources: Array<{
|
|
41
|
+
id: string;
|
|
42
|
+
displayName: string;
|
|
43
|
+
}>;
|
|
44
|
+
visibilityRows: Array<{
|
|
45
|
+
sessionId: string;
|
|
46
|
+
count: number;
|
|
47
|
+
}>;
|
|
48
|
+
hitRows: Array<{
|
|
49
|
+
sessionId: string;
|
|
50
|
+
count: number;
|
|
51
|
+
}>;
|
|
52
|
+
legacyMonthlyFiles: string[];
|
|
53
|
+
stagingEntries: string[];
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* 扫描全部归档/删除残留面(§8 存量孤儿清单)
|
|
57
|
+
*
|
|
58
|
+
* @param home - OCTOPI_HOME(默认运行时 home)
|
|
59
|
+
* @returns 残留清单
|
|
60
|
+
*/
|
|
61
|
+
export declare function collectArchiveResidue(home?: string): Promise<ResidueInventory>;
|
|
62
|
+
/**
|
|
63
|
+
* 归档/删除残留扫描 → doctor findings(ARC001–ARC007)
|
|
64
|
+
*
|
|
65
|
+
* @returns findings(不修改磁盘)
|
|
66
|
+
*/
|
|
67
|
+
export declare function detectArchiveResidue(): Promise<DataFixResult['findings']>;
|
|
68
|
+
/**
|
|
69
|
+
* 应用残留清除(--fix):删孤儿 dump/会话目录、注销孤儿 session 源、删 visibility/hits 行。
|
|
70
|
+
* ARC006/ARC007 永不动手(唯一正文可能只在源文件 / reconcile 有收尾语义)。
|
|
71
|
+
*
|
|
72
|
+
* @param options - dryRun 时只描述将做什么
|
|
73
|
+
* @returns findings + notes
|
|
74
|
+
*/
|
|
75
|
+
export declare function applyArchiveResidueFixes(options?: {
|
|
76
|
+
dryRun?: boolean;
|
|
77
|
+
}): Promise<DataFixResult>;
|
|
@@ -0,0 +1,418 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* doctor 归档/删除残留扫描(P3,arch/session-archive-recycle.md §8 存量孤儿)
|
|
3
|
+
*
|
|
4
|
+
* 判定口径:会话「在世」= OCTOPI_HOME/sessions/ 下存在其 jsonl/state 文件或元索引条目
|
|
5
|
+
* (名字集按 id 的全部已知规范化形态匹配,偏安全方向:拿不准的不报孤儿,绝不误删在世会话)。
|
|
6
|
+
* 不在世会话的 wire dump 目录 / 空会话目录(含 derived/ 残留)/ knowledge session 源与
|
|
7
|
+
* visibility·hits 行 = 残留;--fix 显式清除。旧月度 gz(迁移保留件)与 archives/.staging
|
|
8
|
+
* 只报告不动手(唯一正文可能只在那里 / reconcile 有收尾语义)。本地确定性,不调用 LLM。
|
|
9
|
+
*
|
|
10
|
+
* @module
|
|
11
|
+
*/
|
|
12
|
+
import { existsSync, readdirSync, readFileSync, rmSync, statSync } from 'node:fs';
|
|
13
|
+
import { join } from 'node:path';
|
|
14
|
+
import { getOctopiHome } from '@octopi-agent/engine/paths.js';
|
|
15
|
+
import { toSessionFileName } from '@octopi-agent/core/session-filename.js';
|
|
16
|
+
import { sanitizeSessionDirName } from '@octopi-agent/engine/integration/providers/wire-dump.js';
|
|
17
|
+
import { knowledgeDbPath, removeKnowledgeSource } from './data.js';
|
|
18
|
+
function dirStats(path) {
|
|
19
|
+
const out = { bytes: 0, files: 0, unreadable: 0 };
|
|
20
|
+
let entries;
|
|
21
|
+
try {
|
|
22
|
+
entries = readdirSync(path);
|
|
23
|
+
}
|
|
24
|
+
catch {
|
|
25
|
+
out.unreadable += 1;
|
|
26
|
+
return out;
|
|
27
|
+
}
|
|
28
|
+
for (const name of entries) {
|
|
29
|
+
const child = join(path, name);
|
|
30
|
+
try {
|
|
31
|
+
const st = statSync(child);
|
|
32
|
+
if (st.isDirectory()) {
|
|
33
|
+
const sub = dirStats(child);
|
|
34
|
+
out.bytes += sub.bytes;
|
|
35
|
+
out.files += sub.files;
|
|
36
|
+
out.unreadable += sub.unreadable;
|
|
37
|
+
}
|
|
38
|
+
else {
|
|
39
|
+
out.bytes += st.size;
|
|
40
|
+
out.files += 1;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
catch {
|
|
44
|
+
out.unreadable += 1;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
return out;
|
|
48
|
+
}
|
|
49
|
+
function formatBytes(n) {
|
|
50
|
+
if (n >= 1024 * 1024)
|
|
51
|
+
return `${(n / 1024 / 1024).toFixed(1)}MB`;
|
|
52
|
+
if (n >= 1024)
|
|
53
|
+
return `${(n / 1024).toFixed(1)}KB`;
|
|
54
|
+
return `${n}B`;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* 收集在世会话名册:sessions.json 元索引 + 磁盘 jsonl/state 文件名(双源取并集,偏安全)
|
|
58
|
+
*
|
|
59
|
+
* @param home - OCTOPI_HOME
|
|
60
|
+
* @returns id 集合与名字集合
|
|
61
|
+
*/
|
|
62
|
+
export function collectLiveSessionRoster(home) {
|
|
63
|
+
const ids = new Set();
|
|
64
|
+
const names = new Set();
|
|
65
|
+
const add = (id) => {
|
|
66
|
+
ids.add(id);
|
|
67
|
+
names.add(id);
|
|
68
|
+
names.add(toSessionFileName(id));
|
|
69
|
+
names.add(sanitizeSessionDirName(id));
|
|
70
|
+
};
|
|
71
|
+
const dir = join(home, 'sessions');
|
|
72
|
+
try {
|
|
73
|
+
for (const name of readdirSync(dir)) {
|
|
74
|
+
if (name === 'sessions.json') {
|
|
75
|
+
try {
|
|
76
|
+
const meta = JSON.parse(readFileSync(join(dir, name), 'utf-8'));
|
|
77
|
+
for (const id of Object.keys(meta))
|
|
78
|
+
add(id);
|
|
79
|
+
}
|
|
80
|
+
catch {
|
|
81
|
+
// 元索引不可解析:仍以文件名源兜底(doctor 不因单源损坏失明)
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
else if (name.endsWith('.jsonl')) {
|
|
85
|
+
add(name.slice(0, -'.jsonl'.length));
|
|
86
|
+
}
|
|
87
|
+
else if (name.endsWith('.state.json')) {
|
|
88
|
+
add(name.slice(0, -'.state.json'.length));
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
catch {
|
|
93
|
+
// sessions/ 不存在:名册为空,所有残留面按空集判定
|
|
94
|
+
}
|
|
95
|
+
return { ids, names };
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* 扫描全部归档/删除残留面(§8 存量孤儿清单)
|
|
99
|
+
*
|
|
100
|
+
* @param home - OCTOPI_HOME(默认运行时 home)
|
|
101
|
+
* @returns 残留清单
|
|
102
|
+
*/
|
|
103
|
+
export async function collectArchiveResidue(home = getOctopiHome()) {
|
|
104
|
+
const live = collectLiveSessionRoster(home);
|
|
105
|
+
const out = {
|
|
106
|
+
dumpDirs: [],
|
|
107
|
+
unscopedDump: null,
|
|
108
|
+
sessionDirs: [],
|
|
109
|
+
knowledgeSources: [],
|
|
110
|
+
visibilityRows: [],
|
|
111
|
+
hitRows: [],
|
|
112
|
+
legacyMonthlyFiles: [],
|
|
113
|
+
stagingEntries: [],
|
|
114
|
+
};
|
|
115
|
+
// A. wire dump 目录(隐私面最大的残留;缺省清除后不该有孤儿)
|
|
116
|
+
const dumpsDir = join(home, 'logs', 'llm-requests');
|
|
117
|
+
try {
|
|
118
|
+
for (const name of readdirSync(dumpsDir)) {
|
|
119
|
+
const path = join(dumpsDir, name);
|
|
120
|
+
let st;
|
|
121
|
+
try {
|
|
122
|
+
st = statSync(path);
|
|
123
|
+
}
|
|
124
|
+
catch {
|
|
125
|
+
continue;
|
|
126
|
+
}
|
|
127
|
+
if (!st.isDirectory())
|
|
128
|
+
continue;
|
|
129
|
+
const stats = dirStats(path);
|
|
130
|
+
if (name === '_unscoped') {
|
|
131
|
+
out.unscopedDump = { path, bytes: stats.bytes };
|
|
132
|
+
}
|
|
133
|
+
else if (!live.names.has(name)) {
|
|
134
|
+
out.dumpDirs.push({ path, bytes: stats.bytes });
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
catch {
|
|
139
|
+
// 目录不存在 = 无残留
|
|
140
|
+
}
|
|
141
|
+
// B. sessions/ 孤儿子目录(无 jsonl/state 支撑;含 derived/ 等残留内容)
|
|
142
|
+
const sessionsDir = join(home, 'sessions');
|
|
143
|
+
try {
|
|
144
|
+
for (const name of readdirSync(sessionsDir)) {
|
|
145
|
+
const path = join(sessionsDir, name);
|
|
146
|
+
let st;
|
|
147
|
+
try {
|
|
148
|
+
st = statSync(path);
|
|
149
|
+
}
|
|
150
|
+
catch {
|
|
151
|
+
continue;
|
|
152
|
+
}
|
|
153
|
+
if (!st.isDirectory())
|
|
154
|
+
continue;
|
|
155
|
+
if (live.names.has(name))
|
|
156
|
+
continue;
|
|
157
|
+
const stats = dirStats(path);
|
|
158
|
+
out.sessionDirs.push({ path, bytes: stats.bytes, files: stats.files });
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
catch {
|
|
162
|
+
// 目录不存在 = 无残留
|
|
163
|
+
}
|
|
164
|
+
// C. knowledge:失去会话的 session 源与 visibility/hits 行
|
|
165
|
+
const dbPath = knowledgeDbPath();
|
|
166
|
+
if (existsSync(dbPath)) {
|
|
167
|
+
try {
|
|
168
|
+
const { KnowledgeDatabase } = await import('@octopi-agent/engine/harness/knowledge/db.js');
|
|
169
|
+
const db = await KnowledgeDatabase.create({ dbPath });
|
|
170
|
+
try {
|
|
171
|
+
const sources = db.raw
|
|
172
|
+
.prepare("SELECT id, display_name, scope_key FROM knowledge_sources WHERE scope_level = 'session'")
|
|
173
|
+
.all();
|
|
174
|
+
for (const s of sources) {
|
|
175
|
+
if (!live.ids.has(s.scope_key)) {
|
|
176
|
+
out.knowledgeSources.push({ id: s.id, displayName: s.display_name || s.id });
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
const visSessions = db.raw
|
|
180
|
+
.prepare('SELECT DISTINCT local_session_id AS sid FROM knowledge_session_visibility')
|
|
181
|
+
.all();
|
|
182
|
+
for (const { sid } of visSessions) {
|
|
183
|
+
if (!sid || live.ids.has(sid))
|
|
184
|
+
continue;
|
|
185
|
+
const row = db.raw
|
|
186
|
+
.prepare('SELECT COUNT(*) AS n FROM knowledge_session_visibility WHERE local_session_id = ?')
|
|
187
|
+
.get(sid);
|
|
188
|
+
if (row.n > 0)
|
|
189
|
+
out.visibilityRows.push({ sessionId: sid, count: row.n });
|
|
190
|
+
}
|
|
191
|
+
const hitSessions = db.raw
|
|
192
|
+
.prepare('SELECT DISTINCT session_id AS sid FROM knowledge_hits')
|
|
193
|
+
.all();
|
|
194
|
+
for (const { sid } of hitSessions) {
|
|
195
|
+
if (!sid || live.ids.has(sid))
|
|
196
|
+
continue;
|
|
197
|
+
const row = db.raw
|
|
198
|
+
.prepare('SELECT COUNT(*) AS n FROM knowledge_hits WHERE session_id = ?')
|
|
199
|
+
.get(sid);
|
|
200
|
+
if (row.n > 0)
|
|
201
|
+
out.hitRows.push({ sessionId: sid, count: row.n });
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
finally {
|
|
205
|
+
db.close();
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
catch {
|
|
209
|
+
// knowledge.db 不可打开:KN 系列已覆盖库健康;这里不重复报、不失明其它残留面
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
// D. 旧月度冷备(迁移保留件:坏行/撞 id/封存失败)——只报告,唯一正文可能只在源文件
|
|
213
|
+
const archivesDir = join(home, 'archives');
|
|
214
|
+
try {
|
|
215
|
+
for (const name of readdirSync(archivesDir)) {
|
|
216
|
+
if (name.endsWith('.sessions.jsonl.gz'))
|
|
217
|
+
out.legacyMonthlyFiles.push(join(archivesDir, name));
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
catch {
|
|
221
|
+
// 无 archives/ = 无残留
|
|
222
|
+
}
|
|
223
|
+
// E. archives/.staging 尾巴(启动 reconcile 有收尾语义;只提示)
|
|
224
|
+
try {
|
|
225
|
+
out.stagingEntries = readdirSync(join(archivesDir, '.staging')).map((n) => join(archivesDir, '.staging', n));
|
|
226
|
+
}
|
|
227
|
+
catch {
|
|
228
|
+
// 无 staging = 无残留
|
|
229
|
+
}
|
|
230
|
+
return out;
|
|
231
|
+
}
|
|
232
|
+
/**
|
|
233
|
+
* 归档/删除残留扫描 → doctor findings(ARC001–ARC007)
|
|
234
|
+
*
|
|
235
|
+
* @returns findings(不修改磁盘)
|
|
236
|
+
*/
|
|
237
|
+
export async function detectArchiveResidue() {
|
|
238
|
+
const r = await collectArchiveResidue();
|
|
239
|
+
const findings = [];
|
|
240
|
+
const totalDumpBytes = r.dumpDirs.reduce((sum, d) => sum + d.bytes, 0);
|
|
241
|
+
if (r.dumpDirs.length > 0) {
|
|
242
|
+
findings.push({
|
|
243
|
+
id: 'ARC001',
|
|
244
|
+
domain: 'data',
|
|
245
|
+
severity: 'warn',
|
|
246
|
+
message: `orphan wire-dump dirs in logs/llm-requests (no live session): ${r.dumpDirs.length} dir(s), ${formatBytes(totalDumpBytes)}`,
|
|
247
|
+
hint: 'dumps hold full request bodies incl. system prompt — safe to remove once the sessions are truly gone; --fix deletes them',
|
|
248
|
+
fixable: true,
|
|
249
|
+
group: 'data',
|
|
250
|
+
});
|
|
251
|
+
}
|
|
252
|
+
if (r.unscopedDump) {
|
|
253
|
+
findings.push({
|
|
254
|
+
id: 'ARC002',
|
|
255
|
+
domain: 'data',
|
|
256
|
+
severity: 'warn',
|
|
257
|
+
message: `unscoped dump dir logs/llm-requests/_unscoped: ${formatBytes(r.unscopedDump.bytes)}`,
|
|
258
|
+
hint: 'historical dumps that never got a session scope; --fix deletes the dir',
|
|
259
|
+
fixable: true,
|
|
260
|
+
group: 'data',
|
|
261
|
+
});
|
|
262
|
+
}
|
|
263
|
+
if (r.sessionDirs.length > 0) {
|
|
264
|
+
const total = r.sessionDirs.reduce((sum, d) => sum + d.bytes, 0);
|
|
265
|
+
const withFiles = r.sessionDirs.filter((d) => d.files > 0).length;
|
|
266
|
+
findings.push({
|
|
267
|
+
id: 'ARC003',
|
|
268
|
+
domain: 'data',
|
|
269
|
+
severity: 'warn',
|
|
270
|
+
message: `orphan session dirs under sessions/ (no jsonl/state): ${r.sessionDirs.length} dir(s), ${formatBytes(total)}${withFiles > 0 ? `; ${withFiles} still hold residue (attachments/derived/…)` : ''}`,
|
|
271
|
+
hint: '--fix deletes them; content is unreachable without its session',
|
|
272
|
+
fixable: true,
|
|
273
|
+
group: 'data',
|
|
274
|
+
});
|
|
275
|
+
}
|
|
276
|
+
if (r.knowledgeSources.length > 0) {
|
|
277
|
+
findings.push({
|
|
278
|
+
id: 'ARC004',
|
|
279
|
+
domain: 'data',
|
|
280
|
+
severity: 'warn',
|
|
281
|
+
message: `session-scoped knowledge sources without a live session: ${r.knowledgeSources.length} (${r.knowledgeSources.map((s) => s.displayName).slice(0, 3).join(', ')}${r.knowledgeSources.length > 3 ? ', …' : ''})`,
|
|
282
|
+
hint: 'seal/restore re-registers sources for sealed sessions; these lost their session entirely — --fix unregisters them',
|
|
283
|
+
fixable: true,
|
|
284
|
+
group: 'data',
|
|
285
|
+
});
|
|
286
|
+
}
|
|
287
|
+
const visTotal = r.visibilityRows.reduce((sum, v) => sum + v.count, 0);
|
|
288
|
+
const hitTotal = r.hitRows.reduce((sum, v) => sum + v.count, 0);
|
|
289
|
+
if (visTotal > 0 || hitTotal > 0) {
|
|
290
|
+
findings.push({
|
|
291
|
+
id: 'ARC005',
|
|
292
|
+
domain: 'data',
|
|
293
|
+
severity: 'warn',
|
|
294
|
+
message: `knowledge rows for gone sessions: session_visibility=${visTotal}, hits=${hitTotal}`,
|
|
295
|
+
hint: '--fix deletes the rows',
|
|
296
|
+
fixable: true,
|
|
297
|
+
group: 'data',
|
|
298
|
+
});
|
|
299
|
+
}
|
|
300
|
+
if (r.legacyMonthlyFiles.length > 0) {
|
|
301
|
+
findings.push({
|
|
302
|
+
id: 'ARC006',
|
|
303
|
+
domain: 'data',
|
|
304
|
+
severity: 'warn',
|
|
305
|
+
message: `legacy monthly archive files kept by migration: ${r.legacyMonthlyFiles.length}`,
|
|
306
|
+
hint: 'unconverted lines exist only here — serve start retries conversion (idempotent); delete manually only when content is expendable',
|
|
307
|
+
fixable: false,
|
|
308
|
+
});
|
|
309
|
+
}
|
|
310
|
+
if (r.stagingEntries.length > 0) {
|
|
311
|
+
findings.push({
|
|
312
|
+
id: 'ARC007',
|
|
313
|
+
domain: 'data',
|
|
314
|
+
severity: 'info',
|
|
315
|
+
message: `archives/.staging leftovers: ${r.stagingEntries.length} entr(ies)`,
|
|
316
|
+
hint: 'startup reconcile settles or clears them; investigate only if they survive a serve start',
|
|
317
|
+
fixable: false,
|
|
318
|
+
});
|
|
319
|
+
}
|
|
320
|
+
return findings;
|
|
321
|
+
}
|
|
322
|
+
/**
|
|
323
|
+
* 应用残留清除(--fix):删孤儿 dump/会话目录、注销孤儿 session 源、删 visibility/hits 行。
|
|
324
|
+
* ARC006/ARC007 永不动手(唯一正文可能只在源文件 / reconcile 有收尾语义)。
|
|
325
|
+
*
|
|
326
|
+
* @param options - dryRun 时只描述将做什么
|
|
327
|
+
* @returns findings + notes
|
|
328
|
+
*/
|
|
329
|
+
export async function applyArchiveResidueFixes(options = {}) {
|
|
330
|
+
const r = await collectArchiveResidue();
|
|
331
|
+
const notes = [];
|
|
332
|
+
const findings = [];
|
|
333
|
+
const dumpTotal = r.dumpDirs.reduce((sum, d) => sum + d.bytes, 0);
|
|
334
|
+
const dirTotal = r.sessionDirs.reduce((sum, d) => sum + d.bytes, 0);
|
|
335
|
+
if (options.dryRun) {
|
|
336
|
+
if (r.dumpDirs.length > 0) {
|
|
337
|
+
notes.push(`[dry-run] would delete ${r.dumpDirs.length} orphan wire-dump dir(s) (${formatBytes(dumpTotal)})`);
|
|
338
|
+
}
|
|
339
|
+
if (r.unscopedDump) {
|
|
340
|
+
notes.push(`[dry-run] would delete logs/llm-requests/_unscoped (${formatBytes(r.unscopedDump.bytes)})`);
|
|
341
|
+
}
|
|
342
|
+
if (r.sessionDirs.length > 0) {
|
|
343
|
+
notes.push(`[dry-run] would delete ${r.sessionDirs.length} orphan session dir(s) (${formatBytes(dirTotal)})`);
|
|
344
|
+
}
|
|
345
|
+
if (r.knowledgeSources.length > 0) {
|
|
346
|
+
notes.push(`[dry-run] would unregister ${r.knowledgeSources.length} orphan session knowledge source(s)`);
|
|
347
|
+
}
|
|
348
|
+
const visTotal = r.visibilityRows.reduce((sum, v) => sum + v.count, 0);
|
|
349
|
+
const hitTotal = r.hitRows.reduce((sum, v) => sum + v.count, 0);
|
|
350
|
+
if (visTotal > 0 || hitTotal > 0) {
|
|
351
|
+
notes.push(`[dry-run] would delete knowledge rows: session_visibility=${visTotal}, hits=${hitTotal}`);
|
|
352
|
+
}
|
|
353
|
+
if (notes.length === 0)
|
|
354
|
+
notes.push('[dry-run] no archive residue to clean');
|
|
355
|
+
findings.push({
|
|
356
|
+
id: 'FIX003',
|
|
357
|
+
domain: 'data',
|
|
358
|
+
severity: 'info',
|
|
359
|
+
message: `dry-run archive-residue fixes: ${notes.length} action(s); disk not modified`,
|
|
360
|
+
hint: notes.join('; '),
|
|
361
|
+
fixable: true,
|
|
362
|
+
});
|
|
363
|
+
return { findings, notes };
|
|
364
|
+
}
|
|
365
|
+
for (const d of r.dumpDirs) {
|
|
366
|
+
rmSync(d.path, { recursive: true, force: true });
|
|
367
|
+
notes.push(`wire-dump dir deleted: ${d.path} (${formatBytes(d.bytes)})`);
|
|
368
|
+
}
|
|
369
|
+
if (r.unscopedDump) {
|
|
370
|
+
rmSync(r.unscopedDump.path, { recursive: true, force: true });
|
|
371
|
+
notes.push(`unscoped dump dir deleted: ${r.unscopedDump.path}`);
|
|
372
|
+
}
|
|
373
|
+
for (const d of r.sessionDirs) {
|
|
374
|
+
rmSync(d.path, { recursive: true, force: true });
|
|
375
|
+
notes.push(`orphan session dir deleted: ${d.path} (${formatBytes(d.bytes)})`);
|
|
376
|
+
}
|
|
377
|
+
for (const s of r.knowledgeSources) {
|
|
378
|
+
notes.push(await removeKnowledgeSource(s.id));
|
|
379
|
+
}
|
|
380
|
+
const dbPath = knowledgeDbPath();
|
|
381
|
+
const visTotal = r.visibilityRows.reduce((sum, v) => sum + v.count, 0);
|
|
382
|
+
const hitTotal = r.hitRows.reduce((sum, v) => sum + v.count, 0);
|
|
383
|
+
if (visTotal > 0 || hitTotal > 0) {
|
|
384
|
+
if (existsSync(dbPath)) {
|
|
385
|
+
const { KnowledgeDatabase } = await import('@octopi-agent/engine/harness/knowledge/db.js');
|
|
386
|
+
const db = await KnowledgeDatabase.create({ dbPath });
|
|
387
|
+
try {
|
|
388
|
+
for (const v of r.visibilityRows) {
|
|
389
|
+
db.raw
|
|
390
|
+
.prepare('DELETE FROM knowledge_session_visibility WHERE local_session_id = ?')
|
|
391
|
+
.run(v.sessionId);
|
|
392
|
+
}
|
|
393
|
+
for (const h of r.hitRows) {
|
|
394
|
+
db.raw.prepare('DELETE FROM knowledge_hits WHERE session_id = ?').run(h.sessionId);
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
finally {
|
|
398
|
+
db.close();
|
|
399
|
+
}
|
|
400
|
+
notes.push(`knowledge residue rows deleted: session_visibility=${visTotal}, hits=${hitTotal}`);
|
|
401
|
+
}
|
|
402
|
+
else {
|
|
403
|
+
// 计数与库现状之间的竞态(库被并发删除):显式降级,不静默假装清了
|
|
404
|
+
notes.push(`knowledge rows skipped: ${dbPath} disappeared mid-fix`);
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
if (notes.length === 0)
|
|
408
|
+
notes.push('no archive residue to clean');
|
|
409
|
+
findings.push({
|
|
410
|
+
id: 'FIX003',
|
|
411
|
+
domain: 'data',
|
|
412
|
+
severity: 'ok',
|
|
413
|
+
message: `archive-residue fixes applied: ${notes.length} action(s)`,
|
|
414
|
+
hint: notes.join('; ') || undefined,
|
|
415
|
+
fixable: true,
|
|
416
|
+
});
|
|
417
|
+
return { findings, notes };
|
|
418
|
+
}
|
|
@@ -5,6 +5,7 @@
|
|
|
5
5
|
*/
|
|
6
6
|
import type { CliArgs } from '../args.js';
|
|
7
7
|
import { applyDataLayerFixes, checkDeprecatedSessionStore, detectDataLayer, resolveAgentDataTargets, detectKnowledgeDb, detectKnowledgeSourcesAsync, detectKnowledgeStorageHealth, vacuumKnowledgeDatabaseIfWorthwhile, migrateKnowledgeDatabase, knowledgeDbPath, listKnowledgeSources, removeKnowledgeSource } from './data.js';
|
|
8
|
+
import { applyArchiveResidueFixes, collectArchiveResidue, collectLiveSessionRoster, detectArchiveResidue } from './archive-residue.js';
|
|
8
9
|
import { latestConfigBackup, listConfigBackups, restoreConfigFromBackup } from './restore.js';
|
|
9
10
|
export type DoctorSeverity = 'error' | 'warn' | 'info' | 'ok';
|
|
10
11
|
export type FixGroup = 'config' | 'layout' | 'data';
|
|
@@ -128,4 +129,4 @@ export declare function doctorCommand(args: CliArgs, options?: DoctorOptions): P
|
|
|
128
129
|
/** 供测试:列出 agent 目录 */
|
|
129
130
|
export declare function listAgentDirs(home: string): string[];
|
|
130
131
|
export { listConfigBackups, latestConfigBackup, restoreConfigFromBackup };
|
|
131
|
-
export { detectDataLayer, applyDataLayerFixes, resolveAgentDataTargets, checkDeprecatedSessionStore, detectKnowledgeDb, detectKnowledgeSourcesAsync, detectKnowledgeStorageHealth, vacuumKnowledgeDatabaseIfWorthwhile, migrateKnowledgeDatabase, knowledgeDbPath, listKnowledgeSources, removeKnowledgeSource, };
|
|
132
|
+
export { detectDataLayer, applyDataLayerFixes, resolveAgentDataTargets, checkDeprecatedSessionStore, detectKnowledgeDb, detectKnowledgeSourcesAsync, detectKnowledgeStorageHealth, vacuumKnowledgeDatabaseIfWorthwhile, migrateKnowledgeDatabase, knowledgeDbPath, listKnowledgeSources, removeKnowledgeSource, detectArchiveResidue, applyArchiveResidueFixes, collectArchiveResidue, collectLiveSessionRoster, };
|
package/dist/cli/doctor/index.js
CHANGED
|
@@ -10,6 +10,7 @@ import { agentHomeDir, agentWorkspaceDir, expandHomePath, getOctopiHome } from '
|
|
|
10
10
|
import { validateConfig } from '../../config-schema.js';
|
|
11
11
|
import { applyConfigMigrations, CONFIG_MIGRATION_RULES, detectConfigMigrations, expandEnvPlaceholders, parseConfigJson, } from '../../config-migrations.js';
|
|
12
12
|
import { applyDataLayerFixes, checkDeprecatedSessionStore, detectDataLayer, resolveAgentDataTargets, detectKnowledgeDb, detectKnowledgeSourcesAsync, detectKnowledgeStorageHealth, detectRegistrySeeding, vacuumKnowledgeDatabaseIfWorthwhile, migrateKnowledgeDatabase, knowledgeDbPath, listKnowledgeSources, removeKnowledgeSource, } from './data.js';
|
|
13
|
+
import { applyArchiveResidueFixes, collectArchiveResidue, collectLiveSessionRoster, detectArchiveResidue, } from './archive-residue.js';
|
|
13
14
|
import { latestConfigBackup, listConfigBackups, restoreConfigFromBackup, } from './restore.js';
|
|
14
15
|
import { redactConfig, looksLikeSecret, redactSecretString } from './redact.js';
|
|
15
16
|
// ── 路径解析 ──
|
|
@@ -348,6 +349,8 @@ async function diagnoseConfigLayoutData(home, configPath, allowDeleteLegacyDirs,
|
|
|
348
349
|
// knowledge 源注册健康(不自动删源)
|
|
349
350
|
findings.push(...(await detectKnowledgeSourcesAsync()));
|
|
350
351
|
findings.push(...(await detectKnowledgeStorageHealth()));
|
|
352
|
+
// 归档/删除残留(ARC001–ARC007;--fix 清孤儿 dump/会话目录/knowledge session 残留)
|
|
353
|
+
findings.push(...(await detectArchiveResidue()));
|
|
351
354
|
if (configPath) {
|
|
352
355
|
findings.push(...checkDeprecatedSessionStore(rawUnexpanded, targets, dirname(resolve(configPath))));
|
|
353
356
|
}
|
|
@@ -496,6 +499,8 @@ export async function runDoctor(args, options = {}) {
|
|
|
496
499
|
if (applyData) {
|
|
497
500
|
const dataResult = await applyDataLayerFixes(diagnosis.targets, { dryRun: false });
|
|
498
501
|
fixFindings.push(...dataResult.findings);
|
|
502
|
+
const residueResult = await applyArchiveResidueFixes({ dryRun: false });
|
|
503
|
+
fixFindings.push(...residueResult.findings);
|
|
499
504
|
}
|
|
500
505
|
diagnosis = await diagnoseConfigLayoutData(paths.home, paths.configPath, Boolean(options.allowDeleteLegacyDirs), explicitConfig);
|
|
501
506
|
}
|
|
@@ -504,6 +509,7 @@ export async function runDoctor(args, options = {}) {
|
|
|
504
509
|
const probe = diagnosis.raw ? structuredClone(diagnosis.raw) : null;
|
|
505
510
|
const applied = probe ? applyConfigMigrations(probe) : { notes: [] };
|
|
506
511
|
const dataDry = await applyDataLayerFixes(diagnosis.targets, { dryRun: true });
|
|
512
|
+
const residueDry = await applyArchiveResidueFixes({ dryRun: true });
|
|
507
513
|
fixFindings.push({
|
|
508
514
|
id: 'FIX000',
|
|
509
515
|
domain: 'config',
|
|
@@ -513,6 +519,7 @@ export async function runDoctor(args, options = {}) {
|
|
|
513
519
|
fixable: true,
|
|
514
520
|
});
|
|
515
521
|
fixFindings.push(...dataDry.findings);
|
|
522
|
+
fixFindings.push(...residueDry.findings);
|
|
516
523
|
}
|
|
517
524
|
const filtered = diagnosis.findings
|
|
518
525
|
.filter((f) => domainAllowed(f.id, f.domain, options.only))
|
|
@@ -750,4 +757,4 @@ export function listAgentDirs(home) {
|
|
|
750
757
|
.sort();
|
|
751
758
|
}
|
|
752
759
|
export { listConfigBackups, latestConfigBackup, restoreConfigFromBackup };
|
|
753
|
-
export { detectDataLayer, applyDataLayerFixes, resolveAgentDataTargets, checkDeprecatedSessionStore, detectKnowledgeDb, detectKnowledgeSourcesAsync, detectKnowledgeStorageHealth, vacuumKnowledgeDatabaseIfWorthwhile, migrateKnowledgeDatabase, knowledgeDbPath, listKnowledgeSources, removeKnowledgeSource, };
|
|
760
|
+
export { detectDataLayer, applyDataLayerFixes, resolveAgentDataTargets, checkDeprecatedSessionStore, detectKnowledgeDb, detectKnowledgeSourcesAsync, detectKnowledgeStorageHealth, vacuumKnowledgeDatabaseIfWorthwhile, migrateKnowledgeDatabase, knowledgeDbPath, listKnowledgeSources, removeKnowledgeSource, detectArchiveResidue, applyArchiveResidueFixes, collectArchiveResidue, collectLiveSessionRoster, };
|
|
@@ -398,6 +398,16 @@ export declare const OctopiConfigSchema: z.ZodObject<{
|
|
|
398
398
|
maxWakesPerHour: z.ZodOptional<z.ZodNumber>;
|
|
399
399
|
tickIntervalMs: z.ZodOptional<z.ZodNumber>;
|
|
400
400
|
}, z.core.$strip>>;
|
|
401
|
+
archives: z.ZodOptional<z.ZodObject<{
|
|
402
|
+
trashRetentionDays: z.ZodOptional<z.ZodNumber>;
|
|
403
|
+
includeLlmDumps: z.ZodOptional<z.ZodBoolean>;
|
|
404
|
+
autoArchive: z.ZodOptional<z.ZodObject<{
|
|
405
|
+
enabled: z.ZodOptional<z.ZodBoolean>;
|
|
406
|
+
recentRetentionDays: z.ZodOptional<z.ZodNumber>;
|
|
407
|
+
forceDays: z.ZodOptional<z.ZodNumber>;
|
|
408
|
+
retentionDays: z.ZodOptional<z.ZodNumber>;
|
|
409
|
+
}, z.core.$strip>>;
|
|
410
|
+
}, z.core.$strip>>;
|
|
401
411
|
contextEngine: z.ZodOptional<z.ZodObject<{
|
|
402
412
|
type: z.ZodOptional<z.ZodEnum<{
|
|
403
413
|
default: "default";
|
|
@@ -1529,6 +1539,16 @@ export declare const HarnessConfigSchema: z.ZodObject<{
|
|
|
1529
1539
|
maxWakesPerHour: z.ZodOptional<z.ZodNumber>;
|
|
1530
1540
|
tickIntervalMs: z.ZodOptional<z.ZodNumber>;
|
|
1531
1541
|
}, z.core.$strip>>;
|
|
1542
|
+
archives: z.ZodOptional<z.ZodObject<{
|
|
1543
|
+
trashRetentionDays: z.ZodOptional<z.ZodNumber>;
|
|
1544
|
+
includeLlmDumps: z.ZodOptional<z.ZodBoolean>;
|
|
1545
|
+
autoArchive: z.ZodOptional<z.ZodObject<{
|
|
1546
|
+
enabled: z.ZodOptional<z.ZodBoolean>;
|
|
1547
|
+
recentRetentionDays: z.ZodOptional<z.ZodNumber>;
|
|
1548
|
+
forceDays: z.ZodOptional<z.ZodNumber>;
|
|
1549
|
+
retentionDays: z.ZodOptional<z.ZodNumber>;
|
|
1550
|
+
}, z.core.$strip>>;
|
|
1551
|
+
}, z.core.$strip>>;
|
|
1532
1552
|
contextEngine: z.ZodOptional<z.ZodObject<{
|
|
1533
1553
|
type: z.ZodOptional<z.ZodEnum<{
|
|
1534
1554
|
default: "default";
|
package/dist/config.js
CHANGED
|
@@ -249,6 +249,7 @@ export function toGatewayConfig(config) {
|
|
|
249
249
|
webSearch: config.webSearch,
|
|
250
250
|
browser: config.browser,
|
|
251
251
|
schedules: config.schedules,
|
|
252
|
+
archives: config.archives,
|
|
252
253
|
concurrency: config.concurrency,
|
|
253
254
|
plugins: config.plugins,
|
|
254
255
|
knowledge: config.knowledge,
|
package/octopi.example.json
CHANGED
|
@@ -278,6 +278,17 @@
|
|
|
278
278
|
"tickIntervalMs": 60000
|
|
279
279
|
},
|
|
280
280
|
|
|
281
|
+
"archives": {
|
|
282
|
+
"trashRetentionDays": 30,
|
|
283
|
+
"includeLlmDumps": false,
|
|
284
|
+
"autoArchive": {
|
|
285
|
+
"enabled": true,
|
|
286
|
+
"recentRetentionDays": 30,
|
|
287
|
+
"forceDays": 90,
|
|
288
|
+
"retentionDays": 180
|
|
289
|
+
}
|
|
290
|
+
},
|
|
291
|
+
|
|
281
292
|
"channels": [{
|
|
282
293
|
"type": "http",
|
|
283
294
|
"port": 18180,
|
package/octopi.schema.json
CHANGED
|
@@ -1217,6 +1217,42 @@
|
|
|
1217
1217
|
}
|
|
1218
1218
|
}
|
|
1219
1219
|
},
|
|
1220
|
+
"archives": {
|
|
1221
|
+
"type": "object",
|
|
1222
|
+
"properties": {
|
|
1223
|
+
"trashRetentionDays": {
|
|
1224
|
+
"type": "integer",
|
|
1225
|
+
"exclusiveMinimum": 0,
|
|
1226
|
+
"maximum": 9007199254740991
|
|
1227
|
+
},
|
|
1228
|
+
"includeLlmDumps": {
|
|
1229
|
+
"type": "boolean"
|
|
1230
|
+
},
|
|
1231
|
+
"autoArchive": {
|
|
1232
|
+
"type": "object",
|
|
1233
|
+
"properties": {
|
|
1234
|
+
"enabled": {
|
|
1235
|
+
"type": "boolean"
|
|
1236
|
+
},
|
|
1237
|
+
"recentRetentionDays": {
|
|
1238
|
+
"type": "integer",
|
|
1239
|
+
"exclusiveMinimum": 0,
|
|
1240
|
+
"maximum": 9007199254740991
|
|
1241
|
+
},
|
|
1242
|
+
"forceDays": {
|
|
1243
|
+
"type": "integer",
|
|
1244
|
+
"exclusiveMinimum": 0,
|
|
1245
|
+
"maximum": 9007199254740991
|
|
1246
|
+
},
|
|
1247
|
+
"retentionDays": {
|
|
1248
|
+
"type": "integer",
|
|
1249
|
+
"exclusiveMinimum": 0,
|
|
1250
|
+
"maximum": 9007199254740991
|
|
1251
|
+
}
|
|
1252
|
+
}
|
|
1253
|
+
}
|
|
1254
|
+
}
|
|
1255
|
+
},
|
|
1220
1256
|
"contextEngine": {
|
|
1221
1257
|
"type": "object",
|
|
1222
1258
|
"properties": {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "octopi-agent",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.130.2",
|
|
4
4
|
"description": "Octopi suite — CLI (bin: octopi), config IO, and dependency umbrella for core/engine/gateway/webui",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -68,10 +68,10 @@
|
|
|
68
68
|
"dependencies": {
|
|
69
69
|
"@earendil-works/pi-tui": "^0.78.0",
|
|
70
70
|
"@modelcontextprotocol/sdk": "^1.29.0",
|
|
71
|
-
"@octopi-agent/core": "0.
|
|
72
|
-
"@octopi-agent/engine": "0.
|
|
73
|
-
"@octopi-agent/gateway": "0.
|
|
74
|
-
"@octopi-agent/webui": "0.
|
|
71
|
+
"@octopi-agent/core": "0.130.2",
|
|
72
|
+
"@octopi-agent/engine": "0.130.2",
|
|
73
|
+
"@octopi-agent/gateway": "0.130.2",
|
|
74
|
+
"@octopi-agent/webui": "0.130.2",
|
|
75
75
|
"@types/ws": "^8.18.1",
|
|
76
76
|
"chalk": "^5.6.2",
|
|
77
77
|
"js-yaml": "^4.3.2",
|