feat: 2025-11-25 review fixes (#201)

* fix: review fixes of the latest changes

* fix: updated unit tests

* chore: more conformance compliance improvements

* chore: reverted protocol changes

* chore: more compliance improvements

* chore: more conformance compliance improvements

* chore: improvements to the backcompat and updated to Tool unit tests

* chore: ci update
This commit is contained in:
yehorsobko 2026-03-03 16:33:28 +01:00 • committed by GitHub
commit 973b46b8f7
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -182,7 +182,8 @@ public actor StatefulHTTPServerTransport: Transport {
return .error(
statusCode: 405,
.invalidRequest("Method Not Allowed"),
sessionID: sessionID
sessionID: sessionID,
extraHeaders: [HTTPHeaderName.allow: "GET, POST, DELETE"]
)
}
}
@ -238,6 +239,15 @@ public actor StatefulHTTPServerTransport: Transport {
}
private func handleInitializationRequest(_ body: Data, request: HTTPRequest) -> HTTPResponse {
// Reject re-initialization at the transport level
if sessionID != nil {
return .error(
statusCode: 400,
.invalidRequest("Bad Request: Session already initialized"),
sessionID: sessionID
)
}
// Generate session ID
let newSessionID = sessionIDGenerator.generateSessionID()
@ -357,9 +367,7 @@ public actor StatefulHTTPServerTransport: Transport {
supportedProtocolVersions: Version.supported
)
// Only run session validation for DELETE (not all validators)
let sessionValidator = SessionValidator()
if let errorResponse = sessionValidator.validate(request, context: context) {
if let errorResponse = validationPipeline.validate(request, context: context) {
return errorResponse
}
@ -481,14 +489,27 @@ public actor StatefulHTTPServerTransport: Transport {
continuation: AsyncThrowingStream<Data, Swift.Error>.Continuation,
protocolVersion: String
) {
// Priming events with empty data are only safe for clients >= 2025-11-25
guard protocolVersion >= "2025-11-25" else { return }
// Priming events with empty data are only safe for clients >= 2025-03-26
guard protocolVersion >= "2025-03-26" else { return }
let primingEventID = storeEvent(streamID: streamID, message: nil)
let primingEvent = SSEEvent.priming(id: primingEventID, retry: retryInterval)
continuation.yield(primingEvent.formatted())
}
private func extractProtocolVersion(from body: Data, request: HTTPRequest) -> String {
// For initialize requests, extract from the request body params
if let json = try? JSONSerialization.jsonObject(with: body) as? [String: Any],
let method = json["method"] as? String, method == "initialize",
let params = json["params"] as? [String: Any],
let version = params["protocolVersion"] as? String
{
return version
}
// For other requests, use the header
return request.header(HTTPHeaderName.protocolVersion) ?? Version.latest
}
// MARK: - Session Helpers
private func sessionHeaders() -> [String: String] {
@ -504,17 +525,16 @@ public actor StatefulHTTPServerTransport: Transport {
return id.utf8.allSatisfy { $0 >= 0x21 && $0 <= 0x7E }
}
private func extractProtocolVersion(from body: Data, request: HTTPRequest) -> String {
// For initialize requests, extract from the request body params
if let json = try? JSONSerialization.jsonObject(with: body) as? [String: Any],
let method = json["method"] as? String, method == "initialize",
let params = json["params"] as? [String: Any],
let version = params["protocolVersion"] as? String
{
return version
}
// For other requests, use the header
return request.header(HTTPHeaderName.protocolVersion) ?? Version.latest
// MARK: - Stream Control
/// Closes the SSE stream for a specific request without sending a response.
///
/// Used to trigger client reconnection mid-call (SEP-1699). The response,
/// when eventually sent, will be stored and replayed to the reconnected stream.
package func closeSSEStream(forRequestID requestID: String) {
guard let continuation = requestSSEContinuations[requestID] else { return }
continuation.finish()
requestSSEContinuations.removeValue(forKey: requestID)
}
// MARK: - Termination