@njuptlzf/agent-chat-server 0.3.0 → 0.4.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/checkpoint-store.d.ts +78 -0
- package/dist/checkpoint-store.js +52 -0
- package/dist/components.d.ts +15 -0
- package/dist/components.js +50 -0
- package/dist/extension-admission.d.ts +32 -0
- package/dist/extension-admission.js +155 -0
- package/dist/flows.d.ts +49 -0
- package/dist/flows.js +208 -0
- package/dist/ledger.d.ts +90 -0
- package/dist/ledger.js +115 -0
- package/dist/main.js +14 -2
- package/dist/manifest.js +14 -1
- package/dist/orchestrator/engine.d.ts +132 -0
- package/dist/orchestrator/engine.js +1 -0
- package/dist/orchestrator/engines/fake.d.ts +14 -0
- package/dist/orchestrator/engines/fake.js +282 -0
- package/dist/orchestrator/engines/taskflow.d.ts +4 -0
- package/dist/orchestrator/engines/taskflow.js +541 -0
- package/dist/orchestrator/run-spec.d.ts +154 -0
- package/dist/orchestrator/run-spec.js +11 -0
- package/dist/server.d.ts +21 -2
- package/dist/server.js +543 -187
- package/dist/sqlite-stores.d.ts +46 -6
- package/dist/sqlite-stores.js +292 -33
- package/dist/stores.d.ts +0 -36
- package/dist/stores.js +0 -27
- package/dist/testing.d.ts +5 -4
- package/dist/testing.js +5 -4
- package/package.json +7 -6
- package/scripts/agentchat-integrate.mjs +853 -853
- package/src/checkpoint-store.ts +122 -0
- package/src/components.ts +66 -0
- package/src/extension-admission.ts +131 -0
- package/src/flows.ts +244 -0
- package/src/ledger.ts +171 -0
- package/src/main.ts +16 -2
- package/src/manifest.ts +14 -1
- package/src/orchestrator/engine.ts +134 -0
- package/src/orchestrator/engines/fake.ts +330 -0
- package/src/orchestrator/engines/taskflow.ts +626 -0
- package/src/orchestrator/run-spec.ts +171 -0
- package/src/server.ts +553 -183
- package/src/session.ts +689 -689
- package/src/sqlite-stores.ts +343 -35
- package/src/stores.ts +0 -41
- package/src/testing.ts +4 -4
package/src/sqlite-stores.ts
CHANGED
|
@@ -4,7 +4,11 @@ import type { FileEntry } from "@earendil-works/pi-coding-agent";
|
|
|
4
4
|
import type { FeedbackEntry, FeedbackKind, FeedbackRating, FeedbackStatus, SessionSummary, TokenUsage } from "@njuptlzf/agent-chat-protocol";
|
|
5
5
|
import { DEFAULT_PERSONA_ID } from "@njuptlzf/agent-chat-protocol";
|
|
6
6
|
import { assertMembershipWrite, assertProductId, summarizeHistory, quotaBlockedReason } from "./stores.ts";
|
|
7
|
-
import type { AccountStateStore, AuditEvent, AuditQuery, AuditRow, AuditStore, BillingAdjustOpts, BillingEntry, BillingOrder, BillingStore, NewOrderMeta, HistoryStore, MembershipStore, MemberPoolRow, MemberPoolStore,
|
|
7
|
+
import type { AccountStateStore, AuditEvent, AuditQuery, AuditRow, AuditStore, BillingAdjustOpts, BillingEntry, BillingOrder, BillingStore, NewOrderMeta, HistoryStore, MembershipStore, MemberPoolRow, MemberPoolStore, RunLogRecord, RunLogStore, RunUsageReport, RunUsageSummary } from "./stores.ts";
|
|
8
|
+
import type { Ledger, ReserveInput, ReserveOutcome, SettleInput, SettleOutcome } from "./ledger.ts";
|
|
9
|
+
import { RESERVATION_TTL_MS } from "./ledger.ts";
|
|
10
|
+
import type { CheckpointStore, RunCheckpoint, RunJobRow } from "./checkpoint-store.ts";
|
|
11
|
+
import type { PhaseCheckpoint } from "./orchestrator/run-spec.ts";
|
|
8
12
|
import { auditPageWindow } from "./stores.ts";
|
|
9
13
|
import { GENESIS_HASH, assertNoSensitiveKeys, canonicalize, rowHash, segmentsFrom, verifyChain } from "./audit.ts";
|
|
10
14
|
import type { VerifyResult } from "./audit.ts";
|
|
@@ -26,6 +30,10 @@ export function openDatabase(file: string, defaultScope = "", defaultPersona = D
|
|
|
26
30
|
const db = new DatabaseSync(file);
|
|
27
31
|
db.exec(`
|
|
28
32
|
PRAGMA journal_mode = WAL;
|
|
33
|
+
-- 跨进程并发写必设(2026-11-16 压测实锤):默认 0 时,第二个写事务拿不到锁
|
|
34
|
+
-- 立刻抛 SQLITE_BUSY——多副本部署下 8 个并发 reserve 有 75% 直接炸成 500。
|
|
35
|
+
-- WAL 下这只影响写事务之间排队(读不阻塞),reserve/settle 临界区都是毫秒级。
|
|
36
|
+
PRAGMA busy_timeout = 5000;
|
|
29
37
|
CREATE TABLE IF NOT EXISTS history (
|
|
30
38
|
user_id TEXT NOT NULL,
|
|
31
39
|
scope TEXT NOT NULL DEFAULT '',
|
|
@@ -48,11 +56,30 @@ export function openDatabase(file: string, defaultScope = "", defaultPersona = D
|
|
|
48
56
|
tokens INTEGER NOT NULL,
|
|
49
57
|
PRIMARY KEY (user_id, persona, day)
|
|
50
58
|
);
|
|
59
|
+
-- 预留-结算账本(M-14 W3)。**这是并发额度的唯一计数口径**:
|
|
60
|
+
-- 今日已结算的预留 = 今日已跑的 run;未过期的 reserved = 在飞。
|
|
61
|
+
-- 旧模型查 runs 表 + 事后累加,并发下必然超发,故不再作为上限依据。
|
|
62
|
+
CREATE TABLE IF NOT EXISTS quota_reservations (
|
|
63
|
+
operation_id TEXT PRIMARY KEY,
|
|
64
|
+
user_id TEXT NOT NULL,
|
|
65
|
+
persona TEXT NOT NULL,
|
|
66
|
+
day TEXT NOT NULL,
|
|
67
|
+
tokens INTEGER NOT NULL DEFAULT 0,
|
|
68
|
+
state TEXT NOT NULL,
|
|
69
|
+
created_at INTEGER NOT NULL,
|
|
70
|
+
expires_at INTEGER NOT NULL
|
|
71
|
+
);
|
|
72
|
+
CREATE INDEX IF NOT EXISTS quota_reservations_day ON quota_reservations (user_id, persona, day, state, expires_at);
|
|
51
73
|
CREATE TABLE IF NOT EXISTS runs (
|
|
52
74
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
53
75
|
user_id TEXT NOT NULL,
|
|
54
76
|
session_id TEXT NOT NULL,
|
|
55
77
|
product TEXT NOT NULL,
|
|
78
|
+
-- 编排层(M-14 W4):单 phase 的 run 恒为 NULL;多 phase 的 run 指向父 run。
|
|
79
|
+
-- 老库由下方幂等 ALTER 补列,值留空(= 都是单 phase 时代的行,语义正确)。
|
|
80
|
+
parent_run_id TEXT,
|
|
81
|
+
-- Run 六态里的终态;老行按 error 有无回填(见迁移)。
|
|
82
|
+
state TEXT,
|
|
56
83
|
started_at TEXT NOT NULL,
|
|
57
84
|
duration_ms INTEGER NOT NULL,
|
|
58
85
|
tool_rounds INTEGER NOT NULL,
|
|
@@ -62,6 +89,38 @@ export function openDatabase(file: string, defaultScope = "", defaultPersona = D
|
|
|
62
89
|
error TEXT
|
|
63
90
|
);
|
|
64
91
|
CREATE INDEX IF NOT EXISTS runs_user_day ON runs (user_id, started_at);
|
|
92
|
+
-- 编排层:一次 Run 的子会话记录(M-14 W4)。
|
|
93
|
+
-- 与 run_checkpoints 分工见 checkpoint-store.ts 的文件头(基数不同,不能合成一张)。
|
|
94
|
+
-- lease_until 让「写这条的进程还活着吗」可判定:过期即孤儿,可回收。
|
|
95
|
+
CREATE TABLE IF NOT EXISTS run_jobs (
|
|
96
|
+
run_id TEXT NOT NULL,
|
|
97
|
+
phase_id TEXT NOT NULL,
|
|
98
|
+
job_id TEXT NOT NULL,
|
|
99
|
+
state TEXT NOT NULL,
|
|
100
|
+
tokens INTEGER NOT NULL DEFAULT 0,
|
|
101
|
+
cost_usd REAL NOT NULL DEFAULT 0,
|
|
102
|
+
attempt INTEGER,
|
|
103
|
+
lease_until INTEGER,
|
|
104
|
+
updated_at INTEGER NOT NULL,
|
|
105
|
+
PRIMARY KEY (run_id, job_id)
|
|
106
|
+
);
|
|
107
|
+
CREATE INDEX IF NOT EXISTS run_jobs_run ON run_jobs (run_id, phase_id);
|
|
108
|
+
CREATE INDEX IF NOT EXISTS run_jobs_lease ON run_jobs (state, lease_until);
|
|
109
|
+
-- 编排层:已完成 phase 的产物(续跑数据源,I7)。usage 四列分开存,
|
|
110
|
+
-- 因为续跑要能按 input/output/cache 分别加总——只存 total 会把账算丢。
|
|
111
|
+
CREATE TABLE IF NOT EXISTS run_checkpoints (
|
|
112
|
+
run_id TEXT NOT NULL,
|
|
113
|
+
phase_id TEXT NOT NULL,
|
|
114
|
+
output TEXT NOT NULL,
|
|
115
|
+
input_tokens INTEGER NOT NULL DEFAULT 0,
|
|
116
|
+
output_tokens INTEGER NOT NULL DEFAULT 0,
|
|
117
|
+
cache_read INTEGER NOT NULL DEFAULT 0,
|
|
118
|
+
cache_write INTEGER NOT NULL DEFAULT 0,
|
|
119
|
+
total_tokens INTEGER NOT NULL DEFAULT 0,
|
|
120
|
+
cost_usd REAL NOT NULL DEFAULT 0,
|
|
121
|
+
at INTEGER NOT NULL,
|
|
122
|
+
PRIMARY KEY (run_id, phase_id)
|
|
123
|
+
);
|
|
65
124
|
CREATE TABLE IF NOT EXISTS billing_balance (
|
|
66
125
|
user_id TEXT PRIMARY KEY,
|
|
67
126
|
balance INTEGER NOT NULL DEFAULT 0
|
|
@@ -279,6 +338,19 @@ function migrateScope(db: DatabaseSync, defaultScope: string, defaultPersona: st
|
|
|
279
338
|
// 钱与权益加产品维度(membership/member_pool 重建 + 两处加列)。
|
|
280
339
|
// 迁移本身是幂等的:新库走 CREATE 已是新形状,这里直接跳过。
|
|
281
340
|
migrateProductDimension(db, defaultPersona);
|
|
341
|
+
|
|
342
|
+
// 编排层(M-14 W4):runs 补 parent_run_id / state 两列。
|
|
343
|
+
// **不重建表**:两列都不进主键、都可空,`ALTER TABLE ADD COLUMN` 就够(同 persona 那次的口径)。
|
|
344
|
+
// 老行 `parent_run_id` 留 NULL 是对的(那时都是单 phase,没有父 run);
|
|
345
|
+
// `state` 按 error 有无回填——那时只有成功/失败两种结局,回填成
|
|
346
|
+
// completed / failed 是**还原当时的真实语义**,不是编数据。
|
|
347
|
+
if (!columns("runs").includes("parent_run_id")) {
|
|
348
|
+
db.exec("ALTER TABLE runs ADD COLUMN parent_run_id TEXT");
|
|
349
|
+
}
|
|
350
|
+
if (!columns("runs").includes("state")) {
|
|
351
|
+
db.exec("ALTER TABLE runs ADD COLUMN state TEXT");
|
|
352
|
+
db.exec("UPDATE runs SET state = CASE WHEN error IS NULL THEN 'completed' ELSE 'failed' END WHERE state IS NULL");
|
|
353
|
+
}
|
|
282
354
|
}
|
|
283
355
|
|
|
284
356
|
/**
|
|
@@ -442,8 +514,18 @@ export class SqliteHistoryStore implements HistoryStore {
|
|
|
442
514
|
}
|
|
443
515
|
}
|
|
444
516
|
|
|
445
|
-
/**
|
|
446
|
-
|
|
517
|
+
/**
|
|
518
|
+
* 预留-结算账本,SQLite 实现(M-14 W3)。
|
|
519
|
+
*
|
|
520
|
+
* 并发正确性靠三件事:
|
|
521
|
+
* ① `BEGIN IMMEDIATE` 把「数在飞 + 插入预留」放进同一个写事务(SQLite 串行化写);
|
|
522
|
+
* ② **计数口径只认 `quota_reservations`**(`settled` = 今日已跑,未过期 `reserved` = 在飞)。
|
|
523
|
+
* 不再用「查 runs 表 + 事后累加」——那正是并发超发的来源;
|
|
524
|
+
* ③ `expires_at` 让崩溃留下的孤儿预留自动失效,不需要「启动时清理」这种会漏的逻辑。
|
|
525
|
+
*
|
|
526
|
+
* token 上限的能力边界见 `ledger.ts` 文件头:run 位精确,token 只能保证「到顶就不再开新 run」。
|
|
527
|
+
*/
|
|
528
|
+
export class SqliteLedger implements Ledger {
|
|
447
529
|
constructor(
|
|
448
530
|
private readonly db: DatabaseSync,
|
|
449
531
|
private readonly dailyTokens: number | undefined,
|
|
@@ -454,45 +536,144 @@ export class SqliteQuotaGate implements QuotaGate {
|
|
|
454
536
|
return this.now().toISOString().slice(0, 10);
|
|
455
537
|
}
|
|
456
538
|
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
if (caps?.runCap !== undefined && caps.runCap > 0) {
|
|
460
|
-
const runs = this.db
|
|
461
|
-
.prepare("SELECT COUNT(*) AS n FROM runs WHERE user_id = ? AND persona = ? AND substr(started_at, 1, 10) = ?")
|
|
462
|
-
.get(userId, persona, this.day()) as { n: number };
|
|
463
|
-
if (runs.n >= caps.runCap)
|
|
464
|
-
return {
|
|
465
|
-
ok: false,
|
|
466
|
-
reason: quotaBlockedReason("runs", { runCap: caps.runCap, tokenCap: caps?.tokenCap ?? this.dailyTokens }),
|
|
467
|
-
};
|
|
468
|
-
}
|
|
469
|
-
const tokenCap = caps?.tokenCap ?? this.dailyTokens;
|
|
470
|
-
if (tokenCap === undefined) return { ok: true };
|
|
539
|
+
/** 上限口径:今日已结算 + 未过期的在飞。 */
|
|
540
|
+
private countForCap(userId: string, persona: string, day: string, nowMs: number): number {
|
|
471
541
|
const row = this.db
|
|
472
|
-
.prepare(
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
542
|
+
.prepare(
|
|
543
|
+
`SELECT COUNT(*) AS n FROM quota_reservations
|
|
544
|
+
WHERE user_id = ? AND persona = ? AND day = ?
|
|
545
|
+
AND (state = 'settled' OR (state = 'reserved' AND expires_at > ?))`,
|
|
546
|
+
)
|
|
547
|
+
.get(userId, persona, day, nowMs) as { n: number };
|
|
548
|
+
return row.n;
|
|
477
549
|
}
|
|
478
550
|
|
|
479
|
-
|
|
480
|
-
const
|
|
481
|
-
.prepare(
|
|
482
|
-
|
|
483
|
-
|
|
551
|
+
private countSettled(userId: string, persona: string, day: string): number {
|
|
552
|
+
const row = this.db
|
|
553
|
+
.prepare(
|
|
554
|
+
`SELECT COUNT(*) AS n FROM quota_reservations
|
|
555
|
+
WHERE user_id = ? AND persona = ? AND day = ? AND state = 'settled'`,
|
|
556
|
+
)
|
|
557
|
+
.get(userId, persona, day) as { n: number };
|
|
558
|
+
return row.n;
|
|
559
|
+
}
|
|
560
|
+
|
|
561
|
+
private tokensUsed(userId: string, persona: string, day: string): number {
|
|
562
|
+
const row = this.db
|
|
484
563
|
.prepare("SELECT tokens FROM quota WHERE user_id = ? AND persona = ? AND day = ?")
|
|
485
|
-
.get(userId, persona,
|
|
486
|
-
return
|
|
564
|
+
.get(userId, persona, day) as { tokens: number } | undefined;
|
|
565
|
+
return row?.tokens ?? 0;
|
|
566
|
+
}
|
|
567
|
+
|
|
568
|
+
async reserve(input: ReserveInput): Promise<ReserveOutcome> {
|
|
569
|
+
const nowMs = this.now().getTime();
|
|
570
|
+
const day = this.day();
|
|
571
|
+
this.db.exec("BEGIN IMMEDIATE");
|
|
572
|
+
try {
|
|
573
|
+
// 幂等(I6):同一 operationId 重复投递返回同一预留,不重复占位。
|
|
574
|
+
const existing = this.db
|
|
575
|
+
.prepare("SELECT state, expires_at FROM quota_reservations WHERE operation_id = ?")
|
|
576
|
+
.get(input.operationId) as { state: string; expires_at: number } | undefined;
|
|
577
|
+
if (existing) {
|
|
578
|
+
this.db.exec("COMMIT");
|
|
579
|
+
if (existing.state === "reserved" && existing.expires_at > nowMs) {
|
|
580
|
+
return { ok: true, reservation: { operationId: input.operationId, expiresAt: existing.expires_at } };
|
|
581
|
+
}
|
|
582
|
+
return { ok: false, reason: "this operation already ran (duplicate request)" };
|
|
583
|
+
}
|
|
584
|
+
|
|
585
|
+
const runCap = input.caps?.runCap;
|
|
586
|
+
if (runCap !== undefined && runCap > 0) {
|
|
587
|
+
if (this.countForCap(input.userId, input.persona, day, nowMs) >= runCap) {
|
|
588
|
+
this.db.exec("ROLLBACK");
|
|
589
|
+
return {
|
|
590
|
+
ok: false,
|
|
591
|
+
reason: quotaBlockedReason("runs", { runCap, tokenCap: input.caps?.tokenCap ?? this.dailyTokens }),
|
|
592
|
+
};
|
|
593
|
+
}
|
|
594
|
+
}
|
|
595
|
+
|
|
596
|
+
const tokenCap = input.caps?.tokenCap ?? this.dailyTokens;
|
|
597
|
+
if (tokenCap !== undefined) {
|
|
598
|
+
const used = this.tokensUsed(input.userId, input.persona, day);
|
|
599
|
+
// 在飞预留的 tokens 目前恒为 0(一次 run 事前不可知)。保留这个求和,
|
|
600
|
+
// 是为了将来有了「单次 run 的 token 上限」时能直接生效(§6.1 待办 T3)。
|
|
601
|
+
const reserved = this.db
|
|
602
|
+
.prepare(
|
|
603
|
+
`SELECT COALESCE(SUM(tokens), 0) AS n FROM quota_reservations
|
|
604
|
+
WHERE user_id = ? AND persona = ? AND day = ? AND state = 'reserved' AND expires_at > ?`,
|
|
605
|
+
)
|
|
606
|
+
.get(input.userId, input.persona, day, nowMs) as { n: number };
|
|
607
|
+
if (used + reserved.n >= tokenCap) {
|
|
608
|
+
this.db.exec("ROLLBACK");
|
|
609
|
+
return { ok: false, reason: quotaBlockedReason("tokens", { runCap, tokenCap }) };
|
|
610
|
+
}
|
|
611
|
+
}
|
|
612
|
+
|
|
613
|
+
const expiresAt = nowMs + (input.ttlMs ?? RESERVATION_TTL_MS);
|
|
614
|
+
this.db
|
|
615
|
+
.prepare(
|
|
616
|
+
`INSERT INTO quota_reservations (operation_id, user_id, persona, day, tokens, state, created_at, expires_at)
|
|
617
|
+
VALUES (?, ?, ?, ?, 0, 'reserved', ?, ?)`,
|
|
618
|
+
)
|
|
619
|
+
.run(input.operationId, input.userId, input.persona, day, nowMs, expiresAt);
|
|
620
|
+
this.db.exec("COMMIT");
|
|
621
|
+
return { ok: true, reservation: { operationId: input.operationId, expiresAt } };
|
|
622
|
+
} catch (err) {
|
|
623
|
+
this.db.exec("ROLLBACK");
|
|
624
|
+
throw err;
|
|
625
|
+
}
|
|
626
|
+
}
|
|
627
|
+
|
|
628
|
+
async settle(input: SettleInput): Promise<SettleOutcome> {
|
|
629
|
+
const day = this.day();
|
|
630
|
+
this.db.exec("BEGIN IMMEDIATE");
|
|
631
|
+
try {
|
|
632
|
+
// 条件更新 = 幂等闸:只有 `reserved → settled` 的那一次会写 quota,
|
|
633
|
+
// 重复 settle 直接返回 already(不重复扣费)。
|
|
634
|
+
const res = this.db
|
|
635
|
+
.prepare(
|
|
636
|
+
`UPDATE quota_reservations SET state = 'settled', tokens = ?
|
|
637
|
+
WHERE operation_id = ? AND state = 'reserved'`,
|
|
638
|
+
)
|
|
639
|
+
.run(input.tokens, input.operationId) as { changes: number };
|
|
640
|
+
if (res.changes === 0) {
|
|
641
|
+
this.db.exec("COMMIT");
|
|
642
|
+
return "already";
|
|
643
|
+
}
|
|
644
|
+
if (input.tokens > 0) {
|
|
645
|
+
this.db
|
|
646
|
+
.prepare(
|
|
647
|
+
`INSERT INTO quota (user_id, persona, day, tokens) VALUES (?, ?, ?, ?)
|
|
648
|
+
ON CONFLICT (user_id, persona, day) DO UPDATE SET tokens = tokens + excluded.tokens`,
|
|
649
|
+
)
|
|
650
|
+
.run(input.userId, input.persona, day, input.tokens);
|
|
651
|
+
}
|
|
652
|
+
this.db.exec("COMMIT");
|
|
653
|
+
return "settled";
|
|
654
|
+
} catch (err) {
|
|
655
|
+
this.db.exec("ROLLBACK");
|
|
656
|
+
throw err;
|
|
657
|
+
}
|
|
487
658
|
}
|
|
488
659
|
|
|
489
|
-
async
|
|
660
|
+
async release(operationId: string): Promise<void> {
|
|
490
661
|
this.db
|
|
491
|
-
.prepare(
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
662
|
+
.prepare(`UPDATE quota_reservations SET state = 'released' WHERE operation_id = ? AND state = 'reserved'`)
|
|
663
|
+
.run(operationId);
|
|
664
|
+
}
|
|
665
|
+
|
|
666
|
+
async todayStats(userId: string, persona: string): Promise<{ runsToday: number; tokensToday: number }> {
|
|
667
|
+
const day = this.day();
|
|
668
|
+
// 展示口径只算已结算的(在飞不算「今天跑过」)。
|
|
669
|
+
return { runsToday: this.countSettled(userId, persona, day), tokensToday: this.tokensUsed(userId, persona, day) };
|
|
670
|
+
}
|
|
671
|
+
|
|
672
|
+
async sweepExpired(now = this.now().getTime()): Promise<number> {
|
|
673
|
+
const res = this.db
|
|
674
|
+
.prepare(`UPDATE quota_reservations SET state = 'expired' WHERE state = 'reserved' AND expires_at <= ?`)
|
|
675
|
+
.run(now) as { changes: number };
|
|
676
|
+
return res.changes;
|
|
496
677
|
}
|
|
497
678
|
}
|
|
498
679
|
|
|
@@ -1038,3 +1219,130 @@ export class SqliteAudit implements AuditStore {
|
|
|
1038
1219
|
return ids.length;
|
|
1039
1220
|
}
|
|
1040
1221
|
}
|
|
1222
|
+
|
|
1223
|
+
/**
|
|
1224
|
+
* 持久化 checkpoint 与 run_jobs(M-14 / R-010 W4)。
|
|
1225
|
+
*
|
|
1226
|
+
* 两条写入纪律,与 `SqliteLedger` 同源:
|
|
1227
|
+
* ① `run_checkpoints` 用 `INSERT … ON CONFLICT DO UPDATE`——续跑成功后产物要更新,
|
|
1228
|
+
* 但同一 `(run_id, phase_id)` 永远只有一行;
|
|
1229
|
+
* ② `run_jobs` 的租约回收用**条件写**(`WHERE state='running' AND lease_until<=?`),
|
|
1230
|
+
* 绝不先读后写:并发两个进程同时回收,条件更新让它们天然只生效一次。
|
|
1231
|
+
*/
|
|
1232
|
+
export class SqliteCheckpointStore implements CheckpointStore {
|
|
1233
|
+
constructor(
|
|
1234
|
+
private readonly db: DatabaseSync,
|
|
1235
|
+
private readonly now: () => number = () => Date.now(),
|
|
1236
|
+
) {}
|
|
1237
|
+
|
|
1238
|
+
async saveJob(runId: string, job: RunJobRow): Promise<void> {
|
|
1239
|
+
const now = this.now();
|
|
1240
|
+
this.db
|
|
1241
|
+
.prepare(
|
|
1242
|
+
`INSERT INTO run_jobs (run_id, phase_id, job_id, state, tokens, cost_usd, attempt, lease_until, updated_at)
|
|
1243
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
|
|
1244
|
+
ON CONFLICT (run_id, job_id) DO UPDATE SET
|
|
1245
|
+
state = excluded.state,
|
|
1246
|
+
tokens = excluded.tokens,
|
|
1247
|
+
cost_usd = excluded.cost_usd,
|
|
1248
|
+
attempt = excluded.attempt,
|
|
1249
|
+
lease_until = excluded.lease_until,
|
|
1250
|
+
updated_at = excluded.updated_at`,
|
|
1251
|
+
)
|
|
1252
|
+
.run(
|
|
1253
|
+
runId,
|
|
1254
|
+
job.phaseId,
|
|
1255
|
+
job.jobId,
|
|
1256
|
+
job.state,
|
|
1257
|
+
job.usage.total,
|
|
1258
|
+
job.usage.costUsd,
|
|
1259
|
+
job.attempt ?? null,
|
|
1260
|
+
job.leaseUntil ?? null,
|
|
1261
|
+
now,
|
|
1262
|
+
);
|
|
1263
|
+
}
|
|
1264
|
+
|
|
1265
|
+
async savePhase(checkpoint: PhaseCheckpoint & { runId: string }): Promise<void> {
|
|
1266
|
+
const u = checkpoint.usage;
|
|
1267
|
+
this.db
|
|
1268
|
+
.prepare(
|
|
1269
|
+
`INSERT INTO run_checkpoints (run_id, phase_id, output, input_tokens, output_tokens, cache_read, cache_write, total_tokens, cost_usd, at)
|
|
1270
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
|
1271
|
+
ON CONFLICT (run_id, phase_id) DO UPDATE SET
|
|
1272
|
+
output = excluded.output,
|
|
1273
|
+
input_tokens = excluded.input_tokens,
|
|
1274
|
+
output_tokens = excluded.output_tokens,
|
|
1275
|
+
cache_read = excluded.cache_read,
|
|
1276
|
+
cache_write = excluded.cache_write,
|
|
1277
|
+
total_tokens = excluded.total_tokens,
|
|
1278
|
+
cost_usd = excluded.cost_usd,
|
|
1279
|
+
at = excluded.at`,
|
|
1280
|
+
)
|
|
1281
|
+
.run(
|
|
1282
|
+
checkpoint.runId,
|
|
1283
|
+
checkpoint.phaseId,
|
|
1284
|
+
checkpoint.output,
|
|
1285
|
+
u.input,
|
|
1286
|
+
u.output,
|
|
1287
|
+
u.cacheRead,
|
|
1288
|
+
u.cacheWrite,
|
|
1289
|
+
u.total,
|
|
1290
|
+
u.costUsd,
|
|
1291
|
+
checkpoint.at,
|
|
1292
|
+
);
|
|
1293
|
+
}
|
|
1294
|
+
|
|
1295
|
+
async load(runId: string): Promise<RunCheckpoint | undefined> {
|
|
1296
|
+
const rows = this.db
|
|
1297
|
+
.prepare("SELECT * FROM run_checkpoints WHERE run_id = ?")
|
|
1298
|
+
.all(runId) as Array<Record<string, string | number>>;
|
|
1299
|
+
if (rows.length === 0) return undefined;
|
|
1300
|
+
const phases: Record<string, PhaseCheckpoint> = {};
|
|
1301
|
+
for (const r of rows) {
|
|
1302
|
+
phases[String(r.phase_id)] = {
|
|
1303
|
+
phaseId: String(r.phase_id),
|
|
1304
|
+
output: String(r.output),
|
|
1305
|
+
usage: {
|
|
1306
|
+
input: Number(r.input_tokens),
|
|
1307
|
+
output: Number(r.output_tokens),
|
|
1308
|
+
cacheRead: Number(r.cache_read),
|
|
1309
|
+
cacheWrite: Number(r.cache_write),
|
|
1310
|
+
total: Number(r.total_tokens),
|
|
1311
|
+
costUsd: Number(r.cost_usd),
|
|
1312
|
+
},
|
|
1313
|
+
at: Number(r.at),
|
|
1314
|
+
};
|
|
1315
|
+
}
|
|
1316
|
+
return { runId, phases };
|
|
1317
|
+
}
|
|
1318
|
+
|
|
1319
|
+
async jobs(runId: string): Promise<RunJobRow[]> {
|
|
1320
|
+
const rows = this.db
|
|
1321
|
+
.prepare("SELECT * FROM run_jobs WHERE run_id = ? ORDER BY updated_at ASC")
|
|
1322
|
+
.all(runId) as Array<Record<string, string | number | null>>;
|
|
1323
|
+
return rows.map((r) => ({
|
|
1324
|
+
runId: String(r.run_id),
|
|
1325
|
+
phaseId: String(r.phase_id),
|
|
1326
|
+
jobId: String(r.job_id),
|
|
1327
|
+
state: String(r.state),
|
|
1328
|
+
usage: {
|
|
1329
|
+
input: 0,
|
|
1330
|
+
output: 0,
|
|
1331
|
+
cacheRead: 0,
|
|
1332
|
+
cacheWrite: 0,
|
|
1333
|
+
total: Number(r.tokens),
|
|
1334
|
+
costUsd: Number(r.cost_usd),
|
|
1335
|
+
},
|
|
1336
|
+
...(r.attempt == null ? {} : { attempt: Number(r.attempt) }),
|
|
1337
|
+
...(r.lease_until == null ? {} : { leaseUntil: Number(r.lease_until) }),
|
|
1338
|
+
}));
|
|
1339
|
+
}
|
|
1340
|
+
|
|
1341
|
+
async reclaimOrphans(runId: string, now = this.now()): Promise<number> {
|
|
1342
|
+
// 条件写,不先读后写:两个进程同时回收也只生效一次(第二次匹配 0 行)。
|
|
1343
|
+
const res = this.db
|
|
1344
|
+
.prepare("UPDATE run_jobs SET state = 'blocked', updated_at = ? WHERE run_id = ? AND state = 'running' AND lease_until IS NOT NULL AND lease_until <= ?")
|
|
1345
|
+
.run(now, runId, now);
|
|
1346
|
+
return Number(res.changes);
|
|
1347
|
+
}
|
|
1348
|
+
}
|
package/src/stores.ts
CHANGED
|
@@ -170,28 +170,11 @@ export class MemoryHistoryStore implements HistoryStore {
|
|
|
170
170
|
}
|
|
171
171
|
}
|
|
172
172
|
|
|
173
|
-
export type QuotaDecision = { ok: true } | { ok: false; reason: string };
|
|
174
|
-
|
|
175
|
-
/** 按角色现算的当日上限;缺省项 = 沿用装配时的全局值/不限。 */
|
|
176
173
|
export interface QuotaCaps {
|
|
177
174
|
runCap?: number;
|
|
178
175
|
tokenCap?: number;
|
|
179
176
|
}
|
|
180
177
|
|
|
181
|
-
export interface QuotaGate {
|
|
182
|
-
/**
|
|
183
|
-
* `persona` 分账:各个人格各算各的,互不吃对方的额度。
|
|
184
|
-
*
|
|
185
|
-
* **必填**(通用化):core 不设隐式默认键。账本键就是人格 id,谁省略谁就写进
|
|
186
|
-
* 另一本账——实测过,改默认值会让老数据整片读不出来。调用方一律显式传
|
|
187
|
-
* 产品 manifest 声明的 id(默认助手用 `product.defaultPersona`)。
|
|
188
|
-
*/
|
|
189
|
-
check(userId: string, caps: QuotaCaps | undefined, persona: string): Promise<QuotaDecision>;
|
|
190
|
-
record(userId: string, usage: TokenUsage, persona: string): Promise<void>;
|
|
191
|
-
/** whoami 的配额展示:今日已用轮数/token(默认助手那本账)。 */
|
|
192
|
-
todayStats?(userId: string, persona: string): Promise<{ runsToday: number; tokensToday: number }>;
|
|
193
|
-
}
|
|
194
|
-
|
|
195
178
|
/** 万位读法:5000000 → 「500 万」(撞闸文案与权益卡共用,别在 UI 里二次造轮子)。 */
|
|
196
179
|
export function fmtTokensWan(n: number): string {
|
|
197
180
|
return n >= 10000 ? `${(n / 10000).toLocaleString("zh-CN")} 万` : String(n);
|
|
@@ -213,30 +196,6 @@ export function quotaBlockedReason(hit: "runs" | "tokens", caps: QuotaCaps): str
|
|
|
213
196
|
return `今日用量已达上限${both ? `(${both})` : ""}。触顶通常来自异常使用(脚本刷量或失控会话),次日自动重置;如认为判误请联系管理员`;
|
|
214
197
|
}
|
|
215
198
|
|
|
216
|
-
/** Per-user token budget per day. Replace with a database-backed gate for real billing. */
|
|
217
|
-
export class DailyTokenQuota implements QuotaGate {
|
|
218
|
-
private used = new Map<string, number>();
|
|
219
|
-
constructor(private readonly dailyTokens: number | undefined) {}
|
|
220
|
-
|
|
221
|
-
private key(userId: string, persona: string) {
|
|
222
|
-
return `${userId}\u0000${persona}\u0000${new Date().toISOString().slice(0, 10)}`;
|
|
223
|
-
}
|
|
224
|
-
async check(userId: string, caps: QuotaCaps | undefined, persona: string): Promise<QuotaDecision> {
|
|
225
|
-
// 角色 token 上限优先于装配值(会员兜底更高时公平阀随之抬)。
|
|
226
|
-
const cap = caps?.tokenCap ?? this.dailyTokens;
|
|
227
|
-
if (cap === undefined) return { ok: true };
|
|
228
|
-
const used = this.used.get(this.key(userId, persona)) ?? 0;
|
|
229
|
-
return used >= cap ? { ok: false, reason: quotaBlockedReason("tokens", { runCap: caps?.runCap, tokenCap: cap }) } : { ok: true };
|
|
230
|
-
}
|
|
231
|
-
async todayStats(userId: string, persona: string) {
|
|
232
|
-
// 内存实现没有 runs 表概念——轮数据在配额闸之外,报 0 轮如实(开发态展示)。
|
|
233
|
-
return { runsToday: 0, tokensToday: this.used.get(this.key(userId, persona)) ?? 0 };
|
|
234
|
-
}
|
|
235
|
-
async record(userId: string, usage: TokenUsage, persona: string) {
|
|
236
|
-
const k = this.key(userId, persona);
|
|
237
|
-
this.used.set(k, (this.used.get(k) ?? 0) + usage.total);
|
|
238
|
-
}
|
|
239
|
-
}
|
|
240
199
|
|
|
241
200
|
/**
|
|
242
201
|
* 会员有效期。会员是**时间态**不是余额态——「充钱是充钱,会员是
|
package/src/testing.ts
CHANGED
|
@@ -19,8 +19,8 @@
|
|
|
19
19
|
* ## 为什么不做成"产品自造替身"
|
|
20
20
|
*
|
|
21
21
|
* 曾考虑让产品自己写内存 store。否决理由:`createChatServer` 只认平台接口
|
|
22
|
-
* (`HistoryStore`/`
|
|
23
|
-
* 行为——尤其是 `
|
|
22
|
+
* (`HistoryStore`/`Ledger`/…),产品手写的替身必须逐字段对齐这些接口的
|
|
23
|
+
* 行为——尤其是 `SqliteLedger` 的"今日计数"、`SqliteMembership` 的幂等
|
|
24
24
|
* 写入这类带语义的。两侧各写一份,行为漂移时测试全绿而生产出问题。
|
|
25
25
|
* 复用平台的实现,测试才验的是**真代码路径**。
|
|
26
26
|
*
|
|
@@ -32,8 +32,8 @@
|
|
|
32
32
|
*/
|
|
33
33
|
|
|
34
34
|
// ── store 实现(内存版:测试首选,无 IO) ──────────────────────────────
|
|
35
|
+
export { MemoryLedger } from "./ledger.ts";
|
|
35
36
|
export {
|
|
36
|
-
DailyTokenQuota,
|
|
37
37
|
MemoryAccountState,
|
|
38
38
|
MemoryAudit,
|
|
39
39
|
MemoryBilling,
|
|
@@ -54,7 +54,7 @@ export {
|
|
|
54
54
|
SqliteHistoryStore,
|
|
55
55
|
SqliteMemberPool,
|
|
56
56
|
SqliteMembership,
|
|
57
|
-
|
|
57
|
+
SqliteLedger,
|
|
58
58
|
} from "./sqlite-stores.ts";
|
|
59
59
|
|
|
60
60
|
// ── 授权 ────────────────────────────────────────────────────────────────
|