Skip to content

Keep chat Redis data short-lived and schema-light #1267

Closed

Description

@sentry-junior

Redis chat/runtime records have nested shapes and multi-day TTLs, so schema changes hang around after deploy. SQL remains the durable history authority. Redis should only hold bounded operational state that is safe to expire. OAuth tokens/sessions are out of scope.

Goals

  1. Redis schema changes must not require multi-day compatibility code.
  2. Durable conversation history and metadata have one authority in SQL.
  3. Redis holds only bounded operational state that is safe to expire.
  4. Terminal state is deleted quickly or kept only for a short diagnostic grace period.
  5. Redis records avoid embedding shared nested plugin/API contracts when stable ids or an authoritative lookup suffice.

A mailbox or idempotency record can still be authoritative in Redis while work is in flight. The test is whether loss/expiry is an accepted operational outcome—not whether the data is "history."

Exit criteria

  • No chat Redis record uses the chat-sdk 30d TTL.
  • Every key family documents authority, loss behavior, active TTL, and terminal TTL/deletion.
  • SQL-owned conversation metadata is not duplicated into Redis except during a named rollout.
  • Redis schemas do not store full Source / Destination / actor contracts unless that store explicitly owns the bounded operational record.
  • Dashboard/reporting reads SQL, not Redis indexes.
  • Compatibility code has a release/date removal condition and an owning TODO.
  • User-authored configuration and user-visible context do not silently disappear because a scratch TTL elapsed.
  • Tests prove terminal cleanup and authoritative reconstruction without legacy Redis fields.

