Summary
- keep
session_message.seq as the projected transcript source-event order
- reset pre-launch
session_message rows before adding seq instead of failing startup when historical rows have no durable creator event
- preserve canonical V1
message and part history while clearing only the experimental V2 projection
- document the pre-launch projection compatibility boundary
Why The Migration Failed
Older experimental builds projected session.next.* transcript rows even when durable event persistence was still disabled behind the experimental-workspaces flag. A valid old database could therefore contain a session_message row without a matching durable event row.
The new projection-order migration tried to backfill every session_message.seq from event.seq and intentionally died when a matching event did not exist. Because database migrations run while the database layer starts, that blocked server startup.
sequenceDiagram
participant User
participant OpenCode
participant Projection as session_message
participant Events as event
User->>OpenCode: ordinary prompt
OpenCode->>Projection: insert projected transcript row
opt experimental workspaces enabled
OpenCode->>Events: insert durable creator event
end
Note over Projection,Events: older valid DBs can contain projection-only rows
Loading
Pre-launch session_message rows are experimental projection state, while canonical V1 history remains in message and part. This patch explicitly clears the projection before adding the required ordering field.
DELETE FROM session_message;
ALTER TABLE session_message ADD COLUMN seq INTEGER NOT NULL;
Why Keep seq
session_message.id identifies a projected message. session_message.seq records the durable aggregate sequence at which that message entered visible Session history. Those are different concerns.
V2 admits prompts into a durable inbox before projecting them into visible history. steer promotes at the next safe provider-turn boundary; queue opens a later activity after the current one settles. IDs and admission timestamps therefore cannot define visible-history order.
sequenceDiagram
participant Caller
participant Inbox as session_input
participant Runner
participant Transcript as session_message
Caller->>Inbox: admit prompt(delivery)
Note over Inbox: durable, not visible yet
Runner->>Inbox: promote at safe boundary
Inbox->>Transcript: append Prompted(seq)
Runner->>Transcript: reload ordered context
Loading
The projected seq keeps context assembly, pagination, compaction boundaries, and latest-message lookup aligned with durable causal order.
Verification
Passed locally:
bun run test -- test/database-migration.test.ts test/session-projector.test.ts test/session-prompt.test.ts test/session-runner.test.ts
90 pass
0 fail
bun run test -- test/session/prompt.test.ts test/session/processor-effect.test.ts test/session/llm-native.test.ts
84 pass
1 skip
0 fail
bun script/migration.ts --check
No schema changes, nothing to migrate
bun typecheck # packages/core
bun typecheck # packages/opencode
bun typecheck # packages/sdk/js
git diff --check
The pre-push hook also passed workspace Turbo typecheck across 22 packages.
Validated against a backup copy of a real existing database:
legacy messages after startup 277169
legacy parts after startup 1176709
v2 projected messages after startup 0
seq column 1
projection migration applied 1
Canonical V1 history remained intact while the pre-launch V2 projection reset and the corrected migration completed successfully.