Skip to content

fix(share): preserve share ID on import/re-share and handle missing metadata#29982

Open
lexlian wants to merge 1 commit into
anomalyco:devfrom
lexlian:fix/29821-share-id-corruption
Open

fix(share): preserve share ID on import/re-share and handle missing metadata#29982
lexlian wants to merge 1 commit into
anomalyco:devfrom
lexlian:fix/29821-share-id-corruption

Conversation

@lexlian
Copy link
Copy Markdown

@lexlian lexlian commented May 30, 2026

Issue for this PR

Closes #29821

Type of change

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

What does this PR do?

The bug had two root causes:

  1. Share ID corruption on import/re-share: Share ID is derived from session ID at creation time on the server. When a session was imported on another machine, it got a new local session ID. When re-sharing, the server generated a new share ID from the new session ID, orphaning the original share data and causing "Sharing content found empty" errors.

  2. TypeError on missing metadata: The fromV1() function used non-null assertions (!) on assistant metadata and tool metadata that may be missing in imported sessions, causing TypeError: Cannot read properties of undefined (reading 'id').

Fixes (3 total)

Fix 1: Preserve original share ID

  • File: packages/opencode/src/session/session.ts, packages/opencode/src/share/share-next.ts
  • Added check in ShareNext.create(): if session already has a share (from previous share or import), reuse it instead of creating new

Fix 2: Defensive handling of missing metadata

  • File: packages/web/src/components/Share.tsx
  • Changed v1.metadata.assistant! to v1.metadata.assistant (using optional chaining ?.)
  • Changed v1.metadata.tool?.[part.toolInvocation.toolCallId] to use defensive checks

Fix 3: Validate messages on import

  • File: packages/opencode/src/cli/cmd/import.ts
  • Added validateImportMessages() to check for missing ID and sessionID during import

How did you verify your code works?

  • bun test packages/web/test/components/Share.test.ts — 9 tests cases covering fromV1() with missing/partial metadata
  • bun test packages/opencode/test/import.test.ts — 8 tests cases covering import scenarios
  • bun run typecheck — passes in all packages

Checklist

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

…etadata

Closes anomalyco#29821

## What changed
- Fix 1: Added share reuse logic in ShareNext.create() - checks for existing
  share before creating a new one, preserving the original share ID after import
- Fix 2: Added defensive null checks in fromV1() for missing assistant metadata
  and tool metadata, preventing TypeError on malformed share data
- Fix 3: Added validateImportMessages() to validate imported message metadata
  and warn/error on malformed data during import

## Why
Share ID was derived from session ID at creation time. When a session was
imported on another machine, it got a new session ID, causing re-share to
create a different share ID and orphan the original share data. Also,
fromV1() crashed on missing metadata using non-null assertions.

## How to verify
- bun test packages/web/test/components/Share.test.ts (9 tests)
- bun test packages/opencode/test/import.test.ts (8 tests)
@github-actions
Copy link
Copy Markdown
Contributor

Thanks for updating your PR! It now meets our contributing guidelines. 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants