Skip to content

armgit / qm

Public Imported from GitHub
0

Multiplayer agent harness for work.

53.3 KB
Raw Blame
import { isSessionTurn, sessionTurnOrigin } from "../core/turn-origin.ts";
import { TURN_STOPPED_KIND } from "../harness/replay.ts";
import { principalDestination } from "../reach/reach.ts";
import type { DeliveryStore } from "../delivery/delivery-store.ts";
import { deliveryCandidatesFor } from "../core/orchestrator/turn-helpers.ts";
import type { SessionMailbox, SessionMessage } from "./session-mailbox.ts";
import { conversationScope } from "../resolution/resolution-service.ts";
import { sleep } from "../util/async.ts";
import { pgTextSafe } from "../util/text.ts";
import { createMemoryAdvisoryLock, type AdvisoryLock } from "../persistence/advisory-lock.ts";
import { errMessage, swallowAs } from "../util/errors.ts";
import { randomUUID } from "node:crypto";
import { hashId } from "../util/crypto.ts";
import { canonicalJson } from "../../cli/src/canonical-json.ts";
import type { CoreSearch } from "../search/core-search.ts";
import type { ScopeId, Session, SessionEntry, SpawnMeta } from "../types.ts";
import type { OrchestratorInput } from "../core/orchestrator/types.ts";
import type { Run, RunStore } from "../runs/run-store.ts";
import type { RunSignalStore } from "../runs/run-signal-store.ts";
import type { SessionStore } from "./session-store.ts";
import { xmlAttrEscape, xmlEscape } from "../util/message-tag.ts";

const SUBAGENT_THREAD_PREFIX = "agent:main:subagent:";

export function isSubagentThreadRef(threadRef: string): boolean {
  return threadRef.startsWith(SUBAGENT_THREAD_PREFIX);
}

export function requiresDelegation(
  request: Pick<OrchestratorInput, "surface" | "surfaceTools" | "conversation" | "swarm">,
  enabled: boolean,
): boolean {
  return (
    enabled &&
    !request.swarm &&
    !isSubagentThreadRef(request.conversation.threadRef) &&
    (request.surface === "slack" || request.surfaceTools === true)
  );
}

export async function delegatedAuthorizationOrigin(
  request: OrchestratorInput,
  deps: { runs: Pick<RunStore, "get">; sessions: Pick<SessionStore, "getByThread" | "getForParticipant"> },
): Promise<OrchestratorInput["origin"] | undefined> {
  const seen = new Set<string>();
  let current = request;
  for (let depth = 0; depth < 32; depth++) {
    if (
      current.origin.kind === "automation" ||
      current.readOnly ||
      current.privateSessionMessage ||
      current.swarm ||
      !current.delegatingRunId ||
      !current.sessionSenderId ||
      seen.has(current.delegatingRunId)
    )
      return;
    seen.add(current.delegatingRunId);
    const source = await deps.runs.get(current.delegatingRunId);
    const sender = await deps.sessions.getForParticipant(current.sessionSenderId, request.actor.id);
    const child = await deps.sessions.getByThread(current.conversation.threadRef);
    if (!source || !sender || !child) return;
    const delegated =
      isSubagentThreadRef(child.threadRef) &&
      child.parentSessionId === sender.id &&
      source.sessionId === sender.threadRef;
    const continued =
      isSubagentThreadRef(sender.threadRef) &&
      sender.parentSessionId === child.id &&
      source.sessionId === child.threadRef;
    const started =
      current.surface === "web" &&
      !child.parentSessionId &&
      !isSubagentThreadRef(child.threadRef) &&
      source.sessionId === sender.threadRef;
    if (!delegated && !continued && !started) return;
    const parent = source.request;
    const audience = new Set([parent.actor.id, ...parent.conversation.audience.map((person) => person.id)]);
    if (
      parent.actor.id !== request.actor.id ||
      parent.actor.type !== "internal" ||
      parent.readOnly ||
      parent.privateSessionMessage ||
      parent.swarm ||
      sender.scopeId !== child.scopeId ||
      child.scopeId !== conversationScope(parent.conversation, parent.actor.id) ||
      child.scopeId !== conversationScope(current.conversation, current.actor.id) ||
      current.conversation.audience.some((person) => !audience.has(person.id))
    )
      return;
    const internal =
      parent.conversation.audience.every((person) => person.type === "internal") &&
      (parent.conversation.kind === "dm" ||
        (!!parent.conversation.publishMembers?.length &&
          parent.conversation.publishMembers.every((person) => person.type === "internal")));
    if (
      !isSessionTurn(parent) &&
      internal &&
      (parent.origin.kind === "human" ||
        (parent.origin.kind === "ambient" && parent.origin.live === true && parent.conversation.kind !== "dm"))
    )
      return parent.origin;
    current = parent;
  }
}

export const SUBAGENT_TREE_RUN_CAP = 50;
const SESSION_MESSAGE_DEPTH_CAP = 8;
const READ_DEFAULT_LIMIT = 30;
const SESSION_LIST_LIMIT = 50;
const WEB_ONLY = "sessions are a web UI feature and aren't available here.";
const READ_DEFAULT_MAX_CHARS = 4_000;
const READ_MAX_CHARS_CEILING = 20_000;
const MAIL_ERROR_CAP = 1_000;
const LAST_SAID_SCAN = 40;

interface SessionOpenInput {
  requestId?: string;
  task: string;
  name?: string;
  readOnly?: boolean;
  model?: string;
  harness?: string;
  thinkingLevel?: string;
  fastMode?: boolean;
}

type SessionOpenResult =
  { ok: true; sessionId: string; title: string; liveRunsRemaining: number } | { ok: false; message: string };

interface SessionWriteInput {
  target: string;
  peer?: boolean;
  text?: string;
  interrupt?: boolean;
  followup?: boolean;
  requestId?: string;
}

type SessionWriteResult =
  | {
      ok: true;
      sessionId: string;
      title: string;
      delivered: "steered" | "queued_turn" | "interrupted" | "queued_message";
    }
  | { ok: false; message: string };

interface SessionReadInput {
  target?: string;
  peer?: boolean;
  limit?: number;
  maxChars?: number;
}

interface SessionChildSummary {
  sessionId: string;
  title: string;
  status: "running" | "pending" | "idle";
  lastSaid?: string;
}

interface SessionStartInput {
  fork: boolean;
  text?: string;
  title?: string;
}

