export function getStatusPluginSessionLineageSource(): string[] { return [ '// Why: oh-my-opencode style tools spawn child sessions that emit their', '// own session.idle / message events. Those child completions must not', '// flip the root Orca pane to done or overwrite the parent turn preview.', '// Resolve the full parentID chain so descendant attention can be attributed', '// to the root while child completion and previews remain non-authoritative.', 'async function resolveRootSessionID(client, sessionID) {', ' if (!sessionID) return null;', ' if (rootSessionById.has(sessionID)) return rootSessionById.get(sessionID);', ' if (!client?.session?.get && !client?.session?.list) return null;', ' if (rootSessionLookupById.has(sessionID)) return rootSessionLookupById.get(sessionID);', ' const lookup = lookupRootSessionID(client, sessionID);', ' rootSessionLookupById.set(sessionID, lookup);', ' try {', ' return await lookup;', ' } finally {', ' if (rootSessionLookupById.get(sessionID) === lookup) {', ' rootSessionLookupById.delete(sessionID);', ' }', ' }', '}', '', 'async function isChildSession(client, sessionID) {', ' const rootSessionID = await resolveRootSessionID(client, sessionID);', ' return rootSessionID === null ? null : rootSessionID !== sessionID;', '}', '', 'function rememberSessionRoot(sessionID, rootSessionID) {', ' if (rootSessionById.size >= 128 && !rootSessionById.has(sessionID)) {', ' const first = rootSessionById.keys().next().value;', ' if (first !== undefined) rootSessionById.delete(first);', ' }', ' rootSessionById.set(sessionID, rootSessionID);', '}', '', 'async function lookupRootSessionID(client, sessionID) {', ' const controller = new AbortController();', ' let timeout;', ' const deadline = new Promise((_, reject) => {', ' timeout = setTimeout(() => {', ' controller.abort();', ' reject(new Error("session lookup timed out"));', ' }, SESSION_LOOKUP_TIMEOUT_MS);', ' if (timeout.unref) timeout.unref();', ' });', ' try {', ' const rootSessionID = await Promise.race([', ' walkSessionParents(client, sessionID, controller.signal),', ' deadline,', ' ]);', ' return rootSessionID;', ' } catch {', ' return null;', ' } finally {', ' clearTimeout(timeout);', ' }', '}', '', 'async function walkSessionParents(client, sessionID, signal) {', ' const lineage = [];', ' let currentSessionID = sessionID;', ' // Why: malformed or unexpectedly deep ancestry must not monopolize the', ' // lifecycle FIFO even when every individual SDK lookup succeeds.', ' while (lineage.length < MAX_SESSION_ANCESTRY_DEPTH) {', ' const cachedRoot = rootSessionById.get(currentSessionID);', ' if (cachedRoot) {', ' for (const id of lineage) rememberSessionRoot(id, cachedRoot);', ' return cachedRoot;', ' }', ' if (lineage.includes(currentSessionID)) return null;', ' lineage.push(currentSessionID);', ' const sessions = await lookupSessionList(client, currentSessionID, signal);', ' const list = Array.isArray(sessions?.data) ? sessions.data : [];', ' const session = list.find((entry) => entry?.id === currentSessionID);', ' if (!session) return null;', ' if (!session.parentID) {', ' for (const id of lineage) rememberSessionRoot(id, currentSessionID);', ' return currentSessionID;', ' }', ' currentSessionID = session.parentID;', ' }', ' return null;', '}', '', 'async function lookupSessionList(client, sessionID, signal) {', ' // Why: point lookup avoids the SDK list page dropping older children;', ' // current SDKs put AbortSignal in a second options argument, while legacy', ' // generated clients accept one request-options object.', ' if (client?.session?.get) {', ' const calls = client.session.get.length >= 2', ' ? [', ' [{ sessionID }, { signal }],', ' [{ path: { id: sessionID }, signal }],', ' ]', ' : [[{ path: { id: sessionID }, signal }]];', ' for (const args of calls) {', ' try {', ' const result = await client.session.get(...args);', ' if (result?.data?.id === sessionID) return { data: [result.data] };', ' } catch {', ' if (signal.aborted) throw new Error("session lookup aborted");', ' // Try the other supported SDK generation, then list fallback.', ' }', ' }', ' }', ' if (!client?.session?.list) return { data: [] };', ' if (client.session.list.length >= 2) {', ' return client.session.list({}, { signal });', ' }', ' return client.session.list({ signal });', '}', '' ] }