threads

js/ai/threads.ts

fino:ai/threads — user-facing thread metadata and attention over session storage.

fino:ai/session already owns everything durable about a conversation: thread heads, the history graph, and run checkpoints. An application that lets a person keep several conversations — a sidebar, a sessions subcommand, a list of background jobs — needs three more things per thread: a title, whether it is archived, and whether something happened there that the person has not looked at yet. This module adds exactly that, and nothing that duplicates the session layer.

Design

Attention

latest run head seen? attention
running either 'running'
suspended either 'input'
error no 'error'
done / cancelled no 'unread'
any settled status yes 'idle'
none either 'idle'

running is read from the durable checkpoint. After a process exits mid-step that checkpoint still says running; Session.resume() re-drives such a run. Marking a thread seen acknowledges a settled outcome only — a suspended run keeps asking for input until it is resumed.

Limits

Listing reads every thread head plus one run checkpoint and one info record per thread; it is meant for interactive lists, not for stores with very large thread counts. Deleting a thread through deleteConversationThread() leaves its info record behind; listings ignore such orphans and deleteThreadInfo() removes them.

import { sqliteStore } from 'fino:store';
import {
  listThreadSummaries,
  markThreadSeen,
  summarizeAttention,
  updateThreadInfo,
} from 'fino:ai/threads';

const store = await sqliteStore({ path: '.fino/sessions.db' });
await updateThreadInfo(store, 'ticket-42', { title: 'Flaky loader test' });

const threads = await listThreadSummaries(store);
for (const { thread, info, attention } of threads) {
  console.log(info.title ?? thread.threadId, attention);
}
const badge = summarizeAttention(threads, { exclude: 'ticket-42' });
if (badge.input) console.log('another thread is waiting for you');
await markThreadSeen(store, 'ticket-42');

Interfaces

interface ThreadMarker {

Identity of one committed thread head: the history revision and the run that committed it. Two heads with the same revision but different runs are different outcomes — a run that fails before appending anything leaves the revision unchanged.

Properties

runId?: string

ThreadState.runId of the head, when a run committed it.

historyRevisionId?: string

ThreadState.historyRevisionId of the head, when it has one.

interface ThreadInfo {

User-facing metadata for one conversation thread.

Properties

threadId: string

Thread this record describes.

title?: string

Human title; absent until set.

archived: boolean

Archived threads drop out of the default listing.

seen?: ThreadMarker

Head the person last acknowledged with markThreadSeen().

interface ThreadSummary {

One thread with its metadata, latest run, and derived attention.

Properties

thread: ThreadState

Thread head as stored by fino:ai/session, with its store version.

run: RunState | null

Checkpoint of thread.runId, or null without one.

info: ThreadInfo

Stored metadata, or the defaults when none has been written.

attention: ThreadAttention

threadAttention(thread, run, info).

interface AttentionSummary {

Attention across many threads folded into flags for a single indicator.

Properties

running: boolean

Some thread has a step in flight.

input: boolean

Some thread is suspended waiting for input or approval.

error: boolean

Some thread failed and the failure has not been seen.

unread: boolean

Some thread finished unseen.

interface ThreadInfoChange {

Changes accepted by updateThreadInfo(); omitted fields are kept.

Properties

title?: string | null

New title, or null to clear it. Must be a non-empty string.

archived?: boolean

Archive or restore the thread.

seen?: ThreadMarker | null

New seen marker, or null to mark the thread unseen again.

Types

type ThreadAttention = 'running' | 'input' | 'error' | 'unread' | 'idle'

What a thread needs from the person, derived by threadAttention().

  • 'running' — the latest run's checkpoint says a step is in flight.
  • 'input' — the latest run is suspended on input or a tool approval.
  • 'error' — the latest run failed and the failure has not been seen.
  • 'unread' — the latest run finished or was cancelled unseen.
  • 'idle' — nothing to report.

Functions

function threadMarker(thread: ThreadState): ThreadMarker

The marker identifying thread's current head, for markThreadSeen().

Pass the thread an interface actually displayed, so a commit that lands between rendering and acknowledging stays unread.

const [first] = await listThreadSummaries(store);
render(first);
await markThreadSeen(store, first.thread.threadId, threadMarker(first.thread));

function threadAttention( thread: ThreadState, run: RunState | null, info: ThreadInfo, ): ThreadAttention

Derive what a thread needs from the person; see the module's attention table. Pure: the same thread, run, and info always give the same answer.

run should be the checkpoint named by thread.runId. A run belonging to another head is ignored, so a stale checkpoint never shows as current.

const thread = (await loadConversationThread(store, 'ticket-42'))!;
const run = thread.runId ? await loadAgentRun(store, thread.runId) : null;
const info = await loadThreadInfo(store, 'ticket-42');
console.log(threadAttention(thread, run, info));

function summarizeAttention( summaries: Iterable<Pick<ThreadSummary, 'thread' | 'attention'>>, opts: { exclude?: string } = {}, ): AttentionSummary

Fold the attention of many threads into one set of flags. exclude skips one thread — typically the one on screen, whose state is already visible.

const flags = summarizeAttention(await listThreadSummaries(store), { exclude: currentId });
statusBar.set(flags.input ? 'waiting' : flags.unread ? 'new' : '');

async function loadThreadInfo(store: AtomicStore, threadId: string): Promise<ThreadInfo>

Load a thread's metadata, or the defaults (archived: false, no title, not seen) when none has been written. Does not check that the thread exists.

const info = await loadThreadInfo(store, 'ticket-42');
console.log(info.title ?? 'Untitled', info.archived);

async function updateThreadInfo( store: AtomicStore, threadId: string, change: ThreadInfoChange, ): Promise<ThreadInfo>

Apply change to a thread's metadata and return the stored result.

The update is a checked read-modify-write that retries on conflict, so concurrent changes to different fields all persist. Throws a TypeError for an invalid change and an Error when the thread does not exist, which keeps metadata from being written for threads that were never created.

await updateThreadInfo(store, 'ticket-42', { title: 'Loader flake', archived: true });
await updateThreadInfo(store, 'ticket-42', { title: null }); // clear the title

async function markThreadSeen( store: AtomicStore, threadId: string, marker?: ThreadMarker, ): Promise<ThreadInfo>

Acknowledge a thread's head so a finished or failed run stops asking for attention. marker defaults to the head stored now; pass threadMarker(thread) for the head that was actually displayed.

await markThreadSeen(store, 'ticket-42');

async function deleteThreadInfo(store: AtomicStore, threadId: string): Promise<boolean>

Remove a thread's metadata record, returning whether one existed. Pair with deleteConversationThread() when deleting a thread outright.

await deleteConversationThread(store, 'ticket-42');
await deleteThreadInfo(store, 'ticket-42');

async function loadThreadSummary( store: AtomicStore, threadId: string, ): Promise<ThreadSummary | null>

Load one thread with its metadata, latest run, and attention, or null when the thread does not exist.

const summary = await loadThreadSummary(store, 'ticket-42');
if (summary?.attention === 'input') console.log(summary.run!.suspendedOn!.reason);

async function listThreadSummaries( store: AtomicStore, opts: { archived?: boolean | 'all' } = {}, ): Promise<ThreadSummary[]>

List threads newest first (by ThreadState.updatedAt, then id).

archived selects which threads to return: false (the default) for active threads, true for archived ones, or 'all'. Metadata records whose thread no longer exists are ignored.

const active = await listThreadSummaries(store);
const history = await listThreadSummaries(store, { archived: true });