Skip to content

Commit 86ee410

Browse files
benchgaojunran
authored andcommitted
L1 history cache: incremental load + projector invalidation
1 parent f0ed4f6 commit 86ee410

6 files changed

Lines changed: 447 additions & 16 deletions

File tree

‎packages/core/bench-history.ts‎

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
/**
2+
* 微基准 v3:opencode v2.0.10 生产路径分段计时(真实会话 DB 副本)
3+
* A: drizzle 全量读(messageEntries 查询原样,含执行+行映射+JSON.parse)
4+
* B: Effect Schema 解码(decodeMessageRow 复刻,Effect.forEach 顺序执行)
5+
* A+B: SessionHistory.load;A+B+tx: 事务包裹(entriesForRunner 等价)
6+
* C: toLLMMessages(同步);D: JSON.stringify(converted)(同步)
7+
* any 使用说明:一次性基准脚本,只关心运行时形状
8+
*/
9+
import { and, asc, eq } from "drizzle-orm"
10+
import { Effect, Layer, Schema } from "effect"
11+
import { Database } from "./src/database/database.js"
12+
import { SessionHistory } from "./src/session/history.js"
13+
import { SessionMessageTable } from "./src/session/sql.js"
14+
import { SessionMessage } from "@opencode/schema/session-message"
15+
import { toLLMMessages } from "./src/session/runner/to-llm-message.js"
16+
17+
const DB_PATH = process.argv[2] ?? "/tmp/memstudy/bench.db"
18+
19+
const program = Effect.gen(function* () {
20+
const { db } = yield* Database.Service
21+
const first = (yield* db.select().from(SessionMessageTable).limit(1)) as any[]
22+
const sessionID = first[0].session_id
23+
24+
const readRows = () =>
25+
db
26+
.select()
27+
.from(SessionMessageTable)
28+
.where(and(eq(SessionMessageTable.session_id, sessionID)))
29+
.orderBy(asc(SessionMessageTable.seq))
30+
.all()
31+
const decode = Schema.decodeUnknownEffect(SessionMessage.Info)
32+
const decodeAll = (rs: any[]) =>
33+
Effect.forEach(rs, (row: any) => decode({ ...row.data, id: row.id, type: row.type }))
34+
const loadOnce = () => SessionHistory.load(db, sessionID, "local")
35+
const loadTx = () =>
36+
(db as any)
37+
.transaction(() => SessionHistory.load(db, sessionID, "local"))
38+
.pipe(Effect.orDie)
39+
40+
// 预热
41+
let rows: any[] = []
42+
let decoded: any[] = []
43+
let model: any
44+
for (let i = 0; i < 20; i++) {
45+
rows = (yield* readRows()) as any[]
46+
decoded = yield* decodeAll(rows)
47+
model = decoded.find((m) => m.type === "assistant")?.model
48+
}
49+
const providerKey = String(model.providerID)
50+
const converted = toLLMMessages(decoded, model, providerKey)
51+
const N = rows.length
52+
53+
const sizeOf = (v: unknown) => JSON.stringify(v).length
54+
console.log(`rows=${N} data=${(rows.reduce((s, r) => s + JSON.stringify(r.data).length, 0) / 1024) | 0}KB model=${model.providerID}/${model.id}`)
55+
console.log(`输出体积: A(rows)=${(sizeOf(rows) / 1024) | 0}KB B(decoded)=${(sizeOf(decoded) / 1024) | 0}KB C(converted)=${(sizeOf(converted) / 1024) | 0}KB`)
56+
57+
const results: Record<string, number> = {}
58+
const report = (name: string, mean: number, iters: number, t: number[]) => {
59+
results[name] = mean
60+
console.log(`${name}: mean=${mean.toFixed(3)}ms p50=${t[(iters / 2) | 0].toFixed(3)}ms 每行=${((mean * 1000) / N).toFixed(1)}µs`)
61+
}
62+
// Effect 版:yield* 真执行,fiber 开销与生产路径一致
63+
function benchE(name: string, eff: () => any, iters: number) {
64+
return Effect.gen(function* () {
65+
for (let i = 0; i < 10; i++) yield* eff()
66+
const t: number[] = []
67+
for (let i = 0; i < iters; i++) {
68+
const s = performance.now()
69+
yield* eff()
70+
t.push(performance.now() - s)
71+
}
72+
t.sort((a, b) => a - b)
73+
report(name, t.reduce((a, b) => a + b, 0) / t.length, iters, t)
74+
})
75+
}
76+
// 同步版:给生产里就是同步调用的阶段(toLLMMessages / stringify)
77+
function bench(name: string, fn: () => unknown, iters: number) {
78+
for (let i = 0; i < 10; i++) void fn()
79+
const t: number[] = []
80+
for (let i = 0; i < iters; i++) {
81+
const s = performance.now()
82+
fn()
83+
t.push(performance.now() - s)
84+
}
85+
t.sort((a, b) => a - b)
86+
report(name, t.reduce((a, b) => a + b, 0) / t.length, iters, t)
87+
}
88+
89+
console.log(`\n=== 全量历史 N=${N} ===`)
90+
yield* benchE("A 全量读(drizzle+parse)", () => readRows(), 200)
91+
yield* benchE("B Schema解码(forEach)", () => decodeAll(rows), 200)
92+
yield* benchE("A+B load生产路径", () => loadOnce(), 200)
93+
yield* benchE("A+B+tx 事务包裹", () => loadTx(), 150)
94+
bench("C toLLMMessages", () => toLLMMessages(decoded, model, providerKey), 200)
95+
bench("D stringify(converted)", () => JSON.stringify(converted), 200)
96+
97+
// 线性验证:只取前一半行
98+
const half = N >> 1
99+
const readRowsHalf = () =>
100+
db
101+
.select()
102+
.from(SessionMessageTable)
103+
.where(and(eq(SessionMessageTable.session_id, sessionID)))
104+
.orderBy(asc(SessionMessageTable.seq))
105+
.limit(half)
106+
.all()
107+
console.log(`\n=== 子集 N=${half}(验证每行成本恒定) ===`)
108+
yield* benchE("A/2 全量读", () => readRowsHalf(), 200)
109+
yield* benchE("B/2 解码", () => decodeAll(rows.slice(0, half)), 200)
110+
bench("C/2 转换", () => toLLMMessages(decoded.slice(0, half), model, providerKey), 200)
111+
112+
const ab = results["A+B load生产路径"]
113+
const c = results["C toLLMMessages"]
114+
const d = results["D stringify(converted)"]
115+
console.log(`\n=== 汇总(每步, N=${N}) ===`)
116+
console.log(`L1 目标(A+B)=${ab.toFixed(2)}ms | L2 目标(C)=${c.toFixed(2)}ms | 剩余地板(D)=${d.toFixed(2)}ms | 链合计=${(ab + c + d).toFixed(2)}ms`)
117+
console.log(`占比: L1=${((ab / (ab + c + d)) * 100).toFixed(0)}% L2=${((c / (ab + c + d)) * 100).toFixed(0)}% 地板=${((d / (ab + c + d)) * 100).toFixed(0)}%`)
118+
})
119+
120+
await Effect.runPromise(
121+
Effect.gen(function* () {
122+
const context = yield* Layer.build(Database.layer({ path: DB_PATH }))
123+
yield* program.pipe(Effect.provide(context))
124+
}).pipe(Effect.scoped),
125+
)
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import type { SessionMessage } from "./message.js"
2+
3+
/**
4+
* L1 history cache: decoded session messages held between LLM steps so each
5+
* step reads only rows appended since the last read (`seq > watermark`)
6+
* instead of re-reading and re-decoding the whole transcript.
7+
*
8+
* Process-local by design. The projector (the only writer besides transfer
9+
* import and the one-time v1 migration, which runs before any cache exists)
10+
* runs in the same process as the session runners and drops entries on every
11+
* rewrite of an already-read row, so a cache entry can never go stale:
12+
*
13+
* - UPDATE of a row at or below the high-water mark (projector `updateMessage`)
14+
* - revert committed (rows are deleted)
15+
* - compaction ended (the boundary moves)
16+
* - transfer import (direct INSERT outside the projector)
17+
*
18+
* Aliasing invariant: entries returned from this cache must never be handed to
19+
* code that mutates messages in place. Today the runner converts history
20+
* through `toLLMMessages` before any plugin hook sees it, and that conversion
21+
* builds fresh objects, so cached `SessionMessage.Info` values are never
22+
* aliased into mutable request state.
23+
*/
24+
25+
export interface Entry {
26+
/** seq of the newest row included; cached rows span the boundary in force when the entry was built up to here. */
27+
readonly watermark: number
28+
/** decoded entries in seq order */
29+
readonly entries: ReadonlyArray<{ readonly seq: number; readonly message: SessionMessage.Info }>
30+
}
31+
32+
// sessionID -> boundary key -> entry. One entry per boundary per session; a
33+
// boundary change (new compaction) replaces the entry for its key.
34+
const cache = new Map<string, Map<string, Entry>>()
35+
36+
export const drop = (sessionID: string) => {
37+
cache.delete(sessionID)
38+
}
39+
40+
/** Highest cached seq for a session across all boundaries, or -1 when nothing is cached. */
41+
export const highWater = (sessionID: string): number => {
42+
let max = -1
43+
for (const entry of cache.get(sessionID)?.values() ?? []) if (entry.watermark > max) max = entry.watermark
44+
return max
45+
}
46+
47+
export const get = (sessionID: string, boundaryKey: string): Entry | undefined =>
48+
cache.get(sessionID)?.get(boundaryKey)
49+
50+
export const put = (sessionID: string, boundaryKey: string, entry: Entry) => {
51+
let perSession = cache.get(sessionID)
52+
if (!perSession) {
53+
perSession = new Map()
54+
cache.set(sessionID, perSession)
55+
}
56+
perSession.set(boundaryKey, entry)
57+
}
58+
59+
export * as HistoryCache from "./history-cache.js"

