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(core): append V1 messages written after the migration completed by Veld101 · Pull Request #57080 · anomalyco/opencode

Conversation

@Veld101

@Veld101 Veld101 commented Oct 19, 2026 •
edited
Loading

Uh oh!

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

Copy link
Copy Markdown

Issue for this PR

Closes #57000

Type of change

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

What does this PR do?

A pre-upgrade V1 client can keep writing to the legacy message table after V2 has imported the session. Completion was a permanent latch, so those messages never reached session_message: the TUI showed a truncated session and opencode session export silently dropped them — 812 messages in the report.

Each imported session now records the newest legacy time_created it imported, and completion appends only legacy messages past that watermark. Three properties matter, and each has an assertion:

  • Absence from the projection is not "late". RevertEvent.Committed deletes every session_message row from the revert boundary on and clears session_v2.revert, so a message the user reverted in V2 looks exactly like a message that was never imported. Keying off absence re-appended reverted messages on the next start. The watermark is what distinguishes them: a reverted message is at or below it.
  • The watermark advances past everything considered. It moves in the same transaction as the rows, and past messages the transform drops, so a row that can never be projected is not reconsidered on every start.
  • V2 session state is not recomputed. transformed.session is a V1-only aggregate: writing it over the live row replaced cost and the token totals with V1 sums and cleared revert and time_compacting. The append path adds the appended messages' own amounts to the live totals and leaves agent, model, revert and time_compacting alone.

seq continues after the highest projected value (the unique (session_id, seq) index, and V2 may have written since), and the aggregate sequence moves to max(current, last appended) — the report warned that rewinding event_sequence corrupts the log.

Databases already migrated before this watermark

Those cannot be told apart from a reverted one, so the first run adopts the current maximum and appends nothing; messages written from then on are picked up normally. Repairing what such a database already lost needs a separate path and is deliberately not attempted here — resurrecting reverted content would be worse than leaving the existing loss.

Scope

This covers new messages inside an already imported session. Whole V1 sessions created after completion are #50130/#49641, which #54272 addresses with a session-level watermark; the two are complementary and both edit run().

How did you verify your code works?

Four database-level cases in packages/core/test/v1-migration.test.ts:

  • appends legacy messages written after the import completed — the reported case; also keeps a V2 message and a projected event_sequence of 5 intact.
  • does not re-append a legacy message a V2 revert removed — deletes a projected row the way a committed revert does, then asserts it stays gone.
  • adds appended totals to V2 session state instead of replacing it — V2 sets cost = 42, tokens_input = 7, agent = v2-agent, revert, time_compacting = 99; after the append, totals are 43 and 9 and everything else is untouched.
  • adopts a completed database that has no watermark — nothing is appended on the first run, then later messages are.

All three of the last group fail against the previous revision of this branch; the state case shows revert becoming null.

bun test test/v1-migration.test.ts   # 30 pass, 0 fail
bun test test/database               # 32 pass, 0 fail
bun typecheck                        # packages/core, clean
bunx prettier --check                # clean
bun run lint                         # exit 0, no new findings

Screenshots / recordings

Not applicable; no UI change.

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

Sorry, something went wrong.

Uh oh!

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

All reactions
@github-actions

github-actions Bot commented Oct 19, 2026

Copy link
Copy Markdown
Contributor

The following comment was made by an LLM, it may be inaccurate:

All reactions

Sorry, something went wrong.

Uh oh!

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

opencode-agent[bot]
opencode-agent Bot reviewed Oct 19, 2026
View reviewed changes

@opencode-agent opencode-agent Bot left a comment

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The fix works for the reported case, but it can bring back messages a user reverted in V2 and it overwrites V2 session state. Both need fixing before this can merge.

How I checked: I ran the #57000 reproduction from source on both checkouts, writing the legacy V1 rows directly. On the base, 20 legacy messages give 10 projected rows and an export of 10. With this PR, all 20 are projected and exported. v1-migration.test.ts passes (27/27), the new test fails against the base code, and packages/core typecheck is clean.

Public API: unchanged.