type SessionStartResult =
  { ok: true; sessionId: string; title: string; refused?: string } | { ok: false; message: string };

interface SessionSummary {
  sessionId: string;
  title: string;
  status: "running" | "pending" | "idle";
  current: boolean;
}

type SessionListResult = { ok: true; sessions: SessionSummary[] } | { ok: false; message: string };

type SessionReadResult =
  | { ok: true; mode: "children"; children: SessionChildSummary[] }
  | { ok: true; mode: "transcript"; sessionId: string; title: string; status: string; rendered: string }
  | { ok: false; message: string };

interface SessionSyscallBinding {
  session: Session;
  scopeId: ScopeId;
  orgScopeId?: ScopeId;
  liveTurn?: boolean;
  request: Pick<
    OrchestratorInput,
    | "cancel"
    | "surface"
    | "surfaceTools"
    | "deliveryCandidates"
    | "unattendedGrants"
    | "privateSessionMessage"
    | "sessionMessageDepth"
    | "swarm"
    | "runId"
    | "conversation"
    | "actor"
    | "deliveryTarget"
    | "timezone"
    | "readOnly"
    | "scopeVersion"
    | "sessionParticipantIds"
    | "slackSource"
    | "externalSlack"
  > & { origin?: OrchestratorInput["origin"] };
}

export interface SessionSyscalls {
  receive?(timeoutMs?: number): Promise<SessionMessage[]>;
  acknowledge?(ids: string[]): Promise<void>;
  open(input: SessionOpenInput): Promise<SessionOpenResult>;
  write(input: SessionWriteInput): Promise<SessionWriteResult>;
  read(input: SessionReadInput): Promise<SessionReadResult>;
  search?(input: { query: string; limit?: number }): ReturnType<CoreSearch["search"]>;
  list?(): Promise<SessionListResult>;
  start?(input: SessionStartInput): Promise<SessionStartResult>;
}

export interface SessionSyscallsFactory {
  rejectMessage?(sessionId: string, messageId: string): Promise<void>;
  forTurn(binding: SessionSyscallBinding): SessionSyscalls;
}

export interface SessionSyscallDeps {
  mailbox: SessionMailbox;
  enabled?: (actorId: string) => Promise<boolean>;
  sessions: Pick<
    SessionStore,
    | "get"
    | "getByThread"
    | "getOrCreateByThread"
    | "setParentSession"
    | "setSpawnMeta"
    | "childrenOf"
    | "addParticipant"
    | "updateTitle"
    | "getEntries"
    | "latestEntrySeq"
    | "visibleEntries"
    | "getForParticipant"
  >;
  runs: Pick<RunStore, "enqueue" | "inFlightForThread" | "latestForThread" | "getByDedupKey" | "get" | "withdraw">;
  signals: Pick<RunSignalStore, "send" | "pending">;
  maxAttempts: number;
  treeRunCap?: number;
  advisoryLock?: AdvisoryLock;
  prepareRequest?: (request: OrchestratorInput) => Promise<OrchestratorInput>;
  authorize?: (session: Session, actorId: string) => Promise<boolean>;
  validateRuntime?: (input: SessionOpenInput, scopeId: ScopeId) => Promise<void>;
  conversations?: {
    search?: CoreSearch["search"];
    list(actorId: string): Promise<Session[]>;
    start(
      actorId: string,
      input: {
        scopeId: ScopeId;
        forkOf?: string;
        text?: string;
        title?: string;
        origin?: OrchestratorInput["origin"];
        sessionSenderId?: string;
        delegatingRunId?: string;
      },
    ): Promise<{ session: Session; refused?: string } | { error: string }>;
  };
}

function autoTitle(task: string): string {
  const line = task.trim().split("\n")[0] ?? "";
  return line.length <= 60 ? line : `${line.slice(0, 57).trimEnd()}…`;
}

function entryText(entry: SessionEntry): string | undefined {
  const payload = entry.payload as { text?: unknown; tool?: unknown; aside?: unknown } | null;
  if (typeof payload?.text === "string" && payload.text.trim()) return payload.text;
  if (typeof payload?.tool === "string") return `[${payload.tool}]`;
  return undefined;
}

function lastAssistantText(entries: readonly SessionEntry[]): string | undefined {
  for (let i = entries.length - 1; i >= 0; i--) {
    const e = entries[i]!;
    const stop = e.type === "system" ? (e.payload as { kind?: unknown; reason?: unknown } | null) : null;
    if (stop?.kind === TURN_STOPPED_KIND) {
      const before = entries[i - 1];
      const partial = before?.type === "assistant" ? entryText(before) : "";
      return (
        partial || `[system] Subagent stopped${typeof stop.reason === "string" ? ` ${stop.reason}` : ""}. No reply.`
      );
    }
    if (e.type !== "assistant" && e.type !== "text") continue;
    const text = entryText(e);
    if (text) return text;
  }
  return undefined;
}

function snippet(text: string, max: number): string {
  const oneLine = text.replace(/\s+/g, " ").trim();
  return oneLine.length <= max ? oneLine : `${oneLine.slice(0, max - 1).trimEnd()}…`;
}

function renderSubagentTask(input: { title: string; parentTitle: string; task: string }): string {
  return [
    `<subagent-task session="${xmlAttrEscape(input.title)}">`,
    `You are the subagent session "${input.title}", spawned from the conversation "${input.parentTitle}". To message your parent use subagents send_message with target="parent"; use an exact sibling title or sessionId for peers, never filesystem paths. When your turn ends, your final message is delivered to your current parent session — make it the result, stated plainly. Your parent can change while you work; detached sessions have no automatic return. Use your judgment on details within the task's real limits; if an instruction seems to work against the goal, follow the goal and say so. Do not contact people, post to conversations, or change standing configuration unless the task says to. If you are blocked, end your turn saying exactly what you need.`,
    "",
    "<task>",
    input.task.trim(),
    "</task>",
    "</subagent-task>",
  ].join("\n");
}

function renderSubagentMessage(sender: { title: string; sessionId: string }, text: string): string {
  return [
    `<subagent-message from="${xmlAttrEscape(sender.title)}" fromSessionId="${sender.sessionId}">`,
    xmlEscape(text.trim()),
    "</subagent-message>",
  ].join("\n");
}

export type SubagentMailKind = "final_answer" | "no_reply" | "awaiting_input" | "errored" | "refused";