Cutover rule

  1. Deploy readers that use the canonical authority (usually SQL).
  2. Dual-write only while old readers may still be running.
  3. Once canonical readers are deployed, remove the Redis write path and obsolete schema fields immediately. Do not wait for old Redis TTLs.
  4. Put cleanup TODOs beside temporary dual-write/schema code and reference this tracker (#1267).

Target model for thread scratch

Redis holds active thread state only:

  • processing (activeTurnId, pendingAuth)
  • maybe sandboxRef while a sandbox is still useful

Cold start rebuilds from SQL. Do not keep a long-lived idle bag for nostalgia.

Vision summaries are a disposable, rebuildable cache outside shared scratch. Keep them bounded and versioned; expiry may cause re-analysis but does not lose product state.

Artifacts are not product state. Canvas and list objects live in Slack. Their ids and URLs already exist in durable tool results. The artifact bag is an implicit tool contract, not an authority. Remove it instead of moving it to SQL.

Do not ship adaptive multi-TTL schemes on a mixed bag. That papers over two jobs in one key. #1361 explored that path and was closed without merge. After the bag was thinned, dual active/terminal TTLs on the remaining thin record were also skipped (#1369).

Done

Turn sessions — complete

Resume lease over SQL history. Nested routing/actors and ops metadata are out of Redis; TTLs are short; dashboard indexes are gone.

  • #1268 — resume routing from SQL only
  • #1286 — stop nested source/destination writes; explicit strip schemas
  • #1298 — stop actor writes
  • #1308 — 24h unfinished / 1h terminal; drop runtimeContext on terminal
  • #1313 — thin recovery indexes; remove global dashboard list
  • #1315 — remove ops metadata from Redis records
  • #1356 — drop dead turn-cursor model metadata
  • #1266 closed without merge once Redis no longer owned nested source

Thread/channel scratch — complete enough

  • #1320 — Junior 7d TTL instead of chat-sdk 30d
  • #1323 — drop rebuildable conversation stats/backfill from Redis
  • #1357 — location configuration in SQL (durable; Redis only temporary import)
  • #1367 — remove artifact bag; list targets explicit
  • #1368 — vision summaries as a separate bounded cache

Still in Redis scratch today: processing / pendingAuth and optional sandboxRef. The bag already TTLs at 7d and cold-starts from SQL. Further dual-TTL / terminal-grace churn on this thin record is not worth it.

Closed without merge

  • #1361 — adaptive/split scratch TTL. Wrong model while artifacts/vision share the bag.
  • #1369 — active-only / dual TTL after the bag was already thin. No meaningful product gain once artifacts and vision left; existing 7d expiry is enough.

Remaining

Decide authority and loss behavior before thinning each store. Do not move fields to SQL only to satisfy the slogan, and do not keep accidental durability in Redis.

1. Thin the conversation work mailbox

junior:conversation:* still mixes in-flight mailbox/control with duplicated durable conversation metadata (destination, actor, channel name, source, title) under a 7d TTL.

  • Remove schema-v1 mailbox compatibility (TODO(v0.110.0)) once aged out
  • Keep mailbox entries, execution status/lease, dedupe ids, recovery cursors
  • Stop mirroring SQL-owned conversation metadata
  • Define terminal/idle deletion or short grace instead of refreshing whole records for 7d
  • Decision: must pending messages survive Redis loss? If yes, Redis cannot be their sole authority.

2. Redesign agent dispatch deliberately

junior:agent_dispatch:* still stores strict 7d records embedding actor, credential subject, destination, visibility, source, input, metadata, attribution, and status, plus legacy rollout fields through v0.116.0.

Before coding:

  • Is a dispatch a durable product/API object whose idempotency/status must survive Redis loss or a >7d delay? If yes, SQL is canonical and Redis keeps locks/wake indexes only.
  • If it is bounded operational state, document the 7d expiry contract and thin nested routing/authority via lookups where possible.
  • Remove legacy execution fields and key/input compatibility after the stated release window.
  • Split terminal retention from active retention.

Working order

  1. Remove the artifact bag and make list tool targets explicit. — complete in refactor(chat): Remove thread artifact state #1367
  2. Keep vision summaries as a bounded, versioned cache outside shared thread scratch. — complete in ref(chat): Separate vision cache from thread scratch #1368
  3. Reduce thread scratch to active-only state with short TTL. — skipped; bag is already thin (processing / optional sandboxRef), 7d TTL expires it, cold start is SQL. Dual TTL closed without merge in ref(chat): Bound thread scratch to active-only TTL #1369.
  4. Thin mailbox metadata; remove schema-v1 compatibility.
  5. Decide whether pending mailbox work must survive Redis loss; move only if yes.
  6. Define dispatch durability/idempotency guarantees; SQL-canonicalize or explicitly bound/thin the Redis record.
  7. Add terminal deletion/grace and remove expired compatibility code for mailbox and dispatch.

Out of scope

  • OAuth user tokens and auth sessions
  • SQL conversation history / events (already durable authority)
  • Further dual-TTL ceremony on thin thread scratch after artifacts/vision left the bag

Activity

  1. sentry-junior commented on Aug 6, 2026

    @sentry-junior
    ContributorAuthor

    Inventory: agent turn sessions (item 1)

    Owner: packages/junior/src/chat/state/turn-session.ts (+ turn-session-keys.ts)

    Keys / TTL

    • Record: junior:agent_turn_session:{conversationId}:{sessionId}
    • Global summary list: junior:agent_turn_session:index (cap 5_000)
    • Per-conversation summary list: junior:agent_turn_session:conversation:{conversationId}:index
    • TTL: 30d via chat-sdk THREAD_STATE_TTL_MS (AGENT_TURN_SESSION_TTL_MS = THREAD_STATE_TTL_MS)
    • Not junior’s 7d JUNIOR_THREAD_STATE_TTL_MS

    What redis stores

    Full record (storedAgentTurnSessionRecordSchema, .strict()):

    • Identity / lifecycle: conversationId, sessionId, sliceId, version, state, startedAtMs, lastProgressAtMs, updatedAtMs, surface, traceId, resumeReason, resumedFromSliceId, errorMessage
    • Resume cursors into SQL: committedSeq, historyVersion?, turnStartSeq?
    • Nested platform contracts: optional source (sourceSchema), destination (destinationSchema), actor / actors (actorSchema)
    • Dispatch linkage: dispatchId?, dispatchOutcome?, resultMessageId?
    • Ops/meta: channelName?, modelId?, reasoningLevel?, loadedSkillNames?, cumulativeDurationMs, cumulativeUsage?
    • Volatile bootstrap only in redis: runtimeContext?: PiMessage[] (AGENTS.md / <runtime-turn-context> parts retained for unfinished resumes; not full transcript)

    Summary indexes store the same summary fields (no committedSeq / runtimeContext / actors list), also strict-parsed.

    On read, piMessages are not taken from redis — they are re-projected from SQL via committedSeq (+ optional runtimeContext restore while running / awaiting_resume).

    Readers / writers (schema-coupled)

    • Write/update: upsertAgentTurnSessionRecord, recordAgentTurnSessionSummary, updateAgentTurnSessionState
    • Resume path: getAgentTurnSessionRecordForResume → agent-continue-runner, slack resume, oauth callbacks, reply-executor
    • Dashboard/ops lists: listAgentTurnSessionSummaries*
    • Also: agent-dispatch work path links dispatch ↔ turn session

    Resume requires stored nested source / destination / actor today (agent-continue-runner fails closed if source missing).

    Why shape changes hang around

    1. 30d TTL — longest chat hangover in scope
    2. Strict zod on record + summary — bad old shape → parse throw / skipped summary (resume can hard-fail; list warns and drops entry)
    3. Shared nested contracts (source / destination / actor) live in redis, not just ids — same class as open temporary compat #1266
    4. Duplicate shapes — full record key + two summary lists; list appends refresh list TTL independently of record rewrite patterns
    5. SQL already owns durable pi history; redis is denser than “cursor + ids” for resume

    Density vs need (inventory only)

    Field group Needed for short-lived resume? Notes
    committedSeq / historyVersion / turnStartSeq / status / slice yes true ephemeral resume metadata
    runtimeContext yes while unfinished intentionally volatile; should die with short TTL
    source / destination / actor full objects currently yes (resume rebuilds routing from them) highest schema-churn cost; couple redis to plugin-api contracts
    usage / model / skills / channelName ops/dashboard could be sql or derived; not resume-critical
    summary list copies of nested contracts list/dashboard multiplies hangover surface

    Related

    • Temporary source visibility coerce: #1266 (open; not a storage redesign)
    • Tracker working order continues with thread/channel scratch next
  2. sentry-junior commented on Aug 6, 2026

    @sentry-junior
    ContributorAuthor

    Analysis: turn sessions — ephemeral vs not, and what to improve

    Lens: what must survive a pause, what can be rebuilt from SQL/other authority, what only exists because redis is currently the resume bag.

    Already authoritative elsewhere (rebuildable)

    Redis field Rebuild from Confidence
    piMessages / provenance SQL junior_conversation_events via committedSeq high — already done on every read
    destination SQL junior_conversations.destination / destination row high — written on every summary via recordActivity
    source (session-stable parts) SQL sessionSource (source_json) high for locator; medium for per-message messageTs
    actor (slack user) SQL actor identity + conversation events / user message author medium-high — continue path already re-resolves slack actor when possible
    actors[] instruction provenance on committed events high — materialize path already falls back to provenance
    channelName, usage, duration SQL conversation aggregates (recordExecution) high
    modelId / skills / reasoning turn events / live turn context if still needed medium — ops/debug, not resume-critical
    global + per-conversation summary lists SQL conversation/execution rows + events high for dashboard/ops; lists are a redis-shaped index, not a truth source

    Truly ephemeral (cannot honestly rebuild today)

    These are the only turn-session bits that are ops state for an in-flight run, not history:

    Field Why it’s ephemeral If lost
    state + resumeReason lease/control for pause/continue cannot know whether to continue vs ignore
    version optimistic concurrency for continue payloads stale continues can double-fire or no-op wrong
    sliceId / resumedFromSliceId slice accounting / limits weak rebuild; wrong slice breaks limits/metrics
    committedSeq + historyVersion + turnStartSeq exact resume cursor into SQL without them, “continue” becomes “guess last boundary”
    runtimeContext volatile bootstrap (AGENTS.md / runtime-turn-context), never SQL unfinished resume loses bootstrap; finished runs don’t need it
    dispatchId / resultMessageId / dispatchOutcome cross-store linkage for this run partially recoverable from dispatch store / events, but not free today

    Important: even the ephemeral set is only needed while the turn is non-terminal (running / awaiting_resume). After completed / failed / abandoned, redis is mostly a 30d ops cache of things SQL already got.

    Pretends to be ephemeral but is treated as durable bag

    Resume currently requires nested redis source (and uses redis destination / actor) even though SQL already holds conversation-level copies:

    • agent-continue-runner fails closed if activeSessionRecord.source is missing
    • getAwaitingAgentContinueRequest requires sessionRecord.destination
    • queue wakeups only carry conversationId + sessionId + expectedVersion + destination on the continue request — so redis (or a rebuild step) must rehydrate routing

    That is the core design smell: short-lived control plane fields are co-located with long-lived nested platform contracts, then given a 30d TTL and strict parse.

    What “good” looks like for turn sessions

    Keep in redis (short TTL, hours / low days, only while unfinished):

    • identity: conversationId, sessionId
    • control: state, resumeReason, version, sliceId
    • cursors: committedSeq, historyVersion, turnStartSeq
    • volatile: runtimeContext (drop on terminal)
    • thin linkage ids only if still needed: dispatchId?, resultMessageId?

    Do not keep as nested redis contracts:

    • full source / destination / actor / actors — rebuild from SQL conversation + events at resume
    • usage / model / skills / channelName — SQL only
    • summary list copies of the above — delete or replace with SQL queries

    TTL policy:

    • active/unfinished: short (bounded by real resume window, not dashboard nostalgia)
    • terminal: delete or tiny grace; do not keep 30d “history” in redis
    • stop aliasing to chat-sdk THREAD_STATE_TTL_MS (30d)

    Parse policy:

    Improvement order (turn sessions only)

    1. Resume rebuild path — load destination/sessionSource/actor from SQL conversation; stop requiring redis source
    2. Shrink stored record to cursors + control + runtimeContext; drop nested contracts from writes
    3. TTL split — short while unfinished; purge/skip rewrite on terminal (or explicit delete)
    4. Retire summary indexes (or stop embedding nested contracts in them); dashboard reads SQL
    5. Remove temporary source coerce once redis no longer carries source (fix(plugin-api): Coerce legacy source visibility on read #1266 becomes unnecessary for this store)

    Non-goals / traps

    • Moving full turn sessions into SQL as a new durable history table — fights the “redis ephemeral / SQL history” split and recreates hangover in postgres JSON
    • Keeping 30d redis “for the dashboard” — that’s what junior_conversations + events are for
    • Dual-writing fat objects “for a while” without shrinking readers — extends the hangover

    Bottom line

    Turn sessions should be a small resume lease over SQL history, not a 30-day mirror of routing + ops metadata. Most of the hangover surface is duplicated, rebuildable data; the irreducible ephemeral core is cursors + control + short-lived runtimeContext.

  3. sentry-junior commented on Aug 6, 2026

    @sentry-junior
    ContributorAuthor

    Slice 1 in progress: resume rebuild from SQL

    Implementing the first turn-session improvement from the analysis:

    • Prefer turn-session redis source / destination when present
    • If missing, rebuild from SQL junior_conversations.sessionSource / destination
    • Unblock resume paths that currently fail closed on missing redis nested contracts

    Out of this slice: shrinking writes, TTL split, dropping summary indexes, stop writing nested contracts.

    Tracked call sites:

    • getAwaitingAgentContinueRequest
    • continueSlackAgentRun / stranded recovery
    • OAuth + MCP OAuth resume paths
  4. sentry-junior commented on Aug 6, 2026

    @sentry-junior
    ContributorAuthor

    Slice 1 PR

    Draft PR: #1268

    Implemented resume rebuild from SQL for missing turn-session source/destination:

    • helper: resolveTurnSessionRouting
    • wired into automatic continuation, stranded recovery, OAuth + MCP OAuth resume
    • unit coverage for prefer-redis / fill-from-sql / partial rebuild
    • component coverage for SQL rebuild success + still-fail when both missing

    Still later slices: shrink writes, TTL split, drop summary-list fat copies, stop writing nested contracts.

  5. sentry-junior commented on Aug 6, 2026

    @sentry-junior
    ContributorAuthor

    Slice 1 update: SQL is the only routing authority

    PR #1268 now always loads destination + session source from junior_conversations on resume. No redis-prefer / SQL-fallback dual path.

    Still later: stop writing nested contracts into turn-session redis, TTL split, drop fat summary indexes.

  6. 27 remaining items

  7. sentry-junior commented on Aug 8, 2026

    @sentry-junior
    ContributorAuthor

    Correction to the residual note above: #1328 no longer changes recovery-index collapse policy. Repeated lifecycle eval failures showed that summary ordering interacts with continuation recovery more broadly than this metadata-cleanup slice, so those changes and their added coverage were removed in 6118d73.

    The follow-up remains: review the fire-and-forget turn-start running summary together with continuation discovery/recovery ownership. Treat that as a separate vertical slice with the existing lifecycle evals as its product coverage, rather than extending #1328.

  8. sentry-junior commented on Aug 9, 2026

    @sentry-junior
    ContributorAuthor

    Superseded the stale #1328 cleanup with a fresh PR on current main after #1344: turn-cursor honesty only (drop dead model/skills/reasoning from checkpoint writes; rename runtimeMetadata → runtimeMetrics for SQL channel/metrics). No recovery-index changes. Fresh PR will be linked from this thread / the closed #1328 comment.

  9. sentry-junior commented on Aug 9, 2026

    @sentry-junior
    ContributorAuthor

    Fresh cleanup PR: #1356

  10. sentry-junior commented on Aug 10, 2026

    @sentry-junior
    ContributorAuthor

    Slice: active-only thread scratch

    Artifacts (#1367) and vision cache separation (#1368) are merged. Next remaining scratch item:

    Draft PR: #1369

    This slice

    • Redis thread scratch keeps only processing / optional sandboxRef
    • Active TTL 24h while activeTurnId or pendingAuth is set
    • Terminal/idle grace 1h; delete empty bags immediately
    • Cold start still rebuilds from SQL; no long-lived idle bag

    Still later

    • Thin conversation mailbox metadata
    • Decide/clean agent-dispatch durability and retention
  11. sentry-junior commented on Aug 10, 2026

    @sentry-junior
    ContributorAuthor

    Active-only thread scratch — skipped

    Closed #1369 without merge.

    After artifacts (#1367) and vision (#1368) left the bag, dual active/terminal TTLs on the remaining thin record are ceremony. The bag already TTLs out; cold start is already SQL. No meaningful product gain.

    Still remaining

    • Thin conversation mailbox metadata
    • Decide/clean agent-dispatch durability and retention
  12. sentry-junior commented on Nov 9, 2026

    @sentry-junior
    ContributorAuthor

    Closing as done: about 15 slices merged (#1268–#1368). Open a narrow issue if a remaining leftover (mailbox metadata, agent-dispatch retention) matters.

    Closed during a tracker cleanup. Reopen if this still matters.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions