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 | 'cacheRead'
34> & {
35 contextWindow?: ApiFactSource
36 maxOutput?: ApiFactSource
37 modalities?: ApiFactSource
38 pricing?: ApiFactSource
39 reasoning?: ApiFactSource
40 requestMap?: ApiFactSource
41 requestMapFields?: Partial<Record<keyof ChatRequestMap, ApiFactSource>>
42 capabilities?: Record<string, FactSource> | SourceSilentEvidence
43 serverTools?: Record<string, FactSource> | SourceSilentEvidence
44 schemaEndpointId?: ApiFactSource
45 /** A stated "-" or blank cached-input cell, or a ledgered absence. */
46 cacheRead?: ApiFactSource
47 efforts?: SourceSilentEvidence
48}
49
50export type SourceSilentEvidenceLedger = Map<
51 string,
52 Map<FactKey, SourceSilentEvidence>
53>
54
55/** Same entry grammar as scoring; metadata is copied only when actually recorded. */
56export function parseSourceSilentEvidence(
57 markdown: string,
58): SourceSilentEvidenceLedger {
59 const ledger = parseLedger(markdown)
60 const evidence: SourceSilentEvidenceLedger = new Map()
61 for (const line of markdown.split('\n')) {
62 const entry = parseLedgerEntry(line)
63 if (!entry) continue
64 const { scope } = entry
65 const key = entry.fact as FactKey
66 if (!ledger.get(scope)?.has(key)) continue
67 const url = /https?:\/\/[^\s,`)]+/.exec(line)?.[0]
68 const date = /\bchecked\s+([^\s,`);.]+)/.exec(line)?.[1]
69 if (date) {
70 const time = Date.parse(`${date}T00:00:00Z`)
71 if (
72 !/^\d{4}-\d{2}-\d{2}$/.test(date) ||
73 !Number.isFinite(time) ||
74 new Date(time).toISOString().slice(0, 10) !== date
75 ) {
76 throw new Error(`source-silent ledger: invalid checked date ${date}`)
77 }
78 }
79 const source: SourceSilentEvidence = {
80 derivation: 'source-silent',
81 ...(url ? { sourceUrl: url } : {}),
82 ...(date ? { checkedAt: date } : {}),
83 }
84 const entries =
85 evidence.get(scope) ?? new Map<FactKey, SourceSilentEvidence>()
86 if (entries.has(key))
87 throw new Error(`source-silent ledger: duplicate ${scope}: ${key}`)
88 entries.set(key, source)
89 evidence.set(scope, entries)
90 }
91 return evidence
92}
93
94interface SilentModelFacts {
95 providerId: string
96 rawId?: string
97 activity: string | null
98 contextWindow: unknown
99 maxOutput: unknown
100 modalities: unknown
101 pricing: unknown
102 capabilities: unknown
103 reasoning: unknown
104 requestMap: unknown
105}
106
107function record(value: unknown): value is Record<string, unknown> {
108 return value !== null && typeof value === 'object' && !Array.isArray(value)
109}
110
111function missingCacheRead(pricing: unknown): boolean {
112 if (
113 !record(pricing) ||
114 !record(pricing.tables) ||
115 !record(pricing.tables.rate)
116 )
117 return true
118 return !Object.values(pricing.tables.rate).some(
119 (tier) =>
120 record(tier) &&
121 (tier.cache_read_tokens != null || tier.input_cache_read != null),
122 )
123}
124
125/** Never alter model values, populated-fact provenance, or resolved identity evidence. */
126export function withSourceSilentEvidence(
127 row: SilentModelFacts,
128 schemaEndpointId: string | null,
129 stored: ModelFactSources | null,
130 ledger: SourceSilentEvidenceLedger,
131): ApiModelFactSources | null {
132 // The source-silent ledger describes the chat facts scored by completeness.
133 if (row.activity !== 'chat') return stored
134 const entries = new Map([
135 ...(ledger.get(row.providerId) ?? []),
136 ...(row.rawId !== undefined
137 ? (ledger.get(modelLedgerScope(row.providerId, row.rawId)) ?? [])
138 : []),
139 ])
140 if (!entries.size) return stored
141 const sources: ApiModelFactSources = { ...stored }
142 for (const [key, evidence] of entries) {
143 switch (key) {
144 case 'contextWindow':
145 case 'maxOutput':
146 case 'modalities':
147 case 'capabilities':
148 case 'reasoning':
149 case 'requestMap':
150 if (row[key] == null) sources[key] = evidence
151 break
152 case 'replayReasoningContent':
153 if (
154 !record(row.requestMap) ||
155 row.requestMap.replayReasoningContent == null
156 )
157 sources.requestMapFields = {
158 ...sources.requestMapFields,
159 replayReasoningContent: evidence,
160 }
161 break
162 case 'priced':
163 if (row.pricing == null) sources.pricing = evidence
164 break
165 case 'endpoint':
166 if (schemaEndpointId === null) sources.schemaEndpointId = evidence
167 break
168 case 'cacheRead':
169 // A stored `not-offered` source is the stated absence. The ledger
170 // only fills a price the row does not already explain.
171 if (missingCacheRead(row.pricing) && stored?.cacheRead == null)
172 sources.cacheRead = evidence
173 break
174 case 'efforts':
175 if (
176 record(row.reasoning) &&
177 (row.reasoning.mode === 'effort' ||
178 row.reasoning.mode === 'adaptive') &&
179 (!Array.isArray(row.reasoning.efforts) ||
180 row.reasoning.efforts.length === 0)
181 )
182 sources.efforts = evidence
183 break
184 }
185 }
186 return Object.keys(sources).length ? sources : stored
187}