Skip to content

Add session history / new / delete toolbar to embedded terminal (#9)#10

Open
bommerts wants to merge 3 commits into
sailro:mainfrom
bommerts:feature/session-history-toolbar
Open

Add session history / new / delete toolbar to embedded terminal (#9)#10
bommerts wants to merge 3 commits into
sailro:mainfrom
bommerts:feature/session-history-toolbar

Conversation

@bommerts
Copy link
Copy Markdown
Contributor

Closes #9.

Adds a toolbar to the embedded Copilot CLI tool window with three buttons, right-aligned at the top:

  • View Session History (KnownMonikers.History) — opens a VS-themed picker filtered to the current workspace, lets the user pick a previous Copilot CLI session, and resumes it via copilot --resume=<id>.
  • New Session (KnownMonikers.NewItem) — restarts the terminal with a fresh copilot.
  • Delete Current Thread (KnownMonikers.DeleteListItem) — confirms, then transactionally deletes the current chat thread (across sessions, turns, session_files, session_refs, checkpoints, and the search_index FTS5 table) and restarts fresh.

Implementation notes

  • Reads ~/.copilot/session-store.db via Microsoft.Data.Sqlite (Mode=ReadOnly for the picker, Mode=ReadWrite for delete) so it does not contend with the live CLI's WAL writer.
  • Native e_sqlite3.dll is bundled in the VSIX for both win-x64 and win-arm64 via an MSBuild target running AfterTargets="RemoveVSSDKAssemblies".
  • Session ids passed to --resume= are validated against a strict UUID regex before being interpolated into the cmd.exe command line (defense against shell injection).
  • Workspace path matching is separator-aware in both SQL (LIKE workspace + sep + ''%'') and C# to avoid sibling overmatch (e.g. C:\repo should not match C:\repo-old).
  • The toolbar is a custom WPF bar inside the tool window content rather than a VS native command toolbar — VS tool window toolbars are left-aligned only, and a right-aligned placement matches VS Code''s terminal.
  • TerminalSessionService exposes explicit RestartFresh / RestartResuming / RestartPreservingMode methods (replaces a brittle tri-state RestartSession parameter set).
  • Fixed a pre-existing bug where TerminalProcess.IsRunning stayed true after the hosted CLI exited, breaking the Enter-to-restart UX.

New dependencies

  • Microsoft.Data.Sqlite 9.0.0
  • SQLitePCLRaw.lib.e_sqlite3 2.1.10

Both target netstandard2.0 and work on net472.

Verification

  • msbuild clean (0 warnings, 0 errors).
  • All 294 server tests pass.
  • Manually tested in VS 2026 (build 18): toolbar renders top-right, picker populates with workspace sessions, resume launches with correct --resume=<id>, delete removes the row from session-store.db and restarts fresh, both old commands disabled when no solution is open.

Per the heads-up posted on issue #9.

bommerts and others added 2 commits April 18, 2026 14:28
Adds a toolbar to the embedded Copilot CLI tool window with two buttons:

- View Session History — VS-themed picker filtered to the current
  workspace that resumes a previous Copilot CLI session via
  `copilot --resume=<id>`. Reads ~/.copilot/session-store.db in
  read-only mode (Microsoft.Data.Sqlite).
- New Session — restarts the terminal with a fresh `copilot`.

Buttons use KnownMonikers.History and KnownMonikers.NewItem via
`IconIsMoniker` so they pick up the active VS theme. Both commands
are disabled when no solution is open.

Hardening:

- Session ids passed to `--resume=` are validated against a strict
  UUID regex before being interpolated into the cmd.exe command line.
- Workspace path matching is separator-aware in both SQL (`LIKE
  workspace + sep + '%'`) and C# to avoid sibling overmatch
  (`C:\repo` matching `C:\repo-old`).
- `TerminalProcess.IsRunning` now returns false after the hosted CLI
  exits — fixes the Enter-to-restart UX.
- `TerminalSessionService` exposes explicit `RestartFresh` /
  `RestartResuming` / `RestartPreservingMode` methods replacing the
  previous tri-state `RestartSession` parameters.

Native `e_sqlite3.dll` (win-x64 + win-arm64) is bundled in the VSIX
via a `BundleSqliteInVsix` MSBuild target running after the VSSDK
strip step.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Adds a third toolbar button to the embedded Copilot CLI tool window:

- Delete Current Thread (KnownMonikers.DeleteListItem) — stops the
  running CLI, transactionally deletes the current session and all
  dependent rows (turns, session_files, session_refs, checkpoints,
  search_index FTS entries) from session-store.db, then restarts
  with a fresh session. Confirms before deleting and shows the first
  8 chars of the session id so the user can sanity-check.

The current session id is resolved from TerminalSessionService.LastResumeSessionId
when known (we used --resume=<id>), otherwise from the most-recently-updated
session for the workspace in session-store.db.

Switches the toolbar from VS's native command toolbar to a custom WPF
toolbar hosted inside TerminalToolWindowControl. VS tool window
toolbars are left-aligned only; the WPF toolbar lets the buttons sit
top-right, matching VS Code's terminal placement.

WPF buttons invoke handler methods on the package via Action delegates
exposed through VsServices.Instance.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@sailro
Copy link
Copy Markdown
Owner

sailro commented Apr 21, 2026

Thank you @bommerts for this contribution.

I get the impression that the toolbar is causing resize synchronization issues on the device; for me, it’s consistently broken. I think adding XAML components prevents the system from correctly targeting the right control to force a refresh.

I’ll also look into whether it’s possible to add the icons to the toolbar frame instead of adding a bar that ultimately takes up space.

Finally, there are rendering issues related to how VS handles themes.

That’s why I’m always hesitant whenever we start introducing UI elements. It takes a lot of code and effort to meet VS’s constraints.

Comment thread src/CopilotCliIde/Polyfills.cs Outdated
// Polyfills for C# language features that target newer BCLs but only require
// internal sentinel types. .NET Framework 4.7.2 doesn't ship these — they're
// declared here so we can use `record`, `init`, and `required` in this assembly.
namespace System.Runtime.CompilerServices
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

uneeded polyfills, we can use regular structs instead of records

@@ -0,0 +1,16 @@
using System.Text.RegularExpressions;

namespace CopilotCliIde;
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Guid.TryParse is not usable here ?


namespace CopilotCliIde;

// VS-themed picker for resuming a previous Copilot CLI session.
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

the AI is lying :p it is not themed at all. VS is now providing themed styles for all controls but you have to wire stuff a bit. With my black-theme the rendering is a bit strange.

Content = _termControl;

// Attach to session early — Resize may fire before OnLoaded
// Two-row layout: a thin right-aligned toolbar above the terminal control.
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

this layout brakes the terminal rendering.

@bommerts
Copy link
Copy Markdown
Contributor Author

Thank you @bommerts for this contribution.

I get the impression that the toolbar is causing resize synchronization issues on the device; for me, it’s consistently broken. I think adding XAML components prevents the system from correctly targeting the right control to force a refresh.

I’ll also look into whether it’s possible to add the icons to the toolbar frame instead of adding a bar that ultimately takes up space.

Finally, there are rendering issues related to how VS handles themes.

That’s why I’m always hesitant whenever we start introducing UI elements. It takes a lot of code and effort to meet VS’s constraints.

Gotcha. I was wondering the same. I'm also curious when the VS team will include GitHub Copilot CLI native to VS because it is so much better than GitHub Copilot Chat, effectively removing the need for this repo. Or maybe they'll merge this repo into VS :)

Thanks for the detailed feedback above. I'll take a pass at fixing these issues.

- Drop Polyfills.cs; convert SessionInfo / SessionQueryResult to plain
  readonly structs and Row to a regular class (no records, no required/init).
- Replace SessionId regex with Guid.TryParseExact ("D" format).
- Revert custom WPF toolbar; restore the native VSCT ToolWindowToolbar with
  the three buttons. The WPF Grid wrapper above the native TerminalControl
  HWND host caused resize-sync issues for the maintainer; the native toolbar
  avoids that and matches every other VS tool window's left-aligned
  convention (Solution Explorer, Output, Error List, etc.).

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@bommerts
Copy link
Copy Markdown
Contributor Author

Pushed 6b3e1ee addressing all four review comments:

  • Polyfills: deleted; SessionInfo / SessionQueryResult are now plain readonly structs with explicit ctors, and Row is a regular class.
  • SessionId.IsValid: now Guid.TryParseExact(id, "D", out _) — same guarantee, one line.
  • Layout / resize sync: reverted the custom WPF toolbar. TerminalControl is a native HWND host, and wrapping it in a Grid row was causing the resize-sync issues you saw. Restored the native VSCT ToolWindowToolbar with the three buttons — no XAML above the terminal anymore.
  • Right-alignment: investigated alternatives (WPF Adorner overlay, post-load visual-tree injection into the rendered native toolbar, IVsWindowFrame caption customization) and concluded each either re-introduces the same HWND/airspace issue or is exactly the kind of brittle UI hackery you flagged. Going with the native left-aligned toolbar matches every other first-party VS tool window (Solution Explorer, Output, Error List, Test Explorer, Git Changes), so the placement should now feel native.
  • Dialog theming: still on the list — I''ll do a follow-up commit wiring ImageThemingUtilities and the proper themed resource keys so the picker stops looking off in the dark/black themes.

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: Tool window toolbar with session history

2 participants