Open History to see the last commit
1/** Read-time evidence for provider facts absent from the published sources. */
2import { parseLedger } from '#/lib/completeness.ts'
3import type { FactKey } from '#/lib/completeness.ts'
4import {
5 modelLedgerScope,
6 parseLedgerEntry,
7} from '#/lib/source-silence-ledger.ts'
8import type { ChatRequestMap } from './providers/request-map.ts'
9import type { FactSource, ModelFactSources } from './providers/types.ts'
10
11export interface SourceSilentEvidence {
12 derivation: 'source-silent'
13 sourceUrl?: string
14 /** The date recorded by the ledger author, never a request timestamp. */
15 checkedAt?: string
16}
17
18export type ApiFactSource = FactSource | SourceSilentEvidence
19
20/** Stored ingest provenance remains unchanged; silence is added only on reads. */
21export type ApiModelFactSources = Omit<
22 ModelFactSources,
23 | 'contextWindow'
24 | 'maxOutput'
25 | 'modalities'
26 | 'pricing'
27 | 'reasoning'
28 | 'requestMap'
29 | 'capabilities'
30 | 'serverTools'
31 | 'schemaEndpointId'
32 | 'requestMapFields'
33> & {
34 contextWindow?: ApiFactSource
35 maxOutput?: ApiFactSource
36 modalities?: ApiFactSource
37 pricing?: ApiFactSource
38 reasoning?: ApiFactSource
39 requestMap?: ApiFactSource
40 requestMapFields?: Partial<Record<keyof ChatRequestMap, ApiFactSource>>
41 capabilities?: Record<string, FactSource> | SourceSilentEvidence
42 serverTools?: Record<string, FactSource> | SourceSilentEvidence
43 schemaEndpointId?: ApiFactSource
44 cacheRead?: SourceSilentEvidence
45 efforts?: SourceSilentEvidence
46}
47
48export type SourceSilentEvidenceLedger = Map<
49 string,
50 Map<FactKey, SourceSilentEvidence>
51>
52
53/** Same entry grammar as scoring; metadata is copied only when actually recorded. */
54export function parseSourceSilentEvidence(
55 markdown: string,
56): SourceSilentEvidenceLedger {
57 const ledger = parseLedger(markdown)
58 const evidence: SourceSilentEvidenceLedger = new Map()
59 for (const line of markdown.split('\n')) {
60 const entry = parseLedgerEntry(line)
61 if (!entry) continue
62 const { scope } = entry
63 const key = entry.fact as FactKey
64 if (!ledger.get(scope)?.has(key)) continue
65 const url = /https?:\/\/[^\s,`)]+/.exec(line)?.[0]
66 const date = /\bchecked\s+([^\s,`);.]+)/.exec(line)?.[1]
67 if (date) {
68 const time = Date.parse(`${date}T00:00:00Z`)
69 if (
70 !/^\d{4}-\d{2}-\d{2}$/.test(date) ||
71 !Number.isFinite(time) ||
72 new Date(time).toISOString().slice(0, 10) !== date
73 ) {
74 throw new Error(`source-silent ledger: invalid checked date ${date}`)
75 }
76 }
77 const source: SourceSilentEvidence = {
78 derivation: 'source-silent',
79 ...(url ? { sourceUrl: url } : {}),
80 ...(date ? { checkedAt: date } : {}),
81 }
82 const entries =
83 evidence.get(scope) ?? new Map<FactKey, SourceSilentEvidence>()
84 if (entries.has(key))
85 throw new Error(`source-silent ledger: duplicate ${scope}: ${key}`)
86 entries.set(key, source)
87 evidence.set(scope, entries)
88 }
89 return evidence
90}
91
92interface SilentModelFacts {
93 providerId: string
94 rawId?: string
95 activity: string | null
96 contextWindow: unknown
97 maxOutput: unknown
98 modalities: unknown
99 pricing: unknown
100 capabilities: unknown
101 reasoning: unknown
102 requestMap: unknown
103}
104
105function record(value: unknown): value is Record<string, unknown> {
106 return value !== null && typeof value === 'object' && !Array.isArray(value)
107}
108
109function missingCacheRead(pricing: unknown): boolean {
110 if (
111 !record(pricing) ||
112 !record(pricing.tables) ||
113 !record(pricing.tables.rate)
114 )
115 return true
116 return !Object.values(pricing.tables.rate).some(
117 (tier) =>
118 record(tier) &&
119 (tier.cache_read_tokens != null || tier.input_cache_read != null),
120 )
121}
122
123/** Never alter model values, populated-fact provenance, or resolved identity evidence. */
124export function withSourceSilentEvidence(
125 row: SilentModelFacts,
126 schemaEndpointId: string | null,
127 stored: ModelFactSources | null,
128 ledger: SourceSilentEvidenceLedger,
129): ApiModelFactSources | null {
130 // The source-silent ledger describes the chat facts scored by completeness.
131 if (row.activity !== 'chat') return stored
132 const entries = new Map([
133 ...(ledger.get(row.providerId) ?? []),
134 ...(row.rawId !== undefined
135 ? (ledger.get(modelLedgerScope(row.providerId, row.rawId)) ?? [])
136 : []),
137 ])
138 if (!entries.size) return stored
139 const sources: ApiModelFactSources = { ...stored }
140 for (const [key, evidence] of entries) {
141 switch (key) {
142 case 'contextWindow':
143 case 'maxOutput':
144 case 'modalities':
145 case 'capabilities':
146 case 'reasoning':
147 case 'requestMap':
148 if (row[key] == null) sources[key] = evidence
149 break
150 case 'replayReasoningContent':
151 if (
152 !record(row.requestMap) ||
153 row.requestMap.replayReasoningContent == null
154 )
155 sources.requestMapFields = {
156 ...sources.requestMapFields,
157 replayReasoningContent: evidence,
158 }
159 break
160 case 'priced':
161 if (row.pricing == null) sources.pricing = evidence
162 break
163 case 'endpoint':
164 if (schemaEndpointId === null) sources.schemaEndpointId = evidence
165 break
166 case 'cacheRead':
167 if (missingCacheRead(row.pricing)) sources.cacheRead = evidence
168 break
169 case 'efforts':
170 if (
171 record(row.reasoning) &&
172 (row.reasoning.mode === 'effort' ||
173 row.reasoning.mode === 'adaptive') &&
174 (!Array.isArray(row.reasoning.efforts) ||
175 row.reasoning.efforts.length === 0)
176 )
177 sources.efforts = evidence
178 break
179 }
180 }
181 return Object.keys(sources).length ? sources : stored
182}