export function renderSubagentMail(input: {
  title: string;
  sessionId: string;
  kind: SubagentMailKind;
  body: string;
}): string {
  const why: Record<SubagentMailKind, string> = {
    final_answer: "finished a turn and sent back its result",
    no_reply: "finished a turn without sending a reply",
    awaiting_input: "is blocked waiting for a human approval",
    errored: "failed",
    refused: "was refused",
  };
  return [
    `<wake reason="subagent" name="${xmlAttrEscape(input.title)}" sessionId="${input.sessionId}" kind="${input.kind}" at="${new Date().toISOString()}">`,
    `  <why>Your subagent session "${xmlEscape(input.title)}" ${why[input.kind]}.</why>`,
    `  <content>${xmlEscape(input.body)}</content>`,
    `  <instructions>If the person who asked for this work is waiting on it, relay what matters in your own words. Otherwise act on it internally without acknowledging it. Never repeat a result already reported or send a no-action-needed update. The subagent's full transcript is kept with it; use the subagents tool to read it or give it another task.</instructions>`,
    "</wake>",
  ].join("\n");
}

function childRunRequest(child: Session, meta: SpawnMeta, text: string, displayText: string): OrchestratorInput {
  return {
    ...(meta.scopeVersion ? { scopeVersion: meta.scopeVersion } : {}),
    ...(meta.sessionParticipantIds ? { sessionParticipantIds: meta.sessionParticipantIds } : {}),
    surface: meta.surface,
    ...(meta.slackSource ? { slackSource: meta.slackSource } : {}),
    ...(meta.externalSlack ? { externalSlack: meta.externalSlack } : {}),
    ...(meta.deliveryTarget ? { deliveryTarget: meta.deliveryTarget } : {}),
    ...(meta.deliveryCandidates ? { deliveryCandidates: meta.deliveryCandidates } : {}),
    actor: meta.actor,
    conversation: { ...meta.conversation, threadRef: child.threadRef },
    origin: sessionTurnOrigin(meta.origin, text),
    ...(meta.unattendedGrants ? { unattendedGrants: [...meta.unattendedGrants] } : {}),
    text,
    displayText,
    envelopeWrapped: true,
    ...(meta.readOnly ? { readOnly: true } : {}),
    ...(meta.model ? { model: meta.model } : {}),
    ...(meta.harness ? { harness: meta.harness } : {}),
    ...(meta.thinkingLevel ? { thinkingLevel: meta.thinkingLevel } : {}),
    ...(meta.fastMode !== undefined ? { fastMode: meta.fastMode } : {}),
    ...(meta.timezone ? { timezone: meta.timezone } : {}),
  };
}

function assertAudienceCompatible(
  source: Pick<OrchestratorInput, "actor" | "conversation">,
  target: Pick<OrchestratorInput, "actor" | "conversation">,
): void {
  const allowed = new Set([source.actor.id, ...source.conversation.audience.map((person) => person.id)]);
  if ([target.actor.id, ...target.conversation.audience.map((person) => person.id)].some((id) => !allowed.has(id)))
    throw new Error("the target audience includes people outside the sender's authorized audience");
}

async function sessionAncestors(sessions: Pick<SessionStore, "get">, session: Session): Promise<Session[]> {
  const ancestors: Session[] = [];
  let current = session;
  const seen = new Set<string>();
  while (current.parentSessionId) {
    if (seen.has(current.id)) throw new Error("session parent cycle");
    seen.add(current.id);
    const parent = await sessions.get(current.parentSessionId);
    if (!parent) break;
    ancestors.push(parent);
    current = parent;
  }
  return ancestors;
}

export async function sessionTreeRoot(sessions: Pick<SessionStore, "get">, session: Session): Promise<Session> {
  return (await sessionAncestors(sessions, session)).at(-1) ?? session;
}

export async function stoppableAncestors(sessions: Pick<SessionStore, "get">, session: Session): Promise<Session[]> {
  return (await sessionAncestors(sessions, session)).filter((ancestor) => ancestor.scopeId === session.scopeId);
}

export async function sessionTreeWorking(
  sessions: Pick<SessionStore, "childrenOf">,
  runs: Pick<RunStore, "activeForThread">,
  root: Session,
): Promise<boolean> {
  for (const session of await treeSessions(sessions, root)) {
    if (session.scopeId === root.scopeId && (await runs.activeForThread(session.threadRef))) return true;
  }
  return false;
}

export async function workingSessionThreadRefs(
  sessions: Pick<SessionStore, "get" | "getByThread">,
  runs: Pick<RunStore, "activeSessionIds">,
  awaitingSessionIds: ReadonlySet<string>,
): Promise<Set<string>> {
  const active = await runs.activeSessionIds();
  const working = new Set(active);
  await Promise.all(
    active
      .map(async (threadRef) => {
        const session = await sessions.getByThread(threadRef);
        if (!session?.parentSessionId) return;
        for (const ancestor of await stoppableAncestors(sessions, session))
          if (!awaitingSessionIds.has(ancestor.id)) working.add(ancestor.threadRef);
      })
      .map((lookup) => lookup.catch(swallowAs("session list: working ancestors", undefined))),
  );
  return working;
}

async function treeSessions(sessions: Pick<SessionStore, "childrenOf">, root: Session): Promise<Session[]> {
  const all: Session[] = [root];
  const queue = [root];
  const seen = new Set([root.id]);
  while (queue.length) {
    const next = queue.shift()!;
    const children = await sessions.childrenOf(next.id);
    for (const child of children) {
      if (seen.has(child.id)) throw new Error("session parent cycle");
      seen.add(child.id);
      all.push(child);
      queue.push(child);
    }
  }
  return all;
}

async function sessionRunCancelled(
  runs: Pick<RunStore, "get">,
  signals: Pick<RunSignalStore, "pending">,
  runId: string | undefined,
): Promise<boolean> {
  const seen = new Set<string>();
  while (runId) {
    if (seen.has(runId)) return true;
    seen.add(runId);
    const run = await runs.get(runId);
    if (!run) return true;
    if (run.result?.stopped || (await signals.pending(runId)).some(({ signal }) => signal.kind === "abort"))
      return true;
    runId = run.request.delegatingRunId;
  }
  return false;
}

