@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
56 lines • 2.07 kB
JavaScript
/**
* The turn count the idle decision should see. A healthy observer's count is the truth. An
* unhealthy one cannot see turns at all, so its count is UNKNOWN and reported as 1 (busy) so a
* momentary blip never kills a host mid-turn - but only for `graceMs`; past that, unknown has
* been allowed to hold the host open for a whole idle window and it stops counting as busy.
*/
export function activeTurnsForIdleDecision(input) {
if (input.healthy)
return input.observedBusy;
if (input.unhealthySince === undefined)
return 1;
return input.now - input.unhealthySince < input.unknownGraceMs ? 1 : 0;
}
export function createObserverLink(options) {
let current;
let healthy = false;
let unhealthySince;
let retry;
// Arming is deliberately NOT guarded by socket identity. That guard belongs to `lost`, whose
// job is to collapse one socket's close AND error into a single transition; reusing it for a
// failed retry ended the chain, because by then the link owns no socket and the guard is
// always true. Only settling stops the retries now (#1979).
const armRetry = () => {
if (retry !== undefined || options.settled())
return;
retry = options.setTimer(() => {
retry = undefined;
void open().catch(armRetry);
}, options.retryDelayMs);
};
async function open() {
const socket = await options.open();
current = socket;
healthy = true;
unhealthySince = undefined;
socket.onLost(() => {
if (current !== socket || options.settled())
return;
healthy = false;
unhealthySince ??= options.now();
current = undefined;
armRetry();
});
}
return {
open,
healthy: () => healthy,
unhealthySince: () => unhealthySince,
stop: () => {
retry?.cancel();
retry = undefined;
current = undefined;
},
};
}
//# sourceMappingURL=observer-link.js.map