-
Notifications
You must be signed in to change notification settings - Fork 1
Fix/37 vscode dead sessions #39
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jjeliga
wants to merge
3
commits into
main
Choose a base branch
from
fix/37-vscode-dead-sessions
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,113 @@ | ||
| import Foundation | ||
| import SQLite3 | ||
|
|
||
| /// Reads hidden session IDs from VS Code-family IDE global state databases. | ||
| /// | ||
| /// When a user deletes a session in VS Code's Claude sidebar (trash icon), | ||
| /// the extension adds the session ID to `hiddenSessionIds` in its global state | ||
| /// (stored in a SQLite DB). The Claude process often stays alive, so neither | ||
| /// SessionEnd hooks nor PID liveness checks clean up the state file. | ||
| /// | ||
| /// This provides a definitive cleanup signal: if VS Code says a session is | ||
| /// hidden, its state file can be safely removed. | ||
| /// | ||
| /// Discovers all VS Code variants (Code, Insiders, Cursor, Windsurf, etc.) | ||
| /// automatically by scanning Application Support for the standard | ||
| /// `User/globalStorage/state.vscdb` layout. | ||
| public enum VSCodeHiddenSessions { | ||
|
|
||
| /// The key under which the Claude Code extension stores its global state. | ||
| /// This is the `publisher.name` identifier from the extension's package.json. | ||
| private static let extensionStateKey = "Anthropic.claude-code" | ||
|
|
||
| /// Swift equivalent of C's SQLITE_TRANSIENT — tells SQLite to copy the bound value | ||
| /// immediately. The C macro `((sqlite3_destructor_type)-1)` can't be auto-imported. | ||
| private static let sqliteTransient = unsafeBitCast(-1, to: sqlite3_destructor_type.self) | ||
|
|
||
| /// Cached result to avoid scanning Application Support on every 3-second poll. | ||
| /// Thread safety: these are only accessed from SessionStateWatcher's serial ioQueue. | ||
| /// If called from multiple threads, add a lock. | ||
| private static var cachedHiddenIDs = Set<String>() | ||
| private static var cacheTimestamp: Date = .distantPast | ||
| private static let cacheLifetime: TimeInterval = 30.0 | ||
|
|
||
| /// Returns all session IDs that users have hidden/deleted across all | ||
| /// VS Code-family IDEs on this machine. Cached for 30 seconds. | ||
| public static func allHiddenSessionIDs() -> Set<String> { | ||
| let now = Date() | ||
| if now.timeIntervalSince(cacheTimestamp) < cacheLifetime { | ||
| return cachedHiddenIDs | ||
| } | ||
|
|
||
| var result = Set<String>() | ||
| for dbPath in discoverStateDBPaths() { | ||
| result.formUnion(readHiddenIDs(from: dbPath)) | ||
| } | ||
|
|
||
| cachedHiddenIDs = result | ||
| cacheTimestamp = now | ||
| debugLog("[IDEHidden] Refreshed: \(result.count) hidden session IDs") | ||
| return result | ||
| } | ||
|
|
||
| /// Discover VS Code-family global state databases by scanning | ||
| /// `~/Library/Application Support/*/User/globalStorage/state.vscdb`. | ||
| private static func discoverStateDBPaths() -> [String] { | ||
| guard | ||
| let appSupportURL = FileManager.default.urls( | ||
| for: .applicationSupportDirectory, in: .userDomainMask | ||
| ).first | ||
| else { return [] } | ||
|
|
||
| guard | ||
| let entries = try? FileManager.default.contentsOfDirectory( | ||
| at: appSupportURL, | ||
| includingPropertiesForKeys: nil, | ||
| options: .skipsHiddenFiles | ||
| ) | ||
|
jjeliga marked this conversation as resolved.
|
||
| else { return [] } | ||
|
|
||
| return entries.compactMap { dir in | ||
| let dbURL = | ||
| dir | ||
| .appendingPathComponent("User") | ||
| .appendingPathComponent("globalStorage") | ||
| .appendingPathComponent("state.vscdb") | ||
| return FileManager.default.fileExists(atPath: dbURL.path) ? dbURL.path : nil | ||
| } | ||
| } | ||
|
jjeliga marked this conversation as resolved.
|
||
|
|
||
| /// Read `hiddenSessionIds` from a single VS Code global state database. | ||
| private static func readHiddenIDs(from dbPath: String) -> Set<String> { | ||
| var db: OpaquePointer? | ||
| guard | ||
| sqlite3_open_v2(dbPath, &db, SQLITE_OPEN_READONLY | SQLITE_OPEN_NOMUTEX, nil) | ||
| == SQLITE_OK | ||
| else { | ||
| debugLog("[IDEHidden] Failed to open DB: \(dbPath)") | ||
| return [] | ||
| } | ||
| defer { sqlite3_close(db) } | ||
|
|
||
| var stmt: OpaquePointer? | ||
| let query = "SELECT value FROM ItemTable WHERE key = ?" | ||
| guard sqlite3_prepare_v2(db, query, -1, &stmt, nil) == SQLITE_OK else { return [] } | ||
| defer { sqlite3_finalize(stmt) } | ||
|
|
||
| sqlite3_bind_text(stmt, 1, extensionStateKey, -1, sqliteTransient) | ||
|
|
||
| guard sqlite3_step(stmt) == SQLITE_ROW, | ||
| let cString = sqlite3_column_text(stmt, 0) | ||
| else { return [] } | ||
|
|
||
| guard let data = String(cString: cString).data(using: .utf8), | ||
| let dict = try? JSONSerialization.jsonObject(with: data) as? [String: Any], | ||
| let ids = dict["hiddenSessionIds"] as? [String] | ||
| else { | ||
| debugLog("[IDEHidden] Failed to parse hiddenSessionIds from: \(dbPath)") | ||
| return [] | ||
| } | ||
|
|
||
| return Set(ids) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.