sailfork / t3code-github / apps/ server/ src/ orchestration-v2/ ProjectionStore.test.ts

import { assert, it, vi } from "@effect/vitest";
import {
  EnvironmentId,
  EventId,
  CommandId,
  CheckpointId,
  ContextTransferId,
  CheckpointRef,
  CheckpointScopeId,
  MessageId,
  type ModelSelection,
  type OrchestrationV2ProviderThread,
  type OrchestrationV2TurnItem,
  type OrchestrationV2ThreadHistoryPage,
  type OrchestrationV2ThreadShellSnapshot,
  NodeId,
  PlanId,
  ProjectId,
  ProviderDriverKind,
  ProviderInstanceId,
  ProviderSessionId,
  ProviderThreadId,
  ProviderTurnId,
  RuntimeRequestId,
  RunAttemptId,
  RunId,
  ThreadId,
  TurnItemId,
  type ThreadPullRequestLink,
  isLiveSubagentShell,
  isListedThreadLineage,
} from "@t3tools/contracts";
import * as DateTime from "effect/DateTime";
import * as Effect from "effect/Effect";
import * as Deferred from "effect/Deferred";
import * as Fiber from "effect/Fiber";
import * as Layer from "effect/Layer";
import * as Option from "effect/Option";
import * as Schema from "effect/Schema";
import * as Tracer from "effect/Tracer";
import * as SqlClient from "effect/sql/SqlClient";
import * as Statement from "effect/sql/Statement";
import { projectThreadAwarenessV2 } from "@t3tools/shared/agentAwareness";

import * as SqlitePersistence from "../persistence/Sqlite.ts";
import { CodexProviderCapabilitiesV2 } from "./Adapters/CodexAdapterV2.ts";
import * as ProjectionStore from "./ProjectionStore.ts";
import {
  buildBoundedThreadProjection,
  decodeThreadHistoryCursor,
  encodeThreadHistoryCursor,
  selectHistoryPageFromCursor,
  THREAD_HISTORY_PAGE_POLICY,
  THREAD_HISTORY_TURN_SNAPSHOT_ROW_LIMIT,
} from "./threadHistoryPaging.ts";
import { limitRecoveryCommand } from "./UsageLimitRecoveryWorker.ts";

import { layerReaderDatabase, traceSqlRouting } from "../persistence/testkit/SqliteRouting.ts";

const layerTest = Layer.mergeAll(
  ProjectionStore.layer.pipe(Layer.provideMerge(SqlitePersistence.layerMemory)),
  SqlitePersistence.layerMemory,
);
const modelSelection = {
  instanceId: ProviderInstanceId.make("codex"),
  model: "gpt-5.4",
} satisfies ModelSelection;
const driver = ProviderDriverKind.make("codex");
const providerInstanceId = modelSelection.instanceId;
const encodeUnknownJsonString = Schema.encodeSync(Schema.fromJsonString(Schema.Unknown));

const traceSqlStatements = () => {
  const statements: Array<string> = [];
  const queryTransactions: Array<string | null> = [];
  const tracer = Tracer.make({
    span(options) {
      const span = new Tracer.NativeSpan(options);
      const end = span.end.bind(span);
      span.end = (endTime, exit) => {
        end(endTime, exit);
        const query = span.attributes.get("db.query.text");
        if (typeof query === "string") {
          statements.push(query);
          let parent = Option.getOrUndefined(span.parent);
          while (parent?._tag === "Span" && parent.name !== "sql.transaction") {
            parent = Option.getOrUndefined(parent.parent);
          }
          queryTransactions.push(parent?._tag === "Span" ? parent.spanId : null);
        }
      };
      return span;
    },
  });
  return { statements, tracer, queryTransactions };
};

/** The shell index flags every live shell and matches its shells on the fields subscriber trims read. */
const assertShellIndexCoversShells = Effect.fnUntraced(function* () {
  const store = yield* ProjectionStore.ProjectionStoreV2;
  const snapshot = yield* store.getShellSnapshot();
  const shells = snapshot.threads;
  const index = yield* store.listActiveShellIndex();
  const { tracer, statements, queryTransactions } = traceSqlStatements();
  const activity = yield* store.getAgentAwarenessSnapshot().pipe(Effect.withTracer(tracer));
  assert.equal(activity.snapshotSequence, snapshot.snapshotSequence);
  const awareness = (thread: Parameters<typeof projectThreadAwarenessV2>[0]["thread"]) =>
    projectThreadAwarenessV2({
      environmentId: EnvironmentId.make("test"),
      project: { title: "Test" },
      thread,
    });
  assert.deepEqual(
    activity.threads.map(awareness),
    shells.filter((thread) => isListedThreadLineage(thread.lineage)).map(awareness),
  );
  assert.deepEqual(
    activity.threads.map((thread) => [thread.id, thread.projectId, thread.latestRunCompletedAt]),
    shells
      .filter((thread) => isListedThreadLineage(thread.lineage))
      .map((thread) => [thread.id, thread.projectId, thread.latestRunCompletedAt]),
  );
  assert.isFalse(statements.some((statement) => /COUNT\(\*\)|GROUP BY/i.test(statement)));
  assert.isTrue(queryTransactions.every((transaction) => transaction !== null));
  assert.equal(new Set(queryTransactions).size, 1);
  for (const shell of [...shells, ...snapshot.archivedThreads]) {
    const targeted = yield* store
      .getAgentAwarenessSnapshot(shell.id)
      .pipe(Effect.withTracer(tracer));
    assert.deepEqual(targeted.threads.map(awareness), [awareness(shell)]);
    assert.deepEqual(targeted.threads[0]?.archivedAt, shell.archivedAt);
  }
  assert.isFalse(statements.some((statement) => /COUNT\(\*\)|GROUP BY/i.test(statement)));

  const iso = (value: DateTime.Utc | null | undefined) =>
    value == null ? null : DateTime.formatIso(value);
  const trimmed = (
    row: Pick<
      ProjectionStore.ActiveShellIndexEntry,
      "id" | "projectId" | "lineage" | "settledOverride" | "settledAt" | "pinnedAt" | "snoozedUntil"
    >,
  ) => ({
    id: row.id,
    projectId: row.projectId,
    lineage: row.lineage,
    settledOverride: row.settledOverride,
    settledAt: iso(row.settledAt),
    pinnedAt: iso(row.pinnedAt),
    snoozedUntil: iso(row.snoozedUntil),
  });
  assert.deepEqual(index.map(trimmed), shells.map(trimmed));
  shells.forEach((shell, position) => {
    if (isLiveSubagentShell(shell)) assert.isTrue(index[position]?.mayBeLive, shell.id);
  });
});

const addRolledBackRecoveryCandidate = Effect.fn("addRolledBackRecoveryCandidate")(function* (
  suffix: string,
) {
  const projectionStore = yield* ProjectionStore.ProjectionStoreV2;
  const now = yield* DateTime.now;
  const threadId = ThreadId.make(`thread:${suffix}:rolled-back`);
  const runId = RunId.make(`run:${suffix}:rolled-back`);
  const rootNodeId = NodeId.make(`node:${suffix}:rolled-back`);
  const run = {
    id: runId,
    threadId,
    ordinal: 1,
    providerInstanceId,
    modelSelection,
    providerThreadId: null,
    userMessageId: MessageId.make(`message:${suffix}:rolled-back`),
    rootNodeId,
    activeAttemptId: null,
    status: "running" as const,
    requestedAt: now,
    startedAt: now,
    completedAt: null,
    checkpointId: null,
    contextHandoffId: null,
  };

  yield* projectionStore.apply({
    id: EventId.make(`event:${suffix}:thread-created`),
    type: "thread.created",
    threadId,
    occurredAt: now,
    payload: {
      createdBy: "user",
      creationSource: "web",
      id: threadId,
      projectId: ProjectId.make(`project:${suffix}`),
      title: "Rolled-back recovery candidate",
      providerInstanceId,
      modelSelection,
      runtimeMode: "full-access",
      interactionMode: "default",
      branch: null,
      worktreePath: null,
      activeProviderThreadId: null,
      lineage: {
        parentThreadId: null,
        relationshipToParent: null,
        rootThreadId: threadId,
      },
      forkedFrom: null,
      createdAt: now,
      updatedAt: now,
      archivedAt: null,
      settledOverride: null,
      settledAt: null,
      lastVisitedAt: null,
      deletedAt: null,
    },
  });
  yield* projectionStore.apply({
    id: EventId.make(`event:${suffix}:run-created`),
    type: "run.created",
    threadId,
    runId,
    nodeId: rootNodeId,
    driver,
    providerInstanceId,
    occurredAt: now,
    payload: run,
  });
  yield* projectionStore.apply({
    id: EventId.make(`event:${suffix}:item-running`),
    type: "turn-item.updated",
    threadId,
    runId,
    nodeId: rootNodeId,
    driver,
    occurredAt: now,
    payload: {
      id: TurnItemId.make(`item:${suffix}:rolled-back`),
      threadId,
      runId,
      nodeId: rootNodeId,
      providerThreadId: null,
      providerTurnId: null,
      nativeItemRef: null,
      parentItemId: null,
      ordinal: 1,
      status: "running",
      title: "abandoned command",
      startedAt: now,
      completedAt: null,
      updatedAt: now,
      type: "command_execution",
      input: "sleep 60",
    },
  });
  yield* projectionStore.apply({
    id: EventId.make(`event:${suffix}:run-rolled-back`),
    type: "run.updated",
    threadId,
    runId,
    nodeId: rootNodeId,
    driver,
    occurredAt: now,
    payload: { ...run, status: "rolled_back", completedAt: now },
  });

  return threadId;
});

