@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
113 lines • 4.35 kB
JavaScript
import { isStaleExtensionContextError } from "./stale-context.js";
import { formatGoalWaitLabel } from "./wait-progress.js";
/** Footer countdown refresh cadence while a Goal continuation is delayed. */
export const GOAL_WAIT_TICK_INTERVAL_MS = 1000;
export const GOAL_WAIT_STATUS_KEY = "goal-wait";
/**
* Drives a once-per-second footer countdown while a Goal continuation timer is
* armed. Mirrors the existing GoalElapsedTicker and MonitorStatusTicker: the
* interval is unref'd and unchanged visible labels are not rendered again.
*/
export class GoalWaitTicker {
constructor(options) {
this.dueAtMs = 0;
this.totalMs = 0;
this.channelCounts = {};
this.render = options.render;
this.now = options.now ?? Date.now;
}
get running() {
return this.intervalId !== undefined;
}
/** Render the current wait immediately and keep it live until stopped. */
sync(ctx, input) {
this.ctx = ctx;
this.kind = input.kind;
this.dueAtMs = this.now() + Math.max(0, input.remainingMs);
this.totalMs = input.totalMs;
this.channelCounts = { ...input.channelCounts };
this.lastRenderedStatus = undefined;
this.tick();
// The immediate render hit a retired ctx and retired the ticker: arm nothing for it.
if (this.ctx === undefined)
return;
if (this.intervalId !== undefined)
return;
const handle = setInterval(() => this.tick(), GOAL_WAIT_TICK_INTERVAL_MS);
handle.unref();
this.intervalId = handle;
}
/** Refresh channel wording without changing the countdown deadline. */
setChannelCounts(channelCounts) {
if (this.ctx === undefined || this.kind !== "monitor")
return;
this.channelCounts = { ...channelCounts };
this.tick();
}
/** Stop ticking, clear the footer segment, and drop the retained context. */
stop() {
if (this.intervalId !== undefined) {
clearInterval(this.intervalId);
this.intervalId = undefined;
}
const ctx = this.ctx;
this.ctx = undefined;
this.kind = undefined;
this.lastRenderedStatus = undefined;
if (ctx !== undefined) {
try {
this.render(ctx, undefined);
}
catch (error) {
// A stale ctx cannot clear its own footer status; dropping it is enough.
if (!isStaleExtensionContextError(error))
throw error;
}
}
}
/**
* Drop the interval and retained ctx without a final render. Used when a tick
* discovers the ctx was retired by a session replacement or reload: every
* render against it throws from now on, and a ticker that keeps ticking dead
* renders freezes the footer forever without crashing. The next sync() with a
* live ctx re-arms everything.
*/
retire() {
if (this.intervalId !== undefined) {
clearInterval(this.intervalId);
this.intervalId = undefined;
}
this.ctx = undefined;
this.kind = undefined;
this.lastRenderedStatus = undefined;
}
tick() {
if (this.ctx === undefined || this.kind === undefined)
return;
try {
// The countdown is a parked-state display: while a turn runs the goal is
// pursued, not waited on, so the segment clears. The armed timer (and its
// cache-TTL deadline) is untouched, so the next idle tick re-renders it.
const status = this.ctx.isIdle()
? formatGoalWaitLabel({
kind: this.kind,
remainingMs: Math.max(0, this.dueAtMs - this.now()),
totalMs: this.totalMs,
channelCounts: this.channelCounts,
})
: undefined;
if (status === this.lastRenderedStatus)
return;
this.lastRenderedStatus = status;
this.render(this.ctx, status);
}
catch (error) {
if (isStaleExtensionContextError(error)) {
this.retire();
return;
}
throw error;
}
}
}
//# sourceMappingURL=wait-ticker.js.map