-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathsession-store.ts
More file actions
215 lines (192 loc) · 5.64 KB
/
Copy pathsession-store.ts
File metadata and controls
215 lines (192 loc) · 5.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/**
* SessionStore - Pure data management for debug sessions
*
* This class is extracted from SessionManager to handle all session
* state management without external dependencies. This improves
* testability and follows the Single Responsibility Principle.
*/
import { v4 as uuidv4 } from 'uuid';
import {
DebugLanguage,
SessionState,
SessionLifecycleState,
ExecutionState,
DebugSessionInfo,
Breakpoint,
AdapterPolicy,
getPolicyForLanguage
} from '@debugmcp/shared';
import { SessionNotFoundError } from '../errors/debug-errors.js';
/**
* Parameters for creating a new debug session
*/
export interface CreateSessionParams {
language: DebugLanguage;
name?: string;
executablePath?: string; // Language-agnostic executable path
}
import { IProxyManager } from '../proxy/proxy-manager.js';
export interface ToolchainValidationState {
compatible: boolean;
toolchain: string;
message?: string;
suggestions?: string[];
behavior?: string;
binaryInfo?: Record<string, unknown>;
}
/**
* Internal session representation with full details
*/
export interface ManagedSession extends DebugSessionInfo {
executablePath?: string; // Language-agnostic executable path
proxyManager?: IProxyManager;
breakpoints: Map<string, Breakpoint>;
// New state model fields
sessionLifecycle: SessionLifecycleState;
executionState?: ExecutionState;
logDir?: string;
toolchainValidation?: ToolchainValidationState;
// True once the first 'stopped' event after launch has been observed.
// Used by the auto-continue trigger to identify the initial entry stop
// even when the adapter reports a non-'entry' reason (e.g., js-debug
// emits 'pause' from its post-attach forced pause).
firstStopHandled?: boolean;
// True for sessions established via attach_to_process. Attach targets may
// run on a remote filesystem (container, pod, other machine), so host-side
// file existence checks do not apply to their source paths.
attachMode?: boolean;
}
/**
* SessionStore manages the lifecycle and state of debug sessions
* without any external dependencies, making it highly testable.
*/
export class SessionStore {
private sessions: Map<string, ManagedSession> = new Map();
/**
* Selects the appropriate adapter policy based on language
*/
public selectPolicy(language: DebugLanguage): AdapterPolicy {
return getPolicyForLanguage(language);
}
/**
* Creates a new debug session
*/
createSession(params: CreateSessionParams): DebugSessionInfo {
const { language, name, executablePath } = params;
const sessionId = uuidv4();
const sessionName = name || `session-${sessionId.substring(0, 8)}`;
// Validate language
if (!Object.values(DebugLanguage).includes(language)) {
throw new Error(`Language '${language}' is not supported.`);
}
// Use policy to resolve executable path
const policy = this.selectPolicy(language);
const effectiveExecutablePath = policy.resolveExecutablePath(executablePath);
const session: ManagedSession = {
id: sessionId,
name: sessionName,
language: language,
state: SessionState.CREATED,
createdAt: new Date(),
updatedAt: new Date(),
breakpoints: new Map<string, Breakpoint>(),
executablePath: effectiveExecutablePath,
proxyManager: undefined,
// Initialize new state model
sessionLifecycle: SessionLifecycleState.CREATED,
executionState: undefined,
};
this.sessions.set(sessionId, session);
return {
id: sessionId,
name: sessionName,
language: session.language,
state: session.state,
createdAt: session.createdAt,
updatedAt: session.updatedAt
};
}
/**
* Retrieves a session by ID
*/
get(sessionId: string): ManagedSession | undefined {
return this.sessions.get(sessionId);
}
/**
* Retrieves a session by ID, throwing if not found
*/
getOrThrow(sessionId: string): ManagedSession {
const session = this.sessions.get(sessionId);
if (!session) {
throw new SessionNotFoundError(sessionId);
}
return session;
}
/**
* Sets a session directly (for testing purposes)
*/
set(sessionId: string, session: ManagedSession): void {
this.sessions.set(sessionId, session);
}
/**
* Updates session fields
*/
update(sessionId: string, updates: Partial<ManagedSession>): void {
const session = this.getOrThrow(sessionId);
Object.assign(session, updates);
session.updatedAt = new Date();
}
/**
* Updates only the session state
*/
updateState(sessionId: string, newState: SessionState): void {
const session = this.getOrThrow(sessionId);
if (session.state !== newState) {
session.state = newState;
session.updatedAt = new Date();
}
}
/**
* Removes a session
*/
remove(sessionId: string): boolean {
return this.sessions.delete(sessionId);
}
/**
* Gets all sessions as DebugSessionInfo (public interface)
*/
getAll(): DebugSessionInfo[] {
return Array.from(this.sessions.values()).map(s => ({
id: s.id,
name: s.name,
language: s.language,
state: s.state,
createdAt: s.createdAt,
updatedAt: s.updatedAt
}));
}
/**
* Gets all sessions with full internal data
*/
getAllManaged(): ManagedSession[] {
return Array.from(this.sessions.values());
}
/**
* Checks if a session exists
*/
has(sessionId: string): boolean {
return this.sessions.has(sessionId);
}
/**
* Gets the number of sessions
*/
size(): number {
return this.sessions.size;
}
/**
* Clears all sessions
*/
clear(): void {
this.sessions.clear();
}
}