(
db: Database,
sessionId: string,
directory: string,
liveSessionState?: LiveSessionState,
injectionBudgetTokens?: number,
// Optional config so the sidebar can show the effective execute threshold
// alongside `usagePercentage` (e.g. "47.5% / 65%"). Resolved per-model from
// `liveSessionState.liveModelBySession`. When omitted (e.g. legacy test
// callers), the snapshot falls back to the runtime default of 65%.
config?: Record<string, unknown>,
)
| 136 | // Exported for test access. Production code reaches this via the |
| 137 | // "sidebar-snapshot" RPC handler registered below. |
| 138 | export function buildSidebarSnapshot( |
| 139 | db: Database, |
| 140 | sessionId: string, |
| 141 | directory: string, |
| 142 | liveSessionState?: LiveSessionState, |
| 143 | injectionBudgetTokens?: number, |
| 144 | // Optional config so the sidebar can show the effective execute threshold |
| 145 | // alongside `usagePercentage` (e.g. "47.5% / 65%"). Resolved per-model from |
| 146 | // `liveSessionState.liveModelBySession`. When omitted (e.g. legacy test |
| 147 | // callers), the snapshot falls back to the runtime default of 65%. |
| 148 | config?: Record<string, unknown>, |
| 149 | ): SidebarSnapshot { |
| 150 | const empty: SidebarSnapshot = { |
| 151 | sessionId, |
| 152 | usagePercentage: 0, |
| 153 | inputTokens: 0, |
| 154 | contextLimit: 0, |
| 155 | systemPromptTokens: 0, |
| 156 | compartmentCount: 0, |
| 157 | factCount: 0, |
| 158 | memoryCount: 0, |
| 159 | memoryBlockCount: 0, |
| 160 | pendingOpsCount: 0, |
| 161 | historianRunning: false, |
| 162 | compartmentInProgress: false, |
| 163 | sessionNoteCount: 0, |
| 164 | readySmartNoteCount: 0, |
| 165 | cacheTtl: "5m", |
| 166 | lastDreamerRunAt: null, |
| 167 | projectIdentity: null, |
| 168 | compartmentTokens: 0, |
| 169 | factTokens: 0, |
| 170 | memoryTokens: 0, |
| 171 | docsTokens: 0, |
| 172 | profileTokens: 0, |
| 173 | conversationTokens: 0, |
| 174 | toolCallTokens: 0, |
| 175 | toolDefinitionTokens: 0, |
| 176 | executeThreshold: 65, |
| 177 | newWorkTokens: null, |
| 178 | totalInputTokens: null, |
| 179 | }; |
| 180 | |
| 181 | try { |
| 182 | const projectIdentity = resolveProjectIdentity(directory); |
| 183 | |
| 184 | const meta = db |
| 185 | .prepare<[string], Record<string, unknown>>( |
| 186 | "SELECT * FROM session_meta WHERE session_id = ?", |
| 187 | ) |
| 188 | .get(sessionId); |
| 189 | |
| 190 | const usagePercentage = meta |
| 191 | ? Number(meta.last_context_percentage ?? meta.last_usage_percentage ?? 0) |
| 192 | : 0; |
| 193 | const inputTokens = meta ? Number(meta.last_input_tokens ?? 0) : 0; |
| 194 | // Work-metrics are computed lazily + incrementally HERE (the only |
| 195 | // consumer), not in the transform hot path. The persisted session_meta |
no test coverage detected