export async function stopSessionTree(
  deps: {
    sessions: Pick<SessionStore, "childrenOf">;
    runs: Pick<RunStore, "inFlightForThread" | "withdraw" | "get">;
    signals: Pick<RunSignalStore, "send">;
  },
  root: Session,
  reason?: string,
): Promise<boolean> {
  let stopped = false;
  for (const session of await treeSessions(deps.sessions, root)) {
    if (session.scopeId !== root.scopeId) continue;
    for (const run of await deps.runs.inFlightForThread(session.threadRef)) {
      await deps.signals.send(run.id, { kind: "abort", ...(reason ? { reason } : {}) });
      if (run.status === "pending") await deps.runs.withdraw(run.id);
      stopped = true;
    }
  }
  return stopped;
}

export async function sessionTreeRunCount(
  sessions: Pick<SessionStore, "childrenOf">,
  runs: Pick<RunStore, "inFlightForThread">,
  root: Session,
): Promise<number> {
  let live = 0;
  for (const session of await treeSessions(sessions, root)) {
    live += (await runs.inFlightForThread(session.threadRef)).length;
  }
  return live;
}

export function createSessionSyscalls(deps: SessionSyscallDeps): SessionSyscallsFactory {
  const lock = deps.advisoryLock ?? createMemoryAdvisoryLock();
  const cap = deps.treeRunCap ?? SUBAGENT_TREE_RUN_CAP;

  return {
    rejectMessage: (sessionId, messageId) => deps.mailbox.acknowledge(sessionId, [messageId]),
    forTurn(binding) {
      const callerTitle = binding.session.title?.trim() || "this conversation";

      async function resolveTarget(ref: string): Promise<Session | null> {
        const trimmed = ref.trim();
        if (!trimmed) return null;
        const current = await deps.sessions.get(binding.session.id);
        if (trimmed.toLowerCase() === "parent")
          return current?.parentSessionId ? deps.sessions.get(current.parentSessionId) : null;
        const byId = await deps.sessions.get(trimmed);
        if (byId) return byId;
        const byThread = await deps.sessions.getByThread(trimmed);
        if (byThread) return byThread;
        const children = await deps.sessions.childrenOf(binding.session.id);
        const siblings = current?.parentSessionId ? await deps.sessions.childrenOf(current.parentSessionId) : [];
        return [...children, ...siblings].find((c) => c.title?.trim().toLowerCase() === trimmed.toLowerCase()) ?? null;
      }

      function sidebarRefusal(): string | null {
        if (binding.request.surface !== "web") return WEB_ONLY;
        if (binding.request.swarm) return "swarm workers coordinate through the swarm API.";
        return null;
      }

      function peerRefusal(target: Session): string | null {
        const refusal = sidebarRefusal();
        if (refusal) return refusal;
        if (target.parentSessionId || isSubagentThreadRef(target.threadRef) || target.threadRef.startsWith("swarm:"))
          return `"${target.title?.trim() || target.id}" is a subagent, not a session — use the subagents tool for it.`;
        return null;
      }

      async function statusOf(session: Session): Promise<"running" | "pending" | "idle"> {
        const inFlight = await deps.runs.inFlightForThread(session.threadRef);
        if (inFlight.some((r) => r.status === "running")) return "running";
        return inFlight.length ? "pending" : "idle";
      }

      const visibleTitle = (target: Session) => target.title?.trim() || target.id;
      async function visibleHistory(target: Session): Promise<SessionEntry[]> {
        const audience = binding.request.conversation.audience.length
          ? binding.request.conversation.audience
          : [binding.request.actor];
        const views = await Promise.all(
          [...new Set([binding.request.actor.id, ...audience.map((p) => p.id)])].map((id) =>
            deps.sessions.visibleEntries(target.id, id),
          ),
        );
        const allowed = views.slice(1).map((view) => new Set(view.map((entry) => entry.seq)));
        return (views[0] ?? []).filter((entry) => allowed.every((seqs) => seqs.has(entry.seq)));
      }

      async function currentCaller(): Promise<OrchestratorInput> {
        binding.request.cancel?.throwIfAborted();
        if (await sessionRunCancelled(deps.runs, deps.signals, binding.request.runId))
          throw new Error("this delegated task was stopped");
        if (deps.enabled && !(await deps.enabled(binding.request.actor.id)))
          throw new Error("persistent subagents are not enabled for this user");
        const current = await deps.sessions.get(binding.session.id);
        if (
          !current ||
          current.scopeId !== binding.scopeId ||
          (deps.authorize && !(await deps.authorize(current, binding.request.actor.id)))
        )
          throw new Error("the sender no longer has access to this session");
        const request: OrchestratorInput = {
          ...binding.request,
          origin: binding.request.origin ?? { kind: "automation" },
          text: "",
        };
        return deps.prepareRequest ? deps.prepareRequest(request) : request;
      }

      return {
        async receive(timeoutMs = 0) {
          const deadline = Date.now() + Math.min(60_000, Math.max(0, timeoutMs));
          do {
            binding.request.cancel?.throwIfAborted();
            const caller = await currentCaller();
            const messages = await deps.mailbox.pending(binding.session.id);
            const visible: SessionMessage[] = [];
            for (const message of messages) {
              const sender = await deps.sessions.getForParticipant(message.senderId, caller.actor.id);
              if (!sender || sender.scopeId !== binding.scopeId) continue;
              if (deps.authorize && !(await deps.authorize(sender, caller.actor.id))) continue;
              try {
                assertAudienceCompatible(
                  { actor: message.actor, conversation: { ...caller.conversation, audience: message.audience } },
                  caller,
                );
                if (message.sourceEntrySeq !== undefined) {
                  const viewers = new Set([
                    caller.actor.id,
                    ...caller.conversation.audience.map((person) => person.id),
                  ]);
                  const readable = await Promise.all(
                    [...viewers].map(async (id) =>
                      (await deps.sessions.visibleEntries(sender.id, id)).some(
                        (entry) => entry.seq === message.sourceEntrySeq && entry.scopeLabel === binding.scopeId,
                      ),
                    ),
                  );
                  if (!readable.every(Boolean)) continue;
                }
                visible.push(message);
                if (visible.length >= 4) break;
              } catch {
                continue;
              }
            }
            if (visible.length || Date.now() >= deadline) return visible;
            await sleep(Math.min(250, deadline - Date.now()));
          } while (Date.now() <= deadline);
          return [];
        },
        async acknowledge(ids) {
          await deps.mailbox?.acknowledge(binding.session.id, ids);
        },
        async open(input) {
          return lock
            .withLock("session-tree-admission", async (): Promise<SessionOpenResult> => {
              if (binding.request.swarm || binding.session.threadRef.startsWith("swarm:"))
                throw new Error("swarm workers coordinate through the swarm API");
              const caller = await currentCaller();
              assertAudienceCompatible(binding.request, caller);
              if (binding.request.privateSessionMessage)
                throw new Error("private session messages cannot delegate new work");
              const task = input.task?.trim();
              if (!task)
                return { ok: false, message: "open requires a task: the full instruction the subagent works from." };
              await deps.validateRuntime?.(input, binding.scopeId);
              const threadRef = `${SUBAGENT_THREAD_PREFIX}${
                input.requestId
                  ? hashId([binding.session.id, binding.request.runId ?? "", input.requestId], 40)
                  : randomUUID()
              }`;
              const fingerprint = hashId([canonicalJson(input)], 40);
              const existing = await deps.sessions.getByThread(threadRef);
              const root = await sessionTreeRoot(
                deps.sessions,
                existing ?? (await deps.sessions.get(binding.session.id))!,
              );
              const live = await sessionTreeRunCount(deps.sessions, deps.runs, root);
              if (existing?.spawnMeta?.openFingerprint && existing.spawnMeta.openFingerprint !== fingerprint)
                throw new Error("the requestId was already used for different work");
              const initialRun = await deps.runs.getByDedupKey(`subagent-open:${threadRef}`);
              if (existing && initialRun)
                return {
                  ok: true,
                  sessionId: existing.id,
                  title: visibleTitle(existing),
                  liveRunsRemaining: Math.max(0, cap - live),
                };
              if (live >= cap) {
                return {
                  ok: false,
                  message: `all ${cap} subagent run slots for this conversation are in use — wait for one to finish, read their sessions, or interrupt one you no longer need.`,
                };
              }
              const child = await deps.sessions.getOrCreateByThread(
                threadRef,
                binding.session.type,
                binding.scopeId,
                binding.session.channelName,
                binding.session.surface ?? binding.request.surface,
              );
              const title = existing ? visibleTitle(existing) : input.name?.trim() || autoTitle(task);
              const meta: SpawnMeta = {
                origin: caller.origin,
                ...(binding.request.surfaceTools ? { surfaceTools: true } : {}),
                ...(binding.request.deliveryCandidates
                  ? { deliveryCandidates: binding.request.deliveryCandidates }
                  : {}),
                ...(caller.unattendedGrants ? { unattendedGrants: [...caller.unattendedGrants] } : {}),
                openFingerprint: fingerprint,
                ...(binding.request.slackSource ? { slackSource: binding.request.slackSource } : {}),
                ...(binding.request.externalSlack ? { externalSlack: binding.request.externalSlack } : {}),
                ...(binding.request.scopeVersion ? { scopeVersion: binding.request.scopeVersion } : {}),
                ...(binding.request.sessionParticipantIds
                  ? { sessionParticipantIds: binding.request.sessionParticipantIds }
                  : {}),
                surface: binding.request.surface ?? binding.session.surface ?? "web",
                conversation: binding.request.conversation,
                actor: binding.request.actor,
                ...(binding.request.deliveryTarget ? { deliveryTarget: binding.request.deliveryTarget } : {}),
                ...(binding.request.timezone ? { timezone: binding.request.timezone } : {}),
                ...(input.readOnly || binding.request.readOnly ? { readOnly: true } : {}),
                ...(input.model ? { model: input.model } : {}),
                ...(input.harness ? { harness: input.harness } : {}),
                ...(input.thinkingLevel ? { thinkingLevel: input.thinkingLevel } : {}),
                ...(input.fastMode !== undefined ? { fastMode: input.fastMode } : {}),
              };
              if (!existing?.spawnMeta) {
                await deps.sessions.setParentSession(child.id, binding.session.id);
                await deps.sessions.setSpawnMeta(child.id, meta);
              }
              for (const id of new Set([
                binding.request.actor.id,
                ...(binding.request.sessionParticipantIds ?? binding.request.conversation.audience.map((p) => p.id)),
              ]))
                await deps.sessions.addParticipant(child.id, id);
              await deps.sessions.updateTitle(child.id, title);
              const text = renderSubagentTask({ title, parentTitle: callerTitle, task });
              const request = {
                ...childRunRequest(child, meta, text, task),
                sessionSenderId: binding.session.id,
                ...(binding.request.runId ? { delegatingRunId: binding.request.runId } : {}),
              };
              await deps.runs.enqueue({
                sessionId: child.threadRef,
                dedupKey: `subagent-open:${threadRef}`,
                request: deps.prepareRequest ? await deps.prepareRequest(request) : request,
                maxAttempts: deps.maxAttempts,
              });
              return { ok: true, sessionId: child.id, title, liveRunsRemaining: Math.max(0, cap - live - 1) };
            })
            .catch((error): SessionOpenResult => ({ ok: false, message: errMessage(error) }));
        },

        async write(input) {
          return lock
            .withLock("session-tree-admission", async (): Promise<SessionWriteResult> => {
              if (binding.request.swarm || binding.session.threadRef.startsWith("swarm:"))
                throw new Error("swarm workers coordinate through the swarm API");
              const caller = await currentCaller();
              assertAudienceCompatible(binding.request, caller);
              const target = await resolveTarget(input.target);
              if (!target)
                return {
                  ok: false,
                  message: `no session matches "${input.target}" — use a sessionId from open, read, or list.`,
                };
              const refusal = input.peer ? peerRefusal(target) : null;
              if (refusal) return { ok: false, message: refusal };
              if (target.threadRef.startsWith("swarm:"))
                throw new Error("send messages to swarm workers through the swarm API");
              if (target.id === binding.session.id)
                return { ok: false, message: "a session cannot message itself — just continue your turn." };
              if (target.scopeId !== binding.scopeId)
                return {
                  ok: false,
                  message: "that session lives in a different context and cannot be reached from here.",
                };
              if (!(await deps.sessions.getForParticipant(target.id, binding.request.actor.id)))
                return { ok: false, message: "you are not a participant in that session." };
              if (deps.authorize && !(await deps.authorize(target, caller.actor.id)))
                return { ok: false, message: "you no longer have access to the target session" };
              const previous = await deps.runs.latestForThread(target.threadRef, { excludePrivateMessages: true });
              const meta = previous?.request ?? target.spawnMeta;
              if (!meta) return { ok: false, message: "the target has no verified runtime context yet" };
              const privateChild = isSubagentThreadRef(target.threadRef);
              const privateMessage = binding.request.privateSessionMessage === true || !privateChild;
              const messageDepth = (binding.request.sessionMessageDepth ?? 0) + 1;
              if (privateMessage && messageDepth > SESSION_MESSAGE_DEPTH_CAP)
                throw new Error("private session message reply limit reached");
              const prepared = childRunRequest(
                target,
                {
                  ...meta,
                  ...(target.parentSessionId
                    ? {
                        model: target.spawnMeta?.model,
                        harness: target.spawnMeta?.harness,
                        thinkingLevel: target.spawnMeta?.thinkingLevel,
                        fastMode: target.spawnMeta?.fastMode,
                      }
                    : {}),
                  surface: meta.surface ?? target.surface ?? "web",
                  actor: caller.actor,
                  origin: caller.origin,
                  unattendedGrants: caller.unattendedGrants,
                  readOnly: privateMessage || caller.readOnly || meta.readOnly || target.spawnMeta?.readOnly,
                },
                "",
                "",
              );
              const request = deps.prepareRequest ? await deps.prepareRequest(prepared) : prepared;
              assertAudienceCompatible(caller, request);
              const title = visibleTitle(target);
              if (input.interrupt) {
                if (privateMessage)
                  return { ok: false, message: "ordinary sessions accept private messages, not interrupts" };
                if (caller.readOnly && !target.spawnMeta?.readOnly)
                  return { ok: false, message: "a read-only session cannot interrupt a writable task" };
                const stopped = await stopSessionTree(deps, target, "by the parent agent");
                return stopped
                  ? { ok: true, sessionId: target.id, title, delivered: "interrupted" }
                  : { ok: false, message: `subagent "${title}" is not running — nothing to interrupt.` };
              }
              const text = input.text?.trim();
              if (!text)
                return {
                  ok: false,
                  message: input.followup
                    ? "followup_task requires `task`: the full instruction the subagent works from."
                    : "send_message requires `text` (or interrupt: true).",
                };
              if (text.length > 16_000) return { ok: false, message: "message exceeds 16000 characters" };
              const stamped = renderSubagentMessage({ title: callerTitle, sessionId: binding.session.id }, text);
              if (!input.followup) {
                const sourceEntrySeq = await deps.sessions.latestEntrySeq(binding.session.id);
                await deps.mailbox.send({
                  id: input.requestId
                    ? hashId([binding.session.id, binding.request.runId ?? "", input.requestId], 40)
                    : randomUUID(),
                  recipientId: target.id,
                  senderId: binding.session.id,
                  actor: caller.actor,
                  text: stamped,
                  ...(sourceEntrySeq >= 0 ? { sourceEntrySeq } : {}),
                  audience: caller.conversation.audience,
                  createdAt: Date.now(),
                });
                return { ok: true, sessionId: target.id, title, delivered: "queued_message" };
              }
              if (input.followup && binding.request.privateSessionMessage)
                return { ok: false, message: "private turns cannot assign follow-up work" };
              if (input.followup && !target.parentSessionId)
                return {
                  ok: false,
                  message: "follow-up tasks can only target an attached subagent; use a message for the root",
                };

              const dedupKey = input.requestId
                ? `subagent-followup:${hashId([binding.session.id, binding.request.runId ?? "", input.requestId], 40)}`
                : undefined;
              if (dedupKey) {
                const existing = await deps.runs.getByDedupKey(dedupKey);
                if (existing) {
                  if (
                    existing.sessionId !== target.threadRef ||
                    (existing.request.text !== stamped && existing.request.text !== pgTextSafe(stamped))
                  )
                    return { ok: false, message: "the requestId was already used for different work" };
                  return { ok: true, sessionId: target.id, title, delivered: "queued_turn" };
                }
              }
              request.origin = sessionTurnOrigin(request.origin, stamped);
              request.sessionSenderId = binding.session.id;
              request.delegatingRunId = binding.request.runId;
              if (
                (await sessionTreeRunCount(deps.sessions, deps.runs, await sessionTreeRoot(deps.sessions, target))) >=
                cap
              )
                return { ok: false, message: `all ${cap} session run slots are in use.` };
              await deps.runs.enqueue({
                sessionId: target.threadRef,
                dedupKey,
                request: {
                  ...request,
                  ...(privateMessage
                    ? { privateSessionMessage: true as const, sessionMessageDepth: messageDepth }
                    : {}),
                  text: stamped,
                  displayText: text,
                },
                maxAttempts: deps.maxAttempts,
              });
              return { ok: true, sessionId: target.id, title, delivered: "queued_turn" };
            })
            .catch((error): SessionWriteResult => ({ ok: false, message: errMessage(error) }));
        },

        async read(input) {
          await currentCaller();
          if (!input.target?.trim()) {
            const children = await deps.sessions.childrenOf(binding.session.id);
            const summaries: SessionChildSummary[] = [];
            for (const child of children) {
              if (
                child.scopeId !== binding.scopeId ||
                !(await deps.sessions.getForParticipant(child.id, binding.request.actor.id))
              )
                continue;
              const entries = (await visibleHistory(child)).slice(-LAST_SAID_SCAN);
              const said = lastAssistantText(entries);
              summaries.push({
                sessionId: child.id,
                title: visibleTitle(child),
                status: await statusOf(child),
                ...(said ? { lastSaid: snippet(said, 200) } : {}),
              });
            }
            return { ok: true, mode: "children", children: summaries };
          }
          const target = await resolveTarget(input.target);
          if (!target)
            return {
              ok: false,
              message: `no session matches "${input.target}" — use a sessionId from open, read, or list.`,
            };
          const refusal = input.peer ? peerRefusal(target) : null;
          if (refusal) return { ok: false, message: refusal };
          if (target.scopeId !== binding.scopeId)
            return { ok: false, message: "that session lives in a different context and cannot be read from here." };
          const limit = Math.min(Math.max(1, input.limit ?? READ_DEFAULT_LIMIT), 200);
          const maxChars = Math.min(Math.max(200, input.maxChars ?? READ_DEFAULT_MAX_CHARS), READ_MAX_CHARS_CEILING);
          if (!(await deps.sessions.getForParticipant(target.id, binding.request.actor.id)))
            return { ok: false, message: "you are not a participant in that session." };
          const entries = await visibleHistory(target);
          const lines: string[] = [];
          for (const entry of entries.slice(-limit)) {
            const text = entryText(entry);
            if (!text) continue;
            lines.push(`${entry.type}#${entry.seq} ${snippet(text, 300)}`);
          }
          let rendered = lines.join("\n");
          if (rendered.length > maxChars) rendered = `…${rendered.slice(rendered.length - maxChars)}`;
          return {
            ok: true,
            mode: "transcript",
            sessionId: target.id,
            title: visibleTitle(target),
            status: await statusOf(target),
            rendered: rendered || "[no readable entries yet]",
          };
        },
        async search(input) {
          const caller = await currentCaller();
          const refusal = sidebarRefusal();
          if (refusal) throw new Error(refusal);
          const search = deps.conversations?.search;
          if (!search) throw new Error("session search isn't available on this deployment.");
          const members = caller.conversation.publishMembers;
          if (caller.conversation.kind !== "dm" && !members?.length)
            throw new Error("search requires the complete principal set for a shared conversation");
          if (!input.query.trim()) throw new Error("search requires a query");
          return search({
            query: input.query,
            principals: [caller.actor, ...(members ?? caller.conversation.audience)],
            ...(input.limit !== undefined ? { limit: input.limit } : {}),
          });
        },
        async list() {
          await currentCaller();
          const conversations = deps.conversations;
          if (!conversations) return { ok: false, message: "sessions aren't available on this deployment." };
          const refusal = sidebarRefusal();
          if (refusal) return { ok: false, message: refusal };
          const audience = binding.request.conversation.audience.length
            ? binding.request.conversation.audience
            : [binding.request.actor];
          const candidates = (await conversations.list(binding.request.actor.id)).filter(
            (s) =>
              s.scopeId === binding.scopeId &&
              !s.parentSessionId &&
              !isSubagentThreadRef(s.threadRef) &&
              !s.threadRef.startsWith("swarm:") &&
              !s.archived,
          );
          const visible: Session[] = [];
          for (const session of candidates) {
            const seen = await Promise.all(
              audience.map((person) => deps.sessions.getForParticipant(session.id, person.id)),
            );
            if (seen.every(Boolean)) visible.push(session);
          }
          const recent = visible
            .sort((a, b) => (b.lastActivityAt ?? b.createdAt) - (a.lastActivityAt ?? a.createdAt))
            .slice(0, SESSION_LIST_LIMIT);
          const sessions: SessionSummary[] = [];
          for (const session of recent)
            sessions.push({
              sessionId: session.id,
              title: session.title?.trim() || "Untitled",
              status: await statusOf(session),
              current: session.id === binding.session.id,
            });
          return { ok: true, sessions };
        },
        async start(input) {
          try {
            await currentCaller();
          } catch (error) {
            return { ok: false, message: errMessage(error) };
          }
          const conversations = deps.conversations;
          if (!conversations) return { ok: false, message: "sessions aren't available on this deployment." };
          const refusal = sidebarRefusal();
          if (refusal) return { ok: false, message: refusal };
          const verb = input.fork ? "fork" : "new";
          if (binding.liveTurn !== true)
            return {
              ok: false,
              message: `${verb} needs a person attending this turn — not a cron, trigger, subagent, or other automation.`,
            };
          if (binding.request.readOnly) return { ok: false, message: "a read-only turn cannot create sessions." };
          const text = input.text?.trim();
          if (!input.fork && !text)
            return { ok: false, message: "new requires `text`: the new session's first message." };
          if (text && text.length > 16_000) return { ok: false, message: "text exceeds 16000 characters" };
          const out = await conversations
            .start(binding.request.actor.id, {
              scopeId: binding.scopeId,
              origin: binding.request.origin,
              sessionSenderId: binding.session.id,
              ...(binding.request.runId ? { delegatingRunId: binding.request.runId } : {}),
              ...(input.fork ? { forkOf: binding.session.id } : {}),
              ...(text ? { text } : {}),
              ...(input.title?.trim() ? { title: input.title.trim() } : {}),
            })
            .catch((error: unknown) => ({ error: errMessage(error) }));
          if ("error" in out) return { ok: false, message: out.error };
          return {
            ok: true,
            sessionId: out.session.id,
            title: out.session.title?.trim() || "Untitled",
            ...(out.refused ? { refused: out.refused } : {}),
          };
        },
      };
    },
  };
}

