pantheon-opencode 1.4.3 → 1.5.0-beta.2

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 (99) hide show
  1. package/.pantheon/code-mode/README.md +93 -0
  2. package/.pantheon/code-mode/checkpoint-session.py +7 -0
  3. package/.pantheon/code-mode/checkpoint-session.sh +11 -0
  4. package/.pantheon/code-mode/checkpoint_session.py +481 -0
  5. package/.pantheon/code-mode/compress-inline.py +450 -0
  6. package/.pantheon/code-mode/eval-llm-judge.py +255 -0
  7. package/.pantheon/code-mode/eval-monte-carlo.py +368 -0
  8. package/.pantheon/code-mode/eval-run.py +197 -0
  9. package/.pantheon/code-mode/eval-static.py +321 -0
  10. package/.pantheon/code-mode/example-sync.sh +5 -0
  11. package/.pantheon/code-mode/session-end-save.py +186 -0
  12. package/.pantheon/code-mode/session-end-save.sh +18 -0
  13. package/.pantheon/code-mode/test-allowed.sh +5 -0
  14. package/.pantheon/code-mode/test-auto-continue-checkpoint.sh +179 -0
  15. package/.pantheon/code-mode/test-env.sh +7 -0
  16. package/.pantheon/code-mode/test-prlimit.sh +9 -0
  17. package/.pantheon/code-mode/test-timeout.sh +6 -0
  18. package/README.md +161 -1101
  19. package/README.pt-BR.md +222 -0
  20. package/bin/pantheon-init.mjs +30 -11
  21. package/opencode.json +1 -0
  22. package/package.json +14 -3
  23. package/plugin.json +15 -0
  24. package/scripts/_pantheon_paths.py +48 -7
  25. package/scripts/ci-validate-yaml.py +7 -2
  26. package/scripts/code_mode_server.py +33 -2
  27. package/scripts/cost.mjs +30 -14
  28. package/scripts/doctor.mjs +135 -127
  29. package/scripts/generate-preset-docs.mjs +127 -66
  30. package/scripts/generate-prompts.sh +2 -2
  31. package/scripts/install/cli-ui.mjs +8 -1
  32. package/scripts/install/config-migration.mjs +401 -0
  33. package/scripts/install/health-check.mjs +3 -3
  34. package/scripts/install/model-picker.mjs +72 -23
  35. package/scripts/install/opencode-version.mjs +47 -0
  36. package/scripts/install/opencode.mjs +261 -88
  37. package/scripts/install/plugin.mjs +112 -42
  38. package/scripts/install/shared.mjs +13 -158
  39. package/scripts/install-mcp.mjs +44 -232
  40. package/scripts/mcp_persistence_server.py +382 -16
  41. package/scripts/mcp_resources_server.py +144 -40
  42. package/scripts/memory_mcp_server.py +2 -2
  43. package/scripts/probe-context-rehydrate.mjs +240 -0
  44. package/scripts/probe-pantheon-cost.mjs +304 -0
  45. package/scripts/recover-zenodo-deposition.mjs +146 -0
  46. package/scripts/sync-tui.mjs +32 -7
  47. package/scripts/test-opencode-v1-v2-sandbox.sh +944 -0
  48. package/scripts/uninstall.mjs +298 -954
  49. package/scripts/validate-zenodo-release.mjs +154 -0
  50. package/scripts/zenodo-artifact.mjs +21 -0
  51. package/scripts/zenodo-release-state.mjs +53 -0
  52. package/src/agents/zeus.md +25 -14
  53. package/src/mcp/_pantheon_paths.py +48 -7
  54. package/src/mcp/code_mode_server.py +33 -2
  55. package/src/mcp/eval_store.py +1 -1
  56. package/src/mcp/mcp_persistence_server.py +382 -16
  57. package/src/mcp/mcp_resources_server.py +144 -40
  58. package/src/mcp/pantheon_vision_server.py +1 -1
  59. package/src/mcp/toon_codec.py +491 -0
  60. package/src/pantheon/background-job-board.ts +67 -1
  61. package/src/pantheon/cost-command.ts +60 -39
  62. package/src/pantheon/delegate-manager.ts +669 -0
  63. package/src/pantheon/delegation-classifier.ts +307 -0
  64. package/src/pantheon/delegation-enforce.ts +13 -12
  65. package/src/pantheon/delegation-finalize.ts +374 -0
  66. package/src/pantheon/delegation-notify.ts +212 -0
  67. package/src/pantheon/delegation-toast.ts +73 -0
  68. package/src/pantheon/delegation.ts +1120 -0
  69. package/src/pantheon/goal-loop.ts +2 -2
  70. package/src/pantheon/goal-store.ts +1 -1
  71. package/src/pantheon/hashline/tool.ts +2 -2
  72. package/src/pantheon/model-command.ts +2 -1
  73. package/src/pantheon/permission-eval.ts +284 -0
  74. package/src/pantheon/step-cap.ts +110 -0
  75. package/src/pantheon/token-opt.ts +781 -0
  76. package/src/pantheon/toon-codec.ts +481 -0
  77. package/src/pantheon/tui-version-check.ts +1 -3
  78. package/src/pantheon/v2-bridge.ts +129 -0
  79. package/src/pantheon/v2-events.ts +131 -0
  80. package/src/pantheon/v2-hooks.ts +287 -0
  81. package/src/pantheon/v2-tool-definitions.ts +331 -0
  82. package/src/pantheon/zeus-delegate-with-retry.ts +406 -0
  83. package/src/plugin-v2/index.ts +21 -0
  84. package/src/plugin-v2.ts +824 -0
  85. package/src/plugin.ts +236 -42
  86. package/src/plugins/tui/dist/server.d.ts +1 -1
  87. package/src/plugins/tui/dist/server.d.ts.map +1 -1
  88. package/src/plugins/tui/dist/server.js +1 -1
  89. package/src/plugins/tui/dist/server.js.map +1 -1
  90. package/src/plugins/tui/dist/tui.d.ts +10 -7
  91. package/src/plugins/tui/dist/tui.d.ts.map +1 -1
  92. package/src/plugins/tui/dist/tui.js +11 -8
  93. package/src/plugins/tui/dist/tui.js.map +1 -1
  94. package/src/plugins/tui/dist/tui.tsx +22 -16
  95. package/src/plugins/tui/package-lock.json +51 -51
  96. package/src/plugins/tui/package.json +7 -7
  97. package/src/plugins/tui/src/index.tsx +28 -24
  98. package/src/skills/README.md +0 -2
  99. package/src/pantheon/tool-context.ts +0 -41