it.effect("routes full projection reads to readers and retains uncommitted writer state", () =>
  Effect.gen(function* () {
    const store = yield* ProjectionStore.ProjectionStoreV2;
    const sql = yield* SqlClient.SqlClient;
    const parentId = yield* addRolledBackRecoveryCandidate("reader-routing");
    const parent = yield* store.getThreadProjection(parentId);
    const threadId = ThreadId.make("thread:reader-routing:fork");
    yield* store.apply({
      id: EventId.make("event:reader-routing:fork"),
      type: "thread.created",
      threadId,
      occurredAt: parent.thread.createdAt,
      payload: {
        ...parent.thread,
        id: threadId,
        forkedFrom: { type: "run", threadId: parentId, runId: parent.runs[0]!.id },
      },
    });
    const messageId = MessageId.make("message:reader-routing");
    yield* store.apply({
      id: EventId.make("event:reader-routing:message"),
      type: "message.updated",
      threadId,
      occurredAt: parent.thread.createdAt,
      payload: {
        id: messageId,
        threadId,
        runId: null,
        nodeId: null,
        role: "user",
        text: "Committed message",
        attachments: [],
        streaming: false,
        createdBy: "user",
        creationSource: "web",
        createdAt: parent.thread.createdAt,
        updatedAt: parent.thread.createdAt,
      },
    });
    const { queries, trace } = traceSqlRouting();
    yield* store.getAgentAwarenessSnapshot().pipe(trace);
    const projection = yield* store.getThreadProjection(threadId).pipe(trace);
    assert.equal(projection.thread.id, threadId);
    assert.deepEqual(
      projection.visibleTurnItems
        .filter((row) => row.visibility === "inherited")
        .map((row) => row.sourceItemId),
      parent.turnItems.map((item) => item.id),
    );
    assert.equal(
      (yield* store.getMessagesById(threadId, [messageId]).pipe(trace)).get(messageId)?.text,
      "Committed message",
    );
    assert.isAbove(queries.length, 1);
    assert.deepEqual([...new Set(queries.map((row) => row.connection))], ["reader"]);
    const commitRequested = yield* Deferred.make<void>();
    const writer = yield* Deferred.await(commitRequested).pipe(
      Effect.andThen(
        sql.withTransaction(
          Effect.gen(function* () {
            yield* sql`
          UPDATE orchestration_v2_projection_threads
          SET payload_json = json_set(payload_json, '$.title', 'Committed title')
          WHERE thread_id = ${threadId}
        `;
            yield* sql`
          UPDATE orchestration_v2_projection_turn_items
          SET payload_json = json_set(payload_json, '$.title', 'Committed command')
          WHERE thread_id = ${parentId}
        `;
          }),
        ),
      ),
      Effect.forkChild,
    );
    let commitInjected = false;
    const duringCommit = yield* store.getThreadProjection(threadId).pipe(
      Effect.provideService(Statement.CurrentTransformer, (statement) => {
        const [query] = statement.compile();
        if (!commitInjected && query.includes("FROM orchestration_v2_projection_turn_items")) {
          commitInjected = true;
          return Deferred.succeed(commitRequested, undefined).pipe(
            Effect.andThen(Fiber.join(writer).pipe(Effect.orDie)),
            Effect.as(statement),
          );
        }
        return Effect.succeed(statement);
      }),
    );
    assert.isTrue(commitInjected);
    assert.deepEqual(duringCommit, projection);
    const afterCommit = yield* store.getThreadProjection(threadId);
    assert.equal(afterCommit.thread.title, "Committed title");
    assert.equal(
      afterCommit.visibleTurnItems.find((row) => row.visibility === "inherited")?.item.title,
      "Committed command",
    );
    yield* sql.withTransaction(
      Effect.gen(function* () {
        yield* sql`
        UPDATE orchestration_v2_projection_threads
        SET payload_json = json_set(payload_json, '$.title', 'Uncommitted title')
        WHERE thread_id = ${threadId}
      `;
        yield* sql`
          UPDATE orchestration_v2_projection_messages
          SET payload_json = json_set(payload_json, '$.text', 'Uncommitted message')
          WHERE message_id = ${messageId}
        `;
        assert.equal(
          (yield* store.getMessagesById(threadId, [messageId])).get(messageId)?.text,
          "Uncommitted message",
        );
        assert.equal(
          (yield* store.getAgentAwarenessSnapshot()).threads.find(
            (thread) => thread.id === threadId,
          )?.title,
          "Uncommitted title",
        );
        assert.equal(
          (yield* store.getThreadProjection(threadId)).thread.title,
          "Uncommitted title",
        );
      }),
    );
  }).pipe(Effect.provide(ProjectionStore.layer.pipe(Layer.provideMerge(layerReaderDatabase)))),
);

