Spaces:
Sleeping
Sleeping
File size: 4,873 Bytes
23d418f | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 | // What the Ask page shows, as a pure function of the events seen so far (live or a
// recorded replay go through the same reducer, so they look the same).
import type { AnswerEvent, AskEvent, Passage, StepDetail, SystemId } from './types'
export type StepStatus = 'pending' | 'running' | 'done' | 'skipped'
export type StepView = {
name: string
status: StepStatus
ms: number | null
detail: StepDetail | null
}
export type RunState = {
phase: 'idle' | 'running' | 'done' | 'error'
system: SystemId | null
day: string | null
live: boolean // System C reports each step; A and B are one call (no progress midway)
steps: StepView[]
evidence: Passage[] | null
answer: AnswerEvent | null
error: string | null
rewrite: { question: string; changed: boolean } | null // a follow-up, as understood
}
export const idle: RunState = {
phase: 'idle',
system: null,
day: null,
live: false,
steps: [],
evidence: null,
answer: null,
error: null,
rewrite: null,
}
/** A run starting before any event (the request is on its way). */
export function starting(system: SystemId): RunState {
return { ...idle, phase: 'running', system }
}
/** The pending step after the last finished one starts running (C: one node at a time). */
function advance(steps: StepView[]): StepView[] {
const next = steps.findIndex((s) => s.status === 'pending')
return steps.map((s, i) => (i === next ? { ...s, status: 'running' } : s))
}
function skip(steps: StepView[], names: string[]): StepView[] {
return steps.map((s) =>
names.includes(s.name) && s.status !== 'done' ? { ...s, status: 'skipped' } : s,
)
}
/** C's conditional steps, known as soon as the step before them reports: the second
* search runs only when the judge found a fact no passage states; the second judge
* only when that search found new passages. */
function skipped(name: string, detail: StepDetail): string[] {
if (name === 'judge' && detail.sufficient) return ['research', 'judge_new']
if (name === 'research' && detail.new_candidates === 0) return ['judge_new']
return []
}
export function reduce(state: RunState, event: AskEvent): RunState {
// a finished run stays finished (9b audit: an error after the answer flipped it)
if ((state.phase === 'done' || state.phase === 'error') && event.type !== 'start') return state
switch (event.type) {
case 'start': {
const live = event.system === 'c'
const steps = event.steps.map((name) => ({
name,
// A and B run their steps inside one call: all "running" until the answer
status: (live ? 'pending' : 'running') as StepStatus,
ms: null,
detail: null,
}))
return { ...idle, phase: 'running', system: event.system, day: event.day, live, steps: live ? advance(steps) : steps }
}
case 'step': {
const at = state.steps.findIndex((s) => s.name === event.name)
if (at < 0) return state
let steps = state.steps.map((s, i): StepView => {
if (i === at) return { ...s, status: 'done', ms: event.ms, detail: event.detail }
// a step before this one that never reported did not run (e.g. no candidates)
if (i < at && s.status !== 'done') return { ...s, status: 'skipped' }
return s.status === 'running' ? { ...s, status: 'pending' } : s
})
steps = advance(skip(steps, skipped(event.name, event.detail)))
return { ...state, steps }
}
case 'rewrite':
return { ...state, rewrite: { question: event.question, changed: event.changed } }
case 'evidence':
return { ...state, evidence: event.passages }
case 'answer': {
const steps = state.steps.map((s): StepView => {
if (s.status === 'done' || s.status === 'skipped') return s
// A/B: their steps all ran inside the call; C: one never reported did not run
return { ...s, status: state.live ? 'skipped' : 'done' }
})
return { ...state, phase: 'done', steps, answer: event }
}
case 'error': {
const steps = state.steps.map((s): StepView => (s.status === 'running' ? { ...s, status: 'pending' } : s))
return { ...state, phase: 'error', steps, error: event.message }
}
}
}
export const ENDED = 'The connection closed before an answer arrived. Please try again.'
/** The stream closed: a run still "running" then never got its answer or error (a
* server restart, a proxy closing the connection), so it ends in an error instead of
* spinning forever (9b audit). A finished run is unchanged. */
export function ended(state: RunState): RunState {
return state.phase === 'running' ? reduce(state, { type: 'error', kind: 'ended', message: ENDED }) : state
}
/** The step running now (for its live timer), if any. */
export function running(state: RunState): StepView | undefined {
return state.steps.find((s) => s.status === 'running')
}
|