@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
85 lines • 4.61 kB
JavaScript
import { legacyResyncMessage } from "./compact-legacy-view.js";
import { recovered } from "./compact-recovery-types.js";
import { MEDIA_PLACEHOLDERS_CAPABILITY } from "./custom-capability.js";
import { resyncCut } from "./legacy-resync-view.js";
export const admissionOf = (ordinal) => `attach-${ordinal}`;
/**
* The checkpoints a host pins for its peers' legacy views (S08a producers): one per held lane-owned session on the
* admission (the attach ordinal) that holds it, and the shed's recovery cut read from them.
*/
export class SessionRecoveryPins {
constructor(acceptance, checkpoints, replay, peers, shedMode) {
this.acceptance = acceptance;
this.checkpoints = checkpoints;
this.replay = replay;
this.peers = peers;
this.shedMode = shedMode;
}
/** One freeze per held lane-owned session, on the admission (the attach ordinal) that holds it for `id`. */
pin(id, sessionIds) {
const socketEpoch = this.peers.socketEpoch(id);
return sessionIds.flatMap((sessionId) => {
const lane = this.acceptance.get(sessionId);
const ordinal = this.peers.ordinals(id)?.get(sessionId);
if (!lane || socketEpoch === undefined)
return [];
const mirror = this.checkpoints.stream({ socketEpoch, sessionId })?.routes[0];
const admissionId = mirror ?? (ordinal === undefined ? undefined : admissionOf(ordinal));
if (admissionId === undefined)
return [];
const route = {
socketEpoch,
admissionId,
routeKind: ordinal === undefined ? "mirror" : "ordinary",
peer: id,
sessionId,
};
return [{ sessionId, lane, sessionEpoch: lane.sessionEpoch, outcome: this.checkpoints.freeze(route, lane) }];
});
}
unpin(pinned) {
for (const { outcome } of pinned)
if (outcome.ok)
this.checkpoints.release(outcome.value.token);
}
/** The admission holding `sessionId` for `id` ends (a detach): its route leaves its stream. */
retire(id, sessionId) {
const socketEpoch = this.peers.socketEpoch(id);
const ordinal = this.peers.ordinals(id)?.get(sessionId);
if (socketEpoch !== undefined && ordinal !== undefined)
this.checkpoints.retireRoute({ socketEpoch, sessionId }, admissionOf(ordinal), "client_detach");
}
/**
* The shed's recovery: checkpoints frozen at the shed, before the actor awaits its drain (R3.C1.5), as the next
* generation of each held admission; released when the recovery ends. The cut owns each root from the freeze to
* that end: a detach, re-attach or supersession before its gap is delivered retires the token, never the root.
*/
resync(id, reason, carried = []) {
const capture = this.shedMode === "capture";
const required = capture ? (this.peers.required?.(id) ?? []) : [];
const attached = [...new Set([...(this.peers.ordinals(id)?.keys() ?? []), ...required])];
const ordinals = new Map(this.peers.ordinals(id));
const pinned = this.pin(id, attached);
const roots = new Map(pinned.map(({ sessionId, outcome }) => [sessionId, outcome.ok ? recovered(outcome.value.retain()) : outcome]));
const view = {
reason,
placeholders: this.peers.has(id, MEDIA_PLACEHOLDERS_CAPABILITY),
// Capture: closed once the attachment held at the shed is gone (carry 30); drop: today's rule.
closedNow: (sessionId) => capture
? !(this.peers.required?.(id)?.includes(sessionId) ||
(ordinals.has(sessionId) && this.peers.ordinals(id)?.get(sessionId) === ordinals.get(sessionId)))
: !(this.peers.ordinals(id)?.has(sessionId) ?? false),
onRelease: () => {
for (const root of roots.values())
if (root.ok)
root.value.end();
this.unpin(pinned);
},
};
const sessions = capture ? [...new Set([...carried, ...attached])] : attached;
return resyncCut(sessions, capture ? () => sessions : () => [...new Set([...sessions, ...(this.peers.ordinals(id)?.keys() ?? [])])], (sessionId) => this.acceptance.has(sessionId)
? legacyResyncMessage(sessionId, roots.get(sessionId))
: Promise.resolve({ message: this.replay.latestMessage(sessionId) }), view, this.replay.track);
}
}
//# sourceMappingURL=session-recovery-pins.js.map