22
33# ⚡ TSG — Terminal State Guard
44
5- ### Cross-platform CLI for Copilot Performance Boosting, Safe Monitoring & Session Recovery
5+ ### The Complete Terminal Intelligence Platform for Developers
66
77[ ![ NuGet] ( https://img.shields.io/nuget/v/TerminalStateGuard?style=for-the-badge&logo=nuget&color=004880 )] ( https://www.nuget.org/packages/TerminalStateGuard )
88[ ![ License: MIT] ( https://img.shields.io/badge/License-MIT-yellow.svg?style=for-the-badge )] ( LICENSE )
99[ ![ .NET 10] ( https://img.shields.io/badge/.NET-10.0-512BD4?style=for-the-badge&logo=dotnet )] ( https://dotnet.microsoft.com )
1010[ ![ Platform] ( https://img.shields.io/badge/Platform-Windows%20%7C%20Linux-blue?style=for-the-badge )] ( )
1111
12- ** One command to install. Keyboard shortcuts ready. Zero config. **
12+ ** Real-time window tracking · Live tab detection · Process management · Session recovery **
1313
14- [ Installation] ( #-installation ) · [ Commands] ( #-commands ) · [ Monitor ] ( #-safe-monitor ) · [ Recovery ] ( #-session-recovery ) · [ Doctor ] ( #-doctor )
14+ [ Installation] ( #-installation ) · [ Commands] ( #-commands ) · [ Windows ] ( #-window-tracking ) · [ Processes ] ( #-process-manager ) · [ Monitor ] ( #-safe-monitor ) · [ Recovery ] ( #-session-recovery )
1515
1616---
1717
@@ -24,39 +24,209 @@ dotnet tool install -g TerminalStateGuard
2424tsg install
2525```
2626
27- ** Done.** Reopen your terminal — shortcuts and integration are active.
27+ ** Done.** Reopen your terminal — shortcuts, profiles, and integration are active.
2828
2929### What ` tsg install ` does:
3030
3131| Platform | Action |
3232| ----------| --------|
33- | ** Windows** | Installs [ Windows Terminal Fragment] ( https://learn.microsoft.com/en-us/windows/terminal/json-fragment-extensions ) for native shortcut integration |
33+ | ** Windows** | Installs [ Windows Terminal Fragment] ( https://learn.microsoft.com/en-us/windows/terminal/json-fragment-extensions ) with dedicated profiles & icons |
3434| ** Windows** | Configures PSReadLine keyboard shortcuts in PowerShell profile |
35+ | ** Windows** | Deploys ` FileSystemWatcher ` for event-driven state tracking |
3536| ** Linux** | Adds shell aliases and keybindings to ` .bashrc ` /` .zshrc ` |
36- | ** Both** | Deploys monitoring scripts to ` ~/.tsg/ ` |
37+ | ** Both** | Deploys scripts to ` ~/.tsg/ ` and creates SQLite database |
3738
3839## ⌨️ Commands
3940
41+ ### Core
42+
4043``` bash
4144tsg install # Setup scripts, shortcuts & terminal integration
45+ tsg uninstall # Remove configuration
46+ tsg doctor # 🩺 Diagnose environment issues
47+ tsg config # ⚙️ Show/set configuration (max-snapshots, etc.)
48+ tsg version # Show version
49+ ```
50+
51+ ### Copilot Performance
52+
53+ ``` bash
4254tsg boost # ⚡ Elevate Copilot process priority (Admin/sudo)
4355tsg monitor # 📊 Safe live monitor with diagnostics
4456tsg status # 📋 Quick health check
45- tsg recover # 🔄 Recover terminal tabs + Copilot sessions
4657tsg restore # 🔄 Revert all priority changes
47- tsg doctor # 🩺 Diagnose environment issues
48- tsg uninstall # 🗑️ Remove configuration
58+ tsg focus # 🎯 Focus ALL resources on stuck process (Admin)
59+ ```
60+
61+ ### Window & Session Management
62+
63+ ``` bash
64+ tsg windows # Show active & recently closed windows with tabs
65+ tsg windows -i # 🖥️ Interactive window dashboard
66+ tsg windows --history # Browse window history from database
67+ tsg windows --restore # Restore closed windows with all tabs
68+ tsg recover # 🔄 Recover terminal tabs + Copilot sessions
69+ tsg snapshots # 📸 List all saved terminal snapshots
70+ tsg snapshots --all # Show all with tab details
71+ tsg capture # Capture current terminal state to SQLite
72+ ```
73+
74+ ### Process Manager
75+
76+ ``` bash
77+ tsg processes # Show dev processes with ports & resource usage
78+ tsg ps # Alias for tsg processes
79+ tsg ps -i # 🔧 Interactive process manager with navigation
80+ tsg ps --orphans # ⚠️ Show only orphaned background processes
81+ tsg ps --ports # 🌐 Show only port-binding processes
82+ tsg ps --kill < PID> # 💀 Kill a process (with confirmation)
83+ tsg ps --kill-tree < PID> # 🌳 Kill process tree
84+ ```
85+
86+ ### Database Query
87+
88+ ``` bash
89+ tsg db " SELECT * FROM windows" # Query terminal database
90+ tsg db " SELECT * FROM events ORDER BY ts DESC LIMIT 10"
4991```
5092
5193### Keyboard Shortcuts
5294
53- | Shortcut | Action |
54- | ----------| --------|
55- | ` Ctrl+Alt+B ` | Boost |
56- | ` Ctrl+Alt+M ` | Monitor |
57- | ` Ctrl+Alt+S ` | Status |
58- | ` Ctrl+Alt+F ` | Recover |
59- | ` Ctrl+Alt+R ` | Restore |
95+ | Shortcut | Action | Terminal Profile |
96+ | ----------| --------| -----------------|
97+ | ` Ctrl+Alt+B ` | Boost | — |
98+ | ` Ctrl+Alt+M ` | Monitor | — |
99+ | ` Ctrl+Alt+S ` | Status | — |
100+ | ` Ctrl+Alt+F ` | Recover | — |
101+ | ` Ctrl+Alt+R ` | Restore | — |
102+ | ` Ctrl+Alt+W ` | Window Dashboard | 🖼️ TSG Windows |
103+ | ` Ctrl+Alt+N ` | Snapshots | 📸 TSG Snapshots |
104+ | ` Ctrl+Alt+P ` | Process Manager | 🔧 TSG Processes |
105+
106+ ## 🖥️ Window Tracking
107+
108+ TSG provides ** real-time window and tab tracking** using COM IUIAutomation — no stale data.
109+
110+ ```
111+ 🪟 Terminal Windows — 2 active, 1 recently closed
112+ 📅 2026-04-18 12:15:05 📑 8 tabs 🤖 5 [live] 👁️ UIA:2
113+
114+ ── Active Windows ──
115+ 🟢 Window 1 [a1b2c3d4e5f6] 📑 6 tabs 🤖 3
116+ 📅 Opened: 2026-04-18 10:30:00 👁️ Last seen: 2026-04-18 12:15:05
117+ 🤖 Build microservice API
118+ 🤖 Fix auth middleware
119+ 🤖 Debug test failures
120+ 📂 Project Documentation
121+ 📂 Terminal Configs
122+ 📂 Source Repos
123+
124+ 🟢 Window 2 [f6e5d4c3b2a1] 📑 2 tabs 🤖 2
125+ 🤖 Deploy staging
126+ 🤖 Monitor logs
127+
128+ ── Recently Closed ──
129+ 🔴 Window 3 [x9y8z7w6v5u4] 📑 4 tabs
130+ 📅 Opened: 2026-04-18 08:00:00 ❌ Closed: 2026-04-18 11:45:00
131+ 🤖 Old debug session (restorable)
132+ ```
133+
134+ ### Interactive Dashboard (` tsg windows -i ` )
135+
136+ The interactive dashboard provides a menu-driven interface:
137+
138+ - ** [ R] Restore** — Restore closed windows with all their tabs
139+ - ** [ S] Snapshot** — Take a snapshot of current terminal state
140+ - ** [ H] History** — Browse window open/close timeline from database
141+ - ** [ P] Processes** — Switch to process manager
142+ - ** [ F] Refresh** — Refresh live window data
143+ - ** [ Q] Quit**
144+
145+ ### How Live Detection Works
146+
147+ TSG uses ** COM IUIAutomation** (via P/Invoke with ` CoCreateInstance ` ) to enumerate real terminal windows and tabs in real-time. This replaces the unreliable ` state.json ` which never removes closed tabs.
148+
149+ - ** Primary source:** Live UIA tab enumeration (class ` CASCADIA_HOSTING_WINDOW_CLASS ` )
150+ - ** Fallback:** ` state.json ` replay actions (when UIA is unavailable)
151+ - ** Event tracking:** ` FileSystemWatcher ` on ` state.json ` triggers re-capture on any change
152+ - ** Storage:** All captures stored in SQLite at ` ~/.tsg/terminal.db `
153+
154+ ## 🔧 Process Manager
155+
156+ A comprehensive dev process manager that identifies development servers, background tasks, and orphaned processes.
157+
158+ ```
159+ 🔧 Dev Processes — 45 found | 4200 MB | 3 ports | 2 orphans
160+
161+ ── 🖥️ Terminal (PID 18672) ── (12 processes, 1800 MB)
162+ ▶ 20360 copilot 396.1 MB ⏱️ 1.0h 🕐 9h
163+ 📋 copilot-win32-x64.exe --stdio
164+ 34388 pwsh 146.8 MB ⏱️ 5s 🕐 16m
165+ 📋 "C:\Program Files\PowerShell\7\pwsh.exe"
166+ 48008 wsl 13.1 MB ⏱️ 0s 🕐 1m 🌐 :8080
167+ 📋 wsl.exe bash -lc "npm run dev"
168+
169+ ── ⚠️ Unattributed ── (33 processes, 2400 MB)
170+ 21904 devenv 1329.2 MB ⏱️ 10m 🕐 12h
171+ ...
172+ ```
173+
174+ ### Interactive Mode (` tsg ps -i ` )
175+
176+ Full keyboard-driven process management with viewport scrolling:
177+
178+ | Key | Action |
179+ | -----| --------|
180+ | ` ↑ ` ` ↓ ` | Navigate processes (viewport auto-scrolls) |
181+ | ` Enter ` | Expand process details with impact analysis |
182+ | ` K ` | Kill selected process (with confirmation) |
183+ | ` T ` | Tree-kill process and all children |
184+ | ` O ` | Filter to orphan processes only |
185+ | ` P ` | Filter to port-binding processes only |
186+ | ` C ` | Clean ALL orphan processes |
187+ | ` F ` | Refresh (rescan all processes) |
188+ | ` Q ` | Quit |
189+
190+ ### Process Detail View
191+
192+ Pressing ` Enter ` on a process shows:
193+
194+ ```
195+ ═══ Process Details ═══
196+ Name: node
197+ PID: 48008
198+ Parent: pwsh (34388)
199+ Memory: 256.4 MB
200+ CPU Time: 2m 15s
201+ Uptime: 45 minutes
202+ Terminal: Window 1 (PID 18672)
203+ Ports: :3000, :3001
204+
205+ 📋 Command Line:
206+ node /home/user/project/node_modules/.bin/next dev --port 3000
207+
208+ 👶 Child Processes (3):
209+ PID 48120 node 45.2 MB
210+ PID 48200 node 32.1 MB
211+ PID 48350 esbuild 12.0 MB
212+
213+ ⚠️ Kill Impact Analysis:
214+ 💾 Memory freed: 345.7 MB (4 processes)
215+ 🌐 Ports released: :3000, :3001
216+ 📂 Directories affected: /home/user/project
217+
218+ [K] Kill [T] Tree-kill [←] Back
219+ ```
220+
221+ ### What It Detects
222+
223+ | Category | Detection Method |
224+ | ----------| -----------------|
225+ | ** Terminal processes** | Parent chain tracing to ` WindowsTerminal.exe ` |
226+ | ** Dev servers** | Keywords: node, python, dotnet, cargo, go, java, ruby, etc. |
227+ | ** Listening ports** | P/Invoke ` GetExtendedTcpTable ` from ` iphlpapi.dll ` |
228+ | ** Orphan processes** | Parent PID points to dead/non-existent process |
229+ | ** Resource usage** | WMI ` Win32_Process ` bulk query + working set size |
60230
61231## 📊 Safe Monitor
62232
@@ -103,21 +273,76 @@ Scans Windows Terminal state + Copilot sessions and reopens tabs with `copilot -
103273 💬 Fix DllNotFoundException
104274```
105275
276+ ### Window Restore (` tsg windows --restore ` )
277+
278+ Restores closed windows with their last known tabs from the SQLite database:
279+
280+ ``` bash
281+ tsg windows --restore # Interactive selection from closed windows
282+ ```
283+
284+ ## 📸 Snapshots
285+
286+ Snapshots capture the complete terminal state (windows, tabs, directories, Copilot sessions) at a point in time:
287+
288+ ``` bash
289+ tsg snapshots # List recent snapshots with timestamps
290+ tsg snapshots --all # Show all snapshots with tab details
291+ tsg capture # Take a manual snapshot
292+ ```
293+
294+ ```
295+ 📸 Terminal Snapshots (15 total)
296+ [ 1] 📅 2026-04-18 12:15:05 ⏱️ 2m ago 📺 2 win 📑 8 tabs 🤖 5
297+ [ 2] 📅 2026-04-18 12:10:00 ⏱️ 7m ago 📺 2 win 📑 8 tabs 🤖 5
298+ [ 3] 📅 2026-04-18 11:45:00 ⏱️ 32m ago 📺 3 win 📑 12 tabs 🤖 8
299+ ```
300+
301+ Snapshots are configurable via ` tsg config max-snapshots <N> ` (default: 50, range: 5–1000).
302+
303+ ## 🗄️ SQLite Database
304+
305+ All terminal state is tracked in a local SQLite database at ` ~/.tsg/terminal.db ` :
306+
307+ | Table | Purpose |
308+ | -------| ---------|
309+ | ` captures ` | Point-in-time state captures with quality flags |
310+ | ` capture_windows ` | Windows in each capture |
311+ | ` capture_tabs ` | Tabs with titles, directories, types |
312+ | ` windows ` | Persistent window identity (first_seen, last_seen, closed_at) |
313+ | ` events ` | State change timeline (window_opened, window_closed) |
314+
315+ Query directly with:
316+
317+ ``` bash
318+ tsg db " SELECT * FROM windows WHERE closed_at IS NOT NULL"
319+ tsg db " SELECT * FROM events ORDER BY ts DESC LIMIT 20"
320+ ```
321+
322+ ### Data Quality Flags
323+
324+ | Flag | Source | Reliability |
325+ | ------| --------| -------------|
326+ | ` live ` | COM IUIAutomation real-time | ✅ Highest |
327+ | ` verified ` | UIA confirmed | ✅ High |
328+ | ` trimmed ` | UIA-corrected count | ✅ High |
329+ | ` stale ` | state.json age > threshold | ⚠️ Low |
330+ | ` no-uia ` | UIA unavailable | ⚠️ Fallback |
331+
106332## 🩺 Doctor
107333
108334``` bash
109335tsg doctor
110336```
111337
112- Checks environment, finds stuck sessions, validates setup:
113-
114338```
115339 🩺 TSG Doctor — Environment Check
116340
117341 ✅ .NET 10.0.0
118342 ✅ Shell: C:\Program Files\PowerShell\7\pwsh.exe
119343 ✅ TSG dir: C:\Users\you\.tsg
120344 ✅ Copilot sessions: 12
345+ ✅ Snapshots: 15/50 (max configurable)
121346 ⚠️ 1 session(s) > 20MB — may cause slowness
122347 ✅ Windows Terminal Fragment installed
123348 ✅ Terminal state: 2 windows, 8 tabs
@@ -132,7 +357,8 @@ Checks environment, finds stuck sessions, validates setup:
132357- ✅ All monitoring is ** read-only** — only reads metadata
133358- ✅ Never deletes, trims, or edits ` events.jsonl `
134359- ✅ Session recovery uses native ` copilot --resume `
135- - ✅ Diagnostics show root cause + safe manual recommendations
360+ - ✅ Process kill requires explicit user confirmation with impact analysis
361+ - ✅ All data stored locally in ` ~/.tsg/ ` — no network access
136362
137363## ⚡ Architecture
138364
@@ -143,15 +369,25 @@ tsg (dotnet tool)
143369 │ ├── WindowsHost.cs — Windows Terminal Fragment + PSReadLine
144370 │ └── LinuxHost.cs — bash/zsh aliases + keybindings
145371 ├── Scripts/
146- │ ├── windows/*.ps1 — PowerShell monitoring scripts
372+ │ ├── windows/*.ps1 — PowerShell monitoring & tracking scripts
147373 │ └── linux/*.sh — Bash monitoring scripts
148- ├── CommandRegistry.cs — Lambda-based command routing
374+ ├── CommandRegistry.cs — Lambda-based command routing (C# 14)
375+ ├── Configuration.cs — Persistent settings (max-snapshots, etc.)
376+ ├── StateCapture.cs — Live state capture engine (UIA + state.json)
377+ ├── UiaComHelper.cs — COM IUIAutomation interop via P/Invoke
378+ ├── TerminalDatabase.cs — SQLite temporal database layer
379+ ├── Windows.cs — Window tracking & interactive dashboard
380+ ├── ProcessManager.cs — Dev process manager with interactive UI
381+ ├── Snapshots.cs — Snapshot listing and management
382+ ├── DbQuery.cs — Direct SQL query interface
149383 ├── Installer.cs — Script deployment + profile config
150384 ├── ScriptRunner.cs — Cross-platform script execution
151385 └── Diagnostics.cs — Environment health checks
152386```
153387
154- ** Built with:** .NET 10 · C# 14 · Windows Terminal Fragments API
388+ ** Built with:** .NET 10 · C# 14 · COM IUIAutomation · SQLite · Windows Terminal Fragments API
389+
390+ ** Dependencies:** ` Microsoft.Data.Sqlite ` 10.0.6 (single external dependency)
155391
156392## 📦 Requirements
157393
@@ -168,6 +404,8 @@ tsg uninstall
168404dotnet tool uninstall -g TerminalStateGuard
169405```
170406
407+ Removes all configuration including Fragment profiles, PSReadLine shortcuts, and ` ~/.tsg/ ` scripts. The SQLite database (` ~/.tsg/terminal.db ` ) is preserved for reference.
408+
171409## 📄 License
172410
173411[ MIT] ( LICENSE ) — Made with ⚡ by [ sbay-dev] ( https://github.com/sbay-dev )
0 commit comments