Skip to content

feat: merge multiple sessions into one, with similarity scoring - #1164

Open
LiuQhahah wants to merge 2 commits into
agegr:mainfrom
LiuQhahah:feat/session-merge
Open

LiuQhahah wants to merge 2 commits into
agegr:mainfrom
LiuQhahah:feat/session-merge

Conversation

@LiuQhahah

Copy link
Copy Markdown

Closes #1163

Summary

This PR adds a session merge feature to the sidebar for the pi coding
agent web UI: pick two or more sessions, preview how similar they are, and
combine them into one session file (sources are deleted afterwards).

User story

As a pi user working across several related sessions of the same project,
I want to merge multiple sessions into a single one, so that I can keep one
coherent history instead of a fragmented list — and before anything is
deleted, I want to know how similar the sessions are, so I only merge
ones that actually belong together.

What it does

  • Selection: row menu Merge… (also bound to M) and a top-bar merge
    button open a searchable multi-select dialog.
  • Similarity preview: every pair of selected sessions is scored by
    Dice coefficient over shingles of the actual .jsonl content
    (thresholds: ≥ 0.45 suggests merging, < 0.25 warns). The UI shows the
    per-pair score and an overall verdict.
  • Safety gates:
    • Dissimilar pairs need an explicit “merge anyway” confirmation
      (force); the server re-validates it (409 on refusal).
    • Default same-directory only: sessions whose header cwd differs
      are refused unless the user turns off “only merge sessions in the same
      directory”; the server enforces this (sameCwdOnly, 400 on refusal).
    • Subagent sessions are refused — their pi-web:subagent metadata
      cannot be carried into a normal session (409).
  • Merge semantics:
    • The first selected session is the main line; following sessions have
      their ids re-mapped and the parent/child tree relinked (branch
      structure preserved), then are appended after the main line with a
      custom_message source marker.
    • Sources are deleted after success; existing fork children are
      re-parented to the new file. The merged session is created beside the
      main session and opened in the UI.
  • i18n: en / zh-CN / zh-TW.

Files

  • lib/session-merge.ts — scoring (textShingles, dice),
    previewSessionMerge, mergeSessions, SessionMergeError
  • app/api/sessions/merge/route.ts — POST endpoint (preview or execute)
  • components/SessionMergeDialog.tsx — the dialog
  • components/SessionSidebar.tsx, components/SidebarIcons.tsx,
    lib/sidebar-actions.ts — entry points
  • app/sidebar.css — dialog styling
  • lib/session-merge.test.mjs — 9 unit tests (node:test + jiti)

Test status

  • node --experimental-strip-types --test lib/session-merge.test.mjs — 9/9
  • tsc --noEmit — clean

Adds a session merge feature to the sidebar: select two or more sessions
and combine them into a single .jsonl file beside the main session,
deleting the sources. Before merging, a preview scores every pair by
Dice similarity over the full session content; dissimilar pairs need an
explicit confirmation (force), cross-cwd merges are rejected unless the
user turns off the same-directory switch, and subagent sources are
refused outright.

- lib/session-merge.ts: textShingles/dice scoring, previewSessionMerge,
  mergeSessions (id remap + parent relink, source deletion, child
  reparenting), SessionMergeError with typed codes
- app/api/sessions/merge/route.ts: POST endpoint with preview/force/
  sameCwdOnly handling, shutdowns RPC wrappers before writing
- components/SessionMergeDialog.tsx: searchable multi-select dialog with
  live similarity preview, force confirmation, same-directory toggle
- components/SessionSidebar.tsx: row menu Merge... + top bar button,
  opens the merged session and closes deleted ones
- i18n: en / zh-CN / zh-TW strings
- tests: 9 cases covering scoring, merging, refusals, cross-cwd and
  subagent sources

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Feature request: merge multiple sessions into one, with similarity scoring

1 participant