pantheon-opencode 1.5.0-beta.14 → 1.5.0-beta.15

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.
Files changed (42) hide show
  1. package/CHANGELOG.md +38 -0
  2. package/package.json +1 -1
  3. package/plugin.json +1 -1
  4. package/pyproject.toml +2 -5
  5. package/src/agents/zeus.md +4 -26
  6. package/src/pantheon/background-job-board.ts +4 -4
  7. package/src/pantheon/delegate-manager.ts +95 -10
  8. package/src/pantheon/delegation-classifier.ts +3 -4
  9. package/src/pantheon/delegation-finalize.ts +69 -7
  10. package/src/pantheon/delegation.ts +16 -4
  11. package/src/pantheon/presets.d.mts +0 -20
  12. package/src/pantheon/presets.mjs +0 -64
  13. package/src/pantheon/toon-codec.ts +1 -1
  14. package/src/plugin-v2.ts +4 -6
  15. package/src/plugin.ts +6 -6
  16. package/src/plugins/pantheon-hooks.ts +1 -1
  17. package/src/plugins/tui/dist/tui.d.ts +43 -36
  18. package/src/plugins/tui/dist/tui.d.ts.map +1 -1
  19. package/src/plugins/tui/dist/tui.js +102 -38
  20. package/src/plugins/tui/package-lock.json +2 -2
  21. package/src/plugins/tui/package.json +1 -1
  22. package/src/plugins/tui/src/index.tsx +168 -67
  23. package/src/routing.yml +0 -16
  24. package/scripts/hash_verify.py +0 -192
  25. package/scripts/test-context-compression.sh +0 -169
  26. package/src/pantheon/auto-wake.ts +0 -85
  27. package/src/pantheon/chat-reminders.ts +0 -9
  28. package/src/pantheon/comment-checker.ts +0 -48
  29. package/src/pantheon/compaction-assert.ts +0 -160
  30. package/src/pantheon/compaction-handoff.ts +0 -304
  31. package/src/pantheon/cost-tracker.ts +0 -108
  32. package/src/pantheon/dispatch-guard.ts +0 -109
  33. package/src/pantheon/permission-eval.ts +0 -284
  34. package/src/pantheon/preemptive-compact.ts +0 -76
  35. package/src/pantheon/provider-capabilities.ts +0 -221
  36. package/src/pantheon/provider-stream-tracker.ts +0 -182
  37. package/src/pantheon/retry-policy.ts +0 -212
  38. package/src/pantheon/token-opt.ts +0 -689
  39. package/src/pantheon/v2-events.ts +0 -148
  40. package/src/pantheon/v2-hooks.ts +0 -287
  41. package/src/pantheon/v2-tool-definitions.ts +0 -331
  42. package/src/pantheon/zeus-delegate-with-retry.ts +0 -406
@@ -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
- }