File size: 5,344 Bytes
48ea638
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import type { SubagentStatus } from '@hermes/shared/gateway-events'
import { atom } from 'nanostores'

import type { SpawnTreeLoadResponse } from '../gatewayTypes.js'
import type { SubagentProgress } from '../types.js'

export interface SpawnSnapshot {
  finishedAt: number
  fromDisk?: boolean
  id: string
  label: string
  path?: string
  sessionId: null | string
  startedAt: number
  subagents: SubagentProgress[]
}

export interface SpawnDiffPair {
  baseline: SpawnSnapshot
  candidate: SpawnSnapshot
}

const HISTORY_LIMIT = 10

const KNOWN_SUBAGENT_STATUSES = new Set<SubagentStatus>([
  'completed',
  'error',
  'failed',
  'interrupted',
  'queued',
  'running',
  'timeout'
])

const normalizeSubagentStatus = (status: unknown, fallback: SubagentStatus): SubagentStatus => {
  if (typeof status !== 'string') {
    return fallback
  }

  const normalized = status.toLowerCase() as SubagentStatus

  return KNOWN_SUBAGENT_STATUSES.has(normalized) ? normalized : fallback
}

export const $spawnHistory = atom<SpawnSnapshot[]>([])
export const $spawnDiff = atom<null | SpawnDiffPair>(null)

export const getSpawnHistory = () => $spawnHistory.get()
export const getSpawnDiff = () => $spawnDiff.get()

export const clearSpawnHistory = () => $spawnHistory.set([])
export const clearDiffPair = () => $spawnDiff.set(null)
export const setDiffPair = (pair: SpawnDiffPair) => $spawnDiff.set(pair)

/**
 * Commit a finished turn's spawn tree to history.  Keeps the last 10
 * non-empty snapshots — empty turns (no subagents) are dropped.
 *
 * Why in-memory?  The primary investigation loop is "I just ran a fan-out,
 * it misbehaved, let me look at what happened" — same-session debugging.
 * Disk persistence across process restarts is a natural extension but
 * adds RPC surface for a less-common path.
 */
export const pushSnapshot = (
  subagents: readonly SubagentProgress[],
  meta: { sessionId?: null | string; startedAt?: null | number }
) => {
  if (!subagents.length) {
    return
  }

  const now = Date.now()
  const started = meta.startedAt ?? Math.min(...subagents.map(s => s.startedAt ?? now))

  const snap: SpawnSnapshot = {
    finishedAt: now,
    id: `snap-${now.toString(36)}`,
    label: summarizeLabel(subagents),
    sessionId: meta.sessionId ?? null,
    startedAt: Number.isFinite(started) ? started : now,
    subagents: subagents.map(item => ({ ...item }))
  }

  const next = [snap, ...$spawnHistory.get()].slice(0, HISTORY_LIMIT)
  $spawnHistory.set(next)
}

function summarizeLabel(subagents: readonly SubagentProgress[]): string {
  const top = subagents
    .filter(s => s.parentId == null || subagents.every(o => o.id !== s.parentId))
    .slice(0, 2)
    .map(s => s.goal || 'subagent')
    .join(' · ')

  return top || `${subagents.length} agent${subagents.length === 1 ? '' : 's'}`
}

/**
 * Push a disk-loaded snapshot onto the front of the history stack so the
 * overlay can pick it up at index 1 via /replay load.  Normalises the
 * server payload (arbitrary list) into the same SubagentProgress shape
 * used for live data — defensive against cross-version reads.
 */
export const pushDiskSnapshot = (r: SpawnTreeLoadResponse, path: string) => {
  const raw = Array.isArray(r.subagents) ? r.subagents : []
  const normalised = raw.map(normaliseSubagent)

  if (!normalised.length) {
    return
  }

  const snap: SpawnSnapshot = {
    finishedAt: (r.finished_at ?? Date.now() / 1000) * 1000,
    fromDisk: true,
    id: `disk-${path}`,
    label: r.label || `${normalised.length} subagents`,
    path,
    sessionId: r.session_id ?? null,
    startedAt: (r.started_at ?? r.finished_at ?? Date.now() / 1000) * 1000,
    subagents: normalised
  }

  const next = [snap, ...$spawnHistory.get()].slice(0, HISTORY_LIMIT)
  $spawnHistory.set(next)
}

function normaliseSubagent(raw: unknown): SubagentProgress {
  const o = raw as Record<string, unknown>
  const s = (v: unknown) => (typeof v === 'string' ? v : undefined)
  const n = (v: unknown) => (typeof v === 'number' ? v : undefined)
  const arr = <T>(v: unknown): T[] | undefined => (Array.isArray(v) ? (v as T[]) : undefined)

  return {
    apiCalls: n(o.apiCalls),
    costUsd: n(o.costUsd),
    depth: typeof o.depth === 'number' ? o.depth : 0,
    durationSeconds: n(o.durationSeconds),
    filesRead: arr<string>(o.filesRead),
    filesWritten: arr<string>(o.filesWritten),
    goal: s(o.goal) ?? 'subagent',
    id: s(o.id) ?? `sa-${Math.random().toString(36).slice(2, 8)}`,
    index: typeof o.index === 'number' ? o.index : 0,
    inputTokens: n(o.inputTokens),
    iteration: n(o.iteration),
    model: s(o.model),
    notes: (arr<string>(o.notes) ?? []).filter(x => typeof x === 'string'),
    outputTail: arr(o.outputTail) as SubagentProgress['outputTail'],
    outputTokens: n(o.outputTokens),
    parentId: s(o.parentId) ?? null,
    reasoningTokens: n(o.reasoningTokens),
    startedAt: n(o.startedAt),
    status: normalizeSubagentStatus(o.status, 'completed'),
    summary: s(o.summary),
    taskCount: typeof o.taskCount === 'number' ? o.taskCount : 1,
    thinking: (arr<string>(o.thinking) ?? []).filter(x => typeof x === 'string'),
    toolCount: typeof o.toolCount === 'number' ? o.toolCount : 0,
    tools: (arr<string>(o.tools) ?? []).filter(x => typeof x === 'string'),
    toolsets: arr<string>(o.toolsets)
  }
}