@@ -63,6 +63,40 @@ function unquoteGitPath(input: string) { |
63 | 63 | return Buffer.from(bytes).toString() |
64 | 64 | } |
65 | 65 |
|
| 66 | +/** Vendor/generated trees that must not be retained in durable session summaries. */ |
| 67 | +const DENIED_SUMMARY_PATH = |
| 68 | + /(^|\/)(node_modules|\.node-runtime|\.git|dist|build|out|coverage|\.cache|\.venv|venv|target|\.next|\.nuxt|\.turbo|\.vite|vendor|__pycache__|site-packages)(\/|$)/i |
| 69 | + |
| 70 | +/** |
| 71 | + * Persist only lightweight diff metadata on the user message. |
| 72 | + * Full `patch` text is reconstructable from snapshots via `diff()` / `computeDiff()`. |
| 73 | + * Storing full patches in `message.summary.diffs` caused multi-GB `message.updated` |
| 74 | + * event-log fanout and OOM/Jetsam (see anomalyco/opencode#32005). |
| 75 | + */ |
| 76 | +export function compactSummaryDiffs(diffs: readonly Snapshot.FileDiff[]): Snapshot.FileDiff[] { |
| 77 | + return diffs.flatMap((diff) => { |
| 78 | + const file = diff.file?.replaceAll("\\", "/") |
| 79 | + if (file && DENIED_SUMMARY_PATH.test(file)) return [] |
| 80 | + return [ |
| 81 | + { |
| 82 | + file: diff.file, |
| 83 | + additions: diff.additions, |
| 84 | + deletions: diff.deletions, |
| 85 | + status: diff.status, |
| 86 | + }, |
| 87 | + ] |
| 88 | + }) |
| 89 | +} |
| 90 | + |
| 91 | +function mapUnquoted(diffs: readonly Snapshot.FileDiff[]): Snapshot.FileDiff[] { |
| 92 | + return diffs.map((item) => { |
| 93 | + if (item.file === undefined) return item |
| 94 | + const file = unquoteGitPath(item.file) |
| 95 | + if (file === item.file) return item |
| 96 | + return { ...item, file } |
| 97 | + }) |
| 98 | +} |
| 99 | + |
66 | 100 | export interface Interface { |
67 | 101 | readonly summarize: (input: { sessionID: SessionID; messageID: MessageID }) => Effect.Effect<void> |
68 | 102 | readonly diff: (input: { sessionID: SessionID; messageID?: MessageID }) => Effect.Effect<Snapshot.FileDiff[]> |
@@ -122,23 +156,23 @@ const layer = Layer.effect( |
122 | 156 | const target = messages.find((m) => m.info.id === input.messageID) |
123 | 157 | if (!target || target.info.role !== "user") return |
124 | 158 | const msgDiffs = yield* computeDiff({ messages }) |
125 | | - target.info.summary = { ...target.info.summary, diffs: msgDiffs } |
| 159 | + target.info.summary = { ...target.info.summary, diffs: compactSummaryDiffs(msgDiffs) } |
126 | 160 | yield* sessions.updateMessage(target.info) |
127 | 161 | }) |
128 | 162 |
|
129 | 163 | const diff = Effect.fn("SessionSummary.diff")(function* (input: { sessionID: SessionID; messageID?: MessageID }) { |
130 | 164 | if (!input.messageID) return [] |
131 | | - const message = (yield* sessions.messages({ sessionID: input.sessionID }).pipe(Effect.orDie)).find( |
132 | | - (item) => item.info.id === input.messageID, |
| 165 | + const all = yield* sessions.messages({ sessionID: input.sessionID }).pipe(Effect.orDie) |
| 166 | + const messages = all.filter( |
| 167 | + (m) => m.info.id === input.messageID || (m.info.role === "assistant" && m.info.parentID === input.messageID), |
133 | 168 | ) |
| 169 | + const recomputed = yield* computeDiff({ messages }) |
| 170 | + if (recomputed.length) return mapUnquoted(recomputed) |
| 171 | + |
| 172 | + // Legacy / imported sessions whose snapshots are gone: return stored metadata. |
| 173 | + const message = all.find((item) => item.info.id === input.messageID) |
134 | 174 | if (!message || message.info.role !== "user") return [] |
135 | | - const diffs = message.info.summary?.diffs ?? [] |
136 | | - return diffs.map((item) => { |
137 | | - if (item.file === undefined) return item |
138 | | - const file = unquoteGitPath(item.file) |
139 | | - if (file === item.file) return item |
140 | | - return { ...item, file } |
141 | | - }) |
| 175 | + return mapUnquoted(message.info.summary?.diffs ?? []) |
142 | 176 | }) |
143 | 177 |
|
144 | 178 | return Service.of({ summarize, diff, computeDiff }) |
|