it.effect("record reads restrict provider histories to delivery identities", () =>
  Effect.gen(function* () {
    const store = yield* ProjectionStore.ProjectionStoreV2;
    const threadId = yield* addRolledBackRecoveryCandidate("delivery-identities");
    const sql = yield* SqlClient.SqlClient;
    const now = yield* DateTime.now;
    for (const suffix of ["selected", "history"]) {
      const id = ProviderThreadId.make(`provider-thread:${suffix}`);
      yield* store.apply({
        id: EventId.make(`event:delivery-identities:${suffix}`),
        type: "provider-thread.updated",
        threadId,
        occurredAt: now,
        payload: {
          id,
          appThreadId: threadId,
          driver,
          providerInstanceId,
          providerSessionId: null,
          ownerNodeId: null,
          nativeThreadRef: null,
          nativeConversationHeadRef: null,
          lastRunOrdinal: null,
          handoffIds: [],
          forkedFrom: null,
          status: "active",
          firstRunOrdinal: 1,
          createdAt: now,
          updatedAt: now,
        },
      });
    }
    for (const [suffix, attempt] of [
      ["selected", "current"],
      ["history", "old"],
      ["runless", null],
    ] as const) {
      yield* store.apply({
        id: EventId.make(`event:delivery-turn:${suffix}`),
        type: "provider-turn.updated",
        threadId,
        occurredAt: now,
        payload: {
          id: ProviderTurnId.make(`turn:${suffix}`),
          providerThreadId: ProviderThreadId.make("provider-thread:selected"),
          nodeId: NodeId.make("node:delivery"),
          runAttemptId: attempt === null ? null : RunAttemptId.make(attempt),
          nativeTurnRef: null,
          ordinal: suffix === "selected" ? 1 : suffix === "history" ? 2 : 3,
          status: "completed",
          startedAt: now,
          completedAt: now,
        },
      });
    }
    assert.deepEqual(
      (yield* store.getThreadRecords(threadId, ["providerTurns"], {
        providerTurnAttemptIds: [RunAttemptId.make("current")],
      })).providerTurns.map((turn) => turn.id),
      ["turn:selected"],
    );
    assert.deepEqual(
      (yield* store.getThreadRecords(threadId, ["providerTurns"], {
        providerTurnAttemptIds: [],
      })).providerTurns,
      [],
    );
    const selected = yield* store.getThreadRecords(threadId, ["providerThreads"], {
      providerThreadIds: [ProviderThreadId.make("provider-thread:selected")],
    });
    assert.deepEqual(
      selected.providerThreads.map((row) => row.id),
      ["provider-thread:selected"],
    );
    assert.deepEqual(
      (yield* store.getThreadRecords(threadId, ["providerThreads"], {
        providerThreadIds: [],
      })).providerThreads,
      [],
    );
    const ownerNodeId = NodeId.make("node:delivery-identities:owner");
    yield* store.apply({
      id: EventId.make("event:delivery-identities:owner-node"),
      type: "node.updated",
      threadId,
      runId: RunId.make("run:delivery-identities:rolled-back"),
      nodeId: ownerNodeId,
      driver,
      occurredAt: now,
      payload: {
        id: ownerNodeId,
        threadId,
        runId: RunId.make("run:delivery-identities:rolled-back"),
        rootNodeId: NodeId.make("node:delivery-identities:rolled-back"),
        parentNodeId: NodeId.make("node:delivery-identities:rolled-back"),
        kind: "subagent",
        status: "completed",
        countsForRun: false,
        providerThreadId: null,
        providerTurnId: null,
        nativeItemRef: null,
        runtimeRequestId: null,
        checkpointScopeId: null,
        startedAt: now,
        completedAt: now,
      },
    });
    yield* store.apply({
      id: EventId.make("event:delivery-identities:owned"),
      type: "provider-thread.updated",
      threadId,
      occurredAt: now,
      payload: {
        id: ProviderThreadId.make("provider-thread:owned"),
        appThreadId: ThreadId.make("thread:delivery-identities:child"),
        driver,
        providerInstanceId,
        providerSessionId: null,
        ownerNodeId,
        nativeThreadRef: null,
        nativeConversationHeadRef: null,
        lastRunOrdinal: null,
        handoffIds: [],
        forkedFrom: null,
        status: "idle",
        firstRunOrdinal: 2,
        createdAt: now,
        updatedAt: now,
      },
    });
    assert.deepEqual(
      (yield* store.getThreadRecords(threadId, ["providerThreads"])).providerThreads.map(
        (row) => row.id,
      ),
      ["provider-thread:history", "provider-thread:selected", "provider-thread:owned"],
    );
    // Unselected history must not be decoded, even if its payload is unreadable.
    yield* sql`UPDATE orchestration_v2_projection_provider_threads SET payload_json = '{}'
      WHERE provider_thread_id = 'provider-thread:history'`;
    assert.lengthOf(
      (yield* store.getThreadRecords(threadId, ["providerThreads"], {
        providerThreadIds: [ProviderThreadId.make("provider-thread:selected")],
      })).providerThreads,
      1,
    );
  }).pipe(Effect.provide(layerTest)),
);

it.effect("idle compaction filters keep the latest run, live work and the newest usage", () =>
  Effect.gen(function* () {
    const store = yield* ProjectionStore.ProjectionStoreV2;
    // Its rolled-back run 1 still has a running item, which background work must ignore.
    const threadId = yield* addRolledBackRecoveryCandidate("idle-compaction");
    const now = yield* DateTime.now;
    const minutes = (count: number) => DateTime.add(now, { minutes: count });
    const selected = ProviderThreadId.make("provider-thread:idle-selected");
    const nodeId = NodeId.make("node:idle-compaction");
    for (const [ordinal, status] of [
      [2, "completed"],
      [3, "completed"],
      [4, "failed"],
      // A removed or steered queued message.
      [5, "cancelled"],
    ] as const) {
      const runId = RunId.make(`run:idle-compaction:${ordinal}`);
      yield* store.apply({
        id: EventId.make(`event:idle-compaction:run-${ordinal}`),
        type: "run.created",
        threadId,
        runId,
        nodeId,
        driver,
        providerInstanceId,
        occurredAt: now,
        payload: {
          id: runId,
          threadId,
          ordinal,
          providerInstanceId,
          modelSelection,
          providerThreadId: selected,
          userMessageId: MessageId.make(`message:idle-compaction:${ordinal}`),
          rootNodeId: nodeId,
          activeAttemptId: null,
          status,
          requestedAt: now,
          startedAt: status === "cancelled" ? null : now,
          completedAt: now,
          checkpointId: null,
          contextHandoffId: null,
        },
      });
    }
    for (const [id, providerThreadId, ordinal, usageAt] of [
      ["turn:idle-old", selected, 1, minutes(1)],
      ["turn:idle-newest", selected, 2, minutes(3)],
      ["turn:idle-later-ordinal", selected, 3, minutes(2)],
      ["turn:idle-no-usage", selected, 4, null],
      [
        "turn:idle-other-thread",
        ProviderThreadId.make("provider-thread:idle-other"),
        1,
        minutes(9),
      ],
    ] as const) {
      yield* store.apply({
        id: EventId.make(`event:${id}`),
        type: "provider-turn.updated",
        threadId,
        nodeId,
        driver,
        occurredAt: now,
        payload: {
          id: ProviderTurnId.make(id),
          providerThreadId,
          nodeId,
          runAttemptId: null,
          nativeTurnRef: null,
          ordinal,
          status: "completed",
          startedAt: now,
          completedAt: now,
          ...(usageAt === null
            ? {}
            : { tokenUsage: { usedTokens: 150_000, updatedAt: DateTime.formatIso(usageAt) } }),
        },
      });
    }
    for (const status of ["pending", "resolved"] as const) {
      yield* store.apply({
        id: EventId.make(`event:idle-compaction:request-${status}`),
        type: "runtime-request.updated",
        threadId,
        nodeId,
        occurredAt: now,
        payload: {
          id: RuntimeRequestId.make(`request:idle-compaction:${status}`),
          nodeId,
          providerTurnId: null,
          nativeRequestRef: null,
          kind: "user_input",
          status,
          responseCapability: { type: "message" },
          createdAt: now,
          resolvedAt: status === "pending" ? null : now,
        },
      });
    }
    for (const status of ["pending", "consumed"] as const) {
      yield* store.apply({
        id: EventId.make(`event:idle-compaction:transfer-${status}`),
        type: "context-transfer.created",
        threadId,
        occurredAt: now,
        payload: {
          id: ContextTransferId.make(`transfer:idle-compaction:${status}`),
          type: "merge_back",
          sourceThreadId: ThreadId.make("thread:idle-compaction:side"),
          targetThreadId: threadId,
          sourcePoint: { threadId: ThreadId.make("thread:idle-compaction:side") },
          basePoint: null,
          sourceProviderInstanceId: providerInstanceId,
          targetProviderInstanceId: providerInstanceId,
          targetRunId: null,
          status,
          resolution: null,
          createdBy: "user",
          error: null,
          createdAt: now,
          updatedAt: now,
          consumedAt: status === "pending" ? null : now,
        },
      });
    }

    const records = yield* store.getThreadRecords(
      threadId,
      ["runs", "providerTurns", "runtimeRequests", "contextTransfers"],
      {
        runStatuses: ["preparing", "starting", "running", "rolled_back"],
        includeLatestRun: true,
        providerTurnThreadIds: [selected],
        latestProviderTurnUsageOnly: true,
        runtimeRequestStatuses: ["pending"],
        contextTransferStatuses: ["pending"],
      },
    );
    // The failed latest run stays, so a completed earlier run is not mistaken for the latest,
    // and a cancelled queued run never counts as the latest.
    assert.deepEqual(
      records.runs.map((run) => run.id),
      ["run:idle-compaction:rolled-back", "run:idle-compaction:4"],
    );
    assert.deepEqual(
      records.providerTurns.map((turn) => turn.id),
      ["turn:idle-newest"],
    );
    assert.deepEqual(
      records.runtimeRequests.map((request) => request.id),
      ["request:idle-compaction:pending"],
    );
    assert.deepEqual(
      records.contextTransfers.map((transfer) => transfer.id),
      ["transfer:idle-compaction:pending"],
    );
    const empty = yield* store.getThreadRecords(threadId, ["runs", "providerTurns"], {
      runStatuses: [],
      providerTurnThreadIds: [],
      latestProviderTurnUsageOnly: true,
    });
    assert.deepEqual([empty.runs, empty.providerTurns], [[], []]);
  }).pipe(Effect.provide(layerTest)),
);

