Skip to content

Commit 6c85d01

Browse files
yotsudaclaude
andcommitted
Simplify console title management
- Replace Dictionary with HashSet for tracking titled PIDs - Remove name recycling logic for simpler implementation - Rename AssignNameToPid to TryAssignNameToPid - Return null when PID already has a title to prevent duplicates - Fix trailing whitespace in PowerShellTools.cs 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent ed39365 commit 6c85d01

2 files changed

Lines changed: 12 additions & 22 deletions

File tree

PowerShell.MCP.Proxy/Services/ConsoleSessionManager.cs

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,9 @@ public class ConsoleSessionManager
5050
private readonly Queue<string> _shuffledNames = new();
5151

5252
/// <summary>
53-
/// Mapping from pwsh PID to assigned name (for recycling names when console closes)
53+
/// Set of pwsh PIDs that have been assigned a console title
5454
/// </summary>
55-
private readonly Dictionary<int, string> _pidToName = new();
55+
private readonly HashSet<int> _titledPids = new();
5656

5757
/// <summary>
5858
/// Path to the category lock file
@@ -203,7 +203,7 @@ public HashSet<int> ConsumeKnownBusyPids()
203203
}
204204

205205
/// <summary>
206-
/// Clears a dead pipe from active pipe and busy tracking, and recycles the console name
206+
/// Clears a dead pipe from active pipe and busy tracking
207207
/// </summary>
208208
public void ClearDeadPipe(string pipeName)
209209
{
@@ -217,14 +217,7 @@ public void ClearDeadPipe(string pipeName)
217217
if (pid.HasValue)
218218
{
219219
_knownBusyPids.Remove(pid.Value);
220-
221-
// Recycle the console name back to the queue
222-
if (_pidToName.TryGetValue(pid.Value, out var name))
223-
{
224-
_pidToName.Remove(pid.Value);
225-
_shuffledNames.Enqueue(name);
226-
Console.Error.WriteLine($"[INFO] ConsoleSessionManager: Recycled name '{name}' from dead pipe '{pipeName}'");
227-
}
220+
_titledPids.Remove(pid.Value);
228221
}
229222
Console.Error.WriteLine($"[INFO] ConsoleSessionManager: Cleared dead pipe '{pipeName}'");
230223
}
@@ -324,25 +317,21 @@ public IEnumerable<string> EnumerateUnownedPipes()
324317
}
325318

326319
/// <summary>
327-
/// Assigns a console name to the specified pwsh PID. Returns the same name if already assigned.
328-
/// When the console is closed, call ClearDeadPipe to recycle the name.
320+
/// Assigns a console name to the specified pwsh PID. Returns null if already assigned.
329321
/// </summary>
330322
/// <param name="pwshPid">The PID of the PowerShell process</param>
331-
/// <returns>Console name in format "#pwshPid name"</returns>
332-
public string AssignNameToPid(int pwshPid)
323+
/// <returns>Console name in format "#pwshPid name", or null if already titled</returns>
324+
public string? TryAssignNameToPid(int pwshPid)
333325
{
334326
lock (_lock)
335327
{
336-
// Return existing name if already assigned
337-
if (_pidToName.TryGetValue(pwshPid, out var existingName))
338-
return $"#{pwshPid} {existingName}";
328+
if (!_titledPids.Add(pwshPid))
329+
return null;
339330

340-
// Get next name from shuffled queue
341331
if (_shuffledNames.Count == 0)
342332
RefillShuffledNames();
343333

344334
var name = _shuffledNames.Dequeue();
345-
_pidToName[pwshPid] = name;
346335
return $"#{pwshPid} {name}";
347336
}
348337
}

PowerShell.MCP.Proxy/Tools/PowerShellTools.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -235,14 +235,15 @@ private static string GetPidString(string? pipeName)
235235
}
236236

237237
/// <summary>
238-
/// Sets the console window title (fire and forget)
238+
/// Sets the console window title if not already set
239239
/// </summary>
240240
private static async Task SetConsoleTitleAsync(IPowerShellService powerShellService, string pipeName, CancellationToken cancellationToken)
241241
{
242242
var pid = ConsoleSessionManager.GetPidFromPipeName(pipeName);
243243
if (pid == null) return;
244244

245-
var title = ConsoleSessionManager.Instance.AssignNameToPid(pid.Value);
245+
var title = ConsoleSessionManager.Instance.TryAssignNameToPid(pid.Value);
246+
if (title == null) return;
246247
await powerShellService.SetWindowTitleAsync(pipeName, title, cancellationToken);
247248
}
248249

0 commit comments

Comments
 (0)