pantheon-opencode 1.3.5-beta.60.efa533f → 1.3.6
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/README.md +38 -6
- package/package.json +4 -3
- package/scripts/_pantheon_paths.py +0 -1
- package/scripts/build-agents-md.mjs +4 -2
- package/scripts/code_mode_server.py +307 -21
- package/scripts/doctor.mjs +58 -15
- package/scripts/hooks/format-multi-language.sh +6 -3
- package/scripts/hooks/log-session-start.sh +6 -0
- package/scripts/hooks/on-subagent-delegation-start.sh +22 -6
- package/scripts/hooks/validate-post-conditions.sh +1 -1
- package/scripts/hooks/validate-tool-safety.sh +5 -2
- package/scripts/install/agents-md.mjs +3 -1
- package/scripts/install/cli-ui.mjs +24 -4
- package/scripts/install/interactive.mjs +18 -7
- package/scripts/install/migrate-config.mjs +13 -19
- package/scripts/install/migrate.mjs +8 -5
- package/scripts/install/model-picker.mjs +4 -1
- package/scripts/install/opencode.mjs +8 -2
- package/scripts/install/shared.mjs +8 -4
- package/scripts/install/state.mjs +9 -6
- package/scripts/mcp_persistence_server.py +23 -22
- package/scripts/mcp_resources_server.py +60 -1
- package/scripts/memory_mcp_server.py +80 -4
- package/scripts/postinstall.mjs +27 -25
- package/scripts/prune.mjs +1 -1
- package/scripts/redaction-gate.mjs +3 -1
- package/scripts/uninstall.mjs +0 -2
- package/scripts/validation-policy.mjs +6 -1
- package/scripts/version-check.mjs +9 -4
- package/src/agents/aphrodite.md +1 -1
- package/src/agents/apollo.md +3 -0
- package/src/agents/demeter.md +1 -1
- package/src/agents/gaia.md +3 -0
- package/src/agents/hephaestus.md +1 -1
- package/src/agents/hermes.md +1 -1
- package/src/agents/themis.md +4 -0
- package/src/agents/zeus.md +5 -1
- package/src/mcp/code_mode_server.py +304 -18
- package/src/mcp/eval_store.py +233 -0
- package/src/mcp/mcp_persistence_server.py +23 -22
- package/src/mcp/mcp_resources_server.py +58 -0
- package/src/mcp/memory_mcp_server.py +80 -3
- package/src/pantheon/auto-wake.ts +2 -4
- package/src/pantheon/background-job-board.ts +17 -4
- package/src/pantheon/comment-checker.ts +48 -0
- package/src/pantheon/delegation-classifier.ts +307 -0
- package/src/pantheon/delegation-enforce.ts +67 -3
- package/src/pantheon/delegation-finalize.ts +44 -4
- package/src/pantheon/delegation.ts +413 -30
- package/src/pantheon/dispatch-guard.ts +2 -0
- package/src/pantheon/file-persistence.ts +7 -3
- package/src/pantheon/goal-loop.ts +13 -2
- package/src/pantheon/permission-eval.ts +284 -0
- package/src/pantheon/permission-globs.ts +86 -0
- package/src/pantheon/presets.d.mts +41 -0
- package/src/pantheon/presets.mjs +135 -0
- package/src/pantheon/retry-policy.ts +212 -0
- package/src/pantheon/session-guard.ts +48 -0
- package/src/pantheon/step-cap.ts +110 -0
- package/src/pantheon/todo-enforcer.ts +31 -5
- package/src/pantheon/todo-preserve.ts +7 -1
- package/src/pantheon/vision.ts +7 -5
- package/src/pantheon/zeus-delegate-with-retry.ts +401 -0
- package/src/plugin.ts +72 -13
- package/src/plugins/hook-runner.ts +1 -1
- package/src/plugins/pantheon-hooks.ts +1 -1
- package/src/plugins/tui/dist/tui.d.ts +1 -1
- package/src/plugins/tui/dist/tui.d.ts.map +1 -1
- package/src/plugins/tui/dist/tui.js +36 -14
- package/src/plugins/tui/dist/tui.tsx +71 -16
- package/src/plugins/tui/package.json +1 -1
- package/src/plugins/tui/src/index.tsx +71 -16
- package/src/plugins/tui/tsdown.config.ts +1 -1
- package/src/routing.yml +47 -0
- package/src/agents/README.md +0 -26
|
@@ -0,0 +1,212 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Session Guard — shared server-side validation for session IDs.
|
|
3
|
+
*
|
|
4
|
+
* Opencode session IDs always start with "ses". The TUI previously forwarded
|
|
5
|
+
* the literal template placeholder "{sessionID}" (URL-encoded as "%7BsessionID%7D")
|
|
6
|
+
* into `session.children`, causing ~1.5 errors/s from the 1s safety poll.
|
|
7
|
+
* This module is the single chokepoint: every `client.session.*({path:{id}})`;
|
|
8
|
+
* call site MUST go through `safeSessionPath`/`buildChildrenPath`, which
|
|
9
|
+
* reject placeholders before they reach the server (fail-open: warn + return,
|
|
10
|
+
* never throw, never send the placeholder).
|
|
11
|
+
*
|
|
12
|
+
* Mirrors the TUI's `src/plugins/tui/src/index.tsx` contract verbatim so both
|
|
13
|
+
* sides agree on what "valid" means. The TUI file remains self-contained
|
|
14
|
+
* (dist/tui.tsx is a raw copy, no relative imports), so its local copy is kept
|
|
15
|
+
* in sync — this module is the shared source for `src/pantheon/*` and
|
|
16
|
+
* `src/plugin.ts`.
|
|
17
|
+
*
|
|
18
|
+
* @module session-guard
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Server-aligned session id validity: opencode rejects anything not starting
|
|
23
|
+
* with "ses" (SchemaError). Mirrors that exact contract — nothing stricter,
|
|
24
|
+
* nothing looser — so "{sessionID}", "%7BsessionID%7D", "", "wrk_…" never
|
|
25
|
+
* reach a path.
|
|
26
|
+
*/
|
|
27
|
+
export function isValidSessionId(id: unknown): id is string {
|
|
28
|
+
return typeof id === 'string' && id.startsWith('ses')
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* The single choke point for every `session.*({path:{id}})` call.
|
|
33
|
+
* Returns `{path:{id}}` ONLY for a server-valid id; otherwise null so the
|
|
34
|
+
* caller skips the call entirely (fail-open).
|
|
35
|
+
*/
|
|
36
|
+
export function safeSessionPath(id: unknown): { path: { id: string } } | null {
|
|
37
|
+
if (!isValidSessionId(id)) return null
|
|
38
|
+
return { path: { id } }
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Build the `session.children` path ONLY from a validated session id.
|
|
43
|
+
* Delegates to {@link safeSessionPath} — the single choke point. Returns
|
|
44
|
+
* null for null/invalid ids so the caller skips the fetch.
|
|
45
|
+
*/
|
|
46
|
+
export function buildChildrenPath(id: string | null | undefined): { path: { id: string } } | null {
|
|
47
|
+
return safeSessionPath(id)
|
|
48
|
+
}
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* R4 — Per-agent step caps (max_steps).
|
|
3
|
+
*
|
|
4
|
+
* Pure library. routing.yml `agents.<name>.max_steps` gives each agent a
|
|
5
|
+
* step budget (e.g. explore: 25, reviewer: 25, oracle: 15). When an agent's
|
|
6
|
+
* step count reaches max_steps it is forced to summarize-and-stop: the
|
|
7
|
+
* delegation is either skipped with a capped summary (already at cap) or
|
|
8
|
+
* the prompt is appended with a stop instruction (cap hit mid-dispatch).
|
|
9
|
+
*
|
|
10
|
+
* The tracker is PERMANENT-PER-PROCESS: counters accumulate for the process
|
|
11
|
+
* lifetime and are intentionally NOT reset on successful delegations or
|
|
12
|
+
* session end — a per-session reset would let an agent exceed its process
|
|
13
|
+
* budget over a long session. `reset()` exists as a tested utility for
|
|
14
|
+
* tests and future explicit reset points; no production caller wires it.
|
|
15
|
+
*
|
|
16
|
+
* @module step-cap
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
// ─── Types ─────────────────────────────────────────────────────────────
|
|
20
|
+
|
|
21
|
+
/** Default step budget when routing.yml omits max_steps for an agent. */
|
|
22
|
+
export const DEFAULT_MAX_STEPS = 25
|
|
23
|
+
|
|
24
|
+
// ─── Stop instruction ──────────────────────────────────────────────────
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* The forced summarize-and-stop instruction injected into a prompt when an
|
|
28
|
+
* agent's step count reaches max_steps.
|
|
29
|
+
*/
|
|
30
|
+
export function buildStopInstruction(agent: string, maxSteps: number): string {
|
|
31
|
+
return (
|
|
32
|
+
`\n\n[STEP CAP REACHED — ${agent} at ${maxSteps} steps] ` +
|
|
33
|
+
`You have reached the maximum step budget for this delegation. ` +
|
|
34
|
+
`STOP working immediately and summarize what was accomplished (and ` +
|
|
35
|
+
`what remains, if any) as your final message. Do not start new work.`
|
|
36
|
+
)
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* The text returned when a delegation is skipped because the agent is
|
|
41
|
+
* already at its step cap — marks the delegation as capped and asks for a
|
|
42
|
+
* summary of prior results instead of starting new work.
|
|
43
|
+
*/
|
|
44
|
+
export function cappedSummary(agent: string, maxSteps: number): string {
|
|
45
|
+
return (
|
|
46
|
+
`[STEP CAP REACHED] ${agent} has reached its max_steps budget (${maxSteps}). ` +
|
|
47
|
+
`Delegation skipped — no new work was started. ` +
|
|
48
|
+
`Summarize prior results and stop.`
|
|
49
|
+
)
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// ─── Tracker ───────────────────────────────────────────────────────────
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Per-agent step counters with a max_steps budget. Keyed by lowercase agent
|
|
56
|
+
* name (matching is case-insensitive). Agents without a configured budget
|
|
57
|
+
* are never capped.
|
|
58
|
+
*/
|
|
59
|
+
export class StepCapTracker {
|
|
60
|
+
private readonly steps = new Map<string, number>()
|
|
61
|
+
readonly maxStepsByAgent: Readonly<Record<string, number>>
|
|
62
|
+
|
|
63
|
+
constructor(maxStepsByAgent: Readonly<Record<string, number>> = {}) {
|
|
64
|
+
this.maxStepsByAgent = maxStepsByAgent
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** The configured budget for an agent, or undefined when uncapped. */
|
|
68
|
+
maxStepsFor(agent: string): number | undefined {
|
|
69
|
+
return this.maxStepsByAgent[agent.toLowerCase()]
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** Current step count for an agent. */
|
|
73
|
+
stepCount(agent: string): number {
|
|
74
|
+
return this.steps.get(agent.toLowerCase()) ?? 0
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/** Whether the agent has reached (or exceeded) its max_steps budget. */
|
|
78
|
+
isCapped(agent: string): boolean {
|
|
79
|
+
const max = this.maxStepsFor(agent)
|
|
80
|
+
if (max === undefined) return false
|
|
81
|
+
return this.stepCount(agent) >= max
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Record one step for an agent. Returns the new count and whether this
|
|
86
|
+
* step hit the cap (capped === true exactly when steps >= maxSteps).
|
|
87
|
+
*/
|
|
88
|
+
recordStep(agent: string): {
|
|
89
|
+
steps: number
|
|
90
|
+
capped: boolean
|
|
91
|
+
maxSteps: number | undefined
|
|
92
|
+
} {
|
|
93
|
+
const key = agent.toLowerCase()
|
|
94
|
+
const max = this.maxStepsFor(agent)
|
|
95
|
+
const steps = (this.steps.get(key) ?? 0) + 1
|
|
96
|
+
this.steps.set(key, steps)
|
|
97
|
+
return { steps, capped: max !== undefined && steps >= max, maxSteps: max }
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Reset an agent's step count.
|
|
102
|
+
*
|
|
103
|
+
* Intentionally NOT wired in production: the tracker is permanent-per-
|
|
104
|
+
* process (see module docstring) so agents cannot exceed their process
|
|
105
|
+
* budget via resets. Exists for tests and future explicit reset points.
|
|
106
|
+
*/
|
|
107
|
+
reset(agent: string): void {
|
|
108
|
+
this.steps.delete(agent.toLowerCase())
|
|
109
|
+
}
|
|
110
|
+
}
|
|
@@ -49,6 +49,7 @@
|
|
|
49
49
|
|
|
50
50
|
import type { BackgroundJobBoard } from './background-job-board.ts'
|
|
51
51
|
import { createPantheonLogger } from './logger.ts'
|
|
52
|
+
import { buildChildrenPath, safeSessionPath } from './session-guard.ts'
|
|
52
53
|
|
|
53
54
|
// Silence-by-default TUI policy (pantheon-hooks L42-58): the default warn
|
|
54
55
|
// fallback logs to .pantheon/logs/hooks.log; console echo is opt-in via
|
|
@@ -233,8 +234,13 @@ export class TodoEnforcer {
|
|
|
233
234
|
*/
|
|
234
235
|
async listPendingTodos(sessionID: string): Promise<TodoLike[]> {
|
|
235
236
|
if (!this.options.enabled) return []
|
|
237
|
+
const path = safeSessionPath(sessionID)
|
|
238
|
+
if (!path) {
|
|
239
|
+
this.warn(`todo list skipped: invalid sessionID "${sessionID}"`)
|
|
240
|
+
return []
|
|
241
|
+
}
|
|
236
242
|
try {
|
|
237
|
-
const todos = await this.client.session.todo(
|
|
243
|
+
const todos = await this.client.session.todo(path)
|
|
238
244
|
return todos.filter((todo) => todo.status !== 'completed' && todo.status !== 'cancelled')
|
|
239
245
|
} catch (err: unknown) {
|
|
240
246
|
const reason = err instanceof Error ? err.message : String(err)
|
|
@@ -280,7 +286,12 @@ export class TodoEnforcer {
|
|
|
280
286
|
// still waiting on it, so skip.
|
|
281
287
|
if ((await this.activeChildren(sessionID, now)) > 0) return
|
|
282
288
|
|
|
283
|
-
const
|
|
289
|
+
const path = safeSessionPath(sessionID)
|
|
290
|
+
if (!path) {
|
|
291
|
+
this.warn(`todo check skipped: invalid sessionID "${sessionID}"`)
|
|
292
|
+
return
|
|
293
|
+
}
|
|
294
|
+
const todos = await this.client.session.todo(path)
|
|
284
295
|
const incomplete = todos.filter(
|
|
285
296
|
(todo) => todo.status !== 'completed' && todo.status !== 'cancelled',
|
|
286
297
|
).length
|
|
@@ -328,8 +339,13 @@ export class TodoEnforcer {
|
|
|
328
339
|
// (throwing) injection still applies the cooldown — no hot retry loop.
|
|
329
340
|
this.lastInjectionAt.set(sessionID, now)
|
|
330
341
|
const inherited = await this.inheritAgentOrModel(sessionID)
|
|
342
|
+
const promptPath = safeSessionPath(sessionID)
|
|
343
|
+
if (!promptPath) {
|
|
344
|
+
this.warn(`todo injection skipped: invalid sessionID "${sessionID}"`)
|
|
345
|
+
return
|
|
346
|
+
}
|
|
331
347
|
await this.client.session.promptAsync({
|
|
332
|
-
path:
|
|
348
|
+
path: promptPath.path,
|
|
333
349
|
body: {
|
|
334
350
|
...(inherited.agent !== undefined ? { agent: inherited.agent } : {}),
|
|
335
351
|
...(inherited.model !== undefined ? { model: inherited.model } : {}),
|
|
@@ -368,8 +384,13 @@ export class TodoEnforcer {
|
|
|
368
384
|
* version-sensitive runtimes.
|
|
369
385
|
*/
|
|
370
386
|
private async activeChildren(sessionID: string, now: number): Promise<number> {
|
|
387
|
+
const path = safeSessionPath(sessionID)
|
|
388
|
+
if (!path) {
|
|
389
|
+
this.warn(`children check skipped: invalid sessionID "${sessionID}"`)
|
|
390
|
+
return 0
|
|
391
|
+
}
|
|
371
392
|
try {
|
|
372
|
-
const children = await this.client.session.children(
|
|
393
|
+
const children = await this.client.session.children(path)
|
|
373
394
|
return children.filter((child) => this.isChildActive(child, now)).length
|
|
374
395
|
} catch (err: unknown) {
|
|
375
396
|
const reason = err instanceof Error ? err.message : String(err)
|
|
@@ -398,7 +419,12 @@ export class TodoEnforcer {
|
|
|
398
419
|
* SDK then applies the session default.
|
|
399
420
|
*/
|
|
400
421
|
private async inheritAgentOrModel(sessionID: string): Promise<InheritedContext> {
|
|
401
|
-
const
|
|
422
|
+
const path = safeSessionPath(sessionID)
|
|
423
|
+
if (!path) {
|
|
424
|
+
this.warn(`inherit skipped: invalid sessionID "${sessionID}"`)
|
|
425
|
+
return {}
|
|
426
|
+
}
|
|
427
|
+
const messages = await this.client.session.messages(path)
|
|
402
428
|
for (const msg of [...messages].reverse()) {
|
|
403
429
|
const info = msg.info
|
|
404
430
|
if (info === undefined || info.role !== 'assistant') continue
|
|
@@ -48,6 +48,7 @@
|
|
|
48
48
|
*/
|
|
49
49
|
|
|
50
50
|
import { createPantheonLogger } from './logger.ts'
|
|
51
|
+
import { safeSessionPath } from './session-guard.ts'
|
|
51
52
|
import type { TodoLike } from './todo-enforcer.ts'
|
|
52
53
|
|
|
53
54
|
// Silence-by-default TUI policy (pantheon-hooks L42-58): console echo is
|
|
@@ -173,8 +174,13 @@ export class TodoPreserver {
|
|
|
173
174
|
*/
|
|
174
175
|
async capture(sessionID: string): Promise<void> {
|
|
175
176
|
this.pruneExpired()
|
|
177
|
+
const path = safeSessionPath(sessionID)
|
|
178
|
+
if (!path) {
|
|
179
|
+
this.warn(`todo snapshot capture skipped: invalid sessionID "${sessionID}"`)
|
|
180
|
+
return
|
|
181
|
+
}
|
|
176
182
|
try {
|
|
177
|
-
const todos = await this.client.session.todo(
|
|
183
|
+
const todos = await this.client.session.todo(path)
|
|
178
184
|
if (todos.length === 0) {
|
|
179
185
|
// Nothing to preserve → drop any stale entry so no restore happens.
|
|
180
186
|
this.snapshots.delete(sessionID)
|
package/src/pantheon/vision.ts
CHANGED
|
@@ -7,7 +7,12 @@ import { fileURLToPath } from 'node:url'
|
|
|
7
7
|
import type { Hooks, PluginInput } from '@opencode-ai/plugin'
|
|
8
8
|
import type { FilePart, Part, TextPart, UserMessage } from '@opencode-ai/sdk'
|
|
9
9
|
import type { ResolvedPreset } from './presets.mjs'
|
|
10
|
-
import {
|
|
10
|
+
import {
|
|
11
|
+
CAPABILITY_TABLE,
|
|
12
|
+
hasVision,
|
|
13
|
+
resolveActivePreset,
|
|
14
|
+
visionBrokenOnGateway,
|
|
15
|
+
} from './presets.mjs'
|
|
11
16
|
|
|
12
17
|
// ─── Constants ────────────────────────────────────────────────────────────
|
|
13
18
|
|
|
@@ -136,10 +141,7 @@ export function providerIsTextOnly(providerID: string): boolean {
|
|
|
136
141
|
}
|
|
137
142
|
|
|
138
143
|
/** Return whether OpenCode's current model is known to accept image input. */
|
|
139
|
-
export function modelAcceptsImages(
|
|
140
|
-
model: ModelInfo | undefined,
|
|
141
|
-
providerID?: string,
|
|
142
|
-
): boolean {
|
|
144
|
+
export function modelAcceptsImages(model: ModelInfo | undefined, providerID?: string): boolean {
|
|
143
145
|
if (!model) {
|
|
144
146
|
// Fail-closed for known text-only providers
|
|
145
147
|
if (providerID && providerIsTextOnly(providerID)) return false
|