export function getStatusPluginDeliverySource(): string[] { return [ '', 'function enqueueLifecycle(task) {', ' // Why: OpenCode intentionally fire-and-forgets hook promises, so async', ' // lookups and posts need their own FIFO to preserve event order.', ' const run = lifecycleQueue.then(async () => {', ' try {', ' await task();', ' } catch {', ' // Hook delivery must never reject into OpenCode.', ' }', ' });', ' lifecycleQueue = run;', ' return run;', '}', '', 'function clearStatusRetry() {', ' if (statusRetryTimer) clearTimeout(statusRetryTimer);', ' statusRetryTimer = null;', '}', '', 'function scheduleStatusRetry(revision) {', ' if (', ' statusRetryTimer ||', ' revision !== statusRevision ||', ' !statusDeliveryDirty ||', ' !activeFactoryIDs.has(desiredFactoryID)', ' ) return;', ' const delay = Math.min(', ' STATUS_RETRY_BASE_MS * Math.pow(2, Math.min(statusRetryAttempt, 6)),', ' STATUS_RETRY_MAX_MS', ' );', ' statusRetryAttempt = Math.min(statusRetryAttempt + 1, 7);', ' statusRetryTimer = setTimeout(() => {', ' statusRetryTimer = null;', ' void enqueueLifecycle(async () => {', ' if (', ' revision !== statusRevision ||', ' !statusDeliveryDirty ||', ' !activeFactoryIDs.has(desiredFactoryID)', ' ) return;', ' await publishDesiredStatus(revision);', ' });', ' }, delay);', ' if (statusRetryTimer.unref) statusRetryTimer.unref();', '}', '', 'async function publishDesiredStatus(revision) {', ' if (revision !== statusRevision) return;', ' if (!activeFactoryIDs.has(desiredFactoryID)) return;', ' const endpointKey = hookEndpointKey();', ' if (', ' !statusDeliveryDirty &&', ' deliveredStatusKey === desiredStatusKey &&', ' deliveredEndpointKey === endpointKey &&', ' deliveredMessagePartFactoryID === null', ' ) return;', ' const delivered = await post(desiredHookEventName, desiredStatusProperties);', ' if (revision !== statusRevision) return;', ' if (!delivered) {', ' statusDeliveryDirty = true;', ' scheduleStatusRetry(revision);', ' return;', ' }', ' clearStatusRetry();', ' statusRetryAttempt = 0;', ' deliveredStatusKey = desiredStatusKey;', ' deliveredEndpointKey = endpointKey;', ' deliveredMessagePartFactoryID = null;', ' statusDeliveryDirty = false;', '}', '', 'async function setDeliveryTarget(', ' next,', ' nextKey,', ' hookEventName,', ' extraProperties,', ' factoryID', ') {', ' const endpointChanged = deliveredEndpointKey !== hookEndpointKey();', ' if (', ' nextKey === desiredStatusKey &&', ' nextKey === deliveredStatusKey &&', ' desiredFactoryID === factoryID &&', ' !statusDeliveryDirty &&', ' !endpointChanged &&', ' deliveredMessagePartFactoryID === null', ' ) return;', ' const targetChanged = nextKey !== desiredStatusKey || desiredFactoryID !== factoryID;', ' clearStatusRetry();', ' if (targetChanged) {', ' statusRetryAttempt = 0;', ' busyRecoveryUsed = false;', ' busyRecoveryEndpointKey = "";', ' }', ' desiredStatus = next;', ' desiredHookEventName = hookEventName;', ' desiredStatusKey = nextKey;', ' desiredStatusProperties = extraProperties || {};', ' desiredFactoryID = factoryID;', ' statusDeliveryDirty =', ' statusDeliveryDirty ||', ' deliveredMessagePartFactoryID !== null ||', ' deliveredStatusKey !== nextKey ||', ' endpointChanged;', ' const revision = ++statusRevision;', ' await publishDesiredStatus(revision);', '}', '', 'async function setStatus(next, extraProperties, factoryID) {', ' const nextKey = next + ":" + (extraProperties?.sessionID || "");', ' const hookEventName = next === "busy" ? "SessionBusy" : "SessionIdle";', ' await setDeliveryTarget(next, nextKey, hookEventName, extraProperties, factoryID);', '}', '', 'async function setAttention(hookEventName, properties, factoryID, sourceSessionID) {', ' const requestID = properties?.id || properties?.sessionID || "";', ' const requestKey = attentionKey(factoryID, hookEventName, requestID, sourceSessionID);', ' await flushPendingAssistantPart(true);', ' await setDeliveryTarget(', ' "waiting",', ' "waiting:" + requestKey,', ' hookEventName,', ' properties,', ' factoryID', ' );', '}', '', 'function recoverBusyFromDelta(client, sessionID, factoryID) {', ' if (busyRecoveryQueued) return lifecycleQueue;', ' if (', ' busyRecoveryUsed &&', ' busyRecoveryEndpointKey === hookEndpointKey()', ' ) return lifecycleQueue;', ' busyRecoveryQueued = true;', ' return enqueueLifecycle(async () => {', ' try {', ' if (!activeFactoryIDs.has(factoryID)) return;', ' if (sessionID && (await isChildSession(client, sessionID)) === true) return;', ' if (!activeFactoryIDs.has(factoryID)) return;', ' const endpointKey = hookEndpointKey();', ' const endpointChanged = deliveredEndpointKey !== endpointKey;', ' if (desiredStatus !== "busy" || (!statusDeliveryDirty && !endpointChanged)) return;', ' if (busyRecoveryUsed && !endpointChanged) return;', ' busyRecoveryUsed = true;', ' busyRecoveryEndpointKey = endpointKey;', ' clearStatusRetry();', ' statusDeliveryDirty = true;', ' const revision = ++statusRevision;', ' await publishDesiredStatus(revision);', ' } finally {', ' busyRecoveryQueued = false;', ' }', ' });', '}' ] } export function getTuiStatusDeliverySource(): string[] { return String.raw` // Why queued: levels derived before disposal still post, in order, before the identity retires. function releaseTuiStatusDelivery(factoryID, forgetUndelivered) { return enqueueLifecycle(async () => { disposingFactoryIDs.add(factoryID); while (messagePartPostInFlight) await messagePartPostInFlight; if (pendingAssistantPart?.factoryID === factoryID) { if (assistantPartFlushTimer) clearTimeout(assistantPartFlushTimer); assistantPartFlushTimer = null; pendingAssistantPart = null; } if (desiredFactoryID === factoryID) { if (statusDeliveryDirty) forgetUndelivered(); clearStatusRetry(); statusRevision += 1; } activeFactoryIDs.delete(factoryID); disposingFactoryIDs.delete(factoryID); }); } `.split('\n') }