@@ -4,7 +4,9 @@ import * as Effect from "effect/Effect";
44import * as Deferred from "effect/Deferred" ;
55import * as Fiber from "effect/Fiber" ;
66import * as Exit from "effect/Exit" ;
7+ import * as Logger from "effect/Logger" ;
78import * as Queue from "effect/Queue" ;
9+ import * as References from "effect/References" ;
810import * as Schema from "effect/Schema" ;
911import * as Scope from "effect/Scope" ;
1012import * as Sink from "effect/Sink" ;
@@ -209,50 +211,116 @@ it.layer(NodeServices.layer)("effect-acp protocol", (it) => {
209211 } ) ,
210212 ) ;
211213
212- it . effect ( "keeps invalid core notification values only in the schema cause" , ( ) =>
214+ // clanka once sent a usage_update without `used` and `size`. One bad update
215+ // must not end a session whose later updates are fine.
216+ it . effect ( "drops a session/update that fails to decode and keeps reading" , ( ) =>
213217 Effect . gen ( function * ( ) {
214218 const secret = "acp-core-notification-secret-sentinel" ;
215219 const { stdio, input } = yield * makeInMemoryStdio ( ) ;
216- const termination = yield * Deferred . make < AcpError . AcpError > ( ) ;
220+ const events : Array < AcpProtocol . AcpProtocolLogEvent > = [ ] ;
221+ const warnings : Array < unknown > = [ ] ;
222+ let terminated = false ;
217223 let transformCalls = 0 ;
218- yield * AcpProtocol . makeAcpPatchedProtocol ( {
224+ const transport = yield * AcpProtocol . makeAcpPatchedProtocol ( {
219225 stdio,
220226 serverRequestMethods : new Set ( ) ,
227+ logIncoming : true ,
228+ logger : ( event ) =>
229+ Effect . sync ( ( ) => {
230+ events . push ( event ) ;
231+ } ) ,
221232 transformSessionUpdate : ( notification ) => {
222233 transformCalls += 1 ;
223234 return notification ;
224235 } ,
225- onTermination : ( error ) => Deferred . succeed ( termination , error ) . pipe ( Effect . asVoid ) ,
236+ onTermination : ( ) =>
237+ Effect . sync ( ( ) => {
238+ terminated = true ;
239+ } ) ,
240+ } ) . pipe (
241+ Effect . provide (
242+ Logger . layer ( [
243+ Logger . make ( ( { logLevel, fiber } ) => {
244+ if ( logLevel === "Warn" )
245+ warnings . push ( fiber . getRef ( References . CurrentLogAnnotations ) ) ;
246+ } ) ,
247+ ] ) ,
248+ ) ,
249+ ) ;
250+ const received = yield * Deferred . make < AcpProtocol . AcpIncomingNotification > ( ) ;
251+ yield * transport . incoming . pipe (
252+ Stream . runForEach ( ( notification ) => Deferred . succeed ( received , notification ) ) ,
253+ Effect . forkScoped ,
254+ ) ;
255+
256+ const send = ( params : unknown ) =>
257+ Queue . offer (
258+ input ,
259+ encoder . encode (
260+ `${ encodeUnknownJsonString ( { jsonrpc : "2.0" , method : "session/update" , params } ) } \n` ,
261+ ) ,
262+ ) ;
263+ const malformed = {
264+ sessionId : "session-1" ,
265+ update : { sessionUpdate : "usage_update" , cost : { amount : 1 , currency : secret } } ,
266+ } ;
267+ yield * send ( malformed ) ;
268+ yield * send ( malformed ) ;
269+ yield * send ( {
270+ sessionId : "session-1" ,
271+ update : { sessionUpdate : "usage_update" , used : 10 , size : 100 } ,
226272 } ) ;
227273
228- yield * Queue . offer (
229- input ,
230- encoder . encode (
231- `${ encodeUnknownJsonString ( {
232- jsonrpc : "2.0" ,
233- method : "session/update" ,
234- params : {
235- sessionId : { secret } ,
236- update : {
237- sessionUpdate : "plan" ,
238- entries : [ ] ,
239- } ,
240- } ,
241- } ) } \n`,
242- ) ,
274+ const notification = yield * TestClock . withLive (
275+ Deferred . await ( received ) . pipe ( Effect . timeout ( "2 seconds" ) ) ,
243276 ) ;
277+ assert . equal ( notification . _tag , "SessionUpdate" ) ;
278+ assert . deepEqual ( notification . params , {
279+ sessionId : "session-1" ,
280+ update : { sessionUpdate : "usage_update" , used : 10 , size : 100 } ,
281+ } ) ;
282+ assert . isFalse ( terminated ) ;
283+ assert . equal ( transformCalls , 1 ) ;
284+
285+ const decodeFailed = events . find ( ( { stage } ) => stage === "decode_failed" ) ;
286+ assert . deepInclude ( decodeFailed ?. payload as object , {
287+ operation : "decode-notification-payload" ,
288+ method : "session/update" ,
289+ issueSummary : "update.used missing" ,
290+ } ) ;
291+ // The repeated bad update is dropped again but warned about once.
292+ assert . lengthOf (
293+ events . filter ( ( { stage } ) => stage === "decode_failed" ) ,
294+ 2 ,
295+ ) ;
296+ assert . lengthOf ( warnings , 1 ) ;
297+ assert . deepInclude ( warnings [ 0 ] as object , {
298+ issueSummary : "update.used missing" ,
299+ } ) ;
300+ assert . notInclude ( encodeUnknownJsonString ( { decodeFailed, warnings } ) , secret ) ;
301+ } ) ,
302+ ) ;
244303
245- const error = yield * Deferred . await ( termination ) ;
246- assert . equal ( transformCalls , 0 ) ;
247- assert . instanceOf ( error , AcpError . AcpProtocolParseError ) ;
248- const parseError = error as AcpError . AcpProtocolParseError ;
249- const { cause, ...directDiagnostics } = parseError ;
304+ it . effect ( "keeps invalid core notification values only in the schema cause" , ( ) =>
305+ Effect . gen ( function * ( ) {
306+ const secret = "acp-core-notification-secret-sentinel" ;
307+ const cause = yield * Schema . decodeUnknownEffect ( AcpSchema . SessionNotification ) ( {
308+ sessionId : { secret } ,
309+ update : { sessionUpdate : "plan" , entries : [ ] } ,
310+ } ) . pipe ( Effect . flip ) ;
311+ const parseError = AcpError . AcpProtocolParseError . fromSchemaError (
312+ "decode-notification-payload" ,
313+ "session/update" ,
314+ cause ,
315+ ) ;
316+ const { cause : directCause , ...directDiagnostics } = parseError ;
250317 assert . equal ( parseError . operation , "decode-notification-payload" ) ;
251318 assert . equal ( parseError . method , "session/update" ) ;
252319 assert . isAbove ( parseError . issueCount ?? 0 , 0 ) ;
253320 assert . include ( parseError . issueKinds ?? [ ] , "Pointer" ) ;
254321 assert . isAbove ( parseError . maximumPathDepth ?? 0 , 0 ) ;
255- assert . isTrue ( Schema . isSchemaError ( cause ) ) ;
322+ assert . equal ( parseError . issueSummary , "sessionId wrong type" ) ;
323+ assert . isTrue ( Schema . isSchemaError ( directCause ) ) ;
256324 assert . notInclude ( parseError . message , secret ) ;
257325 assert . notInclude ( encodeUnknownJsonString ( directDiagnostics ) , secret ) ;
258326 } ) ,
0 commit comments