|
1 | 1 | import { isDeepStrictEqual } from "node:util" |
| 2 | +import { and, eq } from "drizzle-orm" |
| 3 | +import { Database } from "@opencode-ai/core/database/database" |
| 4 | +import { MessageTable } from "@opencode-ai/core/session/sql" |
2 | 5 | import { LayerNode } from "@opencode-ai/core/effect/layer-node" |
3 | 6 | import { Effect, Layer, Context, Schema, Semaphore } from "effect" |
4 | 7 | import { SessionV1 } from "@opencode-ai/core/v1/session" |
@@ -82,6 +85,7 @@ const layer = Layer.effect( |
82 | 85 | const snapshot = yield* Snapshot.Service |
83 | 86 | const events = yield* EventV2Bridge.Service |
84 | 87 | const config = yield* Config.Service |
| 88 | + const { db } = yield* Database.Service |
85 | 89 |
|
86 | 90 | const computeDiff = Effect.fn("SessionSummary.computeDiff")(function* (input: { messages: SessionV1.WithParts[] }) { |
87 | 91 | let from: string | undefined |
@@ -151,12 +155,51 @@ const layer = Layer.effect( |
151 | 155 |
|
152 | 156 | const diff = Effect.fn("SessionSummary.diff")(function* (input: { sessionID: SessionID; messageID?: MessageID }) { |
153 | 157 | if (!input.messageID) return [] |
154 | | - const message = (yield* sessions.messages({ sessionID: input.sessionID }).pipe(Effect.orDie)).find( |
155 | | - (item) => item.info.id === input.messageID, |
156 | | - ) |
| 158 | + const all = yield* sessions.messages({ sessionID: input.sessionID }).pipe(Effect.orDie) |
| 159 | + const message = all.find((item) => item.info.id === input.messageID) |
157 | 160 | if (!message || message.info.role !== "user") return [] |
158 | 161 | const diffs = message.info.summary?.diffs ?? [] |
159 | | - return diffs.map((item) => { |
| 162 | + // Local publish projects full patches. Only a patchless replay projection |
| 163 | + // needs snapshot recovery; successful recovery makes subsequent reads local. |
| 164 | + const resolved = yield* Effect.gen(function* () { |
| 165 | + if (diffs.every((item) => item.patch !== undefined)) return diffs |
| 166 | + const computed = yield* computeDiff({ |
| 167 | + messages: all.filter( |
| 168 | + (item) => |
| 169 | + item.info.id === input.messageID || |
| 170 | + (item.info.role === "assistant" && item.info.parentID === input.messageID), |
| 171 | + ), |
| 172 | + }).pipe(Effect.catchCause(() => Effect.succeed([] as Snapshot.FileDiff[]))) |
| 173 | + const patches = new Map( |
| 174 | + computed.filter((item) => item.file !== undefined).map((item) => [unquoteGitPath(item.file!), item.patch]), |
| 175 | + ) |
| 176 | + const recovered = diffs.map((item) => { |
| 177 | + if (item.patch !== undefined) return item |
| 178 | + const patch = item.file === undefined ? undefined : patches.get(unquoteGitPath(item.file)) |
| 179 | + // truncated is runtime-only: FileDiff's schema drops it, so never persist |
| 180 | + // a failed recovery (including its empty patch) as a complete projection. |
| 181 | + return patch === undefined ? { ...item, patch: "", truncated: true } : { ...item, patch } |
| 182 | + }) |
| 183 | + if (recovered.some((item) => "truncated" in item)) return recovered |
| 184 | + const row = yield* db |
| 185 | + .select() |
| 186 | + .from(MessageTable) |
| 187 | + .where(and(eq(MessageTable.id, message.info.id), eq(MessageTable.session_id, input.sessionID))) |
| 188 | + .get() |
| 189 | + .pipe(Effect.orDie) |
| 190 | + if (row && isDeepStrictEqual(row.data.summary?.diffs, diffs)) { |
| 191 | + // Bypass publish/updateMessage. A read must neither recurse nor append an |
| 192 | + // event; the compare-and-set also avoids overwriting a concurrent update. |
| 193 | + yield* db |
| 194 | + .update(MessageTable) |
| 195 | + .set({ data: { ...row.data, summary: { ...row.data.summary, diffs: recovered } } }) |
| 196 | + .where(and(eq(MessageTable.id, row.id), eq(MessageTable.data, row.data))) |
| 197 | + .run() |
| 198 | + .pipe(Effect.orDie) |
| 199 | + } |
| 200 | + return recovered |
| 201 | + }) |
| 202 | + return resolved.map((item) => { |
160 | 203 | if (item.file === undefined) return item |
161 | 204 | const file = unquoteGitPath(item.file) |
162 | 205 | if (file === item.file) return item |
@@ -177,7 +220,7 @@ export type DiffInput = Schema.Schema.Type<typeof DiffInput> |
177 | 220 | export const node = LayerNode.make({ |
178 | 221 | service: Service, |
179 | 222 | layer: layer, |
180 | | - deps: [Session.node, Snapshot.node, EventV2Bridge.node, Config.node], |
| 223 | + deps: [Session.node, Snapshot.node, EventV2Bridge.node, Config.node, Database.node], |
181 | 224 | }) |
182 | 225 |
|
183 | 226 | export * as SessionSummary from "./summary" |