@njuptlzf/agent-chat-server 0.7.0 → 0.9.0
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/history-archive.d.ts +116 -0
- package/dist/history-archive.js +352 -0
- package/dist/main.js +15 -1
- package/dist/server.d.ts +3 -0
- package/dist/server.js +35 -0
- package/dist/session.d.ts +31 -1
- package/dist/session.js +78 -2
- package/dist/sqlite-stores.d.ts +25 -2
- package/dist/sqlite-stores.js +263 -24
- package/package.json +65 -65
- package/scripts/agentchat-integrate.mjs +892 -892
- package/src/history-archive.ts +437 -0
- package/src/main.ts +17 -1
- package/src/server.ts +33 -0
- package/src/session.ts +100 -3
- package/src/sqlite-stores.ts +283 -27
package/dist/session.js
CHANGED
|
@@ -358,6 +358,56 @@ export function compileSystemPrompt(product, qual, persona, override) {
|
|
|
358
358
|
* session also means a crashed session cannot leak a handle into the next one.
|
|
359
359
|
*/
|
|
360
360
|
const memorySessionsByAgent = new WeakMap();
|
|
361
|
+
/** 后台记忆补全的硬边界:超时与 token 都由宿主定,provider 只能通过请求收紧、不能放宽。 */
|
|
362
|
+
const MEMORY_COMPLETION_TIMEOUT_MS = 15_000;
|
|
363
|
+
const MEMORY_COMPLETION_MAX_TOKENS = 512;
|
|
364
|
+
/**
|
|
365
|
+
* 把宿主的 `modelRuntime` 适配成注入 provider 的 `MemoryCompletion`。
|
|
366
|
+
*
|
|
367
|
+
* 复用当前会话已解析的 model(与聊天同一物理模型),但**不进用户会话**:
|
|
368
|
+
* 一次 `completeSimple` 调用,独立历史,失败以 `ok:false` 结构化返回,
|
|
369
|
+
* 不参与聊天主链的 failover,也不向 provider 抛异常。超时双保险:signal
|
|
370
|
+
* 传给 provider 请求,Promise.race 兜底不遵守 signal 的底层实现。
|
|
371
|
+
*/
|
|
372
|
+
export function createMemoryCompletion(modelRuntime, model) {
|
|
373
|
+
return async (req) => {
|
|
374
|
+
const controller = new AbortController();
|
|
375
|
+
// 调用方 signal(例如会话销毁)要能取消这次后台补全。
|
|
376
|
+
const onAbort = () => controller.abort();
|
|
377
|
+
req.signal?.addEventListener("abort", onAbort, { once: true });
|
|
378
|
+
let timer;
|
|
379
|
+
try {
|
|
380
|
+
const timeout = new Promise((_, reject) => {
|
|
381
|
+
timer = setTimeout(() => {
|
|
382
|
+
controller.abort();
|
|
383
|
+
reject(new Error("memory completion timeout"));
|
|
384
|
+
}, MEMORY_COMPLETION_TIMEOUT_MS);
|
|
385
|
+
});
|
|
386
|
+
const reply = await Promise.race([
|
|
387
|
+
modelRuntime.completeSimple(model, { systemPrompt: req.systemPrompt, messages: [{ role: "user", content: req.userPrompt, timestamp: Date.now() }] }, {
|
|
388
|
+
signal: controller.signal,
|
|
389
|
+
maxTokens: Math.min(req.maxTokens ?? MEMORY_COMPLETION_MAX_TOKENS, MEMORY_COMPLETION_MAX_TOKENS),
|
|
390
|
+
}),
|
|
391
|
+
timeout,
|
|
392
|
+
]);
|
|
393
|
+
if (req.signal?.aborted)
|
|
394
|
+
return { text: "", ok: false, error: "aborted" };
|
|
395
|
+
const text = reply.content
|
|
396
|
+
.filter((c) => c.type === "text")
|
|
397
|
+
.map((c) => c.text)
|
|
398
|
+
.join("\n");
|
|
399
|
+
return { text, ok: true };
|
|
400
|
+
}
|
|
401
|
+
catch (err) {
|
|
402
|
+
return { text: "", ok: false, error: err instanceof Error ? err.message : String(err) };
|
|
403
|
+
}
|
|
404
|
+
finally {
|
|
405
|
+
if (timer !== undefined)
|
|
406
|
+
clearTimeout(timer);
|
|
407
|
+
req.signal?.removeEventListener("abort", onAbort);
|
|
408
|
+
}
|
|
409
|
+
};
|
|
410
|
+
}
|
|
361
411
|
/**
|
|
362
412
|
* 产品依赖跨会话画像,但选中的 provider 没声明该能力时给宿主的可读告警(C6)。
|
|
363
413
|
* 导出为纯函数,便于契约测试钉住文案;返回 null 表示无需告警。
|
|
@@ -369,6 +419,30 @@ export function sharedProfileMismatchWarning(requiresSharedProfile, providerSupp
|
|
|
369
419
|
+ "但所选记忆 provider 未声明支持该能力(capabilities.sharedProfile);"
|
|
370
420
|
+ "用户偏好不会在 scope 之间共享。请改用支持该能力的 provider,或移除该依赖声明。";
|
|
371
421
|
}
|
|
422
|
+
/**
|
|
423
|
+
* The single source of truth for memory root paths. Pure: no fs, no env, no cwd.
|
|
424
|
+
*
|
|
425
|
+
* Two disjoint roots per tenant:
|
|
426
|
+
* - `storageRoot` — one per (user, scope), dies with the qualification/session;
|
|
427
|
+
* - `sharedRoot` — one per user, dies only on account deletion.
|
|
428
|
+
* Siblings, never nested: deleting a qualification must not delete user identity.
|
|
429
|
+
* The scope key falls back through memoryScopeKey → qual → "default"; callers
|
|
430
|
+
* must not re-implement that order (job-scoped sessions pass an explicit key).
|
|
431
|
+
* `qual` is the second fallback so callers pass both and own no precedence logic.
|
|
432
|
+
*
|
|
433
|
+
* Path layers, so nobody has to count the two directory names:
|
|
434
|
+
* - `tenants/` is the HOST layer — one root per (user, scope), plus one per user
|
|
435
|
+
* for the shared profile. The host allocates and erases it.
|
|
436
|
+
* - `memory/` (under a tenant root) is the PROVIDER's internal layout, which
|
|
437
|
+
* this host neither knows nor asserts.
|
|
438
|
+
*/
|
|
439
|
+
export function resolveMemoryRoots(dataDir, userId, memoryScopeKey, qual) {
|
|
440
|
+
const scope = memoryScopeKey || qual || "default";
|
|
441
|
+
return {
|
|
442
|
+
storageRoot: path.join(dataDir, "tenants", userId, scope),
|
|
443
|
+
sharedRoot: path.join(dataDir, "tenants", userId, "profile"),
|
|
444
|
+
};
|
|
445
|
+
}
|
|
372
446
|
/**
|
|
373
447
|
* Build a pi session locked down for multi-user serving:
|
|
374
448
|
* - no built-in tools (no bash / read / write / edit), verified by test
|
|
@@ -506,8 +580,7 @@ export async function createProductSession(opts) {
|
|
|
506
580
|
// The host owns the roots, so it must materialise them before open(). The
|
|
507
581
|
// old built-in created parents lazily on write; hermes uses mkdtemp under
|
|
508
582
|
// storageRoot and expects the host-allocated directory to already exist.
|
|
509
|
-
const storageRoot =
|
|
510
|
-
const sharedRoot = path.join(opts.dataDir, "memory", opts.userId, "profile");
|
|
583
|
+
const { storageRoot, sharedRoot } = resolveMemoryRoots(opts.dataDir, opts.userId, opts.memoryScopeKey, opts.qual);
|
|
511
584
|
if (memoryProvider) {
|
|
512
585
|
mkdirSync(storageRoot, { recursive: true });
|
|
513
586
|
mkdirSync(sharedRoot, { recursive: true });
|
|
@@ -518,6 +591,9 @@ export async function createProductSession(opts) {
|
|
|
518
591
|
sharedRoot,
|
|
519
592
|
agentDir: opts.dataDir,
|
|
520
593
|
scope: { userId: opts.userId, productId: product.manifest.id, qual: opts.qual || "" },
|
|
594
|
+
// 后台整理(巩固/review/flush)用的补全能力。复用本会话已解析的 model,
|
|
595
|
+
// 不进用户会话历史;provider 拿不到凭据,超时/预算由这里统一控。
|
|
596
|
+
complete: createMemoryCompletion(modelRuntime, model),
|
|
521
597
|
})
|
|
522
598
|
: null;
|
|
523
599
|
const memoryExtensions = memorySession ? [memorySession.factory] : [];
|
package/dist/sqlite-stores.d.ts
CHANGED
|
@@ -5,6 +5,8 @@ import type { AccountStateStore, AuditEvent, AuditQuery, AuditRow, AuditStore, B
|
|
|
5
5
|
import type { Ledger, ReserveInput, ReserveOutcome, SettleInput, SettleOutcome } from "./ledger.ts";
|
|
6
6
|
import type { CheckpointStore, RunCheckpoint, RunJobRow } from "./checkpoint-store.ts";
|
|
7
7
|
import type { PhaseCheckpoint } from "./orchestrator/run-spec.ts";
|
|
8
|
+
import { HistoryBodyCache } from "./history-archive.ts";
|
|
9
|
+
import type { ArchiveSweepOptions, ArchiveSweepReport, HistoryArchiveCapable, HistoryArchiveStore } from "./history-archive.ts";
|
|
8
10
|
import type { VerifyResult } from "./audit.ts";
|
|
9
11
|
/**
|
|
10
12
|
* SQLite-backed stores (node:sqlite, no extra dependency). One connection per
|
|
@@ -53,11 +55,31 @@ export interface ProductMigrationReport {
|
|
|
53
55
|
};
|
|
54
56
|
}
|
|
55
57
|
export declare function migrateProductDimension(db: DatabaseSync, defaultPersona: string): ProductMigrationReport;
|
|
56
|
-
export declare class SqliteHistoryStore implements HistoryStore {
|
|
58
|
+
export declare class SqliteHistoryStore implements HistoryStore, HistoryArchiveCapable {
|
|
57
59
|
private readonly db;
|
|
58
|
-
|
|
60
|
+
private readonly archiveStore?;
|
|
61
|
+
private readonly options;
|
|
62
|
+
private readonly cache;
|
|
63
|
+
constructor(db: DatabaseSync, archiveStore?: HistoryArchiveStore | undefined, options?: {
|
|
64
|
+
hot?: boolean;
|
|
65
|
+
cache?: HistoryBodyCache;
|
|
66
|
+
});
|
|
67
|
+
/** 取回归档正文并验内容寻址 hash;未配置或对象损坏都只影响这条已归档会话。 */
|
|
68
|
+
private readArchived;
|
|
69
|
+
/** local 读 SQLite;remote/archived 先查 LRU,命中零对象 GET,未命中验 hash 后回填。 */
|
|
70
|
+
private readBody;
|
|
59
71
|
load(userId: string, scope: string, sessionId: string): Promise<FileEntry[] | undefined>;
|
|
60
72
|
save(userId: string, scope: string, sessionId: string, entries: FileEntry[], persona?: string, personaVersion?: number): Promise<void>;
|
|
73
|
+
/**
|
|
74
|
+
* 后台归档一轮:local 正文先写对象并回读验 hash;remote 正文已在外置,只需验 hash。
|
|
75
|
+
* 成功后条件 UPDATE 翻 archived 并淘汰缓存——与 save 竞态时只留无害孤儿对象。
|
|
76
|
+
*/
|
|
77
|
+
archiveIdle(options: ArchiveSweepOptions): Promise<ArchiveSweepReport>;
|
|
78
|
+
/**
|
|
79
|
+
* 恢复单条会话:验 hash 后按当前模式落点——热模式回 remote(正文仍在外置、刷新缓存),
|
|
80
|
+
* 关闭时回 local(写回 SQLite 正文)。updated_at 不动,不伪造新活动。
|
|
81
|
+
*/
|
|
82
|
+
restore(userId: string, scope: string, sessionId: string): Promise<FileEntry[] | undefined>;
|
|
61
83
|
personaOf(userId: string, sessionId: string): Promise<string | null>;
|
|
62
84
|
scopeOf(userId: string, sessionId: string): Promise<string | undefined>;
|
|
63
85
|
list(userId: string, scope?: string): Promise<SessionSummary[]>;
|
|
@@ -184,6 +206,7 @@ export declare class SqliteAudit implements AuditStore {
|
|
|
184
206
|
append(e: AuditEvent): Promise<number>;
|
|
185
207
|
/** 同步内核:purge 要在同一同步段里「白名单先入链、再删行」,不能经 await 让出。 */
|
|
186
208
|
private appendSync;
|
|
209
|
+
private appendRow;
|
|
187
210
|
query(q: AuditQuery): Promise<{
|
|
188
211
|
rows: AuditRow[];
|
|
189
212
|
total: number;
|
package/dist/sqlite-stores.js
CHANGED
|
@@ -3,6 +3,7 @@ import { DatabaseSync } from "node:sqlite";
|
|
|
3
3
|
import { DEFAULT_PERSONA_ID } from "@njuptlzf/agent-chat-protocol";
|
|
4
4
|
import { assertMembershipWrite, assertProductId, summarizeHistory, quotaBlockedCostReason, quotaBlockedReason } from "./stores.js";
|
|
5
5
|
import { RESERVATION_TTL_MS } from "./ledger.js";
|
|
6
|
+
import { archiveKeyFor, HistoryArchiveUnavailableError, HistoryBodyCache, sessionCacheKey, verifyArchiveObject } from "./history-archive.js";
|
|
6
7
|
import { auditPageWindow } from "./stores.js";
|
|
7
8
|
import { GENESIS_HASH, assertNoSensitiveKeys, canonicalize, rowHash, segmentsFrom, verifyChain } from "./audit.js";
|
|
8
9
|
/**
|
|
@@ -37,6 +38,12 @@ export function openDatabase(file, defaultScope = "", defaultPersona = DEFAULT_P
|
|
|
37
38
|
persona TEXT,
|
|
38
39
|
-- 建会话时生效的人格配置版本;0 = manifest,NULL = 更早的老行(不编历史)。
|
|
39
40
|
persona_version INTEGER,
|
|
41
|
+
-- 冷热分层阶段 0:list() 只读这四列,不再解析 entries 正文。
|
|
42
|
+
-- state='local' = 正文在 SQLite;'archived' 留给阶段 1(对象存储),现在不会写入。
|
|
43
|
+
title TEXT NOT NULL DEFAULT '新会话',
|
|
44
|
+
message_count INTEGER NOT NULL DEFAULT 0,
|
|
45
|
+
state TEXT NOT NULL DEFAULT 'local',
|
|
46
|
+
archive_key TEXT,
|
|
40
47
|
PRIMARY KEY (user_id, scope, session_id)
|
|
41
48
|
);
|
|
42
49
|
-- persona 进主键:各个人格各算各的。老库由 migrateScope 重建。
|
|
@@ -295,6 +302,41 @@ function migrateScope(db, defaultScope, defaultPersona) {
|
|
|
295
302
|
if (!columns("history").includes("persona_version")) {
|
|
296
303
|
db.exec("ALTER TABLE history ADD COLUMN persona_version INTEGER");
|
|
297
304
|
}
|
|
305
|
+
// 会话历史冷热分层阶段 0:瘦表物化列。加列与老行回填放在同一个事务里——
|
|
306
|
+
// 中途崩溃整体回滚,下次打开仍是旧形状,不会留下「列加了一半、摘要没回填」的库。
|
|
307
|
+
const historyCols = columns("history");
|
|
308
|
+
if (historyCols.some((c) => !["title", "message_count", "state", "archive_key"].includes(c))) {
|
|
309
|
+
db.exec("BEGIN");
|
|
310
|
+
try {
|
|
311
|
+
if (!historyCols.includes("title"))
|
|
312
|
+
db.exec("ALTER TABLE history ADD COLUMN title TEXT NOT NULL DEFAULT '新会话'");
|
|
313
|
+
if (!historyCols.includes("message_count"))
|
|
314
|
+
db.exec("ALTER TABLE history ADD COLUMN message_count INTEGER NOT NULL DEFAULT 0");
|
|
315
|
+
if (!historyCols.includes("state"))
|
|
316
|
+
db.exec("ALTER TABLE history ADD COLUMN state TEXT NOT NULL DEFAULT 'local'");
|
|
317
|
+
if (!historyCols.includes("archive_key"))
|
|
318
|
+
db.exec("ALTER TABLE history ADD COLUMN archive_key TEXT");
|
|
319
|
+
const rows = db
|
|
320
|
+
.prepare("SELECT user_id, scope, session_id, entries FROM history")
|
|
321
|
+
.all();
|
|
322
|
+
const backfill = db.prepare("UPDATE history SET title = ?, message_count = ? WHERE user_id = ? AND scope = ? AND session_id = ?");
|
|
323
|
+
for (const r of rows) {
|
|
324
|
+
let summary = { title: "新会话", messageCount: 0 };
|
|
325
|
+
try {
|
|
326
|
+
summary = summarizeHistory(JSON.parse(r.entries));
|
|
327
|
+
}
|
|
328
|
+
catch {
|
|
329
|
+
// 老正文本来就坏:保留默认摘要,不猜标题,也不让开库失败。
|
|
330
|
+
}
|
|
331
|
+
backfill.run(summary.title, summary.messageCount, r.user_id, r.scope, r.session_id);
|
|
332
|
+
}
|
|
333
|
+
db.exec("COMMIT");
|
|
334
|
+
}
|
|
335
|
+
catch (err) {
|
|
336
|
+
db.exec("ROLLBACK");
|
|
337
|
+
throw err;
|
|
338
|
+
}
|
|
339
|
+
}
|
|
298
340
|
// 轮数闸也要分账(默认助手的 10 轮不该被声明人格吃掉)。老行一律回填默认人格——
|
|
299
341
|
// 它们本来就是默认助手会话,猜成别的会把历史算到声明人格头上。
|
|
300
342
|
if (!columns("runs").includes("persona")) {
|
|
@@ -419,26 +461,190 @@ export function migrateProductDimension(db, defaultPersona) {
|
|
|
419
461
|
}
|
|
420
462
|
export class SqliteHistoryStore {
|
|
421
463
|
db;
|
|
422
|
-
|
|
464
|
+
archiveStore;
|
|
465
|
+
options;
|
|
466
|
+
cache;
|
|
467
|
+
constructor(db, archiveStore, options = {}) {
|
|
423
468
|
this.db = db;
|
|
469
|
+
this.archiveStore = archiveStore;
|
|
470
|
+
this.options = options;
|
|
471
|
+
if (options.hot && !archiveStore)
|
|
472
|
+
throw new Error("history hot mode requires an archive store");
|
|
473
|
+
this.cache = options.cache ?? new HistoryBodyCache();
|
|
474
|
+
}
|
|
475
|
+
/** 取回归档正文并验内容寻址 hash;未配置或对象损坏都只影响这条已归档会话。 */
|
|
476
|
+
async readArchived(key) {
|
|
477
|
+
if (!key || !this.archiveStore) {
|
|
478
|
+
throw new HistoryArchiveUnavailableError("history archive is not configured for this session");
|
|
479
|
+
}
|
|
480
|
+
const body = await this.archiveStore.get(key);
|
|
481
|
+
if (body === null || !verifyArchiveObject(key, body)) {
|
|
482
|
+
throw new HistoryArchiveUnavailableError(`archived history failed hash verification: ${key}`);
|
|
483
|
+
}
|
|
484
|
+
return body;
|
|
485
|
+
}
|
|
486
|
+
/** local 读 SQLite;remote/archived 先查 LRU,命中零对象 GET,未命中验 hash 后回填。 */
|
|
487
|
+
async readBody(row, userId, scope, sessionId) {
|
|
488
|
+
if (row.state === "local")
|
|
489
|
+
return row.entries;
|
|
490
|
+
const sessionKey = sessionCacheKey(userId, scope, sessionId);
|
|
491
|
+
const key = row.archive_key;
|
|
492
|
+
const cached = key ? this.cache.get(sessionKey, key) : undefined;
|
|
493
|
+
if (cached !== undefined)
|
|
494
|
+
return cached;
|
|
495
|
+
const body = await this.readArchived(key);
|
|
496
|
+
if (key)
|
|
497
|
+
this.cache.set(sessionKey, key, body);
|
|
498
|
+
return body;
|
|
424
499
|
}
|
|
425
500
|
async load(userId, scope, sessionId) {
|
|
426
501
|
const row = this.db
|
|
427
|
-
.prepare("SELECT entries FROM history WHERE user_id = ? AND scope = ? AND session_id = ?")
|
|
502
|
+
.prepare("SELECT entries, state, archive_key FROM history WHERE user_id = ? AND scope = ? AND session_id = ?")
|
|
428
503
|
.get(userId, scope, sessionId);
|
|
429
|
-
|
|
504
|
+
if (!row)
|
|
505
|
+
return undefined;
|
|
506
|
+
return JSON.parse(await this.readBody(row, userId, scope, sessionId));
|
|
430
507
|
}
|
|
431
508
|
async save(userId, scope, sessionId, entries, persona, personaVersion) {
|
|
509
|
+
const body = JSON.stringify(entries);
|
|
510
|
+
const { title, messageCount } = summarizeHistory(entries);
|
|
511
|
+
let storedBody = body;
|
|
512
|
+
let state = "local";
|
|
513
|
+
let archiveKey = null;
|
|
514
|
+
if (this.options.hot && this.archiveStore) {
|
|
515
|
+
// 对象先写成功再翻指针;写失败就让整次 save 失败,不静默退回本地正文。
|
|
516
|
+
archiveKey = archiveKeyFor(body);
|
|
517
|
+
await this.archiveStore.put(archiveKey, body);
|
|
518
|
+
storedBody = "";
|
|
519
|
+
state = "remote";
|
|
520
|
+
}
|
|
432
521
|
this.db
|
|
433
|
-
.prepare(`INSERT INTO history (user_id, scope, session_id, entries, updated_at, persona, persona_version
|
|
522
|
+
.prepare(`INSERT INTO history (user_id, scope, session_id, entries, updated_at, persona, persona_version, title, message_count, state, archive_key)
|
|
523
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
|
434
524
|
ON CONFLICT (user_id, scope, session_id) DO UPDATE
|
|
435
525
|
SET entries = excluded.entries,
|
|
436
526
|
updated_at = excluded.updated_at,
|
|
527
|
+
title = excluded.title,
|
|
528
|
+
message_count = excluded.message_count,
|
|
529
|
+
state = excluded.state,
|
|
530
|
+
archive_key = excluded.archive_key,
|
|
437
531
|
-- 首次绑定后不再改写:中途换人格该由端点拦(persona_mismatch),这里是第二道。
|
|
438
532
|
persona = COALESCE(history.persona, excluded.persona),
|
|
439
533
|
-- 版本与人格同一生命周期:第一轮落库那一刻定死,之后改配置也不追认。
|
|
440
534
|
persona_version = COALESCE(history.persona_version, excluded.persona_version)`)
|
|
441
|
-
.run(userId, scope, sessionId,
|
|
535
|
+
.run(userId, scope, sessionId, storedBody, new Date().toISOString(), persona ?? null, personaVersion ?? null, title, messageCount, state, archiveKey);
|
|
536
|
+
const sessionKey = sessionCacheKey(userId, scope, sessionId);
|
|
537
|
+
if (state === "remote" && archiveKey)
|
|
538
|
+
this.cache.set(sessionKey, archiveKey, body);
|
|
539
|
+
else
|
|
540
|
+
this.cache.delete(sessionKey);
|
|
541
|
+
}
|
|
542
|
+
/**
|
|
543
|
+
* 后台归档一轮:local 正文先写对象并回读验 hash;remote 正文已在外置,只需验 hash。
|
|
544
|
+
* 成功后条件 UPDATE 翻 archived 并淘汰缓存——与 save 竞态时只留无害孤儿对象。
|
|
545
|
+
*/
|
|
546
|
+
async archiveIdle(options) {
|
|
547
|
+
const report = { scanned: 0, archived: 0, skipped: 0, failed: 0 };
|
|
548
|
+
if (!this.archiveStore)
|
|
549
|
+
return report;
|
|
550
|
+
const cutoff = new Date(Date.now() - options.idleDays * 86_400_000).toISOString();
|
|
551
|
+
const rows = this.db
|
|
552
|
+
.prepare(`SELECT user_id, scope, session_id, entries, state, archive_key
|
|
553
|
+
FROM history
|
|
554
|
+
WHERE state IN ('local', 'remote') AND updated_at < ?
|
|
555
|
+
AND (state = 'remote' OR entries <> '')
|
|
556
|
+
ORDER BY updated_at ASC LIMIT ?`)
|
|
557
|
+
.all(cutoff, options.batchSize);
|
|
558
|
+
report.scanned = rows.length;
|
|
559
|
+
for (const r of rows) {
|
|
560
|
+
const sessionKey = sessionCacheKey(r.user_id, r.scope, r.session_id);
|
|
561
|
+
try {
|
|
562
|
+
let key;
|
|
563
|
+
let body;
|
|
564
|
+
if (r.state === "remote") {
|
|
565
|
+
if (!r.archive_key)
|
|
566
|
+
throw new Error("remote history row without archive_key");
|
|
567
|
+
key = r.archive_key;
|
|
568
|
+
const cached = this.cache.get(sessionKey, key);
|
|
569
|
+
body = cached ?? await this.readArchived(key);
|
|
570
|
+
if (!verifyArchiveObject(key, body))
|
|
571
|
+
throw new Error(`archive object failed hash verification: ${key}`);
|
|
572
|
+
}
|
|
573
|
+
else {
|
|
574
|
+
body = r.entries;
|
|
575
|
+
key = archiveKeyFor(body);
|
|
576
|
+
// 上一轮写完对象却没更指针时,key 已存在且 hash 正确:本轮只补指针,不重写对象。
|
|
577
|
+
const existing = await this.archiveStore.get(key);
|
|
578
|
+
if (existing === null) {
|
|
579
|
+
await this.archiveStore.put(key, body);
|
|
580
|
+
}
|
|
581
|
+
else if (!verifyArchiveObject(key, existing)) {
|
|
582
|
+
throw new Error(`existing archive object failed hash verification: ${key}`);
|
|
583
|
+
}
|
|
584
|
+
const verified = await this.archiveStore.get(key);
|
|
585
|
+
if (verified === null || !verifyArchiveObject(key, verified)) {
|
|
586
|
+
throw new Error(`archive object failed hash verification: ${key}`);
|
|
587
|
+
}
|
|
588
|
+
}
|
|
589
|
+
const updated = r.state === "local"
|
|
590
|
+
? this.db
|
|
591
|
+
.prepare(`UPDATE history SET entries = '', state = 'archived', archive_key = ?
|
|
592
|
+
WHERE user_id = ? AND scope = ? AND session_id = ? AND state = 'local'`)
|
|
593
|
+
.run(key, r.user_id, r.scope, r.session_id)
|
|
594
|
+
: this.db
|
|
595
|
+
.prepare(`UPDATE history SET entries = '', state = 'archived', archive_key = ?
|
|
596
|
+
WHERE user_id = ? AND scope = ? AND session_id = ? AND state = 'remote' AND archive_key = ?`)
|
|
597
|
+
.run(key, r.user_id, r.scope, r.session_id, key);
|
|
598
|
+
if (updated.changes === 0) {
|
|
599
|
+
report.skipped++;
|
|
600
|
+
}
|
|
601
|
+
else {
|
|
602
|
+
this.cache.delete(sessionKey);
|
|
603
|
+
report.archived++;
|
|
604
|
+
}
|
|
605
|
+
}
|
|
606
|
+
catch (err) {
|
|
607
|
+
report.failed++;
|
|
608
|
+
console.warn("[history-archive] archive failed for a session:", err instanceof Error ? err.message : String(err));
|
|
609
|
+
}
|
|
610
|
+
}
|
|
611
|
+
return report;
|
|
612
|
+
}
|
|
613
|
+
/**
|
|
614
|
+
* 恢复单条会话:验 hash 后按当前模式落点——热模式回 remote(正文仍在外置、刷新缓存),
|
|
615
|
+
* 关闭时回 local(写回 SQLite 正文)。updated_at 不动,不伪造新活动。
|
|
616
|
+
*/
|
|
617
|
+
async restore(userId, scope, sessionId) {
|
|
618
|
+
const row = this.db
|
|
619
|
+
.prepare("SELECT entries, state, archive_key FROM history WHERE user_id = ? AND scope = ? AND session_id = ?")
|
|
620
|
+
.get(userId, scope, sessionId);
|
|
621
|
+
if (!row)
|
|
622
|
+
return undefined;
|
|
623
|
+
if (row.state === "local")
|
|
624
|
+
return JSON.parse(row.entries);
|
|
625
|
+
const body = await this.readBody(row, userId, scope, sessionId);
|
|
626
|
+
const sessionKey = sessionCacheKey(userId, scope, sessionId);
|
|
627
|
+
if (this.options.hot && row.archive_key) {
|
|
628
|
+
const updated = this.db
|
|
629
|
+
.prepare(`UPDATE history SET entries = '', state = 'remote', archive_key = ?
|
|
630
|
+
WHERE user_id = ? AND scope = ? AND session_id = ?
|
|
631
|
+
AND state IN ('remote', 'archived') AND archive_key = ?`)
|
|
632
|
+
.run(row.archive_key, userId, scope, sessionId, row.archive_key);
|
|
633
|
+
if (updated.changes === 0)
|
|
634
|
+
throw new HistoryArchiveUnavailableError("archive state changed during restore; retry");
|
|
635
|
+
this.cache.set(sessionKey, row.archive_key, body);
|
|
636
|
+
}
|
|
637
|
+
else {
|
|
638
|
+
const updated = this.db
|
|
639
|
+
.prepare(`UPDATE history SET entries = ?, state = 'local', archive_key = NULL
|
|
640
|
+
WHERE user_id = ? AND scope = ? AND session_id = ?
|
|
641
|
+
AND state IN ('remote', 'archived') AND archive_key = ?`)
|
|
642
|
+
.run(body, userId, scope, sessionId, row.archive_key);
|
|
643
|
+
if (updated.changes === 0)
|
|
644
|
+
throw new HistoryArchiveUnavailableError("archive state changed during restore; retry");
|
|
645
|
+
this.cache.delete(sessionKey);
|
|
646
|
+
}
|
|
647
|
+
return JSON.parse(body);
|
|
442
648
|
}
|
|
443
649
|
async personaOf(userId, sessionId) {
|
|
444
650
|
const row = this.db
|
|
@@ -452,18 +658,25 @@ export class SqliteHistoryStore {
|
|
|
452
658
|
.get(userId, sessionId);
|
|
453
659
|
return row?.scope;
|
|
454
660
|
}
|
|
455
|
-
//
|
|
456
|
-
// sessions per user. Upgrade path: persist `title`/`message_count` columns at save time.
|
|
661
|
+
// 只读索引列:正文不进 list,坏 JSON / 归档后的空 entries 都不影响会话列表。
|
|
457
662
|
async list(userId, scope) {
|
|
458
663
|
const where = scope === undefined ? "user_id = ?" : "user_id = ? AND scope = ?";
|
|
459
664
|
const args = scope === undefined ? [userId] : [userId, scope];
|
|
460
665
|
const rows = this.db
|
|
461
|
-
.prepare(`SELECT session_id, scope,
|
|
666
|
+
.prepare(`SELECT session_id, scope,
|
|
667
|
+
COALESCE(title, '新会话') AS title,
|
|
668
|
+
COALESCE(message_count, 0) AS message_count,
|
|
669
|
+
updated_at, persona
|
|
670
|
+
FROM history WHERE ${where} ORDER BY updated_at DESC LIMIT 50`)
|
|
462
671
|
.all(...args);
|
|
463
|
-
return rows.map((r) => {
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
672
|
+
return rows.map((r) => ({
|
|
673
|
+
sessionId: r.session_id,
|
|
674
|
+
scope: r.scope,
|
|
675
|
+
title: r.title,
|
|
676
|
+
updatedAt: r.updated_at,
|
|
677
|
+
messageCount: r.message_count,
|
|
678
|
+
persona: r.persona,
|
|
679
|
+
}));
|
|
467
680
|
}
|
|
468
681
|
}
|
|
469
682
|
/**
|
|
@@ -1028,6 +1241,22 @@ export class SqliteAudit {
|
|
|
1028
1241
|
}
|
|
1029
1242
|
/** 同步内核:purge 要在同一同步段里「白名单先入链、再删行」,不能经 await 让出。 */
|
|
1030
1243
|
appendSync(e) {
|
|
1244
|
+
// 「读上一行 hash → 写本行」必须是一个写事务。跨进程并发 append 时,两个进程会读到
|
|
1245
|
+
// 同一个 prev_hash,各写一行——链分叉(2026-11-21 实测:6 进程 × 25 条,3 轮里 1 轮
|
|
1246
|
+
// verify.ok=false、forks=3)。BEGIN IMMEDIATE 拿写锁后再读,天然串行;busy_timeout
|
|
1247
|
+
// 已在 openDatabase 设 5000,排队几毫秒远好于断链。
|
|
1248
|
+
this.db.exec("BEGIN IMMEDIATE");
|
|
1249
|
+
try {
|
|
1250
|
+
const id = this.appendRow(e);
|
|
1251
|
+
this.db.exec("COMMIT");
|
|
1252
|
+
return id;
|
|
1253
|
+
}
|
|
1254
|
+
catch (err) {
|
|
1255
|
+
this.db.exec("ROLLBACK");
|
|
1256
|
+
throw err;
|
|
1257
|
+
}
|
|
1258
|
+
}
|
|
1259
|
+
appendRow(e) {
|
|
1031
1260
|
const meta = e.meta ?? {};
|
|
1032
1261
|
assertNoSensitiveKeys(meta);
|
|
1033
1262
|
const body = {
|
|
@@ -1099,19 +1328,29 @@ export class SqliteAudit {
|
|
|
1099
1328
|
if (!doomed.length)
|
|
1100
1329
|
return 0;
|
|
1101
1330
|
const ids = doomed.map((d) => Number(d.id));
|
|
1102
|
-
//
|
|
1103
|
-
|
|
1104
|
-
|
|
1105
|
-
|
|
1106
|
-
|
|
1107
|
-
|
|
1108
|
-
|
|
1109
|
-
|
|
1110
|
-
|
|
1111
|
-
|
|
1112
|
-
|
|
1331
|
+
// 白名单入链与物理删必须在同一事务里:中间崩会让「删了行但没有 purge 背书」,
|
|
1332
|
+
// verify 会把 id 空洞判成恶意删行(断链)。BEGIN IMMEDIATE 同时排他,防并发 purge 互相覆盖。
|
|
1333
|
+
this.db.exec("BEGIN IMMEDIATE");
|
|
1334
|
+
try {
|
|
1335
|
+
// ①白名单行先入链(last_hash=段尾行存储 hash,删前读取)。
|
|
1336
|
+
this.appendRow({
|
|
1337
|
+
actor: "system",
|
|
1338
|
+
action: "ops.audit.purge",
|
|
1339
|
+
result: "ok",
|
|
1340
|
+
meta: { cutoff: cutoffIso, count: ids.length, segments: segmentsFrom(doomed.map((d) => ({ id: Number(d.id), hash: d.hash }))) },
|
|
1341
|
+
});
|
|
1342
|
+
// ②再物理删(IN 分块,防变量数上限)。
|
|
1343
|
+
for (let i = 0; i < ids.length; i += 500) {
|
|
1344
|
+
const chunk = ids.slice(i, i + 500);
|
|
1345
|
+
this.db.prepare(`DELETE FROM audit_log WHERE id IN (${chunk.map(() => "?").join(",")})`).run(...chunk);
|
|
1346
|
+
}
|
|
1347
|
+
this.db.exec("COMMIT");
|
|
1348
|
+
return ids.length;
|
|
1349
|
+
}
|
|
1350
|
+
catch (err) {
|
|
1351
|
+
this.db.exec("ROLLBACK");
|
|
1352
|
+
throw err;
|
|
1113
1353
|
}
|
|
1114
|
-
return ids.length;
|
|
1115
1354
|
}
|
|
1116
1355
|
}
|
|
1117
1356
|
/**
|
package/package.json
CHANGED
|
@@ -1,65 +1,65 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@njuptlzf/agent-chat-server",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "agentChat platform kernel: sessions, authz, billing, audit, model/MCP/persona config planes, and the product-directory loader.",
|
|
5
|
-
"type": "module",
|
|
6
|
-
"license": "Apache-2.0",
|
|
7
|
-
"main": "./dist/index.js",
|
|
8
|
-
"types": "./dist/index.d.ts",
|
|
9
|
-
"bin": {
|
|
10
|
-
"agentchat-integrate": "./scripts/agentchat-integrate.mjs"
|
|
11
|
-
},
|
|
12
|
-
"exports": {
|
|
13
|
-
".": {
|
|
14
|
-
"agent-chat-source": "./src/index.ts",
|
|
15
|
-
"types": "./dist/index.d.ts",
|
|
16
|
-
"default": "./dist/index.js"
|
|
17
|
-
},
|
|
18
|
-
"./main": {
|
|
19
|
-
"agent-chat-source": "./src/main.ts",
|
|
20
|
-
"default": "./dist/main.js"
|
|
21
|
-
},
|
|
22
|
-
"./testing": {
|
|
23
|
-
"agent-chat-source": "./src/testing.ts",
|
|
24
|
-
"types": "./dist/testing.d.ts",
|
|
25
|
-
"default": "./dist/testing.js"
|
|
26
|
-
},
|
|
27
|
-
"./package.json": "./package.json"
|
|
28
|
-
},
|
|
29
|
-
"files": [
|
|
30
|
-
"dist",
|
|
31
|
-
"src",
|
|
32
|
-
"scripts/agentchat-integrate.mjs"
|
|
33
|
-
],
|
|
34
|
-
"repository": {
|
|
35
|
-
"type": "git",
|
|
36
|
-
"url": "git+https://github.com/njuptlzf/agent-chat.git",
|
|
37
|
-
"directory": "packages/server"
|
|
38
|
-
},
|
|
39
|
-
"bugs": {
|
|
40
|
-
"url": "https://github.com/njuptlzf/agent-chat/issues"
|
|
41
|
-
},
|
|
42
|
-
"homepage": "https://github.com/njuptlzf/agent-chat#readme",
|
|
43
|
-
"keywords": [
|
|
44
|
-
"agent-chat",
|
|
45
|
-
"chat-server",
|
|
46
|
-
"authz",
|
|
47
|
-
"billing",
|
|
48
|
-
"audit"
|
|
49
|
-
],
|
|
50
|
-
"engines": {
|
|
51
|
-
"node": ">=22.15"
|
|
52
|
-
},
|
|
53
|
-
"publishConfig": {
|
|
54
|
-
"access": "public"
|
|
55
|
-
},
|
|
56
|
-
"dependencies": {
|
|
57
|
-
"@earendil-works/pi-coding-agent": "1.5.2",
|
|
58
|
-
"@njuptlzf/agent-chat-protocol": "^0.
|
|
59
|
-
"@njuptlzf/pi-hermes-memory": "0.11.0-scoped.
|
|
60
|
-
"casbin": "^5.51.1",
|
|
61
|
-
"taskflow-core": "1.0.0",
|
|
62
|
-
"typebox": "^1.3.27",
|
|
63
|
-
"yaml": "^2.8.0"
|
|
64
|
-
}
|
|
65
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "@njuptlzf/agent-chat-server",
|
|
3
|
+
"version": "0.9.0",
|
|
4
|
+
"description": "agentChat platform kernel: sessions, authz, billing, audit, model/MCP/persona config planes, and the product-directory loader.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"license": "Apache-2.0",
|
|
7
|
+
"main": "./dist/index.js",
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"bin": {
|
|
10
|
+
"agentchat-integrate": "./scripts/agentchat-integrate.mjs"
|
|
11
|
+
},
|
|
12
|
+
"exports": {
|
|
13
|
+
".": {
|
|
14
|
+
"agent-chat-source": "./src/index.ts",
|
|
15
|
+
"types": "./dist/index.d.ts",
|
|
16
|
+
"default": "./dist/index.js"
|
|
17
|
+
},
|
|
18
|
+
"./main": {
|
|
19
|
+
"agent-chat-source": "./src/main.ts",
|
|
20
|
+
"default": "./dist/main.js"
|
|
21
|
+
},
|
|
22
|
+
"./testing": {
|
|
23
|
+
"agent-chat-source": "./src/testing.ts",
|
|
24
|
+
"types": "./dist/testing.d.ts",
|
|
25
|
+
"default": "./dist/testing.js"
|
|
26
|
+
},
|
|
27
|
+
"./package.json": "./package.json"
|
|
28
|
+
},
|
|
29
|
+
"files": [
|
|
30
|
+
"dist",
|
|
31
|
+
"src",
|
|
32
|
+
"scripts/agentchat-integrate.mjs"
|
|
33
|
+
],
|
|
34
|
+
"repository": {
|
|
35
|
+
"type": "git",
|
|
36
|
+
"url": "git+https://github.com/njuptlzf/agent-chat.git",
|
|
37
|
+
"directory": "packages/server"
|
|
38
|
+
},
|
|
39
|
+
"bugs": {
|
|
40
|
+
"url": "https://github.com/njuptlzf/agent-chat/issues"
|
|
41
|
+
},
|
|
42
|
+
"homepage": "https://github.com/njuptlzf/agent-chat#readme",
|
|
43
|
+
"keywords": [
|
|
44
|
+
"agent-chat",
|
|
45
|
+
"chat-server",
|
|
46
|
+
"authz",
|
|
47
|
+
"billing",
|
|
48
|
+
"audit"
|
|
49
|
+
],
|
|
50
|
+
"engines": {
|
|
51
|
+
"node": ">=22.15"
|
|
52
|
+
},
|
|
53
|
+
"publishConfig": {
|
|
54
|
+
"access": "public"
|
|
55
|
+
},
|
|
56
|
+
"dependencies": {
|
|
57
|
+
"@earendil-works/pi-coding-agent": "1.5.2",
|
|
58
|
+
"@njuptlzf/agent-chat-protocol": "^0.9.0",
|
|
59
|
+
"@njuptlzf/pi-hermes-memory": "0.11.0-scoped.6",
|
|
60
|
+
"casbin": "^5.51.1",
|
|
61
|
+
"taskflow-core": "1.0.0",
|
|
62
|
+
"typebox": "^1.3.27",
|
|
63
|
+
"yaml": "^2.8.0"
|
|
64
|
+
}
|
|
65
|
+
}
|