Skip to content

fix(opencode): v2 session migrate by setting correct seq and prevent dedupe project id#30705

Open
Eric-Guo wants to merge 2 commits into
anomalyco:devfrom
Eric-Guo:fix_v2_session_migrate
Open

fix(opencode): v2 session migrate by setting correct seq and prevent dedupe project id#30705
Eric-Guo wants to merge 2 commits into
anomalyco:devfrom
Eric-Guo:fix_v2_session_migrate

Conversation

@Eric-Guo
Copy link
Copy Markdown
Contributor

@Eric-Guo Eric-Guo commented Jun 4, 2026

Issue for this PR

Closes #30701, for #30632

Type of change

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

What does this PR do?

Fix session_message seq order and prevent dedupe project id by worktree introduced at #30632

How did you verify your code works?

It works on my Mac!

Checklist

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

Copilot AI review requested due to automatic review settings June 4, 2026 07:34
@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Jun 4, 2026

Hey! Your PR title Fix v2 session migrate by setting correct seq and prevent dedupe project id doesn't follow conventional commit format.

Please update it to start with one of:

  • feat: or feat(scope): new feature
  • fix: or fix(scope): bug fix
  • docs: or docs(scope): documentation changes
  • chore: or chore(scope): maintenance tasks
  • refactor: or refactor(scope): code refactoring
  • test: or test(scope): adding or updating tests

Where scope is the package name (e.g., app, desktop, opencode).

See CONTRIBUTING.md for details.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

This PR improves project ID/worktree deduplication and makes the session_message.seq migration resilient when durable event rows are missing.

Changes:

  • Add logic to migrate data from multiple project rows sharing the same worktree into the canonical project ID.
  • Add a new DB migration to dedupe projects with identical worktrees and update related tables (session, workspace, project_directory).
  • Update the session_message.seq backfill to fall back to projected ordering when durable events are absent; update tests accordingly.

Reviewed changes

Copilot reviewed 8 out of 8 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
packages/opencode/test/project/project.test.ts Adds a live test asserting worktree-based project ID migration also rewrites project_directory.
packages/opencode/src/project/project.ts Adds runtime worktree dedupe migration logic and migrates project_directory rows during project ID migration.
packages/core/test/database-migration.test.ts Updates projection-order migration test semantics and adds coverage for worktree dedupe migration.
packages/core/src/database/migration/20260604120000_dedupe_project_worktrees.ts Introduces a migration to canonicalize duplicate worktree projects and rewire dependent rows.
packages/core/src/database/migration/20260603040000_session_message_projection_order.ts Changes backfill strategy to use durable event seq when available, otherwise derive an order from projections.
packages/core/src/database/migration.gen.ts Registers the new dedupe-worktrees migration.
packages/core/migration/20260604120000_dedupe_project_worktrees/migration.sql SQL artifact for the new worktree dedupe migration.
packages/core/migration/20260603040000_session_message_projection_order/migration.sql Updates SQL artifact for the new seq backfill behavior.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines 1 to +17
ALTER TABLE `session_message` ADD `seq` integer NOT NULL;--> statement-breakpoint
UPDATE `session_message`
SET `seq` = COALESCE(
(SELECT `seq` FROM `event` WHERE `event`.`id` = `session_message`.`id`),
(
SELECT COUNT(*) - 1
FROM `session_message` AS `ordered`
WHERE `ordered`.`session_id` = `session_message`.`session_id`
AND (
`ordered`.`time_created` < `session_message`.`time_created`
OR (
`ordered`.`time_created` = `session_message`.`time_created`
AND `ordered`.`id` <= `session_message`.`id`
)
)
)
);--> statement-breakpoint
Comment on lines +10 to +25
`UPDATE \`session_message\`
SET \`seq\` = COALESCE(
(SELECT \`seq\` FROM \`event\` WHERE \`event\`.\`id\` = \`session_message\`.\`id\`),
(
SELECT COUNT(*) - 1
FROM \`session_message\` AS \`ordered\`
WHERE \`ordered\`.\`session_id\` = \`session_message\`.\`session_id\`
AND (
\`ordered\`.\`time_created\` < \`session_message\`.\`time_created\`
OR (
\`ordered\`.\`time_created\` = \`session_message\`.\`time_created\`
AND \`ordered\`.\`id\` <= \`session_message\`.\`id\`
)
)
)
);`,
Comment on lines +212 to +221
yield* d.run(sql`
INSERT OR IGNORE INTO project_directory (project_id, directory, type, time_created)
SELECT
${newID},
directory,
type,
time_created
FROM project_directory
WHERE project_id = ${oldID}
`)
@Eric-Guo Eric-Guo changed the title Fix v2 session migrate by setting correct seq and prevent dedupe project id fix(opencode): v2 session migrate by setting correct seq and prevent dedupe project id Jun 4, 2026
@Eric-Guo Eric-Guo force-pushed the fix_v2_session_migrate branch from 9d49fb1 to 776b683 Compare June 4, 2026 07:39
@github-actions github-actions Bot mentioned this pull request Jun 4, 2026
6 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

After #30632 embedded v2 session runtime and tool foundation, the DB migrate break.

2 participants