import { Column, getTableName, is, sql, type SQL, type SQLWrapper } from "drizzle-orm"; import type { SessionEventType } from "@opengeni/contracts"; function correlatedReference(reference: SQLWrapper): SQLWrapper { // Drizzle's single-table SELECT projection removes Column table qualifiers, // including inside nested SQL. Here that would bind workspace_id/session_id // to the inner event row and turn correlation into a tautology. Identifiers // retain the exact outer table (or alias) through that projection rewrite. return is(reference, Column) ? sql`${sql.identifier(getTableName(reference.table))}.${sql.identifier(reference.name)}` : reference; } /** Attention is conversational output or an actionable boundary, not activity. * Keep this allow-list shared by rail, tree and consumption queries. */ export const MEANINGFUL_SESSION_EVENT_TYPES = [ "agent.message.completed", "turn.completed", "turn.failed", "session.requiresAction", "session.humanInput.requested", "tool.auth_needed", "credential.auth_needed", "goal.completed", "goal.paused", "goal.progress", "goal.rewrite.proposed", "rig.setup.failed", "sandbox.operation.failed", "sandbox.box.lost", "workspace.revision.degraded", "machine.op.failed", "machine.link.lost", "session.event.envelope_omitted", ] as const; /** * Events that never count as session activity by themselves: raw stream * deltas, and infrastructure diagnostics such as the idle reaper's * `sandbox.box.terminated` fan-out to every session sharing a drained box. A * statement whose events are all of these advances the durable cursor and * `last_sequence`, but neither `updated_at` (the rail's "last activity" time) * nor `activity_xid` (rail order and incremental discovery). Any other event * in the same statement keeps the ordinary activity projection. Attention * events (`sandbox.box.lost`, `machine.link.lost`, ...) are deliberately * absent: something a person must act on is activity. * * Migration 0904 holds the same literal list in the event cursor trigger; * a change here needs a new migration. */ export const SESSION_ACTIVITY_NEUTRAL_EVENT_TYPES = [ "agent.message.delta", "agent.reasoning.delta", "sandbox.command.output.delta", "terminal.pty.output.delta", "sandbox.box.created", "sandbox.box.terminated", "sandbox.box.snapshot", "sandbox.env.drift", "session.route.reconciled", "workspace.revision.captured", "machine.op.recovered", "machine.link.restored", "machine.runner.restarted", ] as const satisfies readonly SessionEventType[]; /** * `0503` is that migration's index predicate. `0527` adds two changes: completed * commentary is activity, and a `turn.completed` recording the `reply` a human * or API message received before its turn waited for input is an answer even * though its `output` is empty. `0774` adds one more: a turn stopped because * its task reached its usage limit (`segmentLimit: "session_usage_limit"`) is * an actionable boundary — only a person can raise or clear the limit — so it * needs attention exactly like a failure, without any explanatory model call. * Other segment limits (`max_turns`, `budget_exhausted`) keep their meaning. */ function attentionSessionEventSql(alias: string, revision: "0503" | "0527" | "0774"): SQL { const e = sql.identifier(alias); const turnResult = sql`coalesce(nullif(${e}.payload -> 'output', 'null'::jsonb), ${e}.payload -> 'result')`; const usageLimitStop = revision === "0774" ? sql`(${e}.payload ->> 'segmentLimit') = 'session_usage_limit' or ` : sql``; // These are code-owned literals, not request values. Literal predicates let // PostgreSQL use the matching partial index even with a generic cached plan. return sql`${e}.type in (${sql.join( MEANINGFUL_SESSION_EVENT_TYPES.map((type) => sql.raw(`'${type}'`)), sql`, `, )}) and ${e}.duplicate_of_event_id is null and (${e}.turn_association is null or ${e}.turn_association = 'current') and (${e}.type <> 'agent.message.completed' or coalesce(${e}.payload ->> 'text', '') <> '')${ revision !== "0503" ? sql` and (${e}.type <> 'agent.message.completed' or coalesce(${e}.payload ->> 'phase', '') <> 'commentary') and (${e}.type <> 'turn.completed' or ${usageLimitStop}( not (${e}.payload ?| array['maintenance', 'segmentLimit']) and (( ${turnResult} is not null and ${turnResult} not in ('null'::jsonb, '""'::jsonb) ) or coalesce(${e}.payload ->> 'reply', '') <> '') ))` : sql` and (${e}.type <> 'turn.completed' or ( not (${e}.payload ?| array['maintenance', 'segmentLimit']) and ${turnResult} is not null and ${turnResult} not in ('null'::jsonb, '""'::jsonb) ))` }`; } /** * Completed commentary is progress narrating the work, not an answer, so it * never creates attention on its own; the answer or turn outcome that follows * does. A turn that ends waiting for input has no output, but when a human or * API message started it, the `reply` it records is that answer. Exactly the * migration 0774 partial-index predicate, which also counts a task usage limit * stop as an actionable boundary. * * Since migration 1289 an answer that is exactly the no-reply token is not * attention either (`noReplyAnswerExcludedSql`): it is nothing for the person * and no timeline draws it. That exclusion is a filter on top of the 0774 * index predicate, so queries keep using that index. */ export function meaningfulSessionEventSql(alias: string): SQL { return sql`${attentionSessionEventSql(alias, "0774")} and ${noReplyAnswerExcludedSql(alias)}`; } /** * Exactly the migration 0774 partial-index predicate * (`session_events_meaningful_attention_v3_idx`). Attention queries use * `meaningfulSessionEventSql`, which implies it. */ export function meaningfulAttentionIndexPredicateSql(alias: string): SQL { return attentionSessionEventSql(alias, "0774"); } /** * The no-reply token test, as SQL over a text expression: the same * normalization as `isNoReplyText` in `@opengeni/contracts/no-reply` and * migration 1289's `opengeni_private.no_reply_text_v1`. Code-owned literals, * so the predicate needs no runtime routine or parameter. */ export function noReplyTextSql(text: SQL): SQL { return sql`coalesce(char_length(${text}) <= 256 and upper(regexp_replace(${text}, ${sql.raw( NO_REPLY_WRAPPING_PATTERN_LITERAL, )}, '', 'g')) = 'NO_REPLY', false)`; } const NO_REPLY_WRAPPING_PATTERN_LITERAL = `'^[[:space:]\`*_"''“”]+|[[:space:]\`*_"''“”.!]+$'`; /** * An agent message, or a turn's output, that is the no-reply token is not an * answer for the person: migration 1289's cursor trigger does not count it as * attention, so a quiet run sets no unread mark. A turn that recorded a * `reply` keeps it. */ export function noReplyAnswerExcludedSql(alias: string): SQL { const e = sql.identifier(alias); return sql`(${e}.type <> 'agent.message.completed' or not ${noReplyTextSql( sql`${e}.payload ->> 'text'`, )}) and (${e}.type <> 'turn.completed' or not ${noReplyTextSql( sql`${e}.payload ->> 'output'`, )} or coalesce(${e}.payload ->> 'reply', '') <> '')`; } /** * The migration 0527 index predicate (no usage-limit stops). Pre-0774 API * processes still probe with it during a rolling deploy; nothing current * queries it. */ export function commentaryExclusiveMeaningfulSessionEventSql(alias: string): SQL { return attentionSessionEventSql(alias, "0527"); } /** * The migration 0503 index predicate. Older API processes still probe with it * during a rolling deploy; nothing current queries it. */ export function commentaryInclusiveMeaningfulSessionEventSql(alias: string): SQL { return attentionSessionEventSql(alias, "0503"); } /** A stored audit preview can still need attention, but cannot stand in for * consumption of the original content, even when the reader returned it whole. * This is about what a reader received, so an answer that is the no-reply * token is still content a parent agent can have read. */ export function completeMeaningfulSessionEventSql(alias: string): SQL { const e = sql.identifier(alias); return sql`${meaningfulAttentionIndexPredicateSql(alias)} and coalesce(${e}.payload -> 'truncation' ->> 'truncated', 'false') <> 'true' and coalesce(${e}.payload ->> 'sourceOmitted', 'false') <> 'true'`; } /** One reverse partial-index probe, independent of the raw-delta tail length. * Derivation also repairs historical bookkeeping-only dots without changing * personal state or rewriting append-only events. */ export function meaningfulSessionSequenceSql( workspaceId: SQLWrapper, sessionId: SQLWrapper, ): SQL { return sql`coalesce(( select meaningful.sequence from session_events meaningful where meaningful.workspace_id = ${correlatedReference(workspaceId)} and meaningful.session_id = ${correlatedReference(sessionId)} and ${meaningfulSessionEventSql("meaningful")} order by meaningful.sequence desc limit 1 ), 0)`; } /** The child's newest ordinary turn outcomes, newest first. Only a * result-bearing `turn.completed` here is the child's current answer: an older * answer behind a newer failed, cancelled, superseded, or segment-limited turn * is never reported as the result. A segment-limit completion (`max_turns`, * `budget_exhausted`) is the outcome of the turn that stopped there, and its * empty output means "no answer". Only standalone maintenance is not an * outcome. * * `goalContinuationOnly` marks a turn claimed only to continue the child's * goal: a goal-routed turn to which no other input (a message, a Steer, a * child or command result) was ever delivered. Such a turn follows an answer * rather than producing the task's result, so the caller walks back past it. * Each outcome probe walks the (workspace, session, type, sequence) index * backwards within `limit`, and only the selected rows' payloads are read. * The child's delivered input is scanned once for all selected turns. * * `goalActivatedAt` is the newest `goal.set` or `goal.resumed` event: goal * continuation turns never run across an idle boundary that reported the goal * inactive, so a continuation walk stops there instead of reaching output a * result for an earlier run already reported. */ export function childRecentTurnOutcomesSql( workspaceId: SQLWrapper, sessionId: SQLWrapper, limit: number, ): SQL { return sql`with latest as materialized ( select outcome.sequence, outcome.turn_id from session_events outcome where outcome.workspace_id = ${workspaceId} and outcome.session_id = ${sessionId} and outcome.type in ('turn.completed', 'turn.failed', 'turn.cancelled', 'turn.superseded') and outcome.duplicate_of_event_id is null and (outcome.turn_association is null or outcome.turn_association = 'current') and (outcome.type <> 'turn.completed' or not (outcome.payload ? 'maintenance')) order by outcome.sequence desc limit ${limit} ), goal_turns as materialized ( select turn.id from session_turns turn where turn.workspace_id = ${workspaceId} and turn.session_id = ${sessionId} and turn.id in (select latest.turn_id from latest where latest.turn_id is not null) and turn.source = 'goal' ), goal_turns_with_input as materialized ( select distinct input.delivered_turn_id as id from session_system_updates input where input.workspace_id = ${workspaceId} and input.session_id = ${sessionId} and input.delivered_turn_id in (select goal_turns.id from goal_turns) and input.kind <> 'goal_continuation' ), goal_activation as ( select max(activation.sequence) as sequence from ( (select event.sequence from session_events event where event.workspace_id = ${workspaceId} and event.session_id = ${sessionId} and event.type = 'goal.set' order by event.sequence desc limit 1) union all (select event.sequence from session_events event where event.workspace_id = ${workspaceId} and event.session_id = ${sessionId} and event.type = 'goal.resumed' order by event.sequence desc limit 1) ) activation ) select outcome.sequence, outcome.type, outcome.payload, outcome.payload_codec_version as "payloadCodecVersion", (latest.turn_id in (select goal_turns.id from goal_turns) and latest.turn_id not in (select goal_turns_with_input.id from goal_turns_with_input)) is true as "goalContinuationOnly", coalesce((select goal_activation.sequence from goal_activation), 0) as "goalActivatedAt" from latest join session_events outcome on outcome.workspace_id = ${workspaceId} and outcome.session_id = ${sessionId} and outcome.sequence = latest.sequence order by outcome.sequence desc`; } /** Bound indexed candidate rows BEFORE testing payload size/completeness. Without * this boundary, a long run of oversized answers can cause an unbounded scan. */ export function childLifecycleEvidenceCandidatesSql( workspaceId: SQLWrapper, sessionId: SQLWrapper, ): SQL { return sql`with candidates as materialized ( select meaningful.* from session_events meaningful where meaningful.workspace_id = ${workspaceId} and meaningful.session_id = ${sessionId} and ${meaningfulSessionEventSql("meaningful")} order by meaningful.sequence desc limit 32 ) select candidates.sequence, candidates.type, candidates.payload, candidates.payload_codec_version as "payloadCodecVersion" from candidates where ${completeMeaningfulSessionEventSql("candidates")} -- Inspection cap only: the 8 KiB evidence budget applies after logical -- decoding in boundedChildLifecycleEvidence, not to this stored encoding. and octet_length(candidates.payload::text) <= 65536 order by candidates.sequence desc`; }