Concerns (details inline):

  • Reverted messages come back. Any legacy message without a session_message row counts as "late". V2 removes rows deliberately: RevertEvent.Committed in session/projector.ts deletes every row from the revert point on. A V1 message that was imported and later reverted in V2 is re-appended at the end of the session on the next startup. I checked this with a database-level test.
  • V2 session state is overwritten. transformed.session is computed from V1 data only. Writing it over the live session_v2 row replaces cost and the token totals with V1-only sums, sets revert and time_compacting to null, and can change agent/model. In my test, cost went from 42 to 0 and time_compacting was cleared.

One way to address both is to record which legacy messages were already imported at completion (for example the highest legacy time_created or ID per session) and only append legacy messages newer than that. Session totals could then be updated by adding the new messages' amounts instead of being recomputed.

Related: #50130, #49641, #53971

Sorry, something went wrong.

Uh oh!

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

All reactions
Comment thread packages/core/src/database/v1-migration.bun.ts
SELECT DISTINCT m.session_id AS id
FROM message m
JOIN session_v2 projected_session ON projected_session.id = m.session_id
WHERE NOT EXISTS (SELECT 1 FROM session_message projected WHERE projected.id = m.id)

@opencode-agent opencode-agent Bot Oct 19, 2026

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A legacy message with no session_message row isn't always a late V1 write. V2 deletes rows on purpose: RevertEvent.Committed in session/projector.ts removes every row from the revert point on. With this check, a V1 message that was imported and then reverted in V2 is re-appended at the end of the session on a later startup. I reproduced this by deleting an imported row the way a revert does, adding one new legacy message and running again: the reverted message came back at seq 1.

The append should be limited to legacy messages written after this session was imported, for example newer than a per-session high-water mark (legacy time_created or ID) recorded at import. "Not projected" alone doesn't tell you that.

Related: legacy messages that never decode (#53971) also match this check on every startup, so their sessions are read and transformed again each time.

Sorry, something went wrong.

Uh oh!

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

All reactions
Comment thread packages/core/src/database/v1-migration.bun.ts Outdated
)
yield* tx
.update(SessionTable)
.set({ ...transformed.session })

@opencode-agent opencode-agent Bot Oct 19, 2026

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

transformed.session is computed from V1 data only: cost, the token totals, agent and model, plus revert: null and time_compacting: null. Writing it over the live session_v2 row discards V2 state. In a quick test, cost set by V2 went from 42 to 0, and time_compacting was cleared. A revert in progress would be cleared the same way.

For an append, only add the late messages' cost and tokens to the existing totals, and leave the other fields alone.

Sorry, something went wrong.

Uh oh!

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

All reactions
anqiang.ex
fix(core): append V1 messages written after the migration completed
2298adc
V1→V2 import reached `completed` once and never looked at an already imported
session again. A pre-upgrade V1 client that keeps running after the upgrade
therefore kept writing to the legacy `message` table while V2 wrote to
`session_message`, and the extra messages never reached the projection: the TUI
showed a truncated session and `opencode session export` silently dropped them.

Each imported session now records the newest legacy `time_created` it imported,
and completion appends only legacy messages past that watermark. Absence from
`session_message` alone is not enough: `RevertEvent.Committed` deletes rows from
the revert boundary on, so a message the user reverted in V2 would otherwise be
re-appended on the next start. The watermark is written in the same transaction
as the rows and advanced past every message it considered, including ones the
transform drops, so nothing is reconsidered on later starts.

Appended rows take `seq` after the highest projected one, and the aggregate
sequence moves to `max(current, last appended)`; the report warned that rewinding
`event_sequence` corrupts the log. Totals are the appended messages' own
amounts, added to the live row, because `transformed.session` is a V1-only
recomputation that would replace V2's cost and tokens and clear `revert` and
`time_compacting`.

A database migrated before the watermark existed cannot tell a late message from
a reverted one, so the first run adopts what is there and appends nothing;
messages written from then on are picked up normally. Repairing what such a
database already lost needs a separate path, and is not attempted here.