export interface SubagentMailDeps {
  signals?: Pick<RunSignalStore, "pending">;
  deliveries?: Pick<DeliveryStore, "enqueue">;
  delegationEnabled?: (actorId: string) => Promise<boolean>;
  mailbox: SessionMailbox;
  sessions: Pick<SessionStore, "get" | "getByThread" | "getEntries" | "latestEntrySeq" | "visibleEntries">;
  runs: Pick<RunStore, "enqueue" | "inFlightForThread" | "getByDedupKey" | "withdraw"> &
    Partial<Pick<RunStore, "latestForThread" | "get">>;
  maxAttempts: number;
  prepareRequest?: (request: OrchestratorInput) => Promise<OrchestratorInput>;
}

export async function deliverSubagentMail(deps: SubagentMailDeps, run: Run): Promise<boolean> {
  if (!isSubagentThreadRef(run.sessionId) || run.request.privateSessionMessage) return true;
  if (
    run.result?.stopped ||
    (deps.signals && deps.runs.get && (await sessionRunCancelled({ get: deps.runs.get }, deps.signals, run.id)))
  )
    return true;
  const child = await deps.sessions.getByThread(run.sessionId);
  if (!child?.parentSessionId || !child.spawnMeta) return true;
  const parent = await deps.sessions.get(child.parentSessionId);
  if (!parent) return true;
  if (parent.threadRef.startsWith("swarm:")) throw new Error("subagent returns cannot enter swarm workers");
  const latestParent = await deps.runs.latestForThread?.(parent.threadRef, { excludePrivateMessages: true });
  if (parent.scopeId !== child.scopeId) throw new Error("parent and child contexts no longer match");
  const initiatingRun = run.request.delegatingRunId ? await deps.runs.get?.(run.request.delegatingRunId) : undefined;
  if (run.request.delegatingRunId && !initiatingRun) throw new Error("the delegation's originating run is unavailable");
  const initiatingContext = initiatingRun?.request ?? child.spawnMeta;
  const currentContext = latestParent?.request ?? parent.spawnMeta;
  const originalParent = parent.threadRef === initiatingContext.conversation.threadRef;
  if (!originalParent && !currentContext) throw new Error("the current parent has no verified runtime context yet");
  const meta = originalParent
    ? initiatingContext
    : {
        ...currentContext!,
        actor: run.request.actor,
        origin: {
          kind: "automation" as const,
          ...(currentContext!.origin?.kind === "automation" && currentContext!.origin.destination
            ? { destination: currentContext!.origin.destination }
            : {}),
        },
        unattendedGrants: undefined,
      };
  const title = child.title?.trim() || child.id;
  const result = run.result;
  let kind: SubagentMailKind;
  let body: string;
  if (run.status === "failed" || result?.status === "failed") {
    kind = "errored";
    body = snippet(result?.reason ?? "the turn failed", MAIL_ERROR_CAP);
  } else if (result?.pendingApprovals?.length || result?.status === "pending_approval") {
    kind = "awaiting_input";
    body = snippet(
      result.pendingApprovals?.map((a) => a.command).join("; ") ?? "a command needs human approval",
      MAIL_ERROR_CAP,
    );
  } else if (result?.status === "refused") {
    kind = "refused";
    body = snippet(result.reason ?? "the turn was refused", MAIL_ERROR_CAP);
  } else if (result?.status === "ok" && (result.reply?.trim() || result.attachments?.length)) {
    kind = "final_answer";
    body = result.reply?.trim() || `Produced ${result.attachments!.map((file) => file.name).join(", ")}.`;
  } else {
    kind = "no_reply";
    body = "completed without sending a reply.";
  }
  const text = renderSubagentMail({
    title,
    sessionId: child.id,
    kind,
    body:
      body.length > 16_000 ? `${body.slice(0, 16_000)}\n[truncated; read the child session for the full result]` : body,
  });
  const inherited = { ...meta };
  delete (inherited as Partial<SpawnMeta>).openFingerprint;
  const request: OrchestratorInput = {
    ...inherited,
    sessionSenderId: child.id,
    conversation: {
      ...meta.conversation,
      ...(currentContext
        ? { audience: currentContext.conversation.audience, publishMembers: currentContext.conversation.publishMembers }
        : {}),
      threadRef: parent.threadRef,
    },
    origin: sessionTurnOrigin(meta.origin, text),
    text,
    displayText: `[subagent ${title}: ${kind.replace("_", " ")}]`,
    envelopeWrapped: true,
  };
  for (const key of [
    "runId",
    "attempt",
    "runLeaseToken",
    "runStartedAt",
    "finalAttempt",
    "background",
    "cancel",
    "shutdown",
    "modelAccount",
    "privateSessionMessage",
    "sessionMessageDepth",
    "delegatingRunId",
    "swarm",
    "approval",
    "proactiveOpener",
    "redeliveryKey",
    "attachments",
    "inboundNotes",
    "priorTurns",
    "overheard",
    "detectContext",
    "detectOpener",
    "conversationHeader",
  ] as const)
    delete request[key];
  if (!originalParent) request.addressed = false;
  const prepared = deps.prepareRequest ? await deps.prepareRequest(request) : request;
  assertAudienceCompatible(run.request, prepared);
  const outputSeq = run.result?.sourceAssistantEntrySeq ?? run.turnUserSeq ?? run.result?.sourceUserSeq;
  if (outputSeq !== undefined && outputSeq !== null) {
    for (const id of new Set([prepared.actor.id, ...prepared.conversation.audience.map((person) => person.id)])) {
      const visible = await deps.sessions.visibleEntries(child.id, id);
      if (!visible.some((entry) => entry.seq === outputSeq))
        throw new Error("the current parent audience cannot read this child result");
    }
  }
  if (result?.attachments?.length && deps.deliveries) {
    const delivery = deliveryCandidatesFor(meta.surface, meta.deliveryTarget, meta.deliveryCandidates, parent.scopeId);
    const candidate = delivery.candidates.find((item) => item.key === delivery.defaultKey);
    const destination =
      candidate ??
      (meta.origin?.kind === "automation" ? meta.origin.destination : undefined) ??
      principalDestination(meta.actor.id, meta.actor.id);
    await deps.deliveries.enqueue({
      destination,
      text: "",
      attachments: result.attachments,
      idempotencyKey: `subagent-files:${run.id}`,
      provenance: {
        trigger: "subagent",
        surface: meta.surface ?? "unknown",
        fireKey: `subagent-files:${run.id}`,
        sourceScopeId: child.scopeId,
        sourceThreadRef: child.threadRef,
        sourceSessionId: child.id,
      },
    });
  }
  await deps.mailbox.send({
    id: `subagent-mail-${run.id}`,
    recipientId: parent.id,
    senderId: child.id,
    actor: prepared.actor,
    text,
    ...(outputSeq !== undefined && outputSeq !== null ? { sourceEntrySeq: outputSeq } : {}),
    audience: prepared.conversation.audience,
    createdAt: Date.now(),
  });
  if (!isSubagentThreadRef(parent.threadRef)) {
    const surfaceTools =
      meta.surfaceTools === true ||
      requiresDelegation(prepared, (await deps.delegationEnabled?.(prepared.actor.id)) === true);
    const dedupKey = `subagent-return:${run.id}`;
    const existing = await deps.runs.getByDedupKey(dedupKey);
    const unread = (await deps.mailbox.pending(parent.id)).some((message) => message.id === `subagent-mail-${run.id}`);
    if (!unread) {
      if (existing?.status === "pending") await deps.runs.withdraw(existing.id, { unstartedOnly: true });
      return true;
    }
    if (existing && (existing.status === "done" || existing.status === "failed")) return true;
    if ((await deps.runs.inFlightForThread(parent.threadRef)).length) return false;
    const wake =
      "A delegated task finished. Check internal messages with subagents wait (timeoutMs: 0), then report any new result or blocker relevant to the user's request. If it was already handled or no message is available, end without posting.";
    await deps.runs.enqueue({
      sessionId: parent.threadRef,
      dedupKey,
      request: {
        ...prepared,
        ...(surfaceTools ? { surfaceTools } : {}),
        ...(originalParent && initiatingRun ? { delegatingRunId: initiatingRun.id } : {}),
        origin: sessionTurnOrigin(prepared.origin, wake),
        text: wake,
        displayText: "Delegated task completed",
      },
      maxAttempts: deps.maxAttempts,
    });
    return false;
  }
  return true;
}