it.effect("record reads filter notification sources without dropping matching message bodies", () =>
  Effect.gen(function* () {
    const store = yield* ProjectionStore.ProjectionStoreV2;
    const threadId = yield* addRolledBackRecoveryCandidate("notification-filter");
    const now = yield* DateTime.now;
    for (const [id, role, kind] of [
      ["ordinary", "user", null],
      ["monitor", "user", "monitor"],
      ["system", "user", "system"],
      ["assistant-monitor", "assistant", "monitor"],
    ] as const) {
      yield* store.apply({
        id: EventId.make(`notification-filter:${id}`),
        type: "message.updated",
        threadId,
        occurredAt: now,
        payload: {
          id: MessageId.make(id),
          threadId,
          runId: null,
          nodeId: null,
          createdBy: "agent",
          creationSource: "server",
          role,
          text: `Complete body for ${id}`,
          attachments: [],
          streaming: false,
          createdAt: now,
          updatedAt: now,
          ...(kind === null
            ? {}
            : {
                notification: {
                  source: { kind },
                  outcome: "updated",
                  summary: "Background notification",
                },
              }),
        },
      });
    }
    const matching = yield* store.getThreadRecords(threadId, ["messages"], {
      messageRoles: ["user"],
      monitorNotificationsOnly: true,
    });
    assert.deepEqual(
      matching.messages.map((message) => [message.id, message.text]),
      [["monitor", "Complete body for monitor"]],
    );
    assert.lengthOf((yield* store.getThreadRecords(threadId, ["messages"])).messages, 4);
    assert.lengthOf(
      (yield* store.getThreadRecords(threadId, ["messages"], {
        monitorNotificationsOnly: true,
      })).messages,
      2,
    );
    assert.lengthOf(
      (yield* store.getThreadRecords(threadId, ["messages"], {
        monitorNotificationsOnly: false,
      })).messages,
      4,
    );
  }).pipe(Effect.provide(layerTest)),
);

it.effect("SQL queue readiness skips held runs without bypassing active work", () =>
  Effect.gen(function* () {
    const store = yield* ProjectionStore.ProjectionStoreV2;
    const threadId = yield* addRolledBackRecoveryCandidate("held-queue");
    const { runs } = yield* store.getThreadProjection(threadId);
    const run = runs[0]!;
    const now = yield* DateTime.now;
    const held = { ...run, status: "queued" as const, queueHeld: true };
    yield* store.apply({
      id: EventId.make("held-queue:held"),
      type: "run.updated",
      threadId,
      occurredAt: now,
      payload: held,
    });
    assert.isFalse(yield* store.canStartQueuedRun(threadId));

    const wake = { ...run, id: RunId.make("held-queue:wake"), ordinal: 2 };
    for (const status of ["running", "queued", "completed"] as const) {
      yield* store.apply({
        id: EventId.make(`held-queue:wake:${status}`),
        type: "run.updated",
        threadId,
        occurredAt: now,
        payload: { ...wake, status },
      });
      assert.equal(yield* store.canStartQueuedRun(threadId), status === "queued");
    }
    assert.deepEqual((yield* store.getThreadProjection(threadId)).runs[0], held);
  }).pipe(Effect.provide(layerTest)),
);

it.effect("SQL shells flag only an active turn blocked solely on T3 thread waits", () =>
  Effect.gen(function* () {
    const store = yield* ProjectionStore.ProjectionStoreV2;
    const threadId = yield* addRolledBackRecoveryCandidate("thread-wait");
    const initial = yield* store.getThreadProjection(threadId);
    const at = initial.updatedAt;
    const previousCommand = initial.turnItems.find((item) => item.type === "command_execution")!;
    const itemBase = {
      ...previousCommand,
      providerThreadId: previousCommand.providerThreadId ?? null,
      providerTurnId: previousCommand.providerTurnId ?? null,
      nativeItemRef: previousCommand.nativeItemRef ?? null,
      parentItemId: previousCommand.parentItemId ?? null,
    };
    yield* store.apply({
      id: EventId.make("event:thread-wait:previous-completed"),
      type: "run.updated",
      threadId,
      occurredAt: at,
      payload: { ...initial.runs[0]!, status: "completed", completedAt: at },
    });
    const run = {
      ...initial.runs[0]!,
      id: RunId.make("run:thread-wait:active"),
      ordinal: 2,
      status: "running" as const,
      completedAt: null,
    };
    yield* store.apply({
      id: EventId.make("event:thread-wait:active"),
      type: "run.created",
      threadId,
      occurredAt: at,
      payload: run,
    });
    let sequence = 0;
    const updateItem = (payload: OrchestrationV2TurnItem) =>
      store.apply({
        id: EventId.make(`event:thread-wait:item:${sequence++}`),
        type: "turn-item.updated",
        threadId,
        runId: payload.runId ?? undefined,
        driver,
        occurredAt: at,
        payload,
      });
    const assertBlocked = Effect.fnUntraced(function* (expected: boolean) {
      const listed = (yield* store.getShellSnapshot()).threads.find((row) => row.id === threadId);
      for (const shell of [yield* store.getThreadShell(threadId), listed]) {
        assert.equal(shell?.activeRunId, run.id);
        assert.equal(shell?.status, "running");
        assert.equal(shell?.blockedOnThreadWait ?? false, expected);
      }
      yield* assertShellIndexCoversShells();
    });
    const wait = {
      ...itemBase,
      id: TurnItemId.make("item:thread-wait:active"),
      runId: run.id,
      type: "dynamic_tool" as const,
      toolName: "mcp__t3_code__t3_thread_wait",
      input: { threadId: "child-thread" },
      status: "running" as const,
    };

    yield* updateItem({
      ...wait,
      id: TurnItemId.make("item:thread-wait:stale"),
      runId: initial.runs[0]!.id,
    });
    yield* assertBlocked(false);
    const foreign = {
      ...wait,
      id: TurnItemId.make("item:thread-wait:foreign"),
      toolName: "weather.t3_thread_wait",
    };
    yield* updateItem(foreign);
    yield* assertBlocked(false);
    yield* updateItem({ ...foreign, status: "completed", completedAt: at });
    yield* updateItem({ ...wait, status: "pending", startedAt: null });
    yield* assertBlocked(false);
    yield* updateItem(wait);
    yield* assertBlocked(true);

    yield* updateItem({
      ...wait,
      id: TurnItemId.make("item:thread-wait:plan"),
      type: "todo_list",
      planId: PlanId.make("plan:thread-wait"),
      steps: [{ id: "review", text: "Review the child result", status: "running" }],
    });
    yield* assertBlocked(true);
    yield* updateItem({
      ...wait,
      id: TurnItemId.make("item:thread-wait:proposed-plan"),
      type: "proposed_plan",
      planId: PlanId.make("plan:thread-wait:proposed"),
      markdown: "Review the child result",
      streaming: true,
    });
    yield* updateItem({
      ...wait,
      id: TurnItemId.make("item:thread-wait:named-message"),
      nativeItemRef: { driver, nativeId: "named-message", strength: "weak" },
      type: "assistant_message",
      messageId: MessageId.make("message:thread-wait:named"),
      text: "Waiting for the child result",
      streaming: true,
    });
    yield* updateItem({
      ...wait,
      id: TurnItemId.make("item:thread-wait:named-thought"),
      nativeItemRef: { driver, nativeId: "named-thought", strength: "weak" },
      type: "reasoning",
      text: "I need the child result",
      streaming: true,
    });
    yield* assertBlocked(true);

    const command = {
      ...itemBase,
      id: TurnItemId.make("item:thread-wait:concurrent"),
      runId: run.id,
    };
    yield* updateItem(command);
    yield* assertBlocked(false);
    yield* updateItem({ ...wait, status: "waiting" });
    yield* assertBlocked(false);
    yield* updateItem({ ...command, status: "completed", completedAt: at });
    yield* assertBlocked(true);
    const fileChange = {
      ...command,
      id: TurnItemId.make("item:thread-wait:concurrent-edit"),
      type: "file_change" as const,
      fileName: "example.ts",
    };
    yield* updateItem(fileChange);
    yield* assertBlocked(false);
    yield* updateItem({ ...fileChange, status: "completed", completedAt: at });
    yield* assertBlocked(true);
    const compaction = {
      ...command,
      id: TurnItemId.make("item:thread-wait:compaction"),
      type: "compaction" as const,
      driver,
    };
    yield* updateItem(compaction);
    yield* assertBlocked(false);
    yield* updateItem({ ...compaction, status: "completed", completedAt: at });
    yield* assertBlocked(true);
    yield* updateItem({ ...wait, status: "completed", completedAt: at });
    yield* assertBlocked(false);

    const delegation = {
      ...wait,
      id: TurnItemId.make("item:thread-wait:delegation"),
      toolName: "t3-code-thread_opencode2-adapter_delegate_task",
      input: { task: "Review", mode: "async" },
    };
    yield* updateItem(delegation);
    yield* assertBlocked(false);
    yield* updateItem({ ...delegation, status: "completed", completedAt: at });
    const blockingDelegation = {
      ...delegation,
      id: TurnItemId.make("item:thread-wait:blocking-delegation"),
      input: { task: "Review", mode: "wait" },
    };
    yield* updateItem({
      ...command,
      id: TurnItemId.make("item:thread-wait:child"),
      type: "subagent",
      subagentId: NodeId.make("node:thread-wait:child"),
      origin: "app_owned",
      driver,
      providerInstanceId,
      childThreadId: ThreadId.make("child-thread"),
      prompt: "Review",
      result: null,
    });
    yield* updateItem(blockingDelegation);
    yield* assertBlocked(true);
    yield* updateItem({ ...blockingDelegation, status: "completed", completedAt: at });
    yield* assertBlocked(false);
  }).pipe(Effect.provide(layerTest)),
);

