Repository navigation
Expand file tree
/
Copy pathclass-wp-agent-iteration-budget.php
More file actions
114 lines (100 loc) · 2.57 KB
/
Copy pathclass-wp-agent-iteration-budget.php
File metadata and controls
114 lines (100 loc) · 2.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<?php
/**
* Iteration Budget — generic bounded-iteration primitive.
*
* Counts a named dimension (turns, tool calls, wall-clock seconds, retries)
* and exposes a uniform API for checking exceedance. A budget is a
* stateful value object — call {@see increment()} at each iteration,
* then {@see exceeded()} to decide whether to continue.
*
* The substrate ships only the per-execution value object. Registries,
* configuration persistence, and ceiling policies are consumer concerns.
*
* @package AgentsAPI
*/
namespace AgentsAPI\AI;
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
final class WP_Agent_Iteration_Budget {
/**
* Budget name (e.g. "turns", "tool_calls", "chain_depth").
*
* @var string
*/
private string $name;
/**
* Maximum allowed value (inclusive ceiling — exceeded when current >= ceiling).
*
* @var int
*/
private int $ceiling;
/**
* Current counter value.
*
* @var int
*/
private int $current;
/**
* @param string $name Budget name. Used in result status and event payloads.
* @param int $ceiling Maximum allowed value (must be >= 1).
* @param int $current Starting counter value (default 0). Negative values are clamped to 0.
*/
public function __construct( string $name, int $ceiling, int $current = 0 ) {
$this->name = $name;
$this->ceiling = max( 1, $ceiling );
$this->current = max( 0, $current );
}
/**
* Increment the counter by one.
*/
public function increment(): void {
++$this->current;
}
/**
* Replace the current counter value.
*
* Wall-clock budgets are measured externally by the loop, then recorded on
* this value object so event payloads and diagnostics share the same shape
* as iteration budgets.
*
* @param int $current Current counter value. Negative values are clamped to 0.
*/
public function set_current( int $current ): void {
$this->current = max( 0, $current );
}
/**
* Whether the counter has reached or exceeded the ceiling.
*
* @return bool True when current >= ceiling.
*/
public function exceeded(): bool {
return $this->current >= $this->ceiling;
}
/**
* Remaining iterations before exceedance.
*
* @return int Zero when exceeded, otherwise ceiling - current.
*/
public function remaining(): int {
return max( 0, $this->ceiling - $this->current );
}
/**
* Budget name.
*/
public function name(): string {
return $this->name;
}
/**
* Current counter value.
*/
public function current(): int {
return $this->current;
}
/**
* Ceiling for this budget.
*/
public function ceiling(): int {
return $this->ceiling;
}
}