GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — fix(session): store turn summaries as names and counts, compute patches on request by lihaokun · Pull Request #57 · lihaokun/opencode

fix(session): store turn summaries as names and counts, compute patches on request #57

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Sign up for GitHub

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
lihaokun merged 2 commits into dev from slim-summary
Oct 11, 2026

Uh oh!

There was an error while loading. Please reload this page.

Merged

fix(session): store turn summaries as names and counts, compute patches on request #57

Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit Hold shift + click to select a range
6a3b64c
fix(session): store turn summaries as names and counts, compute patch…
lihaokun Oct 11, 2026
13074b0
docs: position the fork in the README
lihaokun Oct 11, 2026
File filter
Filter by extension

Clear filters
Conversations
Failed to load comments.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Jump to
Jump to file
Failed to load files.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Diff view
Diff view
Next Next commit
fix(session): store turn summaries as names and counts, compute patch…
…es on request

Each turn's summary embedded the full-context patch of every file that
changed in the workspace, and the durable message.updated event copied it
on every write. One turn that regenerated a thousand tracked logs produced
a 249 MB user message; the process reached 47 GB and was killed, and the
event table grew to 44 GB. Upstream carries the same code and six open
issues for it; three fixes there were closed unmerged.

The summary now stores file names, line counts and status from git
numstat; nothing is read into memory. The diff endpoint computes patches
from the turn's snapshot hashes when asked, with a per-file and a
per-response size limit. Summaries scheduled for the same message are
coalesced and written once; a result equal to what is stored is not
written again, while a turn that changed nothing still writes once to
reset the session's counts. Session-level counts and the session.diff
event now carry the turn's file list. A git failure during comparison is
an error that writes nothing, not an empty change set. Stored file names
are unquoted, matching the endpoint.

Design, upstream comparison, review record and test layers are in
docs/fixes/session-fix-summary-diff-size.md.
  • Loading branch information

Uh oh!

There was an error while loading. Please reload this page.

@lihaokun
lihaokun committed Oct 11, 2026
commit 6a3b64c68976f72ce02218175e51fde971ea6622

There are no files selected for viewing

282 changes: 282 additions & 0 deletions 282 docs/fixes/session-fix-summary-diff-size.md
Show comments
View file
Open in desktop

Large diffs are not rendered by default.

Oops, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

170 changes: 128 additions & 42 deletions 170 packages/opencode/src/session/summary.ts
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { LayerNode } from "@opencode-ai/core/effect/layer-node"
import { Effect, Layer, Context, Schema } from "effect"
import { Effect, Layer, Context, Schema, Semaphore } from "effect"
import { SessionV1 } from "@opencode-ai/core/v1/session"
import { EventV2Bridge } from "@/event-v2-bridge"
import { Snapshot } from "@/snapshot"
Expand Down Expand Up @@ -63,6 +63,37 @@ function unquoteGitPath(input: string) {
return Buffer.from(bytes).toString()
}

/** The user message and the assistant messages that answered it. */
function turn(messages: SessionV1.WithParts[], messageID: MessageID) {
return messages.filter(
(m) => m.info.id === messageID || (m.info.role === "assistant" && m.info.parentID === messageID),
)
}

/** git C-quotes paths with quotes, backslashes or control characters; callers get the real name. */
function unquote(item: Snapshot.FileDiff): Snapshot.FileDiff {
if (item.file === undefined) return item
const file = unquoteGitPath(item.file)
return file === item.file ? item : { ...item, file }
}

/** Same files, counts and statuses, and neither side carries a patch. */
function sameStat(stored: readonly Snapshot.FileDiff[], fresh: readonly Snapshot.FileDiff[]) {
return (
stored.length === fresh.length &&
stored.every((row, i) => {
const next = fresh[i]!
return (
row.patch === undefined &&
row.file === next.file &&
row.additions === next.additions &&
row.deletions === next.deletions &&
row.status === next.status
)
})
)
}

