@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
63 lines • 2.24 kB
JavaScript
import { serializeJsonLine } from "./jsonl.js";
export const EVENT_RESYNC_CAPABILITY = "event_resync";
export function isCorrelatedReply(value) {
return (typeof value === "object" &&
value !== null &&
"type" in value &&
value.type === "response" &&
"id" in value &&
typeof value.id === "string");
}
class SocketPeerLivenessError extends Error {
constructor() {
super("socket peer missed its liveness ping");
this.name = "SocketPeerLivenessError";
}
}
/** One gap and one outstanding ping, regardless of how many events the producer emits. */
export class SocketPeerRecovery {
constructor(write) {
this.awaitingPing = false;
this.pingBuffered = false;
this.replyLost = false;
this.write = write;
// Negotiate the heartbeat before session traffic can fill the output side.
// Pulses travel client -> host even while host -> client is backpressured.
this.pingBuffered = true;
this.write(serializeJsonLine({ type: "rpc_ping" }));
}
get pending() {
return this.reason !== undefined;
}
/** The first shed names the gap's reason; a supersession while pending keeps it. */
shed(reason) {
this.reason ??= reason;
}
/** A fresh set_client_info is the existing connection-scoped acknowledgement. */
acknowledge() {
this.awaitingPing = false;
}
loseReply() {
this.replyLost = true;
}
ping() {
if (this.awaitingPing)
throw new SocketPeerLivenessError();
this.awaitingPing = true;
if (!this.pingBuffered) {
this.pingBuffered = true;
this.write(serializeJsonLine({ type: "rpc_ping" }));
}
}
/** The gap line that opens the recovery; taken once the peer drains again, before the cut's frames. */
takeGap() {
const reason = this.reason;
this.reason = undefined;
this.awaitingPing = false;
this.pingBuffered = false;
const replyLost = this.replyLost;
this.replyLost = false;
return serializeJsonLine({ type: "resync_required", reason, replyLost });
}
}
//# sourceMappingURL=socket-peer-recovery.js.map