@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
784 lines • 36 kB
JavaScript
/**
* `/loop` extension factory and lifecycle wiring.
*
* This module owns the only impure parts of the feature: real timers, the session store,
* the extension event wiring, and message dispatch. Every decision it makes is delegated
* to the pure modules beside it (`scheduler.ts`, `tick-prompt.ts`, `cron-planner.ts`,
* `loopfile.ts`, `status.ts`), so the runtime here is a thin, testable adapter.
*
* Load-bearing rules implemented here:
* - A tick STEERS. Idle dispatch goes through `sendUserMessage(..., {
* expandPromptTemplates: true })` so a slash payload reaches the real command path;
* a busy session receives the tick as a steer, delivered into the running turn at its
* next boundary, never parked as a follow-up behind it.
* - Shutdown SUSPENDS: timers are cancelled, the ticker disposed, and the snapshot is
* persisted without a terminal reason (every senpi shutdown reason is resumable).
* - Keepalive is applied only to an attributed dynamic iteration, never after an ordinary
* user turn, and never after a user abort (an abort PAUSES the loop instead).
* - A store failure is not swallowed: the affected loops end with `error` and the user is
* told, because a schedule that cannot be persisted must not keep running.
*/
import { homedir } from "node:os";
import { join } from "node:path";
import { getAgentDir } from "../../../../config.js";
import { noticeEntryRenderer } from "../../notice/index.js";
import { registerLoopCommand } from "./command-registration.js";
import { describeCron, normalizeInterval } from "./cron-planner.js";
import { nodeFs, resolveLoopFile as resolveLoopFileDefault } from "./loopfile.js";
import { createLoopScheduler, } from "./scheduler.js";
import { sessionHeld, userStopped } from "./session-hold.js";
import { formatLoopStatus, formatNoopFold, LoopStatusTicker } from "./status.js";
import { readLoopState, writeLoopState } from "./store.js";
import { buildTickMessage } from "./tick-prompt.js";
import { registerLoopTools, SCHEDULE_WAKEUP_TOOL } from "./tools.js";
/** Custom entry type carrying one dispatched tick, used for attribution and folding. */
export const LOOP_TICK_ENTRY_TYPE = "loop-tick";
export const renderLoopTickEntry = noticeEntryRenderer((entry) => {
const data = entry.data;
if (data === undefined)
return undefined;
const folded = data.folded ? formatNoopFold(data.noopStreak) : "";
if (folded.length > 0) {
return {
title: folded,
why: "Consecutive loop ticks reported no actionable change.",
};
}
return {
title: `↻ loop tick (${data.mode}) · ${data.delivery}`,
why: "A /loop schedule dispatched this tick.",
};
});
/**
* Real timer port: exactly one live timeout per key, replaced on re-arm.
*
* Follows the generation-counter discipline of `cache-keepalive/index.ts`: cancelling or
* re-arming a key bumps its generation, so a callback whose generation is stale returns
* without firing even if it was already scheduled on the event loop.
*/
export function createNodeTimerPort() {
const handles = new Map();
const generations = new Map();
function cancel(key) {
const handle = handles.get(key);
if (handle !== undefined)
clearTimeout(handle);
handles.delete(key);
generations.set(key, (generations.get(key) ?? 0) + 1);
}
return {
arm(key, dueAt, callback) {
cancel(key);
const generation = generations.get(key) ?? 0;
const delayMs = Math.max(0, dueAt - Date.now());
const handle = setTimeout(() => {
handles.delete(key);
if (generations.get(key) !== generation)
return;
callback();
}, delayMs);
handle.unref?.();
handles.set(key, handle);
},
cancel,
cancelAll() {
for (const key of [...handles.keys()])
cancel(key);
},
};
}
function defaultIds() {
let sequence = 0;
const mint = (prefix) => {
sequence += 1;
return `${prefix}-${Date.now().toString(36)}-${sequence.toString(36)}`;
};
return {
loopId: () => mint("loop"),
wakeupId: () => mint("wakeup"),
deliveryId: () => mint("delivery"),
};
}
function defaultStoreRef(ctx) {
const sessionFile = ctx.sessionManager.getSessionFile();
const baseDir = sessionFile === undefined
? join(getAgentDir(), "extensions", "loop", "no-session")
: join(ctx.sessionManager.getSessionDir(), "extensions", "loop");
return { baseDir, sessionId: ctx.sessionManager.getSessionId() };
}
function emptyDeliveryState() {
return { autonomousPreambleDelivered: false, lastLoopFileDelivered: null, forceFullDelivery: false };
}
function loopFileSnapshot(result) {
if (!result.found)
return { present: false };
return {
present: true,
path: result.path,
content: result.content,
mtimeMs: result.fingerprint.mtimeMs,
size: result.fingerprint.size,
contentHash: result.fingerprint.contentHash,
};
}
function sentinelFor(mode, loopFilePresent) {
if (loopFilePresent)
return mode === "fixed" ? "<<loop.md>>" : "<<loop.md-dynamic>>";
return mode === "fixed" ? "<<autonomous-loop>>" : "<<autonomous-loop-dynamic>>";
}
function modeOf(entry) {
return entry.kind === "fixed" ? "fixed" : "dynamic";
}
/** Reads the tick payload of a `loop-tick` entry, whichever entry kind carries it. */
function anchorPayloadOf(entry) {
if (entry.type === "custom") {
return entry.customType === LOOP_TICK_ENTRY_TYPE ? entry.data : undefined;
}
if (entry.type === "custom_message") {
return entry.customType === LOOP_TICK_ENTRY_TYPE ? entry.details : undefined;
}
return undefined;
}
/**
* True when the compaction-aware context still contains the delivery that anchored a full
* sentinel block. Compaction can drop it, and a reminder's "established earlier in this
* conversation" phrasing must never dangle, so a missing anchor forces the next tick back
* to full. When no specific anchor id was recorded (the autonomous preamble carries none),
* any surviving tick delivery for that loop counts as the anchor.
*/
function anchorPresent(ctx, loopId, deliveryId) {
return ctx.sessionManager.buildContextEntries().some((entry) => {
const payload = anchorPayloadOf(entry);
if (payload === undefined)
return false;
if (payload.loopId !== undefined && payload.loopId !== loopId)
return false;
return deliveryId === undefined ? true : payload.deliveryId === deliveryId;
});
}
export function createLoopExtension(deps = {}) {
const clock = deps.clock ?? { now: () => Date.now() };
const makeTimers = deps.timers ?? createNodeTimerPort;
const ids = deps.ids ?? defaultIds();
const storeRefOf = deps.storeRef ?? defaultStoreRef;
const resolveLoopFile = deps.resolveLoopFile ??
((options) => resolveLoopFileDefault({ cwd: options.cwd, homeDir: options.homeDir, fs: nodeFs, path: { join } }));
const readState = deps.readState ?? readLoopState;
const writeState = deps.writeState ?? writeLoopState;
return (pi) => {
let ctx;
let scheduler;
let storeRef;
let timers = makeTimers();
let storeFailure;
let sessionNamed = false;
/** Loop the currently running iteration is attributed to, for keepalive + tools. */
let attributedLoopId;
/** Delivery id of the tick that started the attributed iteration. */
let attributedDeliveryId;
/** True once the attributed iteration rescheduled or stopped itself. */
let attributionResolved = false;
/** True when the attributed tick was steered into a turn the loop did not start (keepalive is not charged). */
let attributedSteered = false;
/** Ticks held back because their slash payload cannot be queued while streaming. */
const deferredDispatches = [];
const deliveryStates = new Map();
/** Loops retired with `error` because their state could not be read or written. */
const endedWithError = new Set();
/** True once this session has state worth persisting (loaded or created). */
let storeTouched = false;
let persistTail = Promise.resolve();
const ticker = new LoopStatusTicker({
render: (key, text) => {
try {
ctx?.ui.setStatus(key, text);
}
catch {
// A stale context after session replacement must not break the loop runtime.
}
},
now: () => clock.now(),
});
pi.registerEntryRenderer(LOOP_TICK_ENTRY_TYPE, renderLoopTickEntry);
registerLoopTools(pi, {
scheduler: {
getWakeupTarget: () => {
if (scheduler === undefined || attributedLoopId === undefined)
return null;
const entry = scheduler.getState().entries[attributedLoopId];
if (entry === undefined || entry.phase === "ended")
return null;
return { kind: entry.kind, loopId: entry.id };
},
scheduleWakeup: async (request) => {
const active = requireScheduler();
const result = active.onScheduleWakeup(request);
if (!result.ok)
throw new Error(`schedule_wakeup rejected: ${result.reason}`);
attributionResolved = true;
await persist();
refreshStatus();
return {
wakeupId: result.wakeupId,
...(result.replacedWakeupId === undefined ? {} : { replacedWakeupId: result.replacedWakeupId }),
dueAt: result.dueAt,
noopStreak: result.noopStreak,
};
},
stopDynamicLoop: async (request) => {
const active = requireScheduler();
active.stop(request.loopId, "model-stop");
attributionResolved = true;
await persist();
refreshStatus();
return { endedAt: clock.now() };
},
},
});
function requireScheduler() {
if (scheduler === undefined)
throw new Error("loop extension has no active session");
return scheduler;
}
/** Merges extension-owned sentinel delivery state into the scheduler's snapshot. */
function withDeliveryOverlay(state) {
const entries = {};
for (const [id, entry] of Object.entries(state.entries)) {
const delivery = deliveryStates.get(id);
entries[id] = delivery === undefined ? entry : { ...entry, sentinelDelivery: delivery };
}
return { ...state, entries };
}
/** Serializes persistence so a timer, tool, and command write cannot interleave. */
function persist() {
const ref = storeRef;
const active = scheduler;
if (ref === undefined || active === undefined)
return Promise.resolve();
const snapshot = withDeliveryOverlay(active.getState());
// The extension is registered for every session, so a session that never ran a loop
// must not leave a sidecar behind.
if (!storeTouched && Object.keys(snapshot.entries).length === 0)
return Promise.resolve();
storeTouched = true;
persistTail = persistTail.then(async () => {
try {
await writeState(ref, snapshot);
}
catch (error) {
failStore(error, Object.keys(snapshot.entries));
}
});
return persistTail;
}
/**
* A schedule that cannot be read or written must not keep running: end the affected
* loops with `error` and tell the user, rather than silently losing the schedule. The
* terminal record is kept in memory because the store that would hold it is exactly the
* thing that just failed; what matters is that nothing stays armed and the user knows.
*/
function failStore(error, loopIds) {
const message = error instanceof Error ? error.message : String(error);
timers.cancelAll();
for (const loopId of loopIds)
endedWithError.add(loopId);
storeFailure = {
endReason: "error",
message,
loopIds,
};
ticker.dispose();
ctx?.ui.notify(`/loop state could not be used and the affected loops were ended: ${message}`, "error");
}
/**
* `schedule_wakeup` is search-exposed, so it is callable only while this extension keeps it
* active: exactly when a dynamic loop is live (not ended, not suspended). Idempotent, so
* every state transition can call it without churning the active tool list.
*/
function syncScheduleWakeupActivation() {
if (scheduler === undefined)
return;
const wanted = Object.values(scheduler.getState().entries).some((entry) => entry.kind === "dynamic" && entry.phase !== "ended" && entry.phase !== "suspended");
const active = pi.getActiveTools();
const present = active.includes(SCHEDULE_WAKEUP_TOOL);
if (wanted === present)
return;
pi.setActiveTools(wanted ? [...active, SCHEDULE_WAKEUP_TOOL] : active.filter((name) => name !== SCHEDULE_WAKEUP_TOOL));
}
function refreshStatus() {
syncScheduleWakeupActivation();
if (scheduler === undefined)
return;
const state = scheduler.getState();
// The ticker owns a repeating render timer, so it only runs while a loop is armed;
// a session that never used /loop must not carry one.
if (Object.values(state.entries).every((entry) => entry.phase === "ended")) {
if (ticker.running)
ticker.dispose();
return;
}
ticker.sync(state);
}
function deliveryStateOf(loopId) {
return deliveryStates.get(loopId) ?? emptyDeliveryState();
}
function markForceFullDelivery() {
for (const [id, state] of deliveryStates) {
deliveryStates.set(id, { ...state, forceFullDelivery: true });
}
const active = scheduler;
if (active === undefined)
return;
for (const entry of Object.values(active.getState().entries)) {
if (entry.phase === "ended")
continue;
if (deliveryStates.has(entry.id))
continue;
deliveryStates.set(entry.id, { ...entry.sentinelDelivery, forceFullDelivery: true });
}
}
function isExtensionCommandPayload(text) {
if (!text.startsWith("/"))
return false;
const name = text.slice(1).split(/\s+/)[0];
return pi.getCommands().some((command) => command.name === name);
}
/**
* Delivers one tick. Idle dispatch expands prompt templates so a slash payload
* reaches its real command handler; a busy session gets the tick steered into the
* running turn. A slash payload cannot be queued at all, so it waits for the turn to
* settle rather than being rewritten into plain text.
*/
async function dispatchTick(tick) {
const active = requireScheduler();
const entry = active.getState().entries[tick.loopId];
if (entry === undefined || entry.phase === "ended")
return;
if (endedWithError.has(tick.loopId))
return;
const currentCtx = ctx;
if (currentCtx === undefined)
return;
const mode = modeOf(entry);
const loopFile = entry.payload.type === "sentinel" ? await resolveLoopFileSafely(currentCtx) : { found: false };
const built = buildTickMessage({
loopId: entry.id,
deliveryId: tick.deliveryId,
mode,
payload: entry.payload,
reentryPrompt: entry.reentryPrompt,
deliveryState: deliveryStateOf(entry.id),
loopFile: loopFileSnapshot(loopFile),
});
deliveryStates.set(entry.id, built.deliveryState);
const busy = !currentCtx.isIdle() || currentCtx.hasPendingMessages();
const held = sessionHeld(currentCtx);
if (busy && !held && isExtensionCommandPayload(built.text)) {
// Extension commands cannot be queued; hold this delivery until the turn settles.
deferredDispatches.push(tick);
return;
}
const noopStreak = entry.noopStreak;
pi.appendEntry(LOOP_TICK_ENTRY_TYPE, {
loopId: entry.id,
deliveryId: tick.deliveryId,
scheduledForAt: tick.scheduledForAt,
mode,
delivery: built.delivery,
...(built.details.sentinel === undefined ? {} : { sentinel: built.details.sentinel }),
noopStreak,
folded: noopStreak >= 2,
});
if (!held) {
attributedLoopId = entry.id;
attributedDeliveryId = tick.deliveryId;
attributionResolved = false;
attributedSteered = busy;
}
// The tick prompt tells the model to call schedule_wakeup, so the tool must be active
// before the tick is queued, not after the turn has already read its tool list.
syncScheduleWakeupActivation();
pi.sendUserMessage(built.text, {
expandPromptTemplates: true,
...(busy ? { deliverAs: tick.deliverAs } : {}),
});
if (held) {
// The session keeps this tick as a notice (senpi#3811): no iteration ran, so neither
// keepalive nor the tick budget is charged, and a fixed schedule stays armed.
active.onTickSettled({ loopId: entry.id, deliveryId: tick.deliveryId, outcome: "completed" });
}
await persist();
refreshStatus();
}
async function resolveLoopFileSafely(currentCtx) {
try {
return await resolveLoopFile({ cwd: currentCtx.cwd, homeDir: homedir() });
}
catch {
// A loop file that cannot be read is treated as absent for this tick; the loop
// stays alive and picks the file up again once it is readable.
return { found: false };
}
}
async function drainDeferred() {
if (deferredDispatches.length === 0)
return;
const pending = deferredDispatches.splice(0, deferredDispatches.length);
for (const tick of pending)
await dispatchTick(tick);
}
async function fireDue(loopId) {
const active = scheduler;
const currentCtx = ctx;
if (active === undefined || currentCtx === undefined)
return;
const busy = !currentCtx.isIdle() || currentCtx.hasPendingMessages();
const decision = active.onDue(loopId, clock.now(), busy, sessionHeld(currentCtx));
await persist();
if (decision.action === "dispatch") {
await dispatchTick(decision.tick);
return;
}
if (decision.action === "expire") {
ctx?.ui.notify("Loop expired after 7 days and is no longer armed.", "info");
}
refreshStatus();
}
/**
* Timer callbacks are synchronous, so the async due step is scheduled and its failure is
* contained: a rejected dispatch must never take down the timer that owns the loop.
*/
function handleTimerFire(loopId) {
void fireDue(loopId).catch((error) => {
ctx?.ui.notify(`Loop tick failed: ${error instanceof Error ? error.message : String(error)}`, "error");
});
}
function payloadForPrompt(prompt) {
return { type: "prompt", prompt };
}
async function startFixed(request) {
const active = requireScheduler();
const effective = normalizeInterval(request.requestedInterval);
const created = active.createFixed({
originalArgs: request.originalArgs,
reentryPrompt: `/loop ${request.originalArgs}`.trimEnd(),
payload: payloadForPrompt(request.prompt),
requestedInterval: request.requestedInterval,
effectiveInterval: effective,
cronExpression: describeCron(effective),
intervalMs: effective.intervalMs,
});
if (!created.ok)
return { ok: false, message: created.message };
deliveryStates.set(created.loopId, emptyDeliveryState());
await persist();
nameSessionAfterLoop(request.prompt);
await fireDue(created.loopId);
refreshStatus();
return {
ok: true,
loopId: created.loopId,
...(effective.roundingNotice === undefined ? {} : { roundingNotice: effective.roundingNotice }),
cronExpression: describeCron(effective),
effectiveCadence: effective.human,
expiresAt: active.getState().entries[created.loopId]?.expiresAt,
};
}
async function startDynamic(request) {
const active = requireScheduler();
const created = active.createDynamic({
originalArgs: request.originalArgs,
reentryPrompt: `/loop ${request.originalArgs}`.trimEnd(),
payload: payloadForPrompt(request.prompt),
});
if (!created.ok)
return { ok: false, message: created.message };
deliveryStates.set(created.loopId, emptyDeliveryState());
await persist();
nameSessionAfterLoop(request.prompt);
await fireDue(created.loopId);
refreshStatus();
return {
ok: true,
loopId: created.loopId,
...(created.supersededLoopId === undefined ? {} : { supersededLoopId: created.supersededLoopId }),
expiresAt: active.getState().entries[created.loopId]?.expiresAt,
};
}
async function startBare(request) {
const active = requireScheduler();
const currentCtx = ctx;
if (currentCtx === undefined)
return { ok: false, message: "loop extension has no active session" };
const loopFile = await resolveLoopFileSafely(currentCtx);
const mode = request.interval === undefined ? "dynamic" : "fixed";
const payload = { type: "sentinel", sentinel: sentinelFor(mode, loopFile.found) };
const reentryPrompt = request.originalArgs.trim() === "" ? "/loop" : `/loop ${request.originalArgs.trim()}`;
const created = request.interval === undefined
? active.createDynamic({ originalArgs: request.originalArgs, reentryPrompt, payload })
: (() => {
const effective = normalizeInterval(request.interval);
return active.createFixed({
originalArgs: request.originalArgs,
reentryPrompt,
payload,
requestedInterval: request.interval,
effectiveInterval: effective,
cronExpression: describeCron(effective),
intervalMs: effective.intervalMs,
});
})();
if (!created.ok)
return { ok: false, message: created.message };
deliveryStates.set(created.loopId, emptyDeliveryState());
await persist();
nameSessionAfterLoop(reentryPrompt);
await fireDue(created.loopId);
refreshStatus();
return {
ok: true,
loopId: created.loopId,
expiresAt: active.getState().entries[created.loopId]?.expiresAt,
};
}
function nameSessionAfterLoop(prompt) {
if (sessionNamed)
return;
sessionNamed = true;
if (pi.getSessionName() !== undefined)
return;
pi.setSessionName(`loop: ${prompt}`.trim());
}
const controller = {
startFixed,
startDynamic,
startBare,
fireDue,
scheduleWakeup: async (request) => {
const active = requireScheduler();
const result = active.onScheduleWakeup(request);
if (result.ok)
attributionResolved = true;
await persist();
refreshStatus();
},
stop: async (target, detail) => {
const active = requireScheduler();
const result = active.stop(target, detail);
await persist();
refreshStatus();
return result.affectedLoopIds;
},
pause: async (target) => {
const active = requireScheduler();
const result = active.pause(target);
await persist();
refreshStatus();
return result.affectedLoopIds;
},
resume: async (target) => {
const active = requireScheduler();
const result = active.resume(target);
await persist();
refreshStatus();
return result.affectedLoopIds;
},
getState: () => requireScheduler().getState(),
statusLine: () => formatLoopStatus(requireScheduler().getState(), clock.now()),
lastStoreFailure: () => storeFailure,
isEndedWithError: (loopId) => endedWithError.has(loopId),
};
deps.onControllerReady?.(controller);
registerLoopCommand(pi, { controller });
pi.on("session_start", async (_event, nextCtx) => {
ctx = nextCtx;
timers.cancelAll();
timers = makeTimers();
storeFailure = undefined;
endedWithError.clear();
storeTouched = false;
sessionNamed = false;
attributedLoopId = undefined;
attributedDeliveryId = undefined;
attributionResolved = false;
deferredDispatches.length = 0;
deliveryStates.clear();
storeRef = storeRefOf(nextCtx);
let initialState;
try {
initialState = (await readState(storeRef)) ?? undefined;
}
catch (error) {
// Fail closed: arm nothing, end nothing silently, and tell the user.
failStore(error, []);
scheduler = createLoopScheduler({
sessionId: nextCtx.sessionManager.getSessionId(),
clock,
timers,
ids,
onFire: handleTimerFire,
});
return;
}
scheduler = createLoopScheduler({
sessionId: nextCtx.sessionManager.getSessionId(),
clock,
timers,
ids,
onFire: handleTimerFire,
...(initialState === undefined ? {} : { initialState }),
});
storeTouched = initialState !== undefined;
for (const entry of Object.values(scheduler.getState().entries)) {
const anchorDeliveryId = entry.sentinelDelivery.lastLoopFileDelivered?.anchorDeliveryId;
const anchored = anchorPresent(nextCtx, entry.id, anchorDeliveryId);
const needsFull = !anchored && hasAnchoredDelivery(entry.sentinelDelivery);
deliveryStates.set(entry.id, {
...entry.sentinelDelivery,
forceFullDelivery: entry.sentinelDelivery.forceFullDelivery || needsFull,
});
}
const restored = scheduler.restore(clock.now());
await persist();
refreshStatus();
for (const tick of restored.recoveryTicks)
await dispatchTick(tick);
if (restored.expiredLoopIds.length > 0) {
nextCtx.ui.notify(`Loop expired after 7 days: ${restored.expiredLoopIds.join(", ")}`, "info");
}
});
pi.on("session_compact", async (event) => {
// Accepted compaction may have dropped the anchor the reminders point at, so the
// next sentinel tick must carry the full instructions again. Timers and phase are
// deliberately untouched.
if (event.accepted !== true)
return;
markForceFullDelivery();
await persist();
});
pi.on("input", async (event) => {
// A user turn is never an attributed loop iteration, so keepalive must not see it.
// A tick dispatched by this extension arrives with source "extension" and MUST keep
// its attribution, or no dynamic iteration would ever be judged.
if (event.source === "extension")
return;
attributedLoopId = undefined;
attributedDeliveryId = undefined;
attributionResolved = false;
});
pi.on("agent_end", async (event) => {
// A retrying turn is still in progress: neither settle it nor judge its liveness.
if (event.willRetry === true)
return;
if (event.aborted === true && event.abortSource === "user") {
await stopOrPauseAttributedLoop();
return;
}
await settleAttributedTick(event.aborted === true ? "error" : "completed");
});
pi.on("agent_settled", async () => {
await settleAttributedTick("completed");
await drainDeferred();
refreshStatus();
});
pi.on("session_abort", async () => {
await stopOrPauseAttributedLoop();
});
// Under a user Stop the session holds every later tick, so the loop is not paused itself.
async function stopOrPauseAttributedLoop() {
if (ctx !== undefined && userStopped(ctx))
await settleAttributedTick("error", true);
else
await pauseAttributedLoop();
}
pi.on("session_shutdown", async (event) => {
const active = scheduler;
if (active !== undefined) {
// Shutdown SUSPENDS: every senpi shutdown reason leaves the session resumable, so the snapshot
// carries no terminal reason. A graceful close ends them "stopped" instead (oh-my-openagent#14313).
if (event.closed === true)
active.stop("all", "the session was closed on request");
active.onShutdown();
await persist();
}
timers.cancelAll();
deferredDispatches.length = 0;
ticker.dispose();
attributedLoopId = undefined;
attributedDeliveryId = undefined;
attributionResolved = false;
ctx = undefined;
});
async function pauseAttributedLoop() {
const active = scheduler;
if (active === undefined)
return;
const targets = attributedLoopId === undefined
? Object.values(active.getState().entries)
.filter((entry) => entry.phase !== "ended")
.map((entry) => entry.id)
: [attributedLoopId];
const paused = [];
for (const loopId of targets) {
if (active.onUserAbort(loopId).action === "paused")
paused.push(loopId);
}
attributedLoopId = undefined;
attributedDeliveryId = undefined;
attributionResolved = false;
if (paused.length === 0)
return;
await persist();
refreshStatus();
ctx?.ui.notify("loop paused - /loop resume or /loop stop", "info");
}
/**
* Settles the tick this turn was attributed to, drains a coalesced occurrence, and
* applies the two-strike keepalive to a dynamic iteration that ended without calling
* `schedule_wakeup`. An ordinary user turn clears the attribution first, so keepalive
* can never be charged to it, and a user abort takes the pause path instead.
*/
async function settleAttributedTick(outcome, userStopped = false) {
const active = scheduler;
const loopId = attributedLoopId;
const deliveryId = attributedDeliveryId;
if (active === undefined || loopId === undefined)
return;
const entry = active.getState().entries[loopId];
if (entry === undefined)
return;
const resolved = attributionResolved;
const steered = attributedSteered;
attributedLoopId = undefined;
attributedDeliveryId = undefined;
attributionResolved = false;
const settled = deliveryId === undefined ? undefined : active.onTickSettled({ loopId, deliveryId, outcome });
await persist();
if (!resolved && entry.kind === "dynamic" && !userStopped) {
const keepalive = active.onTurnEndedWithoutSchedule(loopId, { chargeCredit: !steered });
await persist();
refreshStatus();
if (keepalive.action === "ended") {
ctx?.ui.notify(keepalive.endReason === "keepalive_exhausted"
? "Loop ended: the model stopped scheduling wakeups."
: "Loop expired after 7 days and is no longer armed.", "info");
}
return;
}
if (settled?.action === "dispatch") {
// Exactly one delivery drains a coalesced occurrence, however many were missed.
await dispatchTick(settled.tick);
}
else if (settled?.action === "ended") {
ctx?.ui.notify(`Loop ended: ${settled.endReason}.`, "info");
}
refreshStatus();
}
};
}
/** True when a reminder would refer back to a full delivery that must still exist. */
function hasAnchoredDelivery(state) {
return state.autonomousPreambleDelivered || state.lastLoopFileDelivered !== null;
}
export default createLoopExtension();
//# sourceMappingURL=index.js.map