it.effect("SQL shells use the latest subagent owner for duplicate provider-thread ids", () =>
  Effect.gen(function* () {
    const store = yield* ProjectionStore.ProjectionStoreV2;
    const threadId = yield* addRolledBackRecoveryCandidate("duplicate-provider-thread-owner");
    const initial = yield* store.getThreadProjection(threadId);
    const at = initial.updatedAt;
    const otherThreadId = yield* addRolledBackRecoveryCandidate(
      "duplicate-provider-thread-owner:other",
    );
    const other = yield* store.getThreadProjection(otherThreadId);
    const otherRun = other.runs[0]!;
    const otherInstanceId = ProviderInstanceId.make("codex-other");
    yield* store.apply({
      id: EventId.make("event:duplicate-provider-thread-owner:other-completed"),
      type: "run.updated",
      threadId: otherThreadId,
      occurredAt: at,
      payload: { ...otherRun, status: "completed", completedAt: at },
    });
    yield* store.apply({
      id: EventId.make("event:duplicate-provider-thread-owner:other-switched"),
      type: "thread.provider-switched",
      threadId: otherThreadId,
      occurredAt: at,
      payload: {
        ...other.thread,
        providerInstanceId: otherInstanceId,
        modelSelection: { ...modelSelection, instanceId: otherInstanceId },
      },
    });
    const providerThreadId = ProviderThreadId.make("provider-thread:duplicate-owner");
    const nativeTaskId = "native-task:duplicate-provider-thread-owner";
    for (const [appThreadId, owner] of [
      [otherThreadId, otherInstanceId],
      [threadId, providerInstanceId],
    ] as const) {
      yield* store.apply({
        id: EventId.make(`event:duplicate-provider-thread-owner:${owner}`),
        type: "provider-thread.updated",
        threadId: appThreadId,
        occurredAt: at,
        payload: {
          id: providerThreadId,
          driver,
          providerInstanceId: owner,
          providerSessionId: null,
          appThreadId,
          ownerNodeId: null,
          nativeThreadRef: null,
          nativeConversationHeadRef: null,
          status: "idle",
          firstRunOrdinal: 1,
          lastRunOrdinal: 1,
          handoffIds: [],
          forkedFrom: null,
          createdAt: at,
          updatedAt: at,
          pendingBackgroundTasks:
            owner === otherInstanceId
              ? [{ taskId: nativeTaskId, kind: "subagent", description: "Review" }]
              : [],
        },
      });
    }
    yield* store.apply({
      id: EventId.make("event:duplicate-provider-thread-owner:completed"),
      type: "run.updated",
      threadId,
      occurredAt: at,
      payload: { ...initial.runs[0]!, status: "completed", completedAt: at },
    });
    const item = {
      ...initial.turnItems[0]!,
      id: TurnItemId.make("item:duplicate-provider-thread-owner"),
      providerThreadId,
      nativeItemRef: { driver, nativeId: nativeTaskId, strength: "strong" as const },
      type: "subagent" as const,
      status: "running" as const,
      subagentId: NodeId.make("node:duplicate-provider-thread-owner"),
      origin: "provider_native" as const,
      driver,
      providerInstanceId,
      childThreadId: null,
      title: "Review",
      prompt: "Review",
      result: null,
    };
    yield* store.apply({
      id: EventId.make("event:duplicate-provider-thread-owner:item"),
      type: "turn-item.updated",
      threadId,
      occurredAt: at,
      payload: item,
    });
    yield* store.apply({
      id: EventId.make("event:duplicate-provider-thread-owner:other-item"),
      type: "turn-item.updated",
      threadId: otherThreadId,
      occurredAt: at,
      payload: {
        ...item,
        id: TurnItemId.make("item:duplicate-provider-thread-owner:other"),
        threadId: otherThreadId,
        runId: otherRun.id,
        nodeId: otherRun.rootNodeId,
        subagentId: NodeId.make("node:duplicate-provider-thread-owner:other"),
      },
    });
    yield* store.apply({
      id: EventId.make("event:duplicate-provider-thread-owner:switched"),
      type: "thread.provider-switched",
      threadId,
      occurredAt: at,
      payload: {
        ...initial.thread,
        providerInstanceId: otherInstanceId,
        modelSelection: { ...modelSelection, instanceId: otherInstanceId },
      },
    });
    assert.deepEqual((yield* store.getThreadProjection(otherThreadId)).providerThreads, []);
    const assertOwners = Effect.fnUntraced(function* (
      targetThreadId: ThreadId,
      expected: ReadonlyArray<unknown>,
    ) {
      const listed = (yield* store.getShellSnapshot()).threads.find(
        (thread) => thread.id === targetThreadId,
      );
      yield* assertShellIndexCoversShells();
      for (const shell of [yield* store.getThreadShell(targetThreadId), listed]) {
        assert.equal(shell?.providerInstanceId, otherInstanceId);
        assert.equal(shell?.activeRunId, null);
        assert.deepEqual(
          shell?.pendingBackgroundTasks?.filter((task) => task.kind === "subagent"),
          expected,
        );
      }
    });
    const owned = [
      { taskId: nativeTaskId, description: item.title, kind: "subagent", providerInstanceId },
    ];
    yield* assertOwners(threadId, owned);
    yield* assertOwners(otherThreadId, owned);
    yield* store.apply({
      id: EventId.make("event:duplicate-provider-thread-owner:stopped"),
      type: "turn-item.updated",
      threadId,
      occurredAt: at,
      payload: { ...item, status: "cancelled", completedAt: at },
    });
    yield* assertOwners(threadId, []);
    const ownerSnapshot = (yield* store.getThreadProjection(threadId)).providerThreads[0]!;
    // Rollback writes its provider snapshot before the rolled-back run events.
    yield* store.apply({
      id: EventId.make("event:duplicate-provider-thread-owner:rollback-provider-thread"),
      type: "provider-thread.updated",
      threadId: otherThreadId,
      occurredAt: at,
      payload: ownerSnapshot,
    });
    yield* store.apply({
      id: EventId.make("event:duplicate-provider-thread-owner:other-rolled-back"),
      type: "run.updated",
      threadId: otherThreadId,
      occurredAt: at,
      payload: { ...otherRun, status: "rolled_back", completedAt: at },
    });
    const rolledBack = yield* store.getThreadProjection(otherThreadId);
    assert.deepEqual(rolledBack.providerThreads, []);
    assert.equal(
      (yield* store.getThreadProjection(threadId)).thread.activeProviderThreadId,
      providerThreadId,
    );
    assert.deepEqual(
      ProjectionStore.threadShellFromProjection(rolledBack).pendingBackgroundTasks,
      [],
    );
  }).pipe(Effect.provide(layerTest)),
);