Adds four database-level cases: the late append itself, a reverted message staying
gone, appended totals not overwriting V2 state, and the watermark-less adoption.
@Veld101
Veld101 force-pushed the migration-append-messages branch from 8dd6c3f to 2298adc Compare October 19, 2026 13:31
@Veld101

Veld101 commented Oct 19, 2026

Copy link
Copy Markdown
Author

Both issues were real, and 2298adc replaces the approach.

Reverted messages. You were right that absence from session_message cannot mean "late": RevertEvent.Committed deletes rows from the boundary on and clears session_v2.revert, so nothing durable records that a message was deliberately removed. Each imported session now stores the newest legacy time_created it imported, and completion appends only messages past it. A reverted message sits at or below the watermark and stays gone. The watermark advances in the same transaction as the rows, past every message considered, so nothing is reconsidered on later starts.

V2 session state. The append path no longer writes transformed.session. It adds the appended messages' own amounts to the live cost and token columns, and leaves agent, model, revert and time_compacting untouched. The delta comes from transforming just the late sources, so it reuses the existing aggregate rules rather than duplicating them.

On the watermark source you suggested: I keyed it to legacy time_created per session rather than a single highest ID, because the watermark has to be recorded when that session is imported. A global value taken at completion would also cover messages that the V1 client wrote after its session was already imported but before completion, and those would then never be appended.

Databases migrated before this watermark. These cannot be distinguished from a reverted one, so the first run adopts the current maximum and appends nothing; later messages are then picked up normally. The already-lost messages need a separate repair path, and I left that out on purpose — resurrecting reverted content seemed worse than leaving the existing loss.

Three new cases cover all of this, and all three fail against the previous revision (the state one shows revert becoming null):

  • does not re-append a legacy message a V2 revert removed
  • adds appended totals to V2 session state instead of replacing it
  • adopts a completed database that has no watermark

v1-migration.test.ts is 30 pass / 0 fail, packages/core typechecks, lint and prettier are clean.

All reactions

Sorry, something went wrong.

Uh oh!

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

This was referenced Oct 22, 2026
migration: v1 sessions created after the v1->v2 migration completed are stubbed (HTTP 500 on session messages) #58041
Closed
migration: sessions created via 1.19 /api/session lose all messages on v1->v2 migration (2.1.3) #58543
Open
@r266-tech r266-tech mentioned this pull request Oct 23, 2026
fix(core): preserve 1.19 API messages during migration #58549
Closed
3 of 6 tasks
@opencode-agent opencode-agent Bot mentioned this pull request Oct 27, 2026
migration: V1 1.19.4 sessions not migrated to session_v2 #59517
Closed
This was referenced Oct 28, 2026
server: V1 sessions created after migration.v1-v2 completes are never imported #50130
Open
desktop: sessions created on 1.19.4 don't appear after upgrading to 2.2.x (12 sessions orphaned in the v1 schema) #60773
Closed
@github-actions

github-actions Bot commented Nov 19, 2026

Copy link
Copy Markdown
Contributor

Automated PR Cleanup

Thank you for contributing to opencode.

Due to the high volume of PRs from users and AI agents, we periodically close older PRs using automated criteria so maintainers can focus review time on the most active and community-supported contributions.

This PR was closed because it matched the following cleanup criteria:

  • The PR was created more than 1 month ago
  • The PR had fewer than 2 positive reactions
  • Positive reactions are counted as thumbs-up, heart, celebration, or rocket reactions on the PR

PRs created within the last month are not affected by this cleanup.

If you believe this PR was closed incorrectly, or if you are still actively working on it, please leave a comment explaining why it should be reopened. A maintainer can review and reopen it if appropriate.

Thanks again for taking the time to contribute.

All reactions

Sorry, something went wrong.

Uh oh!

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

@github-actions github-actions Bot closed this Nov 19, 2026
@github-actions github-actions Bot added the automated-pr-cleanup label Nov 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@opencode-agent opencode-agent[bot] opencode-agent[bot] left review comments

Uh oh!

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

Assignees

No one assigned

Labels

automated-pr-cleanup

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

session: migrated session missing messages written after V1→V2 migration; export truncated

Uh oh!

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

1 participant

@Veld101
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 01402775 — 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.