// Session run-state: minimal phase tracking.
//
// The abort→resume race (stale SSE events after abort) is handled by the
// runtime event-wait mechanism in thread-session-runtime.ts, NOT by state fields.
// After an abort, the runtime waits for the actual session.idle SSE event
// before starting the next dispatch — so stale events have already passed
// through by the time the new run begins.
//
// This file only tracks two things:
//   1. phase: is a run active? ('idle' or 'running')
//   2. assistant message routing for the active run.
export function initialMainRunState() {
    return {
        phase: 'idle',
        assistantMessageIds: new Set(),
        latestAssistantMessageId: undefined,
    };
}
// ── Pure transition functions ────────────────────────────────────
export function pureMarkRunning(state) {
    return {
        phase: 'running',
        assistantMessageIds: new Set(),
        latestAssistantMessageId: undefined,
    };
}
export function pureMarkIdle(_state) {
    return {
        phase: 'idle',
        assistantMessageIds: new Set(),
        latestAssistantMessageId: undefined,
    };
}
/**
 * Register an assistant message for the active run.
 * Only takes effect during 'running'.
 */
export function pureRegisterAssistantMessage(state, messageId) {
    if (state.phase !== 'running') {
        return state;
    }
    const alreadyTracked = state.assistantMessageIds.has(messageId);
    if (alreadyTracked && state.latestAssistantMessageId === messageId) {
        return state;
    }
    const assistantMessageIds = new Set(state.assistantMessageIds);
    assistantMessageIds.add(messageId);
    return {
        ...state,
        assistantMessageIds,
        latestAssistantMessageId: messageId,
    };
}
