[
  {
    "label": "list",
    "sql": "SELECT t.thread_id, t.payload_json, CASE WHEN json_extract(t.payload_json, '$.forkedFrom.type') = 'run' AND json_extract(t.payload_json, '$.lineage.sideChat') IS NOT 1 THEN json_extract(t.payload_json, '$.forkedFrom.threadId') ELSE NULL END AS forked_from_run_source_thread_id, presented.run_id AS latest_run_id, presented.status AS latest_run_status, presented.requested_at AS latest_run_requested_at, json_extract(presented.payload_json, '$.startedAt') AS latest_run_started_at, presented.completed_at AS latest_run_completed_at, EXISTS ( SELECT 1 FROM orchestration_v2_projection_turn_items stop_request WHERE stop_request.thread_id = t.thread_id AND stop_request.run_id = presented.run_id AND stop_request.type = 'run_interrupt_request' ) AS latest_run_stop_requested, EXISTS ( SELECT 1 FROM orchestration_v2_projection_messages wake WHERE wake.message_id = json_extract(presented.payload_json, '$.userMessageId') AND json_extract(wake.payload_json, '$.notification.source.kind') IN ('background_task', 'background_command', 'command', 'monitor') AND json_extract(wake.payload_json, '$.notification.source.work') IS NULL AND json_extract(wake.payload_json, '$.notification.outcome') = 'updated' AND json_extract(wake.payload_json, '$.delegatedCompletion') IS NULL ) AND NOT EXISTS ( SELECT 1 FROM orchestration_v2_projection_messages steer WHERE steer.run_id = presented.run_id AND json_extract(steer.payload_json, '$.createdBy') = 'user' ) AS latest_run_background_wake, active.run_id AS active_run_id, active.status AS active_run_status, ( SELECT r.status -- Read active runs by status. Walking the ordinal index to skip this sort -- reads every run of a thread with none active. FROM orchestration_v2_projection_runs r INDEXED BY orchestration_v2_projection_runs_thread_status_idx WHERE r.thread_id = t.thread_id AND r.status IN ('preparing', 'starting', 'running', 'waiting') AND NOT ( r.status <> 'failed' AND EXISTS ( SELECT 1 FROM orchestration_v2_projection_messages prompt WHERE prompt.message_id = json_extract(r.payload_json, '$.userMessageId') AND ( prompt.role = 'user' AND lower(trim(json_extract(prompt.payload_json, '$.text'), char(9, 10, 11, 12, 13, 32, 160, 5760, 8192, 8193, 8194, 8195, 8196, 8197, 8198, 8199, 8200, 8201, 8202, 8232, 8233, 8239, 8287, 12288, 65279) )) = ? AND coalesce(json_array_length(prompt.payload_json, '$.attachments'), 0) = 0 AND ( json_extract(prompt.payload_json, '$.idleCompaction') IS NOT NULL OR ( json_extract(prompt.payload_json, '$.createdBy') = 'agent' AND json_extract(prompt.payload_json, '$.creationSource') = 'server' ) ) ) ) AND NOT EXISTS ( SELECT 1 FROM orchestration_v2_projection_turn_items compaction WHERE compaction.thread_id = r.thread_id AND compaction.run_id = r.run_id AND compaction.type = 'compaction' AND compaction.status = 'failed' ) ) ORDER BY r.ordinal DESC, r.run_id DESC LIMIT 1 ) AS activity_run_status, ( -- Mirrors orchestrationV2RunWorkStartedAt. SELECT COALESCE( json_extract(r.payload_json, '$.workStartedAt'), json_extract(r.payload_json, '$.startedAt'), r.requested_at ) FROM orchestration_v2_projection_runs r INDEXED BY orchestration_v2_projection_runs_thread_status_idx WHERE r.thread_id = t.thread_id AND r.status IN ('preparing', 'starting', 'running', 'waiting') AND NOT ( r.status <> 'failed' AND EXISTS ( SELECT 1 FROM orchestration_v2_projection_messages prompt WHERE prompt.message_id = json_extract(r.payload_json, '$.userMessageId') AND ( prompt.role = 'user' AND lower(trim(json_extract(prompt.payload_json, '$.text'), char(9, 10, 11, 12, 13, 32, 160, 5760, 8192, 8193, 8194, 8195, 8196, 8197, 8198, 8199, 8200, 8201, 8202, 8232, 8233, 8239, 8287, 12288, 65279) )) = ? AND coalesce(json_array_length(prompt.payload_json, '$.attachments'), 0) = 0 AND ( json_extract(prompt.payload_json, '$.idleCompaction') IS NOT NULL OR ( json_extract(prompt.payload_json, '$.createdBy') = 'agent' AND json_extract(prompt.payload_json, '$.creationSource') = 'server' ) ) ) ) AND NOT EXISTS ( SELECT 1 FROM orchestration_v2_projection_turn_items compaction WHERE compaction.thread_id = r.thread_id AND compaction.run_id = r.run_id AND compaction.type = 'compaction' AND compaction.status = 'failed' ) ) ORDER BY r.ordinal DESC, r.run_id DESC LIMIT 1 ) AS activity_run_started_at, ( SELECT json_extract(session.payload_json, '$.lastError') -- Keep bindings outermost: one provider instance can hold every session. FROM orchestration_v2_projection_provider_session_bindings binding CROSS JOIN orchestration_v2_projection_provider_sessions session ON session.provider_session_id = binding.provider_session_id WHERE binding.thread_id = t.thread_id AND session.provider_instance_id = t.provider_instance_id ORDER BY session.updated_at DESC, session.provider_session_id DESC LIMIT 1 ) AS last_error, ( SELECT item.payload_json FROM orchestration_v2_projection_turn_items item INNER JOIN orchestration_v2_projection_runs r ON r.run_id = item.run_id WHERE r.run_id = presented.run_id AND r.status = 'failed' AND item.thread_id = t.thread_id AND item.type = 'error' AND item.status = 'failed' AND item.node_id IS json_extract(r.payload_json, '$.rootNodeId') ORDER BY item.updated_at DESC, item.ordinal DESC, item.turn_item_id DESC LIMIT 1 ) AS terminal_failure_payload_json, blocked.run_id AS blocking_run_id, blocked.requested_at AS blocking_run_requested_at, json_extract(blocked.payload_json, '$.startedAt') AS blocking_run_started_at, blocked.completed_at AS blocking_run_completed_at, EXISTS ( SELECT 1 FROM orchestration_v2_projection_turn_items stop_request WHERE stop_request.thread_id = t.thread_id AND stop_request.run_id = blocked.run_id AND stop_request.type = 'run_interrupt_request' ) AS blocking_run_stop_requested, EXISTS ( SELECT 1 FROM orchestration_v2_projection_messages wake WHERE wake.message_id = json_extract(blocked.payload_json, '$.userMessageId') AND json_extract(wake.payload_json, '$.notification.source.kind') IN ('background_task', 'background_command', 'command', 'monitor') AND json_extract(wake.payload_json, '$.notification.source.work') IS NULL AND json_extract(wake.payload_json, '$.notification.outcome') = 'updated' AND json_extract(wake.payload_json, '$.delegatedCompletion') IS NULL ) AND NOT EXISTS ( SELECT 1 FROM orchestration_v2_projection_messages steer WHERE steer.run_id = blocked.run_id AND json_extract(steer.payload_json, '$.createdBy') = 'user' ) AS blocking_run_background_wake, ( SELECT item.payload_json FROM orchestration_v2_projection_turn_items item WHERE item.thread_id = t.thread_id AND item.run_id = blocked.run_id AND item.type = 'error' AND item.status = 'failed' AND item.node_id IS json_extract(blocked.payload_json, '$.rootNodeId') ORDER BY item.updated_at DESC, item.ordinal DESC, item.turn_item_id DESC LIMIT 1 ) AS blocking_failure_payload_json, ( SELECT request.payload_json FROM orchestration_v2_projection_runtime_requests request WHERE request.thread_id = t.thread_id AND request.status = 'pending' ORDER BY (request.kind = 'user_input') ASC, request.created_at DESC, request.runtime_request_id DESC LIMIT 1 ) AS pending_request_payload_json, ( SELECT secret.payload_json -- Keep runs outermost so completed history is never scanned for a pending secret. FROM orchestration_v2_projection_runs r INDEXED BY orchestration_v2_projection_runs_thread_status_idx CROSS JOIN orchestration_v2_projection_turn_items secret INDEXED BY orchestration_v2_projection_turn_items_thread_run_idx WHERE r.thread_id = t.thread_id AND r.status IN ('preparing', 'starting', 'running', 'waiting') AND secret.thread_id = t.thread_id AND secret.run_id = r.run_id AND secret.type = 'secret_request' AND secret.status = 'waiting' ORDER BY secret.updated_at DESC, secret.turn_item_id DESC LIMIT 1 ) AS pending_secret_request_payload_json, ( SELECT link.payload_json FROM orchestration_v2_projection_turn_items link INDEXED BY orchestration_v2_projection_turn_items_waiting_link_idx WHERE link.thread_id = t.thread_id AND link.type = 'link_request' AND link.status = 'waiting' ORDER BY link.updated_at DESC, link.turn_item_id DESC LIMIT 1 ) AS pending_link_request_payload_json, ( SELECT message.updated_at FROM orchestration_v2_projection_messages message WHERE message.thread_id = t.thread_id AND message.role = 'user' AND NOT ( message.role = 'user' AND lower(trim(json_extract(message.payload_json, '$.text'), char(9, 10, 11, 12, 13, 32, 160, 5760, 8192, 8193, 8194, 8195, 8196, 8197, 8198, 8199, 8200, 8201, 8202, 8232, 8233, 8239, 8287, 12288, 65279) )) = ? AND coalesce(json_array_length(message.payload_json, '$.attachments'), 0) = 0 AND ( json_extract(message.payload_json, '$.idleCompaction') IS NOT NULL OR ( json_extract(message.payload_json, '$.createdBy') = 'agent' AND json_extract(message.payload_json, '$.creationSource') = 'server' ) ) ) ORDER BY message.updated_at DESC, message.message_id DESC LIMIT 1 ) AS latest_user_message_at, ( SELECT message.updated_at FROM orchestration_v2_projection_messages message WHERE message.thread_id = t.thread_id AND message.role = 'user' AND json_extract(message.payload_json, '$.createdBy') = 'user' AND NOT ( message.role = 'user' AND lower(trim(json_extract(message.payload_json, '$.text'), char(9, 10, 11, 12, 13, 32, 160, 5760, 8192, 8193, 8194, 8195, 8196, 8197, 8198, 8199, 8200, 8201, 8202, 8232, 8233, 8239, 8287, 12288, 65279) )) = ? AND coalesce(json_array_length(message.payload_json, '$.attachments'), 0) = 0 AND ( json_extract(message.payload_json, '$.idleCompaction') IS NOT NULL OR ( json_extract(message.payload_json, '$.createdBy') = 'agent' AND json_extract(message.payload_json, '$.creationSource') = 'server' ) ) ) ORDER BY message.updated_at DESC, message.message_id DESC LIMIT 1 ) AS latest_user_authored_message_at, EXISTS ( SELECT 1 FROM orchestration_v2_projection_plans plan WHERE plan.thread_id = t.thread_id AND plan.kind = 'proposed_plan' AND plan.status = 'active' ) AS has_actionable_proposed_plan FROM orchestration_v2_projection_threads t LEFT JOIN orchestration_v2_projection_runs active ON active.run_id = ( SELECT r.run_id FROM orchestration_v2_projection_runs r INDEXED BY orchestration_v2_projection_runs_thread_status_idx WHERE r.thread_id = t.thread_id AND r.status IN ('preparing', 'starting', 'running') AND NOT ( r.status <> 'failed' AND EXISTS ( SELECT 1 FROM orchestration_v2_projection_messages prompt WHERE prompt.message_id = json_extract(r.payload_json, '$.userMessageId') AND ( prompt.role = 'user' AND lower(trim(json_extract(prompt.payload_json, '$.text'), char(9, 10, 11, 12, 13, 32, 160, 5760, 8192, 8193, 8194, 8195, 8196, 8197, 8198, 8199, 8200, 8201, 8202, 8232, 8233, 8239, 8287, 12288, 65279) )) = ? AND coalesce(json_array_length(prompt.payload_json, '$.attachments'), 0) = 0 AND ( json_extract(prompt.payload_json, '$.idleCompaction') IS NOT NULL OR ( json_extract(prompt.payload_json, '$.createdBy') = 'agent' AND json_extract(prompt.payload_json, '$.creationSource') = 'server' ) ) ) ) AND NOT EXISTS ( SELECT 1 FROM orchestration_v2_projection_turn_items compaction WHERE compaction.thread_id = r.thread_id AND compaction.run_id = r.run_id AND compaction.type = 'compaction' AND compaction.status = 'failed' ) ) ORDER BY r.ordinal DESC, r.run_id DESC LIMIT 1 ) LEFT JOIN orchestration_v2_projection_runs presented ON presented.run_id = ( SELECT candidate.run_id FROM orchestration_v2_projection_runs candidate WHERE candidate.thread_id = t.thread_id AND NOT ( candidate.status = 'queued' AND json_extract(candidate.payload_json, '$.queueHeld') IS 1 ) AND NOT ( candidate.status = 'cancelled' AND json_extract(candidate.payload_json, '$.startedAt') IS NULL AND candidate.completed_at IS NOT NULL AND EXISTS ( SELECT 1 FROM orchestration_v2_projection_runs earlier WHERE earlier.thread_id = candidate.thread_id AND earlier.ordinal < candidate.ordinal AND earlier.status <> 'queued' AND NOT ( earlier.status <> 'failed' AND EXISTS ( SELECT 1 FROM orchestration_v2_projection_messages prompt WHERE prompt.message_id = json_extract(earlier.payload_json, '$.userMessageId') AND ( prompt.role = 'user' AND lower(trim(json_extract(prompt.payload_json, '$.text'), char(9, 10, 11, 12, 13, 32, 160, 5760, 8192, 8193, 8194, 8195, 8196, 8197, 8198, 8199, 8200, 8201, 8202, 8232, 8233, 8239, 8287, 12288, 65279) )) = ? AND coalesce(json_array_length(prompt.payload_json, '$.attachments'), 0) = 0 AND ( json_extract(prompt.payload_json, '$.idleCompaction') IS NOT NULL OR ( json_extract(prompt.payload_json, '$.createdBy') = 'agent' AND json_extract(prompt.payload_json, '$.creationSource') = 'server' ) ) ) ) AND NOT EXISTS ( SELECT 1 FROM orchestration_v2_projection_turn_items compaction WHERE compaction.thread_id = earlier.thread_id AND compaction.run_id = earlier.run_id AND compaction.type = 'compaction' AND compaction.status = 'failed' ) ) AND ( earlier.completed_at IS NULL OR julianday(earlier.completed_at) >= julianday(candidate.completed_at) ) ) ) AND NOT ( candidate.status <> 'failed' AND EXISTS ( SELECT 1 FROM orchestration_v2_projection_messages prompt WHERE prompt.message_id = json_extract(candidate.payload_json, '$.userMessageId') AND ( prompt.role = 'user' AND lower(trim(json_extract(prompt.payload_json, '$.text'), char(9, 10, 11, 12, 13, 32, 160, 5760, 8192, 8193, 8194, 8195, 8196, 8197, 8198, 8199, 8200, 8201, 8202, 8232, 8233, 8239, 8287, 12288, 65279) )) = ? AND coalesce(json_array_length(prompt.payload_json, '$.attachments'), 0) = 0 AND ( json_extract(prompt.payload_json, '$.idleCompaction') IS NOT NULL OR ( json_extract(prompt.payload_json, '$.createdBy') = 'agent' AND json_extract(prompt.payload_json, '$.creationSource') = 'server' ) ) ) ) AND NOT EXISTS ( SELECT 1 FROM orchestration_v2_projection_turn_items compaction WHERE compaction.thread_id = candidate.thread_id AND compaction.run_id = candidate.run_id AND compaction.type = 'compaction' AND compaction.status = 'failed' ) ) ORDER BY candidate.ordinal DESC, candidate.run_id DESC LIMIT 1 ) LEFT JOIN orchestration_v2_projection_runs blocked ON blocked.run_id = ( SELECT candidate.run_id FROM orchestration_v2_projection_runs candidate WHERE candidate.thread_id = t.thread_id AND candidate.status <> 'queued' AND NOT ( candidate.status = 'cancelled' AND json_extract(candidate.payload_json, '$.startedAt') IS NULL ) -- latestExecutedRun: the run that ended last (runRanAfter). ORDER BY candidate.completed_at IS NULL DESC, candidate.completed_at DESC, candidate.ordinal DESC, candidate.run_id DESC LIMIT 1 ) AND blocked.status = 'failed' WHERE t.deleted_at IS NULL AND t.thread_id = ? ORDER BY t.updated_at ASC, t.thread_id ASC",
    "rows": 1
  },
  {
    "label": "list",
    "sql": "SELECT counts.thread_id, SUM(CASE WHEN counts.run_key = '' OR r.status <> 'rolled_back' THEN counts.item_count - CASE WHEN r.status IN ('queued', 'cancelled') THEN ( SELECT COUNT(*) FROM orchestration_v2_projection_turn_items held INDEXED BY orchestration_v2_projection_turn_items_queued_input_idx WHERE held.run_id = counts.run_key AND held.type = 'user_message' AND json_extract(held.payload_json, '$.inputIntent') = 'queued_turn' ) ELSE 0 END ELSE 0 END) AS item_count, SUM(CASE WHEN counts.run_key = '' THEN counts.item_count ELSE 0 END) AS runless_item_count FROM orchestration_v2_projection_turn_item_counts counts LEFT JOIN orchestration_v2_projection_runs r ON r.run_id = counts.run_key WHERE counts.thread_id IN (SELECT value FROM json_each(?)) GROUP BY counts.thread_id",
    "rows": 1
  },
  {
    "label": "list",
    "sql": "SELECT thread_id, payload_json FROM orchestration_v2_projection_provider_threads WHERE thread_id IN (SELECT value FROM json_each(?))",
    "rows": 1
  },
  {
    "label": "list",
    "sql": "SELECT i.thread_id, i.payload_json, p.provider_instance_id AS owning_provider_instance_id FROM orchestration_v2_projection_turn_items i LEFT JOIN orchestration_v2_projection_provider_threads p ON p.provider_thread_id = i.provider_thread_id LEFT JOIN orchestration_v2_projection_runs r ON r.run_id = i.run_id WHERE i.type IN ('command_execution', 'dynamic_tool', 'subagent') AND i.status NOT IN ('completed', 'interrupted', 'failed', 'cancelled') AND (i.run_id IS NULL OR r.status <> 'rolled_back') AND i.thread_id IN (SELECT value FROM json_each(?)) -- Preserve the pending-item partial index; read other work only from running runs. UNION ALL SELECT i.thread_id, i.payload_json, NULL AS owning_provider_instance_id FROM orchestration_v2_projection_turn_items i WHERE i.run_id IN (SELECT value FROM json_each(?)) AND i.type IN ('file_change', 'file_search', 'web_search', 'compaction') AND i.status IN ('running', 'waiting')",
    "rows": 0
  },
  {
    "label": "list",
    "sql": "SELECT t.thread_id, t.payload_json, CASE WHEN json_extract(t.payload_json, '$.forkedFrom.type') = 'run' AND json_extract(t.payload_json, '$.lineage.sideChat') IS NOT 1 THEN json_extract(t.payload_json, '$.forkedFrom.threadId') ELSE NULL END AS forked_from_run_source_thread_id, presented.run_id AS latest_run_id, presented.status AS latest_run_status, presented.requested_at AS latest_run_requested_at, json_extract(presented.payload_json, '$.startedAt') AS latest_run_started_at, presented.completed_at AS latest_run_completed_at, EXISTS ( SELECT 1 FROM orchestration_v2_projection_turn_items stop_request WHERE stop_request.thread_id = t.thread_id AND stop_request.run_id = presented.run_id AND stop_request.type = 'run_interrupt_request' ) AS latest_run_stop_requested, EXISTS ( SELECT 1 FROM orchestration_v2_projection_messages wake WHERE wake.message_id = json_extract(presented.payload_json, '$.userMessageId') AND json_extract(wake.payload_json, '$.notification.source.kind') IN ('background_task', 'background_command', 'command', 'monitor') AND json_extract(wake.payload_json, '$.notification.source.work') IS NULL AND json_extract(wake.payload_json, '$.notification.outcome') = 'updated' AND json_extract(wake.payload_json, '$.delegatedCompletion') IS NULL ) AND NOT EXISTS ( SELECT 1 FROM orchestration_v2_projection_messages steer WHERE steer.run_id = presented.run_id AND json_extract(steer.payload_json, '$.createdBy') = 'user' ) AS latest_run_background_wake, active.run_id AS active_run_id, active.status AS active_run_status, ( SELECT r.status -- Read active runs by status. Walking the ordinal index to skip this sort -- reads every run of a thread with none active. FROM orchestration_v2_projection_runs r INDEXED BY orchestration_v2_projection_runs_thread_status_idx WHERE r.thread_id = t.thread_id AND r.status IN ('preparing', 'starting', 'running', 'waiting') AND NOT ( r.status <> 'failed' AND EXISTS ( SELECT 1 FROM orchestration_v2_projection_messages prompt WHERE prompt.message_id = json_extract(r.payload_json, '$.userMessageId') AND ( prompt.role = 'user' AND lower(trim(json_extract(prompt.payload_json, '$.text'), char(9, 10, 11, 12, 13, 32, 160, 5760, 8192, 8193, 8194, 8195, 8196, 8197, 8198, 8199, 8200, 8201, 8202, 8232, 8233, 8239, 8287, 12288, 65279) )) = ? AND coalesce(json_array_length(prompt.payload_json, '$.attachments'), 0) = 0 AND ( json_extract(prompt.payload_json, '$.idleCompaction') IS NOT NULL OR ( json_extract(prompt.payload_json, '$.createdBy') = 'agent' AND json_extract(prompt.payload_json, '$.creationSource') = 'server' ) ) ) ) AND NOT EXISTS ( SELECT 1 FROM orchestration_v2_projection_turn_items compaction WHERE compaction.thread_id = r.thread_id AND compaction.run_id = r.run_id AND compaction.type = 'compaction' AND compaction.status = 'failed' ) ) ORDER BY r.ordinal DESC, r.run_id DESC LIMIT 1 ) AS activity_run_status, ( -- Mirrors orchestrationV2RunWorkStartedAt. SELECT COALESCE( json_extract(r.payload_json, '$.workStartedAt'), json_extract(r.payload_json, '$.startedAt'), r.requested_at ) FROM orchestration_v2_projection_runs r INDEXED BY orchestration_v2_projection_runs_thread_status_idx WHERE r.thread_id = t.thread_id AND r.status IN ('preparing', 'starting', 'running', 'waiting') AND NOT ( r.status <> 'failed' AND EXISTS ( SELECT 1 FROM orchestration_v2_projection_messages prompt WHERE prompt.message_id = json_extract(r.payload_json, '$.userMessageId') AND ( prompt.role = 'user' AND lower(trim(json_extract(prompt.payload_json, '$.text'), char(9, 10, 11, 12, 13, 32, 160, 5760, 8192, 8193, 8194, 8195, 8196, 8197, 8198, 8199, 8200, 8201, 8202, 8232, 8233, 8239, 8287, 12288, 65279) )) = ? AND coalesce(json_array_length(prompt.payload_json, '$.attachments'), 0) = 0 AND ( json_extract(prompt.payload_json, '$.idleCompaction') IS NOT NULL OR ( json_extract(prompt.payload_json, '$.createdBy') = 'agent' AND json_extract(prompt.payload_json, '$.creationSource') = 'server' ) ) ) ) AND NOT EXISTS ( SELECT 1 FROM orchestration_v2_projection_turn_items compaction WHERE compaction.thread_id = r.thread_id AND compaction.run_id = r.run_id AND compaction.type = 'compaction' AND compaction.status = 'failed' ) ) ORDER BY r.ordinal DESC, r.run_id DESC LIMIT 1 ) AS activity_run_started_at, ( SELECT json_extract(session.payload_json, '$.lastError') -- Keep bindings outermost: one provider instance can hold every session. FROM orchestration_v2_projection_provider_session_bindings binding CROSS JOIN orchestration_v2_projection_provider_sessions session ON session.provider_session_id = binding.provider_session_id WHERE binding.thread_id = t.thread_id AND session.provider_instance_id = t.provider_instance_id ORDER BY session.updated_at DESC, session.provider_session_id DESC LIMIT 1 ) AS last_error, ( SELECT item.payload_json FROM orchestration_v2_projection_turn_items item INNER JOIN orchestration_v2_projection_runs r ON r.run_id = item.run_id WHERE r.run_id = presented.run_id AND r.status = 'failed' AND item.thread_id = t.thread_id AND item.type = 'error' AND item.status = 'failed' AND item.node_id IS json_extract(r.payload_json, '$.rootNodeId') ORDER BY item.updated_at DESC, item.ordinal DESC, item.turn_item_id DESC LIMIT 1 ) AS terminal_failure_payload_json, blocked.run_id AS blocking_run_id, blocked.requested_at AS blocking_run_requested_at, json_extract(blocked.payload_json, '$.startedAt') AS blocking_run_started_at, blocked.completed_at AS blocking_run_completed_at, EXISTS ( SELECT 1 FROM orchestration_v2_projection_turn_items stop_request WHERE stop_request.thread_id = t.thread_id AND stop_request.run_id = blocked.run_id AND stop_request.type = 'run_interrupt_request' ) AS blocking_run_stop_requested, EXISTS ( SELECT 1 FROM orchestration_v2_projection_messages wake WHERE wake.message_id = json_extract(blocked.payload_json, '$.userMessageId') AND json_extract(wake.payload_json, '$.notification.source.kind') IN ('background_task', 'background_command', 'command', 'monitor') AND json_extract(wake.payload_json, '$.notification.source.work') IS NULL AND json_extract(wake.payload_json, '$.notification.outcome') = 'updated' AND json_extract(wake.payload_json, '$.delegatedCompletion') IS NULL ) AND NOT EXISTS ( SELECT 1 FROM orchestration_v2_projection_messages steer WHERE steer.run_id = blocked.run_id AND json_extract(steer.payload_json, '$.createdBy') = 'user' ) AS blocking_run_background_wake, ( SELECT item.payload_json FROM orchestration_v2_projection_turn_items item WHERE item.thread_id = t.thread_id AND item.run_id = blocked.run_id AND item.type = 'error' AND item.status = 'failed' AND item.node_id IS json_extract(blocked.payload_json, '$.rootNodeId') ORDER BY item.updated_at DESC, item.ordinal DESC, item.turn_item_id DESC LIMIT 1 ) AS blocking_failure_payload_json, ( SELECT request.payload_json FROM orchestration_v2_projection_runtime_requests request WHERE request.thread_id = t.thread_id AND request.status = 'pending' ORDER BY (request.kind = 'user_input') ASC, request.created_at DESC, request.runtime_request_id DESC LIMIT 1 ) AS pending_request_payload_json, ( SELECT secret.payload_json -- Keep runs outermost so completed history is never scanned for a pending secret. FROM orchestration_v2_projection_runs r INDEXED BY orchestration_v2_projection_runs_thread_status_idx CROSS JOIN orchestration_v2_projection_turn_items secret INDEXED BY orchestration_v2_projection_turn_items_thread_run_idx WHERE r.thread_id = t.thread_id AND r.status IN ('preparing', 'starting', 'running', 'waiting') AND secret.thread_id = t.thread_id AND secret.run_id = r.run_id AND secret.type = 'secret_request' AND secret.status = 'waiting' ORDER BY secret.updated_at DESC, secret.turn_item_id DESC LIMIT 1 ) AS pending_secret_request_payload_json, ( SELECT link.payload_json FROM orchestration_v2_projection_turn_items link INDEXED BY orchestration_v2_projection_turn_items_waiting_link_idx WHERE link.thread_id = t.thread_id AND link.type = 'link_request' AND link.status = 'waiting' ORDER BY link.updated_at DESC, link.turn_item_id DESC LIMIT 1 ) AS pending_link_request_payload_json, ( SELECT message.updated_at FROM orchestration_v2_projection_messages message WHERE message.thread_id = t.thread_id AND message.role = 'user' AND NOT ( message.role = 'user' AND lower(trim(json_extract(message.payload_json, '$.text'), char(9, 10, 11, 12, 13, 32, 160, 5760, 8192, 8193, 8194, 8195, 8196, 8197, 8198, 8199, 8200, 8201, 8202, 8232, 8233, 8239, 8287, 12288, 65279) )) = ? AND coalesce(json_array_length(message.payload_json, '$.attachments'), 0) = 0 AND ( json_extract(message.payload_json, '$.idleCompaction') IS NOT NULL OR ( json_extract(message.payload_json, '$.createdBy') = 'agent' AND json_extract(message.payload_json, '$.creationSource') = 'server' ) ) ) ORDER BY message.updated_at DESC, message.message_id DESC LIMIT 1 ) AS latest_user_message_at, ( SELECT message.updated_at FROM orchestration_v2_projection_messages message WHERE message.thread_id = t.thread_id AND message.role = 'user' AND json_extract(message.payload_json, '$.createdBy') = 'user' AND NOT ( message.role = 'user' AND lower(trim(json_extract(message.payload_json, '$.text'), char(9, 10, 11, 12, 13, 32, 160, 5760, 8192, 8193, 8194, 8195, 8196, 8197, 8198, 8199, 8200, 8201, 8202, 8232, 8233, 8239, 8287, 12288, 65279) )) = ? AND coalesce(json_array_length(message.payload_json, '$.attachments'), 0) = 0 AND ( json_extract(message.payload_json, '$.idleCompaction') IS NOT NULL OR ( json_extract(message.payload_json, '$.createdBy') = 'agent' AND json_extract(message.payload_json, '$.creationSource') = 'server' ) ) ) ORDER BY message.updated_at DESC, message.message_id DESC LIMIT 1 ) AS latest_user_authored_message_at, EXISTS ( SELECT 1 FROM orchestration_v2_projection_plans plan WHERE plan.thread_id = t.thread_id AND plan.kind = 'proposed_plan' AND plan.status = 'active' ) AS has_actionable_proposed_plan FROM orchestration_v2_projection_threads t LEFT JOIN orchestration_v2_projection_runs active ON active.run_id = ( SELECT r.run_id FROM orchestration_v2_projection_runs r INDEXED BY orchestration_v2_projection_runs_thread_status_idx WHERE r.thread_id = t.thread_id AND r.status IN ('preparing', 'starting', 'running') AND NOT ( r.status <> 'failed' AND EXISTS ( SELECT 1 FROM orchestration_v2_projection_messages prompt WHERE prompt.message_id = json_extract(r.payload_json, '$.userMessageId') AND ( prompt.role = 'user' AND lower(trim(json_extract(prompt.payload_json, '$.text'), char(9, 10, 11, 12, 13, 32, 160, 5760, 8192, 8193, 8194, 8195, 8196, 8197, 8198, 8199, 8200, 8201, 8202, 8232, 8233, 8239, 8287, 12288, 65279) )) = ? AND coalesce(json_array_length(prompt.payload_json, '$.attachments'), 0) = 0 AND ( json_extract(prompt.payload_json, '$.idleCompaction') IS NOT NULL OR ( json_extract(prompt.payload_json, '$.createdBy') = 'agent' AND json_extract(prompt.payload_json, '$.creationSource') = 'server' ) ) ) ) AND NOT EXISTS ( SELECT 1 FROM orchestration_v2_projection_turn_items compaction WHERE compaction.thread_id = r.thread_id AND compaction.run_id = r.run_id AND compaction.type = 'compaction' AND compaction.status = 'failed' ) ) ORDER BY r.ordinal DESC, r.run_id DESC LIMIT 1 ) LEFT JOIN orchestration_v2_projection_runs presented ON presented.run_id = ( SELECT candidate.run_id FROM orchestration_v2_projection_runs candidate WHERE candidate.thread_id = t.thread_id AND NOT ( candidate.status = 'queued' AND json_extract(candidate.payload_json, '$.queueHeld') IS 1 ) AND NOT ( candidate.status = 'cancelled' AND json_extract(candidate.payload_json, '$.startedAt') IS NULL AND candidate.completed_at IS NOT NULL AND EXISTS ( SELECT 1 FROM orchestration_v2_projection_runs earlier WHERE earlier.thread_id = candidate.thread_id AND earlier.ordinal < candidate.ordinal AND earlier.status <> 'queued' AND NOT ( earlier.status <> 'failed' AND EXISTS ( SELECT 1 FROM orchestration_v2_projection_messages prompt WHERE prompt.message_id = json_extract(earlier.payload_json, '$.userMessageId') AND ( prompt.role = 'user' AND lower(trim(json_extract(prompt.payload_json, '$.text'), char(9, 10, 11, 12, 13, 32, 160, 5760, 8192, 8193, 8194, 8195, 8196, 8197, 8198, 8199, 8200, 8201, 8202, 8232, 8233, 8239, 8287, 12288, 65279) )) = ? AND coalesce(json_array_length(prompt.payload_json, '$.attachments'), 0) = 0 AND ( json_extract(prompt.payload_json, '$.idleCompaction') IS NOT NULL OR ( json_extract(prompt.payload_json, '$.createdBy') = 'agent' AND json_extract(prompt.payload_json, '$.creationSource') = 'server' ) ) ) ) AND NOT EXISTS ( SELECT 1 FROM orchestration_v2_projection_turn_items compaction WHERE compaction.thread_id = earlier.thread_id AND compaction.run_id = earlier.run_id AND compaction.type = 'compaction' AND compaction.status = 'failed' ) ) AND ( earlier.completed_at IS NULL OR julianday(earlier.completed_at) >= julianday(candidate.completed_at) ) ) ) AND NOT ( candidate.status <> 'failed' AND EXISTS ( SELECT 1 FROM orchestration_v2_projection_messages prompt WHERE prompt.message_id = json_extract(candidate.payload_json, '$.userMessageId') AND ( prompt.role = 'user' AND lower(trim(json_extract(prompt.payload_json, '$.text'), char(9, 10, 11, 12, 13, 32, 160, 5760, 8192, 8193, 8194, 8195, 8196, 8197, 8198, 8199, 8200, 8201, 8202, 8232, 8233, 8239, 8287, 12288, 65279) )) = ? AND coalesce(json_array_length(prompt.payload_json, '$.attachments'), 0) = 0 AND ( json_extract(prompt.payload_json, '$.idleCompaction') IS NOT NULL OR ( json_extract(prompt.payload_json, '$.createdBy') = 'agent' AND json_extract(prompt.payload_json, '$.creationSource') = 'server' ) ) ) ) AND NOT EXISTS ( SELECT 1 FROM orchestration_v2_projection_turn_items compaction WHERE compaction.thread_id = candidate.thread_id AND compaction.run_id = candidate.run_id AND compaction.type = 'compaction' AND compaction.status = 'failed' ) ) ORDER BY candidate.ordinal DESC, candidate.run_id DESC LIMIT 1 ) LEFT JOIN orchestration_v2_projection_runs blocked ON blocked.run_id = ( SELECT candidate.run_id FROM orchestration_v2_projection_runs candidate WHERE candidate.thread_id = t.thread_id AND candidate.status <> 'queued' AND NOT ( candidate.status = 'cancelled' AND json_extract(candidate.payload_json, '$.startedAt') IS NULL ) -- latestExecutedRun: the run that ended last (runRanAfter). ORDER BY candidate.completed_at IS NULL DESC, candidate.completed_at DESC, candidate.ordinal DESC, candidate.run_id DESC LIMIT 1 ) AND blocked.status = 'failed' WHERE t.deleted_at IS NULL AND t.thread_id = ? ORDER BY t.updated_at ASC, t.thread_id ASC",
    "rows": 1
  },
  {
    "label": "list",
    "sql": "SELECT counts.thread_id, SUM(CASE WHEN counts.run_key = '' OR r.status <> 'rolled_back' THEN counts.item_count - CASE WHEN r.status IN ('queued', 'cancelled') THEN ( SELECT COUNT(*) FROM orchestration_v2_projection_turn_items held INDEXED BY orchestration_v2_projection_turn_items_queued_input_idx WHERE held.run_id = counts.run_key AND held.type = 'user_message' AND json_extract(held.payload_json, '$.inputIntent') = 'queued_turn' ) ELSE 0 END ELSE 0 END) AS item_count, SUM(CASE WHEN counts.run_key = '' THEN counts.item_count ELSE 0 END) AS runless_item_count FROM orchestration_v2_projection_turn_item_counts counts LEFT JOIN orchestration_v2_projection_runs r ON r.run_id = counts.run_key WHERE counts.thread_id IN (SELECT value FROM json_each(?)) GROUP BY counts.thread_id",
    "rows": 1
  },
  {
    "label": "list",
    "sql": "SELECT thread_id, payload_json FROM orchestration_v2_projection_provider_threads WHERE thread_id IN (SELECT value FROM json_each(?))",
    "rows": 1
  },
  {
    "label": "list",
    "sql": "SELECT i.thread_id, i.payload_json, p.provider_instance_id AS owning_provider_instance_id FROM orchestration_v2_projection_turn_items i LEFT JOIN orchestration_v2_projection_provider_threads p ON p.provider_thread_id = i.provider_thread_id LEFT JOIN orchestration_v2_projection_runs r ON r.run_id = i.run_id WHERE i.type IN ('command_execution', 'dynamic_tool', 'subagent') AND i.status NOT IN ('completed', 'interrupted', 'failed', 'cancelled') AND (i.run_id IS NULL OR r.status <> 'rolled_back') AND i.thread_id IN (SELECT value FROM json_each(?)) -- Preserve the pending-item partial index; read other work only from running runs. UNION ALL SELECT i.thread_id, i.payload_json, NULL AS owning_provider_instance_id FROM orchestration_v2_projection_turn_items i WHERE i.run_id IN (SELECT value FROM json_each(?)) AND i.type IN ('file_change', 'file_search', 'web_search', 'compaction') AND i.status IN ('running', 'waiting')",
    "rows": 0
  },
  {
    "label": "list",
    "sql": "SELECT payload_json FROM orchestration_v2_projection_threads WHERE thread_id = ? LIMIT 1",
    "rows": 1
  },
  {
    "label": "list",
    "sql": "SELECT payload_json FROM orchestration_v2_projection_runs WHERE thread_id = ? ORDER BY ordinal ASC",
    "rows": 107
  },
  {
    "label": "list",
    "sql": "SELECT payload_json FROM orchestration_v2_projection_messages WHERE thread_id = ? ORDER BY created_at ASC, message_id ASC",
    "rows": 213
  },
  {
    "label": "list",
    "sql": "SELECT json_extract(payload_json, '$.messageId') AS message_id, MIN(ordinal) AS ordinal FROM orchestration_v2_projection_turn_items WHERE thread_id = ? AND type IN ('user_message', 'assistant_message') GROUP BY json_extract(payload_json, '$.messageId')",
    "rows": 210
  },
  {
    "label": "read",
    "sql": "SELECT t.thread_id, t.payload_json, CASE WHEN json_extract(t.payload_json, '$.forkedFrom.type') = 'run' AND json_extract(t.payload_json, '$.lineage.sideChat') IS NOT 1 THEN json_extract(t.payload_json, '$.forkedFrom.threadId') ELSE NULL END AS forked_from_run_source_thread_id, presented.run_id AS latest_run_id, presented.status AS latest_run_status, presented.requested_at AS latest_run_requested_at, json_extract(presented.payload_json, '$.startedAt') AS latest_run_started_at, presented.completed_at AS latest_run_completed_at, EXISTS ( SELECT 1 FROM orchestration_v2_projection_turn_items stop_request WHERE stop_request.thread_id = t.thread_id AND stop_request.run_id = presented.run_id AND stop_request.type = 'run_interrupt_request' ) AS latest_run_stop_requested, EXISTS ( SELECT 1 FROM orchestration_v2_projection_messages wake WHERE wake.message_id = json_extract(presented.payload_json, '$.userMessageId') AND json_extract(wake.payload_json, '$.notification.source.kind') IN ('background_task', 'background_command', 'command', 'monitor') AND json_extract(wake.payload_json, '$.notification.source.work') IS NULL AND json_extract(wake.payload_json, '$.notification.outcome') = 'updated' AND json_extract(wake.payload_json, '$.delegatedCompletion') IS NULL ) AND NOT EXISTS ( SELECT 1 FROM orchestration_v2_projection_messages steer WHERE steer.run_id = presented.run_id AND json_extract(steer.payload_json, '$.createdBy') = 'user' ) AS latest_run_background_wake, active.run_id AS active_run_id, active.status AS active_run_status, ( SELECT r.status -- Read active runs by status. Walking the ordinal index to skip this sort -- reads every run of a thread with none active. FROM orchestration_v2_projection_runs r INDEXED BY orchestration_v2_projection_runs_thread_status_idx WHERE r.thread_id = t.thread_id AND r.status IN ('preparing', 'starting', 'running', 'waiting') AND NOT ( r.status <> 'failed' AND EXISTS ( SELECT 1 FROM orchestration_v2_projection_messages prompt WHERE prompt.message_id = json_extract(r.payload_json, '$.userMessageId') AND ( prompt.role = 'user' AND lower(trim(json_extract(prompt.payload_json, '$.text'), char(9, 10, 11, 12, 13, 32, 160, 5760, 8192, 8193, 8194, 8195, 8196, 8197, 8198, 8199, 8200, 8201, 8202, 8232, 8233, 8239, 8287, 12288, 65279) )) = ? AND coalesce(json_array_length(prompt.payload_json, '$.attachments'), 0) = 0 AND ( json_extract(prompt.payload_json, '$.idleCompaction') IS NOT NULL OR ( json_extract(prompt.payload_json, '$.createdBy') = 'agent' AND json_extract(prompt.payload_json, '$.creationSource') = 'server' ) ) ) ) AND NOT EXISTS ( SELECT 1 FROM orchestration_v2_projection_turn_items compaction WHERE compaction.thread_id = r.thread_id AND compaction.run_id = r.run_id AND compaction.type = 'compaction' AND compaction.status = 'failed' ) ) ORDER BY r.ordinal DESC, r.run_id DESC LIMIT 1 ) AS activity_run_status, ( -- Mirrors orchestrationV2RunWorkStartedAt. SELECT COALESCE( json_extract(r.payload_json, '$.workStartedAt'), json_extract(r.payload_json, '$.startedAt'), r.requested_at ) FROM orchestration_v2_projection_runs r INDEXED BY orchestration_v2_projection_runs_thread_status_idx WHERE r.thread_id = t.thread_id AND r.status IN ('preparing', 'starting', 'running', 'waiting') AND NOT ( r.status <> 'failed' AND EXISTS ( SELECT 1 FROM orchestration_v2_projection_messages prompt WHERE prompt.message_id = json_extract(r.payload_json, '$.userMessageId') AND ( prompt.role = 'user' AND lower(trim(json_extract(prompt.payload_json, '$.text'), char(9, 10, 11, 12, 13, 32, 160, 5760, 8192, 8193, 8194, 8195, 8196, 8197, 8198, 8199, 8200, 8201, 8202, 8232, 8233, 8239, 8287, 12288, 65279) )) = ? AND coalesce(json_array_length(prompt.payload_json, '$.attachments'), 0) = 0 AND ( json_extract(prompt.payload_json, '$.idleCompaction') IS NOT NULL OR ( json_extract(prompt.payload_json, '$.createdBy') = 'agent' AND json_extract(prompt.payload_json, '$.creationSource') = 'server' ) ) ) ) AND NOT EXISTS ( SELECT 1 FROM orchestration_v2_projection_turn_items compaction WHERE compaction.thread_id = r.thread_id AND compaction.run_id = r.run_id AND compaction.type = 'compaction' AND compaction.status = 'failed' ) ) ORDER BY r.ordinal DESC, r.run_id DESC LIMIT 1 ) AS activity_run_started_at, ( SELECT json_extract(session.payload_json, '$.lastError') -- Keep bindings outermost: one provider instance can hold every session. FROM orchestration_v2_projection_provider_session_bindings binding CROSS JOIN orchestration_v2_projection_provider_sessions session ON session.provider_session_id = binding.provider_session_id WHERE binding.thread_id = t.thread_id AND session.provider_instance_id = t.provider_instance_id ORDER BY session.updated_at DESC, session.provider_session_id DESC LIMIT 1 ) AS last_error, ( SELECT item.payload_json FROM orchestration_v2_projection_turn_items item INNER JOIN orchestration_v2_projection_runs r ON r.run_id = item.run_id WHERE r.run_id = presented.run_id AND r.status = 'failed' AND item.thread_id = t.thread_id AND item.type = 'error' AND item.status = 'failed' AND item.node_id IS json_extract(r.payload_json, '$.rootNodeId') ORDER BY item.updated_at DESC, item.ordinal DESC, item.turn_item_id DESC LIMIT 1 ) AS terminal_failure_payload_json, blocked.run_id AS blocking_run_id, blocked.requested_at AS blocking_run_requested_at, json_extract(blocked.payload_json, '$.startedAt') AS blocking_run_started_at, blocked.completed_at AS blocking_run_completed_at, EXISTS ( SELECT 1 FROM orchestration_v2_projection_turn_items stop_request WHERE stop_request.thread_id = t.thread_id AND stop_request.run_id = blocked.run_id AND stop_request.type = 'run_interrupt_request' ) AS blocking_run_stop_requested, EXISTS ( SELECT 1 FROM orchestration_v2_projection_messages wake WHERE wake.message_id = json_extract(blocked.payload_json, '$.userMessageId') AND json_extract(wake.payload_json, '$.notification.source.kind') IN ('background_task', 'background_command', 'command', 'monitor') AND json_extract(wake.payload_json, '$.notification.source.work') IS NULL AND json_extract(wake.payload_json, '$.notification.outcome') = 'updated' AND json_extract(wake.payload_json, '$.delegatedCompletion') IS NULL ) AND NOT EXISTS ( SELECT 1 FROM orchestration_v2_projection_messages steer WHERE steer.run_id = blocked.run_id AND json_extract(steer.payload_json, '$.createdBy') = 'user' ) AS blocking_run_background_wake, ( SELECT item.payload_json FROM orchestration_v2_projection_turn_items item WHERE item.thread_id = t.thread_id AND item.run_id = blocked.run_id AND item.type = 'error' AND item.status = 'failed' AND item.node_id IS json_extract(blocked.payload_json, '$.rootNodeId') ORDER BY item.updated_at DESC, item.ordinal DESC, item.turn_item_id DESC LIMIT 1 ) AS blocking_failure_payload_json, ( SELECT request.payload_json FROM orchestration_v2_projection_runtime_requests request WHERE request.thread_id = t.thread_id AND request.status = 'pending' ORDER BY (request.kind = 'user_input') ASC, request.created_at DESC, request.runtime_request_id DESC LIMIT 1 ) AS pending_request_payload_json, ( SELECT secret.payload_json -- Keep runs outermost so completed history is never scanned for a pending secret. FROM orchestration_v2_projection_runs r INDEXED BY orchestration_v2_projection_runs_thread_status_idx CROSS JOIN orchestration_v2_projection_turn_items secret INDEXED BY orchestration_v2_projection_turn_items_thread_run_idx WHERE r.thread_id = t.thread_id AND r.status IN ('preparing', 'starting', 'running', 'waiting') AND secret.thread_id = t.thread_id AND secret.run_id = r.run_id AND secret.type = 'secret_request' AND secret.status = 'waiting' ORDER BY secret.updated_at DESC, secret.turn_item_id DESC LIMIT 1 ) AS pending_secret_request_payload_json, ( SELECT link.payload_json FROM orchestration_v2_projection_turn_items link INDEXED BY orchestration_v2_projection_turn_items_waiting_link_idx WHERE link.thread_id = t.thread_id AND link.type = 'link_request' AND link.status = 'waiting' ORDER BY link.updated_at DESC, link.turn_item_id DESC LIMIT 1 ) AS pending_link_request_payload_json, ( SELECT message.updated_at FROM orchestration_v2_projection_messages message WHERE message.thread_id = t.thread_id AND message.role = 'user' AND NOT ( message.role = 'user' AND lower(trim(json_extract(message.payload_json, '$.text'), char(9, 10, 11, 12, 13, 32, 160, 5760, 8192, 8193, 8194, 8195, 8196, 8197, 8198, 8199, 8200, 8201, 8202, 8232, 8233, 8239, 8287, 12288, 65279) )) = ? AND coalesce(json_array_length(message.payload_json, '$.attachments'), 0) = 0 AND ( json_extract(message.payload_json, '$.idleCompaction') IS NOT NULL OR ( json_extract(message.payload_json, '$.createdBy') = 'agent' AND json_extract(message.payload_json, '$.creationSource') = 'server' ) ) ) ORDER BY message.updated_at DESC, message.message_id DESC LIMIT 1 ) AS latest_user_message_at, ( SELECT message.updated_at FROM orchestration_v2_projection_messages message WHERE message.thread_id = t.thread_id AND message.role = 'user' AND json_extract(message.payload_json, '$.createdBy') = 'user' AND NOT ( message.role = 'user' AND lower(trim(json_extract(message.payload_json, '$.text'), char(9, 10, 11, 12, 13, 32, 160, 5760, 8192, 8193, 8194, 8195, 8196, 8197, 8198, 8199, 8200, 8201, 8202, 8232, 8233, 8239, 8287, 12288, 65279) )) = ? AND coalesce(json_array_length(message.payload_json, '$.attachments'), 0) = 0 AND ( json_extract(message.payload_json, '$.idleCompaction') IS NOT NULL OR ( json_extract(message.payload_json, '$.createdBy') = 'agent' AND json_extract(message.payload_json, '$.creationSource') = 'server' ) ) ) ORDER BY message.updated_at DESC, message.message_id DESC LIMIT 1 ) AS latest_user_authored_message_at, EXISTS ( SELECT 1 FROM orchestration_v2_projection_plans plan WHERE plan.thread_id = t.thread_id AND plan.kind = 'proposed_plan' AND plan.status = 'active' ) AS has_actionable_proposed_plan FROM orchestration_v2_projection_threads t LEFT JOIN orchestration_v2_projection_runs active ON active.run_id = ( SELECT r.run_id FROM orchestration_v2_projection_runs r INDEXED BY orchestration_v2_projection_runs_thread_status_idx WHERE r.thread_id = t.thread_id AND r.status IN ('preparing', 'starting', 'running') AND NOT ( r.status <> 'failed' AND EXISTS ( SELECT 1 FROM orchestration_v2_projection_messages prompt WHERE prompt.message_id = json_extract(r.payload_json, '$.userMessageId') AND ( prompt.role = 'user' AND lower(trim(json_extract(prompt.payload_json, '$.text'), char(9, 10, 11, 12, 13, 32, 160, 5760, 8192, 8193, 8194, 8195, 8196, 8197, 8198, 8199, 8200, 8201, 8202, 8232, 8233, 8239, 8287, 12288, 65279) )) = ? AND coalesce(json_array_length(prompt.payload_json, '$.attachments'), 0) = 0 AND ( json_extract(prompt.payload_json, '$.idleCompaction') IS NOT NULL OR ( json_extract(prompt.payload_json, '$.createdBy') = 'agent' AND json_extract(prompt.payload_json, '$.creationSource') = 'server' ) ) ) ) AND NOT EXISTS ( SELECT 1 FROM orchestration_v2_projection_turn_items compaction WHERE compaction.thread_id = r.thread_id AND compaction.run_id = r.run_id AND compaction.type = 'compaction' AND compaction.status = 'failed' ) ) ORDER BY r.ordinal DESC, r.run_id DESC LIMIT 1 ) LEFT JOIN orchestration_v2_projection_runs presented ON presented.run_id = ( SELECT candidate.run_id FROM orchestration_v2_projection_runs candidate WHERE candidate.thread_id = t.thread_id AND NOT ( candidate.status = 'queued' AND json_extract(candidate.payload_json, '$.queueHeld') IS 1 ) AND NOT ( candidate.status = 'cancelled' AND json_extract(candidate.payload_json, '$.startedAt') IS NULL AND candidate.completed_at IS NOT NULL AND EXISTS ( SELECT 1 FROM orchestration_v2_projection_runs earlier WHERE earlier.thread_id = candidate.thread_id AND earlier.ordinal < candidate.ordinal AND earlier.status <> 'queued' AND NOT ( earlier.status <> 'failed' AND EXISTS ( SELECT 1 FROM orchestration_v2_projection_messages prompt WHERE prompt.message_id = json_extract(earlier.payload_json, '$.userMessageId') AND ( prompt.role = 'user' AND lower(trim(json_extract(prompt.payload_json, '$.text'), char(9, 10, 11, 12, 13, 32, 160, 5760, 8192, 8193, 8194, 8195, 8196, 8197, 8198, 8199, 8200, 8201, 8202, 8232, 8233, 8239, 8287, 12288, 65279) )) = ? AND coalesce(json_array_length(prompt.payload_json, '$.attachments'), 0) = 0 AND ( json_extract(prompt.payload_json, '$.idleCompaction') IS NOT NULL OR ( json_extract(prompt.payload_json, '$.createdBy') = 'agent' AND json_extract(prompt.payload_json, '$.creationSource') = 'server' ) ) ) ) AND NOT EXISTS ( SELECT 1 FROM orchestration_v2_projection_turn_items compaction WHERE compaction.thread_id = earlier.thread_id AND compaction.run_id = earlier.run_id AND compaction.type = 'compaction' AND compaction.status = 'failed' ) ) AND ( earlier.completed_at IS NULL OR julianday(earlier.completed_at) >= julianday(candidate.completed_at) ) ) ) AND NOT ( candidate.status <> 'failed' AND EXISTS ( SELECT 1 FROM orchestration_v2_projection_messages prompt WHERE prompt.message_id = json_extract(candidate.payload_json, '$.userMessageId') AND ( prompt.role = 'user' AND lower(trim(json_extract(prompt.payload_json, '$.text'), char(9, 10, 11, 12, 13, 32, 160, 5760, 8192, 8193, 8194, 8195, 8196, 8197, 8198, 8199, 8200, 8201, 8202, 8232, 8233, 8239, 8287, 12288, 65279) )) = ? AND coalesce(json_array_length(prompt.payload_json, '$.attachments'), 0) = 0 AND ( json_extract(prompt.payload_json, '$.idleCompaction') IS NOT NULL OR ( json_extract(prompt.payload_json, '$.createdBy') = 'agent' AND json_extract(prompt.payload_json, '$.creationSource') = 'server' ) ) ) ) AND NOT EXISTS ( SELECT 1 FROM orchestration_v2_projection_turn_items compaction WHERE compaction.thread_id = candidate.thread_id AND compaction.run_id = candidate.run_id AND compaction.type = 'compaction' AND compaction.status = 'failed' ) ) ORDER BY candidate.ordinal DESC, candidate.run_id DESC LIMIT 1 ) LEFT JOIN orchestration_v2_projection_runs blocked ON blocked.run_id = ( SELECT candidate.run_id FROM orchestration_v2_projection_runs candidate WHERE candidate.thread_id = t.thread_id AND candidate.status <> 'queued' AND NOT ( candidate.status = 'cancelled' AND json_extract(candidate.payload_json, '$.startedAt') IS NULL ) -- latestExecutedRun: the run that ended last (runRanAfter). ORDER BY candidate.completed_at IS NULL DESC, candidate.completed_at DESC, candidate.ordinal DESC, candidate.run_id DESC LIMIT 1 ) AND blocked.status = 'failed' WHERE t.deleted_at IS NULL AND t.thread_id = ? ORDER BY t.updated_at ASC, t.thread_id ASC",
    "rows": 1
  },
  {
    "label": "read",
    "sql": "SELECT counts.thread_id, SUM(CASE WHEN counts.run_key = '' OR r.status <> 'rolled_back' THEN counts.item_count - CASE WHEN r.status IN ('queued', 'cancelled') THEN ( SELECT COUNT(*) FROM orchestration_v2_projection_turn_items held INDEXED BY orchestration_v2_projection_turn_items_queued_input_idx WHERE held.run_id = counts.run_key AND held.type = 'user_message' AND json_extract(held.payload_json, '$.inputIntent') = 'queued_turn' ) ELSE 0 END ELSE 0 END) AS item_count, SUM(CASE WHEN counts.run_key = '' THEN counts.item_count ELSE 0 END) AS runless_item_count FROM orchestration_v2_projection_turn_item_counts counts LEFT JOIN orchestration_v2_projection_runs r ON r.run_id = counts.run_key WHERE counts.thread_id IN (SELECT value FROM json_each(?)) GROUP BY counts.thread_id",
    "rows": 1
  },
  {
    "label": "read",
    "sql": "SELECT thread_id, payload_json FROM orchestration_v2_projection_provider_threads WHERE thread_id IN (SELECT value FROM json_each(?))",
    "rows": 1
  },
  {
    "label": "read",
    "sql": "SELECT i.thread_id, i.payload_json, p.provider_instance_id AS owning_provider_instance_id FROM orchestration_v2_projection_turn_items i LEFT JOIN orchestration_v2_projection_provider_threads p ON p.provider_thread_id = i.provider_thread_id LEFT JOIN orchestration_v2_projection_runs r ON r.run_id = i.run_id WHERE i.type IN ('command_execution', 'dynamic_tool', 'subagent') AND i.status NOT IN ('completed', 'interrupted', 'failed', 'cancelled') AND (i.run_id IS NULL OR r.status <> 'rolled_back') AND i.thread_id IN (SELECT value FROM json_each(?)) -- Preserve the pending-item partial index; read other work only from running runs. UNION ALL SELECT i.thread_id, i.payload_json, NULL AS owning_provider_instance_id FROM orchestration_v2_projection_turn_items i WHERE i.run_id IN (SELECT value FROM json_each(?)) AND i.type IN ('file_change', 'file_search', 'web_search', 'compaction') AND i.status IN ('running', 'waiting')",
    "rows": 0
  },
  {
    "label": "read",
    "sql": "SELECT t.thread_id, t.payload_json, CASE WHEN json_extract(t.payload_json, '$.forkedFrom.type') = 'run' AND json_extract(t.payload_json, '$.lineage.sideChat') IS NOT 1 THEN json_extract(t.payload_json, '$.forkedFrom.threadId') ELSE NULL END AS forked_from_run_source_thread_id, presented.run_id AS latest_run_id, presented.status AS latest_run_status, presented.requested_at AS latest_run_requested_at, json_extract(presented.payload_json, '$.startedAt') AS latest_run_started_at, presented.completed_at AS latest_run_completed_at, EXISTS ( SELECT 1 FROM orchestration_v2_projection_turn_items stop_request WHERE stop_request.thread_id = t.thread_id AND stop_request.run_id = presented.run_id AND stop_request.type = 'run_interrupt_request' ) AS latest_run_stop_requested, EXISTS ( SELECT 1 FROM orchestration_v2_projection_messages wake WHERE wake.message_id = json_extract(presented.payload_json, '$.userMessageId') AND json_extract(wake.payload_json, '$.notification.source.kind') IN ('background_task', 'background_command', 'command', 'monitor') AND json_extract(wake.payload_json, '$.notification.source.work') IS NULL AND json_extract(wake.payload_json, '$.notification.outcome') = 'updated' AND json_extract(wake.payload_json, '$.delegatedCompletion') IS NULL ) AND NOT EXISTS ( SELECT 1 FROM orchestration_v2_projection_messages steer WHERE steer.run_id = presented.run_id AND json_extract(steer.payload_json, '$.createdBy') = 'user' ) AS latest_run_background_wake, active.run_id AS active_run_id, active.status AS active_run_status, ( SELECT r.status -- Read active runs by status. Walking the ordinal index to skip this sort -- reads every run of a thread with none active. FROM orchestration_v2_projection_runs r INDEXED BY orchestration_v2_projection_runs_thread_status_idx WHERE r.thread_id = t.thread_id AND r.status IN ('preparing', 'starting', 'running', 'waiting') AND NOT ( r.status <> 'failed' AND EXISTS ( SELECT 1 FROM orchestration_v2_projection_messages prompt WHERE prompt.message_id = json_extract(r.payload_json, '$.userMessageId') AND ( prompt.role = 'user' AND lower(trim(json_extract(prompt.payload_json, '$.text'), char(9, 10, 11, 12, 13, 32, 160, 5760, 8192, 8193, 8194, 8195, 8196, 8197, 8198, 8199, 8200, 8201, 8202, 8232, 8233, 8239, 8287, 12288, 65279) )) = ? AND coalesce(json_array_length(prompt.payload_json, '$.attachments'), 0) = 0 AND ( json_extract(prompt.payload_json, '$.idleCompaction') IS NOT NULL OR ( json_extract(prompt.payload_json, '$.createdBy') = 'agent' AND json_extract(prompt.payload_json, '$.creationSource') = 'server' ) ) ) ) AND NOT EXISTS ( SELECT 1 FROM orchestration_v2_projection_turn_items compaction WHERE compaction.thread_id = r.thread_id AND compaction.run_id = r.run_id AND compaction.type = 'compaction' AND compaction.status = 'failed' ) ) ORDER BY r.ordinal DESC, r.run_id DESC LIMIT 1 ) AS activity_run_status, ( -- Mirrors orchestrationV2RunWorkStartedAt. SELECT COALESCE( json_extract(r.payload_json, '$.workStartedAt'), json_extract(r.payload_json, '$.startedAt'), r.requested_at ) FROM orchestration_v2_projection_runs r INDEXED BY orchestration_v2_projection_runs_thread_status_idx WHERE r.thread_id = t.thread_id AND r.status IN ('preparing', 'starting', 'running', 'waiting') AND NOT ( r.status <> 'failed' AND EXISTS ( SELECT 1 FROM orchestration_v2_projection_messages prompt WHERE prompt.message_id = json_extract(r.payload_json, '$.userMessageId') AND ( prompt.role = 'user' AND lower(trim(json_extract(prompt.payload_json, '$.text'), char(9, 10, 11, 12, 13, 32, 160, 5760, 8192, 8193, 8194, 8195, 8196, 8197, 8198, 8199, 8200, 8201, 8202, 8232, 8233, 8239, 8287, 12288, 65279) )) = ? AND coalesce(json_array_length(prompt.payload_json, '$.attachments'), 0) = 0 AND ( json_extract(prompt.payload_json, '$.idleCompaction') IS NOT NULL OR ( json_extract(prompt.payload_json, '$.createdBy') = 'agent' AND json_extract(prompt.payload_json, '$.creationSource') = 'server' ) ) ) ) AND NOT EXISTS ( SELECT 1 FROM orchestration_v2_projection_turn_items compaction WHERE compaction.thread_id = r.thread_id AND compaction.run_id = r.run_id AND compaction.type = 'compaction' AND compaction.status = 'failed' ) ) ORDER BY r.ordinal DESC, r.run_id DESC LIMIT 1 ) AS activity_run_started_at, ( SELECT json_extract(session.payload_json, '$.lastError') -- Keep bindings outermost: one provider instance can hold every session. FROM orchestration_v2_projection_provider_session_bindings binding CROSS JOIN orchestration_v2_projection_provider_sessions session ON session.provider_session_id = binding.provider_session_id WHERE binding.thread_id = t.thread_id AND session.provider_instance_id = t.provider_instance_id ORDER BY session.updated_at DESC, session.provider_session_id DESC LIMIT 1 ) AS last_error, ( SELECT item.payload_json FROM orchestration_v2_projection_turn_items item INNER JOIN orchestration_v2_projection_runs r ON r.run_id = item.run_id WHERE r.run_id = presented.run_id AND r.status = 'failed' AND item.thread_id = t.thread_id AND item.type = 'error' AND item.status = 'failed' AND item.node_id IS json_extract(r.payload_json, '$.rootNodeId') ORDER BY item.updated_at DESC, item.ordinal DESC, item.turn_item_id DESC LIMIT 1 ) AS terminal_failure_payload_json, blocked.run_id AS blocking_run_id, blocked.requested_at AS blocking_run_requested_at, json_extract(blocked.payload_json, '$.startedAt') AS blocking_run_started_at, blocked.completed_at AS blocking_run_completed_at, EXISTS ( SELECT 1 FROM orchestration_v2_projection_turn_items stop_request WHERE stop_request.thread_id = t.thread_id AND stop_request.run_id = blocked.run_id AND stop_request.type = 'run_interrupt_request' ) AS blocking_run_stop_requested, EXISTS ( SELECT 1 FROM orchestration_v2_projection_messages wake WHERE wake.message_id = json_extract(blocked.payload_json, '$.userMessageId') AND json_extract(wake.payload_json, '$.notification.source.kind') IN ('background_task', 'background_command', 'command', 'monitor') AND json_extract(wake.payload_json, '$.notification.source.work') IS NULL AND json_extract(wake.payload_json, '$.notification.outcome') = 'updated' AND json_extract(wake.payload_json, '$.delegatedCompletion') IS NULL ) AND NOT EXISTS ( SELECT 1 FROM orchestration_v2_projection_messages steer WHERE steer.run_id = blocked.run_id AND json_extract(steer.payload_json, '$.createdBy') = 'user' ) AS blocking_run_background_wake, ( SELECT item.payload_json FROM orchestration_v2_projection_turn_items item WHERE item.thread_id = t.thread_id AND item.run_id = blocked.run_id AND item.type = 'error' AND item.status = 'failed' AND item.node_id IS json_extract(blocked.payload_json, '$.rootNodeId') ORDER BY item.updated_at DESC, item.ordinal DESC, item.turn_item_id DESC LIMIT 1 ) AS blocking_failure_payload_json, ( SELECT request.payload_json FROM orchestration_v2_projection_runtime_requests request WHERE request.thread_id = t.thread_id AND request.status = 'pending' ORDER BY (request.kind = 'user_input') ASC, request.created_at DESC, request.runtime_request_id DESC LIMIT 1 ) AS pending_request_payload_json, ( SELECT secret.payload_json -- Keep runs outermost so completed history is never scanned for a pending secret. FROM orchestration_v2_projection_runs r INDEXED BY orchestration_v2_projection_runs_thread_status_idx CROSS JOIN orchestration_v2_projection_turn_items secret INDEXED BY orchestration_v2_projection_turn_items_thread_run_idx WHERE r.thread_id = t.thread_id AND r.status IN ('preparing', 'starting', 'running', 'waiting') AND secret.thread_id = t.thread_id AND secret.run_id = r.run_id AND secret.type = 'secret_request' AND secret.status = 'waiting' ORDER BY secret.updated_at DESC, secret.turn_item_id DESC LIMIT 1 ) AS pending_secret_request_payload_json, ( SELECT link.payload_json FROM orchestration_v2_projection_turn_items link INDEXED BY orchestration_v2_projection_turn_items_waiting_link_idx WHERE link.thread_id = t.thread_id AND link.type = 'link_request' AND link.status = 'waiting' ORDER BY link.updated_at DESC, link.turn_item_id DESC LIMIT 1 ) AS pending_link_request_payload_json, ( SELECT message.updated_at FROM orchestration_v2_projection_messages message WHERE message.thread_id = t.thread_id AND message.role = 'user' AND NOT ( message.role = 'user' AND lower(trim(json_extract(message.payload_json, '$.text'), char(9, 10, 11, 12, 13, 32, 160, 5760, 8192, 8193, 8194, 8195, 8196, 8197, 8198, 8199, 8200, 8201, 8202, 8232, 8233, 8239, 8287, 12288, 65279) )) = ? AND coalesce(json_array_length(message.payload_json, '$.attachments'), 0) = 0 AND ( json_extract(message.payload_json, '$.idleCompaction') IS NOT NULL OR ( json_extract(message.payload_json, '$.createdBy') = 'agent' AND json_extract(message.payload_json, '$.creationSource') = 'server' ) ) ) ORDER BY message.updated_at DESC, message.message_id DESC LIMIT 1 ) AS latest_user_message_at, ( SELECT message.updated_at FROM orchestration_v2_projection_messages message WHERE message.thread_id = t.thread_id AND message.role = 'user' AND json_extract(message.payload_json, '$.createdBy') = 'user' AND NOT ( message.role = 'user' AND lower(trim(json_extract(message.payload_json, '$.text'), char(9, 10, 11, 12, 13, 32, 160, 5760, 8192, 8193, 8194, 8195, 8196, 8197, 8198, 8199, 8200, 8201, 8202, 8232, 8233, 8239, 8287, 12288, 65279) )) = ? AND coalesce(json_array_length(message.payload_json, '$.attachments'), 0) = 0 AND ( json_extract(message.payload_json, '$.idleCompaction') IS NOT NULL OR ( json_extract(message.payload_json, '$.createdBy') = 'agent' AND json_extract(message.payload_json, '$.creationSource') = 'server' ) ) ) ORDER BY message.updated_at DESC, message.message_id DESC LIMIT 1 ) AS latest_user_authored_message_at, EXISTS ( SELECT 1 FROM orchestration_v2_projection_plans plan WHERE plan.thread_id = t.thread_id AND plan.kind = 'proposed_plan' AND plan.status = 'active' ) AS has_actionable_proposed_plan FROM orchestration_v2_projection_threads t LEFT JOIN orchestration_v2_projection_runs active ON active.run_id = ( SELECT r.run_id FROM orchestration_v2_projection_runs r INDEXED BY orchestration_v2_projection_runs_thread_status_idx WHERE r.thread_id = t.thread_id AND r.status IN ('preparing', 'starting', 'running') AND NOT ( r.status <> 'failed' AND EXISTS ( SELECT 1 FROM orchestration_v2_projection_messages prompt WHERE prompt.message_id = json_extract(r.payload_json, '$.userMessageId') AND ( prompt.role = 'user' AND lower(trim(json_extract(prompt.payload_json, '$.text'), char(9, 10, 11, 12, 13, 32, 160, 5760, 8192, 8193, 8194, 8195, 8196, 8197, 8198, 8199, 8200, 8201, 8202, 8232, 8233, 8239, 8287, 12288, 65279) )) = ? AND coalesce(json_array_length(prompt.payload_json, '$.attachments'), 0) = 0 AND ( json_extract(prompt.payload_json, '$.idleCompaction') IS NOT NULL OR ( json_extract(prompt.payload_json, '$.createdBy') = 'agent' AND json_extract(prompt.payload_json, '$.creationSource') = 'server' ) ) ) ) AND NOT EXISTS ( SELECT 1 FROM orchestration_v2_projection_turn_items compaction WHERE compaction.thread_id = r.thread_id AND compaction.run_id = r.run_id AND compaction.type = 'compaction' AND compaction.status = 'failed' ) ) ORDER BY r.ordinal DESC, r.run_id DESC LIMIT 1 ) LEFT JOIN orchestration_v2_projection_runs presented ON presented.run_id = ( SELECT candidate.run_id FROM orchestration_v2_projection_runs candidate WHERE candidate.thread_id = t.thread_id AND NOT ( candidate.status = 'queued' AND json_extract(candidate.payload_json, '$.queueHeld') IS 1 ) AND NOT ( candidate.status = 'cancelled' AND json_extract(candidate.payload_json, '$.startedAt') IS NULL AND candidate.completed_at IS NOT NULL AND EXISTS ( SELECT 1 FROM orchestration_v2_projection_runs earlier WHERE earlier.thread_id = candidate.thread_id AND earlier.ordinal < candidate.ordinal AND earlier.status <> 'queued' AND NOT ( earlier.status <> 'failed' AND EXISTS ( SELECT 1 FROM orchestration_v2_projection_messages prompt WHERE prompt.message_id = json_extract(earlier.payload_json, '$.userMessageId') AND ( prompt.role = 'user' AND lower(trim(json_extract(prompt.payload_json, '$.text'), char(9, 10, 11, 12, 13, 32, 160, 5760, 8192, 8193, 8194, 8195, 8196, 8197, 8198, 8199, 8200, 8201, 8202, 8232, 8233, 8239, 8287, 12288, 65279) )) = ? AND coalesce(json_array_length(prompt.payload_json, '$.attachments'), 0) = 0 AND ( json_extract(prompt.payload_json, '$.idleCompaction') IS NOT NULL OR ( json_extract(prompt.payload_json, '$.createdBy') = 'agent' AND json_extract(prompt.payload_json, '$.creationSource') = 'server' ) ) ) ) AND NOT EXISTS ( SELECT 1 FROM orchestration_v2_projection_turn_items compaction WHERE compaction.thread_id = earlier.thread_id AND compaction.run_id = earlier.run_id AND compaction.type = 'compaction' AND compaction.status = 'failed' ) ) AND ( earlier.completed_at IS NULL OR julianday(earlier.completed_at) >= julianday(candidate.completed_at) ) ) ) AND NOT ( candidate.status <> 'failed' AND EXISTS ( SELECT 1 FROM orchestration_v2_projection_messages prompt WHERE prompt.message_id = json_extract(candidate.payload_json, '$.userMessageId') AND ( prompt.role = 'user' AND lower(trim(json_extract(prompt.payload_json, '$.text'), char(9, 10, 11, 12, 13, 32, 160, 5760, 8192, 8193, 8194, 8195, 8196, 8197, 8198, 8199, 8200, 8201, 8202, 8232, 8233, 8239, 8287, 12288, 65279) )) = ? AND coalesce(json_array_length(prompt.payload_json, '$.attachments'), 0) = 0 AND ( json_extract(prompt.payload_json, '$.idleCompaction') IS NOT NULL OR ( json_extract(prompt.payload_json, '$.createdBy') = 'agent' AND json_extract(prompt.payload_json, '$.creationSource') = 'server' ) ) ) ) AND NOT EXISTS ( SELECT 1 FROM orchestration_v2_projection_turn_items compaction WHERE compaction.thread_id = candidate.thread_id AND compaction.run_id = candidate.run_id AND compaction.type = 'compaction' AND compaction.status = 'failed' ) ) ORDER BY candidate.ordinal DESC, candidate.run_id DESC LIMIT 1 ) LEFT JOIN orchestration_v2_projection_runs blocked ON blocked.run_id = ( SELECT candidate.run_id FROM orchestration_v2_projection_runs candidate WHERE candidate.thread_id = t.thread_id AND candidate.status <> 'queued' AND NOT ( candidate.status = 'cancelled' AND json_extract(candidate.payload_json, '$.startedAt') IS NULL ) -- latestExecutedRun: the run that ended last (runRanAfter). ORDER BY candidate.completed_at IS NULL DESC, candidate.completed_at DESC, candidate.ordinal DESC, candidate.run_id DESC LIMIT 1 ) AND blocked.status = 'failed' WHERE t.deleted_at IS NULL AND t.thread_id = ? ORDER BY t.updated_at ASC, t.thread_id ASC",
    "rows": 1
  },
  {
    "label": "read",
    "sql": "SELECT counts.thread_id, SUM(CASE WHEN counts.run_key = '' OR r.status <> 'rolled_back' THEN counts.item_count - CASE WHEN r.status IN ('queued', 'cancelled') THEN ( SELECT COUNT(*) FROM orchestration_v2_projection_turn_items held INDEXED BY orchestration_v2_projection_turn_items_queued_input_idx WHERE held.run_id = counts.run_key AND held.type = 'user_message' AND json_extract(held.payload_json, '$.inputIntent') = 'queued_turn' ) ELSE 0 END ELSE 0 END) AS item_count, SUM(CASE WHEN counts.run_key = '' THEN counts.item_count ELSE 0 END) AS runless_item_count FROM orchestration_v2_projection_turn_item_counts counts LEFT JOIN orchestration_v2_projection_runs r ON r.run_id = counts.run_key WHERE counts.thread_id IN (SELECT value FROM json_each(?)) GROUP BY counts.thread_id",
    "rows": 1
  },
  {
    "label": "read",
    "sql": "SELECT thread_id, payload_json FROM orchestration_v2_projection_provider_threads WHERE thread_id IN (SELECT value FROM json_each(?))",
    "rows": 1
  },
  {
    "label": "read",
    "sql": "SELECT i.thread_id, i.payload_json, p.provider_instance_id AS owning_provider_instance_id FROM orchestration_v2_projection_turn_items i LEFT JOIN orchestration_v2_projection_provider_threads p ON p.provider_thread_id = i.provider_thread_id LEFT JOIN orchestration_v2_projection_runs r ON r.run_id = i.run_id WHERE i.type IN ('command_execution', 'dynamic_tool', 'subagent') AND i.status NOT IN ('completed', 'interrupted', 'failed', 'cancelled') AND (i.run_id IS NULL OR r.status <> 'rolled_back') AND i.thread_id IN (SELECT value FROM json_each(?)) -- Preserve the pending-item partial index; read other work only from running runs. UNION ALL SELECT i.thread_id, i.payload_json, NULL AS owning_provider_instance_id FROM orchestration_v2_projection_turn_items i WHERE i.run_id IN (SELECT value FROM json_each(?)) AND i.type IN ('file_change', 'file_search', 'web_search', 'compaction') AND i.status IN ('running', 'waiting')",
    "rows": 0
  },
  {
    "label": "read",
    "sql": "SELECT payload_json FROM orchestration_v2_projection_threads WHERE thread_id = ? LIMIT 1",
    "rows": 1
  },
  {
    "label": "read",
    "sql": "SELECT payload_json FROM orchestration_v2_projection_runs WHERE thread_id = ? ORDER BY ordinal ASC",
    "rows": 107
  },
  {
    "label": "read",
    "sql": "SELECT payload_json FROM orchestration_v2_projection_messages WHERE thread_id = ? ORDER BY created_at ASC, message_id ASC",
    "rows": 213
  },
  {
    "label": "read",
    "sql": "SELECT json_extract(payload_json, '$.messageId') AS message_id, MIN(ordinal) AS ordinal FROM orchestration_v2_projection_turn_items WHERE thread_id = ? AND type IN ('user_message', 'assistant_message') GROUP BY json_extract(payload_json, '$.messageId')",
    "rows": 210
  },
  {
    "label": "edit",
    "sql": "SELECT t.thread_id, t.payload_json, CASE WHEN json_extract(t.payload_json, '$.forkedFrom.type') = 'run' AND json_extract(t.payload_json, '$.lineage.sideChat') IS NOT 1 THEN json_extract(t.payload_json, '$.forkedFrom.threadId') ELSE NULL END AS forked_from_run_source_thread_id, presented.run_id AS latest_run_id, presented.status AS latest_run_status, presented.requested_at AS latest_run_requested_at, json_extract(presented.payload_json, '$.startedAt') AS latest_run_started_at, presented.completed_at AS latest_run_completed_at, EXISTS ( SELECT 1 FROM orchestration_v2_projection_turn_items stop_request WHERE stop_request.thread_id = t.thread_id AND stop_request.run_id = presented.run_id AND stop_request.type = 'run_interrupt_request' ) AS latest_run_stop_requested, EXISTS ( SELECT 1 FROM orchestration_v2_projection_messages wake WHERE wake.message_id = json_extract(presented.payload_json, '$.userMessageId') AND json_extract(wake.payload_json, '$.notification.source.kind') IN ('background_task', 'background_command', 'command', 'monitor') AND json_extract(wake.payload_json, '$.notification.source.work') IS NULL AND json_extract(wake.payload_json, '$.notification.outcome') = 'updated' AND json_extract(wake.payload_json, '$.delegatedCompletion') IS NULL ) AND NOT EXISTS ( SELECT 1 FROM orchestration_v2_projection_messages steer WHERE steer.run_id = presented.run_id AND json_extract(steer.payload_json, '$.createdBy') = 'user' ) AS latest_run_background_wake, active.run_id AS active_run_id, active.status AS active_run_status, ( SELECT r.status -- Read active runs by status. Walking the ordinal index to skip this sort -- reads every run of a thread with none active. FROM orchestration_v2_projection_runs r INDEXED BY orchestration_v2_projection_runs_thread_status_idx WHERE r.thread_id = t.thread_id AND r.status IN ('preparing', 'starting', 'running', 'waiting') AND NOT ( r.status <> 'failed' AND EXISTS ( SELECT 1 FROM orchestration_v2_projection_messages prompt WHERE prompt.message_id = json_extract(r.payload_json, '$.userMessageId') AND ( prompt.role = 'user' AND lower(trim(json_extract(prompt.payload_json, '$.text'), char(9, 10, 11, 12, 13, 32, 160, 5760, 8192, 8193, 8194, 8195, 8196, 8197, 8198, 8199, 8200, 8201, 8202, 8232, 8233, 8239, 8287, 12288, 65279) )) = ? AND coalesce(json_array_length(prompt.payload_json, '$.attachments'), 0) = 0 AND ( json_extract(prompt.payload_json, '$.idleCompaction') IS NOT NULL OR ( json_extract(prompt.payload_json, '$.createdBy') = 'agent' AND json_extract(prompt.payload_json, '$.creationSource') = 'server' ) ) ) ) AND NOT EXISTS ( SELECT 1 FROM orchestration_v2_projection_turn_items compaction WHERE compaction.thread_id = r.thread_id AND compaction.run_id = r.run_id AND compaction.type = 'compaction' AND compaction.status = 'failed' ) ) ORDER BY r.ordinal DESC, r.run_id DESC LIMIT 1 ) AS activity_run_status, ( -- Mirrors orchestrationV2RunWorkStartedAt. SELECT COALESCE( json_extract(r.payload_json, '$.workStartedAt'), json_extract(r.payload_json, '$.startedAt'), r.requested_at ) FROM orchestration_v2_projection_runs r INDEXED BY orchestration_v2_projection_runs_thread_status_idx WHERE r.thread_id = t.thread_id AND r.status IN ('preparing', 'starting', 'running', 'waiting') AND NOT ( r.status <> 'failed' AND EXISTS ( SELECT 1 FROM orchestration_v2_projection_messages prompt WHERE prompt.message_id = json_extract(r.payload_json, '$.userMessageId') AND ( prompt.role = 'user' AND lower(trim(json_extract(prompt.payload_json, '$.text'), char(9, 10, 11, 12, 13, 32, 160, 5760, 8192, 8193, 8194, 8195, 8196, 8197, 8198, 8199, 8200, 8201, 8202, 8232, 8233, 8239, 8287, 12288, 65279) )) = ? AND coalesce(json_array_length(prompt.payload_json, '$.attachments'), 0) = 0 AND ( json_extract(prompt.payload_json, '$.idleCompaction') IS NOT NULL OR ( json_extract(prompt.payload_json, '$.createdBy') = 'agent' AND json_extract(prompt.payload_json, '$.creationSource') = 'server' ) ) ) ) AND NOT EXISTS ( SELECT 1 FROM orchestration_v2_projection_turn_items compaction WHERE compaction.thread_id = r.thread_id AND compaction.run_id = r.run_id AND compaction.type = 'compaction' AND compaction.status = 'failed' ) ) ORDER BY r.ordinal DESC, r.run_id DESC LIMIT 1 ) AS activity_run_started_at, ( SELECT json_extract(session.payload_json, '$.lastError') -- Keep bindings outermost: one provider instance can hold every session. FROM orchestration_v2_projection_provider_session_bindings binding CROSS JOIN orchestration_v2_projection_provider_sessions session ON session.provider_session_id = binding.provider_session_id WHERE binding.thread_id = t.thread_id AND session.provider_instance_id = t.provider_instance_id ORDER BY session.updated_at DESC, session.provider_session_id DESC LIMIT 1 ) AS last_error, ( SELECT item.payload_json FROM orchestration_v2_projection_turn_items item INNER JOIN orchestration_v2_projection_runs r ON r.run_id = item.run_id WHERE r.run_id = presented.run_id AND r.status = 'failed' AND item.thread_id = t.thread_id AND item.type = 'error' AND item.status = 'failed' AND item.node_id IS json_extract(r.payload_json, '$.rootNodeId') ORDER BY item.updated_at DESC, item.ordinal DESC, item.turn_item_id DESC LIMIT 1 ) AS terminal_failure_payload_json, blocked.run_id AS blocking_run_id, blocked.requested_at AS blocking_run_requested_at, json_extract(blocked.payload_json, '$.startedAt') AS blocking_run_started_at, blocked.completed_at AS blocking_run_completed_at, EXISTS ( SELECT 1 FROM orchestration_v2_projection_turn_items stop_request WHERE stop_request.thread_id = t.thread_id AND stop_request.run_id = blocked.run_id AND stop_request.type = 'run_interrupt_request' ) AS blocking_run_stop_requested, EXISTS ( SELECT 1 FROM orchestration_v2_projection_messages wake WHERE wake.message_id = json_extract(blocked.payload_json, '$.userMessageId') AND json_extract(wake.payload_json, '$.notification.source.kind') IN ('background_task', 'background_command', 'command', 'monitor') AND json_extract(wake.payload_json, '$.notification.source.work') IS NULL AND json_extract(wake.payload_json, '$.notification.outcome') = 'updated' AND json_extract(wake.payload_json, '$.delegatedCompletion') IS NULL ) AND NOT EXISTS ( SELECT 1 FROM orchestration_v2_projection_messages steer WHERE steer.run_id = blocked.run_id AND json_extract(steer.payload_json, '$.createdBy') = 'user' ) AS blocking_run_background_wake, ( SELECT item.payload_json FROM orchestration_v2_projection_turn_items item WHERE item.thread_id = t.thread_id AND item.run_id = blocked.run_id AND item.type = 'error' AND item.status = 'failed' AND item.node_id IS json_extract(blocked.payload_json, '$.rootNodeId') ORDER BY item.updated_at DESC, item.ordinal DESC, item.turn_item_id DESC LIMIT 1 ) AS blocking_failure_payload_json, ( SELECT request.payload_json FROM orchestration_v2_projection_runtime_requests request WHERE request.thread_id = t.thread_id AND request.status = 'pending' ORDER BY (request.kind = 'user_input') ASC, request.created_at DESC, request.runtime_request_id DESC LIMIT 1 ) AS pending_request_payload_json, ( SELECT secret.payload_json -- Keep runs outermost so completed history is never scanned for a pending secret. FROM orchestration_v2_projection_runs r INDEXED BY orchestration_v2_projection_runs_thread_status_idx CROSS JOIN orchestration_v2_projection_turn_items secret INDEXED BY orchestration_v2_projection_turn_items_thread_run_idx WHERE r.thread_id = t.thread_id AND r.status IN ('preparing', 'starting', 'running', 'waiting') AND secret.thread_id = t.thread_id AND secret.run_id = r.run_id AND secret.type = 'secret_request' AND secret.status = 'waiting' ORDER BY secret.updated_at DESC, secret.turn_item_id DESC LIMIT 1 ) AS pending_secret_request_payload_json, ( SELECT link.payload_json FROM orchestration_v2_projection_turn_items link INDEXED BY orchestration_v2_projection_turn_items_waiting_link_idx WHERE link.thread_id = t.thread_id AND link.type = 'link_request' AND link.status = 'waiting' ORDER BY link.updated_at DESC, link.turn_item_id DESC LIMIT 1 ) AS pending_link_request_payload_json, ( SELECT message.updated_at FROM orchestration_v2_projection_messages message WHERE message.thread_id = t.thread_id AND message.role = 'user' AND NOT ( message.role = 'user' AND lower(trim(json_extract(message.payload_json, '$.text'), char(9, 10, 11, 12, 13, 32, 160, 5760, 8192, 8193, 8194, 8195, 8196, 8197, 8198, 8199, 8200, 8201, 8202, 8232, 8233, 8239, 8287, 12288, 65279) )) = ? AND coalesce(json_array_length(message.payload_json, '$.attachments'), 0) = 0 AND ( json_extract(message.payload_json, '$.idleCompaction') IS NOT NULL OR ( json_extract(message.payload_json, '$.createdBy') = 'agent' AND json_extract(message.payload_json, '$.creationSource') = 'server' ) ) ) ORDER BY message.updated_at DESC, message.message_id DESC LIMIT 1 ) AS latest_user_message_at, ( SELECT message.updated_at FROM orchestration_v2_projection_messages message WHERE message.thread_id = t.thread_id AND message.role = 'user' AND json_extract(message.payload_json, '$.createdBy') = 'user' AND NOT ( message.role = 'user' AND lower(trim(json_extract(message.payload_json, '$.text'), char(9, 10, 11, 12, 13, 32, 160, 5760, 8192, 8193, 8194, 8195, 8196, 8197, 8198, 8199, 8200, 8201, 8202, 8232, 8233, 8239, 8287, 12288, 65279) )) = ? AND coalesce(json_array_length(message.payload_json, '$.attachments'), 0) = 0 AND ( json_extract(message.payload_json, '$.idleCompaction') IS NOT NULL OR ( json_extract(message.payload_json, '$.createdBy') = 'agent' AND json_extract(message.payload_json, '$.creationSource') = 'server' ) ) ) ORDER BY message.updated_at DESC, message.message_id DESC LIMIT 1 ) AS latest_user_authored_message_at, EXISTS ( SELECT 1 FROM orchestration_v2_projection_plans plan WHERE plan.thread_id = t.thread_id AND plan.kind = 'proposed_plan' AND plan.status = 'active' ) AS has_actionable_proposed_plan FROM orchestration_v2_projection_threads t LEFT JOIN orchestration_v2_projection_runs active ON active.run_id = ( SELECT r.run_id FROM orchestration_v2_projection_runs r INDEXED BY orchestration_v2_projection_runs_thread_status_idx WHERE r.thread_id = t.thread_id AND r.status IN ('preparing', 'starting', 'running') AND NOT ( r.status <> 'failed' AND EXISTS ( SELECT 1 FROM orchestration_v2_projection_messages prompt WHERE prompt.message_id = json_extract(r.payload_json, '$.userMessageId') AND ( prompt.role = 'user' AND lower(trim(json_extract(prompt.payload_json, '$.text'), char(9, 10, 11, 12, 13, 32, 160, 5760, 8192, 8193, 8194, 8195, 8196, 8197, 8198, 8199, 8200, 8201, 8202, 8232, 8233, 8239, 8287, 12288, 65279) )) = ? AND coalesce(json_array_length(prompt.payload_json, '$.attachments'), 0) = 0 AND ( json_extract(prompt.payload_json, '$.idleCompaction') IS NOT NULL OR ( json_extract(prompt.payload_json, '$.createdBy') = 'agent' AND json_extract(prompt.payload_json, '$.creationSource') = 'server' ) ) ) ) AND NOT EXISTS ( SELECT 1 FROM orchestration_v2_projection_turn_items compaction WHERE compaction.thread_id = r.thread_id AND compaction.run_id = r.run_id AND compaction.type = 'compaction' AND compaction.status = 'failed' ) ) ORDER BY r.ordinal DESC, r.run_id DESC LIMIT 1 ) LEFT JOIN orchestration_v2_projection_runs presented ON presented.run_id = ( SELECT candidate.run_id FROM orchestration_v2_projection_runs candidate WHERE candidate.thread_id = t.thread_id AND NOT ( candidate.status = 'queued' AND json_extract(candidate.payload_json, '$.queueHeld') IS 1 ) AND NOT ( candidate.status = 'cancelled' AND json_extract(candidate.payload_json, '$.startedAt') IS NULL AND candidate.completed_at IS NOT NULL AND EXISTS ( SELECT 1 FROM orchestration_v2_projection_runs earlier WHERE earlier.thread_id = candidate.thread_id AND earlier.ordinal < candidate.ordinal AND earlier.status <> 'queued' AND NOT ( earlier.status <> 'failed' AND EXISTS ( SELECT 1 FROM orchestration_v2_projection_messages prompt WHERE prompt.message_id = json_extract(earlier.payload_json, '$.userMessageId') AND ( prompt.role = 'user' AND lower(trim(json_extract(prompt.payload_json, '$.text'), char(9, 10, 11, 12, 13, 32, 160, 5760, 8192, 8193, 8194, 8195, 8196, 8197, 8198, 8199, 8200, 8201, 8202, 8232, 8233, 8239, 8287, 12288, 65279) )) = ? AND coalesce(json_array_length(prompt.payload_json, '$.attachments'), 0) = 0 AND ( json_extract(prompt.payload_json, '$.idleCompaction') IS NOT NULL OR ( json_extract(prompt.payload_json, '$.createdBy') = 'agent' AND json_extract(prompt.payload_json, '$.creationSource') = 'server' ) ) ) ) AND NOT EXISTS ( SELECT 1 FROM orchestration_v2_projection_turn_items compaction WHERE compaction.thread_id = earlier.thread_id AND compaction.run_id = earlier.run_id AND compaction.type = 'compaction' AND compaction.status = 'failed' ) ) AND ( earlier.completed_at IS NULL OR julianday(earlier.completed_at) >= julianday(candidate.completed_at) ) ) ) AND NOT ( candidate.status <> 'failed' AND EXISTS ( SELECT 1 FROM orchestration_v2_projection_messages prompt WHERE prompt.message_id = json_extract(candidate.payload_json, '$.userMessageId') AND ( prompt.role = 'user' AND lower(trim(json_extract(prompt.payload_json, '$.text'), char(9, 10, 11, 12, 13, 32, 160, 5760, 8192, 8193, 8194, 8195, 8196, 8197, 8198, 8199, 8200, 8201, 8202, 8232, 8233, 8239, 8287, 12288, 65279) )) = ? AND coalesce(json_array_length(prompt.payload_json, '$.attachments'), 0) = 0 AND ( json_extract(prompt.payload_json, '$.idleCompaction') IS NOT NULL OR ( json_extract(prompt.payload_json, '$.createdBy') = 'agent' AND json_extract(prompt.payload_json, '$.creationSource') = 'server' ) ) ) ) AND NOT EXISTS ( SELECT 1 FROM orchestration_v2_projection_turn_items compaction WHERE compaction.thread_id = candidate.thread_id AND compaction.run_id = candidate.run_id AND compaction.type = 'compaction' AND compaction.status = 'failed' ) ) ORDER BY candidate.ordinal DESC, candidate.run_id DESC LIMIT 1 ) LEFT JOIN orchestration_v2_projection_runs blocked ON blocked.run_id = ( SELECT candidate.run_id FROM orchestration_v2_projection_runs candidate WHERE candidate.thread_id = t.thread_id AND candidate.status <> 'queued' AND NOT ( candidate.status = 'cancelled' AND json_extract(candidate.payload_json, '$.startedAt') IS NULL ) -- latestExecutedRun: the run that ended last (runRanAfter). ORDER BY candidate.completed_at IS NULL DESC, candidate.completed_at DESC, candidate.ordinal DESC, candidate.run_id DESC LIMIT 1 ) AND blocked.status = 'failed' WHERE t.deleted_at IS NULL AND t.thread_id = ? ORDER BY t.updated_at ASC, t.thread_id ASC",
    "rows": 1
  },
  {
    "label": "edit",
    "sql": "SELECT counts.thread_id, SUM(CASE WHEN counts.run_key = '' OR r.status <> 'rolled_back' THEN counts.item_count - CASE WHEN r.status IN ('queued', 'cancelled') THEN ( SELECT COUNT(*) FROM orchestration_v2_projection_turn_items held INDEXED BY orchestration_v2_projection_turn_items_queued_input_idx WHERE held.run_id = counts.run_key AND held.type = 'user_message' AND json_extract(held.payload_json, '$.inputIntent') = 'queued_turn' ) ELSE 0 END ELSE 0 END) AS item_count, SUM(CASE WHEN counts.run_key = '' THEN counts.item_count ELSE 0 END) AS runless_item_count FROM orchestration_v2_projection_turn_item_counts counts LEFT JOIN orchestration_v2_projection_runs r ON r.run_id = counts.run_key WHERE counts.thread_id IN (SELECT value FROM json_each(?)) GROUP BY counts.thread_id",
    "rows": 1
  },
  {
    "label": "edit",
    "sql": "SELECT thread_id, payload_json FROM orchestration_v2_projection_provider_threads WHERE thread_id IN (SELECT value FROM json_each(?))",
    "rows": 1
  },
  {
    "label": "edit",
    "sql": "SELECT i.thread_id, i.payload_json, p.provider_instance_id AS owning_provider_instance_id FROM orchestration_v2_projection_turn_items i LEFT JOIN orchestration_v2_projection_provider_threads p ON p.provider_thread_id = i.provider_thread_id LEFT JOIN orchestration_v2_projection_runs r ON r.run_id = i.run_id WHERE i.type IN ('command_execution', 'dynamic_tool', 'subagent') AND i.status NOT IN ('completed', 'interrupted', 'failed', 'cancelled') AND (i.run_id IS NULL OR r.status <> 'rolled_back') AND i.thread_id IN (SELECT value FROM json_each(?)) -- Preserve the pending-item partial index; read other work only from running runs. UNION ALL SELECT i.thread_id, i.payload_json, NULL AS owning_provider_instance_id FROM orchestration_v2_projection_turn_items i WHERE i.run_id IN (SELECT value FROM json_each(?)) AND i.type IN ('file_change', 'file_search', 'web_search', 'compaction') AND i.status IN ('running', 'waiting')",
    "rows": 0
  },
  {
    "label": "edit",
    "sql": "SELECT t.thread_id, t.payload_json, CASE WHEN json_extract(t.payload_json, '$.forkedFrom.type') = 'run' AND json_extract(t.payload_json, '$.lineage.sideChat') IS NOT 1 THEN json_extract(t.payload_json, '$.forkedFrom.threadId') ELSE NULL END AS forked_from_run_source_thread_id, presented.run_id AS latest_run_id, presented.status AS latest_run_status, presented.requested_at AS latest_run_requested_at, json_extract(presented.payload_json, '$.startedAt') AS latest_run_started_at, presented.completed_at AS latest_run_completed_at, EXISTS ( SELECT 1 FROM orchestration_v2_projection_turn_items stop_request WHERE stop_request.thread_id = t.thread_id AND stop_request.run_id = presented.run_id AND stop_request.type = 'run_interrupt_request' ) AS latest_run_stop_requested, EXISTS ( SELECT 1 FROM orchestration_v2_projection_messages wake WHERE wake.message_id = json_extract(presented.payload_json, '$.userMessageId') AND json_extract(wake.payload_json, '$.notification.source.kind') IN ('background_task', 'background_command', 'command', 'monitor') AND json_extract(wake.payload_json, '$.notification.source.work') IS NULL AND json_extract(wake.payload_json, '$.notification.outcome') = 'updated' AND json_extract(wake.payload_json, '$.delegatedCompletion') IS NULL ) AND NOT EXISTS ( SELECT 1 FROM orchestration_v2_projection_messages steer WHERE steer.run_id = presented.run_id AND json_extract(steer.payload_json, '$.createdBy') = 'user' ) AS latest_run_background_wake, active.run_id AS active_run_id, active.status AS active_run_status, ( SELECT r.status -- Read active runs by status. Walking the ordinal index to skip this sort -- reads every run of a thread with none active. FROM orchestration_v2_projection_runs r INDEXED BY orchestration_v2_projection_runs_thread_status_idx WHERE r.thread_id = t.thread_id AND r.status IN ('preparing', 'starting', 'running', 'waiting') AND NOT ( r.status <> 'failed' AND EXISTS ( SELECT 1 FROM orchestration_v2_projection_messages prompt WHERE prompt.message_id = json_extract(r.payload_json, '$.userMessageId') AND ( prompt.role = 'user' AND lower(trim(json_extract(prompt.payload_json, '$.text'), char(9, 10, 11, 12, 13, 32, 160, 5760, 8192, 8193, 8194, 8195, 8196, 8197, 8198, 8199, 8200, 8201, 8202, 8232, 8233, 8239, 8287, 12288, 65279) )) = ? AND coalesce(json_array_length(prompt.payload_json, '$.attachments'), 0) = 0 AND ( json_extract(prompt.payload_json, '$.idleCompaction') IS NOT NULL OR ( json_extract(prompt.payload_json, '$.createdBy') = 'agent' AND json_extract(prompt.payload_json, '$.creationSource') = 'server' ) ) ) ) AND NOT EXISTS ( SELECT 1 FROM orchestration_v2_projection_turn_items compaction WHERE compaction.thread_id = r.thread_id AND compaction.run_id = r.run_id AND compaction.type = 'compaction' AND compaction.status = 'failed' ) ) ORDER BY r.ordinal DESC, r.run_id DESC LIMIT 1 ) AS activity_run_status, ( -- Mirrors orchestrationV2RunWorkStartedAt. SELECT COALESCE( json_extract(r.payload_json, '$.workStartedAt'), json_extract(r.payload_json, '$.startedAt'), r.requested_at ) FROM orchestration_v2_projection_runs r INDEXED BY orchestration_v2_projection_runs_thread_status_idx WHERE r.thread_id = t.thread_id AND r.status IN ('preparing', 'starting', 'running', 'waiting') AND NOT ( r.status <> 'failed' AND EXISTS ( SELECT 1 FROM orchestration_v2_projection_messages prompt WHERE prompt.message_id = json_extract(r.payload_json, '$.userMessageId') AND ( prompt.role = 'user' AND lower(trim(json_extract(prompt.payload_json, '$.text'), char(9, 10, 11, 12, 13, 32, 160, 5760, 8192, 8193, 8194, 8195, 8196, 8197, 8198, 8199, 8200, 8201, 8202, 8232, 8233, 8239, 8287, 12288, 65279) )) = ? AND coalesce(json_array_length(prompt.payload_json, '$.attachments'), 0) = 0 AND ( json_extract(prompt.payload_json, '$.idleCompaction') IS NOT NULL OR ( json_extract(prompt.payload_json, '$.createdBy') = 'agent' AND json_extract(prompt.payload_json, '$.creationSource') = 'server' ) ) ) ) AND NOT EXISTS ( SELECT 1 FROM orchestration_v2_projection_turn_items compaction WHERE compaction.thread_id = r.thread_id AND compaction.run_id = r.run_id AND compaction.type = 'compaction' AND compaction.status = 'failed' ) ) ORDER BY r.ordinal DESC, r.run_id DESC LIMIT 1 ) AS activity_run_started_at, ( SELECT json_extract(session.payload_json, '$.lastError') -- Keep bindings outermost: one provider instance can hold every session. FROM orchestration_v2_projection_provider_session_bindings binding CROSS JOIN orchestration_v2_projection_provider_sessions session ON session.provider_session_id = binding.provider_session_id WHERE binding.thread_id = t.thread_id AND session.provider_instance_id = t.provider_instance_id ORDER BY session.updated_at DESC, session.provider_session_id DESC LIMIT 1 ) AS last_error, ( SELECT item.payload_json FROM orchestration_v2_projection_turn_items item INNER JOIN orchestration_v2_projection_runs r ON r.run_id = item.run_id WHERE r.run_id = presented.run_id AND r.status = 'failed' AND item.thread_id = t.thread_id AND item.type = 'error' AND item.status = 'failed' AND item.node_id IS json_extract(r.payload_json, '$.rootNodeId') ORDER BY item.updated_at DESC, item.ordinal DESC, item.turn_item_id DESC LIMIT 1 ) AS terminal_failure_payload_json, blocked.run_id AS blocking_run_id, blocked.requested_at AS blocking_run_requested_at, json_extract(blocked.payload_json, '$.startedAt') AS blocking_run_started_at, blocked.completed_at AS blocking_run_completed_at, EXISTS ( SELECT 1 FROM orchestration_v2_projection_turn_items stop_request WHERE stop_request.thread_id = t.thread_id AND stop_request.run_id = blocked.run_id AND stop_request.type = 'run_interrupt_request' ) AS blocking_run_stop_requested, EXISTS ( SELECT 1 FROM orchestration_v2_projection_messages wake WHERE wake.message_id = json_extract(blocked.payload_json, '$.userMessageId') AND json_extract(wake.payload_json, '$.notification.source.kind') IN ('background_task', 'background_command', 'command', 'monitor') AND json_extract(wake.payload_json, '$.notification.source.work') IS NULL AND json_extract(wake.payload_json, '$.notification.outcome') = 'updated' AND json_extract(wake.payload_json, '$.delegatedCompletion') IS NULL ) AND NOT EXISTS ( SELECT 1 FROM orchestration_v2_projection_messages steer WHERE steer.run_id = blocked.run_id AND json_extract(steer.payload_json, '$.createdBy') = 'user' ) AS blocking_run_background_wake, ( SELECT item.payload_json FROM orchestration_v2_projection_turn_items item WHERE item.thread_id = t.thread_id AND item.run_id = blocked.run_id AND item.type = 'error' AND item.status = 'failed' AND item.node_id IS json_extract(blocked.payload_json, '$.rootNodeId') ORDER BY item.updated_at DESC, item.ordinal DESC, item.turn_item_id DESC LIMIT 1 ) AS blocking_failure_payload_json, ( SELECT request.payload_json FROM orchestration_v2_projection_runtime_requests request WHERE request.thread_id = t.thread_id AND request.status = 'pending' ORDER BY (request.kind = 'user_input') ASC, request.created_at DESC, request.runtime_request_id DESC LIMIT 1 ) AS pending_request_payload_json, ( SELECT secret.payload_json -- Keep runs outermost so completed history is never scanned for a pending secret. FROM orchestration_v2_projection_runs r INDEXED BY orchestration_v2_projection_runs_thread_status_idx CROSS JOIN orchestration_v2_projection_turn_items secret INDEXED BY orchestration_v2_projection_turn_items_thread_run_idx WHERE r.thread_id = t.thread_id AND r.status IN ('preparing', 'starting', 'running', 'waiting') AND secret.thread_id = t.thread_id AND secret.run_id = r.run_id AND secret.type = 'secret_request' AND secret.status = 'waiting' ORDER BY secret.updated_at DESC, secret.turn_item_id DESC LIMIT 1 ) AS pending_secret_request_payload_json, ( SELECT link.payload_json FROM orchestration_v2_projection_turn_items link INDEXED BY orchestration_v2_projection_turn_items_waiting_link_idx WHERE link.thread_id = t.thread_id AND link.type = 'link_request' AND link.status = 'waiting' ORDER BY link.updated_at DESC, link.turn_item_id DESC LIMIT 1 ) AS pending_link_request_payload_json, ( SELECT message.updated_at FROM orchestration_v2_projection_messages message WHERE message.thread_id = t.thread_id AND message.role = 'user' AND NOT ( message.role = 'user' AND lower(trim(json_extract(message.payload_json, '$.text'), char(9, 10, 11, 12, 13, 32, 160, 5760, 8192, 8193, 8194, 8195, 8196, 8197, 8198, 8199, 8200, 8201, 8202, 8232, 8233, 8239, 8287, 12288, 65279) )) = ? AND coalesce(json_array_length(message.payload_json, '$.attachments'), 0) = 0 AND ( json_extract(message.payload_json, '$.idleCompaction') IS NOT NULL OR ( json_extract(message.payload_json, '$.createdBy') = 'agent' AND json_extract(message.payload_json, '$.creationSource') = 'server' ) ) ) ORDER BY message.updated_at DESC, message.message_id DESC LIMIT 1 ) AS latest_user_message_at, ( SELECT message.updated_at FROM orchestration_v2_projection_messages message WHERE message.thread_id = t.thread_id AND message.role = 'user' AND json_extract(message.payload_json, '$.createdBy') = 'user' AND NOT ( message.role = 'user' AND lower(trim(json_extract(message.payload_json, '$.text'), char(9, 10, 11, 12, 13, 32, 160, 5760, 8192, 8193, 8194, 8195, 8196, 8197, 8198, 8199, 8200, 8201, 8202, 8232, 8233, 8239, 8287, 12288, 65279) )) = ? AND coalesce(json_array_length(message.payload_json, '$.attachments'), 0) = 0 AND ( json_extract(message.payload_json, '$.idleCompaction') IS NOT NULL OR ( json_extract(message.payload_json, '$.createdBy') = 'agent' AND json_extract(message.payload_json, '$.creationSource') = 'server' ) ) ) ORDER BY message.updated_at DESC, message.message_id DESC LIMIT 1 ) AS latest_user_authored_message_at, EXISTS ( SELECT 1 FROM orchestration_v2_projection_plans plan WHERE plan.thread_id = t.thread_id AND plan.kind = 'proposed_plan' AND plan.status = 'active' ) AS has_actionable_proposed_plan FROM orchestration_v2_projection_threads t LEFT JOIN orchestration_v2_projection_runs active ON active.run_id = ( SELECT r.run_id FROM orchestration_v2_projection_runs r INDEXED BY orchestration_v2_projection_runs_thread_status_idx WHERE r.thread_id = t.thread_id AND r.status IN ('preparing', 'starting', 'running') AND NOT ( r.status <> 'failed' AND EXISTS ( SELECT 1 FROM orchestration_v2_projection_messages prompt WHERE prompt.message_id = json_extract(r.payload_json, '$.userMessageId') AND ( prompt.role = 'user' AND lower(trim(json_extract(prompt.payload_json, '$.text'), char(9, 10, 11, 12, 13, 32, 160, 5760, 8192, 8193, 8194, 8195, 8196, 8197, 8198, 8199, 8200, 8201, 8202, 8232, 8233, 8239, 8287, 12288, 65279) )) = ? AND coalesce(json_array_length(prompt.payload_json, '$.attachments'), 0) = 0 AND ( json_extract(prompt.payload_json, '$.idleCompaction') IS NOT NULL OR ( json_extract(prompt.payload_json, '$.createdBy') = 'agent' AND json_extract(prompt.payload_json, '$.creationSource') = 'server' ) ) ) ) AND NOT EXISTS ( SELECT 1 FROM orchestration_v2_projection_turn_items compaction WHERE compaction.thread_id = r.thread_id AND compaction.run_id = r.run_id AND compaction.type = 'compaction' AND compaction.status = 'failed' ) ) ORDER BY r.ordinal DESC, r.run_id DESC LIMIT 1 ) LEFT JOIN orchestration_v2_projection_runs presented ON presented.run_id = ( SELECT candidate.run_id FROM orchestration_v2_projection_runs candidate WHERE candidate.thread_id = t.thread_id AND NOT ( candidate.status = 'queued' AND json_extract(candidate.payload_json, '$.queueHeld') IS 1 ) AND NOT ( candidate.status = 'cancelled' AND json_extract(candidate.payload_json, '$.startedAt') IS NULL AND candidate.completed_at IS NOT NULL AND EXISTS ( SELECT 1 FROM orchestration_v2_projection_runs earlier WHERE earlier.thread_id = candidate.thread_id AND earlier.ordinal < candidate.ordinal AND earlier.status <> 'queued' AND NOT ( earlier.status <> 'failed' AND EXISTS ( SELECT 1 FROM orchestration_v2_projection_messages prompt WHERE prompt.message_id = json_extract(earlier.payload_json, '$.userMessageId') AND ( prompt.role = 'user' AND lower(trim(json_extract(prompt.payload_json, '$.text'), char(9, 10, 11, 12, 13, 32, 160, 5760, 8192, 8193, 8194, 8195, 8196, 8197, 8198, 8199, 8200, 8201, 8202, 8232, 8233, 8239, 8287, 12288, 65279) )) = ? AND coalesce(json_array_length(prompt.payload_json, '$.attachments'), 0) = 0 AND ( json_extract(prompt.payload_json, '$.idleCompaction') IS NOT NULL OR ( json_extract(prompt.payload_json, '$.createdBy') = 'agent' AND json_extract(prompt.payload_json, '$.creationSource') = 'server' ) ) ) ) AND NOT EXISTS ( SELECT 1 FROM orchestration_v2_projection_turn_items compaction WHERE compaction.thread_id = earlier.thread_id AND compaction.run_id = earlier.run_id AND compaction.type = 'compaction' AND compaction.status = 'failed' ) ) AND ( earlier.completed_at IS NULL OR julianday(earlier.completed_at) >= julianday(candidate.completed_at) ) ) ) AND NOT ( candidate.status <> 'failed' AND EXISTS ( SELECT 1 FROM orchestration_v2_projection_messages prompt WHERE prompt.message_id = json_extract(candidate.payload_json, '$.userMessageId') AND ( prompt.role = 'user' AND lower(trim(json_extract(prompt.payload_json, '$.text'), char(9, 10, 11, 12, 13, 32, 160, 5760, 8192, 8193, 8194, 8195, 8196, 8197, 8198, 8199, 8200, 8201, 8202, 8232, 8233, 8239, 8287, 12288, 65279) )) = ? AND coalesce(json_array_length(prompt.payload_json, '$.attachments'), 0) = 0 AND ( json_extract(prompt.payload_json, '$.idleCompaction') IS NOT NULL OR ( json_extract(prompt.payload_json, '$.createdBy') = 'agent' AND json_extract(prompt.payload_json, '$.creationSource') = 'server' ) ) ) ) AND NOT EXISTS ( SELECT 1 FROM orchestration_v2_projection_turn_items compaction WHERE compaction.thread_id = candidate.thread_id AND compaction.run_id = candidate.run_id AND compaction.type = 'compaction' AND compaction.status = 'failed' ) ) ORDER BY candidate.ordinal DESC, candidate.run_id DESC LIMIT 1 ) LEFT JOIN orchestration_v2_projection_runs blocked ON blocked.run_id = ( SELECT candidate.run_id FROM orchestration_v2_projection_runs candidate WHERE candidate.thread_id = t.thread_id AND candidate.status <> 'queued' AND NOT ( candidate.status = 'cancelled' AND json_extract(candidate.payload_json, '$.startedAt') IS NULL ) -- latestExecutedRun: the run that ended last (runRanAfter). ORDER BY candidate.completed_at IS NULL DESC, candidate.completed_at DESC, candidate.ordinal DESC, candidate.run_id DESC LIMIT 1 ) AND blocked.status = 'failed' WHERE t.deleted_at IS NULL AND t.thread_id = ? ORDER BY t.updated_at ASC, t.thread_id ASC",
    "rows": 1
  },
  {
    "label": "edit",
    "sql": "SELECT counts.thread_id, SUM(CASE WHEN counts.run_key = '' OR r.status <> 'rolled_back' THEN counts.item_count - CASE WHEN r.status IN ('queued', 'cancelled') THEN ( SELECT COUNT(*) FROM orchestration_v2_projection_turn_items held INDEXED BY orchestration_v2_projection_turn_items_queued_input_idx WHERE held.run_id = counts.run_key AND held.type = 'user_message' AND json_extract(held.payload_json, '$.inputIntent') = 'queued_turn' ) ELSE 0 END ELSE 0 END) AS item_count, SUM(CASE WHEN counts.run_key = '' THEN counts.item_count ELSE 0 END) AS runless_item_count FROM orchestration_v2_projection_turn_item_counts counts LEFT JOIN orchestration_v2_projection_runs r ON r.run_id = counts.run_key WHERE counts.thread_id IN (SELECT value FROM json_each(?)) GROUP BY counts.thread_id",
    "rows": 1
  },
  {
    "label": "edit",
    "sql": "SELECT thread_id, payload_json FROM orchestration_v2_projection_provider_threads WHERE thread_id IN (SELECT value FROM json_each(?))",
    "rows": 1
  },
  {
    "label": "edit",
    "sql": "SELECT i.thread_id, i.payload_json, p.provider_instance_id AS owning_provider_instance_id FROM orchestration_v2_projection_turn_items i LEFT JOIN orchestration_v2_projection_provider_threads p ON p.provider_thread_id = i.provider_thread_id LEFT JOIN orchestration_v2_projection_runs r ON r.run_id = i.run_id WHERE i.type IN ('command_execution', 'dynamic_tool', 'subagent') AND i.status NOT IN ('completed', 'interrupted', 'failed', 'cancelled') AND (i.run_id IS NULL OR r.status <> 'rolled_back') AND i.thread_id IN (SELECT value FROM json_each(?)) -- Preserve the pending-item partial index; read other work only from running runs. UNION ALL SELECT i.thread_id, i.payload_json, NULL AS owning_provider_instance_id FROM orchestration_v2_projection_turn_items i WHERE i.run_id IN (SELECT value FROM json_each(?)) AND i.type IN ('file_change', 'file_search', 'web_search', 'compaction') AND i.status IN ('running', 'waiting')",
    "rows": 0
  },
  {
    "label": "edit",
    "sql": "SELECT t.thread_id, t.payload_json, CASE WHEN json_extract(t.payload_json, '$.forkedFrom.type') = 'run' AND json_extract(t.payload_json, '$.lineage.sideChat') IS NOT 1 THEN json_extract(t.payload_json, '$.forkedFrom.threadId') ELSE NULL END AS forked_from_run_source_thread_id, presented.run_id AS latest_run_id, presented.status AS latest_run_status, presented.requested_at AS latest_run_requested_at, json_extract(presented.payload_json, '$.startedAt') AS latest_run_started_at, presented.completed_at AS latest_run_completed_at, EXISTS ( SELECT 1 FROM orchestration_v2_projection_turn_items stop_request WHERE stop_request.thread_id = t.thread_id AND stop_request.run_id = presented.run_id AND stop_request.type = 'run_interrupt_request' ) AS latest_run_stop_requested, EXISTS ( SELECT 1 FROM orchestration_v2_projection_messages wake WHERE wake.message_id = json_extract(presented.payload_json, '$.userMessageId') AND json_extract(wake.payload_json, '$.notification.source.kind') IN ('background_task', 'background_command', 'command', 'monitor') AND json_extract(wake.payload_json, '$.notification.source.work') IS NULL AND json_extract(wake.payload_json, '$.notification.outcome') = 'updated' AND json_extract(wake.payload_json, '$.delegatedCompletion') IS NULL ) AND NOT EXISTS ( SELECT 1 FROM orchestration_v2_projection_messages steer WHERE steer.run_id = presented.run_id AND json_extract(steer.payload_json, '$.createdBy') = 'user' ) AS latest_run_background_wake, active.run_id AS active_run_id, active.status AS active_run_status, ( SELECT r.status -- Read active runs by status. Walking the ordinal index to skip this sort -- reads every run of a thread with none active. FROM orchestration_v2_projection_runs r INDEXED BY orchestration_v2_projection_runs_thread_status_idx WHERE r.thread_id = t.thread_id AND r.status IN ('preparing', 'starting', 'running', 'waiting') AND NOT ( r.status <> 'failed' AND EXISTS ( SELECT 1 FROM orchestration_v2_projection_messages prompt WHERE prompt.message_id = json_extract(r.payload_json, '$.userMessageId') AND ( prompt.role = 'user' AND lower(trim(json_extract(prompt.payload_json, '$.text'), char(9, 10, 11, 12, 13, 32, 160, 5760, 8192, 8193, 8194, 8195, 8196, 8197, 8198, 8199, 8200, 8201, 8202, 8232, 8233, 8239, 8287, 12288, 65279) )) = ? AND coalesce(json_array_length(prompt.payload_json, '$.attachments'), 0) = 0 AND ( json_extract(prompt.payload_json, '$.idleCompaction') IS NOT NULL OR ( json_extract(prompt.payload_json, '$.createdBy') = 'agent' AND json_extract(prompt.payload_json, '$.creationSource') = 'server' ) ) ) ) AND NOT EXISTS ( SELECT 1 FROM orchestration_v2_projection_turn_items compaction WHERE compaction.thread_id = r.thread_id AND compaction.run_id = r.run_id AND compaction.type = 'compaction' AND compaction.status = 'failed' ) ) ORDER BY r.ordinal DESC, r.run_id DESC LIMIT 1 ) AS activity_run_status, ( -- Mirrors orchestrationV2RunWorkStartedAt. SELECT COALESCE( json_extract(r.payload_json, '$.workStartedAt'), json_extract(r.payload_json, '$.startedAt'), r.requested_at ) FROM orchestration_v2_projection_runs r INDEXED BY orchestration_v2_projection_runs_thread_status_idx WHERE r.thread_id = t.thread_id AND r.status IN ('preparing', 'starting', 'running', 'waiting') AND NOT ( r.status <> 'failed' AND EXISTS ( SELECT 1 FROM orchestration_v2_projection_messages prompt WHERE prompt.message_id = json_extract(r.payload_json, '$.userMessageId') AND ( prompt.role = 'user' AND lower(trim(json_extract(prompt.payload_json, '$.text'), char(9, 10, 11, 12, 13, 32, 160, 5760, 8192, 8193, 8194, 8195, 8196, 8197, 8198, 8199, 8200, 8201, 8202, 8232, 8233, 8239, 8287, 12288, 65279) )) = ? AND coalesce(json_array_length(prompt.payload_json, '$.attachments'), 0) = 0 AND ( json_extract(prompt.payload_json, '$.idleCompaction') IS NOT NULL OR ( json_extract(prompt.payload_json, '$.createdBy') = 'agent' AND json_extract(prompt.payload_json, '$.creationSource') = 'server' ) ) ) ) AND NOT EXISTS ( SELECT 1 FROM orchestration_v2_projection_turn_items compaction WHERE compaction.thread_id = r.thread_id AND compaction.run_id = r.run_id AND compaction.type = 'compaction' AND compaction.status = 'failed' ) ) ORDER BY r.ordinal DESC, r.run_id DESC LIMIT 1 ) AS activity_run_started_at, ( SELECT json_extract(session.payload_json, '$.lastError') -- Keep bindings outermost: one provider instance can hold every session. FROM orchestration_v2_projection_provider_session_bindings binding CROSS JOIN orchestration_v2_projection_provider_sessions session ON session.provider_session_id = binding.provider_session_id WHERE binding.thread_id = t.thread_id AND session.provider_instance_id = t.provider_instance_id ORDER BY session.updated_at DESC, session.provider_session_id DESC LIMIT 1 ) AS last_error, ( SELECT item.payload_json FROM orchestration_v2_projection_turn_items item INNER JOIN orchestration_v2_projection_runs r ON r.run_id = item.run_id WHERE r.run_id = presented.run_id AND r.status = 'failed' AND item.thread_id = t.thread_id AND item.type = 'error' AND item.status = 'failed' AND item.node_id IS json_extract(r.payload_json, '$.rootNodeId') ORDER BY item.updated_at DESC, item.ordinal DESC, item.turn_item_id DESC LIMIT 1 ) AS terminal_failure_payload_json, blocked.run_id AS blocking_run_id, blocked.requested_at AS blocking_run_requested_at, json_extract(blocked.payload_json, '$.startedAt') AS blocking_run_started_at, blocked.completed_at AS blocking_run_completed_at, EXISTS ( SELECT 1 FROM orchestration_v2_projection_turn_items stop_request WHERE stop_request.thread_id = t.thread_id AND stop_request.run_id = blocked.run_id AND stop_request.type = 'run_interrupt_request' ) AS blocking_run_stop_requested, EXISTS ( SELECT 1 FROM orchestration_v2_projection_messages wake WHERE wake.message_id = json_extract(blocked.payload_json, '$.userMessageId') AND json_extract(wake.payload_json, '$.notification.source.kind') IN ('background_task', 'background_command', 'command', 'monitor') AND json_extract(wake.payload_json, '$.notification.source.work') IS NULL AND json_extract(wake.payload_json, '$.notification.outcome') = 'updated' AND json_extract(wake.payload_json, '$.delegatedCompletion') IS NULL ) AND NOT EXISTS ( SELECT 1 FROM orchestration_v2_projection_messages steer WHERE steer.run_id = blocked.run_id AND json_extract(steer.payload_json, '$.createdBy') = 'user' ) AS blocking_run_background_wake, ( SELECT item.payload_json FROM orchestration_v2_projection_turn_items item WHERE item.thread_id = t.thread_id AND item.run_id = blocked.run_id AND item.type = 'error' AND item.status = 'failed' AND item.node_id IS json_extract(blocked.payload_json, '$.rootNodeId') ORDER BY item.updated_at DESC, item.ordinal DESC, item.turn_item_id DESC LIMIT 1 ) AS blocking_failure_payload_json, ( SELECT request.payload_json FROM orchestration_v2_projection_runtime_requests request WHERE request.thread_id = t.thread_id AND request.status = 'pending' ORDER BY (request.kind = 'user_input') ASC, request.created_at DESC, request.runtime_request_id DESC LIMIT 1 ) AS pending_request_payload_json, ( SELECT secret.payload_json -- Keep runs outermost so completed history is never scanned for a pending secret. FROM orchestration_v2_projection_runs r INDEXED BY orchestration_v2_projection_runs_thread_status_idx CROSS JOIN orchestration_v2_projection_turn_items secret INDEXED BY orchestration_v2_projection_turn_items_thread_run_idx WHERE r.thread_id = t.thread_id AND r.status IN ('preparing', 'starting', 'running', 'waiting') AND secret.thread_id = t.thread_id AND secret.run_id = r.run_id AND secret.type = 'secret_request' AND secret.status = 'waiting' ORDER BY secret.updated_at DESC, secret.turn_item_id DESC LIMIT 1 ) AS pending_secret_request_payload_json, ( SELECT link.payload_json FROM orchestration_v2_projection_turn_items link INDEXED BY orchestration_v2_projection_turn_items_waiting_link_idx WHERE link.thread_id = t.thread_id AND link.type = 'link_request' AND link.status = 'waiting' ORDER BY link.updated_at DESC, link.turn_item_id DESC LIMIT 1 ) AS pending_link_request_payload_json, ( SELECT message.updated_at FROM orchestration_v2_projection_messages message WHERE message.thread_id = t.thread_id AND message.role = 'user' AND NOT ( message.role = 'user' AND lower(trim(json_extract(message.payload_json, '$.text'), char(9, 10, 11, 12, 13, 32, 160, 5760, 8192, 8193, 8194, 8195, 8196, 8197, 8198, 8199, 8200, 8201, 8202, 8232, 8233, 8239, 8287, 12288, 65279) )) = ? AND coalesce(json_array_length(message.payload_json, '$.attachments'), 0) = 0 AND ( json_extract(message.payload_json, '$.idleCompaction') IS NOT NULL OR ( json_extract(message.payload_json, '$.createdBy') = 'agent' AND json_extract(message.payload_json, '$.creationSource') = 'server' ) ) ) ORDER BY message.updated_at DESC, message.message_id DESC LIMIT 1 ) AS latest_user_message_at, ( SELECT message.updated_at FROM orchestration_v2_projection_messages message WHERE message.thread_id = t.thread_id AND message.role = 'user' AND json_extract(message.payload_json, '$.createdBy') = 'user' AND NOT ( message.role = 'user' AND lower(trim(json_extract(message.payload_json, '$.text'), char(9, 10, 11, 12, 13, 32, 160, 5760, 8192, 8193, 8194, 8195, 8196, 8197, 8198, 8199, 8200, 8201, 8202, 8232, 8233, 8239, 8287, 12288, 65279) )) = ? AND coalesce(json_array_length(message.payload_json, '$.attachments'), 0) = 0 AND ( json_extract(message.payload_json, '$.idleCompaction') IS NOT NULL OR ( json_extract(message.payload_json, '$.createdBy') = 'agent' AND json_extract(message.payload_json, '$.creationSource') = 'server' ) ) ) ORDER BY message.updated_at DESC, message.message_id DESC LIMIT 1 ) AS latest_user_authored_message_at, EXISTS ( SELECT 1 FROM orchestration_v2_projection_plans plan WHERE plan.thread_id = t.thread_id AND plan.kind = 'proposed_plan' AND plan.status = 'active' ) AS has_actionable_proposed_plan FROM orchestration_v2_projection_threads t LEFT JOIN orchestration_v2_projection_runs active ON active.run_id = ( SELECT r.run_id FROM orchestration_v2_projection_runs r INDEXED BY orchestration_v2_projection_runs_thread_status_idx WHERE r.thread_id = t.thread_id AND r.status IN ('preparing', 'starting', 'running') AND NOT ( r.status <> 'failed' AND EXISTS ( SELECT 1 FROM orchestration_v2_projection_messages prompt WHERE prompt.message_id = json_extract(r.payload_json, '$.userMessageId') AND ( prompt.role = 'user' AND lower(trim(json_extract(prompt.payload_json, '$.text'), char(9, 10, 11, 12, 13, 32, 160, 5760, 8192, 8193, 8194, 8195, 8196, 8197, 8198, 8199, 8200, 8201, 8202, 8232, 8233, 8239, 8287, 12288, 65279) )) = ? AND coalesce(json_array_length(prompt.payload_json, '$.attachments'), 0) = 0 AND ( json_extract(prompt.payload_json, '$.idleCompaction') IS NOT NULL OR ( json_extract(prompt.payload_json, '$.createdBy') = 'agent' AND json_extract(prompt.payload_json, '$.creationSource') = 'server' ) ) ) ) AND NOT EXISTS ( SELECT 1 FROM orchestration_v2_projection_turn_items compaction WHERE compaction.thread_id = r.thread_id AND compaction.run_id = r.run_id AND compaction.type = 'compaction' AND compaction.status = 'failed' ) ) ORDER BY r.ordinal DESC, r.run_id DESC LIMIT 1 ) LEFT JOIN orchestration_v2_projection_runs presented ON presented.run_id = ( SELECT candidate.run_id FROM orchestration_v2_projection_runs candidate WHERE candidate.thread_id = t.thread_id AND NOT ( candidate.status = 'queued' AND json_extract(candidate.payload_json, '$.queueHeld') IS 1 ) AND NOT ( candidate.status = 'cancelled' AND json_extract(candidate.payload_json, '$.startedAt') IS NULL AND candidate.completed_at IS NOT NULL AND EXISTS ( SELECT 1 FROM orchestration_v2_projection_runs earlier WHERE earlier.thread_id = candidate.thread_id AND earlier.ordinal < candidate.ordinal AND earlier.status <> 'queued' AND NOT ( earlier.status <> 'failed' AND EXISTS ( SELECT 1 FROM orchestration_v2_projection_messages prompt WHERE prompt.message_id = json_extract(earlier.payload_json, '$.userMessageId') AND ( prompt.role = 'user' AND lower(trim(json_extract(prompt.payload_json, '$.text'), char(9, 10, 11, 12, 13, 32, 160, 5760, 8192, 8193, 8194, 8195, 8196, 8197, 8198, 8199, 8200, 8201, 8202, 8232, 8233, 8239, 8287, 12288, 65279) )) = ? AND coalesce(json_array_length(prompt.payload_json, '$.attachments'), 0) = 0 AND ( json_extract(prompt.payload_json, '$.idleCompaction') IS NOT NULL OR ( json_extract(prompt.payload_json, '$.createdBy') = 'agent' AND json_extract(prompt.payload_json, '$.creationSource') = 'server' ) ) ) ) AND NOT EXISTS ( SELECT 1 FROM orchestration_v2_projection_turn_items compaction WHERE compaction.thread_id = earlier.thread_id AND compaction.run_id = earlier.run_id AND compaction.type = 'compaction' AND compaction.status = 'failed' ) ) AND ( earlier.completed_at IS NULL OR julianday(earlier.completed_at) >= julianday(candidate.completed_at) ) ) ) AND NOT ( candidate.status <> 'failed' AND EXISTS ( SELECT 1 FROM orchestration_v2_projection_messages prompt WHERE prompt.message_id = json_extract(candidate.payload_json, '$.userMessageId') AND ( prompt.role = 'user' AND lower(trim(json_extract(prompt.payload_json, '$.text'), char(9, 10, 11, 12, 13, 32, 160, 5760, 8192, 8193, 8194, 8195, 8196, 8197, 8198, 8199, 8200, 8201, 8202, 8232, 8233, 8239, 8287, 12288, 65279) )) = ? AND coalesce(json_array_length(prompt.payload_json, '$.attachments'), 0) = 0 AND ( json_extract(prompt.payload_json, '$.idleCompaction') IS NOT NULL OR ( json_extract(prompt.payload_json, '$.createdBy') = 'agent' AND json_extract(prompt.payload_json, '$.creationSource') = 'server' ) ) ) ) AND NOT EXISTS ( SELECT 1 FROM orchestration_v2_projection_turn_items compaction WHERE compaction.thread_id = candidate.thread_id AND compaction.run_id = candidate.run_id AND compaction.type = 'compaction' AND compaction.status = 'failed' ) ) ORDER BY candidate.ordinal DESC, candidate.run_id DESC LIMIT 1 ) LEFT JOIN orchestration_v2_projection_runs blocked ON blocked.run_id = ( SELECT candidate.run_id FROM orchestration_v2_projection_runs candidate WHERE candidate.thread_id = t.thread_id AND candidate.status <> 'queued' AND NOT ( candidate.status = 'cancelled' AND json_extract(candidate.payload_json, '$.startedAt') IS NULL ) -- latestExecutedRun: the run that ended last (runRanAfter). ORDER BY candidate.completed_at IS NULL DESC, candidate.completed_at DESC, candidate.ordinal DESC, candidate.run_id DESC LIMIT 1 ) AND blocked.status = 'failed' WHERE t.deleted_at IS NULL AND t.thread_id = ? ORDER BY t.updated_at ASC, t.thread_id ASC",
    "rows": 1
  },
  {
    "label": "edit",
    "sql": "SELECT counts.thread_id, SUM(CASE WHEN counts.run_key = '' OR r.status <> 'rolled_back' THEN counts.item_count - CASE WHEN r.status IN ('queued', 'cancelled') THEN ( SELECT COUNT(*) FROM orchestration_v2_projection_turn_items held INDEXED BY orchestration_v2_projection_turn_items_queued_input_idx WHERE held.run_id = counts.run_key AND held.type = 'user_message' AND json_extract(held.payload_json, '$.inputIntent') = 'queued_turn' ) ELSE 0 END ELSE 0 END) AS item_count, SUM(CASE WHEN counts.run_key = '' THEN counts.item_count ELSE 0 END) AS runless_item_count FROM orchestration_v2_projection_turn_item_counts counts LEFT JOIN orchestration_v2_projection_runs r ON r.run_id = counts.run_key WHERE counts.thread_id IN (SELECT value FROM json_each(?)) GROUP BY counts.thread_id",
    "rows": 1
  },
  {
    "label": "edit",
    "sql": "SELECT thread_id, payload_json FROM orchestration_v2_projection_provider_threads WHERE thread_id IN (SELECT value FROM json_each(?))",
    "rows": 1
  },
  {
    "label": "edit",
    "sql": "SELECT i.thread_id, i.payload_json, p.provider_instance_id AS owning_provider_instance_id FROM orchestration_v2_projection_turn_items i LEFT JOIN orchestration_v2_projection_provider_threads p ON p.provider_thread_id = i.provider_thread_id LEFT JOIN orchestration_v2_projection_runs r ON r.run_id = i.run_id WHERE i.type IN ('command_execution', 'dynamic_tool', 'subagent') AND i.status NOT IN ('completed', 'interrupted', 'failed', 'cancelled') AND (i.run_id IS NULL OR r.status <> 'rolled_back') AND i.thread_id IN (SELECT value FROM json_each(?)) -- Preserve the pending-item partial index; read other work only from running runs. UNION ALL SELECT i.thread_id, i.payload_json, NULL AS owning_provider_instance_id FROM orchestration_v2_projection_turn_items i WHERE i.run_id IN (SELECT value FROM json_each(?)) AND i.type IN ('file_change', 'file_search', 'web_search', 'compaction') AND i.status IN ('running', 'waiting')",
    "rows": 0
  },
  {
    "label": "edit",
    "sql": "SELECT payload_json FROM orchestration_v2_projection_threads WHERE thread_id = ? LIMIT 1",
    "rows": 1
  },
  {
    "label": "edit",
    "sql": "SELECT t.thread_id, t.payload_json, CASE WHEN json_extract(t.payload_json, '$.forkedFrom.type') = 'run' AND json_extract(t.payload_json, '$.lineage.sideChat') IS NOT 1 THEN json_extract(t.payload_json, '$.forkedFrom.threadId') ELSE NULL END AS forked_from_run_source_thread_id, presented.run_id AS latest_run_id, presented.status AS latest_run_status, presented.requested_at AS latest_run_requested_at, json_extract(presented.payload_json, '$.startedAt') AS latest_run_started_at, presented.completed_at AS latest_run_completed_at, EXISTS ( SELECT 1 FROM orchestration_v2_projection_turn_items stop_request WHERE stop_request.thread_id = t.thread_id AND stop_request.run_id = presented.run_id AND stop_request.type = 'run_interrupt_request' ) AS latest_run_stop_requested, EXISTS ( SELECT 1 FROM orchestration_v2_projection_messages wake WHERE wake.message_id = json_extract(presented.payload_json, '$.userMessageId') AND json_extract(wake.payload_json, '$.notification.source.kind') IN ('background_task', 'background_command', 'command', 'monitor') AND json_extract(wake.payload_json, '$.notification.source.work') IS NULL AND json_extract(wake.payload_json, '$.notification.outcome') = 'updated' AND json_extract(wake.payload_json, '$.delegatedCompletion') IS NULL ) AND NOT EXISTS ( SELECT 1 FROM orchestration_v2_projection_messages steer WHERE steer.run_id = presented.run_id AND json_extract(steer.payload_json, '$.createdBy') = 'user' ) AS latest_run_background_wake, active.run_id AS active_run_id, active.status AS active_run_status, ( SELECT r.status -- Read active runs by status. Walking the ordinal index to skip this sort -- reads every run of a thread with none active. FROM orchestration_v2_projection_runs r INDEXED BY orchestration_v2_projection_runs_thread_status_idx WHERE r.thread_id = t.thread_id AND r.status IN ('preparing', 'starting', 'running', 'waiting') AND NOT ( r.status <> 'failed' AND EXISTS ( SELECT 1 FROM orchestration_v2_projection_messages prompt WHERE prompt.message_id = json_extract(r.payload_json, '$.userMessageId') AND ( prompt.role = 'user' AND lower(trim(json_extract(prompt.payload_json, '$.text'), char(9, 10, 11, 12, 13, 32, 160, 5760, 8192, 8193, 8194, 8195, 8196, 8197, 8198, 8199, 8200, 8201, 8202, 8232, 8233, 8239, 8287, 12288, 65279) )) = ? AND coalesce(json_array_length(prompt.payload_json, '$.attachments'), 0) = 0 AND ( json_extract(prompt.payload_json, '$.idleCompaction') IS NOT NULL OR ( json_extract(prompt.payload_json, '$.createdBy') = 'agent' AND json_extract(prompt.payload_json, '$.creationSource') = 'server' ) ) ) ) AND NOT EXISTS ( SELECT 1 FROM orchestration_v2_projection_turn_items compaction WHERE compaction.thread_id = r.thread_id AND compaction.run_id = r.run_id AND compaction.type = 'compaction' AND compaction.status = 'failed' ) ) ORDER BY r.ordinal DESC, r.run_id DESC LIMIT 1 ) AS activity_run_status, ( -- Mirrors orchestrationV2RunWorkStartedAt. SELECT COALESCE( json_extract(r.payload_json, '$.workStartedAt'), json_extract(r.payload_json, '$.startedAt'), r.requested_at ) FROM orchestration_v2_projection_runs r INDEXED BY orchestration_v2_projection_runs_thread_status_idx WHERE r.thread_id = t.thread_id AND r.status IN ('preparing', 'starting', 'running', 'waiting') AND NOT ( r.status <> 'failed' AND EXISTS ( SELECT 1 FROM orchestration_v2_projection_messages prompt WHERE prompt.message_id = json_extract(r.payload_json, '$.userMessageId') AND ( prompt.role = 'user' AND lower(trim(json_extract(prompt.payload_json, '$.text'), char(9, 10, 11, 12, 13, 32, 160, 5760, 8192, 8193, 8194, 8195, 8196, 8197, 8198, 8199, 8200, 8201, 8202, 8232, 8233, 8239, 8287, 12288, 65279) )) = ? AND coalesce(json_array_length(prompt.payload_json, '$.attachments'), 0) = 0 AND ( json_extract(prompt.payload_json, '$.idleCompaction') IS NOT NULL OR ( json_extract(prompt.payload_json, '$.createdBy') = 'agent' AND json_extract(prompt.payload_json, '$.creationSource') = 'server' ) ) ) ) AND NOT EXISTS ( SELECT 1 FROM orchestration_v2_projection_turn_items compaction WHERE compaction.thread_id = r.thread_id AND compaction.run_id = r.run_id AND compaction.type = 'compaction' AND compaction.status = 'failed' ) ) ORDER BY r.ordinal DESC, r.run_id DESC LIMIT 1 ) AS activity_run_started_at, ( SELECT json_extract(session.payload_json, '$.lastError') -- Keep bindings outermost: one provider instance can hold every session. FROM orchestration_v2_projection_provider_session_bindings binding CROSS JOIN orchestration_v2_projection_provider_sessions session ON session.provider_session_id = binding.provider_session_id WHERE binding.thread_id = t.thread_id AND session.provider_instance_id = t.provider_instance_id ORDER BY session.updated_at DESC, session.provider_session_id DESC LIMIT 1 ) AS last_error, ( SELECT item.payload_json FROM orchestration_v2_projection_turn_items item INNER JOIN orchestration_v2_projection_runs r ON r.run_id = item.run_id WHERE r.run_id = presented.run_id AND r.status = 'failed' AND item.thread_id = t.thread_id AND item.type = 'error' AND item.status = 'failed' AND item.node_id IS json_extract(r.payload_json, '$.rootNodeId') ORDER BY item.updated_at DESC, item.ordinal DESC, item.turn_item_id DESC LIMIT 1 ) AS terminal_failure_payload_json, blocked.run_id AS blocking_run_id, blocked.requested_at AS blocking_run_requested_at, json_extract(blocked.payload_json, '$.startedAt') AS blocking_run_started_at, blocked.completed_at AS blocking_run_completed_at, EXISTS ( SELECT 1 FROM orchestration_v2_projection_turn_items stop_request WHERE stop_request.thread_id = t.thread_id AND stop_request.run_id = blocked.run_id AND stop_request.type = 'run_interrupt_request' ) AS blocking_run_stop_requested, EXISTS ( SELECT 1 FROM orchestration_v2_projection_messages wake WHERE wake.message_id = json_extract(blocked.payload_json, '$.userMessageId') AND json_extract(wake.payload_json, '$.notification.source.kind') IN ('background_task', 'background_command', 'command', 'monitor') AND json_extract(wake.payload_json, '$.notification.source.work') IS NULL AND json_extract(wake.payload_json, '$.notification.outcome') = 'updated' AND json_extract(wake.payload_json, '$.delegatedCompletion') IS NULL ) AND NOT EXISTS ( SELECT 1 FROM orchestration_v2_projection_messages steer WHERE steer.run_id = blocked.run_id AND json_extract(steer.payload_json, '$.createdBy') = 'user' ) AS blocking_run_background_wake, ( SELECT item.payload_json FROM orchestration_v2_projection_turn_items item WHERE item.thread_id = t.thread_id AND item.run_id = blocked.run_id AND item.type = 'error' AND item.status = 'failed' AND item.node_id IS json_extract(blocked.payload_json, '$.rootNodeId') ORDER BY item.updated_at DESC, item.ordinal DESC, item.turn_item_id DESC LIMIT 1 ) AS blocking_failure_payload_json, ( SELECT request.payload_json FROM orchestration_v2_projection_runtime_requests request WHERE request.thread_id = t.thread_id AND request.status = 'pending' ORDER BY (request.kind = 'user_input') ASC, request.created_at DESC, request.runtime_request_id DESC LIMIT 1 ) AS pending_request_payload_json, ( SELECT secret.payload_json -- Keep runs outermost so completed history is never scanned for a pending secret. FROM orchestration_v2_projection_runs r INDEXED BY orchestration_v2_projection_runs_thread_status_idx CROSS JOIN orchestration_v2_projection_turn_items secret INDEXED BY orchestration_v2_projection_turn_items_thread_run_idx WHERE r.thread_id = t.thread_id AND r.status IN ('preparing', 'starting', 'running', 'waiting') AND secret.thread_id = t.thread_id AND secret.run_id = r.run_id AND secret.type = 'secret_request' AND secret.status = 'waiting' ORDER BY secret.updated_at DESC, secret.turn_item_id DESC LIMIT 1 ) AS pending_secret_request_payload_json, ( SELECT link.payload_json FROM orchestration_v2_projection_turn_items link INDEXED BY orchestration_v2_projection_turn_items_waiting_link_idx WHERE link.thread_id = t.thread_id AND link.type = 'link_request' AND link.status = 'waiting' ORDER BY link.updated_at DESC, link.turn_item_id DESC LIMIT 1 ) AS pending_link_request_payload_json, ( SELECT message.updated_at FROM orchestration_v2_projection_messages message WHERE message.thread_id = t.thread_id AND message.role = 'user' AND NOT ( message.role = 'user' AND lower(trim(json_extract(message.payload_json, '$.text'), char(9, 10, 11, 12, 13, 32, 160, 5760, 8192, 8193, 8194, 8195, 8196, 8197, 8198, 8199, 8200, 8201, 8202, 8232, 8233, 8239, 8287, 12288, 65279) )) = ? AND coalesce(json_array_length(message.payload_json, '$.attachments'), 0) = 0 AND ( json_extract(message.payload_json, '$.idleCompaction') IS NOT NULL OR ( json_extract(message.payload_json, '$.createdBy') = 'agent' AND json_extract(message.payload_json, '$.creationSource') = 'server' ) ) ) ORDER BY message.updated_at DESC, message.message_id DESC LIMIT 1 ) AS latest_user_message_at, ( SELECT message.updated_at FROM orchestration_v2_projection_messages message WHERE message.thread_id = t.thread_id AND message.role = 'user' AND json_extract(message.payload_json, '$.createdBy') = 'user' AND NOT ( message.role = 'user' AND lower(trim(json_extract(message.payload_json, '$.text'), char(9, 10, 11, 12, 13, 32, 160, 5760, 8192, 8193, 8194, 8195, 8196, 8197, 8198, 8199, 8200, 8201, 8202, 8232, 8233, 8239, 8287, 12288, 65279) )) = ? AND coalesce(json_array_length(message.payload_json, '$.attachments'), 0) = 0 AND ( json_extract(message.payload_json, '$.idleCompaction') IS NOT NULL OR ( json_extract(message.payload_json, '$.createdBy') = 'agent' AND json_extract(message.payload_json, '$.creationSource') = 'server' ) ) ) ORDER BY message.updated_at DESC, message.message_id DESC LIMIT 1 ) AS latest_user_authored_message_at, EXISTS ( SELECT 1 FROM orchestration_v2_projection_plans plan WHERE plan.thread_id = t.thread_id AND plan.kind = 'proposed_plan' AND plan.status = 'active' ) AS has_actionable_proposed_plan FROM orchestration_v2_projection_threads t LEFT JOIN orchestration_v2_projection_runs active ON active.run_id = ( SELECT r.run_id FROM orchestration_v2_projection_runs r INDEXED BY orchestration_v2_projection_runs_thread_status_idx WHERE r.thread_id = t.thread_id AND r.status IN ('preparing', 'starting', 'running') AND NOT ( r.status <> 'failed' AND EXISTS ( SELECT 1 FROM orchestration_v2_projection_messages prompt WHERE prompt.message_id = json_extract(r.payload_json, '$.userMessageId') AND ( prompt.role = 'user' AND lower(trim(json_extract(prompt.payload_json, '$.text'), char(9, 10, 11, 12, 13, 32, 160, 5760, 8192, 8193, 8194, 8195, 8196, 8197, 8198, 8199, 8200, 8201, 8202, 8232, 8233, 8239, 8287, 12288, 65279) )) = ? AND coalesce(json_array_length(prompt.payload_json, '$.attachments'), 0) = 0 AND ( json_extract(prompt.payload_json, '$.idleCompaction') IS NOT NULL OR ( json_extract(prompt.payload_json, '$.createdBy') = 'agent' AND json_extract(prompt.payload_json, '$.creationSource') = 'server' ) ) ) ) AND NOT EXISTS ( SELECT 1 FROM orchestration_v2_projection_turn_items compaction WHERE compaction.thread_id = r.thread_id AND compaction.run_id = r.run_id AND compaction.type = 'compaction' AND compaction.status = 'failed' ) ) ORDER BY r.ordinal DESC, r.run_id DESC LIMIT 1 ) LEFT JOIN orchestration_v2_projection_runs presented ON presented.run_id = ( SELECT candidate.run_id FROM orchestration_v2_projection_runs candidate WHERE candidate.thread_id = t.thread_id AND NOT ( candidate.status = 'queued' AND json_extract(candidate.payload_json, '$.queueHeld') IS 1 ) AND NOT ( candidate.status = 'cancelled' AND json_extract(candidate.payload_json, '$.startedAt') IS NULL AND candidate.completed_at IS NOT NULL AND EXISTS ( SELECT 1 FROM orchestration_v2_projection_runs earlier WHERE earlier.thread_id = candidate.thread_id AND earlier.ordinal < candidate.ordinal AND earlier.status <> 'queued' AND NOT ( earlier.status <> 'failed' AND EXISTS ( SELECT 1 FROM orchestration_v2_projection_messages prompt WHERE prompt.message_id = json_extract(earlier.payload_json, '$.userMessageId') AND ( prompt.role = 'user' AND lower(trim(json_extract(prompt.payload_json, '$.text'), char(9, 10, 11, 12, 13, 32, 160, 5760, 8192, 8193, 8194, 8195, 8196, 8197, 8198, 8199, 8200, 8201, 8202, 8232, 8233, 8239, 8287, 12288, 65279) )) = ? AND coalesce(json_array_length(prompt.payload_json, '$.attachments'), 0) = 0 AND ( json_extract(prompt.payload_json, '$.idleCompaction') IS NOT NULL OR ( json_extract(prompt.payload_json, '$.createdBy') = 'agent' AND json_extract(prompt.payload_json, '$.creationSource') = 'server' ) ) ) ) AND NOT EXISTS ( SELECT 1 FROM orchestration_v2_projection_turn_items compaction WHERE compaction.thread_id = earlier.thread_id AND compaction.run_id = earlier.run_id AND compaction.type = 'compaction' AND compaction.status = 'failed' ) ) AND ( earlier.completed_at IS NULL OR julianday(earlier.completed_at) >= julianday(candidate.completed_at) ) ) ) AND NOT ( candidate.status <> 'failed' AND EXISTS ( SELECT 1 FROM orchestration_v2_projection_messages prompt WHERE prompt.message_id = json_extract(candidate.payload_json, '$.userMessageId') AND ( prompt.role = 'user' AND lower(trim(json_extract(prompt.payload_json, '$.text'), char(9, 10, 11, 12, 13, 32, 160, 5760, 8192, 8193, 8194, 8195, 8196, 8197, 8198, 8199, 8200, 8201, 8202, 8232, 8233, 8239, 8287, 12288, 65279) )) = ? AND coalesce(json_array_length(prompt.payload_json, '$.attachments'), 0) = 0 AND ( json_extract(prompt.payload_json, '$.idleCompaction') IS NOT NULL OR ( json_extract(prompt.payload_json, '$.createdBy') = 'agent' AND json_extract(prompt.payload_json, '$.creationSource') = 'server' ) ) ) ) AND NOT EXISTS ( SELECT 1 FROM orchestration_v2_projection_turn_items compaction WHERE compaction.thread_id = candidate.thread_id AND compaction.run_id = candidate.run_id AND compaction.type = 'compaction' AND compaction.status = 'failed' ) ) ORDER BY candidate.ordinal DESC, candidate.run_id DESC LIMIT 1 ) LEFT JOIN orchestration_v2_projection_runs blocked ON blocked.run_id = ( SELECT candidate.run_id FROM orchestration_v2_projection_runs candidate WHERE candidate.thread_id = t.thread_id AND candidate.status <> 'queued' AND NOT ( candidate.status = 'cancelled' AND json_extract(candidate.payload_json, '$.startedAt') IS NULL ) -- latestExecutedRun: the run that ended last (runRanAfter). ORDER BY candidate.completed_at IS NULL DESC, candidate.completed_at DESC, candidate.ordinal DESC, candidate.run_id DESC LIMIT 1 ) AND blocked.status = 'failed' WHERE t.deleted_at IS NULL AND t.thread_id = ? ORDER BY t.updated_at ASC, t.thread_id ASC",
    "rows": 1
  },
  {
    "label": "edit",
    "sql": "SELECT counts.thread_id, SUM(CASE WHEN counts.run_key = '' OR r.status <> 'rolled_back' THEN counts.item_count - CASE WHEN r.status IN ('queued', 'cancelled') THEN ( SELECT COUNT(*) FROM orchestration_v2_projection_turn_items held INDEXED BY orchestration_v2_projection_turn_items_queued_input_idx WHERE held.run_id = counts.run_key AND held.type = 'user_message' AND json_extract(held.payload_json, '$.inputIntent') = 'queued_turn' ) ELSE 0 END ELSE 0 END) AS item_count, SUM(CASE WHEN counts.run_key = '' THEN counts.item_count ELSE 0 END) AS runless_item_count FROM orchestration_v2_projection_turn_item_counts counts LEFT JOIN orchestration_v2_projection_runs r ON r.run_id = counts.run_key WHERE counts.thread_id IN (SELECT value FROM json_each(?)) GROUP BY counts.thread_id",
    "rows": 1
  },
  {
    "label": "edit",
    "sql": "SELECT thread_id, payload_json FROM orchestration_v2_projection_provider_threads WHERE thread_id IN (SELECT value FROM json_each(?))",
    "rows": 1
  },
  {
    "label": "edit",
    "sql": "SELECT i.thread_id, i.payload_json, p.provider_instance_id AS owning_provider_instance_id FROM orchestration_v2_projection_turn_items i LEFT JOIN orchestration_v2_projection_provider_threads p ON p.provider_thread_id = i.provider_thread_id LEFT JOIN orchestration_v2_projection_runs r ON r.run_id = i.run_id WHERE i.type IN ('command_execution', 'dynamic_tool', 'subagent') AND i.status NOT IN ('completed', 'interrupted', 'failed', 'cancelled') AND (i.run_id IS NULL OR r.status <> 'rolled_back') AND i.thread_id IN (SELECT value FROM json_each(?)) -- Preserve the pending-item partial index; read other work only from running runs. UNION ALL SELECT i.thread_id, i.payload_json, NULL AS owning_provider_instance_id FROM orchestration_v2_projection_turn_items i WHERE i.run_id IN (SELECT value FROM json_each(?)) AND i.type IN ('file_change', 'file_search', 'web_search', 'compaction') AND i.status IN ('running', 'waiting')",
    "rows": 0
  },
  {
    "label": "edit",
    "sql": "SELECT payload_json FROM orchestration_v2_projection_threads WHERE thread_id = ? LIMIT 1",
    "rows": 1
  },
  {
    "label": "edit",
    "sql": "SELECT payload_json FROM orchestration_v2_projection_turn_items WHERE rowid IN ( SELECT rowid FROM orchestration_v2_projection_turn_items WHERE thread_id = ? AND type IN (SELECT value FROM json_each(?)) ) ORDER BY ordinal ASC, turn_item_id ASC",
    "rows": 104
  },
  {
    "label": "edit",
    "sql": "SELECT payload_json FROM orchestration_v2_projection_runs WHERE thread_id = ? ORDER BY ordinal ASC",
    "rows": 107
  },
  {
    "label": "edit",
    "sql": "SELECT payload_json FROM orchestration_v2_projection_messages WHERE thread_id = ? AND role IN (SELECT value FROM json_each(?)) ORDER BY created_at ASC, message_id ASC",
    "rows": 107
  },
  {
    "label": "edit",
    "sql": "SELECT t.thread_id, t.payload_json, CASE WHEN json_extract(t.payload_json, '$.forkedFrom.type') = 'run' AND json_extract(t.payload_json, '$.lineage.sideChat') IS NOT 1 THEN json_extract(t.payload_json, '$.forkedFrom.threadId') ELSE NULL END AS forked_from_run_source_thread_id, presented.run_id AS latest_run_id, presented.status AS latest_run_status, presented.requested_at AS latest_run_requested_at, json_extract(presented.payload_json, '$.startedAt') AS latest_run_started_at, presented.completed_at AS latest_run_completed_at, EXISTS ( SELECT 1 FROM orchestration_v2_projection_turn_items stop_request WHERE stop_request.thread_id = t.thread_id AND stop_request.run_id = presented.run_id AND stop_request.type = 'run_interrupt_request' ) AS latest_run_stop_requested, EXISTS ( SELECT 1 FROM orchestration_v2_projection_messages wake WHERE wake.message_id = json_extract(presented.payload_json, '$.userMessageId') AND json_extract(wake.payload_json, '$.notification.source.kind') IN ('background_task', 'background_command', 'command', 'monitor') AND json_extract(wake.payload_json, '$.notification.source.work') IS NULL AND json_extract(wake.payload_json, '$.notification.outcome') = 'updated' AND json_extract(wake.payload_json, '$.delegatedCompletion') IS NULL ) AND NOT EXISTS ( SELECT 1 FROM orchestration_v2_projection_messages steer WHERE steer.run_id = presented.run_id AND json_extract(steer.payload_json, '$.createdBy') = 'user' ) AS latest_run_background_wake, active.run_id AS active_run_id, active.status AS active_run_status, ( SELECT r.status -- Read active runs by status. Walking the ordinal index to skip this sort -- reads every run of a thread with none active. FROM orchestration_v2_projection_runs r INDEXED BY orchestration_v2_projection_runs_thread_status_idx WHERE r.thread_id = t.thread_id AND r.status IN ('preparing', 'starting', 'running', 'waiting') AND NOT ( r.status <> 'failed' AND EXISTS ( SELECT 1 FROM orchestration_v2_projection_messages prompt WHERE prompt.message_id = json_extract(r.payload_json, '$.userMessageId') AND ( prompt.role = 'user' AND lower(trim(json_extract(prompt.payload_json, '$.text'), char(9, 10, 11, 12, 13, 32, 160, 5760, 8192, 8193, 8194, 8195, 8196, 8197, 8198, 8199, 8200, 8201, 8202, 8232, 8233, 8239, 8287, 12288, 65279) )) = ? AND coalesce(json_array_length(prompt.payload_json, '$.attachments'), 0) = 0 AND ( json_extract(prompt.payload_json, '$.idleCompaction') IS NOT NULL OR ( json_extract(prompt.payload_json, '$.createdBy') = 'agent' AND json_extract(prompt.payload_json, '$.creationSource') = 'server' ) ) ) ) AND NOT EXISTS ( SELECT 1 FROM orchestration_v2_projection_turn_items compaction WHERE compaction.thread_id = r.thread_id AND compaction.run_id = r.run_id AND compaction.type = 'compaction' AND compaction.status = 'failed' ) ) ORDER BY r.ordinal DESC, r.run_id DESC LIMIT 1 ) AS activity_run_status, ( -- Mirrors orchestrationV2RunWorkStartedAt. SELECT COALESCE( json_extract(r.payload_json, '$.workStartedAt'), json_extract(r.payload_json, '$.startedAt'), r.requested_at ) FROM orchestration_v2_projection_runs r INDEXED BY orchestration_v2_projection_runs_thread_status_idx WHERE r.thread_id = t.thread_id AND r.status IN ('preparing', 'starting', 'running', 'waiting') AND NOT ( r.status <> 'failed' AND EXISTS ( SELECT 1 FROM orchestration_v2_projection_messages prompt WHERE prompt.message_id = json_extract(r.payload_json, '$.userMessageId') AND ( prompt.role = 'user' AND lower(trim(json_extract(prompt.payload_json, '$.text'), char(9, 10, 11, 12, 13, 32, 160, 5760, 8192, 8193, 8194, 8195, 8196, 8197, 8198, 8199, 8200, 8201, 8202, 8232, 8233, 8239, 8287, 12288, 65279) )) = ? AND coalesce(json_array_length(prompt.payload_json, '$.attachments'), 0) = 0 AND ( json_extract(prompt.payload_json, '$.idleCompaction') IS NOT NULL OR ( json_extract(prompt.payload_json, '$.createdBy') = 'agent' AND json_extract(prompt.payload_json, '$.creationSource') = 'server' ) ) ) ) AND NOT EXISTS ( SELECT 1 FROM orchestration_v2_projection_turn_items compaction WHERE compaction.thread_id = r.thread_id AND compaction.run_id = r.run_id AND compaction.type = 'compaction' AND compaction.status = 'failed' ) ) ORDER BY r.ordinal DESC, r.run_id DESC LIMIT 1 ) AS activity_run_started_at, ( SELECT json_extract(session.payload_json, '$.lastError') -- Keep bindings outermost: one provider instance can hold every session. FROM orchestration_v2_projection_provider_session_bindings binding CROSS JOIN orchestration_v2_projection_provider_sessions session ON session.provider_session_id = binding.provider_session_id WHERE binding.thread_id = t.thread_id AND session.provider_instance_id = t.provider_instance_id ORDER BY session.updated_at DESC, session.provider_session_id DESC LIMIT 1 ) AS last_error, ( SELECT item.payload_json FROM orchestration_v2_projection_turn_items item INNER JOIN orchestration_v2_projection_runs r ON r.run_id = item.run_id WHERE r.run_id = presented.run_id AND r.status = 'failed' AND item.thread_id = t.thread_id AND item.type = 'error' AND item.status = 'failed' AND item.node_id IS json_extract(r.payload_json, '$.rootNodeId') ORDER BY item.updated_at DESC, item.ordinal DESC, item.turn_item_id DESC LIMIT 1 ) AS terminal_failure_payload_json, blocked.run_id AS blocking_run_id, blocked.requested_at AS blocking_run_requested_at, json_extract(blocked.payload_json, '$.startedAt') AS blocking_run_started_at, blocked.completed_at AS blocking_run_completed_at, EXISTS ( SELECT 1 FROM orchestration_v2_projection_turn_items stop_request WHERE stop_request.thread_id = t.thread_id AND stop_request.run_id = blocked.run_id AND stop_request.type = 'run_interrupt_request' ) AS blocking_run_stop_requested, EXISTS ( SELECT 1 FROM orchestration_v2_projection_messages wake WHERE wake.message_id = json_extract(blocked.payload_json, '$.userMessageId') AND json_extract(wake.payload_json, '$.notification.source.kind') IN ('background_task', 'background_command', 'command', 'monitor') AND json_extract(wake.payload_json, '$.notification.source.work') IS NULL AND json_extract(wake.payload_json, '$.notification.outcome') = 'updated' AND json_extract(wake.payload_json, '$.delegatedCompletion') IS NULL ) AND NOT EXISTS ( SELECT 1 FROM orchestration_v2_projection_messages steer WHERE steer.run_id = blocked.run_id AND json_extract(steer.payload_json, '$.createdBy') = 'user' ) AS blocking_run_background_wake, ( SELECT item.payload_json FROM orchestration_v2_projection_turn_items item WHERE item.thread_id = t.thread_id AND item.run_id = blocked.run_id AND item.type = 'error' AND item.status = 'failed' AND item.node_id IS json_extract(blocked.payload_json, '$.rootNodeId') ORDER BY item.updated_at DESC, item.ordinal DESC, item.turn_item_id DESC LIMIT 1 ) AS blocking_failure_payload_json, ( SELECT request.payload_json FROM orchestration_v2_projection_runtime_requests request WHERE request.thread_id = t.thread_id AND request.status = 'pending' ORDER BY (request.kind = 'user_input') ASC, request.created_at DESC, request.runtime_request_id DESC LIMIT 1 ) AS pending_request_payload_json, ( SELECT secret.payload_json -- Keep runs outermost so completed history is never scanned for a pending secret. FROM orchestration_v2_projection_runs r INDEXED BY orchestration_v2_projection_runs_thread_status_idx CROSS JOIN orchestration_v2_projection_turn_items secret INDEXED BY orchestration_v2_projection_turn_items_thread_run_idx WHERE r.thread_id = t.thread_id AND r.status IN ('preparing', 'starting', 'running', 'waiting') AND secret.thread_id = t.thread_id AND secret.run_id = r.run_id AND secret.type = 'secret_request' AND secret.status = 'waiting' ORDER BY secret.updated_at DESC, secret.turn_item_id DESC LIMIT 1 ) AS pending_secret_request_payload_json, ( SELECT link.payload_json FROM orchestration_v2_projection_turn_items link INDEXED BY orchestration_v2_projection_turn_items_waiting_link_idx WHERE link.thread_id = t.thread_id AND link.type = 'link_request' AND link.status = 'waiting' ORDER BY link.updated_at DESC, link.turn_item_id DESC LIMIT 1 ) AS pending_link_request_payload_json, ( SELECT message.updated_at FROM orchestration_v2_projection_messages message WHERE message.thread_id = t.thread_id AND message.role = 'user' AND NOT ( message.role = 'user' AND lower(trim(json_extract(message.payload_json, '$.text'), char(9, 10, 11, 12, 13, 32, 160, 5760, 8192, 8193, 8194, 8195, 8196, 8197, 8198, 8199, 8200, 8201, 8202, 8232, 8233, 8239, 8287, 12288, 65279) )) = ? AND coalesce(json_array_length(message.payload_json, '$.attachments'), 0) = 0 AND ( json_extract(message.payload_json, '$.idleCompaction') IS NOT NULL OR ( json_extract(message.payload_json, '$.createdBy') = 'agent' AND json_extract(message.payload_json, '$.creationSource') = 'server' ) ) ) ORDER BY message.updated_at DESC, message.message_id DESC LIMIT 1 ) AS latest_user_message_at, ( SELECT message.updated_at FROM orchestration_v2_projection_messages message WHERE message.thread_id = t.thread_id AND message.role = 'user' AND json_extract(message.payload_json, '$.createdBy') = 'user' AND NOT ( message.role = 'user' AND lower(trim(json_extract(message.payload_json, '$.text'), char(9, 10, 11, 12, 13, 32, 160, 5760, 8192, 8193, 8194, 8195, 8196, 8197, 8198, 8199, 8200, 8201, 8202, 8232, 8233, 8239, 8287, 12288, 65279) )) = ? AND coalesce(json_array_length(message.payload_json, '$.attachments'), 0) = 0 AND ( json_extract(message.payload_json, '$.idleCompaction') IS NOT NULL OR ( json_extract(message.payload_json, '$.createdBy') = 'agent' AND json_extract(message.payload_json, '$.creationSource') = 'server' ) ) ) ORDER BY message.updated_at DESC, message.message_id DESC LIMIT 1 ) AS latest_user_authored_message_at, EXISTS ( SELECT 1 FROM orchestration_v2_projection_plans plan WHERE plan.thread_id = t.thread_id AND plan.kind = 'proposed_plan' AND plan.status = 'active' ) AS has_actionable_proposed_plan FROM orchestration_v2_projection_threads t LEFT JOIN orchestration_v2_projection_runs active ON active.run_id = ( SELECT r.run_id FROM orchestration_v2_projection_runs r INDEXED BY orchestration_v2_projection_runs_thread_status_idx WHERE r.thread_id = t.thread_id AND r.status IN ('preparing', 'starting', 'running') AND NOT ( r.status <> 'failed' AND EXISTS ( SELECT 1 FROM orchestration_v2_projection_messages prompt WHERE prompt.message_id = json_extract(r.payload_json, '$.userMessageId') AND ( prompt.role = 'user' AND lower(trim(json_extract(prompt.payload_json, '$.text'), char(9, 10, 11, 12, 13, 32, 160, 5760, 8192, 8193, 8194, 8195, 8196, 8197, 8198, 8199, 8200, 8201, 8202, 8232, 8233, 8239, 8287, 12288, 65279) )) = ? AND coalesce(json_array_length(prompt.payload_json, '$.attachments'), 0) = 0 AND ( json_extract(prompt.payload_json, '$.idleCompaction') IS NOT NULL OR ( json_extract(prompt.payload_json, '$.createdBy') = 'agent' AND json_extract(prompt.payload_json, '$.creationSource') = 'server' ) ) ) ) AND NOT EXISTS ( SELECT 1 FROM orchestration_v2_projection_turn_items compaction WHERE compaction.thread_id = r.thread_id AND compaction.run_id = r.run_id AND compaction.type = 'compaction' AND compaction.status = 'failed' ) ) ORDER BY r.ordinal DESC, r.run_id DESC LIMIT 1 ) LEFT JOIN orchestration_v2_projection_runs presented ON presented.run_id = ( SELECT candidate.run_id FROM orchestration_v2_projection_runs candidate WHERE candidate.thread_id = t.thread_id AND NOT ( candidate.status = 'queued' AND json_extract(candidate.payload_json, '$.queueHeld') IS 1 ) AND NOT ( candidate.status = 'cancelled' AND json_extract(candidate.payload_json, '$.startedAt') IS NULL AND candidate.completed_at IS NOT NULL AND EXISTS ( SELECT 1 FROM orchestration_v2_projection_runs earlier WHERE earlier.thread_id = candidate.thread_id AND earlier.ordinal < candidate.ordinal AND earlier.status <> 'queued' AND NOT ( earlier.status <> 'failed' AND EXISTS ( SELECT 1 FROM orchestration_v2_projection_messages prompt WHERE prompt.message_id = json_extract(earlier.payload_json, '$.userMessageId') AND ( prompt.role = 'user' AND lower(trim(json_extract(prompt.payload_json, '$.text'), char(9, 10, 11, 12, 13, 32, 160, 5760, 8192, 8193, 8194, 8195, 8196, 8197, 8198, 8199, 8200, 8201, 8202, 8232, 8233, 8239, 8287, 12288, 65279) )) = ? AND coalesce(json_array_length(prompt.payload_json, '$.attachments'), 0) = 0 AND ( json_extract(prompt.payload_json, '$.idleCompaction') IS NOT NULL OR ( json_extract(prompt.payload_json, '$.createdBy') = 'agent' AND json_extract(prompt.payload_json, '$.creationSource') = 'server' ) ) ) ) AND NOT EXISTS ( SELECT 1 FROM orchestration_v2_projection_turn_items compaction WHERE compaction.thread_id = earlier.thread_id AND compaction.run_id = earlier.run_id AND compaction.type = 'compaction' AND compaction.status = 'failed' ) ) AND ( earlier.completed_at IS NULL OR julianday(earlier.completed_at) >= julianday(candidate.completed_at) ) ) ) AND NOT ( candidate.status <> 'failed' AND EXISTS ( SELECT 1 FROM orchestration_v2_projection_messages prompt WHERE prompt.message_id = json_extract(candidate.payload_json, '$.userMessageId') AND ( prompt.role = 'user' AND lower(trim(json_extract(prompt.payload_json, '$.text'), char(9, 10, 11, 12, 13, 32, 160, 5760, 8192, 8193, 8194, 8195, 8196, 8197, 8198, 8199, 8200, 8201, 8202, 8232, 8233, 8239, 8287, 12288, 65279) )) = ? AND coalesce(json_array_length(prompt.payload_json, '$.attachments'), 0) = 0 AND ( json_extract(prompt.payload_json, '$.idleCompaction') IS NOT NULL OR ( json_extract(prompt.payload_json, '$.createdBy') = 'agent' AND json_extract(prompt.payload_json, '$.creationSource') = 'server' ) ) ) ) AND NOT EXISTS ( SELECT 1 FROM orchestration_v2_projection_turn_items compaction WHERE compaction.thread_id = candidate.thread_id AND compaction.run_id = candidate.run_id AND compaction.type = 'compaction' AND compaction.status = 'failed' ) ) ORDER BY candidate.ordinal DESC, candidate.run_id DESC LIMIT 1 ) LEFT JOIN orchestration_v2_projection_runs blocked ON blocked.run_id = ( SELECT candidate.run_id FROM orchestration_v2_projection_runs candidate WHERE candidate.thread_id = t.thread_id AND candidate.status <> 'queued' AND NOT ( candidate.status = 'cancelled' AND json_extract(candidate.payload_json, '$.startedAt') IS NULL ) -- latestExecutedRun: the run that ended last (runRanAfter). ORDER BY candidate.completed_at IS NULL DESC, candidate.completed_at DESC, candidate.ordinal DESC, candidate.run_id DESC LIMIT 1 ) AND blocked.status = 'failed' WHERE t.deleted_at IS NULL AND t.thread_id = ? ORDER BY t.updated_at ASC, t.thread_id ASC",
    "rows": 1
  },
  {
    "label": "edit",
    "sql": "SELECT counts.thread_id, SUM(CASE WHEN counts.run_key = '' OR r.status <> 'rolled_back' THEN counts.item_count - CASE WHEN r.status IN ('queued', 'cancelled') THEN ( SELECT COUNT(*) FROM orchestration_v2_projection_turn_items held INDEXED BY orchestration_v2_projection_turn_items_queued_input_idx WHERE held.run_id = counts.run_key AND held.type = 'user_message' AND json_extract(held.payload_json, '$.inputIntent') = 'queued_turn' ) ELSE 0 END ELSE 0 END) AS item_count, SUM(CASE WHEN counts.run_key = '' THEN counts.item_count ELSE 0 END) AS runless_item_count FROM orchestration_v2_projection_turn_item_counts counts LEFT JOIN orchestration_v2_projection_runs r ON r.run_id = counts.run_key WHERE counts.thread_id IN (SELECT value FROM json_each(?)) GROUP BY counts.thread_id",
    "rows": 1
  },
  {
    "label": "edit",
    "sql": "SELECT thread_id, payload_json FROM orchestration_v2_projection_provider_threads WHERE thread_id IN (SELECT value FROM json_each(?))",
    "rows": 1
  },
  {
    "label": "edit",
    "sql": "SELECT i.thread_id, i.payload_json, p.provider_instance_id AS owning_provider_instance_id FROM orchestration_v2_projection_turn_items i LEFT JOIN orchestration_v2_projection_provider_threads p ON p.provider_thread_id = i.provider_thread_id LEFT JOIN orchestration_v2_projection_runs r ON r.run_id = i.run_id WHERE i.type IN ('command_execution', 'dynamic_tool', 'subagent') AND i.status NOT IN ('completed', 'interrupted', 'failed', 'cancelled') AND (i.run_id IS NULL OR r.status <> 'rolled_back') AND i.thread_id IN (SELECT value FROM json_each(?)) -- Preserve the pending-item partial index; read other work only from running runs. UNION ALL SELECT i.thread_id, i.payload_json, NULL AS owning_provider_instance_id FROM orchestration_v2_projection_turn_items i WHERE i.run_id IN (SELECT value FROM json_each(?)) AND i.type IN ('file_change', 'file_search', 'web_search', 'compaction') AND i.status IN ('running', 'waiting')",
    "rows": 0
  },
  {
    "label": "edit",
    "sql": "SELECT payload_json FROM orchestration_v2_projection_threads WHERE thread_id = ? LIMIT 1",
    "rows": 1
  },
  {
    "label": "edit",
    "sql": "SELECT payload_json FROM orchestration_v2_projection_runs WHERE +thread_id = ? AND run_id IN (SELECT value FROM json_each(?)) ORDER BY ordinal ASC",
    "rows": 1
  },
  {
    "label": "edit",
    "sql": "SELECT payload_json FROM orchestration_v2_projection_messages WHERE thread_id = ? AND role IN (SELECT value FROM json_each(?)) ORDER BY created_at ASC, message_id ASC",
    "rows": 107
  },
  {
    "label": "edit",
    "sql": "SELECT json_extract(payload_json, '$.messageId') AS message_id, MIN(ordinal) AS ordinal FROM orchestration_v2_projection_turn_items WHERE thread_id = ? AND type = 'user_message' AND json_extract(payload_json, '$.messageId') IN (SELECT value FROM json_each(?)) GROUP BY json_extract(payload_json, '$.messageId')",
    "rows": 104
  }
]