@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
76 lines • 2.62 kB
JavaScript
export function effectStatusRecord(subscription, sessionId, status, at) {
return {
type: "effect_status",
subscriptionId: subscription.id,
seq: subscription.seq,
sessionId,
effectId: status.effectId,
kind: status.kind,
state: status.state,
...(status.disposition === undefined ? {} : { disposition: status.disposition }),
...(status.outcome === undefined ? {} : { outcome: status.outcome }),
...(status.by === undefined ? {} : { by: status.by }),
at,
...(subscription.options?.admissionId !== undefined
? { admissionId: subscription.options.admissionId, generation: 0 }
: {}),
};
}
const sinks = new Set();
const pending = new WeakMap();
const dirty = new Map();
let scheduled = false;
/**
* A ledger persisted a transition: hold it until the causing record is published, then deliver. A session
* no installed sink owns (classic stdio, a worker isolate, a session already replaced) has no reader, so its
* transition is dropped rather than held for the life of the process.
*/
export function noteSessionEffect(session, status) {
let owner;
for (const sink of sinks) {
if (!sink.owns(session))
continue;
owner = sink;
break;
}
if (owner === undefined)
return;
const list = pending.get(session) ?? [];
list.push(status);
pending.set(session, list);
dirty.set(session, owner);
if (!scheduled) {
scheduled = true;
queueMicrotask(flushDirty);
}
}
/** Take a session's held transitions; the writer calls this right after publishing its causing record. */
export function drainSessionEffects(session) {
const list = pending.get(session) ?? [];
if (list.length > 0)
pending.set(session, []);
return list;
}
function flushDirty() {
scheduled = false;
for (const [session, sink] of [...dirty]) {
dirty.delete(session);
const drained = drainSessionEffects(session);
if (drained.length > 0 && sinks.has(sink))
sink.flush(session, drained);
}
}
/** Each host installs its own sink; the returned function removes it (and stops holding for its sessions). */
export function installSessionEffectSink(sink) {
sinks.add(sink);
return () => {
sinks.delete(sink);
for (const [session, owner] of [...dirty]) {
if (owner !== sink)
continue;
dirty.delete(session);
drainSessionEffects(session);
}
};
}
//# sourceMappingURL=session-effect-status.js.map