UNPKG

@sema-agent/core

Version:

Stateless, task-oriented AI agent core

1,813 lines • 81.9 kB
import { randomBytes } from "node:crypto";
import { isCheckpointRowUnreadable } from "./checkpoint-queries.js";
import { describeRejectedValue } from "./tool-search-setting.js";
import { receiveRetentionPolicy } from "./retention-policy.js";
import { commitCompletionIdIfEmpty, completionIdCandidate, mintCompletionId } from "./completion-id.js";
import { agentOutcomeOf, assertDelegationRoot, BACKGROUND_AGENT_REAP_KNOBS, canAccessAgentRecord, delegationRootOf, BackgroundAgentStoreError, clearRevivedRowTerminalPayload, isBackgroundAgentLiveStatus, isBackgroundAgentSettledStatus, nextCycleSeq, REVIVED_ROW_CLEARED_FIELDS, STALE_RUNNING_REAP_ATTRIBUTION, parkedCardState, } from "./background-agent-store.js";
import { shutdownDebug } from "./shutdown-debug.js";
import { adoptHostPromise } from "./host-promise.js";
import { delimitUntrusted } from "./untrusted-text.js";
import { boundedRedactedSummary } from "./untrusted-egress.js";
import { isLoopTask, clipTaskOutput, assertOwnership, sleepPollStep, alreadyTerminalStopNote, canAccess, isLiveSessionResidentAgent, normalizeAgentName, engineAgentName, chosenAgentName, DURABLE_AGENT_HEARTBEAT_MS, DURABLE_AGENT_HANDLE_RE, BG_AGENT_REAP_STOP_ERROR, activityStamp, isActivityInstant, monotonicNow, stampActivity, occupantStamp, } from "./task-registry-shared.js";
import { codedRefusal, thrownLabel } from "./thrown-value.js";
import { readExtensionRequester } from "./extension-requester.js";
import { closestName } from "./closest-name.js";
import { agentParkSentence, agentStateSourceOfHandle, agentStateSummary, approvalHeldOfHandle, readAgentState } from "./agent-state.js";
import { deliverEngineNotice } from "./engine-notice.js";
import { backgroundAgentOutcomeOf, memberRefusalLines } from "./member-outcome.js";
import { rootHeadReadOf } from "./root-head-read.js";
import { isOccupantToken, sameOccupant } from "../engine/session/repo-utils.js";
function sayDurableRowUnpersisted(kind, handle, line) {
    deliverEngineNotice(undefined, { code: "delegation.durable_row_unpersisted", message: line, detail: { kind, handle } });
}
export function activeDelegationHandlesLane(core, scope, rootSessionId) {
    return activeDelegationOccupantsLane(core, scope, rootSessionId).map((o) => o.id);
}
export function activeDelegationOccupantsLane(core, scope, rootSessionId, caller) {
    const out = [];
    for (const bh of activeDelegationTreeLane(core, rootSessionId)) {
        if (bh.scope !== scope)
            continue;
        const requestedBy = readExtensionRequester(bh.requestedBy);
        out.push({ id: bh.id, name: bh.name, ...(requestedBy !== undefined ? { requestedBy } : {}), reachable: caller !== undefined && canAccess(bh, { ...caller, scope }) });
    }
    return out;
}
export function activeDelegationHandlesByRootLane(core, rootSessionId) {
    assertDelegationRoot(rootSessionId);
    const out = [];
    for (const bh of activeDelegationTreeLane(core, rootSessionId)) {
        if (bh.scope !== undefined)
            out.push({ handle: bh.id, scope: bh.scope });
    }
    return out;
}
function activeDelegationTreeLane(core, rootSessionId) {
    const out = [];
    for (const h of core.handles.values()) {
        if (h.type !== "background_agent")
            continue;
        if (h.status !== "running" && h.status !== "pending")
            continue;
        const bh = h;
        if (delegationRootOf(bh) === rootSessionId)
            out.push(bh);
    }
    return out;
}
export function ensureDurableHeartbeatLane(core) {
    if (core.durableHeartbeatTimer !== undefined)
        return;
    const t = setInterval(() => {
        let liveLanes = 0;
        for (const h of core.handles.values()) {
            if (h.type !== "background_agent")
                continue;
            const bh = h;
            if (!bh.durable || bh.durable.poisoned)
                continue;
            if (bh.status === "running") {
                liveLanes++;
                durableAgentWriteLane(bh, {});
            }
            else if (bh.durable.flushFailed) {
                if ((bh.durable.redrives = (bh.durable.redrives ?? 0) + 1) > 60) {
                    bh.durable.poisoned = true;
                    sayDurableRowUnpersisted("terminal_unrecoverable", bh.durable.record.handle, `sema durable-agents: terminal flush for ${bh.durable.record.handle} unrecoverable after 60 heartbeat re-drives — durable writes for this row stop here`);
                    continue;
                }
                liveLanes++;
                durableAgentWriteLane(bh, {});
            }
        }
        if (liveLanes === 0) {
            clearInterval(t);
            core.durableHeartbeatTimer = undefined;
        }
    }, DURABLE_AGENT_HEARTBEAT_MS);
    t.unref?.();
    core.durableHeartbeatTimer = t;
}
export function durableAgentWriteLane(handle, patch, clear) {
    const lane = handle.durable;
    if (!lane || lane.poisoned)
        return;
    lane.chain = lane.chain.then(async () => {
        if (lane.poisoned)
            return;
        Object.assign(lane.record, patch);
        if (clear !== undefined)
            for (const k of clear)
                delete lane.record[k];
        lane.record.updatedAt = Date.now();
        const poison = (why) => {
            lane.poisoned = true;
            sayDurableRowUnpersisted("poisoned", lane.record.handle, `sema durable-agents: ${why} for ${lane.record.handle} — durable writes for this row stop here`);
        };
        const adoptOwnRow = async () => {
            try {
                const live = await lane.store.get(lane.record.handle, lane.record.scope);
                if (!live)
                    return "gone";
                if (live.writerId !== lane.record.writerId)
                    return "foreign";
                if (live.writerEpoch !== lane.record.writerEpoch)
                    return "foreign";
                lane.record.rev = live.rev;
                lane.written = true;
                return "adopted";
            }
            catch {
                return "unreadable";
            }
        };
        let landed = false;
        for (let attempt = 0; attempt < 3; attempt++) {
            if (lane.poisoned)
                return;
            if (attempt > 0) {
                await new Promise((r) => {
                    const t = setTimeout(r, 50 * attempt);
                    t.unref?.();
                });
            }
            try {
                if (!lane.written) {
                    await lane.store.put(structuredClone(lane.record));
                    lane.written = true;
                    lane.flushFailed = false;
                    lane.redrives = 0;
                    landed = true;
                    break;
                }
                const ok = await lane.store.update(lane.record.handle, lane.record.scope, structuredClone(lane.record), {
                    rev: lane.record.rev,
                });
                if (ok) {
                    lane.record.rev += 1;
                    lane.flushFailed = false;
                    lane.redrives = 0;
                    landed = true;
                    break;
                }
                const verdict = await adoptOwnRow();
                if (lane.poisoned)
                    return;
                if (verdict === "adopted")
                    continue;
                if (verdict === "unreadable")
                    continue;
                poison(verdict === "gone" ? "row reaped mid-write" : "CAS lost to a foreign writer");
                return;
            }
            catch (e) {
                if (e instanceof BackgroundAgentStoreError && e.code === "agent_record.already_exists") {
                    const verdict = await adoptOwnRow();
                    if (lane.poisoned)
                        return;
                    if (verdict === "adopted")
                        continue;
                    if (verdict === "unreadable")
                        continue;
                    if (verdict === "gone")
                        continue;
                    poison("handle collision (foreign durable row left untouched)");
                    return;
                }
                shutdownDebug(`durable-agents write failed for ${lane.record.handle} (attempt ${attempt + 1}/3)`, { error: thrownLabel(e) });
            }
        }
        if (!landed) {
            lane.flushFailed = true;
            return;
        }
        removeIfRootGone(handle, lane, lane.record.rev);
    });
}
function removeIfRootGone(handle, lane, rev) {
    void (async () => {
        if (!(await rootOccupantGone(handle, lane)))
            return;
        if (lane.poisoned || handle.durable !== lane)
            return;
        let removed;
        try {
            removed = await lane.store.delete(lane.record.handle, lane.record.scope, { rev });
        }
        catch (e) {
            shutdownDebug(`durable-agents: removing ${lane.record.handle}, whose root session was deleted, failed — the next write is read again`, { error: thrownLabel(e) });
            return;
        }
        if (!removed)
            return;
        const told = lane.poisoned;
        lane.poisoned = true;
        try {
            await lane.store.settleErasure?.();
        }
        catch (e) {
            shutdownDebug(`durable-agents: the erasure batch after removing ${lane.record.handle} did not settle — the store's next batch settles it`, { error: thrownLabel(e) });
        }
        if (!told)
            sayDurableRowUnpersisted("poisoned", lane.record.handle, `sema durable-agents: the root session ${lane.record.rootSessionId ?? ""} its tree was started in was deleted (no row, or another occupant) — the row was removed for ${lane.record.handle} — durable writes for this row stop here`);
    })().catch(() => undefined);
}
const ROOT_HEAD_READS = new WeakMap();
async function rootOccupantGone(handle, lane) {
    let pending = ROOT_HEAD_READS.get(lane);
    if (pending === undefined) {
        const seat = handle.delegationTree;
        pending = seat === undefined ? Promise.resolve(undefined) : seat.read().then((reading) => (reading.bound ? rootHeadReadOf(reading.root, lane.record) : undefined), () => undefined);
        ROOT_HEAD_READS.set(lane, pending);
    }
    const read = await pending;
    const rootId = lane.record.rootSessionId;
    if (read === undefined || rootId === undefined)
        return false;
    let head;
    try {
        head = await read(rootId);
    }
    catch {
        return false;
    }
    if (head === undefined)
        return true;
    return isOccupantToken(head.incarnation) && !sameOccupant(head.incarnation, lane.record.rootSessionIncarnation);
}
export function durableAgentArmedLane(core, id) {
    const h = core.handles.get(id);
    if (!h || h.type !== "background_agent")
        return false;
    const lane = h.durable;
    return lane !== undefined && !lane.poisoned;
}
export function durableAgentRowProbeLane(core, id) {
    const h = core.handles.get(id);
    if (!h || h.type !== "background_agent")
        return undefined;
    const handle = h;
    if (!handle.durable)
        return undefined;
    return async () => {
        const lane = handle.durable;
        if (lane === undefined)
            return false;
        await lane.chain.catch(() => undefined);
        return lane.written && !lane.poisoned && !lane.flushFailed;
    };
}
export function durableAgentRowFactsLane(core, id) {
    const h = core.handles.get(id);
    if (!h || h.type !== "background_agent")
        return undefined;
    const record = h.durable?.record;
    if (record === undefined)
        return undefined;
    return { ...(record.agentType !== undefined ? { agentType: record.agentType } : {}), ...(record.sessionId !== undefined ? { sessionId: record.sessionId } : {}), ...(record.policyFloor !== undefined ? { policyFloor: record.policyFloor } : {}) };
}
export function beginDurableClaimLane(core, id) {
    const live = core.handles.get(id);
    if (live !== undefined && (live.status === "running" || live.status === "pending"))
        return false;
    if (core.reapingHandles.has(id) || core.claimingHandles.has(id))
        return false;
    core.claimingHandles.add(id);
    return true;
}
export function endDurableClaimLane(core, id) {
    core.claimingHandles.delete(id);
}
export async function reapDurableAgentsLane(core, scope, deps, policy) {
    const knobs = receiveRetentionPolicy("reapDurableAgents", policy, BACKGROUND_AGENT_REAP_KNOBS, ["now"]);
    const now = knobs.now ?? Date.now();
    if (knobs.staleRunningMaxAgeMs !== undefined) {
        await deps.store.reap(scope, now, { staleRunningMaxAgeMs: knobs.staleRunningMaxAgeMs });
    }
    if (knobs.maxAgeMs === undefined && knobs.keep === undefined)
        return { rowsReaped: 0, sessionsReleased: 0, skippedNoSessions: 0, orphanPlacedReleased: 0 };
    const terminal = (await deps.store.listByScope(scope)).filter((r) => !("rowVerdict" in r) && isBackgroundAgentSettledStatus(r.status));
    terminal.sort((a, b) => b.spawnedAt - a.spawnedAt);
    const doomed = new Map();
    if (knobs.maxAgeMs !== undefined) {
        for (const r of terminal) {
            if ((r.settledAt ?? r.spawnedAt) < now - knobs.maxAgeMs)
                doomed.set(r.handle, r);
        }
    }
    if (knobs.keep !== undefined) {
        for (const r of terminal.slice(Math.max(0, knobs.keep)))
            doomed.set(r.handle, r);
    }
    let rowsReaped = 0;
    let sessionsReleased = 0;
    let skippedNoSessions = 0;
    for (const r of doomed.values()) {
        if (core.claimingHandles.has(r.handle))
            continue;
        core.reapingHandles.add(r.handle);
        try {
            let live;
            try {
                live = await deps.store.get(r.handle, scope);
            }
            catch {
                continue;
            }
            if (!live || !isBackgroundAgentSettledStatus(live.status))
                continue;
            const inProc = core.handles.get(r.handle);
            if (inProc !== undefined && (inProc.status === "running" || inProc.status === "pending" || inProc.status === "parked"))
                continue;
            if (live.sessionId !== undefined && deps.sessions === undefined) {
                skippedNoSessions++;
                continue;
            }
            try {
                if (!(await deps.store.delete(r.handle, scope, { rev: live.rev })))
                    continue;
            }
            catch {
                continue;
            }
            rowsReaped++;
            {
                const staleInProc = core.handles.get(r.handle);
                if (staleInProc !== undefined && staleInProc.status !== "running" && staleInProc.status !== "pending" && staleInProc.status !== "parked") {
                    core.handles.delete(r.handle);
                }
            }
            core.reapedHandles.add(r.handle);
            if (deps.mailbox !== undefined) {
                try {
                    await deps.mailbox.drop(scope, r.handle);
                }
                catch {
                }
            }
            if (live.sessionId !== undefined && deps.sessions !== undefined) {
                try {
                    await deps.sessions.unpin?.(live.sessionId);
                    await deps.sessions.release(live.sessionId);
                    sessionsReleased++;
                }
                catch {
                }
            }
        }
        finally {
            core.reapingHandles.delete(r.handle);
            core.reapedHandles.delete(r.handle);
        }
    }
    let orphanPlacedReleased = 0;
    if (knobs.maxAgeMs !== undefined && deps.sessions !== undefined && typeof deps.sessions.listPlaced === "function") {
        let placed = [];
        try {
            placed = await deps.sessions.listPlaced("subagent", { olderThanMs: knobs.maxAgeMs, scope });
        }
        catch {
            placed = [];
        }
        for (const p of placed) {
            if ("tupleIncomplete" in p)
                continue;
            if (p.scope !== scope)
                continue;
            if (core.claimingHandles.has(p.handle) || core.reapingHandles.has(p.handle))
                continue;
            let row;
            try {
                row = await deps.store.get(p.handle, scope);
            }
            catch {
                continue;
            }
            if (row !== null)
                continue;
            const inProc = core.handles.get(p.handle);
            if (inProc !== undefined && (inProc.status === "running" || inProc.status === "pending" || inProc.status === "parked"))
                continue;
            try {
                await deps.sessions.unpin?.(p.sessionId);
                await deps.sessions.release(p.sessionId);
                orphanPlacedReleased++;
                releaseDurableTranscriptAnchorLane(core, p.handle, p.sessionId);
            }
            catch {
            }
            if (deps.mailbox !== undefined) {
                try {
                    await deps.mailbox.drop(scope, p.handle);
                }
                catch {
                }
            }
        }
    }
    await deps.store.settleErasure?.();
    return { rowsReaped, sessionsReleased, skippedNoSessions, orphanPlacedReleased };
}
export function childSessionAnchorOf(handle) {
    if (handle.type !== "background_agent" || handle.sessionId === undefined)
        return undefined;
    return { sessionId: handle.sessionId, store: handle.sessionStore };
}
export function releaseDurableTranscriptAnchorLane(core, id, sessionId) {
    const handle = core.handles.get(id);
    if (!handle || handle.type !== "background_agent")
        return;
    if (childSessionAnchorOf(handle)?.sessionId !== sessionId)
        return;
    delete handle.sessionId;
    delete handle.sessionStore;
    durableAgentWriteLane(handle, {}, ["sessionId"]);
}
function bindRefusal(problem) {
    return codedRefusal("config.invalid_argument", `TaskRegistry: ${problem}`);
}
const INVISIBLE_CHARACTER = /[\p{Z}\p{Cc}\p{Cf}]/u;
function notAnAnchorWord(sessionId) {
    if (typeof sessionId !== "string")
        return `a ${sessionId === null ? "null" : typeof sessionId}`;
    if (sessionId === "")
        return "an empty string";
    if (INVISIBLE_CHARACTER.test(sessionId))
        return "a string with an invisible character";
    return undefined;
}
export function bindBackgroundAgentSessionLane(core, id, sessionId, store) {
    const notAnAnchor = notAnAnchorWord(sessionId);
    if (notAnAnchor !== undefined)
        throw bindRefusal(`a background-agent session anchor must be a non-empty session id with no invisible character — no separator, control or format character (got ${notAnAnchor})`);
    const handle = core.handles.get(id);
    if (!handle || handle.type !== "background_agent")
        throw bindRefusal(`no background-agent row ${typeof id === "string" ? JSON.stringify(id.length > 80 ? `${id.slice(0, 80)}…` : id) : `(a ${id === null ? "null" : typeof id})`} to anchor a session to`);
    const anchored = childSessionAnchorOf(handle)?.sessionId;
    if (anchored === sessionId)
        return;
    if (anchored !== undefined)
        throw bindRefusal(`background-agent row ${id} is anchored to a session already — release that anchor before binding another (the row keeps its anchor)`);
    handle.sessionId = sessionId;
    handle.sessionStore = store;
    durableAgentWriteLane(handle, { sessionId });
}
export function recordBackgroundAgentOrgAdmissionLane(core, id, verdict) {
    const handle = core.handles.get(id);
    if (!handle || handle.type !== "background_agent")
        return;
    const lane = handle.durable;
    if (lane === undefined)
        return;
    const disclose = (why) => {
        sayDurableRowUnpersisted("admission", lane.record.handle, `sema durable-agents: org-admission record for ${lane.record.handle} was not persisted (${why}) — the row keeps the previously recorded verdict, which a later revival will seed from`);
    };
    if (lane.poisoned) {
        disclose("durable lane poisoned");
        return;
    }
    durableAgentWriteLane(handle, { admittedOrgScopes: [...verdict.scopes], admittedOrgWriteScope: verdict.writeScope });
    void lane.chain.then(() => {
        if (lane.poisoned)
            disclose("durable lane poisoned");
        else if (lane.flushFailed)
            disclose("durable write failed");
    });
}
export function registerBackgroundAgentLane(core, input) {
    assertOwnership(input, "registerBackgroundAgent");
    if (input.id !== undefined && !DURABLE_AGENT_HANDLE_RE.test(input.id)) {
        throw new Error(`TaskRegistry: background-agent id ${JSON.stringify(input.id)} does not match the a<hex> handle form`);
    }
    const id = input.id ?? core.mintTaskId("background_agent");
    let carriedCompletionSink;
    if (input.durableSeed !== undefined) {
        const prior = core.handles.get(id);
        if (prior !== undefined) {
            const parkedHandoff = prior.status === "parked" && input.initialStatus === "parked";
            if (prior.type !== "background_agent" || prior.status === "running" || prior.status === "pending" || (prior.status === "parked" && !parkedHandoff)) {
                throw new Error(`TaskRegistry: task id already registered: ${id}`);
            }
            const priorLane = prior.durable;
            if (priorLane !== undefined)
                priorLane.poisoned = true;
            if (parkedHandoff)
                carriedCompletionSink = prior.parkedCompletionSink;
            core.handles.delete(id);
        }
    }
    if (core.handles.has(id))
        throw new Error(`TaskRegistry: task id already registered: ${id}`);
    const now = input.now ?? Date.now();
    if (input.name !== undefined && (typeof input.name !== "string" || input.name === "")) {
        throw new Error(`TaskRegistry: background-agent name must be a non-empty string (got ${describeRejectedValue(input.name)}); omit it to take the derived one`);
    }
    const name = input.name ?? engineAgentName(id);
    const requestedBy = readExtensionRequester(input.requestedBy);
    core.handles.set(id, {
        id,
        type: "background_agent",
        status: input.initialStatus ?? "running",
        ...occupantStamp(input),
        ...(input.parkedCheckpointToken !== undefined ? { parkedCheckpointToken: input.parkedCheckpointToken } : {}),
        ...(input.resolveParkedStop !== undefined ? { resolveParkedStop: input.resolveParkedStop } : {}),
        ...(carriedCompletionSink !== undefined ? { parkedCompletionSink: carriedCompletionSink } : {}),
        ...(input.sessionScoped ? { sessionScoped: true } : {}),
        ...(input.description !== undefined ? { description: input.description } : {}),
        name,
        ...(input.agentType !== undefined ? { agentType: input.agentType } : {}),
        ...(requestedBy !== undefined ? { requestedBy } : {}),
        ...(input.parentTaskId !== undefined ? { parentTaskId: input.parentTaskId } : {}),
        ...(input.parentSessionId !== undefined ? { parentSessionId: input.parentSessionId } : {}),
        ...(input.rootSessionId !== undefined ? { rootSessionId: input.rootSessionId } : {}),
        ...(input.crossBoundaryTree !== undefined ? { crossBoundaryTree: input.crossBoundaryTree } : {}),
        ...(input.delegationTree !== undefined ? { delegationTree: input.delegationTree } : {}),
        ...(input.deliveryChannel !== undefined ? { channelState: input.deliveryChannel } : {}),
        ...(input.retainedContinuation !== undefined ? { retainedContinuation: input.retainedContinuation } : {}),
        ...(input.owner !== undefined ? { owner: input.owner } : {}),
        ...(input.scope !== undefined ? { scope: input.scope } : {}),
        ...(input.toolUseId !== undefined ? { toolUseId: input.toolUseId } : {}),
        cycleSeq: input.durableSeed !== undefined ? (input.durableSeed.record.seq ?? 1) : 1,
        createdAt: now,
        ...activityStamp(now),
        abort: input.abort,
        yieldSeat: { lane: input.yieldLane ?? "no_park_lane" },
    });
    if (input.store) {
        const handle = core.handles.get(id);
        if (input.durableSeed !== undefined) {
            const seeded = structuredClone(input.durableSeed.record);
            seeded.rev = input.durableSeed.rev;
            seeded.name = name;
            handle.durable = {
                store: input.store,
                record: seeded,
                chain: Promise.resolve(),
                written: true,
                poisoned: false,
                flushFailed: false,
            };
            ensureDurableHeartbeatLane(core);
            return id;
        }
        handle.durable = {
            store: input.store,
            record: {
                handle: id,
                scope: input.scope,
                owner: input.owner,
                sessionScoped: input.sessionScoped === true,
                ...(input.parentSessionId !== undefined ? { parentSessionId: input.parentSessionId } : {}),
                ...(input.rootSessionId !== undefined ? { rootSessionId: input.rootSessionId } : {}),
                ...(input.rootSessionId !== undefined && input.rootSessionIncarnation !== undefined ? { rootSessionIncarnation: input.rootSessionIncarnation } : {}),
                ...(input.rootSessionId !== undefined && input.rootSessionScope !== undefined ? { rootSessionScope: input.rootSessionScope } : {}),
                ...(input.parentTaskId !== undefined ? { parentTaskId: input.parentTaskId } : {}),
                name,
                ...(input.agentType !== undefined ? { agentType: input.agentType } : {}),
                ...(requestedBy !== undefined ? { requestedBy } : {}),
                ...(input.description !== undefined ? { description: input.description } : {}),
                ...(input.model !== undefined ? { model: input.model } : {}),
                ...(input.policyFloor !== undefined ? { policyFloor: input.policyFloor } : {}),
                ...(input.spawnDepth !== undefined ? { spawnDepth: input.spawnDepth } : {}),
                ...(input.bornInLedgerRun === true ? { bornInLedgerRun: true } : {}),
                writerId: core.writerId,
                writerEpoch: 1,
                seq: 1,
                spawnedAt: now,
                updatedAt: now,
                status: "running",
                rev: 0,
            },
            chain: Promise.resolve(),
            written: false,
            poisoned: false,
            flushFailed: false,
        };
        durableAgentWriteLane(handle, {});
        ensureDurableHeartbeatLane(core);
    }
    return id;
}
export function parkBackgroundAgentLane(core, id, park) {
    const handle = core.handles.get(id);
    if (!handle || handle.type !== "background_agent")
        return undefined;
    if (handle.status !== "running")
        return undefined;
    handle.status = "parked";
    handle.parkedCheckpointToken = park.checkpointToken;
    handle.resolveParkedStop = park.resolveStop;
    handle.parkedCompletionSink = park.completionSink;
    handle.notify = undefined;
    handle.approvalHolds = undefined;
    for (const [, , qResolve] of handle.preAttachQueue ?? [])
        qResolve?.({ ok: false, reason: "not_running" });
    handle.preAttachQueue = undefined;
    for (const r of [...(handle.inFlightDirect ?? [])])
        r({ ok: false, reason: "not_running" });
    handle.inFlightDirect = undefined;
    stampActivity(handle);
    core.pokeBgQuiescence(handle.owner);
    durableAgentWriteLane(handle, {
        status: "parked",
        parkedCheckpointToken: park.checkpointToken,
        parkedAt: Date.now(),
    }, ["parkClaimId"]);
    return "parked";
}
export async function reconcileParkedAgentsLane(core, stores, scope, now, opts) {
    let liveFailed = 0;
    for (const handle of core.handles.values()) {
        if (handle.type !== "background_agent" || handle.status !== "parked")
            continue;
        if (handle.scope !== undefined && handle.scope !== scope)
            continue;
        const token = handle.parkedCheckpointToken;
        const cardOf = (t) => ({ token: t, handle: handle.id, scope, sessionId: childSessionAnchorOf(handle)?.sessionId });
        const state = token === undefined ? "missing" : await parkedCardState(stores.checkpointStore, cardOf(token));
        let error;
        if (state === "expired" || state === "missing") {
            error = `approval checkpoint ${state} while parked (reconciled)`;
        }
        else if (state === "resolved") {
            let row;
            try {
                row = await stores.agentStore.get(handle.id, scope);
            }
            catch {
                continue;
            }
            if (row === null || row.status !== "parked" || row.parkClaimId !== undefined)
                continue;
            if (token === undefined || (await parkedCardState(stores.checkpointStore, cardOf(token))) !== "resolved")
                continue;
            error =
                "approval checkpoint resolved outside the registry's claim path while parked — the decision was " +
                    "redeemed by another leg and this row's outcome is unknown (reconciled)";
        }
        else {
            continue;
        }
        const held = heldPark(core, handle.id, token);
        if (held === undefined)
            continue;
        held.status = "failed";
        held.error = error;
        held.parkedCheckpointToken = undefined;
        held.resolveParkedStop = undefined;
        stampActivity(held, now);
        mintCompletionId(held);
        core.pokeBgQuiescence(held.owner);
        const endingQueued = queuedSignal();
        announceParkedEnding(core, held, { status: "failed", error, endingQueued: endingQueued.promise });
        durableAgentWriteLane(held, { status: "failed", error: held.error, settledAt: now, stoppedBy: "system", completionId: held.completionId }, ["parkedCheckpointToken", "parkClaimId", "parkedAt"]);
        endingQueued.done();
        liveFailed++;
    }
    const { reconcileParkedAgents: storeReconcile } = await import("./background-agent-store.js");
    const liveHandles = new Set();
    for (const h of core.handles.values())
        if (h.type === "background_agent")
            liveHandles.add(h.id);
    const storeHalf = await storeReconcile(stores, scope, now, {
        ...(opts?.staleClaimMaxAgeMs !== undefined ? { staleClaimMaxAgeMs: opts.staleClaimMaxAgeMs } : {}),
        excludeHandles: liveHandles,
    });
    return { failed: storeHalf.failed + liveFailed, rolledBack: storeHalf.rolledBack, unreadable: storeHalf.unreadable };
}
export async function claimParkedAgentLane(stores, handle, access) {
    if (access.scope === undefined)
        return { ok: false, reason: "not_found" };
    let row;
    try {
        row = await stores.agentStore.get(handle, access.scope);
    }
    catch {
        return { ok: false, reason: "store_unreachable" };
    }
    if (!row || !canAccessAgentRecord(row, access))
        return { ok: false, reason: "not_found" };
    if (row.status !== "parked")
        return { ok: false, reason: "not_parked" };
    const token = row.parkedCheckpointToken;
    if (token === undefined || row.sessionId === undefined)
        return { ok: false, reason: "binding_broken" };
    let cp;
    try {
        cp = (await stores.checkpointStore.get(token));
    }
    catch (e) {
        return { ok: false, reason: isCheckpointRowUnreadable(e) ? "binding_broken" : "store_unreachable" };
    }
    if (cp === null || cp.sessionId !== row.sessionId)
        return { ok: false, reason: "binding_broken" };
    if (cp.status !== "pending")
        return { ok: false, reason: "checkpoint_not_pending" };
    const claimId = randomBytes(8).toString("hex");
    const reserved = structuredClone(row);
    reserved.parkClaimId = claimId;
    reserved.updatedAt = Date.now();
    let won;
    try {
        won = await stores.agentStore.updateIf(handle, access.scope, reserved, {
            rev: row.rev,
            status: "parked",
            parkedCheckpointToken: token,
            parkClaimId: null,
        });
    }
    catch {
        return { ok: false, reason: "store_unreachable" };
    }
    if (!won)
        return { ok: false, reason: "claim_lost" };
    return {
        ok: true,
        ticket: { handle, scope: access.scope, token, claimId, reservedRev: row.rev + 1, row: structuredClone(row) },
    };
}
export async function rollbackParkedClaimLane(core, stores, ticket) {
    let cpStatus;
    try {
        const cp = (await stores.checkpointStore.get(ticket.token));
        cpStatus = cp === null ? "missing" : cp.status;
    }
    catch {
        return "retry";
    }
    const now = Date.now();
    const rollbackCompletionIdCandidate = completionIdCandidate();
    const settleLocalParkedHandle = (error) => {
        const h = core.handles.get(ticket.handle);
        if (h !== undefined && h.type === "background_agent" && h.status === "parked") {
            h.status = "failed";
            h.error = error;
            h.parkedCheckpointToken = undefined;
            h.resolveParkedStop = undefined;
            h.parkedCompletionSink = undefined;
            h.terminalNotifier = undefined;
            stampActivity(h, now);
            commitCompletionIdIfEmpty(h, rollbackCompletionIdCandidate);
            core.pokeBgQuiescence(h.owner);
        }
    };
    let liveRow;
    try {
        liveRow = await stores.agentStore.get(ticket.handle, ticket.scope);
    }
    catch {
        return "retry";
    }
    const lostTo = (approval) => (approval === "pending" ? "lost" : "lost_unredeemable");
    const lostNow = async (lastRead = cpStatus) => {
        try {
            const cp = (await stores.checkpointStore.get(ticket.token));
            return lostTo(cp === null ? "missing" : cp.status);
        }
        catch {
            return lostTo(lastRead);
        }
    };
    if (!liveRow || liveRow.parkClaimId !== ticket.claimId)
        return lostNow();
    if (cpStatus === "pending") {
        const restored = structuredClone(liveRow);
        delete restored.parkClaimId;
        restored.updatedAt = now;
        restored.writerEpoch = (liveRow.writerEpoch ?? 0) + 1;
        const liveHandle = core.handles.get(ticket.handle);
        if (liveHandle?.type === "background_agent" &&
            liveHandle.durable !== undefined &&
            liveHandle.durable.record.writerId === liveRow.writerId &&
            (liveHandle.durable.record.writerEpoch ?? 0) === (liveRow.writerEpoch ?? 0)) {
            liveHandle.durable.record.writerEpoch = restored.writerEpoch;
        }
        try {
            const won = await stores.agentStore.updateIf(ticket.handle, ticket.scope, restored, {
                rev: liveRow.rev,
                status: "parked",
                parkedCheckpointToken: ticket.token,
                parkClaimId: ticket.claimId,
            });
            if (!won)
                return await lostNow();
            let cp2;
            try {
                cp2 = (await stores.checkpointStore.get(ticket.token));
            }
            catch {
                return "rolled_back";
            }
            if (cp2 !== null && cp2.status === "pending")
                return "rolled_back";
            const gone = cp2 === null ? "missing" : cp2.status;
            let settledWith;
            try {
                const post = await stores.agentStore.get(ticket.handle, ticket.scope);
                if (post && post.status === "parked" && post.parkedCheckpointToken === ticket.token && post.parkClaimId === undefined) {
                    const term = structuredClone(post);
                    term.status = "failed";
                    term.error = `approval checkpoint ${gone} under the resume claim (claim rollback)`;
                    term.stoppedBy = "system";
                    term.settledAt = now;
                    term.updatedAt = now;
                    term.completionId = rollbackCompletionIdCandidate;
                    delete term.parkedCheckpointToken;
                    delete term.parkClaimId;
                    delete term.parkedAt;
                    if (await stores.agentStore.updateIf(ticket.handle, ticket.scope, term, { rev: post.rev, status: "parked", parkedCheckpointToken: ticket.token, parkClaimId: null }))
                        settledWith = term.error;
                }
            }
            catch {
                return "retry";
            }
            if (settledWith !== undefined) {
                settleLocalParkedHandle(settledWith);
                return "failed";
            }
            return await lostNow(gone);
        }
        catch {
            return "retry";
        }
    }
    const error = cpStatus === "resolved"
        ? "approval consumed but the resume could not proceed — outcome unknown (claim rollback)"
        : `approval checkpoint ${cpStatus} under the resume claim (claim rollback)`;
    if (liveRow.status !== "parked")
        return lostNow();
    const failed = structuredClone(liveRow);
    failed.status = "failed";
    failed.error = error;
    failed.stoppedBy = "system";
    failed.settledAt = now;
    failed.updatedAt = now;
    failed.completionId = rollbackCompletionIdCandidate;
    delete failed.parkedCheckpointToken;
    delete failed.parkClaimId;
    delete failed.parkedAt;
    try {
        const won = await stores.agentStore.updateIf(ticket.handle, ticket.scope, failed, {
            rev: liveRow.rev,
            status: "parked",
            parkClaimId: ticket.claimId,
        });
        if (won)
            settleLocalParkedHandle(error);
        return won ? "failed" : await lostNow();
    }
    catch {
        return "retry";
    }
}
export async function consumeParkedFlipLane(core, id, stores, ticket) {
    const handle = core.handles.get(id);
    if (!handle || handle.type !== "background_agent" || handle.status !== "parked")
        return undefined;
    const lane = handle.durable;
    const doFlip = async () => {
        let live;
        try {
            live = await stores.agentStore.get(ticket.handle, ticket.scope);
        }
        catch {
            return undefined;
        }
        if (!live || live.status !== "parked" || live.parkClaimId !== ticket.claimId || live.parkedCheckpointToken !== ticket.token)
            return undefined;
        const flipped = structuredClone(live);
        flipped.status = "running";
        flipped.writerId = core.writerId;
        flipped.writerEpoch = (live.writerEpoch ?? 0) + 1;
        flipped.updatedAt = Date.now();
        flipped.seq = nextCycleSeq(live);
        delete flipped.parkedCheckpointToken;
        delete flipped.parkedAt;
        try {
            const won = await stores.agentStore.updateIf(ticket.handle, ticket.scope, flipped, {
                rev: live.rev,
                status: "parked",
                parkedCheckpointToken: ticket.token,
                parkClaimId: ticket.claimId,
            });
            if (!won)
                return undefined;
        }
        catch {
            return undefined;
        }
        flipped.rev = live.rev + 1;
        return flipped;
    };
    let flipped;
    if (lane !== undefined && !lane.poisoned) {
        const p = lane.chain.then(doFlip, doFlip);
        lane.chain = p.then((f) => {
            if (f !== undefined) {
                lane.record = structuredClone(f);
                lane.written = true;
            }
        });
        flipped = await p;
    }
    else {
        flipped = await doFlip();
    }
    if (flipped === undefined)
        return undefined;
    handle.status = "running";
    handle.approvalHolds = undefined;
    handle.parkedCheckpointToken = undefined;
    handle.resolveParkedStop = undefined;
    handle.parkedCompletionSink = undefined;
    stampActivity(handle);
    handle.completionId = undefined;
    handle.terminalNotified = undefined;
    handle.cycleSeq = flipped.seq;
    return flipped.seq;
}
export function finalizeParkedResumeLane(core, id) {
    const handle = core.handles.get(id);
    if (!handle || handle.type !== "background_agent")
        return;
    durableAgentWriteLane(handle, {}, ["parkClaimId"]);
}
export function settleBackgroundAgentLane(core, id, outcome) {
    const handle = core.handles.get(id);
    if (!handle || handle.type !== "background_agent")
        return undefined;
    if (outcome.cycle === undefined && (handle.reviveCycle ?? 0) !== 0) {
        throw new Error(`settleBackgroundAgent("${id}"): the row has been revived (cycle ${handle.reviveCycle}) — pass the cycle this settle speaks for, or use settleRevivedAgent`);
    }
    if ((outcome.cycle ?? 0) !== (handle.reviveCycle ?? 0))
        return undefined;
    if (handle.status !== "running") {
        if (handle.status === "killed") {
            mintCompletionId(handle);
            let backfilled = false;
            if (handle.result === undefined && outcome.result !== undefined && outcome.result !== "") {
                handle.result = outcome.result;
                handle.resultIsPartial = true;
                backfilled = true;
                if (outcome.resultFull !== undefined)
                    handle.resultFull = outcome.resultFull;
            }
            if (handle.error === undefined && outcome.error !== undefined && outcome.error !== BG_AGENT_REAP_STOP_ERROR) {
                handle.error = outcome.error;
                backfilled = true;
            }
            if (outcome.leg !== undefined && handle.outcome?.refused === undefined) {
                const withLeg = backgroundAgentOutcomeOf("killed", undefined, handle.stoppedBy, outcome.leg, runEndOf(handle));
                if (handle.outcome === undefined || withLeg.refused !== undefined) {
                    handle.outcome = withLeg;
                    backfilled = true;
                }
            }
            if (backfilled) {
                stampActivity(handle);
                durableAgentWriteLane(handle, {
                    ...(handle.result !== undefined ? { finalOutput: handle.result } : {}),
                    ...(handle.resultFull !== undefined ? { finalOutputFull: handle.resultFull } : {}),
                    ...(handle.resultIsPartial ? { resultIsPartial: true } : {}),
                    ...(handle.error !== undefined ? { error: handle.error } : {}),
                    ...(handle.completionId !== undefined ? { completionId: handle.completionId } : {}),
                    ...(handle.outcome !== undefined ? { outcome: handle.outcome } : {}),
                });
            }
        }
        return isBackgroundAgentSettledStatus(handle.status) ? handle.status : undefined;
    }
    handle.status = outcome.status;
    mintCompletionId(handle);
    handle.notify = undefined;
    handle.terminalNotifier = undefined;
    handle.approvalHolds = undefined;
    for (const [, , qResolve] of handle.preAttachQueue ?? [])
        qResolve?.({ ok: false, reason: "not_running" });
    handle.preAttachQueue = undefined;
    for (const r of [...(handle.inFlightDirect ?? [])])
        r({ ok: false, reason: "not_running" });
    handle.inFlightDirect = undefined;
    if (outcome.status === "killed" && handle.stoppedBy === undefined) {
        handle.stoppedBy = outcome.stoppedBy ?? handle.stopSource ?? "system";
    }
    if (outcome.result !== undefined)
        handle.result = outcome.result;
    handle.outcome = backgroundAgentOutcomeOf(outcome.status, outcome.status === "failed" ? outcome.errorCode : undefined, handle.stoppedBy, outcome.leg, outcome.status === "killed" ? runEndOf(handle) : undefined);
    if (outcome.resultFull !== undefined)
        handle.resultFull = outcome.resultFull;
    if (outcome.status === "killed" && outcome.result !== undefined && outcome.result !== "") {
        handle.resultIsPartial = true;
    }
    if (outcome.error !== undefined && !(outcome.status === "killed" && outcome.error === BG_AGENT_REAP_STOP_ERROR)) {
        handle.error = outcome.error;
        if (outcome.errorCode !== undefined)
            handle.errorCode = outcome.errorCode;
        if (outcome.retryable !== undefined)
            handle.errorRetryable = outcome.retryable;
        if (outcome.errorKind !== undefined)
            handle.errorKind = outcome.errorKind;
        if (outcome.retryAfterMs !== undefined)
            handle.errorRetryAfterMs = outcome.retryAfterMs;
    }
    stampActivity(handle);
    core.pokeBgQuiescence(handle.owner);
    durableAgentWriteLane(handle, {
        status: outcome.status,
        settledAt: Date.now(),
        ...(handle.stoppedBy !== undefined ? { stoppedBy: handle.stoppedBy } : {}),
        ...(handle.completionId !== undefined ? { completionId: handle.completionId } : {}),
        ...(handle.result !== undefined ? { finalOutput: handle.result } : {}),
        ...(handle.resultFull !== undefined ? { finalOutputFull: handle.resultFull } : {}),
        ...(handle.resultIsPartial ? { resultIsPartial: true } : {}),
        ...(handle.error !== undefined ? { error: handle.error } : {}),
        outcome: handle.outcome,
        ...(handle.errorCode !== undefined ? { errorCode: handle.errorCode } : {}),
        ...(handle.errorRetryable !== undefined ? { errorRetryable: handle.errorRetryable } : {}),
        ...(handle.errorKind !== undefined ? { errorKind: handle.errorKind } : {}),
        ...(handle.errorRetryAfterMs !== undefined ? { errorRetryAfterMs: handle.errorRetryAfterMs } : {}),
    }, ["parkedCheckpointToken", "parkClaimId", "parkedAt"]);
    return outcome.status;
}
function runEndOf(handle) {
    return handle.stopRunEnd !== undefined ? { cause: handle.stopRunEnd } : undefined;
}
export function ownerSweepTakes(handle, access, opts) {
    if (!isLoopTask(handle) || handle.status !== "running" || !canAccess(handle, access))
        return false;
    if (opts?.skipSessionScoped && handle.sessionScoped)
        return false;
    if (opts?.sessionScopedOnly && !handle.sessionScoped)
        return false;
    return true;
}
export function abortBackgroundAgentsForOwnerLane(core, access, opts, runEnd) {
    let aborted = 0;
    if (runEnd !== undefined) {
        for (const handle of [...core.handles.values()])
            if (ownerSweepTakes(handle, access, opts) && !handle.abort.signal.aborted)
                core.markRunEnd(handle.id, runEnd);
    }
    for (const handle of core.handles.values()) {
        if (!ownerSweepTakes(handle, access, opts))
            continue;
        core.markStopSource(handle.id, "parent");
        handle.abort.abort();
        aborted++;
    }
    return aborted;
}
export function resolveBackgroundAgentByNameLane(core, name, access, opts) {
    const key = normalizeAgentName(name);
    if (key === "")
        return { status: "not_found" };
    const latestOf = (pool) => {
        let best;
        for (const h of pool)
            if (best === undefined || h.createdAt >= best.createdAt)
                best = h;
        return best;
    };
    const named = [];
    for (const h of core.handles.values()) {
        if (h.type !== "background_agent" || !canAccess(h, access))
            continue;
        if (h.name === name || normalizeAgentName(h.name) === key)
            named.push(h);
    }
    if (opts?.preferRunning === true) {
        const namedRunning = named.filter((h) => h.status === "running");
        if (namedRunning.length > 1) {
            return {
                status: "ambiguous",
                message: `Multiple RUNNING background agents match "${name}": ${namedRunning.map((h) => `${h.id} (${h.name})`).join(", ")}. Use the task ID.`,
            };
        }
        if (namedRunning.length === 1)
            return { status: "found", handle: namedRunning[0] };
    }
    const namedWinner = latestOf(named);
    if (namedWinner !== undefined)
        return { status: "found", handle: namedWinner };
    const exact = [];
    for (const h of core.handles.values()) {
        if (h.type !== "background_agent" || !canAccess(h, access))
            continue;
        if (h.description === name)
            exact.push(h);
    }
    const exactRunning = exact.filter((h) => h.status === "running");
    const exactPool = exactRunning.length > 0 ? exactRunning : exact;
    if (exactPool.length === 1)
        return { status: "found", handle: exactPool[0] };
    if (exactPool.length > 1) {
        return {
            status: "ambiguous",
            message: `Multiple background agents match "${name}": ${exactPool.map((h) => (h.description ? `${h.id} (${h.description})` : h.id)).join(", ")}. Use the task ID.`,
        };
    }
    const matches = [];
    for (const h of core.handles.values()) {
        if (h.type !== "background_agent" || !canAccess(h, access))
            continue;
        if (h.description !== undefined && normalizeAgentName(h.description) === key)
            matches.push(h);
    }
    const running = matches.filter((h) => h.status === "running");
    const pool = running.length > 0 ? running : matches;
    if (pool.length === 1)
        return { status: "found", handle: pool[0] };
    if (pool.length > 1) {
        return {
            status: "ambiguous",
            message: `Multiple background agents match "${name}": ${pool.map((h) => (h.description ? `${h.id} (${h.description})` : h.id)).join(", ")}. Use the task ID.`,
        };
    }
    const byKey = new Map();
    for (const h of core.handles.values()) {
        if (h.type !== "background_agent" || h.status !== "running" || !canAccess(h, access))
            continue;
        for (const label of [h.name, h.description]) {
            if (label === undefined)
                continue;
            const k = normalizeAgentName(label);
            if (k !== "" && !byKey.has(k))
                byKey.set(k, label);
        }
    }
    const closest = closestName(key, byKey.keys());
    return { status: "not_found", ...(closest !== undefined ? { suggestion: byKey.get(closest) } : {}) };
}
const SPAWN_NAME_MAX = 64;
export function claimBackgroundAgentNameLane(core, requested, access) {
    const taken = new Set();
    for (const h of core.handles.values()) {
        if (h.type === "background_agent" && canAccess(h, access))
            taken.add(normalizeAgentName(h.name));
    }
    for (const c of core.agentNameClaims) {
        if (c.scope === access.scope && (c.owner === access.owner || (c.sessionId !== undefined && c.sessionId === access.sessionId)))
            taken.add(c.key);
    }
    let name = requested;
    for (let n = 2; taken.has(normalizeAgentName(name)); n++)
        name = `${requested.slice(0, SPAWN_NAME_MAX - `-${n}`.length)}-${n}`;
    const claim = { key: normalizeAgentName(name), scope: access.scope, owner: access.owner, sessionId: access.sessionId };
    core.agentNameClaims.add(claim);
    return { name, release: () => void core.agentNameClaims.delete(claim) };
}
export function markRetainedContinuationLane(core, id) {
    const handle = core.handles.get(id);
    if (handle && handle.type === "background_agent") {
        handle.retainedContinuation = true;
        handle.retentionHeld = true;
    }
}
export function backgroundAgentCycleSeqLane(core, id) {
    const handle = core.handles.get(id);
    if (handle === undefined || handle.type !== "background_agent")
        return undefined;
    return handle.cycleSeq;
}
async function claimTerminalRowForRevive(core, store, handle, scope) {
    for (let attempt = 0; attempt < 3; attempt++) {
        let live;
        try {
            live = await store.get(handle, scope);
        }
        catch {
            return { status: "still_running" };
        }
        if (live === null)
            return { status: "not_found" };
        if (!isBackgroundAgentSettledStatus(live.status))
            return { status: "still_running" };
        const claimed = structuredClone(live);
        claimed.status = "running";
        clearRevivedRowTerminalPayload(claimed);
        claimed.writerId = core.writerId;
        claimed.writerEpoch = (live.writerEpoch ?? 0) + 1;
        claimed.seq = nextCycleSeq(live);
        claimed.updatedAt = Date.now();
        let won = false;
        try {
            won = await store.updateIf(handle, scope, claimed, { rev: live.rev, status: live.status });
        }
        catch {
            let after;
            try {
                after = await store.get(handle, scope);
            }
            catch {
                return { status: "still_running" };
            }
            if (after !== null && after.status === "running" && after.writerId === core.writerId && after.writerEpoch === claimed.writerEpoch) {
                return { status: "claimed", row: after, previous: live };
            }
            return { status: "still_running" };
        }
        if (!won)
            continue;
        claimed.rev = live.rev + 1;
        return { status: "claimed", row: claimed, previous: live };
    }
    return { status: "still_running" };
}
async function rollbackRevivalClaim(store, claim) {
    const restored = structuredClone(claim.previous);
    restored.writerEpoch = (claim.row.writerEpoch ?? 0) + 1;
    restored.updatedAt = Date.now();
    try {
        await store.updateIf(restored.handle, restored.scope, restored, { rev: claim.row.rev, status: "running" });
    }
    catch {
    }
}
export async function reviveBackgroundAgentLane(core, id, access, abort) {
    const handle = core.handles.get(id);
    if (!handle || handle.type !== "background_agent" || !canAccess(handle, access))
        return { ok: false, reason: "not_found" };
    if (core.reapedHandles.has(id))
        return { ok: false, reason: "not_found" };
    if (core.reapingHandles.has(id) || core.claimingHandles.has(id))
        return { ok: false, reason: "recycling" };
    if (handle.status === "running" || handle.status === "pending")
        return { ok: false, reason: "still_running" };
    if (handle.status === "parked")
        return { ok: false, reason: "still_running" };
    const lane = handle.durable;
    let plainDurableWrite = lane === undefined;
    let claimedSeq;
    if (lane !== undefined) {
        core.claimingHandles.add(id);
        let claim;
        try {
            const run = () => claimTerminalRowForRevive(core, lane.store, lane.record.handle, lane.record.scope);
            const adopt = (row) => {
                lane.poisoned = true;
                handle.durable = { store: lane.store, record: structuredClone(row), chain: Promise.resolve(), written: true, poisoned: false, flushFailed: false };
            };
            if (lane.poisoned) {
                claim = await run();
                if (claim.status === "claimed")
                    adopt(claim.row);
            }
            else {
                const p = lane.chain.then(run, run);
                lane.chain = p.then((r) => {
                    if (r.status === "claimed")
                        adopt(r.row);
                });
                claim = await p;
            }
        }
        finally {
            core.claimingHandles.delete(id);
        }
        if (claim.status !== "claimed") {
            if (claim.status === "not_found" && !lane.written && !lane.poisoned)
                plainDurableWrite = true;
            else
                return { ok: false, reason: claim.status };
        }
        if (core.handles.get(id) !== handle) {
            if (claim.status === "claimed")
                await rollbackRevivalClaim(lane.store, claim);
            return { ok: false, reason: "still_running" };
        }
        ensureDurableHeartbeatLane(core);
        if (claim.status === "claimed")
            claimedSeq = claim.row.seq;
    }
    handle.status = "running";
    handle.approvalHolds = undefined;
    handle.channelState = "attaching";
    handle.notify = undefined;
    if (abort !== undefined)
        handle.abort = abort;
    handle.result = undefined;
    handle.resultFull = undefined;
    handle.artifactRef = undefined;
    handle.error = undefined;
    handle.errorCode = undefined;
    handle.errorRetryable = undefined;
    handle.errorKind = undefined;
    handle.errorRetryAfterMs = undefined;
    handle.resultIsPartial = undefined;
    handle.outcome = undefined;
    handle.stopSource = undefined;
    handle.stopRunEnd = undefined;
    handle.stoppedBy = undefined;
    handle.completionId = undefined;
    handle.terminalNotified = undefined;
    handle.yieldSeat = { lane: "no_park_lane" };
    handle.reviveCycle = (handle.reviveCycle ?? 0) + 1;
    handle.cycleSeq = claimedSeq ?? nextCycleSeq({ seq: handle.cycleSeq });
    stampActivity(handle);
    if (plainDurableWrite)
        durableAgentWriteLane(handle, { status: "running", seq: handle.cycleSeq }, REVIVED_ROW_CLEARED_FIELDS);
    return { ok: true, cycle: handle.reviveCycle };
}
export function settleRevivedAgentLane(core, id, cycle, outcome) {
    return settleBackgroundAgentLane(core, id, { ...outcome, cycle });
}
export function noteBackgroundAgentActivityLane(core, id, atMono = monotonicNow()) {
    if (!isActivityInstant(atMono))
        throw new Error(`noteBackgroundAgentActivity: the instant must be a finite monotonic reading in milliseconds, from 0 to this process's clock (got ${String(atMono)})`);
    const handle = core.handles.get(id);
    if (!handle || handle.type !== "background_agent" || handle.status !== "running")
        return;
    stampActivity(handle, Date.now(), atMono);
}
export function attachBackgroundAgentApprovalHoldsLane(core, id, read) {
    if (typeof read !== "function")
        throw Object.assign(new TypeError(`attachBackgroundAgentApprovalHolds: the reader must be a function (got ${typeof read})`), { code: "config.invalid_argument" });
    const handle = core.handles.get(id);
    if (handle === undefined || handle.type !== "background_agent" || handle.status !== "running")
        return;
    handle.approvalHolds = read;
}
export function rowApprovalHoldsSeat(registry, id) {
    return (read) => registry.attachBackgroundAgentApprovalHolds(id, read);
}
export function reapStaleSessionBackgroundAgentsLane(core, staleMs, nowMono = monotonicNow()) {
    if (!Number.isFinite(staleMs) || staleMs <= 0) {
        throw new Error(`reapStaleSessionBackgroundAgents: staleMs must be a positive finite number of milliseconds (got ${String(staleMs)})`);
    }
    if (!Number.isFinite(nowMono)) {
        throw new Error(`reapStaleSessionBackgroundAgents: the instant must be a finite monotonic reading in milliseconds (got ${String(nowMono)})`);
    }
    let reaped = 0;
    for (const handle of core.handles.values()) {
        if (!isLiveSessionResidentAgent(handle))
            continue;
        if (nowMono - handle.lastActivityMono <= staleMs)
            continue;
        if (approvalHeldOfHandle(handle).length > 0)
            continue;
        core.markStopSource(handle.id, "system");
        const reapTerminalNote = handle.terminalNotifier;
        handle.abort.abort();
        settleBackgroundAgentLane(core, handle.id, {
            status: "killed",
            error: STALE_RUNNING_REAP_ATTRIBUTION,
            cycle: handle.reviveCycle ?? 0,
        });
        announceLandedAgentTerminal(core, handle, reapTerminalNote, { status: "killed" });
        reaped++;
    }
    return reaped;
}
export function announceLandedAgentTerminal(core, handle, notifier, ending) {
    if (notifier === undefined)
        return;
    core.notifyTerminalOnce(handle, () => notifier(ending));
}
function heldPark(core, id, token) {
    const h = core.handles.get(id);
    return h?.type === "background_agent" && h.status === "parked" && h.parkedCheckpointToken === token ? h : undefined;
}
function queuedSignal() {
    let done;
    const promise = new Promise((r) => (done = r));
    return { promise, done };
}
function announceParkedEnding(core, handle, ending) {
    const parkLane = handle.parkedCompletionSink;
    const notifier = handle.terminalNotifier;
    handle.parkedCompletionSink = undefined;
    handle.terminalNotifier = undefined;
    if (parkLane !== undefined)
        announceLandedAgentTerminal(core, handle, notifier, { ...ending, parkLane });
}
export function unmarkRetainedContinuationLane(core, id) {
    const handle = core.handles.get(id);
    if (handle && handle.type === "background_agent")
        handle.retainedContinuation = false;
}
export function endRetentionHoldLane(core, id) {
    const handle = core.handles.get(id);
    if (handle && handle.type === "background_agent")
        delete handle.retentionHeld;
}
export function attachAgentNotifyLane(core, id, notify, cycle) {
    const handle = core.handles.get(id);
    if (!handle || handle.type !== "background_agent" || handle.status !== "running")
        return;
    if (cycle !== undefined && (handle.reviveCycle ?? 0) !== cycle)
        return;
    handle.notify = notify;
    handle.channelState = "attaching";
    const live = handle.preAttachQueue;
    if (live !== undefined && live.length > 0) {
        void (async () => {
            while (handle.preAttachQueue === live && live.length > 0) {
                const [qPayload, qOpts, qResolve] = live[0];
                let result;
                try {
                    const d = await notify(qPayload, qOpts);
                    result =
                        d === "parked" && handle.retainedContinuation !== true
                            ? { ok: false, reason: "not_running" }
                            : d === "parked"
                                ? { ok: true, disposition: "parked" }
                                : { ok: true, disposition: "queued" };
                }
                catch {
                    result = { ok: false, reason: "not_running" };
                }
                if (handle.preAttachQueue !== live)
                    return;
                live.shift();
                qResolve?.(result);
            }
            if (handle.preAttachQueue === live) {
                handle.preAttachQueue = undefined;
                handle.channelState = "attached";
            }
        })();
    }
    else {
        handle.preAttachQueue = undefined;
        handle.channelState = "attached";
    }
}
export async function deliverToRunningAgentLane(core, id, access, notification, opts) {
    const handle = core.handles.get(id);
    if (!handle || handle.type !== "background_agent" || !canAccess(handle, access))
        return { ok: false, reason: "not_found" };
    if (handle.status !== "running")
        return { ok: false, reason: "not_running" };
    const bounded = (p, fallback, onTimeout) => new Promise((resolve) => {
        const t = setTimeout(() => {
            try {
                onTimeout?.();
            }
            catch {
            }
            resolve(fallback);
        }, core.deliverConfirmTimeoutMs);
        t.unref?.();
        void p.then((v) => {
            clearTimeout(t);
            resolve(v);
        }, () => {
            clearTimeout(t);
            resolve(fallback);
        });
    });
    if (handle.notify === undefined || handle.channelState === "attaching") {
        if (handle.channelState !== "attaching")
            return { ok: false, reason: "no_channel" };
        const q = (handle.preAttachQueue ??= []);
        if (q.length >= 8)
            return { ok: false, reason: "queue_full" };
        return bounded(new Promise((resolve) => {
            q.push([notification, opts, resolve]);
        }), { ok: true, disposition: "pending" });
    }
    let detachOnTimeout;
    return await bounded(new Promise((resolve) => {
        const inflight = (handle.inFlightDirect ??= new Set());
        const settleOnce = (r) => {
            if (!inflight.has(settleOnce))
                return;
            inflight.delete(settleOnce);
            resolve(r);
        };
        inflight.add(settleOnce);
        detachOnTimeout = () => inflight.delete(settleOnce);
        void (async () => {
            let result;
            try {
                const d = await handle.notify?.(notification, opts);
                if (d === "parked" || d === "parked_durably") {
                    result = handle.retainedContinuation === true ? { ok: true, disposition: "parked" } : { ok: false, reason: "not_running" };
                }
                else if (d === "refused") {
                    result = { ok: false, reason: "refused" };
                }
                else {
                    result = { ok: true, disposition: "queued" };
                }
            }
            catch {
                result = { ok: false, reason: "not_running" };
            }
            settleOnce(result);
        })();
    }), { ok: true, disposition: "pending" }, () => detachOnTimeout?.());
}
export function runningBackgroundAgentLabelsLane(core, access) {
    const out = [];
    for (const h of core.handles.values()) {
        if (h.type !== "background_agent" || h.status !== "running" || !canAccess(h, access))
            continue;
        const label = chosenAgentName(h.id, h.name) ?? h.description;
        out.push(label ? `${h.id} (${label})` : h.id);
    }
    return out;
}
export function runningAgentFooterLane(core, access) {
    const named = [];
    const background = [];
    for (const h of core.handles.values()) {
        if (h.type !== "background_agent" || h.status !== "running" || !canAccess(h, access))
            continue;
        const chosen = chosenAgentName(h.id, h.name);
        if (chosen !== undefined) {
            named.push(chosen);
            continue;
        }
        background.push(h.description ? `${h.id} (${h.description})` : h.id);
    }
    return { named, background };
}
export function notFoundRunningAgentsTail(footer) {
    return ((footer.named.length > 0 ? `. Running named agents: ${footer.named.join(", ")}` : "") +
        (footer.background.length > 0 ? `. Running background agents: ${footer.background.join(", ")}` : ""));
}
export function agentHandleOutcomeOf(handle) {
    return agentOutcomeOf({ status: handle.status, ...(handle.errorCode !== undefined ? { errorCode: handle.errorCode } : {}), ...(handle.stoppedBy !== undefined ? { stoppedBy: handle.stoppedBy } : {}), ...(handle.outcome !== undefined ? { outcome: handle.outcome } : {}) });
}
function pollRefusalLines(outcome) {
    return memberRefusalLines(outcome).map((l) => `${l}\n`).join("");
}
export function agentOutcomeIsError(status) {
    return status === "failed";
}
export function buildAgentPollDetails(input) {
    const failed = input.status === "failed";
    return {
        task_id: input.taskId,
        type: "background_agent",
        status: input.status,
        retrieval_status: input.retrievalStatus,
        ...(input.seq !== undefined ? { seq: input.seq } : {}),
        ...(input.stoppedBy !== undefined ? { stoppedBy: input.stoppedBy } : {}),
        ...(failed && input.error !== undefined ? { error: delimitUntrusted("agent error", boundedRedactedSummary(input.error, 300)) } : {}),
        ...(failed && input.errorCode !== undefined ? { errorCode: input.errorCode } : {}),
        ...(failed && input.errorRetryable !== undefined ? { retryable: input.errorRetryable } : {}),
        ...(failed && input.errorRetryAfterMs !== undefined ? { retryAfterMs: input.errorRetryAfterMs } : {}),
        ...(input.resultIsPartial === true ? { partial_result: true } : {}),
        ...(input.completionId !== undefined ? { completionId: input.completionId } : {}),
        ...(input.outcome !== undefined ? { outcome: input.outcome } : {}),
        ...(input.agentState !== undefined ? { agentState: input.agentState } : {}),
    };
}
export function serveDurableAgentRowLane(row, agentState) {
    const stateLine = agentState !== undefined ? `state: ${agentStateSummary(agentState)}\n` : "";
    if (row.status === "parked") {
        return {
            content: delimitUntrusted(`task ${row.handle}`, `status: parked
${stateLine}${agentParkSentence(agentState?.park)}`),
            details: buildAgentPollDetails({
                taskId: row.handle,
                status: "parked",
                retrievalStatus: "success",
                ...(row.seq !== undefined ? { seq: row.seq } : {}),
                ...(agentState !== undefined ? { agentState } : {}),
            }),
        };
    }
    const kindClause = row.status === "failed" && row.errorKind !== undefined && row.errorRetryable !== undefined
        ? ` (error_kind: ${row.errorKind}, retryable: ${row.errorRetryable}${row.errorRetryAfterMs !== undefined ? `, retry_after_ms: ${row.errorRetryAfterMs}` : ""})`
        : "";
    const outcome = agentOutcomeOf(row);
    const body = `status: ${row.status}
${stateLine}${row.error ? `error: ${row.error}${kindClause}
` : ""}${pollRefusalLines(outcome)}${row.finalOutput ? `--- result${row.resultIsPartial ? " (partial — produced before the task was stopped)" : ""} ---
${clipTaskOutput(row.finalOutputFull ?? row.finalOutput)}` : "(no result text)"}`;
    return {
        content: delimitUntrusted(`task ${row.handle}`, body),
        details: buildAgentPollDetails({
            taskId: row.handle,
            status: row.status,
            retrievalStatus: "success",
            ...(row.seq !== undefined ? { seq: row.seq } : {}),
            ...(row.stoppedBy !== undefined ? { stoppedBy: row.stoppedBy } : {}),
            ...(row.error !== undefined ? { error: row.error } : {}),
            ...(row.errorCode !== undefined ? { errorCode: row.errorCode } : {}),
            ...(row.errorRetryable !== undefined ? { errorRetryable: row.errorRetryable } : {}),
            ...(row.errorRetryAfterMs !== undefined ? { errorRetryAfterMs: row.errorRetryAfterMs } : {}),
            ...(row.resultIsPartial === true ? { resultIsPartial: true } : {}),
            ...(row.completionId !== undefined ? { completionId: row.completionId } : {}),
            ...(outcome !== undefined ? { outcome } : {}),
            ...(agentState !== undefined ? { agentState } : {}),
        }),
        ...(agentOutcomeIsError(row.status) ? { isError: true } : {}),
    };
}
export async function pollBackgroundAgentLane(handle, deadline, signal, checkpointSeat) {
    while (handle.status === "running" && deadline !== undefined && Date.now() < deadline && !signal?.aborted) {
        await sleepPollStep(deadline, signal);
    }
    const running = handle.status === "running";
    const retrieval = running && deadline !== undefined && Date.now() >= deadline ? "timeout" : "success";
    if (handle.status === "parked") {
        const parkedSource = agentStateSourceOfHandle(handle);
        const parkedSeq = handle.cycleSeq;
        const agentState = await readAgentState(parkedSource, checkpointSeat, Date.now());
        return {
            content: delimitUntrusted(`task ${handle.id}`, `status: parked
state: ${agentStateSummary(agentState)}
${agentParkSentence(agentState.park)}`),
            details: buildAgentPollDetails({
                taskId: handle.id,
                status: "parked",
                retrievalStatus: "success",
                ...(parkedSeq !== undefined ? { seq: parkedSeq } : {}),
                agentState,
            }),
        };
    }
    const snap = {
        status: handle.status,
        result: handle.result,
        resultIsPartial: handle.resultIsPartial,
        error: handle.error,
        errorKind: handle.errorKind,
        errorCode: handle.errorCode,
        errorRetryable: handle.errorRetryable,
        errorRetryAfterMs: handle.errorRetryAfterMs,
        cycleSeq: handle.cycleSeq,
        stoppedBy: handle.stoppedBy,
        completionId: handle.completionId,
        outcome: agentHandleOutcomeOf(handle),
        updatedAt: handle.updatedAt,
    };
    const stateRead = readAgentState(agentStateSourceOfHandle({ id: handle.id, scope: handle.scope, status: snap.status, updatedAt: snap.updatedAt, ...(handle.approvalHolds !== undefined ? { approvalHolds: handle.approvalHolds } : {}) }), checkpointSeat, Date.now());
    const stateAsync = adoptHostPromise(stateRead);
    const agentState = stateAsync !== undefined ? await stateAsync : stateRead;
    const fullResult = snap.result ? (handle.resultFull ?? snap.result) : undefined;
    const resultText = fullResult !== undefined ? clipTaskOutput(fullResult, handle.outputFile) : undefined;
    const kindClause = snap.status === "failed" && snap.errorKind !== undefined && snap.errorRetryable !== undefined
        ? ` (error_kind: ${snap.errorKind}, retryable: ${snap.errorRetryable}${snap.errorRetryAfterMs !== undefined ? `, retry_after_ms: ${snap.errorRetryAfterMs}` : ""})`
        : "";
    const stateLine = `state: ${agentStateSummary(agentState)}\n`;
    const body = running
        ? `status: running
${stateLine}${agentState.approvalWait !== undefined ? "The agent is waiting on an approval (the state line names the call) — it goes on once that is answered, and you will be notified when it completes." : "The agent is still working — you will be notified when it completes."}`
        : `status: ${snap.status}
${stateLine}${snap.error ? `error: ${snap.error}${kindClause}
` : ""}${pollRefusalLines(snap.outcome)}${snap.result ? `--- result${snap.resultIsPartial ? " (partial — produced before the task was stopped)" : ""} ---
${resultText}` : "(no result text)"}`;
    return {
        content: delimitUntrusted(`task ${handle.id}`, body),
        details: buildAgentPollDetails({
            taskId: handle.id,
            status: snap.status,
            retrievalStatus: retrieval,
            ...(snap.cycleSeq !== undefined ? { seq: snap.cycleSeq } : {}),
            ...(snap.stoppedBy !== undefined ? { stoppedBy: snap.stoppedBy } : {}),
            ...(snap.error !== undefined ? { error: snap.error } : {}),
            ...(snap.errorCode !== undefined ? { errorCode: snap.errorCode } : {}),
            ...(snap.errorRetryable !== undefined ? { errorRetryable: snap.errorRetryable } : {}),
            ...(snap.errorRetryAfterMs !== undefined ? { errorRetryAfterMs: snap.errorRetryAfterMs } : {}),
            ...(snap.resultIsPartial === true ? { resultIsPartial: true } : {}),
            ...(snap.completionId !== undefined ? { completionId: snap.completionId } : {}),
            ...(snap.outcome !== undefined && !running ? { outcome: snap.outcome } : {}),
            agentState,
        }),
        ...(agentOutcomeIsError(snap.status) ? { isError: true } : {}),
    };
}
const PARKED_STOP_CAUSE = new Map([
    ["parent", "stopped by its parent"],
    ["manager", "stopped by the manager"],
    ["user", "stopped by a person"],
    ["system", "stopped by the engine"],
    ["timeout", "stopped when its time limit ran out"],
]);
export async function stopBackgroundAgentLane(core, handle, source = "parent") {
    const entryStatus = handle.status;
    if (handle.status === "parked") {
        const arbiter = handle.resolveParkedStop;
        if (arbiter === undefined) {
            return {
                content: `${handle.id} is parked on a pending approval — stop it by deciding (deny/expire) the pending approval in the durable approval inbox.`,
                details: { task_id: handle.id, type: "background_agent", status: "parked", retrieval_status: "not_ready", error: "parked_pending_approval", code: "parked_pending_approval" },
            };
        }
        let stopWon = false;
        let arbiterUnreachable = false;
        const parkedToken = handle.parkedCheckpointToken;
        try {
            stopWon = await arbiter();
        }
        catch {
            stopWon = false;
            arbiterUnreachable = true;
        }
        const landed = stopWon ? heldPark(core, handle.id, parkedToken) : undefined;
        if (landed !== undefined) {
            handle = landed;
            core.markStopSource(handle.id, source);
            handle.status = "killed";
            handle.stoppedBy = handle.stopSource ?? source;
            handle.outcome = backgroundAgentOutcomeOf("killed", undefined, handle.stoppedBy, undefined, runEndOf(handle));
            stampActivity(handle);
            mintCompletionId(handle);
            handle.parkedCheckpointToken = undefined;
            handle.resolveParkedStop = undefined;
            core.pokeBgQuiescence(handle.owner);
            const endingQueued = queuedSignal();
            announceParkedEnding(core, handle, { status: "killed", endingQueued: endingQueued.promise });
            {
                const lane = handle.durable;
                if (lane !== undefined) {
                    try {
                        const live = await lane.store.get(handle.id, lane.record.scope);
                        if (live && isBackgroundAgentLiveStatus(live.status)) {
                            const term = structuredClone(live);
                            term.status = "killed";
                            if (handle.stoppedBy !== undefined)
                                term.stoppedBy = handle.stoppedBy;
                            if (handle.outcome !== undefined)
                                term.outcome = handle.outcome;
                            term.settledAt = Date.now();
                            term.updatedAt = Date.now();
                            delete term.parkedCheckpointToken;
                            delete term.parkClaimId;
                            delete term.parkedAt;
                            await lane.store.update(handle.id, lane.record.scope, term, { rev: live.rev });
                        }
                    }
                    catch {
                    }
                }
            }
            durableAgentWriteLane(handle, {
                status: "killed",
                settledAt: Date.now(),
                ...(handle.stoppedBy !== undefined ? { stoppedBy: handle.stoppedBy } : {}),
                ...(handle.completionId !== undefined ? { completionId: handle.completionId } : {}),
                ...(handle.outcome !== undefined ? { outcome: handle.outcome } : {}),
            }, ["parkedCheckpointToken", "parkClaimId"]);
            endingQueued.done();
            return {
                content: `Terminated ${handle.id}: ${PARKED_STOP_CAUSE.get(handle.stoppedBy ?? source) ?? `stopped (${handle.stoppedBy})`} while it waited for an approval; that approval is withdrawn (its checkpoint expired).`,
                details: {
                    task_id: handle.id,
                    type: "background_agent",
                    status: "killed",
                    retrieval_status: "success",
                    ...(handle.cycleSeq !== undefined ? { seq: handle.cycleSeq } : {}),
                    ...(handle.stoppedBy !== undefined ? { stoppedBy: handle.stoppedBy } : {}),
                    ...(handle.completionId !== undefined ? { completionId: handle.completionId } : {}),
                },
            };
        }
        if (arbiterUnreachable) {
            return {
                content: `${handle.id} was not stopped: the approval arbitration store was unreachable — the row stays parked; retry the stop.`,
                details: { task_id: handle.id, type: "background_agent", status: handle.status, retrieval_status: "not_ready", error: "park_arbiter_unreachable", code: "park_arbiter_unreachable" },
                isError: true,
            };
        }
        return {
            content: `${handle.id} was not stopped: its pending approval was already consumed (a resume won the arbitration).`,
            details: { task_id: handle.id, type: "background_agent", status: handle.status, retrieval_status: "not_ready", error: "park_resume_won", code: "park_resume_won" },
            isError: true,
        };
    }
    if (handle.status === "running") {
        core.markStopSource(handle.id, source);
        handle.abort.abort();
        handle.status = "killed";
        handle.stoppedBy = handle.stopSource ?? source;
        handle.outcome = backgroundAgentOutcomeOf("killed", undefined, handle.stoppedBy, undefined, runEndOf(handle));
        stampActivity(handle);
        mintCompletionId(handle);
        handle.notify = undefined;
        handle.approvalHolds = undefined;
        for (const [, , qResolve] of handle.preAttachQueue ?? [])
            qResolve?.({ ok: false, reason: "not_running" });
        handle.preAttachQueue = undefined;
        for (const r of [...(handle.inFlightDirect ?? [])])
            r({ ok: false, reason: "not_running" });
        handle.inFlightDirect = undefined;
        core.pokeBgQuiescence(handle.owner);
        durableAgentWriteLane(handle, {
            status: "killed",
            settledAt: Date.now(),
            ...(handle.stoppedBy !== undefined ? { stoppedBy: handle.stoppedBy } : {}),
            ...(handle.completionId !== undefined ? { completionId: handle.completionId } : {}),
            outcome: handle.outcome,
        });
    }
    return {
        content: entryStatus === "running" ? `Terminated ${handle.id}.` : alreadyTerminalStopNote(handle.id, handle.status),
        details: {
            task_id: handle.id,
            type: "background_agent",
            status: handle.status,
            retrieval_status: "success",
            ...(handle.cycleSeq !== undefined ? { seq: handle.cycleSeq } : {}),
            ...(handle.status === "killed" && handle.stoppedBy !== undefined ? { stoppedBy: handle.stoppedBy } : {}),
            ...(handle.completionId !== undefined ? { completionId: handle.completionId } : {}),
        },
    };
}