@@ -17,11 +17,11 @@
17
17
  import { z } from 'zod'
18
18
 
19
19
  import type { BackgroundJobBoard } from './background-job-board.ts'
20
+ import type { ToolContextLike } from './delegation.ts'
20
21
  import { GoalStore } from './goal-store.ts'
21
22
  import { createPantheonLogger } from './logger.ts'
22
23
  import { safeSessionPath } from './session-guard.ts'
23
24
  import type { TodoEnforcerMessage } from './todo-enforcer.ts'
24
- import type { ToolContextLike } from './tool-context.ts'
25
25
 
26
26
  // Silence-by-default TUI policy (pantheon-hooks L42-58): warn → hooks.log,
27
27
  // console echo opt-in via PANTHEON_HOOKS_LOG=1. `deps.logger` stays for tests.
@@ -103,7 +103,7 @@ const goalUpdateArgs = {
103
103
 
104
104
  const goalGetArgs = {} satisfies z.ZodRawShape
105
105
 
106
- /** One structural goal tool: description + zod args shape + execute. */
106
+ /** One structural goal tool: description + zod args shape + execute (delegation.ts shape). */
107
107
  export type GoalTool<Args extends z.ZodRawShape> = {
108
108
  description: string
109
109
  args: Args
@@ -15,7 +15,7 @@
15
15
  import { mkdir, readFile, rename, writeFile } from 'node:fs/promises'
16
16
  import { join } from 'node:path'
17
17
 
18
- import { assertSafeParentSessionID } from './tool-context.ts'
18
+ import { assertSafeParentSessionID } from './delegation-finalize.ts'
19
19
 
20
20
  export type GoalStatus = 'pending' | 'in_progress' | 'done'
21
21
 
@@ -2,8 +2,8 @@
2
2
  * Hashline edit tool (Wave 2, PR #46) — structural `hashline_edit` tool that
3
3
  * anchors edits to stable sha256 content tags instead of fragile line numbers.
4
4
  *
5
- * Tool shape follows the plugin tool convention (`{description, args, execute}`,
6
- * zod-validated) used across the Pantheon toolset.
5
+ * Tool shape matches the delegation tools (`{description, args, execute}`,
6
+ * zod-validated — same import pattern as delegation.ts).
7
7
  *
8
8
  * Safety model (council-approved):
9
9
  * 1. ALL refs are validated against the ORIGINAL file snapshot BEFORE any
@@ -23,6 +23,8 @@ import { lstat, mkdir, open, rename, unlink } from 'node:fs/promises'
23
23
  import { homedir } from 'node:os'
24
24
  import { basename, dirname, join, resolve } from 'node:path'
25
25
  import { z } from 'zod'
26
+
27
+ import type { ToolContextLike } from './delegation.ts'
26
28
  import {
27
29
  capabilityEntry,
28
30
  hasVision,
@@ -30,7 +32,6 @@ import {
30
32
  normalizeCapability,
31
33
  resolveActivePreset,
32
34
  } from './presets.mjs'
33
- import type { ToolContextLike } from './tool-context.ts'
34
35
 
35
36
  export type ModelScope = 'project' | 'global'
36
37
  export type ModelAction = 'status' | 'show' | 'set' | 'reset'
@@ -0,0 +1,284 @@
1
+ /**
2
+ * S1 — Deny-first permission evaluation + PreToolUse-style hooks.
3
+ *
4
+ * Deny rules checked BEFORE allow rules. User-level denies cannot be
5
+ * overridden by project config. Hook exit-code semantics:
6
+ * exit 0 → allow, exit 2 → hard deny, missing/throw → fail-open.
7
+ *
8
+ * @module permission-eval
9
+ */
10
+
11
+ // ─── Types ─────────────────────────────────────────────────────────────
12
+
13
+ /** Result of a permission evaluation. */
14
+ export interface PermissionResult {
15
+ allowed: boolean
16
+ reason: 'allow' | 'deny' | 'hook_deny' | 'no_match'
17
+ message?: string
18
+ }
19
+
20
+ /** Hook script result (mirrors hook-runner.ts HookResult). */
21
+ export interface HookResult {
22
+ code: number
23
+ stdout: string
24
+ stderr: string
25
+ /** When true, the hook script was not found — treat as fail-open. */
26
+ missing?: boolean
27
+ }
28
+
29
+ /**
30
+ * PreToolUse hook function signature.
31
+ * exit 0 → allow, exit 2 → hard deny, exit 1 → soft deny.
32
+ * Throw = fail-open.
33
+ */
34
+ export type PreToolUseHook = (tool: string, args: unknown) => Promise<HookResult> | HookResult
35
+
36
+ /** Permission configuration. */
37
+ export interface PermissionConfig {
38
+ /** Deny rules — evaluated first. User-level denies override project allows. */
39
+ deny: readonly string[]
40
+ /** Allow rules — evaluated after deny. */
41
+ allow: readonly string[]
42
+ /** Optional hooks configuration. */
43
+ hooks?: {
44
+ /** PreToolUse hook function (in-process, not script path). */
45
+ preToolUse?: PreToolUseHook
46
+ }
47
+ }
48
+
49
+ // ─── Pattern matching ──────────────────────────────────────────────────
50
+
51
+ /**
52
+ * Parse a permission rule pattern into { tool, argPattern }.
53
+ *
54
+ * Supported formats:
55
+ * - "tool" → tool="tool", argPattern="*"
56
+ * - "tool:*" → tool="tool", argPattern="*"
57
+ * - "tool:subcommand" → tool="tool", argPattern="subcommand"
58
+ * - "tool(pattern)" → tool="tool", argPattern="pattern" (full match)
59
+ * - "*:*" → tool="*", argPattern="*"
60
+ * - "git push:*" → tool="git push", argPattern="*" (subcommand prefix)
61
+ */
62
+ function parsePattern(pattern: string): { tool: string; argPattern: string } {
63
+ // Handle "tool(pattern)" format — the content inside parens is the full
64
+ // arg pattern. Colons inside parens are literal, not separators.
65
+ const parenMatch = /^([^(]+)\(([^)]+)\)$/.exec(pattern)
66
+ if (parenMatch) {
67
+ const tool = parenMatch[1] ?? ''
68
+ const argPattern = parenMatch[2] ?? ''
69
+ return { tool, argPattern }
70
+ }
71
+
72
+ // Handle "tool:argPattern" or just "tool"
73
+ const colonIdx = pattern.indexOf(':')
74
+ if (colonIdx === -1) {
75
+ return { tool: pattern, argPattern: '*' }
76
+ }
77
+ const argPattern = pattern.slice(colonIdx + 1)
78
+ return {
79
+ tool: pattern.slice(0, colonIdx),
80
+ // Empty argPattern after colon = wildcard (e.g. "*:" matches any args)
81
+ argPattern: argPattern === '' ? '*' : argPattern,
82
+ }
83
+ }
84
+
85
+ /**
86
+ * Convert a glob-like pattern to a regex. `*` matches any sequence.
87
+ * Anchored to full match.
88
+ */
89
+ function globToRegex(pattern: string): RegExp {
90
+ const escaped = pattern
91
+ .replace(/[.+^${}()|[\]\\]/g, '\\$&')
92
+ .replace(/\*/g, '.*')
93
+ .replace(/\?/g, '.')
94
+ return new RegExp(`^${escaped}$`)
95
+ }
96
+
97
+ /** Extract a string representation from tool args for pattern matching. */
98
+ function argsToString(args: unknown): string {
99
+ if (args === null || args === undefined) return ''
100
+ if (typeof args === 'string') return args
101
+ if (typeof args === 'object') {
102
+ const record = args as Record<string, unknown>
103
+ // Try common field names
104
+ for (const key of ['command', 'cmd', 'query', 'pattern', 'filePath', 'path', 'url']) {
105
+ if (typeof record[key] === 'string') return record[key]
106
+ }
107
+ return JSON.stringify(args)
108
+ }
109
+ return String(args)
110
+ }
111
+
112
+ /**
113
+ * Build a combined string for matching: "toolName argString".
114
+ * This allows patterns like "git push:*" to match tool="git" with args
115
+ * containing "push".
116
+ */
117
+ function combinedString(tool: string, args: unknown): string {
118
+ const argsStr = argsToString(args)
119
+ return argsStr !== '' ? `${tool} ${argsStr}` : tool
120
+ }
121
+
122
+ /**
123
+ * Check if a tool+args matches a permission rule pattern.
124
+ * Returns true when the pattern matches.
125
+ *
126
+ * Matching strategy:
127
+ * 1. Wildcard tool ("*") → match any tool with argPattern
128
+ * 2. Direct tool match → check argPattern against args
129
+ * 3. Combined match (for "git push:*" style patterns) → prefix match
130
+ */
131
+ function matchesRule(pattern: string, tool: string, args: unknown): boolean {
132
+ const { tool: patternTool, argPattern } = parsePattern(pattern)
133
+
134
+ // Wildcard tool: match any tool
135
+ if (patternTool === '*') {
136
+ if (argPattern === '*') return true
137
+ const argsStr = argsToString(args)
138
+ return globToRegex(argPattern).test(argsStr)
139
+ }
140
+
141
+ // Check direct tool match
142
+ if (globToRegex(patternTool).test(tool)) {
143
+ if (argPattern === '*') return true
144
+ const argsStr = argsToString(args)
145
+ return globToRegex(argPattern).test(argsStr)
146
+ }
147
+
148
+ // Combined match for patterns with spaces (e.g. "git push:*")
149
+ // The pattern tool includes a subcommand that appears in args.
150
+ // Use prefix matching: "git push" matches "git push origin main"
151
+ if (patternTool.includes(' ')) {
152
+ const combined = combinedString(tool, args)
153
+ // Prefix match: combined starts with pattern tool
154
+ if (combined.startsWith(patternTool)) {
155
+ return true
156
+ }
157
+ }
158
+
159
+ return false
160
+ }
161
+
162
+ // ─── Evaluation ────────────────────────────────────────────────────────
163
+
164
+ /**
165
+ * Evaluate a permission config against a tool+args invocation.
166
+ *
167
+ * Evaluation order:
168
+ * 1. Check deny rules (user-level first, then project-level)
169
+ * 2. If no deny matched, run the PreToolUse hook
170
+ * 3. If hook exits 0 (or missing), check allow rules
171
+ * 4. If no allow matched → deny (implicit deny)
172
+ *
173
+ * User-level denies override project-level allows: a deny in the user config
174
+ * cannot be cancelled by an allow in the project config.
175
+ */
176
+ export function evaluatePermission(
177
+ config: PermissionConfig,
178
+ invocation: { tool: string; args?: unknown },
179
+ ): PermissionResult {
180
+ const { tool, args } = invocation
181
+
182
+ // ── Step 1: Deny-first check ────────────────────────────────────────
183
+ for (const pattern of config.deny) {
184
+ if (matchesRule(pattern, tool, args)) {
185
+ return { allowed: false, reason: 'deny', message: `denied by rule: ${pattern}` }
186
+ }
187
+ }
188
+
189
+ // ── Step 2: PreToolUse hook ─────────────────────────────────────────
190
+ if (config.hooks?.preToolUse !== undefined) {
191
+ try {
192
+ const hookResult = config.hooks.preToolUse(tool, args)
193
+ // Support both sync and async hooks
194
+ // For sync hooks (non-Promise), check immediately
195
+ if (!(hookResult instanceof Promise)) {
196
+ // missing flag: hook script not found → fail-open
197
+ if (hookResult.missing === true) {
198
+ // Fall through to config evaluation (fail-open)
199
+ } else if (hookResult.code === 2) {
200
+ return {
201
+ allowed: false,
202
+ reason: 'hook_deny',
203
+ message: `hook hard deny (exit 2): ${hookResult.stderr}`,
204
+ }
205
+ } else if (hookResult.code !== 0) {
206
+ return {
207
+ allowed: false,
208
+ reason: 'hook_deny',
209
+ message: `hook deny (exit ${hookResult.code}): ${hookResult.stderr}`,
210
+ }
211
+ }
212
+ // Hook exited 0 (or missing) → fall through to allow check
213
+ }
214
+ // For async hooks, we cannot await in a sync function.
215
+ // Use evaluatePermissionAsync for async hooks.
216
+ } catch {
217
+ // Hook throw → fail-open: fall through to config evaluation
218
+ }
219
+ }
220
+
221
+ // ── Step 3: Allow check ─────────────────────────────────────────────
222
+ for (const pattern of config.allow) {
223
+ if (matchesRule(pattern, tool, args)) {
224
+ return { allowed: true, reason: 'allow', message: `allowed by rule: ${pattern}` }
225
+ }
226
+ }
227
+
228
+ // ── Step 4: No match → implicit deny ────────────────────────────────
229
+ return { allowed: false, reason: 'no_match', message: 'no matching allow rule' }
230
+ }
231
+
232
+ /**
233
+ * Async version of evaluatePermission that properly awaits hooks.
234
+ * Use this when hooks may be async (e.g., shell scripts).
235
+ */
236
+ export async function evaluatePermissionAsync(
237
+ config: PermissionConfig,
238
+ invocation: { tool: string; args?: unknown },
239
+ ): Promise<PermissionResult> {
240
+ const { tool, args } = invocation
241
+
242
+ // ── Step 1: Deny-first check ────────────────────────────────────────
243
+ for (const pattern of config.deny) {
244
+ if (matchesRule(pattern, tool, args)) {
245
+ return { allowed: false, reason: 'deny', message: `denied by rule: ${pattern}` }
246
+ }
247
+ }
248
+
249
+ // ── Step 2: PreToolUse hook ─────────────────────────────────────────
250
+ if (config.hooks?.preToolUse !== undefined) {
251
+ try {
252
+ const hookResult = await config.hooks.preToolUse(tool, args)
253
+ // missing flag: hook script not found → fail-open
254
+ if (hookResult.missing === true) {
255
+ // Fall through to config evaluation (fail-open)
256
+ } else if (hookResult.code === 2) {
257
+ return {
258
+ allowed: false,
259
+ reason: 'hook_deny',
260
+ message: `hook hard deny (exit 2): ${hookResult.stderr}`,
261
+ }
262
+ } else if (hookResult.code !== 0) {
263
+ return {
264
+ allowed: false,
265
+ reason: 'hook_deny',
266
+ message: `hook deny (exit ${hookResult.code}): ${hookResult.stderr}`,
267
+ }
268
+ }
269
+ // Hook exited 0 (or missing) → fall through to allow check
270
+ } catch {
271
+ // Hook throw → fail-open: fall through to config evaluation
272
+ }
273
+ }
274
+
275
+ // ── Step 3: Allow check ─────────────────────────────────────────────
276
+ for (const pattern of config.allow) {
277
+ if (matchesRule(pattern, tool, args)) {
278
+ return { allowed: true, reason: 'allow', message: `allowed by rule: ${pattern}` }
279
+ }
280
+ }
281
+
282
+ // ── Step 4: No match → implicit deny ────────────────────────────────
283
+ return { allowed: false, reason: 'no_match', message: 'no matching allow rule' }
284
+ }
@@ -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
+ }