pantheon-opencode 1.5.0-beta.14 → 1.5.0-beta.16
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.
- package/AGENTS.md +14 -63
- package/CHANGELOG.md +65 -0
- package/README.md +1 -2
- package/README.pt-BR.md +1 -1
- package/commands/pantheon.md +1 -35
- package/package.json +1 -1
- package/plugin.json +1 -1
- package/pyproject.toml +2 -5
- package/src/agents/zeus.md +4 -26
- package/src/instructions/memory-protocol.instructions.md +6 -3
- package/src/instructions/zeus-anti-stall.instructions.md +2 -4
- package/src/instructions/zeus-council-synthesis.instructions.md +3 -28
- package/src/instructions/zeus-timeout-retry.instructions.md +3 -28
- package/src/mcp/toon_codec.py +2 -4
- package/src/pantheon/background-job-board.ts +4 -4
- package/src/pantheon/delegate-manager.ts +95 -10
- package/src/pantheon/delegation-classifier.ts +3 -4
- package/src/pantheon/delegation-finalize.ts +69 -7
- package/src/pantheon/delegation.ts +16 -4
- package/src/pantheon/presets.d.mts +0 -20
- package/src/pantheon/presets.mjs +0 -64
- package/src/plugin-v2.ts +4 -6
- package/src/plugin.ts +6 -6
- package/src/plugins/pantheon-hooks.ts +1 -1
- package/src/plugins/tui/dist/tui.d.ts +43 -36
- package/src/plugins/tui/dist/tui.d.ts.map +1 -1
- package/src/plugins/tui/dist/tui.js +102 -38
- package/src/plugins/tui/package-lock.json +2 -2
- package/src/plugins/tui/package.json +1 -1
- package/src/plugins/tui/src/index.tsx +168 -67
- package/src/routing.yml +0 -16
- package/scripts/dev-tui.sh +0 -48
- package/scripts/generate-preset-docs.mjs +0 -300
- package/scripts/generate-prompts.sh +0 -222
- package/scripts/hash_verify.py +0 -192
- package/scripts/install/component.mjs +0 -24
- package/scripts/install/migrate-config.mjs +0 -302
- package/scripts/test-context-compression.sh +0 -169
- package/src/pantheon/auto-wake.ts +0 -85
- package/src/pantheon/chat-reminders.ts +0 -9
- package/src/pantheon/comment-checker.ts +0 -48
- package/src/pantheon/compaction-assert.ts +0 -160
- package/src/pantheon/compaction-handoff.ts +0 -304
- package/src/pantheon/cost-tracker.ts +0 -108
- package/src/pantheon/dispatch-guard.ts +0 -109
- package/src/pantheon/permission-eval.ts +0 -284
- package/src/pantheon/preemptive-compact.ts +0 -76
- package/src/pantheon/provider-capabilities.ts +0 -221
- package/src/pantheon/provider-stream-tracker.ts +0 -182
- package/src/pantheon/retry-policy.ts +0 -212
- package/src/pantheon/token-opt.ts +0 -689
- package/src/pantheon/toon-codec.ts +0 -481
- package/src/pantheon/v2-events.ts +0 -148
- package/src/pantheon/v2-hooks.ts +0 -287
- package/src/pantheon/v2-tool-definitions.ts +0 -331
- package/src/pantheon/zeus-delegate-with-retry.ts +0 -406
|
@@ -1,221 +0,0 @@
|
|
|
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
|
-
}
|
|
@@ -1,182 +0,0 @@
|
|
|
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
|
-
}
|
|
@@ -1,212 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* R1 — Per-error-type retry + provider cooldown (LiteLLM pattern).
|
|
3
|
-
*
|
|
4
|
-
* Pure library (no SDK, no I/O). When a delegation fails, the error is
|
|
5
|
-
* classified (auth | rate_limit | timeout | other), the per-error-type
|
|
6
|
-
* retry policy from routing.yml `retry_policy` is applied with exponential
|
|
7
|
-
* backoff, and provider cooldown state (routing.yml `cooldown`) is tracked
|
|
8
|
-
* in-memory: after `allowed_fails` consecutive failures a provider is
|
|
9
|
-
* skipped for `cooldown_time_seconds`; a success resets the counter.
|
|
10
|
-
*
|
|
11
|
-
* YAGNI: no usage-based routing, no Redis — a simple in-memory cooldown
|
|
12
|
-
* map plus config.
|
|
13
|
-
*
|
|
14
|
-
* @module retry-policy
|
|
15
|
-
*/
|
|
16
|
-
|
|
17
|
-
// ─── Types ─────────────────────────────────────────────────────────────
|
|
18
|
-
|
|
19
|
-
/** Error classes a delegation failure can be classified into. */
|
|
20
|
-
export type ErrorType = 'auth' | 'rate_limit' | 'timeout' | 'other'
|
|
21
|
-
|
|
22
|
-
/** Max retries per error class (routing.yml `retry_policy`). */
|
|
23
|
-
export interface RetryPolicy {
|
|
24
|
-
auth: number
|
|
25
|
-
rate_limit: number
|
|
26
|
-
timeout: number
|
|
27
|
-
other: number
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
/** Provider cooldown config (routing.yml `cooldown`). */
|
|
31
|
-
export interface CooldownConfig {
|
|
32
|
-
/** Consecutive failures before the provider is skipped. */
|
|
33
|
-
allowedFails: number
|
|
34
|
-
/** How long the provider stays skipped, in seconds. */
|
|
35
|
-
cooldownTimeSeconds: number
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
/** Defaults matching routing.yml `retry_policy`. */
|
|
39
|
-
export const DEFAULT_RETRY_POLICY: RetryPolicy = {
|
|
40
|
-
auth: 0,
|
|
41
|
-
rate_limit: 3,
|
|
42
|
-
timeout: 2,
|
|
43
|
-
other: 1,
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
/** Defaults matching routing.yml `cooldown`. */
|
|
47
|
-
export const DEFAULT_COOLDOWN: CooldownConfig = {
|
|
48
|
-
allowedFails: 3,
|
|
49
|
-
cooldownTimeSeconds: 60,
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
// ─── Error classification ──────────────────────────────────────────────
|
|
53
|
-
|
|
54
|
-
const AUTH_RE = /(auth|unauthorized|401|403|api[ _-]?key|invalid credential|permission denied)/i
|
|
55
|
-
const RATE_LIMIT_RE = /(rate[ _-]?limit|429|too many requests|quota|throttl)/i
|
|
56
|
-
const TIMEOUT_RE = /(timeout|timed out|deadline|504|408|etimedout)/i
|
|
57
|
-
|
|
58
|
-
/**
|
|
59
|
-
* Classify a thrown error into one of the four retry-policy buckets.
|
|
60
|
-
* Message-based matching (LiteLLM pattern); unknown errors → 'other'.
|
|
61
|
-
*/
|
|
62
|
-
export function classifyError(err: unknown): ErrorType {
|
|
63
|
-
const message = err instanceof Error ? err.message : String(err ?? '')
|
|
64
|
-
if (AUTH_RE.test(message)) return 'auth'
|
|
65
|
-
if (RATE_LIMIT_RE.test(message)) return 'rate_limit'
|
|
66
|
-
if (TIMEOUT_RE.test(message)) return 'timeout'
|
|
67
|
-
return 'other'
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
// ─── Exponential backoff ───────────────────────────────────────────────
|
|
71
|
-
|
|
72
|
-
/**
|
|
73
|
-
* Exponential backoff delay for a retry attempt (0-based): base * 2^attempt,
|
|
74
|
-
* capped at maxMs. attempt 0 → base, attempt 1 → 2x base, etc.
|
|
75
|
-
*/
|
|
76
|
-
export function computeBackoffMs(attempt: number, baseMs = 1000, maxMs = 30_000): number {
|
|
77
|
-
const delay = baseMs * 2 ** attempt
|
|
78
|
-
return Math.min(delay, maxMs)
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
// ─── Provider cooldown tracker ─────────────────────────────────────────
|
|
82
|
-
|
|
83
|
-
interface CooldownEntry {
|
|
84
|
-
fails: number
|
|
85
|
-
cooldownUntil: number
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
/**
|
|
89
|
-
* In-memory provider cooldown state. After `allowedFails` consecutive
|
|
90
|
-
* failures a provider is skipped until `cooldownUntil`; a success resets
|
|
91
|
-
* the counter. The clock is injectable for tests.
|
|
92
|
-
*/
|
|
93
|
-
export class ProviderCooldownTracker {
|
|
94
|
-
private readonly state = new Map<string, CooldownEntry>()
|
|
95
|
-
readonly config: CooldownConfig
|
|
96
|
-
|
|
97
|
-
constructor(config: CooldownConfig = DEFAULT_COOLDOWN) {
|
|
98
|
-
this.config = config
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
/** Record one failure for a provider; trips cooldown at allowedFails. */
|
|
102
|
-
recordFailure(provider: string): void {
|
|
103
|
-
const entry = this.state.get(provider) ?? { fails: 0, cooldownUntil: 0 }
|
|
104
|
-
entry.fails += 1
|
|
105
|
-
if (entry.fails >= this.config.allowedFails) {
|
|
106
|
-
entry.cooldownUntil = Date.now() + this.config.cooldownTimeSeconds * 1000
|
|
107
|
-
}
|
|
108
|
-
this.state.set(provider, entry)
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
/** Record a success — resets the failure counter (no spurious cooldown). */
|
|
112
|
-
recordSuccess(provider: string): void {
|
|
113
|
-
this.state.delete(provider)
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
/**
|
|
117
|
-
* Whether the provider is currently skipped. Expired cooldowns are
|
|
118
|
-
* cleared; entries that never tripped (cooldownUntil 0) are kept so the
|
|
119
|
-
* consecutive-failure counter survives the check.
|
|
120
|
-
*/
|
|
121
|
-
isInCooldown(provider: string, now = Date.now()): boolean {
|
|
122
|
-
const entry = this.state.get(provider)
|
|
123
|
-
if (entry === undefined) return false
|
|
124
|
-
if (entry.cooldownUntil > now) return true
|
|
125
|
-
if (entry.cooldownUntil > 0) this.state.delete(provider)
|
|
126
|
-
return false
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
/** Remaining cooldown time in ms (0 when not in cooldown). */
|
|
130
|
-
remainingCooldownMs(provider: string, now = Date.now()): number {
|
|
131
|
-
const entry = this.state.get(provider)
|
|
132
|
-
if (entry === undefined) return 0
|
|
133
|
-
return Math.max(0, entry.cooldownUntil - now)
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
/** Current consecutive failure count for a provider. */
|
|
137
|
-
failCount(provider: string): number {
|
|
138
|
-
return this.state.get(provider)?.fails ?? 0
|
|
139
|
-
}
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
// ─── Retry policy engine ───────────────────────────────────────────────
|
|
143
|
-
|
|
144
|
-
export type RetryDecisionReason = 'policy' | 'cooldown'
|
|
145
|
-
|
|
146
|
-
export interface RetryDecision {
|
|
147
|
-
shouldRetry: boolean
|
|
148
|
-
/** Retries still available for this error class (0 when exhausted). */
|
|
149
|
-
retriesRemaining: number
|
|
150
|
-
/** Backoff delay to wait before the next attempt (ms). */
|
|
151
|
-
delayMs: number
|
|
152
|
-
/** Why the decision was made: policy budget or provider cooldown. */
|
|
153
|
-
reason: RetryDecisionReason
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
/**
|
|
157
|
-
* Combines error classification, the per-error-type retry policy, and the
|
|
158
|
-
* provider cooldown tracker into one decision point. Enforcement: when a
|
|
159
|
-
* delegation fails, call `decide(provider, err, attemptsUsed)`; when the
|
|
160
|
-
* result is content, call `recordSuccess`; when the budget is exhausted,
|
|
161
|
-
* call `recordFailure` so the cooldown can trip.
|
|
162
|
-
*/
|
|
163
|
-
export class RetryPolicyEngine {
|
|
164
|
-
private readonly cooldown: ProviderCooldownTracker
|
|
165
|
-
readonly policy: RetryPolicy
|
|
166
|
-
readonly backoffBaseMs: number
|
|
167
|
-
|
|
168
|
-
constructor(
|
|
169
|
-
policy: RetryPolicy = DEFAULT_RETRY_POLICY,
|
|
170
|
-
cooldown?: ProviderCooldownTracker,
|
|
171
|
-
backoffBaseMs = 1000,
|
|
172
|
-
) {
|
|
173
|
-
this.policy = policy
|
|
174
|
-
this.backoffBaseMs = backoffBaseMs
|
|
175
|
-
this.cooldown = cooldown ?? new ProviderCooldownTracker()
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
/**
|
|
179
|
-
* Decide whether a failed dispatch should be retried.
|
|
180
|
-
*
|
|
181
|
-
* @param provider provider name (cooldown key, e.g. "opencode")
|
|
182
|
-
* @param err the failure (classified into auth/rate_limit/timeout/other)
|
|
183
|
-
* @param attemptsUsed retries already consumed for this dispatch
|
|
184
|
-
* @param now injectable clock (tests)
|
|
185
|
-
*/
|
|
186
|
-
decide(provider: string, err: unknown, attemptsUsed: number, now = Date.now()): RetryDecision {
|
|
187
|
-
if (this.cooldown.isInCooldown(provider, now)) {
|
|
188
|
-
return { shouldRetry: false, retriesRemaining: 0, delayMs: 0, reason: 'cooldown' }
|
|
189
|
-
}
|
|
190
|
-
const type = classifyError(err)
|
|
191
|
-
const retriesRemaining = Math.max(0, this.policy[type] - attemptsUsed)
|
|
192
|
-
if (retriesRemaining <= 0) {
|
|
193
|
-
return { shouldRetry: false, retriesRemaining: 0, delayMs: 0, reason: 'policy' }
|
|
194
|
-
}
|
|
195
|
-
return {
|
|
196
|
-
shouldRetry: true,
|
|
197
|
-
retriesRemaining,
|
|
198
|
-
delayMs: computeBackoffMs(attemptsUsed, this.backoffBaseMs),
|
|
199
|
-
reason: 'policy',
|
|
200
|
-
}
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
/** Record a failure (feeds the cooldown tracker). */
|
|
204
|
-
recordFailure(provider: string, _err: unknown): void {
|
|
205
|
-
this.cooldown.recordFailure(provider)
|
|
206
|
-
}
|
|
207
|
-
|
|
208
|
-
/** Record a success (resets the provider's failure counter). */
|
|
209
|
-
recordSuccess(provider: string): void {
|
|
210
|
-
this.cooldown.recordSuccess(provider)
|
|
211
|
-
}
|
|
212
|
-
}
|