‎packages/core/src/session/history.ts‎

Lines changed: 59 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
import { and, asc, desc, eq, gte, or, sql } from "drizzle-orm"
1+
import { and, asc, desc, eq, gt, gte, or, sql } from "drizzle-orm"
22
import { Effect, Schema } from "effect"
33
import { Database } from "../database/database.js"
4+
import { HistoryCache } from "./history-cache.js"
45
import { MessageDecodeError } from "./error.js"
56
import { SessionMessage } from "./message.js"
67
import { SessionSchema } from "./schema.js"
@@ -75,11 +76,61 @@ export const decodeMessageRow = (row: typeof SessionMessageTable.$inferSelect) =
7576
),
7677
)
7778

79+
const decodeRows = (rows: Array<typeof SessionMessageTable.$inferSelect>) =>
80+
Effect.forEach(rows, (row) =>
81+
decodeMessageRow(row).pipe(Effect.map((message) => ({ seq: row.seq, message }))),
82+
)
83+
84+
const filterEntries = (
85+
entries: ReadonlyArray<{ seq: number; message: SessionMessage.Info }>,
86+
boundary: Boundary,
87+
) => {
88+
// Re-expansion may cross a native checkpoint whose completion already advanced the instruction
89+
// epoch: the baseline supersedes the chronological updates before it. Forks seed their baseline
90+
// at sequence 0 but retain parent sequences, so the copied checkpoint still retires them.
91+
const native = entries.findLast((entry) => SessionProviderContext.isCheckpoint(entry.message))
92+
// Skipped native checkpoints are not textual summaries. Their original transcript remains available.
93+
return entries.filter(
94+
(entry) =>
95+
!(entry.message.type === "system" && native && entry.seq < native.seq) && replayable(entry.message, boundary),
96+
)
97+
}
98+
99+
const boundaryKey = (boundary: Boundary) => JSON.stringify(boundary)
100+
78101
const messageEntries = Effect.fnUntraced(function* (
79102
db: DatabaseService,
80103
sessionID: SessionSchema.ID,
81104
boundary: Boundary,
105+
useCache = false,
82106
) {
107+
const key = boundaryKey(boundary)
108+
// L1 fast path: only rows appended since the last read. The projector drops
109+
// the cache whenever an already-read row is rewritten or the boundary moves,
110+
// so a hit here is by construction up to date.
111+
const cached = useCache ? HistoryCache.get(sessionID, key) : undefined
112+
if (cached) {
113+
const rows = yield* db
114+
.select()
115+
.from(SessionMessageTable)
116+
.where(and(eq(SessionMessageTable.session_id, sessionID), gt(SessionMessageTable.seq, cached.watermark)))
117+
.orderBy(asc(SessionMessageTable.seq))
118+
.all()
119+
.pipe(Effect.orDie)
120+
// A compaction completing after the watermark moves the read boundary even
121+
// though its row is brand new. Fall through to the full read so the new
122+
// boundary is honored instead of serving the stale pre-compaction range.
123+
if (!rows.some((row) => row.type === "compaction")) {
124+
const appended = yield* decodeRows(rows)
125+
const entries = appended.length === 0 ? cached.entries : [...cached.entries, ...appended]
126+
if (appended.length > 0) {
127+
const last = appended[appended.length - 1]
128+
HistoryCache.put(sessionID, key, { watermark: last.seq, entries })
129+
}
130+
return filterEntries(entries, boundary)
131+
}
132+
HistoryCache.drop(sessionID)
133+
}
83134
const compaction = yield* latestCompaction(db, sessionID, boundary)
84135
const rows = yield* db
85136
.select()
@@ -93,18 +144,12 @@ const messageEntries = Effect.fnUntraced(function* (
93144
.orderBy(asc(SessionMessageTable.seq))
94145
.all()
95146
.pipe(Effect.orDie)
96-
const entries = yield* Effect.forEach(rows, (row) =>
97-
decodeMessageRow(row).pipe(Effect.map((message) => ({ seq: row.seq, message }))),
98-
)
99-
// Re-expansion may cross a native checkpoint whose completion already advanced the instruction
100-
// epoch: the baseline supersedes the chronological updates before it. Forks seed their baseline
101-
// at sequence 0 but retain parent sequences, so the copied checkpoint still retires them.
102-
const native = entries.findLast((entry) => SessionProviderContext.isCheckpoint(entry.message))
103-
// Skipped native checkpoints are not textual summaries. Their original transcript remains available.
104-
return entries.filter(
105-
(entry) =>
106-
!(entry.message.type === "system" && native && entry.seq < native.seq) && replayable(entry.message, boundary),
107-
)
147+
const entries = yield* decodeRows(rows)
148+
if (useCache) {
149+
const last = entries[entries.length - 1]
150+
HistoryCache.put(sessionID, key, { watermark: last ? last.seq : -1, entries })
151+
}
152+
return filterEntries(entries, boundary)
108153
})
109154

110155
export const load = Effect.fn("SessionHistory.load")(function* (
@@ -124,7 +169,7 @@ export const entriesForRunner = Effect.fn("SessionHistory.entriesForRunner")(fun
124169
return yield* db
125170
.transaction(() =>
126171
Effect.gen(function* () {
127-
const messages = yield* messageEntries(db, sessionID, boundary)
172+
const messages = yield* messageEntries(db, sessionID, boundary, true)
128173
return {
129174
initial: yield* InstructionState.initial(db, sessionID, instructions),
130175
entries: messages,

‎packages/core/src/session/projector.ts‎

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { makeGlobalNode } from "@opencode/util/effect/app-node"
99
import { Agent } from "@opencode/schema/agent"
1010
import { Model } from "@opencode/schema/model"
1111
import { SessionEvent } from "./event.js"
12+
import { HistoryCache } from "./history-cache.js"
1213
import { SessionMessage } from "./message.js"
1314
import { SessionMessageUpdater } from "./message-updater.js"
1415
import { SessionInbox } from "./inbox.js"
@@ -241,8 +242,18 @@ function run(db: DatabaseService, event: MessageEvent) {
241242
eq(SessionMessageTable.session_id, event.data.sessionID),
242243
),
243244
)
244-
.run()
245-
.pipe(Effect.orDie)
245+
.returning({ seq: SessionMessageTable.seq })
246+
.get()
247+
.pipe(
248+
Effect.orDie,
249+
// L1 history cache: rewriting an already-read row invalidates the decoded cache.
250+
Effect.tap((row) => {
251+
if (row && row.seq <= HistoryCache.highWater(event.data.sessionID))
252+
HistoryCache.drop(event.data.sessionID)
253+
return Effect.void
254+
}),
255+
Effect.asVoid,
256+
)
246257
}
247258
const appendMessage = (message: SessionMessage.Info) => insertMessage(db, event, message)
248259
const adapter: SessionMessageUpdater.Adapter = {
@@ -709,6 +720,8 @@ const layer = Layer.effectDiscard(
709720
yield* bus.project(SessionEvent.Compaction.Started, (event) => run(db, event))
710721
yield* bus.project(SessionEvent.Compaction.Ended, (event) =>
711722
Effect.gen(function* () {
723+
// L1 history cache: the completed compaction moves the read boundary.
724+
HistoryCache.drop(event.data.sessionID)
712725
yield* run(db, event)
713726
yield* InstructionState.advanceEpoch(db, event.data.sessionID, event.durable.seq)
714727
}),
@@ -738,6 +751,8 @@ const layer = Layer.effectDiscard(
738751
)
739752
yield* bus.project(SessionEvent.RevertEvent.Committed, (event) =>
740753
Effect.gen(function* () {
754+
// L1 history cache: rows deleted by the revert may already be cached.
755+
HistoryCache.drop(event.data.sessionID)
741756
const boundary = yield* db
742757
.select({ seq: SessionMessageTable.seq })
743758
.from(SessionMessageTable)

‎packages/core/src/session/transfer.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import { AbsolutePath, RelativePath } from "../schema.js"
1818
import { Session } from "../session.js"
1919
import { Slug } from "../util/slug.js"
2020
import { SessionEvent } from "./event.js"
21+
import { HistoryCache } from "./history-cache.js"
2122
import { SessionMessage } from "./message.js"
2223
import { SessionProjector } from "./projector.js"
2324
import { SessionMessageTable, SessionTable } from "./sql.js"
@@ -112,6 +113,8 @@ const layer = Layer.effect(
112113
if (messages.length > 0) {
113114
yield* db.insert(SessionMessageTable).values(messages).run().pipe(Effect.orDie)
114115
yield* Bus.reserveSequence(db, sessionID, seq + messages.length)
116+
// L1 history cache: imported rows bypass the projector's invalidation.
117+
HistoryCache.drop(sessionID)
115118
}
116119
yield* db
117120
.update(SessionTable)

0 commit comments

Comments
 (0)