pantheon-opencode 1.5.0-beta.2 → 1.5.0-beta.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,304 @@
1
+ /**
2
+ * Compaction handoff coordinator.
3
+ *
4
+ * Coordinates the handoff of a compaction checkpoint from in-memory state to
5
+ * durable persistence. The coordinator is deliberately dependency-free: it
6
+ * does not import plugin, board, MCP, hooks, or provider modules.
7
+ */
8
+
9
+ export interface HandoffRecord {
10
+ digest: string
11
+ payload: string
12
+ version: number
13
+ sessionId: string
14
+ leaseToken: string
15
+ }
16
+
17
+ export interface PersistencePort {
18
+ prepare(input: HandoffRecord): Promise<void>
19
+ read(sessionId: string, key: string): Promise<HandoffRecord | null>
20
+ commit(sessionId: string, key: string, record: HandoffRecord): Promise<void>
21
+ }
22
+
23
+ export interface MemoryPort {
24
+ publishImmutable(sessionId: string, key: string, record: HandoffRecord): Promise<void>
25
+ readByIdempotencyKey(key: string): Promise<HandoffRecord | null>
26
+ }
27
+
28
+ export type HandoffStatus =
29
+ | 'COMMITTED'
30
+ | 'UNAVAILABLE'
31
+ | 'CONFLICT'
32
+ | 'NOT_SUPPORTED'
33
+ | 'INVALID_INPUT'
34
+
35
+ /** Maximum allowed integer version value. */
36
+ export const MAX_SAFE_INTEGER = Number.MAX_SAFE_INTEGER
37
+
38
+ /** Maximum encoded payload size in bytes (128 KiB). */
39
+ export const MAX_PAYLOAD_BYTES = 65_536
40
+
41
+ /** Maximum length of URL-safe identifiers. */
42
+ export const MAX_ID_LENGTH = 128
43
+
44
+ const URL_SAFE_PATTERN = /^[A-Za-z0-9._~-]+$/
45
+ const DIGEST_PATTERN = /^[a-f0-9]{64}$/
46
+
47
+ /** Validate a URL-safe identifier: 1-128 chars, no control characters. */
48
+ function isValidIdentifier(value: unknown): value is string {
49
+ if (typeof value !== 'string') {
50
+ return false
51
+ }
52
+ if (value.length < 1 || value.length > MAX_ID_LENGTH) {
53
+ return false
54
+ }
55
+ if (value.length > 0 && !URL_SAFE_PATTERN.test(value)) {
56
+ return false
57
+ }
58
+ return true
59
+ }
60
+
61
+ /** Validate a lease token: 1-128 chars, no control characters. */
62
+ function isValidLeaseToken(value: unknown): value is string {
63
+ if (typeof value !== 'string') {
64
+ return false
65
+ }
66
+ if (value.length < 1 || value.length > MAX_ID_LENGTH) {
67
+ return false
68
+ }
69
+ for (const char of value) {
70
+ const code = char.charCodeAt(0)
71
+ if (code < 0x20 || code === 0x7f) {
72
+ return false
73
+ }
74
+ }
75
+ return true
76
+ }
77
+
78
+ /** Validate a positive integer version within the safe integer range. */
79
+ function isValidVersion(value: unknown): value is number {
80
+ return (
81
+ typeof value === 'number' && Number.isInteger(value) && value > 0 && value <= MAX_SAFE_INTEGER
82
+ )
83
+ }
84
+
85
+ /** Validate a 64-character lowercase hex digest. */
86
+ function isValidDigest(value: unknown): value is string {
87
+ return typeof value === 'string' && DIGEST_PATTERN.test(value)
88
+ }
89
+
90
+ /**
91
+ * Validate that a payload is UTF-8 encodable and contains no NUL or control
92
+ * characters (tab, LF, and CR are permitted).
93
+ */
94
+ function isValidPayload(value: unknown): value is string {
95
+ if (typeof value !== 'string') {
96
+ return false
97
+ }
98
+ if (value.length < 1 || value.length > MAX_PAYLOAD_BYTES) {
99
+ return false
100
+ }
101
+
102
+ const bytes = Buffer.byteLength(value, 'utf8')
103
+ if (bytes < 1 || bytes > MAX_PAYLOAD_BYTES) {
104
+ return false
105
+ }
106
+ if (value.includes('\0')) {
107
+ return false
108
+ }
109
+
110
+ for (const char of value) {
111
+ const code = char.charCodeAt(0)
112
+ if (code < 0x20 && code !== 0x09 && code !== 0x0a && code !== 0x0d) {
113
+ return false
114
+ }
115
+ }
116
+ return true
117
+ }
118
+
119
+ /**
120
+ * Compute the digest over the exact UTF-8 encoded bytes of a payload.
121
+ */
122
+ export function computeDigest(payload: string): string {
123
+ const bytes = Buffer.from(payload, 'utf8')
124
+ let hash = ''
125
+ for (const byte of bytes) {
126
+ hash += byte.toString(16).padStart(2, '0')
127
+ }
128
+ return hash
129
+ }
130
+
131
+ /** Create a result describing a failed validation. */
132
+ function invalid(message: string): { status: 'INVALID_INPUT'; message: string } {
133
+ return { status: 'INVALID_INPUT', message }
134
+ }
135
+
136
+ /** Create a result describing a missing dependency. */
137
+ function notSupported(): { status: 'NOT_SUPPORTED' } {
138
+ return { status: 'NOT_SUPPORTED' }
139
+ }
140
+
141
+ /** Create a result describing a conflict. */
142
+ function conflict(message: string): { status: 'CONFLICT'; message: string } {
143
+ return { status: 'CONFLICT', message }
144
+ }
145
+
146
+ /** Create a result describing an unavailable memory port. */
147
+ function unavailable(message: string): { status: 'UNAVAILABLE'; message: string } {
148
+ return { status: 'UNAVAILABLE', message }
149
+ }
150
+
151
+ /** Create a successful committed result. */
152
+ function committed(digest: string): { status: 'COMMITTED'; digest: string } {
153
+ return { status: 'COMMITTED', digest }
154
+ }
155
+
156
+ /**
157
+ * Compaction handoff coordinator.
158
+ *
159
+ * The coordinator drives the handoff FSM:
160
+ *
161
+ * no record -> prepare -> PREPARED -> memory.publishImmutable
162
+ * -> memory.readByIdempotencyKey -> persistence.commit -> COMMITTED
163
+ *
164
+ * It never retries after memory becomes unavailable; the PREPARED state is
165
+ * left intact for a later explicit attempt.
166
+ */
167
+ export class CompactionHandoffCoordinator {
168
+ private readonly persistence: PersistencePort | undefined
169
+ private readonly memory: MemoryPort | undefined
170
+
171
+ constructor(persistence?: PersistencePort, memory?: MemoryPort) {
172
+ this.persistence = persistence
173
+ this.memory = memory
174
+ }
175
+
176
+ /**
177
+ * Attempt to hand off a compaction checkpoint.
178
+ *
179
+ * Returns immediately with INVALID_INPUT if any argument fails validation.
180
+ * Returns NOT_SUPPORTED if either required port is missing.
181
+ * Returns CONFLICT for digest or lease/version mismatches.
182
+ * Returns UNAVAILABLE if the memory port cannot publish/read the record.
183
+ * Returns COMMITTED when the record is durably committed.
184
+ */
185
+ async prepare(
186
+ sessionId: string,
187
+ idempotencyKey: string,
188
+ leaseToken: string,
189
+ version: number,
190
+ digest: string,
191
+ payload: string,
192
+ ): Promise<{ status: HandoffStatus; key?: string; digest?: string; message?: string }> {
193
+ // Validation first: no port calls are made before this block completes.
194
+ if (
195
+ !isValidIdentifier(sessionId) ||
196
+ !isValidIdentifier(idempotencyKey) ||
197
+ !isValidLeaseToken(leaseToken) ||
198
+ !isValidVersion(version) ||
199
+ !isValidDigest(digest) ||
200
+ !isValidPayload(payload)
201
+ ) {
202
+ return invalid('Invalid handoff input')
203
+ }
204
+
205
+ if (this.persistence === undefined || this.memory === undefined) {
206
+ return notSupported()
207
+ }
208
+
209
+ const _key = `${sessionId}:${idempotencyKey}`
210
+
211
+ // Check for an existing committed record before preparing a new one.
212
+ const existingRecord = await this.persistence.read(sessionId, idempotencyKey)
213
+ if (existingRecord !== null) {
214
+ if (existingRecord.digest === digest && existingRecord.sessionId === sessionId) {
215
+ // Idempotent replay: same digest and session. Verify lease/version match.
216
+ if (existingRecord.leaseToken !== leaseToken || existingRecord.version !== version) {
217
+ return conflict('Stale lease or version')
218
+ }
219
+
220
+ // Verify the record is also in memory. If missing, try to recover by publishing once.
221
+ let memoryRecord: HandoffRecord | null = null
222
+ try {
223
+ memoryRecord = await this.memory.readByIdempotencyKey(idempotencyKey)
224
+ } catch {
225
+ return unavailable('Memory port unavailable')
226
+ }
227
+
228
+ if (memoryRecord === null) {
229
+ // Recovery: memory lost the record but persistence has it. Publish once.
230
+ try {
231
+ await this.memory.publishImmutable(sessionId, idempotencyKey, existingRecord)
232
+ } catch (_error) {
233
+ return unavailable('Memory port unavailable')
234
+ }
235
+ try {
236
+ memoryRecord = await this.memory.readByIdempotencyKey(idempotencyKey)
237
+ } catch (_error) {
238
+ return unavailable('Memory port unavailable')
239
+ }
240
+ }
241
+
242
+ if (memoryRecord === null) {
243
+ return unavailable('Record not found in memory')
244
+ }
245
+
246
+ await this.persistence.commit(sessionId, idempotencyKey, existingRecord)
247
+ return committed(memoryRecord.digest)
248
+ }
249
+ // Different digest or session: conflict.
250
+ return conflict('Digest or session mismatch')
251
+ }
252
+
253
+ // Phase 1: prepare in persistence (PREPARED).
254
+ await this.persistence.prepare({ digest, payload, version, sessionId, leaseToken })
255
+
256
+ // Phase 2: publish the record to memory. If memory is unavailable, the
257
+ // coordinator stays in PREPARED and never retries automatically.
258
+ try {
259
+ await this.memory.publishImmutable(sessionId, idempotencyKey, {
260
+ digest,
261
+ payload,
262
+ version,
263
+ sessionId,
264
+ leaseToken,
265
+ })
266
+ } catch (_error) {
267
+ return unavailable('Memory port unavailable')
268
+ }
269
+
270
+ // Phase 3: read back from memory by idempotency key.
271
+ let memoryRecord: HandoffRecord | null = null
272
+ try {
273
+ memoryRecord = await this.memory.readByIdempotencyKey(idempotencyKey)
274
+ } catch (_error) {
275
+ return unavailable('Memory port unavailable')
276
+ }
277
+
278
+ if (memoryRecord === null) {
279
+ return unavailable('Record not found in memory')
280
+ }
281
+
282
+ // Stale lease or version check.
283
+ if (memoryRecord.leaseToken !== leaseToken || memoryRecord.version !== version) {
284
+ return conflict('Stale lease or version')
285
+ }
286
+
287
+ // Idempotent replay: same key and session is a success.
288
+ // The memory record is authoritative for recovery after crash.
289
+ if (memoryRecord.sessionId === sessionId) {
290
+ // Phase 4: commit to persistence.
291
+ try {
292
+ await this.persistence.commit(sessionId, idempotencyKey, memoryRecord)
293
+ } catch (_error) {
294
+ return conflict('Persistence commit failed')
295
+ }
296
+ return committed(memoryRecord.digest)
297
+ }
298
+
299
+ // Same key, different digest or session: conflict.
300
+ return conflict('Digest or session mismatch')
301
+ }
302
+ }
303
+
304
+ export default CompactionHandoffCoordinator
@@ -0,0 +1,221 @@
1
+ /**
2
+ * Exact provider usage contract and the independent V1/V2 capability probes.
3
+ *
4
+ * This module deliberately does not inspect credentials, call a provider, read
5
+ * the TUI, calculate percentages, or derive token counts from text. A provider
6
+ * is supported only when it supplies the complete numeric contract below.
7
+ *
8
+ * @module provider-capabilities
9
+ */
10
+
11
+ /** The only capability statuses exposed by the usage contract. */
12
+ export const USAGE_STATUS = {
13
+ SUPPORTED: 'SUPPORTED',
14
+ UNSUPPORTED: 'UNSUPPORTED',
15
+ } as const
16
+
17
+ export type UsageStatus = (typeof USAGE_STATUS)[keyof typeof USAGE_STATUS]
18
+
19
+ /** Exact token counters supplied by the provider. No totals are inferred. */
20
+ export interface ExactProviderUsage {
21
+ inputTokens: number
22
+ outputTokens: number
23
+ }
24
+
25
+ /** Exact context limit supplied by the provider. */
26
+ export interface ExactProviderLimit {
27
+ contextTokens: number
28
+ }
29
+
30
+ /**
31
+ * Minimum complete provider payload accepted by either capability probe.
32
+ * `resetAt`, when present, is an exact non-negative numeric provider value;
33
+ * date strings and other reset representations are unsupported.
34
+ */
35
+ export interface ExactProviderUsageData {
36
+ sessionId: string
37
+ providerId: string
38
+ modelId: string
39
+ usage: ExactProviderUsage
40
+ limit: ExactProviderLimit
41
+ resetAt?: number
42
+ }
43
+
44
+ /** Stream event metadata required for de-duplication and freshness ordering. */
45
+ export interface ProviderUsageEvent extends ExactProviderUsageData {
46
+ eventId: string
47
+ partId: string
48
+ /** Optional provider sequence. When supplied, it must be an integer. */
49
+ sequence?: number
50
+ /** Optional provider observation time in milliseconds or another numeric scale. */
51
+ observedAt?: number
52
+ }
53
+
54
+ /** A complete snapshot that has passed the exact contract validation. */
55
+ export interface ProviderUsageSnapshot extends ProviderUsageEvent {}
56
+
57
+ export type CapabilityProbeVersion = 'v1' | 'v2'
58
+
59
+ export type CapabilityProbeReason =
60
+ | 'MISSING_SESSION_ID'
61
+ | 'MISSING_PROVIDER_ID'
62
+ | 'MISSING_MODEL_ID'
63
+ | 'MISSING_USAGE'
64
+ | 'MISSING_LIMIT'
65
+ | 'INVALID_USAGE'
66
+ | 'INVALID_LIMIT'
67
+ | 'INVALID_RESET'
68
+
69
+ export type ProviderCapabilityResult =
70
+ | {
71
+ apiVersion: CapabilityProbeVersion
72
+ status: typeof USAGE_STATUS.SUPPORTED
73
+ data: ExactProviderUsageData
74
+ }
75
+ | {
76
+ apiVersion: CapabilityProbeVersion
77
+ status: typeof USAGE_STATUS.UNSUPPORTED
78
+ reason: CapabilityProbeReason
79
+ }
80
+
81
+ type UnknownRecord = Record<string, unknown>
82
+
83
+ function isRecord(value: unknown): value is UnknownRecord {
84
+ return typeof value === 'object' && value !== null && !Array.isArray(value)
85
+ }
86
+
87
+ function isNonEmptyString(value: unknown): value is string {
88
+ return typeof value === 'string' && value.trim().length > 0
89
+ }
90
+
91
+ function isFiniteNonNegativeNumber(value: unknown): value is number {
92
+ return typeof value === 'number' && Number.isFinite(value) && value >= 0
93
+ }
94
+
95
+ function hasOnlyKeys(value: UnknownRecord, allowed: readonly string[]): boolean {
96
+ return Object.keys(value).every((key) => allowed.includes(key))
97
+ }
98
+
99
+ function unsupported(
100
+ apiVersion: CapabilityProbeVersion,
101
+ reason: CapabilityProbeReason,
102
+ ): ProviderCapabilityResult {
103
+ return { apiVersion, status: USAGE_STATUS.UNSUPPORTED, reason }
104
+ }
105
+
106
+ /**
107
+ * Validate the complete exact payload without coercion or estimation.
108
+ *
109
+ * This is intentionally shared only as a validator. V1 and V2 entry points
110
+ * below remain separate capability probes and do not fall back to one another.
111
+ */
112
+ function probeExactData(
113
+ apiVersion: CapabilityProbeVersion,
114
+ input: unknown,
115
+ ): ProviderCapabilityResult {
116
+ if (!isRecord(input)) return unsupported(apiVersion, 'MISSING_SESSION_ID')
117
+
118
+ if (!isNonEmptyString(input.sessionId)) {
119
+ return unsupported(apiVersion, 'MISSING_SESSION_ID')
120
+ }
121
+ if (!isNonEmptyString(input.providerId)) {
122
+ return unsupported(apiVersion, 'MISSING_PROVIDER_ID')
123
+ }
124
+ if (!isNonEmptyString(input.modelId)) {
125
+ return unsupported(apiVersion, 'MISSING_MODEL_ID')
126
+ }
127
+ if (!isRecord(input.usage)) return unsupported(apiVersion, 'MISSING_USAGE')
128
+ if (!isRecord(input.limit)) return unsupported(apiVersion, 'MISSING_LIMIT')
129
+ if (!hasOnlyKeys(input.usage, ['inputTokens', 'outputTokens'])) {
130
+ return unsupported(apiVersion, 'INVALID_USAGE')
131
+ }
132
+ if (!hasOnlyKeys(input.limit, ['contextTokens'])) {
133
+ return unsupported(apiVersion, 'INVALID_LIMIT')
134
+ }
135
+
136
+ const usage = input.usage
137
+ if (!isFiniteNonNegativeNumber(usage.inputTokens)) {
138
+ return unsupported(apiVersion, 'INVALID_USAGE')
139
+ }
140
+ if (!isFiniteNonNegativeNumber(usage.outputTokens)) {
141
+ return unsupported(apiVersion, 'INVALID_USAGE')
142
+ }
143
+
144
+ const limit = input.limit
145
+ if (!isFiniteNonNegativeNumber(limit.contextTokens)) {
146
+ return unsupported(apiVersion, 'INVALID_LIMIT')
147
+ }
148
+
149
+ let resetAt: number | undefined
150
+ if ('reset' in input) return unsupported(apiVersion, 'INVALID_RESET')
151
+ if ('resetAt' in input) {
152
+ const value = input.resetAt
153
+ if (!isFiniteNonNegativeNumber(value)) return unsupported(apiVersion, 'INVALID_RESET')
154
+ resetAt = value
155
+ }
156
+
157
+ const data: ExactProviderUsageData = {
158
+ sessionId: input.sessionId,
159
+ providerId: input.providerId,
160
+ modelId: input.modelId,
161
+ usage: {
162
+ inputTokens: usage.inputTokens,
163
+ outputTokens: usage.outputTokens,
164
+ },
165
+ limit: { contextTokens: limit.contextTokens },
166
+ }
167
+ if (resetAt !== undefined) data.resetAt = resetAt
168
+
169
+ return { apiVersion, status: USAGE_STATUS.SUPPORTED, data }
170
+ }
171
+
172
+ /**
173
+ * Probe the V1 exact payload shape supplied by a caller.
174
+ *
175
+ * V1 does not inherit V2 capability, credentials, cache, or lifecycle data.
176
+ * The caller must pass the exact payload after its host-specific extraction.
177
+ */
178
+ export function probeV1Capability(input: unknown): ProviderCapabilityResult {
179
+ return probeExactData('v1', input)
180
+ }
181
+
182
+ /**
183
+ * Probe the V2 exact payload shape supplied by a caller.
184
+ *
185
+ * V2 is intentionally independent from V1: no V1 event, retry, cache, or
186
+ * fallback value is consulted when the exact payload is absent.
187
+ */
188
+ export function probeV2Capability(input: unknown): ProviderCapabilityResult {
189
+ return probeExactData('v2', input)
190
+ }
191
+
192
+ /** Validate one complete stream event using the same exact data contract. */
193
+ export function validateProviderUsageEvent(input: unknown): ProviderUsageEvent | null {
194
+ if (!isRecord(input)) return null
195
+ if (!isNonEmptyString(input.eventId) || !isNonEmptyString(input.partId)) return null
196
+
197
+ const data = probeExactData('v1', input)
198
+ if (data.status !== USAGE_STATUS.SUPPORTED) return null
199
+
200
+ let sequence: number | undefined
201
+ if ('sequence' in input) {
202
+ const value = input.sequence
203
+ if (!isFiniteNonNegativeNumber(value) || !Number.isInteger(value)) return null
204
+ sequence = value
205
+ }
206
+ let observedAt: number | undefined
207
+ if ('observedAt' in input) {
208
+ const value = input.observedAt
209
+ if (!isFiniteNonNegativeNumber(value)) return null
210
+ observedAt = value
211
+ }
212
+
213
+ const event: ProviderUsageEvent = {
214
+ ...data.data,
215
+ eventId: input.eventId,
216
+ partId: input.partId,
217
+ }
218
+ if (sequence !== undefined) event.sequence = sequence
219
+ if (observedAt !== undefined) event.observedAt = observedAt
220
+ return event
221
+ }
@@ -0,0 +1,182 @@
1
+ /**
2
+ * Pure provider stream usage tracker.
3
+ *
4
+ * The tracker stores only complete exact provider snapshots. It has no timers,
5
+ * polling, persistence, TUI dependency, provider client, credential fallback,
6
+ * token/cost estimation, or compaction/FSM integration.
7
+ *
8
+ * @module provider-stream-tracker
9
+ */
10
+
11
+ import {
12
+ type ProviderUsageEvent,
13
+ type ProviderUsageSnapshot,
14
+ USAGE_STATUS,
15
+ type UsageStatus,
16
+ validateProviderUsageEvent,
17
+ } from './provider-capabilities.ts'
18
+
19
+ export type TrackerUpdateReason = 'PUBLISHED' | 'DUPLICATE' | 'STALE' | 'OUT_OF_SCOPE' | 'INVALID'
20
+
21
+ export interface ProviderStreamTrackerOptions {
22
+ /** Optional exact stream scope. Events from another stream are unsupported. */
23
+ sessionId?: string
24
+ providerId?: string
25
+ modelId?: string
26
+ /** Injectable sink called only after a complete snapshot is stored. */
27
+ onSnapshot?: (snapshot: ProviderUsageSnapshot) => void
28
+ }
29
+
30
+ export interface ProviderStreamTrackerResult {
31
+ status: UsageStatus
32
+ published: boolean
33
+ reason: TrackerUpdateReason
34
+ snapshot?: ProviderUsageSnapshot
35
+ }
36
+
37
+ export interface ProviderStreamTracker {
38
+ /** Ingest one raw event without coercion or heuristic recovery. */
39
+ ingest(input: unknown): ProviderStreamTrackerResult
40
+ /** Read one complete snapshot for an isolated session/provider/model stream. */
41
+ getSnapshot(
42
+ sessionId: string,
43
+ providerId: string,
44
+ modelId: string,
45
+ ): ProviderUsageSnapshot | undefined
46
+ /** Read all complete snapshots, each isolated by stream identity. */
47
+ snapshots(): ProviderUsageSnapshot[]
48
+ /** Remove all in-memory stream state. */
49
+ clear(): void
50
+ }
51
+
52
+ interface StreamState {
53
+ snapshot: ProviderUsageSnapshot
54
+ eventIds: Set<string>
55
+ partIds: Set<string>
56
+ orderKind?: 'sequence' | 'observedAt'
57
+ orderValue?: number
58
+ }
59
+
60
+ function streamKey(sessionId: string, providerId: string, modelId: string): string {
61
+ return JSON.stringify([sessionId, providerId, modelId])
62
+ }
63
+
64
+ function copySnapshot(snapshot: ProviderUsageSnapshot): ProviderUsageSnapshot {
65
+ const copy: ProviderUsageSnapshot = {
66
+ eventId: snapshot.eventId,
67
+ partId: snapshot.partId,
68
+ sessionId: snapshot.sessionId,
69
+ providerId: snapshot.providerId,
70
+ modelId: snapshot.modelId,
71
+ usage: { ...snapshot.usage },
72
+ limit: { ...snapshot.limit },
73
+ }
74
+ if (snapshot.resetAt !== undefined) copy.resetAt = snapshot.resetAt
75
+ if (snapshot.sequence !== undefined) copy.sequence = snapshot.sequence
76
+ if (snapshot.observedAt !== undefined) copy.observedAt = snapshot.observedAt
77
+ return copy
78
+ }
79
+
80
+ function result(
81
+ status: UsageStatus,
82
+ reason: TrackerUpdateReason,
83
+ snapshot?: ProviderUsageSnapshot,
84
+ ): ProviderStreamTrackerResult {
85
+ return snapshot === undefined
86
+ ? { status, published: false, reason }
87
+ : { status, published: true, reason, snapshot: copySnapshot(snapshot) }
88
+ }
89
+
90
+ function isInScope(event: ProviderUsageEvent, options: ProviderStreamTrackerOptions): boolean {
91
+ return (
92
+ (options.sessionId === undefined || options.sessionId === event.sessionId) &&
93
+ (options.providerId === undefined || options.providerId === event.providerId) &&
94
+ (options.modelId === undefined || options.modelId === event.modelId)
95
+ )
96
+ }
97
+
98
+ function isStale(event: ProviderUsageEvent, state: StreamState): boolean {
99
+ if (event.sequence !== undefined) {
100
+ if (state.orderKind === 'sequence' && state.orderValue !== undefined) {
101
+ return event.sequence <= state.orderValue
102
+ }
103
+ return false
104
+ }
105
+
106
+ if (event.observedAt !== undefined) {
107
+ if (state.orderKind === 'observedAt' && state.orderValue !== undefined) {
108
+ return event.observedAt <= state.orderValue
109
+ }
110
+ return state.orderKind === 'sequence'
111
+ }
112
+
113
+ return state.orderKind !== undefined
114
+ }
115
+
116
+ function updateOrder(event: ProviderUsageEvent, state: StreamState): void {
117
+ if (event.sequence !== undefined) {
118
+ state.orderKind = 'sequence'
119
+ state.orderValue = event.sequence
120
+ } else if (event.observedAt !== undefined) {
121
+ state.orderKind = 'observedAt'
122
+ state.orderValue = event.observedAt
123
+ }
124
+ }
125
+
126
+ /**
127
+ * Create an in-memory tracker. With no scope options, multiple streams are
128
+ * tracked independently by their session/provider/model identity.
129
+ */
130
+ export function createProviderStreamTracker(
131
+ options: ProviderStreamTrackerOptions = {},
132
+ ): ProviderStreamTracker {
133
+ const streams = new Map<string, StreamState>()
134
+
135
+ return {
136
+ ingest(input: unknown): ProviderStreamTrackerResult {
137
+ const event = validateProviderUsageEvent(input)
138
+ if (event === null) return result(USAGE_STATUS.UNSUPPORTED, 'INVALID')
139
+ if (!isInScope(event, options)) return result(USAGE_STATUS.UNSUPPORTED, 'OUT_OF_SCOPE')
140
+
141
+ const key = streamKey(event.sessionId, event.providerId, event.modelId)
142
+ const state = streams.get(key)
143
+ if (state?.eventIds.has(event.eventId) || state?.partIds.has(event.partId)) {
144
+ return result(USAGE_STATUS.SUPPORTED, 'DUPLICATE')
145
+ }
146
+
147
+ if (state !== undefined && isStale(event, state)) {
148
+ state.eventIds.add(event.eventId)
149
+ state.partIds.add(event.partId)
150
+ return result(USAGE_STATUS.SUPPORTED, 'STALE')
151
+ }
152
+
153
+ const nextState: StreamState = state ?? {
154
+ snapshot: event,
155
+ eventIds: new Set<string>(),
156
+ partIds: new Set<string>(),
157
+ }
158
+ nextState.snapshot = event
159
+ nextState.eventIds.add(event.eventId)
160
+ nextState.partIds.add(event.partId)
161
+ updateOrder(event, nextState)
162
+ streams.set(key, nextState)
163
+
164
+ const snapshot = copySnapshot(event)
165
+ options.onSnapshot?.(snapshot)
166
+ return result(USAGE_STATUS.SUPPORTED, 'PUBLISHED', snapshot)
167
+ },
168
+
169
+ getSnapshot(sessionId: string, providerId: string, modelId: string) {
170
+ const state = streams.get(streamKey(sessionId, providerId, modelId))
171
+ return state === undefined ? undefined : copySnapshot(state.snapshot)
172
+ },
173
+
174
+ snapshots() {
175
+ return [...streams.values()].map((state) => copySnapshot(state.snapshot))
176
+ },
177
+
178
+ clear() {
179
+ streams.clear()
180
+ },
181
+ }
182
+ }