| 109 | |
| 110 | /** Fail closed: an API error or malformed evidence never becomes a reassuring zero. */ |
| 111 | export function readSelectionImpact(value: unknown): SelectionImpactReply { |
| 112 | const row = object(value); |
| 113 | if (row.status === 'pending' || row.status === 'busy') return { status: row.status }; |
| 114 | if (row.status === 'failed') return { status: 'failed', error: text(row.error) }; |
| 115 | if (row.status !== 'ready' || (row.scope !== 'symbol' && row.scope !== 'file')) |
| 116 | throw new Error('Invalid impact snapshot'); |
| 117 | const snapshot = object(row.snapshot), structural = object(row.structural), history = object(row.history); |
| 118 | const result: SelectionImpact = { |
| 119 | status: 'ready', project: text(row.project), file_path: text(row.file_path), scope: row.scope, |
| 120 | computed_at: count(row.computed_at), cache_seconds: count(row.cache_seconds), |
| 121 | snapshot: { |
| 122 | indexed_at: text(snapshot.indexed_at), generation: text(snapshot.generation), |
| 123 | index_revision: text(snapshot.index_revision), freshness: text(snapshot.freshness), |
| 124 | coverage_recording: text(snapshot.coverage_recording), |
| 125 | coverage: list(snapshot.coverage).map(value => { |
| 126 | const item = object(value); return { kind: text(item.kind), count: count(item.count) }; |
| 127 | }), |
| 128 | }, |
| 129 | structural: { |
| 130 | available: flag(structural.available), basis: text(structural.basis), |
| 131 | max_depth: count(structural.max_depth), visit_cap: count(structural.visit_cap), |
| 132 | result_cap: count(structural.result_cap), seed_count: count(structural.seed_count), |
| 133 | reachable: count(structural.reachable), direct: count(structural.direct), |
| 134 | test_candidates: count(structural.test_candidates), truncated: flag(structural.truncated), |
| 135 | ...(structural.error === undefined ? {} : { error: text(structural.error) }), |
| 136 | findings: list(structural.findings).map(value => { |
| 137 | const item = object(value); |
| 138 | const path = list(item.path).map(value => { |
| 139 | const edge = object(value); |
| 140 | if (edge.type !== 'CALLS' && edge.type !== 'IMPORTS') throw new Error('Unknown impact relation'); |
| 141 | return { edge_id: count(edge.edge_id), type: edge.type, |
| 142 | from: node(edge.from), to: node(edge.to) } as ImpactEvidenceEdge; |
| 143 | }); |
| 144 | const finding = { ...node(item), distance: count(item.distance), |
| 145 | test_candidate: flag(item.test_candidate), path }; |
| 146 | if (path.length === 0 || path.length !== finding.distance || path[0]?.from.id !== finding.id |
| 147 | || path.some((edge, i) => i > 0 && path[i - 1]?.to.id !== edge.from.id)) |
| 148 | throw new Error('Disconnected impact evidence path'); |
| 149 | return finding; |
| 150 | }), |
| 151 | }, |
| 152 | history: { |
| 153 | available: flag(history.available), truncated: flag(history.truncated), |
| 154 | commit_cap: count(history.commit_cap), window_days: count(history.window_days), |
| 155 | mass_change_threshold: count(history.mass_change_threshold), |
| 156 | commits_scanned: count(history.commits_scanned), commits_considered: count(history.commits_considered), |
| 157 | selection_commits: count(history.selection_commits), merges_excluded: count(history.merges_excluded), |
| 158 | mass_changes_excluded: count(history.mass_changes_excluded), read_errors: count(history.read_errors), |
| 159 | cochanges_omitted: count(history.cochanges_omitted), |
| 160 | ...(history.head === undefined ? {} : { head: text(history.head) }), |
| 161 | ...(history.error === undefined ? {} : { error: text(history.error) }), |
| 162 | ...(history.shallow === undefined ? {} : { shallow: flag(history.shallow) }), |
| 163 | ...(history.worktree_status_known === undefined ? {} : { worktree_status_known: flag(history.worktree_status_known) }), |
| 164 | ...(history.selection_uncommitted === undefined ? {} : { selection_uncommitted: flag(history.selection_uncommitted) }), |
| 165 | commits: list(history.commits).map(value => { |
| 166 | const item = object(value); return { hash: text(item.hash), subject: text(item.subject), |
| 167 | time: count(item.time), files: count(item.files) }; |
| 168 | }), |