Skip to content

Commit 5a5859f

Browse files
fix(acp): warn once per distinct undecodable notification
An agent that sends a bad shape repeats it on every update, so the warning is logged once per method and issue summary for each connection. The protocol log still records every dropped notification. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 095be28 commit 5a5859f

2 files changed

Lines changed: 24 additions & 7 deletions

File tree

‎packages/effect-acp/src/protocol.test.ts‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -260,10 +260,12 @@ it.layer(NodeServices.layer)("effect-acp protocol", (it) => {
260260
`${encodeUnknownJsonString({ jsonrpc: "2.0", method: "session/update", params })}\n`,
261261
),
262262
);
263-
yield* send({
263+
const malformed = {
264264
sessionId: "session-1",
265265
update: { sessionUpdate: "usage_update", cost: { amount: 1, currency: secret } },
266-
});
266+
};
267+
yield* send(malformed);
268+
yield* send(malformed);
267269
yield* send({
268270
sessionId: "session-1",
269271
update: { sessionUpdate: "usage_update", used: 10, size: 100 },
@@ -286,6 +288,12 @@ it.layer(NodeServices.layer)("effect-acp protocol", (it) => {
286288
method: "session/update",
287289
issueSummary: "update.used missing",
288290
});
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);
289297
assert.deepInclude(warnings[0] as object, {
290298
issueSummary: "update.used missing",
291299
});

‎packages/effect-acp/src/protocol.ts‎

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,7 @@ export const makeAcpPatchedProtocol = Effect.fn("makeAcpPatchedProtocol")(functi
234234
outstandingAcknowledgements: new Set(),
235235
});
236236
const nextRequestId = yield* Ref.make(1);
237+
const warnedDecodeFailures = new Set<string>();
237238
const terminationHandled = yield* Ref.make(false);
238239
const terminationFailure = yield* Deferred.make<never, AcpError.AcpError>();
239240
const extPending = yield* Ref.make(new Map<string, AcpPendingRequest>());
@@ -512,22 +513,30 @@ export const makeAcpPatchedProtocol = Effect.fn("makeAcpPatchedProtocol")(functi
512513

513514
// A notification has no reply to fail, so one the schema rejects is logged
514515
// and dropped. Ending the connection over it would kill a working session
515-
// because one update, possibly from a newer spec, did not parse.
516+
// because one update, possibly from a newer spec, did not parse. An agent
517+
// repeats a bad shape on every update, so each distinct failure is warned
518+
// about once per connection.
516519
const dropUndecodableNotification = (method: string) => (cause: Schema.SchemaError) => {
517520
const error = AcpError.AcpProtocolParseError.fromSchemaError(
518521
"decode-notification-payload",
519522
method,
520523
cause,
521524
);
525+
const diagnostics = decodeFailureDiagnostics(error);
526+
const key = `${method}\n${error.issueSummary}`;
527+
const firstOfItsKind = !warnedDecodeFailures.has(key);
528+
warnedDecodeFailures.add(key);
522529
return logProtocol({
523530
direction: "incoming",
524531
stage: "decode_failed",
525-
payload: decodeFailureDiagnostics(error),
532+
payload: diagnostics,
526533
}).pipe(
527534
Effect.andThen(
528-
Effect.logWarning("Dropped an ACP notification that failed to decode").pipe(
529-
Effect.annotateLogs(decodeFailureDiagnostics(error)),
530-
),
535+
firstOfItsKind
536+
? Effect.logWarning("Dropped an ACP notification that failed to decode").pipe(
537+
Effect.annotateLogs(diagnostics),
538+
)
539+
: Effect.void,
531540
),
532541
);
533542
};

0 commit comments

Comments
 (0)