export interface Interface {
readonly summarize: (input: { sessionID: SessionID; messageID: MessageID }) => Effect.Effect<void>
readonly diff: (input: { sessionID: SessionID; messageID?: MessageID }) => Effect.Effect<Snapshot.FileDiff[]>
Expand All @@ -79,66 +110,121 @@ const layer = Layer.effect(
const events = yield* EventV2Bridge.Service
const config = yield* Config.Service

const computeDiff = Effect.fn("SessionSummary.computeDiff")(function* (input: { messages: SessionV1.WithParts[] }) {
let from: string | undefined
let to: string | undefined
for (const item of input.messages) {
if (!from) {
for (const part of item.parts) {
if (part.type === "step-start" && part.snapshot) {
from = part.snapshot
break
}
}
}
for (const part of item.parts) {
if (part.type === "step-finish" && part.snapshot) to = part.snapshot
}
// The two snapshot hashes a turn's diff is computed from: the first
// step-start and the last step-finish among the turn's messages.
const snapshots = (messages: SessionV1.WithParts[]) => {
const parts = messages.flatMap((item) => item.parts)
return {
from: parts.filter((part) => part.type === "step-start").find((part) => part.snapshot)?.snapshot,
to: parts.filter((part) => part.type === "step-finish").findLast((part) => part.snapshot)?.snapshot,
}
}

const computeDiff = Effect.fn("SessionSummary.computeDiff")(function* (input: { messages: SessionV1.WithParts[] }) {
const { from, to } = snapshots(input.messages)
if (from && to) return yield* snapshot.diffFull(from, to)
return []
})

const summarize = Effect.fn("SessionSummary.summarize")(function* (input: {
const summarizeOnce = Effect.fn("SessionSummary.summarizeOnce")(function* (input: {
sessionID: SessionID
messageID: MessageID
}) {
if ((yield* config.get()).snapshot === false) {
yield* sessions.setSummary({ sessionID: input.sessionID, summary: { additions: 0, deletions: 0, files: 0 } })
yield* events.publish(Session.Event.Diff, { sessionID: input.sessionID, diff: [] })
return
}
const all = yield* sessions.messages({ sessionID: input.sessionID }).pipe(Effect.orDie)
if (!all.length) return

const messages = turn(all, input.messageID)
const target = messages.find((m) => m.info.id === input.messageID)
if (!target || target.info.role !== "user") return
// Names and counts only. The patch is derived from the two snapshot
// hashes the step parts already carry, and is computed when a client asks
// for it (see diff below). Storing it put every changed file's full text
// into the message, and the durable message.updated event copied that
// again on every write of the message.
const { from, to } = snapshots(messages)
// A failed comparison writes nothing: the next step's summary retries, and
// an empty list written here would read as "this turn changed nothing".
const compare: Effect.Effect<Snapshot.FileDiff[], Snapshot.DiffError> =
from && to ? snapshot.diffStat(from, to) : Effect.succeed([])
const stat = yield* compare.pipe(
Effect.catchTag("SnapshotDiffError", (error) =>
Effect.logWarning("summary skipped: snapshot diff failed", {
sessionID: input.sessionID,
messageID: input.messageID,
error: error.message,
}).pipe(Effect.as(undefined)),
),
)
if (!stat) return
const diffs = stat.map(unquote)
// Every write is a durable event; nothing new to say means no write. A
// message never summarised is not one summarised with no changes: its
// first write, even of an empty list, is what resets the session's counts
// and clears the sidebar after a turn that changed nothing.
const stored = target.info.summary?.diffs
if (stored && sameStat(stored, diffs)) return
target.info.summary = { ...target.info.summary, diffs }
yield* sessions.updateMessage(target.info)
yield* sessions.setSummary({
sessionID: input.sessionID,
summary: {
additions: 0,
deletions: 0,
files: 0,
files: diffs.length,
additions: diffs.reduce((sum, item) => sum + item.additions, 0),
deletions: diffs.reduce((sum, item) => sum + item.deletions, 0),
},
})
yield* events.publish(Session.Event.Diff, { sessionID: input.sessionID, diff: [] })
if ((yield* config.get()).snapshot === false) return
const all = yield* sessions.messages({ sessionID: input.sessionID }).pipe(Effect.orDie)
if (!all.length) return
yield* events.publish(Session.Event.Diff, { sessionID: input.sessionID, diff: diffs })
})

const messages = all.filter(
(m) => m.info.id === input.messageID || (m.info.role === "assistant" && m.info.parentID === input.messageID),
// Several summaries for one message can be in flight at once: the processor
// schedules one per step and releases them together when the attempt ends,
// and the next run's first step schedules one more. One runs at a time per
// message, one more may wait, and any further caller leaves — the waiting
// one reads the same final state it would have.
const inflight = new Map<MessageID, { gate: Semaphore.Semaphore; waiting: number }>()
const summarize = Effect.fn("SessionSummary.summarize")(function* (input: {
sessionID: SessionID
messageID: MessageID
}) {
const entry = inflight.get(input.messageID) ?? { gate: Semaphore.makeUnsafe(1), waiting: 0 }
inflight.set(input.messageID, entry)
if (entry.waiting > 0) return
entry.waiting += 1
yield* entry.gate.withPermits(1)(
Effect.suspend(() => {
entry.waiting -= 1
return summarizeOnce(input)
}),
)
const target = messages.find((m) => m.info.id === input.messageID)
if (!target || target.info.role !== "user") return
const msgDiffs = yield* computeDiff({ messages })
target.info.summary = { ...target.info.summary, diffs: msgDiffs }
yield* sessions.updateMessage(target.info)
if (entry.waiting === 0 && inflight.get(input.messageID) === entry) inflight.delete(input.messageID)
})

const diff = Effect.fn("SessionSummary.diff")(function* (input: { sessionID: SessionID; messageID?: MessageID }) {
if (!input.messageID) return []
const message = (yield* sessions.messages({ sessionID: input.sessionID }).pipe(Effect.orDie)).find(
(item) => item.info.id === input.messageID,
)
const all = yield* sessions.messages({ sessionID: input.sessionID }).pipe(Effect.orDie)
// No message named means the most recent turn, as the sidebar's file list
// asks on opening a session: what that turn stored, names and counts,
// without computing a patch nobody asked for.
if (!input.messageID) {
const latest = all.findLast((item) => item.info.role === "user")
return latest?.info.role === "user" ? (latest.info.summary?.diffs ?? []).map(unquote) : []
}
const message = all.find((item) => item.info.id === input.messageID)
if (!message || message.info.role !== "user") return []
const diffs = message.info.summary?.diffs ?? []
return diffs.map((item) => {
if (item.file === undefined) return item
const file = unquoteGitPath(item.file)
if (file === item.file) return item
return { ...item, file }
})
const stored = message.info.summary?.diffs ?? []
// Patches are computed here, on request, from the turn's snapshot hashes.
// A message that still carries stored patches predates that and keeps
// them: its snapshot objects may be gone by now. A turn with no hashes
// (snapshots off, or a message from before they were recorded) has only
// what it stores.
const diffs = stored.some((item) => item.patch !== undefined)
? stored
: yield* computeDiff({ messages: turn(all, input.messageID) })
return (diffs.length ? diffs : stored).map(unquote)
})

return Service.of({ summarize, diff, computeDiff })
Expand Down
Loading
Oops, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Loading
Oops, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
You are visitor 01402809 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.