const addOrphanedRecoveryCandidate = Effect.fn("addOrphanedRecoveryCandidate")(function* (
  suffix: string,
) {
  const projectionStore = yield* ProjectionStore.ProjectionStoreV2;
  const now = yield* DateTime.now;
  const threadId = ThreadId.make(`thread:${suffix}:orphaned`);
  const runId = RunId.make(`run:${suffix}:missing`);
  const rootNodeId = NodeId.make(`node:${suffix}:orphaned`);

  yield* projectionStore.apply({
    id: EventId.make(`event:${suffix}:thread-created`),
    type: "thread.created",
    threadId,
    occurredAt: now,
    payload: {
      createdBy: "user",
      creationSource: "web",
      id: threadId,
      projectId: ProjectId.make(`project:${suffix}`),
      title: "Orphaned recovery candidate",
      providerInstanceId,
      modelSelection,
      runtimeMode: "full-access",
      interactionMode: "default",
      branch: null,
      worktreePath: null,
      activeProviderThreadId: null,
      lineage: {
        parentThreadId: null,
        relationshipToParent: null,
        rootThreadId: threadId,
      },
      forkedFrom: null,
      createdAt: now,
      updatedAt: now,
      archivedAt: null,
      settledOverride: null,
      settledAt: null,
      lastVisitedAt: null,
      deletedAt: null,
    },
  });
  yield* projectionStore.apply({
    id: EventId.make(`event:${suffix}:item-running`),
    type: "turn-item.updated",
    threadId,
    runId,
    nodeId: rootNodeId,
    driver,
    occurredAt: now,
    payload: {
      id: TurnItemId.make(`item:${suffix}:orphaned`),
      threadId,
      runId,
      nodeId: rootNodeId,
      providerThreadId: null,
      providerTurnId: null,
      nativeItemRef: null,
      parentItemId: null,
      ordinal: 1,
      status: "running",
      title: "orphaned command",
      startedAt: now,
      completedAt: null,
      updatedAt: now,
      type: "command_execution",
      input: "sleep 60",
    },
  });

  return threadId;
});

const restartCancelledWorkSurvivesStaleRunUpdate = Effect.gen(function* () {
  const store = yield* ProjectionStore.ProjectionStoreV2;
  const threadId = yield* addRolledBackRecoveryCandidate("restart-cancelled-work-stale-update");
  const run = (yield* store.getThreadProjection(threadId)).runs[0]!;
  const now = yield* DateTime.now;
  const work = [{ kind: "subagent" as const, label: "Background subagent test" }];
  yield* store.apply({
    id: EventId.make("event:restart-cancelled-work-stale-update:recorded"),
    type: "run.background-work-cancelled",
    threadId,
    runId: run.id,
    providerInstanceId,
    occurredAt: now,
    payload: { runId: run.id, restartCancelledBackgroundWork: work },
  });
  yield* store.apply({
    id: EventId.make("event:restart-cancelled-work-stale-update:completed"),
    type: "run.updated",
    threadId,
    runId: run.id,
    providerInstanceId,
    occurredAt: now,
    payload: { ...run, status: "completed", completedAt: now },
  });
  const updated = (yield* store.getThreadProjection(threadId)).runs[0];
  assert.equal(updated?.status, "completed");
  assert.deepEqual(updated?.restartCancelledBackgroundWork, work);
});

const threadWorktreePathsIncludeArchivedOnRequest = Effect.gen(function* () {
  const store = yield* ProjectionStore.ProjectionStoreV2;
  const now = yield* DateTime.now;
  const create = (id: string, input: { worktreePath: string | null; archived?: boolean }) => {
    const threadId = ThreadId.make(id);
    const thread = {
      createdBy: "user" as const,
      creationSource: "web" as const,
      id: threadId,
      projectId: ProjectId.make("project:worktree-paths"),
      title: `Title for ${id}`,
      providerInstanceId,
      modelSelection,
      runtimeMode: "full-access" as const,
      interactionMode: "default" as const,
      branch: input.worktreePath === null ? null : id,
      worktreePath: input.worktreePath,
      activeProviderThreadId: null,
      lineage: { parentThreadId: null, relationshipToParent: null, rootThreadId: threadId },
      forkedFrom: null,
      createdAt: now,
      updatedAt: now,
      archivedAt: input.archived === true ? now : null,
      settledOverride: null,
      settledAt: null,
      lastVisitedAt: null,
      deletedAt: null,
    };
    return store
      .apply({
        id: EventId.make(`event:${id}:created`),
        type: "thread.created",
        threadId,
        occurredAt: now,
        payload: thread,
      })
      .pipe(Effect.as(thread));
  };
  yield* create("thread:active", { worktreePath: "/worktrees/active" });
  yield* create("thread:archived", { worktreePath: "/worktrees/archived", archived: true });
  yield* create("thread:local", { worktreePath: null });
  const deleted = yield* create("thread:deleted", { worktreePath: "/worktrees/deleted" });
  yield* store.apply({
    id: EventId.make("event:thread:deleted:deleted"),
    type: "thread.deleted",
    threadId: deleted.id,
    occurredAt: now,
    payload: { ...deleted, deletedAt: now },
  });

  assert.deepEqual(yield* store.getThreadWorktreePaths(), [
    { threadId: ThreadId.make("thread:active"), worktreePath: "/worktrees/active" },
  ]);
  assert.deepEqual(yield* store.getThreadWorktreePaths({ includeArchived: true }), [
    { threadId: ThreadId.make("thread:active"), worktreePath: "/worktrees/active" },
    { threadId: ThreadId.make("thread:archived"), worktreePath: "/worktrees/archived" },
  ]);
  assert.deepEqual(
    yield* store.getThreadTitles([
      deleted.id,
      ThreadId.make("thread:active"),
      ThreadId.make("thread:archived"),
    ]),
    {
      [ThreadId.make("thread:active")]: "Title for thread:active",
      [ThreadId.make("thread:archived")]: "Title for thread:archived",
    },
  );
});

const commandReadsSkipSettledLeafNodes = Effect.gen(function* () {
  const store = yield* ProjectionStore.ProjectionStoreV2;
  const now = yield* DateTime.now;
  const threadId = yield* addRolledBackRecoveryCandidate("settled-leaf-nodes");
  const settledRun = (yield* store.getThreadProjection(threadId)).runs[0]!;
  const activeRun = {
    ...settledRun,
    id: RunId.make("run:settled-leaf-nodes:active"),
    ordinal: 2,
    userMessageId: MessageId.make("message:settled-leaf-nodes:active"),
    rootNodeId: NodeId.make("node:settled-leaf-nodes:active-root"),
    status: "running" as const,
    completedAt: null,
  };
  yield* store.apply({
    id: EventId.make("event:settled-leaf-nodes:active-run"),
    type: "run.created",
    threadId,
    runId: activeRun.id,
    nodeId: activeRun.rootNodeId,
    driver,
    providerInstanceId,
    occurredAt: now,
    payload: activeRun,
  });
  const nodes = [
    ["settled-root", settledRun, "root_turn", "completed"],
    ["settled-tool", settledRun, "tool_call", "completed"],
    ["settled-message", settledRun, "assistant_message", "failed"],
    ["settled-reasoning", settledRun, "reasoning", "cancelled"],
    ["settled-subagent", settledRun, "subagent", "completed"],
    ["settled-run-live-tool", settledRun, "tool_call", "running"],
    ["active-run-done-tool", activeRun, "tool_call", "completed"],
  ] as const;
  for (const [name, run, kind, status] of nodes) {
    const id = NodeId.make(`node:settled-leaf-nodes:${name}`);
    yield* store.apply({
      id: EventId.make(`event:settled-leaf-nodes:${name}`),
      type: "node.updated",
      threadId,
      runId: run.id,
      nodeId: id,
      driver,
      occurredAt: now,
      payload: {
        id,
        threadId,
        runId: run.id,
        rootNodeId: kind === "root_turn" ? id : run.rootNodeId!,
        parentNodeId: kind === "root_turn" ? null : run.rootNodeId,
        kind,
        status,
        countsForRun: kind === "root_turn",
        providerThreadId: null,
        providerTurnId: null,
        nativeItemRef: null,
        runtimeRequestId: null,
        checkpointScopeId: null,
        startedAt: now,
        completedAt: status === "running" ? null : now,
      },
    });
  }
  const nodeNames = (records: { readonly nodes: ReadonlyArray<{ readonly id: string }> }) =>
    records.nodes
      .map((node) => node.id.replace("node:settled-leaf-nodes:", ""))
      .filter((name) => nodes.some(([candidate]) => candidate === name))
      .toSorted();

  assert.deepEqual(
    nodeNames(yield* store.getThreadRecords(threadId, ["nodes"], { omitSettledLeafNodes: true })),
    ["active-run-done-tool", "settled-root", "settled-run-live-tool", "settled-subagent"],
  );
  assert.deepEqual(
    nodeNames(yield* store.getThreadRecords(threadId, ["nodes"])),
    nodes.map(([name]) => name).toSorted(),
  );

  const subagents = [
    ["delivered", settledRun, "completed", "delivered"],
    ["acknowledged", settledRun, "failed", "acknowledged"],
    ["disposed", settledRun, "cancelled", "disposed"],
    ["pending-delivery", settledRun, "completed", "pending"],
    ["claimed", settledRun, "completed", "claimed"],
    ["no-delivery", settledRun, "interrupted", null],
    ["running", settledRun, "running", null],
    ["active-run-delivered", activeRun, "completed", "delivered"],
  ] as const;
  // A follow-up reuses its task's child thread.
  const childThreadIds: Partial<Record<(typeof subagents)[number][0], ThreadId>> = {
    delivered: ThreadId.make("thread:settled-subagents:child-a"),
    running: ThreadId.make("thread:settled-subagents:child-a"),
    "no-delivery": ThreadId.make("thread:settled-subagents:child-b"),
  };
  for (const [index, [name, run, status, delivery]] of subagents.entries()) {
    const id = NodeId.make(`node:settled-subagents:${name}`);
    const childThreadId = childThreadIds[name] ?? null;
    yield* store.apply({
      id: EventId.make(`event:settled-subagents:item:${name}`),
      type: "turn-item.updated",
      threadId,
      runId: run.id,
      nodeId: id,
      driver,
      occurredAt: now,
      payload: {
        id: TurnItemId.make(`item:settled-subagents:${name}`),
        type: "subagent",
        threadId,
        runId: run.id,
        nodeId: id,
        providerThreadId: null,
        providerTurnId: null,
        nativeItemRef: null,
        parentItemId: null,
        ordinal: 100 + index,
        status: status === "running" ? "running" : "completed",
        title: name,
        startedAt: now,
        completedAt: status === "running" ? null : now,
        updatedAt: now,
        subagentId: id,
        origin: "app_owned",
        driver,
        providerInstanceId,
        childThreadId,
        prompt: "Do the work",
        result: null,
      },
    });
    yield* store.apply({
      id: EventId.make(`event:settled-subagents:${name}`),
      type: "subagent.updated",
      threadId,
      runId: run.id,
      nodeId: id,
      driver,
      providerInstanceId,
      occurredAt: now,
      payload: {
        id,
        threadId,
        runId: run.id,
        parentNodeId: run.rootNodeId!,
        origin: "app_owned",
        createdBy: "agent",
        driver,
        providerInstanceId,
        providerThreadId: null,
        childThreadId,
        nativeTaskRef: null,
        prompt: "Do the work",
        title: name,
        model: null,
        ...(delivery === null
          ? {}
          : { completionDelivery: { state: delivery, observedByRunId: null } }),
        status,
        result: null,
        startedAt: now,
        completedAt: status === "running" ? null : now,
        updatedAt: now,
      },
    });
  }
  const subagentNames = (records: {
    readonly subagents: ReadonlyArray<{ readonly title: string | null }>;
  }) => records.subagents.map((subagent) => subagent.title ?? "").toSorted();
  assert.deepEqual(
    subagentNames(
      yield* store.getThreadRecords(threadId, ["subagents"], { omitSettledSubagents: true }),
    ),
    ["active-run-delivered", "claimed", "no-delivery", "pending-delivery", "running"],
  );
  assert.deepEqual(
    subagentNames(yield* store.getThreadRecords(threadId, ["subagents"])),
    subagents.map(([name]) => name).toSorted(),
  );
  // A command about one settled task keeps it, and reads only its node and turn item.
  const deliveredId = NodeId.make("node:settled-subagents:delivered");
  const forTask = yield* store.getThreadRecords(threadId, ["subagents", "nodes", "turnItems"], {
    omitSettledSubagents: { except: [deliveredId] },
    nodeIds: [NodeId.make("node:settled-leaf-nodes:settled-tool")],
    turnItemNodeIds: [deliveredId],
  });
  assert.deepEqual(subagentNames(forTask), [
    "active-run-delivered",
    "claimed",
    "delivered",
    "no-delivery",
    "pending-delivery",
    "running",
  ]);
  assert.deepEqual(nodeNames(forTask), ["settled-tool"]);
  assert.deepEqual(
    forTask.turnItems.map((item) => item.title),
    ["delivered"],
  );
  assert.strictEqual(yield* store.getSubagentCount(threadId), subagents.length);
  assert.deepEqual(
    [...(yield* store.listAppOwnedChildThreadIds(threadId))],
    [childThreadIds.delivered, childThreadIds["no-delivery"]],
  );
});

const backgroundThreadUpdatesKeepTimestamp = Effect.gen(function* () {
  const store = yield* ProjectionStore.ProjectionStoreV2;
  const createdAt = DateTime.makeUnsafe("2026-10-06T14:00:00.000Z");
  const threadId = ThreadId.make("thread:background-updates");
  const thread = {
    createdBy: "user" as const,
    creationSource: "web" as const,
    id: threadId,
    projectId: ProjectId.make("project:background-updates"),
    title: "Background updates",
    providerInstanceId,
    modelSelection,
    runtimeMode: "full-access" as const,
    interactionMode: "default" as const,
    branch: null,
    worktreePath: null,
    activeProviderThreadId: null,
    lineage: { parentThreadId: null, relationshipToParent: null, rootThreadId: threadId },
    forkedFrom: null,
    createdAt,
    updatedAt: createdAt,
    archivedAt: null,
    settledOverride: null,
    settledAt: null,
    lastVisitedAt: null,
    deletedAt: null,
  };
  yield* store.apply({
    id: EventId.make("event:background-updates:created"),
    type: "thread.created",
    threadId,
    occurredAt: createdAt,
    payload: thread,
  });
  const readActivity = Effect.gen(function* () {
    const projection = yield* store.getThreadProjection(threadId);
    const thread = yield* store.getThread(threadId);
    const shell = yield* store.getThreadShell(threadId);
    const listed = (yield* store.getShellSnapshot()).threads.find((entry) => entry.id === threadId);
    assert.deepEqual(projection.thread.updatedAt, createdAt);
    assert.deepEqual(thread.updatedAt, createdAt);
    assert.deepEqual(shell?.updatedAt, createdAt);
    assert.deepEqual(listed?.updatedAt, createdAt);
    return projection;
  });
  const link = {
    host: "github.com",
    repository: "owner/repository",
    number: 42,
    url: "https://github.com/owner/repository/pull/42",
    source: "manual" as const,
    linkedAt: DateTime.formatIso(createdAt),
    snapshot: null,
    stack: null,
  };
  yield* store.apply({
    id: EventId.make("event:background-updates:pull-request-synced"),
    type: "thread.pull-request-synced",
    threadId,
    occurredAt: DateTime.add(createdAt, { minutes: 45 }),
    payload: { ...thread, pullRequests: [link] },
  });
  const synced = yield* readActivity;
  assert.deepEqual(synced.updatedAt, createdAt);
  assert.equal(synced.thread.pullRequests?.[0]?.number, 42);

  const snapshot = {
    state: "open" as const,
    title: "Ship it",
    headBranch: "feature",
    baseBranch: "main",
    isDraft: false,
    updatedAt: DateTime.formatIso(DateTime.add(createdAt, { minutes: 46 })),
    syncedAt: DateTime.formatIso(DateTime.add(createdAt, { minutes: 47 })),
    closedAt: null,
    mergedAt: null,
  };
  yield* store.apply({
    id: EventId.make("event:background-updates:pull-request-link-updated"),
    type: "thread.pull-request-link-updated",
    threadId,
    occurredAt: DateTime.add(createdAt, { minutes: 48 }),
    payload: { ...link, snapshot },
  });
  const refreshed = yield* readActivity;
  assert.deepEqual(refreshed.updatedAt, createdAt);
  assert.deepEqual(refreshed.thread.pullRequests?.[0]?.snapshot, snapshot);

  yield* store.apply({
    id: EventId.make("event:background-updates:active-reordered"),
    type: "thread.active-reordered",
    threadId,
    occurredAt: DateTime.add(createdAt, { minutes: 50 }),
    payload: { ...refreshed.thread, activeOrderKey: "a" },
  });
  const reordered = yield* readActivity;
  assert.deepEqual(reordered.updatedAt, createdAt);
  assert.equal(reordered.thread.activeOrderKey, "a");

  // An already-unpinned thread keeps its activity time even when replay's envelope advances.
  yield* store.apply({
    id: EventId.make("event:background-updates:already-unpinned"),
    type: "thread.unpinned",
    threadId,
    occurredAt: DateTime.add(createdAt, { minutes: 55 }),
    payload: { ...reordered.thread, pinnedAt: null, pinOrderKey: null },
  });
  yield* readActivity;
});

