Skip to content

Session Quality Hooks

The session-insights runner analyzes finished chat sessions on a schedule. Which sessions it analyzes, who is told about a flagged session, and what counts as a tool failure vary by deployment, so the runner reads those decisions from hooks instead of hard-coding them. Pika ships the hooks with defaults that keep the runner's standard behavior, and you override only what you need without patching the runner.

FileOwnerWhat it holds
services/pika/src/lib/session-quality-base.tsPika (sync updates it)The hook types and defaultSessionQualityHooks
services/pika/src/lib/custom/session-quality-hooks.tsYou (sync never touches it)Only the members you want to override

services/pika/src/lib/custom/** is protected by the default protected-area list of the installed pika-app CLI (1.9.0 and later). services/pika/test/custom/** is protected the same way.

Your file exports sessionQualityHooks, a Partial<SessionQualityHooks>. Pika resolves the hooks at call time: a member you define replaces the default, and a member you leave out keeps it. Setting a member to undefined also keeps the default, except toolFailureBodySentinel, where undefined turns the check off.

MemberDefaultCalled
isSweepEnabled()trueAt the start of each scheduled sweep. false skips the sweep.
getSweepStartFloor()undefinedOnce per sweep. Returns an ISO date; the sweep skips sessions last active before it. undefined defers to the next source (see Start floor).
getChatAppAllowlist()undefinedOnce per sweep. A Set of chat app ids to analyze. undefined means every chat app, and an empty set means none.
requiresEmployeeFollowup(assessments)critical issues present, follow-up required, or escalation neededFor each analyzed session. true marks the session as urgent.
buildFlaggedSessionNotification(event)undefined (no notification)For an urgent session that has new messages since its last analysis. Returns an object with a send() method, or undefined.
shouldMarkForInsightsOnThumbsDown(ctx)falseWhen an end user or an admin saves a thumbs-down on a session that isn't already queued. true queues the session for analysis.
toolFailureBodySentinelundefinedA string. A tool response body that starts with it counts as a tool failure in the insights payload.

Set the sentinel in both custom_hooks.py and session-quality-hooks.ts; each side reads only its own.

buildFlaggedSessionNotification receives a FlaggedSessionEvent with session, scoring, urgent and notes. Pika calls send() only after the session's analysis update is saved. If a page is retried, the session's earlier pending update and notification are replaced, so each flagged session produces one notification.

Notifications are at-most-once. If the Lambda times out or send() throws after the update is saved, that notification is not retried. The INSIGHTS_FLAGGED log line is written for every flagged session, so a CloudWatch metric filter on it is the durable record.

A session whose chat app isn't in getChatAppAllowlist has its insight_status cleared without analysis, so it isn't re-read on every sweep. Widening the allowlist later covers only sessions with new activity after the change.

shouldMarkForInsightsOnThumbsDown receives a ThumbsDownContext with userId, sessionId, chatAppId, sessionAttributes and source ('end-user' or 'admin'). When the member isn't overridden, a thumbs-down does no extra reads.

Every call is wrapped. A throw is logged as [SESSION-QUALITY] <hook> threw and handled per hook:

HookOn a throw
isSweepEnabledThe sweep is skipped
getSweepStartFloorThe sweep is skipped; no floor stamped
getChatAppAllowlistAn empty set, so the sweep is skipped
requiresEmployeeFollowupThe default predicate
buildFlaggedSessionNotificationNo notification
shouldMarkForInsightsOnThumbsDownfalse, and the feedback is still saved

A broken enable flag or allowlist stops the sweep instead of spending Bedrock on sessions you meant to exclude.

The runner picks its start floor from the first of these that is set:

  1. The getSweepStartFloor hook.
  2. The INSIGHTS_START_FLOOR environment variable, which the sessionInsights.startFloor construct prop sets.
  3. A stamp. On the first scheduled sweep, the runner writes the current time as an item named session-insights-runner#start-floor in the runner mutex table, then reuses it on every later sweep. The item has no expiry.

On the upgrade that introduces the runner's start floor, the first sweep stamps "now", so a backlog of sessions queued earlier isn't analyzed. To backfill, set sessionInsights.startFloor to an ISO date in getPikaConstructProps (in custom-stack-defs.ts) before the first sweep, or delete the stamp item from the mutex table. A single-session invoke ({ sessionId, userId }) ignores the floor and the sweep gates but takes the sweep lock; while a sweep holds it, the invoke returns { busy: true } without analyzing. If the stamp can't be read or written, the sweep is skipped.

services/pika/src/lib/custom/session-quality-hooks.ts
import type { SessionQualityHooks } from '../session-quality-base';
export const sessionQualityHooks: Partial<SessionQualityHooks> = {
getChatAppAllowlist: async () => new Set(['example-app']),
buildFlaggedSessionNotification: async ({ session, urgent }) => ({
send: async () => {
await fetch('https://example.com/hooks', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ sessionId: session.sessionId, urgent })
});
}
})
};

Pika's jest config maps every import whose path ends in /custom/session-quality-hooks to an empty default, services/pika/test/setup/default-session-quality-hooks.ts. Pika's own tests therefore run against the defaults even when your file overrides them, and a test that imports session-quality-hooks.ts itself also gets the empty default. Keep your hook logic in its own modules under services/pika/src/lib/custom/, import them from session-quality-hooks.ts, and put their tests in services/pika/test/custom/.