export function getStatusPluginOwnershipSource(): string[] { return [ '', 'function currentAttention() {', ' let latestQuestion = null;', ' for (const attention of pendingAttentionByKey.values()) {', ' if (attention.hookEventName === "PermissionRequest") return attention;', ' latestQuestion = attention;', ' }', ' return latestQuestion;', '}', '', 'function attentionKey(factoryID, hookEventName, requestID, sourceSessionID) {', ' // Why: custom plugins may reuse request IDs across sessions or factories;', ' // JSON tuple identity prevents one owner from hiding another blocker.', ' return JSON.stringify([factoryID, hookEventName, requestID, sourceSessionID || ""]);', '}', '', 'function clearAttentionForSession(sessionID, factoryID) {', ' let rootSessionID = null;', ' for (const [key, attention] of pendingAttentionByKey) {', ' if (attention.sourceSessionID === sessionID && attention.factoryID === factoryID) {', ' pendingAttentionByKey.delete(key);', ' rootSessionID = attention.properties?.sessionID || sessionID;', ' }', ' }', ' return rootSessionID;', '}', '', 'function clearQuestionForToolPart(part, sessionID, factoryID) {', ' if (', ' part?.type !== "tool" ||', ' part.tool !== "question" ||', ' (part.state?.status !== "completed" && part.state?.status !== "error")', ' ) return null;', ' let rootSessionID = null;', ' for (const [key, attention] of pendingAttentionByKey) {', ' const tool = attention.properties?.tool;', ' if (', ' attention.hookEventName === "AskUserQuestion" &&', ' attention.sourceSessionID === sessionID &&', ' attention.factoryID === factoryID &&', ' tool?.messageID === part.messageID &&', ' tool?.callID === part.callID', ' ) {', ' pendingAttentionByKey.delete(key);', ' rootSessionID = attention.properties?.sessionID || sessionID;', ' }', ' }', ' return rootSessionID;', '}', '', 'function clearAttentionForResolution(event, sessionID, factoryID) {', ' const hookEventName =', ' event.type === "permission.replied" ? "PermissionRequest" : "AskUserQuestion";', ' const requestID = event.properties?.requestID || "";', ' const key = attentionKey(factoryID, hookEventName, requestID, sessionID);', ' const attention = pendingAttentionByKey.get(key);', ' if (!attention || attention.sourceSessionID !== sessionID) return null;', ' pendingAttentionByKey.delete(key);', ' return attention.properties?.sessionID || sessionID;', '}', '', 'function provisionalBusyKey(factoryID, sessionID) {', ' return JSON.stringify([factoryID, sessionID || ""]);', '}', '', 'function rememberProvisionalBusy(sessionID, factoryID) {', ' const key = provisionalBusyKey(factoryID, sessionID);', ' // Why: active ownership cannot be LRU-evicted without allowing false', ' // Idle; exact matching Idle or factory disposal lifecycle-bounds it.', ' provisionalBusyByKey.delete(key);', ' provisionalBusyByKey.set(key, { sessionID, factoryID });', '}', '', 'function clearProvisionalBusy(sessionID, factoryID) {', ' return provisionalBusyByKey.delete(provisionalBusyKey(factoryID, sessionID));', '}', '', 'function busyChildKey(factoryID, sessionID) {', ' return JSON.stringify([factoryID, sessionID || ""]);', '}', '', 'function rememberBusyChild(sessionID, rootSessionID, factoryID) {', ' const key = busyChildKey(factoryID, sessionID);', ' // Why: repeated Busy for the same child is not a state change; report one', ' // only when the pane actually gains a running descendant.', ' if (busyChildRootByKey.get(key)?.sessionID === rootSessionID) return false;', ' busyChildRootByKey.delete(key);', ' busyChildRootByKey.set(key, { sessionID: rootSessionID, factoryID });', ' return true;', '}', '', 'function clearBusyChild(sessionID, factoryID) {', ' return busyChildRootByKey.delete(busyChildKey(factoryID, sessionID));', '}', '', 'function clearKnownBusyRoot(sessionID, factoryID) {', ' if (busyRootOwnerBySessionID.get(sessionID) !== factoryID) return false;', ' busyRootOwnerBySessionID.delete(sessionID);', ' return true;', '}', '', 'function latestBusyOwner() {', ' let latest = null;', ' // Why: a running descendant is reported under its ROOT id, and ranks', ' // below a directly busy root so an ongoing turn keeps labelling the pane.', ' for (const busyChild of busyChildRootByKey.values()) {', ' latest = busyChild;', ' }', ' for (const [sessionID, factoryID] of busyRootOwnerBySessionID) {', ' latest = { sessionID, factoryID };', ' }', ' for (const provisional of provisionalBusyByKey.values()) {', ' latest = provisional;', ' }', ' return latest;', '}', '', 'async function publishAggregateStatus(fallbackFactoryID, preferredSessionID) {', ' const attention = currentAttention();', ' if (attention) {', ' await setAttention(', ' attention.hookEventName,', ' attention.properties,', ' attention.factoryID,', ' attention.sourceSessionID', ' );', ' return;', ' }', ' const busyOwner = latestBusyOwner();', ' if (busyOwner) {', ' await setStatus("busy", { sessionID: busyOwner.sessionID }, busyOwner.factoryID);', ' return;', ' }', ' await setStatus("idle", { sessionID: preferredSessionID }, fallbackFactoryID);', '}', '', 'async function publishOwnershipChange(fallbackFactoryID, preferredSessionID) {', ' stateArrivalRevision += 1;', ' // Why: exact blocker/Busy retirement is authoritative even if ancestry', ' // lookup failed; every older preview must settle before its replacement.', ' await flushPendingAssistantPart(true);', ' await publishAggregateStatus(fallbackFactoryID, preferredSessionID);', '}', '' ] }