it.effect("SQL projection keeps background thread updates out of updatedAt", () =>
  backgroundThreadUpdatesKeepTimestamp.pipe(Effect.provide(layerTest)),
);

const runlessNativeSubagentNeedsRecovery = Effect.gen(function* () {
  const store = yield* ProjectionStore.ProjectionStoreV2;
  const threadId = yield* addRolledBackRecoveryCandidate("runless-native-recovery");
  const now = yield* DateTime.now;
  const id = NodeId.make("node:runless-native-recovery");
  assert.notInclude(yield* store.getRecoveryThreadIds("runtime"), threadId);
  for (const status of ["pending", "running", "waiting", "completed"] as const) {
    yield* store.apply({
      id: EventId.make(`event:runless-native-recovery:${status}`),
      type: "subagent.updated",
      threadId,
      nodeId: id,
      driver,
      providerInstanceId,
      occurredAt: now,
      payload: {
        id,
        threadId,
        runId: null,
        parentNodeId: NodeId.make("node:runless-native-parent"),
        origin: "provider_native",
        createdBy: "agent",
        driver,
        providerInstanceId,
        providerThreadId: null,
        childThreadId: null,
        nativeTaskRef: { driver, nativeId: "runless-native", strength: "strong" },
        prompt: "Do the work",
        title: "Workflow member",
        model: null,
        status,
        result: null,
        startedAt: now,
        completedAt: status === "completed" ? now : null,
        updatedAt: now,
      },
    });
    assert.equal(
      (yield* store.getRecoveryThreadIds("runtime")).includes(threadId),
      status !== "completed",
    );
  }
});

const subagentRecordsSelectById = Effect.gen(function* () {
  const store = yield* ProjectionStore.ProjectionStoreV2;
  const threadId = yield* addRolledBackRecoveryCandidate("subagent-records-by-id");
  const now = yield* DateTime.now;
  const ids = [NodeId.make("node:subagent-by-id:a"), NodeId.make("node:subagent-by-id:b")];
  for (const id of ids) {
    yield* store.apply({
      id: EventId.make(`event:${id}`),
      type: "subagent.updated",
      threadId,
      nodeId: id,
      driver,
      providerInstanceId,
      occurredAt: now,
      payload: {
        id,
        threadId,
        runId: null,
        parentNodeId: NodeId.make("node:subagent-by-id:parent"),
        origin: "app_owned",
        createdBy: "agent",
        driver,
        providerInstanceId,
        providerThreadId: null,
        childThreadId: null,
        nativeTaskRef: null,
        prompt: "Do the work",
        title: "Delegated task",
        model: null,
        status: "running",
        result: null,
        startedAt: now,
        completedAt: null,
        updatedAt: now,
      },
    });
  }
  const read = (subagentIds?: ReadonlyArray<NodeId>) =>
    store
      .getThreadRecords(threadId, ["subagents"], subagentIds === undefined ? {} : { subagentIds })
      .pipe(Effect.map(({ subagents }) => subagents.map((task) => task.id)));
  assert.deepEqual(yield* read(), ids);
  assert.deepEqual(yield* read([ids[1]!]), [ids[1]]);
  assert.deepEqual(yield* read([]), []);
});

const assertProjectScopedShells = Effect.fn("assertProjectScopedShells")(function* (
  suffix: string,
) {
  const projectionStore = yield* ProjectionStore.ProjectionStoreV2;
  const now = yield* DateTime.now;
  const projectId = ProjectId.make(`project:${suffix}:a`);
  const otherProjectId = ProjectId.make(`project:${suffix}:b`);
  const activeThreadId = ThreadId.make(`thread:${suffix}:a-active`);
  const archivedThreadId = ThreadId.make(`thread:${suffix}:a-archived`);
  const forkThreadId = ThreadId.make(`thread:${suffix}:a-fork`);
  const sourceThreadId = ThreadId.make(`thread:${suffix}:b-source`);
  const otherThreadId = ThreadId.make(`thread:${suffix}:b-other`);
  const sourceRunId = RunId.make(`run:${suffix}:b-source`);
  const sourceNodeId = NodeId.make(`node:${suffix}:b-source`);
  const createThread = (input: {
    readonly threadId: ThreadId;
    readonly projectId: ProjectId;
    readonly archived?: boolean;
    readonly forkedFromRunOf?: ThreadId;
  }) =>
    projectionStore.apply({
      id: EventId.make(`event:${input.threadId}:created`),
      type: "thread.created",
      threadId: input.threadId,
      occurredAt: now,
      payload: {
        createdBy: "user",
        creationSource: "web",
        id: input.threadId,
        projectId: input.projectId,
        title: input.threadId,
        providerInstanceId,
        modelSelection,
        runtimeMode: "full-access",
        interactionMode: "default",
        branch: null,
        worktreePath: null,
        activeProviderThreadId: null,
        lineage:
          input.forkedFromRunOf === undefined
            ? { parentThreadId: null, relationshipToParent: null, rootThreadId: input.threadId }
            : {
                parentThreadId: input.forkedFromRunOf,
                relationshipToParent: "fork",
                rootThreadId: input.forkedFromRunOf,
              },
        forkedFrom:
          input.forkedFromRunOf === undefined
            ? null
            : { type: "run", threadId: input.forkedFromRunOf, runId: sourceRunId },
        createdAt: now,
        updatedAt: now,
        archivedAt: input.archived === true ? now : null,
        settledOverride: null,
        settledAt: null,
        lastVisitedAt: null,
        deletedAt: null,
      },
    });

  yield* createThread({ threadId: sourceThreadId, projectId: otherProjectId, archived: true });
  yield* createThread({ threadId: otherThreadId, projectId: otherProjectId });
  yield* createThread({ threadId: activeThreadId, projectId });
  yield* createThread({ threadId: archivedThreadId, projectId, archived: true });
  yield* createThread({ threadId: forkThreadId, projectId, forkedFromRunOf: sourceThreadId });
  yield* projectionStore.apply({
    id: EventId.make(`event:${suffix}:source-run`),
    type: "run.created",
    threadId: sourceThreadId,
    runId: sourceRunId,
    nodeId: sourceNodeId,
    driver,
    occurredAt: now,
    payload: {
      id: sourceRunId,
      threadId: sourceThreadId,
      ordinal: 1,
      providerInstanceId,
      modelSelection,
      providerThreadId: null,
      userMessageId: MessageId.make(`message:${suffix}:source-user`),
      rootNodeId: sourceNodeId,
      activeAttemptId: null,
      status: "completed",
      requestedAt: now,
      startedAt: now,
      completedAt: now,
      checkpointId: null,
      contextHandoffId: null,
    },
  });
  yield* projectionStore.apply({
    id: EventId.make(`event:${suffix}:source-item`),
    type: "turn-item.updated",
    threadId: sourceThreadId,
    runId: sourceRunId,
    occurredAt: now,
    payload: {
      id: TurnItemId.make(`turn-item:${suffix}:source`),
      threadId: sourceThreadId,
      runId: sourceRunId,
      nodeId: null,
      providerThreadId: null,
      providerTurnId: null,
      nativeItemRef: null,
      parentItemId: null,
      ordinal: 1,
      status: "completed",
      title: null,
      startedAt: now,
      completedAt: now,
      updatedAt: now,
      type: "assistant_message",
      messageId: MessageId.make(`message:${suffix}:source-assistant`),
      text: "Source answer",
      streaming: false,
    },
  });

  const unscoped = yield* projectionStore.getShellSnapshot();
  const inProject = (thread: { readonly projectId: ProjectId }) => thread.projectId === projectId;
  const active = yield* projectionStore.getShellSnapshot({ projectId, location: "active" });
  assert.deepEqual(active.threads, unscoped.threads.filter(inProject));
  assert.deepEqual(active.archivedThreads, []);
  assert.sameMembers(
    active.threads.map((thread) => thread.id),
    [activeThreadId, forkThreadId],
  );
  const fork = active.threads.find((thread) => thread.id === forkThreadId);